From c269184c60e69b7aa319fedc5b4bbcb122677cb8 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 13:29:41 -0700 Subject: [PATCH] Add tests for delete predefined kit endpoint --- app/Models/PredefinedKit.php | 2 + database/factories/PredefinedKitFactory.php | 23 ++++++++ database/factories/UserFactory.php | 5 ++ .../Api/DeletePredefinedKitTest.php | 57 +++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 database/factories/PredefinedKitFactory.php create mode 100644 tests/Feature/PredefinedKits/Api/DeletePredefinedKitTest.php diff --git a/app/Models/PredefinedKit.php b/app/Models/PredefinedKit.php index 1bf6cb098b..f9464e12c7 100644 --- a/app/Models/PredefinedKit.php +++ b/app/Models/PredefinedKit.php @@ -4,6 +4,7 @@ namespace App\Models; use App\Models\Traits\Searchable; use App\Presenters\Presentable; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Validation\Rule; use Watson\Validating\ValidatingTrait; @@ -16,6 +17,7 @@ use Watson\Validating\ValidatingTrait; class PredefinedKit extends SnipeModel { protected $presenter = \App\Presenters\PredefinedKitPresenter::class; + use HasFactory; use Presentable; protected $table = 'kits'; diff --git a/database/factories/PredefinedKitFactory.php b/database/factories/PredefinedKitFactory.php new file mode 100644 index 0000000000..32e192655f --- /dev/null +++ b/database/factories/PredefinedKitFactory.php @@ -0,0 +1,23 @@ + + */ +class PredefinedKitFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'name' => $this->faker->words(3, true), + ]; + } +} diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 9f45071538..288949199c 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -351,6 +351,11 @@ class UserFactory extends Factory return $this->appendPermission(['manufacturers.delete' => '1']); } + public function deletePredefinedKits() + { + return $this->appendPermission(['kits.delete' => '1']); + } + private function appendPermission(array $permission) { return $this->state(function ($currentState) use ($permission) { diff --git a/tests/Feature/PredefinedKits/Api/DeletePredefinedKitTest.php b/tests/Feature/PredefinedKits/Api/DeletePredefinedKitTest.php new file mode 100644 index 0000000000..e5eb544149 --- /dev/null +++ b/tests/Feature/PredefinedKits/Api/DeletePredefinedKitTest.php @@ -0,0 +1,57 @@ +create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.kits.destroy', $predefinedKit)) + ->assertForbidden(); + } + + public function testCanDeletePredefinedKits() + { + $predefinedKit = PredefinedKit::factory()->create(); + + $this->actingAsForApi(User::factory()->deletePredefinedKits()->create()) + ->deleteJson(route('api.kits.destroy', $predefinedKit)) + ->assertOk() + ->assertStatusMessageIs('success'); + + $this->assertDatabaseMissing('kits', ['id' => $predefinedKit->id]); + } + + public function testAssociatedDataDetachedWhenPredefinedKitDeleted() + { + $predefinedKit = PredefinedKit::factory() + ->hasAccessories() + ->hasConsumables() + ->hasLicenses() + ->hasModels() + ->create(); + + $this->assertGreaterThan(0, $predefinedKit->accessories->count(), 'Precondition failed: PredefinedKit has no accessories'); + $this->assertGreaterThan(0, $predefinedKit->consumables->count(), 'Precondition failed: PredefinedKit has no consumables'); + $this->assertGreaterThan(0, $predefinedKit->licenses->count(), 'Precondition failed: PredefinedKit has no licenses'); + $this->assertGreaterThan(0, $predefinedKit->models->count(), 'Precondition failed: PredefinedKit has no models'); + + $this->actingAsForApi(User::factory()->deletePredefinedKits()->create()) + ->deleteJson(route('api.kits.destroy', $predefinedKit)) + ->assertStatusMessageIs('success'); + + $this->assertEquals(0, DB::table('kits_accessories')->where('kit_id', $predefinedKit->id)->count()); + $this->assertEquals(0, DB::table('kits_consumables')->where('kit_id', $predefinedKit->id)->count()); + $this->assertEquals(0, DB::table('kits_licenses')->where('kit_id', $predefinedKit->id)->count()); + $this->assertEquals(0, DB::table('kits_models')->where('kit_id', $predefinedKit->id)->count()); + } +}