From e96b9b2f4f5d1e05ef4408ccd89591e01d2ed1f3 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 16 Aug 2024 23:04:11 +0100 Subject: [PATCH] Added test for models with same name but no model number Signed-off-by: snipe --- .../AssetModels/Api/CreateAssetModelsTest.php | 21 +++++++++++++++++++ .../AssetModels/Ui/CreateAssetModelsTest.php | 20 ++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/tests/Feature/AssetModels/Api/CreateAssetModelsTest.php b/tests/Feature/AssetModels/Api/CreateAssetModelsTest.php index 02d6bd5d66..3879436b79 100644 --- a/tests/Feature/AssetModels/Api/CreateAssetModelsTest.php +++ b/tests/Feature/AssetModels/Api/CreateAssetModelsTest.php @@ -79,4 +79,25 @@ class CreateAssetModelsTest extends TestCase } + public function testUniquenessAcrossModelNameAndModelNumberWithBlankModelNumber() + { + AssetModel::factory()->create(['name' => 'Test Model']); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->postJson(route('api.models.store'), [ + 'name' => 'Test Model', + ]) + ->assertStatus(200) + ->assertOk() + ->assertStatusMessageIs('error') + ->assertJson([ + 'messages' => [ + 'name' => ['The name must be unique across models and model number. '], + 'model_number' => ['The model number must be unique across models and name. '], + ], + ]) + ->json(); + + } + } diff --git a/tests/Feature/AssetModels/Ui/CreateAssetModelsTest.php b/tests/Feature/AssetModels/Ui/CreateAssetModelsTest.php index 9e9a8a52dc..2722e8a34d 100644 --- a/tests/Feature/AssetModels/Ui/CreateAssetModelsTest.php +++ b/tests/Feature/AssetModels/Ui/CreateAssetModelsTest.php @@ -65,7 +65,27 @@ class CreateAssetModelsTest extends TestCase ]) ->assertStatus(302) ->assertSessionHasErrors(['name','model_number']) + ->assertRedirect(route('models.create')) + ->assertInvalid(['name','model_number']); + + $this->followRedirects($response)->assertSee(trans('general.error')); + + } + + public function testUniquenessAcrossModelNameAndModelNumberWithoutModelNumber() + { + + AssetModel::factory()->create(['name' => 'Test Model', 'model_number'=> null]); + + $response = $this->actingAs(User::factory()->superuser()->create()) + ->from(route('models.create')) + ->post(route('models.store'), [ + 'name' => 'Test Model', + 'model_number' => null, + 'category_id' => Category::factory()->create()->id + ]) ->assertStatus(302) + ->assertSessionHasErrors(['name','model_number']) ->assertRedirect(route('models.create')) ->assertInvalid(['name','model_number']);