Skip to content

A free open source IT asset/license management system

Improve model factories

Marcus Mooree16c04250e0d
diff --git a/database/factories/AssetFactory.php b/database/factories/AssetFactory.php
index 18fef95273..0916fea17d 100644
--- a/database/factories/AssetFactory.php
+++ b/database/factories/AssetFactory.php
@@ -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),
+ ];
});
}
diff --git a/database/factories/AssetModelFactory.php b/database/factories/AssetModelFactory.php
index 2d14db9aa8..ed3d478261 100644
--- a/database/factories/AssetModelFactory.php
+++ b/database/factories/AssetModelFactory.php
@@ -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),
];
});
}
-
}
diff --git a/database/factories/CustomFieldsetFactory.php b/database/factories/CustomFieldsetFactory.php
index f0ace3f539..9a410ba25f 100644
--- a/database/factories/CustomFieldsetFactory.php
+++ b/database/factories/CustomFieldsetFactory.php
@@ -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]);
});
}
}
diff --git a/tests/Feature/Api/Assets/AssetStoreTest.php b/tests/Feature/Api/Assets/AssetStoreTest.php
index 57563c432d..36678fe05a 100644
--- a/tests/Feature/Api/Assets/AssetStoreTest.php
+++ b/tests/Feature/Api/Assets/AssetStoreTest.php
@@ -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
diff --git a/tests/Feature/Api/Assets/AssetUpdateTest.php b/tests/Feature/Api/Assets/AssetUpdateTest.php
index 593bd9b020..05adbb9fff 100644
--- a/tests/Feature/Api/Assets/AssetUpdateTest.php
+++ b/tests/Feature/Api/Assets/AssetUpdateTest.php
@@ -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");