diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 288949199c..dd92f38f67 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -356,6 +356,11 @@ class UserFactory extends Factory return $this->appendPermission(['kits.delete' => '1']); } + public function deleteStatusLabels() + { + return $this->appendPermission(['statuslabels.delete' => '1']); + } + private function appendPermission(array $permission) { return $this->state(function ($currentState) use ($permission) { diff --git a/tests/Feature/StatusLabels/Api/DeleteStatusLabelTest.php b/tests/Feature/StatusLabels/Api/DeleteStatusLabelTest.php new file mode 100644 index 0000000000..b7e92dcf22 --- /dev/null +++ b/tests/Feature/StatusLabels/Api/DeleteStatusLabelTest.php @@ -0,0 +1,45 @@ +create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.statuslabels.destroy', $statusLabel)) + ->assertForbidden(); + } + + public function testCannotDeleteStatusLabelWhileStillAssociatedToAssets() + { + $statusLabel = Statuslabel::factory()->hasAssets()->create(); + + $this->assertGreaterThan(0, $statusLabel->assets->count(), 'Precondition failed: StatusLabel has no assets'); + + $this->actingAsForApi(User::factory()->deleteStatusLabels()->create()) + ->deleteJson(route('api.statuslabels.destroy', $statusLabel)) + ->assertStatusMessageIs('error'); + + $this->assertNotSoftDeleted($statusLabel); + } + + public function testCanDeleteStatusLabel() + { + $statusLabel = Statuslabel::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteStatusLabels()->create()) + ->deleteJson(route('api.statuslabels.destroy', $statusLabel)) + ->assertOk() + ->assertStatusMessageIs('success'); + + $this->assertSoftDeleted($statusLabel); + } +}