Skip to content

Commit 8ce2512

Browse files
committed
Add tests for delete category endpoint
1 parent 0ec415d commit 8ce2512

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

database/factories/UserFactory.php

+5
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,11 @@ public function deleteUsers()
296296
return $this->appendPermission(['users.delete' => '1']);
297297
}
298298

299+
public function deleteCategories()
300+
{
301+
return $this->appendPermission(['categories.delete' => '1']);
302+
}
303+
299304
public function canEditOwnLocation()
300305
{
301306
return $this->appendPermission(['self.edit_location' => '1']);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Tests\Feature\Categories\Api;
4+
5+
use App\Models\Asset;
6+
use App\Models\Category;
7+
use App\Models\User;
8+
use Tests\Concerns\TestsPermissionsRequirement;
9+
use Tests\TestCase;
10+
11+
class DeleteCategoriesTest extends TestCase implements TestsPermissionsRequirement
12+
{
13+
public function testRequiresPermission()
14+
{
15+
$category = Category::factory()->create();
16+
17+
$this->actingAsForApi(User::factory()->create())
18+
->deleteJson(route('api.categories.destroy', $category))
19+
->assertForbidden();
20+
}
21+
22+
public function testCanDeleteCategory()
23+
{
24+
$category = Category::factory()->create();
25+
26+
$this->actingAsForApi(User::factory()->deleteCategories()->create())
27+
->deleteJson(route('api.categories.destroy', $category))
28+
->assertStatusMessageIs('success');
29+
30+
$this->assertTrue($category->fresh()->trashed());
31+
}
32+
33+
public function testCannotDeleteCategoryThatStillHasAssociatedItems()
34+
{
35+
$asset = Asset::factory()->create();
36+
$category = $asset->model->category;
37+
38+
$this->actingAsForApi(User::factory()->deleteCategories()->create())
39+
->deleteJson(route('api.categories.destroy', $category))
40+
->assertStatusMessageIs('error');
41+
42+
$this->assertFalse($category->fresh()->trashed());
43+
}
44+
}

0 commit comments

Comments
 (0)