mirror of
https://github.com/snipe/snipe-it.git
synced 2025-01-12 06:17:28 -08:00
Improve model factories
This commit is contained in:
parent
ad99aa460b
commit
e16c04250e
|
@ -4,6 +4,7 @@ namespace Database\Factories;
|
|||
|
||||
use App\Models\Asset;
|
||||
use App\Models\AssetModel;
|
||||
use App\Models\CustomField;
|
||||
use App\Models\Location;
|
||||
use App\Models\Statuslabel;
|
||||
use App\Models\Supplier;
|
||||
|
@ -353,10 +354,14 @@ class AssetFactory extends Factory
|
|||
return $this->state(['requestable' => false]);
|
||||
}
|
||||
|
||||
public function hasEncryptedCustomField()
|
||||
public function hasEncryptedCustomField(CustomField $field = null)
|
||||
{
|
||||
return $this->afterMaking(function (Asset $asset) {
|
||||
$asset->model_id = AssetModel::factory()->withEncryptedField()->create()->id;
|
||||
// @todo: update this so existing asset model is used if present on the asset
|
||||
// (may have been created in a test case)
|
||||
return $this->state(function () use ($field) {
|
||||
return [
|
||||
'model_id' => AssetModel::factory()->hasEncryptedCustomField($field),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -431,16 +431,12 @@ class AssetModelFactory extends Factory
|
|||
});
|
||||
}
|
||||
|
||||
public function withEncryptedField()
|
||||
public function hasEncryptedCustomField(CustomField $field = null)
|
||||
{
|
||||
return $this->state(function () {
|
||||
$field = CustomField::factory()->testEncrypted()->create(); // TODO - having to create and then 'find' the thing you just created is WEIRD
|
||||
return $this->state(function () use ($field) {
|
||||
return [
|
||||
'fieldset_id' => function () use ($field) {
|
||||
return CustomFieldset::where('name', 'Has Encrypted Custom Field')->first() ?? CustomFieldset::factory()->has_encrypted_custom_field()->hasAttached(CustomField::where('name', 'Test Encrypted')->first(), ['order' => 1, 'required' => 0], 'fields');
|
||||
},
|
||||
'fieldset_id' => CustomFieldset::factory()->hasEncryptedCustomField($field),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -45,12 +45,12 @@ class CustomFieldsetFactory extends Factory
|
|||
});
|
||||
}
|
||||
|
||||
public function has_encrypted_custom_field()
|
||||
public function hasEncryptedCustomField(CustomField $field = null)
|
||||
{
|
||||
return $this->state(function () {
|
||||
return [
|
||||
'name' => 'Has Encrypted Custom Field',
|
||||
];
|
||||
return $this->afterCreating(function (CustomFieldset $fieldset) use ($field) {
|
||||
$field = $field ?? CustomField::factory()->testEncrypted()->create();
|
||||
|
||||
$fieldset->fields()->attach($field, ['order' => '1', 'required' => false]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -485,7 +485,7 @@ class AssetStoreTest extends TestCase
|
|||
public function testEncryptedCustomFieldCanBeStored()
|
||||
{
|
||||
$field = CustomField::factory()->testEncrypted()->create();
|
||||
$asset = Asset::factory()->hasEncryptedCustomField()->create();
|
||||
$asset = Asset::factory()->hasEncryptedCustomField($field)->create();
|
||||
$superuser = User::factory()->superuser()->create();
|
||||
|
||||
//first, test that an Admin user can save the encrypted custom field
|
||||
|
|
|
@ -12,7 +12,7 @@ class AssetUpdateTest extends TestCase
|
|||
public function testEncryptedCustomFieldCanBeUpdated()
|
||||
{
|
||||
$field = CustomField::factory()->testEncrypted()->create();
|
||||
$asset = Asset::factory()->hasEncryptedCustomField()->create();
|
||||
$asset = Asset::factory()->hasEncryptedCustomField($field)->create();
|
||||
$superuser = User::factory()->superuser()->create();
|
||||
|
||||
//first, test that an Admin user can save the encrypted custom field
|
||||
|
@ -30,7 +30,7 @@ class AssetUpdateTest extends TestCase
|
|||
public function testPermissionNeededToUpdateEncryptedField()
|
||||
{
|
||||
$field = CustomField::factory()->testEncrypted()->create();
|
||||
$asset = Asset::factory()->hasEncryptedCustomField()->create();
|
||||
$asset = Asset::factory()->hasEncryptedCustomField($field)->create();
|
||||
$normal_user = User::factory()->editAssets()->create();
|
||||
|
||||
$asset->{$field->db_column_name()} = \Crypt::encrypt("encrypted value should not change");
|
||||
|
|
Loading…
Reference in a new issue