diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 8b110b23d6..a41813d210 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -296,6 +296,11 @@ class UserFactory extends Factory return $this->appendPermission(['users.delete' => '1']); } + public function deleteCategories() + { + return $this->appendPermission(['categories.delete' => '1']); + } + public function canEditOwnLocation() { return $this->appendPermission(['self.edit_location' => '1']); diff --git a/tests/Feature/Categories/Api/DeleteCategoriesTest.php b/tests/Feature/Categories/Api/DeleteCategoriesTest.php new file mode 100644 index 0000000000..105f6f3948 --- /dev/null +++ b/tests/Feature/Categories/Api/DeleteCategoriesTest.php @@ -0,0 +1,44 @@ +create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.categories.destroy', $category)) + ->assertForbidden(); + } + + public function testCanDeleteCategory() + { + $category = Category::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteCategories()->create()) + ->deleteJson(route('api.categories.destroy', $category)) + ->assertStatusMessageIs('success'); + + $this->assertTrue($category->fresh()->trashed()); + } + + public function testCannotDeleteCategoryThatStillHasAssociatedItems() + { + $asset = Asset::factory()->create(); + $category = $asset->model->category; + + $this->actingAsForApi(User::factory()->deleteCategories()->create()) + ->deleteJson(route('api.categories.destroy', $category)) + ->assertStatusMessageIs('error'); + + $this->assertFalse($category->fresh()->trashed()); + } +}