diff --git a/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php b/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php index 423eaad574..9a58eb5c5b 100644 --- a/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php +++ b/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php @@ -4,6 +4,8 @@ namespace Tests\Feature\AssetModels\Ui; use App\Models\AssetModel; use App\Models\Category; +use App\Models\CustomField; +use App\Models\CustomFieldset; use App\Models\User; use Tests\TestCase; @@ -19,7 +21,7 @@ class UpdateAssetModelsTest extends TestCase ->assertStatus(403) ->assertForbidden(); } - + public function testUserCanEditAssetModels() { $category = Category::factory()->forAssets()->create(); @@ -62,4 +64,41 @@ class UpdateAssetModelsTest extends TestCase } + public function test_default_values_remain_unchanged_after_validation_error_occurs() + { + $this->markIncompleteIfMySQL('Custom Field Tests do not work in MySQL'); + + $assetModel = AssetModel::factory()->create(); + + $customFieldset = CustomFieldset::factory()->create(); + + [$customFieldOne, $customFieldTwo] = CustomField::factory()->count(2)->create(); + + $customFieldset->fields()->attach($customFieldOne, ['order' => 1, 'required' => false]); + $customFieldset->fields()->attach($customFieldTwo, ['order' => 2, 'required' => false]); + + $assetModel->fieldset()->associate($customFieldset); + + $assetModel->defaultValues()->attach($customFieldOne, ['default_value' => 'first default value']); + $assetModel->defaultValues()->attach($customFieldTwo, ['default_value' => 'second default value']); + + $this->actingAs(User::factory()->superuser()->create()) + ->put(route('models.update', ['model' => $assetModel]), [ + // should trigger validation error without name, etc, and NOT remove or change default values + 'add_default_values' => '1', + 'fieldset_id' => $customFieldset->id, + 'default_values' => [ + $customFieldOne->id => 'changed value', + $customFieldTwo->id => 'changed value', + ], + ]); + + $this->assertEquals( + 2, + $assetModel->fresh()->defaultValues->filter(function (CustomField $field) { + return in_array($field->pivot->default_value, ['first default value', 'second default value']); + })->count(), + 'Default field values were changed unexpectedly.' + ); + } }