From 53c673dee21e9f8cb00904950ec66cb05bde6c55 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 13:13:12 -0700 Subject: [PATCH] Add tests for delete manufacturer endpoint --- database/factories/UserFactory.php | 5 ++ .../Api/DeleteManufacturerTest.php | 64 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 tests/Feature/Manufacturers/Api/DeleteManufacturerTest.php diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 747aef0164..9f45071538 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -346,6 +346,11 @@ class UserFactory extends Factory return $this->appendPermission(['depreciations.delete' => '1']); } + public function deleteManufacturers() + { + return $this->appendPermission(['manufacturers.delete' => '1']); + } + private function appendPermission(array $permission) { return $this->state(function ($currentState) use ($permission) { diff --git a/tests/Feature/Manufacturers/Api/DeleteManufacturerTest.php b/tests/Feature/Manufacturers/Api/DeleteManufacturerTest.php new file mode 100644 index 0000000000..70495e62e3 --- /dev/null +++ b/tests/Feature/Manufacturers/Api/DeleteManufacturerTest.php @@ -0,0 +1,64 @@ +create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.manufacturers.destroy', $manufacturer)) + ->assertForbidden(); + + $this->assertNotSoftDeleted($manufacturer); + } + + public function testCannotDeleteManufacturerWithAssociatedData() + { + $manufacturerWithAccessories = Manufacturer::factory()->hasAccessories()->create(); + $manufacturerWithConsumables = Manufacturer::factory()->hasConsumables()->create(); + $manufacturerWithLicenses = Manufacturer::factory()->hasLicenses()->create(); + + $manufacturerWithAssets = Manufacturer::factory()->hasAssets()->create(); + $model = AssetModel::factory()->create(['manufacturer_id' => $manufacturerWithAssets->id]); + Asset::factory()->create(['model_id' => $model->id]); + + $this->assertGreaterThan(0, $manufacturerWithAccessories->accessories->count(), 'Precondition failed: Manufacturer has no accessories'); + $this->assertGreaterThan(0, $manufacturerWithAssets->assets->count(), 'Precondition failed: Manufacturer has no assets'); + $this->assertGreaterThan(0, $manufacturerWithConsumables->consumables->count(), 'Precondition failed: Manufacturer has no consumables'); + $this->assertGreaterThan(0, $manufacturerWithLicenses->licenses->count(), 'Precondition failed: Manufacturer has no licenses'); + + $actor = $this->actingAsForApi(User::factory()->deleteManufacturers()->create()); + + $actor->deleteJson(route('api.manufacturers.destroy', $manufacturerWithAccessories))->assertStatusMessageIs('error'); + $actor->deleteJson(route('api.manufacturers.destroy', $manufacturerWithAssets))->assertStatusMessageIs('error'); + $actor->deleteJson(route('api.manufacturers.destroy', $manufacturerWithConsumables))->assertStatusMessageIs('error'); + $actor->deleteJson(route('api.manufacturers.destroy', $manufacturerWithLicenses))->assertStatusMessageIs('error'); + + $this->assertNotSoftDeleted($manufacturerWithAssets); + $this->assertNotSoftDeleted($manufacturerWithAccessories); + $this->assertNotSoftDeleted($manufacturerWithConsumables); + $this->assertNotSoftDeleted($manufacturerWithLicenses); + } + + public function testCanDeleteManufacturer() + { + $manufacturer = Manufacturer::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteManufacturers()->create()) + ->deleteJson(route('api.manufacturers.destroy', $manufacturer)) + ->assertOk() + ->assertStatusMessageIs('success'); + + $this->assertSoftDeleted($manufacturer); + } +}