From c7ae9d9dfa76b0efadf47695492761a8120439bf Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 12 Sep 2024 12:58:47 -0700 Subject: [PATCH 01/32] Add tests for delete accessory endpoint --- .../TestsMultipleFullCompanySupport.php | 8 +++ .../Accessories/Api/DeleteAccessoryTest.php | 57 ++++++++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 tests/Concerns/TestsMultipleFullCompanySupport.php diff --git a/tests/Concerns/TestsMultipleFullCompanySupport.php b/tests/Concerns/TestsMultipleFullCompanySupport.php new file mode 100644 index 0000000000..7c62b90782 --- /dev/null +++ b/tests/Concerns/TestsMultipleFullCompanySupport.php @@ -0,0 +1,8 @@ +deleteJson(route('api.accessories.destroy', $accessory)) ->assertForbidden(); } + + public function testCanDeleteAccessory() + { + $accessory = Accessory::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteAccessories()->create()) + ->deleteJson(route('api.accessories.destroy', $accessory)) + ->assertStatusMessageIs('success'); + + $this->assertTrue($accessory->fresh()->trashed()); + } + + public function testCannotDeleteAccessoryThatHasCheckouts() + { + $accessory = Accessory::factory()->checkedOutToUser()->create(); + + $this->actingAsForApi(User::factory()->deleteAccessories()->create()) + ->deleteJson(route('api.accessories.destroy', $accessory)) + ->assertStatusMessageIs('error'); + + $this->assertFalse($accessory->fresh()->trashed()); + } + + public function testAdheresToMultipleFullCompanySupportScoping() + { + [$companyA, $companyB] = Company::factory()->count(2)->create(); + + $accessoryA = Accessory::factory()->for($companyA)->create(); + $accessoryB = Accessory::factory()->for($companyB)->create(); + $accessoryC = Accessory::factory()->for($companyB)->create(); + + $superUser = $companyA->users()->save(User::factory()->superuser()->make()); + $userInCompanyA = $companyA->users()->save(User::factory()->deleteAccessories()->make()); + $userInCompanyB = $companyB->users()->save(User::factory()->deleteAccessories()->make()); + + $this->settings->enableMultipleFullCompanySupport(); + + $this->actingAsForApi($userInCompanyA) + ->deleteJson(route('api.accessories.destroy', $accessoryB)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($userInCompanyB) + ->deleteJson(route('api.accessories.destroy', $accessoryA)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($superUser) + ->deleteJson(route('api.accessories.destroy', $accessoryC)) + ->assertStatusMessageIs('success'); + + $this->assertNull($accessoryA->fresh()->deleted_at, 'Accessory unexpectedly deleted'); + $this->assertNull($accessoryB->fresh()->deleted_at, 'Accessory unexpectedly deleted'); + $this->assertNotNull($accessoryC->fresh()->deleted_at, 'Accessory was not deleted'); + } } From 5c2660bd34873b3b9e7ef89990ea450f92ded381 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 12 Sep 2024 13:02:32 -0700 Subject: [PATCH 02/32] Introduce interface --- tests/Concerns/TestsPermissionsRequirement.php | 8 ++++++++ tests/Feature/Accessories/Api/DeleteAccessoryTest.php | 5 +++-- 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 tests/Concerns/TestsPermissionsRequirement.php diff --git a/tests/Concerns/TestsPermissionsRequirement.php b/tests/Concerns/TestsPermissionsRequirement.php new file mode 100644 index 0000000000..2066abf651 --- /dev/null +++ b/tests/Concerns/TestsPermissionsRequirement.php @@ -0,0 +1,8 @@ +create(); From 275cf4630e6461ba2b22c846118ac1e2f352e989 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 12 Sep 2024 13:16:37 -0700 Subject: [PATCH 03/32] Add tests for delete asset endpoint --- tests/Feature/Assets/Api/DeleteAssetTest.php | 69 ++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 tests/Feature/Assets/Api/DeleteAssetTest.php diff --git a/tests/Feature/Assets/Api/DeleteAssetTest.php b/tests/Feature/Assets/Api/DeleteAssetTest.php new file mode 100644 index 0000000000..c834ae7db4 --- /dev/null +++ b/tests/Feature/Assets/Api/DeleteAssetTest.php @@ -0,0 +1,69 @@ +create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.assets.destroy', $asset)) + ->assertForbidden(); + } + + public function testCanDeleteAsset() + { + $asset = Asset::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteAssets()->create()) + ->deleteJson(route('api.assets.destroy', $asset)) + ->assertStatusMessageIs('success'); + + $this->assertTrue($asset->fresh()->trashed()); + } + + public function testCannotDeleteAssetThatIsCheckedOut() + { + $this->markTestSkipped('This behavior is not functioning yet.'); + } + + public function testAdheresToMultipleFullCompanySupportScoping() + { + [$companyA, $companyB] = Company::factory()->count(2)->create(); + + $assetA = Asset::factory()->for($companyA)->create(); + $assetB = Asset::factory()->for($companyB)->create(); + $assetC = Asset::factory()->for($companyB)->create(); + + $superUser = $companyA->users()->save(User::factory()->superuser()->make()); + $userInCompanyA = $companyA->users()->save(User::factory()->deleteAssets()->make()); + $userInCompanyB = $companyB->users()->save(User::factory()->deleteAssets()->make()); + + $this->settings->enableMultipleFullCompanySupport(); + + $this->actingAsForApi($userInCompanyA) + ->deleteJson(route('api.assets.destroy', $assetB)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($userInCompanyB) + ->deleteJson(route('api.assets.destroy', $assetA)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($superUser) + ->deleteJson(route('api.assets.destroy', $assetC)) + ->assertStatusMessageIs('success'); + + $this->assertNull($assetA->fresh()->deleted_at, 'Asset unexpectedly deleted'); + $this->assertNull($assetB->fresh()->deleted_at, 'Asset unexpectedly deleted'); + $this->assertNotNull($assetC->fresh()->deleted_at, 'Asset was not deleted'); + } +} From 872b76b45fbdd41cb6e5ae149b158c83a97a6865 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 12 Sep 2024 13:29:10 -0700 Subject: [PATCH 04/32] Add tests for delete asset maintenance endpoint --- .../Api/DeleteAssetMaintenanceTest.php | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenanceTest.php diff --git a/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenanceTest.php b/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenanceTest.php new file mode 100644 index 0000000000..702f7fd2b3 --- /dev/null +++ b/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenanceTest.php @@ -0,0 +1,68 @@ +create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.maintenances.destroy', $assetMaintenance)) + ->assertForbidden(); + } + + public function testCanDeleteAssetMaintenance() + { + $assetMaintenance = AssetMaintenance::factory()->create(); + + $this->actingAsForApi(User::factory()->editAssets()->create()) + ->deleteJson(route('api.maintenances.destroy', $assetMaintenance)) + ->assertStatusMessageIs('success'); + + $this->assertTrue($assetMaintenance->fresh()->trashed()); + } + + public function testAdheresToMultipleFullCompanySupportScoping() + { + [$companyA, $companyB] = Company::factory()->count(2)->create(); + + $assetMaintenanceA = AssetMaintenance::factory()->create(); + $assetMaintenanceB = AssetMaintenance::factory()->create(); + $assetMaintenanceC = AssetMaintenance::factory()->create(); + + $assetMaintenanceA->asset->update(['company_id' => $companyA->id]); + $assetMaintenanceB->asset->update(['company_id' => $companyB->id]); + $assetMaintenanceC->asset->update(['company_id' => $companyB->id]); + + $superUser = $companyA->users()->save(User::factory()->superuser()->make()); + $userInCompanyA = $companyA->users()->save(User::factory()->editAssets()->make()); + $userInCompanyB = $companyB->users()->save(User::factory()->editAssets()->make()); + + $this->settings->enableMultipleFullCompanySupport(); + + $this->actingAsForApi($userInCompanyA) + ->deleteJson(route('api.maintenances.destroy', $assetMaintenanceB)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($userInCompanyB) + ->deleteJson(route('api.maintenances.destroy', $assetMaintenanceA)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($superUser) + ->deleteJson(route('api.maintenances.destroy', $assetMaintenanceC)) + ->assertStatusMessageIs('success'); + + $this->assertNull($assetMaintenanceA->fresh()->deleted_at, 'Asset Maintenance unexpectedly deleted'); + $this->assertNull($assetMaintenanceB->fresh()->deleted_at, 'Asset Maintenance unexpectedly deleted'); + $this->assertNotNull($assetMaintenanceC->fresh()->deleted_at, 'Asset Maintenance was not deleted'); + } +} From 5299b3e9f066e1779312798ecb150786152cf254 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 12 Sep 2024 13:29:44 -0700 Subject: [PATCH 05/32] Remove code handled by CompanyableChildTrait --- app/Http/Controllers/Api/AssetMaintenancesController.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/Http/Controllers/Api/AssetMaintenancesController.php b/app/Http/Controllers/Api/AssetMaintenancesController.php index ac247a8873..e12dc826a9 100644 --- a/app/Http/Controllers/Api/AssetMaintenancesController.php +++ b/app/Http/Controllers/Api/AssetMaintenancesController.php @@ -188,10 +188,6 @@ class AssetMaintenancesController extends Controller // Check if the asset maintenance exists $assetMaintenance = AssetMaintenance::findOrFail($assetMaintenanceId); - if (! Company::isCurrentUserHasAccess($assetMaintenance->asset)) { - return response()->json(Helper::formatStandardApiResponse('error', null, 'You cannot delete a maintenance for that asset')); - } - $assetMaintenance->delete(); return response()->json(Helper::formatStandardApiResponse('success', $assetMaintenance, trans('admin/asset_maintenances/message.delete.success'))); From b336c6273d57d6ca3181877ba844377cad5c6963 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 12 Sep 2024 13:32:42 -0700 Subject: [PATCH 06/32] Pluralize test classes --- .../Api/{DeleteAccessoryTest.php => DeleteAccessoriesTest.php} | 2 +- ...AssetMaintenanceTest.php => DeleteAssetMaintenancesTest.php} | 2 +- .../Assets/Api/{DeleteAssetTest.php => DeleteAssetsTest.php} | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename tests/Feature/Accessories/Api/{DeleteAccessoryTest.php => DeleteAccessoriesTest.php} (95%) rename tests/Feature/AssetMaintenances/Api/{DeleteAssetMaintenanceTest.php => DeleteAssetMaintenancesTest.php} (95%) rename tests/Feature/Assets/Api/{DeleteAssetTest.php => DeleteAssetsTest.php} (95%) diff --git a/tests/Feature/Accessories/Api/DeleteAccessoryTest.php b/tests/Feature/Accessories/Api/DeleteAccessoriesTest.php similarity index 95% rename from tests/Feature/Accessories/Api/DeleteAccessoryTest.php rename to tests/Feature/Accessories/Api/DeleteAccessoriesTest.php index 79ff609c35..6fa4a78160 100644 --- a/tests/Feature/Accessories/Api/DeleteAccessoryTest.php +++ b/tests/Feature/Accessories/Api/DeleteAccessoriesTest.php @@ -9,7 +9,7 @@ use Tests\Concerns\TestsMultipleFullCompanySupport; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteAccessoryTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement +class DeleteAccessoriesTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement { public function testRequiresPermission() { diff --git a/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenanceTest.php b/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenancesTest.php similarity index 95% rename from tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenanceTest.php rename to tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenancesTest.php index 702f7fd2b3..9532fdcf22 100644 --- a/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenanceTest.php +++ b/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenancesTest.php @@ -9,7 +9,7 @@ use Tests\Concerns\TestsMultipleFullCompanySupport; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteAssetMaintenanceTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement +class DeleteAssetMaintenancesTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement { public function testRequiresPermission() { diff --git a/tests/Feature/Assets/Api/DeleteAssetTest.php b/tests/Feature/Assets/Api/DeleteAssetsTest.php similarity index 95% rename from tests/Feature/Assets/Api/DeleteAssetTest.php rename to tests/Feature/Assets/Api/DeleteAssetsTest.php index c834ae7db4..3d34422b9d 100644 --- a/tests/Feature/Assets/Api/DeleteAssetTest.php +++ b/tests/Feature/Assets/Api/DeleteAssetsTest.php @@ -9,7 +9,7 @@ use Tests\Concerns\TestsMultipleFullCompanySupport; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteAssetTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement +class DeleteAssetsTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement { public function testRequiresPermission() { From 2044570e95789a92ef705d02ca241216eb19d1e3 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 12 Sep 2024 13:39:36 -0700 Subject: [PATCH 07/32] Add tests for delete asset model endpoint --- database/factories/UserFactory.php | 5 ++ .../AssetModels/Api/DeleteAssetModelsTest.php | 51 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 746d88a589..8b110b23d6 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -141,6 +141,11 @@ class UserFactory extends Factory return $this->appendPermission(['assets.view.requestable' => '1']); } + public function deleteAssetModels() + { + return $this->appendPermission(['models.delete' => '1']); + } + public function viewAccessories() { return $this->appendPermission(['accessories.view' => '1']); diff --git a/tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php b/tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php new file mode 100644 index 0000000000..6389b8f2aa --- /dev/null +++ b/tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php @@ -0,0 +1,51 @@ +create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.models.destroy', $assetModel)) + ->assertForbidden(); + } + + public function testCanDeleteAssetModel() + { + $assetModel = AssetModel::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteAssetModels()->create()) + ->deleteJson(route('api.models.destroy', $assetModel)) + ->assertStatusMessageIs('success'); + + $this->assertTrue($assetModel->fresh()->trashed()); + } + + public function testCannotDeleteAssetModelThatStillHasAssociatedAssets() + { + $asset = Asset::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteAssetModels()->create()) + ->deleteJson(route('api.models.destroy', $asset->model)) + ->assertStatusMessageIs('error'); + + $this->assertFalse($asset->model->fresh()->trashed()); + } + + public function testAdheresToMultipleFullCompanySupportScoping() + { + // TODO: Implement testAdheresToMultipleFullCompanySupportScoping() method. + + $this->markTestIncomplete(); + } +} From 0ec415d4d018c0dfb5cd51f9abf01eaf62c4f0d3 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 12 Sep 2024 13:46:22 -0700 Subject: [PATCH 08/32] Clean up --- .../AssetModels/Api/DeleteAssetModelsTest.php | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php b/tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php index 6389b8f2aa..7bc18e95f2 100644 --- a/tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php +++ b/tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php @@ -5,11 +5,10 @@ namespace Tests\Feature\AssetModels\Api; use App\Models\Asset; use App\Models\AssetModel; use App\Models\User; -use Tests\Concerns\TestsMultipleFullCompanySupport; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteAssetModelsTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement +class DeleteAssetModelsTest extends TestCase implements TestsPermissionsRequirement { public function testRequiresPermission() { @@ -33,19 +32,12 @@ class DeleteAssetModelsTest extends TestCase implements TestsMultipleFullCompany public function testCannotDeleteAssetModelThatStillHasAssociatedAssets() { - $asset = Asset::factory()->create(); + $assetModel = Asset::factory()->create()->model; $this->actingAsForApi(User::factory()->deleteAssetModels()->create()) - ->deleteJson(route('api.models.destroy', $asset->model)) + ->deleteJson(route('api.models.destroy', $assetModel)) ->assertStatusMessageIs('error'); - $this->assertFalse($asset->model->fresh()->trashed()); - } - - public function testAdheresToMultipleFullCompanySupportScoping() - { - // TODO: Implement testAdheresToMultipleFullCompanySupportScoping() method. - - $this->markTestIncomplete(); + $this->assertFalse($assetModel->fresh()->trashed()); } } From 8ce2512f55a6c293c0d744932453a12cf48f7d6d Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 12 Sep 2024 13:54:44 -0700 Subject: [PATCH 09/32] Add tests for delete category endpoint --- database/factories/UserFactory.php | 5 +++ .../Categories/Api/DeleteCategoriesTest.php | 44 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 tests/Feature/Categories/Api/DeleteCategoriesTest.php 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()); + } +} From 910f13c1f78fd2761c2c54ef8a560f80fdf611c6 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 12 Sep 2024 15:38:30 -0700 Subject: [PATCH 10/32] Add tests for delete companies endpoint --- database/factories/UserFactory.php | 15 ++++-- .../Companies/Api/DeleteCompaniesTest.php | 49 +++++++++++++++++++ 2 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 tests/Feature/Companies/Api/DeleteCompaniesTest.php diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index a41813d210..03bc193329 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -246,11 +246,6 @@ class UserFactory extends Factory return $this->appendPermission(['components.view' => '1']); } - public function createCompanies() - { - return $this->appendPermission(['companies.create' => '1']); - } - public function createComponents() { return $this->appendPermission(['components.create' => '1']); @@ -276,6 +271,16 @@ class UserFactory extends Factory return $this->appendPermission(['components.checkout' => '1']); } + public function createCompanies() + { + return $this->appendPermission(['companies.create' => '1']); + } + + public function deleteCompanies() + { + return $this->appendPermission(['companies.delete' => '1']); + } + public function viewUsers() { return $this->appendPermission(['users.view' => '1']); diff --git a/tests/Feature/Companies/Api/DeleteCompaniesTest.php b/tests/Feature/Companies/Api/DeleteCompaniesTest.php new file mode 100644 index 0000000000..7b3224c987 --- /dev/null +++ b/tests/Feature/Companies/Api/DeleteCompaniesTest.php @@ -0,0 +1,49 @@ +create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.companies.destroy', $company)) + ->assertForbidden(); + } + + public function testCanDeleteCompany() + { + $company = Company::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteCompanies()->create()) + ->deleteJson(route('api.companies.destroy', $company)) + ->assertStatusMessageIs('success'); + + $this->assertDatabaseMissing('companies', ['id' => $company->id]); + } + + public function testCannotDeleteCompanyThatHasAssociatedItems() + { + $companyWithAssets = Company::factory()->hasAssets()->create(); + $companyWithAccessories = Company::factory()->hasAccessories()->create(); + $companyWithConsumables = Company::factory()->hasConsumables()->create(); + $companyWithComponents = Company::factory()->hasComponents()->create(); + $companyWithUsers = Company::factory()->hasUsers()->create(); + + $actor = $this->actingAsForApi(User::factory()->deleteCompanies()->create()); + + $actor->deleteJson(route('api.companies.destroy', $companyWithAssets))->assertStatusMessageIs('error'); + $actor->deleteJson(route('api.companies.destroy', $companyWithAccessories))->assertStatusMessageIs('error'); + $actor->deleteJson(route('api.companies.destroy', $companyWithConsumables))->assertStatusMessageIs('error'); + $actor->deleteJson(route('api.companies.destroy', $companyWithComponents))->assertStatusMessageIs('error'); + $actor->deleteJson(route('api.companies.destroy', $companyWithUsers))->assertStatusMessageIs('error'); + } +} From 6df8b0ac0e6936833bd93512c6a7e50c1f34091f Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 12 Sep 2024 15:52:07 -0700 Subject: [PATCH 11/32] Add tests for delete component endpoint --- .../Components/Api/DeleteComponentsTest.php | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/Feature/Components/Api/DeleteComponentsTest.php diff --git a/tests/Feature/Components/Api/DeleteComponentsTest.php b/tests/Feature/Components/Api/DeleteComponentsTest.php new file mode 100644 index 0000000000..98f418449c --- /dev/null +++ b/tests/Feature/Components/Api/DeleteComponentsTest.php @@ -0,0 +1,64 @@ +create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.components.destroy', $component)) + ->assertForbidden(); + } + + public function testCanDeleteComponents() + { + $component = Component::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteComponents()->create()) + ->deleteJson(route('api.components.destroy', $component)) + ->assertStatusMessageIs('success'); + + $this->assertTrue($component->fresh()->trashed()); + } + + public function testAdheresToMultipleFullCompanySupportScoping() + { + [$companyA, $companyB] = Company::factory()->count(2)->create(); + + $componentA = Component::factory()->for($companyA)->create(); + $componentB = Component::factory()->for($companyB)->create(); + $componentC = Component::factory()->for($companyB)->create(); + + $superUser = $companyA->users()->save(User::factory()->superuser()->make()); + $userInCompanyA = $companyA->users()->save(User::factory()->deleteComponents()->make()); + $userInCompanyB = $companyB->users()->save(User::factory()->deleteComponents()->make()); + + $this->settings->enableMultipleFullCompanySupport(); + + $this->actingAsForApi($userInCompanyA) + ->deleteJson(route('api.components.destroy', $componentB)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($userInCompanyB) + ->deleteJson(route('api.components.destroy', $componentA)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($superUser) + ->deleteJson(route('api.components.destroy', $componentC)) + ->assertStatusMessageIs('success'); + + $this->assertNull($componentA->fresh()->deleted_at, 'Component unexpectedly deleted'); + $this->assertNull($componentB->fresh()->deleted_at, 'Component unexpectedly deleted'); + $this->assertNotNull($componentC->fresh()->deleted_at, 'Component was not deleted'); + } +} From e3268d32df495ed23e60c4c0587bd53fbb6d7485 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 12 Sep 2024 16:00:02 -0700 Subject: [PATCH 12/32] Add tests for delete consumable endpoint --- .../Consumables/Api/DeleteConsumablesTest.php | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/Feature/Consumables/Api/DeleteConsumablesTest.php diff --git a/tests/Feature/Consumables/Api/DeleteConsumablesTest.php b/tests/Feature/Consumables/Api/DeleteConsumablesTest.php new file mode 100644 index 0000000000..7a800ca500 --- /dev/null +++ b/tests/Feature/Consumables/Api/DeleteConsumablesTest.php @@ -0,0 +1,64 @@ +create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.consumables.destroy', $consumable)) + ->assertForbidden(); + } + + public function testCanDeleteConsumables() + { + $consumable = Consumable::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteConsumables()->create()) + ->deleteJson(route('api.consumables.destroy', $consumable)) + ->assertStatusMessageIs('success'); + + $this->assertTrue($consumable->fresh()->trashed()); + } + + public function testAdheresToMultipleFullCompanySupportScoping() + { + [$companyA, $companyB] = Company::factory()->count(2)->create(); + + $consumableA = Consumable::factory()->for($companyA)->create(); + $consumableB = Consumable::factory()->for($companyB)->create(); + $consumableC = Consumable::factory()->for($companyB)->create(); + + $superUser = $companyA->users()->save(User::factory()->superuser()->make()); + $userInCompanyA = $companyA->users()->save(User::factory()->deleteConsumables()->make()); + $userInCompanyB = $companyB->users()->save(User::factory()->deleteConsumables()->make()); + + $this->settings->enableMultipleFullCompanySupport(); + + $this->actingAsForApi($userInCompanyA) + ->deleteJson(route('api.consumables.destroy', $consumableB)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($userInCompanyB) + ->deleteJson(route('api.consumables.destroy', $consumableA)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($superUser) + ->deleteJson(route('api.consumables.destroy', $consumableC)) + ->assertStatusMessageIs('success'); + + $this->assertNull($consumableA->fresh()->deleted_at, 'Consumable unexpectedly deleted'); + $this->assertNull($consumableB->fresh()->deleted_at, 'Consumable unexpectedly deleted'); + $this->assertNotNull($consumableC->fresh()->deleted_at, 'Consumable was not deleted'); + } +} From 2047cfed09a231a90f1958ab0ce650808e05985d Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 12 Sep 2024 16:20:32 -0700 Subject: [PATCH 13/32] Add tests for delete custom fields endpoint --- database/factories/UserFactory.php | 5 ++ .../Api/DeleteCustomFieldsTest.php | 48 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 03bc193329..cc884a90b6 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -321,6 +321,11 @@ class UserFactory extends Factory return $this->appendPermission(['import' => '1']); } + public function deleteCustomFields() + { + return $this->appendPermission(['customfields.delete' => '1']); + } + private function appendPermission(array $permission) { return $this->state(function ($currentState) use ($permission) { diff --git a/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php b/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php new file mode 100644 index 0000000000..cc16e77ce6 --- /dev/null +++ b/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php @@ -0,0 +1,48 @@ +create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.customfields.destroy', $customField)) + ->assertForbidden(); + } + + public function testCustomFieldsCanBeDeleted() + { + $customField = CustomField::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteCustomFields()->create()) + ->deleteJson(route('api.customfields.destroy', $customField)) + ->assertStatusMessageIs('success'); + + $this->assertDatabaseMissing('custom_fields', ['id' => $customField->id]); + } + + public function testCustomFieldsCannotBeDeletedIfTheyHaveAssociatedFieldsets() + { + $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); + + $customField = CustomField::factory()->create(); + $customFieldset = CustomFieldset::factory()->create(); + + $customField->fieldset()->attach($customFieldset, ['order' => 1, 'required' => 'false']); + + $this->actingAsForApi(User::factory()->deleteCustomFields()->create()) + ->deleteJson(route('api.customfields.destroy', $customField)) + ->assertStatusMessageIs('error'); + + $this->assertDatabaseHas('custom_fields', ['id' => $customField->id]); + } +} From 3105f53afffdd78311314e970f6fe893e1162f8e Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 12 Sep 2024 16:54:29 -0700 Subject: [PATCH 14/32] Add tests for delete custom fieldsets endpoint --- database/factories/UserFactory.php | 5 ++ .../Api/DeleteCustomFieldsTest.php | 4 ++ .../Api/DeleteCustomFieldsetsTest.php | 65 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 tests/Feature/CustomFieldsets/Api/DeleteCustomFieldsetsTest.php diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index cc884a90b6..c82cabcc49 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -326,6 +326,11 @@ class UserFactory extends Factory return $this->appendPermission(['customfields.delete' => '1']); } + public function deleteCustomFieldsets() + { + return $this->appendPermission(['customfields.delete' => '1']); + } + private function appendPermission(array $permission) { return $this->state(function ($currentState) use ($permission) { diff --git a/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php b/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php index cc16e77ce6..72d8a06953 100644 --- a/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php +++ b/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php @@ -12,6 +12,8 @@ class DeleteCustomFieldsTest extends TestCase implements TestsPermissionsRequire { public function testRequiresPermission() { + $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); + $customField = CustomField::factory()->create(); $this->actingAsForApi(User::factory()->create()) @@ -21,6 +23,8 @@ class DeleteCustomFieldsTest extends TestCase implements TestsPermissionsRequire public function testCustomFieldsCanBeDeleted() { + $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); + $customField = CustomField::factory()->create(); $this->actingAsForApi(User::factory()->deleteCustomFields()->create()) diff --git a/tests/Feature/CustomFieldsets/Api/DeleteCustomFieldsetsTest.php b/tests/Feature/CustomFieldsets/Api/DeleteCustomFieldsetsTest.php new file mode 100644 index 0000000000..2610b4cff5 --- /dev/null +++ b/tests/Feature/CustomFieldsets/Api/DeleteCustomFieldsetsTest.php @@ -0,0 +1,65 @@ +markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); + + $customFieldset = CustomFieldset::factory()->create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.fieldsets.destroy', $customFieldset)) + ->assertForbidden(); + } + + public function testCanDeleteCustomFieldsets() + { + $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); + + $customFieldset = CustomFieldset::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteCustomFieldsets()->create()) + ->deleteJson(route('api.fieldsets.destroy', $customFieldset)) + ->assertStatusMessageIs('success'); + + $this->assertDatabaseMissing('custom_fieldsets', ['id' => $customFieldset->id]); + } + + public function testCannotDeleteCustomFieldsetWithAssociatedFields() + { + $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); + + $customField = CustomField::factory()->create(); + $customFieldset = CustomFieldset::factory()->create(); + + $customField->fieldset()->attach($customFieldset, ['order' => 1, 'required' => 'false']); + + $this->actingAsForApi(User::factory()->deleteCustomFieldsets()->create()) + ->deleteJson(route('api.fieldsets.destroy', $customFieldset)) + ->assertStatusMessageIs('error'); + + $this->assertDatabaseHas('custom_fieldsets', ['id' => $customFieldset->id]); + } + + public function testCannotDeleteCustomFieldsetWithAssociatedModels() + { + $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); + + $customFieldset = CustomFieldset::factory()->hasModels()->create(); + + $this->actingAsForApi(User::factory()->deleteCustomFieldsets()->create()) + ->deleteJson(route('api.fieldsets.destroy', $customFieldset)) + ->assertStatusMessageIs('error'); + + $this->assertDatabaseHas('custom_fieldsets', ['id' => $customFieldset->id]); + } +} From 38b9f4a43893f8c86e75d141a2ec41a7c2f90758 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 10:29:20 -0700 Subject: [PATCH 15/32] Add tests for delete departments endpoint --- database/factories/UserFactory.php | 5 ++ .../Companies/Api/DeleteCompaniesTest.php | 1 - .../Departments/Api/DeleteDepartmentTest.php | 65 +++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/Departments/Api/DeleteDepartmentTest.php diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index c82cabcc49..f30733854f 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -206,6 +206,11 @@ class UserFactory extends Factory return $this->appendPermission(['consumables.checkout' => '1']); } + public function deleteDepartments() + { + return $this->appendPermission(['departments.delete' => '1']); + } + public function viewDepartments() { return $this->appendPermission(['departments.view' => '1']); diff --git a/tests/Feature/Companies/Api/DeleteCompaniesTest.php b/tests/Feature/Companies/Api/DeleteCompaniesTest.php index 7b3224c987..64421b47d4 100644 --- a/tests/Feature/Companies/Api/DeleteCompaniesTest.php +++ b/tests/Feature/Companies/Api/DeleteCompaniesTest.php @@ -2,7 +2,6 @@ namespace Tests\Feature\Companies\Api; -use App\Models\Asset; use App\Models\Company; use App\Models\User; use Tests\Concerns\TestsPermissionsRequirement; diff --git a/tests/Feature/Departments/Api/DeleteDepartmentTest.php b/tests/Feature/Departments/Api/DeleteDepartmentTest.php new file mode 100644 index 0000000000..d8d2045af7 --- /dev/null +++ b/tests/Feature/Departments/Api/DeleteDepartmentTest.php @@ -0,0 +1,65 @@ +create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.departments.destroy', $department)) + ->assertForbidden(); + } + + public function testCanDeleteDepartment() + { + $department = Department::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteDepartments()->create()) + ->deleteJson(route('api.departments.destroy', $department)) + ->assertStatusMessageIs('success'); + + $this->assertDatabaseMissing('departments', ['id' => $department->id]); + } + + public function testAdheresToMultipleFullCompanySupportScoping() + { + [$companyA, $companyB] = Company::factory()->count(2)->create(); + + $departmentA = Department::factory()->for($companyA)->create(); + $departmentB = Department::factory()->for($companyB)->create(); + $departmentC = Department::factory()->for($companyB)->create(); + + $superUser = $companyA->users()->save(User::factory()->superuser()->make()); + $userInCompanyA = $companyA->users()->save(User::factory()->deleteDepartments()->make()); + $userInCompanyB = $companyB->users()->save(User::factory()->deleteDepartments()->make()); + + $this->settings->enableMultipleFullCompanySupport(); + + $this->actingAsForApi($userInCompanyA) + ->deleteJson(route('api.departments.destroy', $departmentB)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($userInCompanyB) + ->deleteJson(route('api.departments.destroy', $departmentA)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($superUser) + ->deleteJson(route('api.departments.destroy', $departmentC)) + ->assertStatusMessageIs('success'); + + $this->assertNotNull($departmentA->fresh(), 'Department unexpectedly deleted'); + $this->assertNotNull($departmentB->fresh(), 'Department unexpectedly deleted'); + $this->assertNull($departmentC->fresh(), 'Department was not deleted'); + } +} From 2f76c1bc5b5259cbb069356da8e8249a18910011 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 10:33:21 -0700 Subject: [PATCH 16/32] Add assertion --- .../Feature/Departments/Api/DeleteDepartmentTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/Feature/Departments/Api/DeleteDepartmentTest.php b/tests/Feature/Departments/Api/DeleteDepartmentTest.php index d8d2045af7..8e064b1566 100644 --- a/tests/Feature/Departments/Api/DeleteDepartmentTest.php +++ b/tests/Feature/Departments/Api/DeleteDepartmentTest.php @@ -32,6 +32,17 @@ class DeleteDepartmentTest extends TestCase implements TestsMultipleFullCompanyS $this->assertDatabaseMissing('departments', ['id' => $department->id]); } + public function testCannotDeleteDepartmentThatStillHasUsers() + { + $department = Department::factory()->hasUsers()->create(); + + $this->actingAsForApi(User::factory()->deleteDepartments()->create()) + ->deleteJson(route('api.departments.destroy', $department)) + ->assertStatusMessageIs('error'); + + $this->assertNotNull($department->fresh(), 'Department unexpectedly deleted'); + } + public function testAdheresToMultipleFullCompanySupportScoping() { [$companyA, $companyB] = Company::factory()->count(2)->create(); From 79a4bb73169445bb7a1e1e6bef4aff8a25dc1a12 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 10:35:44 -0700 Subject: [PATCH 17/32] Add tests for delete depreciation endpoint --- database/factories/UserFactory.php | 5 +++ .../Api/DeleteDepreciationTest.php | 42 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 tests/Feature/Depreciations/Api/DeleteDepreciationTest.php diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index f30733854f..598de7c39c 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -336,6 +336,11 @@ class UserFactory extends Factory return $this->appendPermission(['customfields.delete' => '1']); } + public function deleteDepreciations() + { + return $this->appendPermission(['depreciations.delete' => '1']); + } + private function appendPermission(array $permission) { return $this->state(function ($currentState) use ($permission) { diff --git a/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php b/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php new file mode 100644 index 0000000000..2e93f92092 --- /dev/null +++ b/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php @@ -0,0 +1,42 @@ +create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.depreciations.destroy', $depreciation)) + ->assertForbidden(); + } + + public function testCanDeleteDepreciation() + { + $depreciation = Depreciation::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteDepreciations()->create()) + ->deleteJson(route('api.depreciations.destroy', $depreciation)) + ->assertStatusMessageIs('success'); + + $this->assertDatabaseMissing('depreciations', ['id' => $depreciation->id]); + } + + public function testCannotDeleteDepreciationThatHasAssociatedModels() + { + $depreciation = Depreciation::factory()->hasModels()->create(); + + $this->actingAsForApi(User::factory()->deleteDepreciations()->create()) + ->deleteJson(route('api.depreciations.destroy', $depreciation)) + ->assertStatusMessageIs('error'); + + $this->assertNotNull($depreciation->fresh(), 'Depreciation unexpectedly deleted'); + } +} From 446e962a503078e0c5fabef90aac7ada21ecb6e2 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 10:38:51 -0700 Subject: [PATCH 18/32] Add tests for delete group endpoint --- tests/Feature/Groups/Api/DeleteGroupTest.php | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tests/Feature/Groups/Api/DeleteGroupTest.php diff --git a/tests/Feature/Groups/Api/DeleteGroupTest.php b/tests/Feature/Groups/Api/DeleteGroupTest.php new file mode 100644 index 0000000000..8d057a7ed3 --- /dev/null +++ b/tests/Feature/Groups/Api/DeleteGroupTest.php @@ -0,0 +1,32 @@ +create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.groups.destroy', $group)) + ->assertForbidden(); + } + + public function testCanDeleteGroup() + { + $group = Group::factory()->create(); + + // only super admins can delete groups + $this->actingAsForApi(User::factory()->superuser()->create()) + ->deleteJson(route('api.groups.destroy', $group)) + ->assertStatusMessageIs('success'); + + $this->assertDatabaseMissing('permission_groups', ['id' => $group->id]); + } +} From 60a54cee797aa86e80c5abfe57699f263a85e841 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 12:33:30 -0700 Subject: [PATCH 19/32] Add tests for delete license endpoint --- .../Controllers/Api/LicensesController.php | 1 - .../Licenses/Api/DeleteLicenseTest.php | 88 +++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/Licenses/Api/DeleteLicenseTest.php diff --git a/app/Http/Controllers/Api/LicensesController.php b/app/Http/Controllers/Api/LicensesController.php index 0dae68dbb7..61f04bbd0b 100644 --- a/app/Http/Controllers/Api/LicensesController.php +++ b/app/Http/Controllers/Api/LicensesController.php @@ -220,7 +220,6 @@ class LicensesController extends Controller */ public function destroy($id) : JsonResponse { - // $license = License::findOrFail($id); $this->authorize('delete', $license); diff --git a/tests/Feature/Licenses/Api/DeleteLicenseTest.php b/tests/Feature/Licenses/Api/DeleteLicenseTest.php new file mode 100644 index 0000000000..c41c0eca88 --- /dev/null +++ b/tests/Feature/Licenses/Api/DeleteLicenseTest.php @@ -0,0 +1,88 @@ +create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.licenses.destroy', $license)) + ->assertForbidden(); + } + + public function testAdheresToMultipleFullCompanySupportScoping() + { + [$companyA, $companyB] = Company::factory()->count(2)->create(); + + $licenseA = License::factory()->for($companyA)->create(); + $licenseB = License::factory()->for($companyB)->create(); + $licenseC = License::factory()->for($companyB)->create(); + + $superUser = $companyA->users()->save(User::factory()->superuser()->make()); + $userInCompanyA = $companyA->users()->save(User::factory()->deleteLicenses()->make()); + $userInCompanyB = $companyB->users()->save(User::factory()->deleteLicenses()->make()); + + $this->settings->enableMultipleFullCompanySupport(); + + $this->actingAsForApi($userInCompanyA) + ->deleteJson(route('api.licenses.destroy', $licenseB)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($userInCompanyB) + ->deleteJson(route('api.licenses.destroy', $licenseA)) + ->assertStatusMessageIs('error'); + + $this->actingAsForApi($superUser) + ->deleteJson(route('api.licenses.destroy', $licenseC)) + ->assertStatusMessageIs('success'); + + $this->assertNotSoftDeleted($licenseA); + $this->assertNotSoftDeleted($licenseB); + $this->assertSoftDeleted($licenseC); + } + + public function testLicenseCannotBeDeletedIfStillAssigned() + { + $license = License::factory()->create(['seats' => 2]); + $license->freeSeat()->update(['assigned_to' => User::factory()->create()->id]); + + $this->actingAsForApi(User::factory()->deleteLicenses()->create()) + ->deleteJson(route('api.licenses.destroy', $license)) + ->assertStatusMessageIs('error'); + + $this->assertNotSoftDeleted($license); + } + + public function testCanDeleteLicense() + { + $license = License::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteLicenses()->create()) + ->deleteJson(route('api.licenses.destroy', $license)) + ->assertStatusMessageIs('success'); + + $this->assertSoftDeleted($license); + } + + public function testLicenseSeatsAreDeletedWhenLicenseIsDeleted() + { + $license = License::factory()->create(['seats' => 2]); + + $this->assertTrue($license->fresh()->licenseseats->isNotEmpty(), 'License seats not created like expected'); + + $this->actingAsForApi(User::factory()->deleteLicenses()->create()) + ->deleteJson(route('api.licenses.destroy', $license)); + + $this->assertTrue($license->fresh()->licenseseats->isEmpty()); + } +} From 50730fc4fba2519fb13799bc93ab682c10092c25 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 12:37:18 -0700 Subject: [PATCH 20/32] Add tests for delete location endpoint --- database/factories/UserFactory.php | 5 +++++ tests/Feature/Locations/Api/DeleteLocationsTest.php | 12 +++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 598de7c39c..747aef0164 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -311,6 +311,11 @@ class UserFactory extends Factory return $this->appendPermission(['categories.delete' => '1']); } + public function deleteLocations() + { + return $this->appendPermission(['locations.delete' => '1']); + } + public function canEditOwnLocation() { return $this->appendPermission(['self.edit_location' => '1']); diff --git a/tests/Feature/Locations/Api/DeleteLocationsTest.php b/tests/Feature/Locations/Api/DeleteLocationsTest.php index 270582c901..c023b4b3ad 100644 --- a/tests/Feature/Locations/Api/DeleteLocationsTest.php +++ b/tests/Feature/Locations/Api/DeleteLocationsTest.php @@ -9,7 +9,6 @@ use Tests\TestCase; class DeleteLocationsTest extends TestCase { - public function testErrorReturnedViaApiIfLocationDoesNotExist() { $this->actingAsForApi(User::factory()->superuser()->create()) @@ -90,4 +89,15 @@ class DeleteLocationsTest extends TestCase ->json(); } + public function testCanDeleteLocation() + { + $location = Location::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteLocations()->create()) + ->deleteJson(route('api.locations.destroy', $location->id)) + ->assertOk() + ->assertStatusMessageIs('success'); + + $this->assertSoftDeleted($location); + } } From 53c673dee21e9f8cb00904950ec66cb05bde6c55 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 13:13:12 -0700 Subject: [PATCH 21/32] 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); + } +} From c269184c60e69b7aa319fedc5b4bbcb122677cb8 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 13:29:41 -0700 Subject: [PATCH 22/32] 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()); + } +} From b06e8d442d81b40e82ba70aaacf277eb5b5e075f Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 13:37:08 -0700 Subject: [PATCH 23/32] Add tests for delete status label endpoint --- database/factories/UserFactory.php | 5 +++ .../Api/DeleteStatusLabelTest.php | 45 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 tests/Feature/StatusLabels/Api/DeleteStatusLabelTest.php 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); + } +} From 7f40f55343fe7e801c6de0cd0d1fa63f442b8349 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 13:52:02 -0700 Subject: [PATCH 24/32] Add tests for delete supplier endpoint --- database/factories/UserFactory.php | 5 ++ .../Suppliers/Api/DeleteSupplierTest.php | 51 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 tests/Feature/Suppliers/Api/DeleteSupplierTest.php diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index dd92f38f67..b375142196 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -361,6 +361,11 @@ class UserFactory extends Factory return $this->appendPermission(['statuslabels.delete' => '1']); } + public function deleteSuppliers() + { + return $this->appendPermission(['suppliers.delete' => '1']); + } + private function appendPermission(array $permission) { return $this->state(function ($currentState) use ($permission) { diff --git a/tests/Feature/Suppliers/Api/DeleteSupplierTest.php b/tests/Feature/Suppliers/Api/DeleteSupplierTest.php new file mode 100644 index 0000000000..795ff2b576 --- /dev/null +++ b/tests/Feature/Suppliers/Api/DeleteSupplierTest.php @@ -0,0 +1,51 @@ +create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.suppliers.destroy', $supplier)) + ->assertForbidden(); + } + + public function testCannotDeleteSupplierWithDataStillAssociated() + { + $supplierWithAsset = Supplier::factory()->hasAssets()->create(); + $supplierWithAssetMaintenance = Supplier::factory()->has(AssetMaintenance::factory(), 'asset_maintenances')->create(); + $supplierWithLicense = Supplier::factory()->hasLicenses()->create(); + + $actor = $this->actingAsForApi(User::factory()->deleteSuppliers()->create()); + + $actor->deleteJson(route('api.suppliers.destroy', $supplierWithAsset))->assertStatusMessageIs('error'); + $actor->deleteJson(route('api.suppliers.destroy', $supplierWithAssetMaintenance))->assertStatusMessageIs('error'); + $actor->deleteJson(route('api.suppliers.destroy', $supplierWithLicense))->assertStatusMessageIs('error'); + + $this->assertNotSoftDeleted($supplierWithAsset); + $this->assertNotSoftDeleted($supplierWithAssetMaintenance); + $this->assertNotSoftDeleted($supplierWithLicense); + } + + public function testCanDeleteSupplier() + { + $supplier = Supplier::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteSuppliers()->create()) + ->deleteJson(route('api.suppliers.destroy', $supplier)) + ->assertOk() + ->assertStatusMessageIs('success'); + + $this->assertSoftDeleted($supplier); + } +} From b8b3f91ce49e5d1829bcb2a84f4d145dc1701b29 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 13:55:18 -0700 Subject: [PATCH 25/32] Formatting --- tests/Feature/Users/Api/DeleteUserTest.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/Feature/Users/Api/DeleteUserTest.php b/tests/Feature/Users/Api/DeleteUserTest.php index 49625daac3..315bb56189 100644 --- a/tests/Feature/Users/Api/DeleteUserTest.php +++ b/tests/Feature/Users/Api/DeleteUserTest.php @@ -10,8 +10,6 @@ use Tests\TestCase; class DeleteUserTest extends TestCase { - - public function testErrorReturnedViaApiIfUserDoesNotExist() { $this->actingAsForApi(User::factory()->deleteUsers()->create()) @@ -33,7 +31,6 @@ class DeleteUserTest extends TestCase ->json(); } - public function testDisallowUserDeletionViaApiIfStillManagingPeople() { $manager = User::factory()->create(); @@ -96,7 +93,6 @@ class DeleteUserTest extends TestCase ->json(); } - public function testPermissionsForDeletingIfNotInSameCompanyAndNotSuperadmin() { $this->settings->enableMultipleFullCompanySupport(); @@ -150,6 +146,4 @@ class DeleteUserTest extends TestCase ->json(); } - - } From 4af893df6189423edeb73d5239a12d4c7d5a68d0 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 14:20:24 -0700 Subject: [PATCH 26/32] Improve assertions --- .../Feature/Accessories/Api/DeleteAccessoriesTest.php | 10 +++++----- .../Api/DeleteAssetMaintenancesTest.php | 8 ++++---- .../Feature/AssetModels/Api/DeleteAssetModelsTest.php | 4 ++-- tests/Feature/Assets/Api/DeleteAssetsTest.php | 8 ++++---- tests/Feature/Categories/Api/DeleteCategoriesTest.php | 4 ++-- tests/Feature/Companies/Api/DeleteCompaniesTest.php | 6 ++++++ tests/Feature/Components/Api/DeleteComponentsTest.php | 8 ++++---- .../Feature/Consumables/Api/DeleteConsumablesTest.php | 8 ++++---- 8 files changed, 31 insertions(+), 25 deletions(-) diff --git a/tests/Feature/Accessories/Api/DeleteAccessoriesTest.php b/tests/Feature/Accessories/Api/DeleteAccessoriesTest.php index 6fa4a78160..f36e5fbedd 100644 --- a/tests/Feature/Accessories/Api/DeleteAccessoriesTest.php +++ b/tests/Feature/Accessories/Api/DeleteAccessoriesTest.php @@ -28,7 +28,7 @@ class DeleteAccessoriesTest extends TestCase implements TestsMultipleFullCompany ->deleteJson(route('api.accessories.destroy', $accessory)) ->assertStatusMessageIs('success'); - $this->assertTrue($accessory->fresh()->trashed()); + $this->assertSoftDeleted($accessory); } public function testCannotDeleteAccessoryThatHasCheckouts() @@ -39,7 +39,7 @@ class DeleteAccessoriesTest extends TestCase implements TestsMultipleFullCompany ->deleteJson(route('api.accessories.destroy', $accessory)) ->assertStatusMessageIs('error'); - $this->assertFalse($accessory->fresh()->trashed()); + $this->assertNotSoftDeleted($accessory); } public function testAdheresToMultipleFullCompanySupportScoping() @@ -68,8 +68,8 @@ class DeleteAccessoriesTest extends TestCase implements TestsMultipleFullCompany ->deleteJson(route('api.accessories.destroy', $accessoryC)) ->assertStatusMessageIs('success'); - $this->assertNull($accessoryA->fresh()->deleted_at, 'Accessory unexpectedly deleted'); - $this->assertNull($accessoryB->fresh()->deleted_at, 'Accessory unexpectedly deleted'); - $this->assertNotNull($accessoryC->fresh()->deleted_at, 'Accessory was not deleted'); + $this->assertNotSoftDeleted($accessoryA); + $this->assertNotSoftDeleted($accessoryB); + $this->assertSoftDeleted($accessoryC); } } diff --git a/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenancesTest.php b/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenancesTest.php index 9532fdcf22..80c570646e 100644 --- a/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenancesTest.php +++ b/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenancesTest.php @@ -28,7 +28,7 @@ class DeleteAssetMaintenancesTest extends TestCase implements TestsMultipleFullC ->deleteJson(route('api.maintenances.destroy', $assetMaintenance)) ->assertStatusMessageIs('success'); - $this->assertTrue($assetMaintenance->fresh()->trashed()); + $this->assertSoftDeleted($assetMaintenance); } public function testAdheresToMultipleFullCompanySupportScoping() @@ -61,8 +61,8 @@ class DeleteAssetMaintenancesTest extends TestCase implements TestsMultipleFullC ->deleteJson(route('api.maintenances.destroy', $assetMaintenanceC)) ->assertStatusMessageIs('success'); - $this->assertNull($assetMaintenanceA->fresh()->deleted_at, 'Asset Maintenance unexpectedly deleted'); - $this->assertNull($assetMaintenanceB->fresh()->deleted_at, 'Asset Maintenance unexpectedly deleted'); - $this->assertNotNull($assetMaintenanceC->fresh()->deleted_at, 'Asset Maintenance was not deleted'); + $this->assertNotSoftDeleted($assetMaintenanceA); + $this->assertNotSoftDeleted($assetMaintenanceB); + $this->assertSoftDeleted($assetMaintenanceC); } } diff --git a/tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php b/tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php index 7bc18e95f2..8511d72d80 100644 --- a/tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php +++ b/tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php @@ -27,7 +27,7 @@ class DeleteAssetModelsTest extends TestCase implements TestsPermissionsRequirem ->deleteJson(route('api.models.destroy', $assetModel)) ->assertStatusMessageIs('success'); - $this->assertTrue($assetModel->fresh()->trashed()); + $this->assertSoftDeleted($assetModel); } public function testCannotDeleteAssetModelThatStillHasAssociatedAssets() @@ -38,6 +38,6 @@ class DeleteAssetModelsTest extends TestCase implements TestsPermissionsRequirem ->deleteJson(route('api.models.destroy', $assetModel)) ->assertStatusMessageIs('error'); - $this->assertFalse($assetModel->fresh()->trashed()); + $this->assertNotSoftDeleted($assetModel); } } diff --git a/tests/Feature/Assets/Api/DeleteAssetsTest.php b/tests/Feature/Assets/Api/DeleteAssetsTest.php index 3d34422b9d..fb1fc0cad0 100644 --- a/tests/Feature/Assets/Api/DeleteAssetsTest.php +++ b/tests/Feature/Assets/Api/DeleteAssetsTest.php @@ -28,7 +28,7 @@ class DeleteAssetsTest extends TestCase implements TestsMultipleFullCompanySuppo ->deleteJson(route('api.assets.destroy', $asset)) ->assertStatusMessageIs('success'); - $this->assertTrue($asset->fresh()->trashed()); + $this->assertSoftDeleted($asset); } public function testCannotDeleteAssetThatIsCheckedOut() @@ -62,8 +62,8 @@ class DeleteAssetsTest extends TestCase implements TestsMultipleFullCompanySuppo ->deleteJson(route('api.assets.destroy', $assetC)) ->assertStatusMessageIs('success'); - $this->assertNull($assetA->fresh()->deleted_at, 'Asset unexpectedly deleted'); - $this->assertNull($assetB->fresh()->deleted_at, 'Asset unexpectedly deleted'); - $this->assertNotNull($assetC->fresh()->deleted_at, 'Asset was not deleted'); + $this->assertNotSoftDeleted($assetA); + $this->assertNotSoftDeleted($assetB); + $this->assertSoftDeleted($assetC); } } diff --git a/tests/Feature/Categories/Api/DeleteCategoriesTest.php b/tests/Feature/Categories/Api/DeleteCategoriesTest.php index 105f6f3948..e4a28b7b98 100644 --- a/tests/Feature/Categories/Api/DeleteCategoriesTest.php +++ b/tests/Feature/Categories/Api/DeleteCategoriesTest.php @@ -27,7 +27,7 @@ class DeleteCategoriesTest extends TestCase implements TestsPermissionsRequireme ->deleteJson(route('api.categories.destroy', $category)) ->assertStatusMessageIs('success'); - $this->assertTrue($category->fresh()->trashed()); + $this->assertSoftDeleted($category); } public function testCannotDeleteCategoryThatStillHasAssociatedItems() @@ -39,6 +39,6 @@ class DeleteCategoriesTest extends TestCase implements TestsPermissionsRequireme ->deleteJson(route('api.categories.destroy', $category)) ->assertStatusMessageIs('error'); - $this->assertFalse($category->fresh()->trashed()); + $this->assertNotSoftDeleted($category); } } diff --git a/tests/Feature/Companies/Api/DeleteCompaniesTest.php b/tests/Feature/Companies/Api/DeleteCompaniesTest.php index 64421b47d4..868dd36ab2 100644 --- a/tests/Feature/Companies/Api/DeleteCompaniesTest.php +++ b/tests/Feature/Companies/Api/DeleteCompaniesTest.php @@ -44,5 +44,11 @@ class DeleteCompaniesTest extends TestCase implements TestsPermissionsRequiremen $actor->deleteJson(route('api.companies.destroy', $companyWithConsumables))->assertStatusMessageIs('error'); $actor->deleteJson(route('api.companies.destroy', $companyWithComponents))->assertStatusMessageIs('error'); $actor->deleteJson(route('api.companies.destroy', $companyWithUsers))->assertStatusMessageIs('error'); + + $this->assertDatabaseHas('companies', ['id' => $companyWithAssets->id]); + $this->assertDatabaseHas('companies', ['id' => $companyWithAccessories->id]); + $this->assertDatabaseHas('companies', ['id' => $companyWithConsumables->id]); + $this->assertDatabaseHas('companies', ['id' => $companyWithComponents->id]); + $this->assertDatabaseHas('companies', ['id' => $companyWithUsers->id]); } } diff --git a/tests/Feature/Components/Api/DeleteComponentsTest.php b/tests/Feature/Components/Api/DeleteComponentsTest.php index 98f418449c..89bd8ffc8c 100644 --- a/tests/Feature/Components/Api/DeleteComponentsTest.php +++ b/tests/Feature/Components/Api/DeleteComponentsTest.php @@ -28,7 +28,7 @@ class DeleteComponentsTest extends TestCase implements TestsMultipleFullCompanyS ->deleteJson(route('api.components.destroy', $component)) ->assertStatusMessageIs('success'); - $this->assertTrue($component->fresh()->trashed()); + $this->assertSoftDeleted($component); } public function testAdheresToMultipleFullCompanySupportScoping() @@ -57,8 +57,8 @@ class DeleteComponentsTest extends TestCase implements TestsMultipleFullCompanyS ->deleteJson(route('api.components.destroy', $componentC)) ->assertStatusMessageIs('success'); - $this->assertNull($componentA->fresh()->deleted_at, 'Component unexpectedly deleted'); - $this->assertNull($componentB->fresh()->deleted_at, 'Component unexpectedly deleted'); - $this->assertNotNull($componentC->fresh()->deleted_at, 'Component was not deleted'); + $this->assertNotSoftDeleted($componentA); + $this->assertNotSoftDeleted($componentB); + $this->assertSoftDeleted($componentC); } } diff --git a/tests/Feature/Consumables/Api/DeleteConsumablesTest.php b/tests/Feature/Consumables/Api/DeleteConsumablesTest.php index 7a800ca500..72666a5d1a 100644 --- a/tests/Feature/Consumables/Api/DeleteConsumablesTest.php +++ b/tests/Feature/Consumables/Api/DeleteConsumablesTest.php @@ -28,7 +28,7 @@ class DeleteConsumablesTest extends TestCase implements TestsMultipleFullCompany ->deleteJson(route('api.consumables.destroy', $consumable)) ->assertStatusMessageIs('success'); - $this->assertTrue($consumable->fresh()->trashed()); + $this->assertSoftDeleted($consumable); } public function testAdheresToMultipleFullCompanySupportScoping() @@ -57,8 +57,8 @@ class DeleteConsumablesTest extends TestCase implements TestsMultipleFullCompany ->deleteJson(route('api.consumables.destroy', $consumableC)) ->assertStatusMessageIs('success'); - $this->assertNull($consumableA->fresh()->deleted_at, 'Consumable unexpectedly deleted'); - $this->assertNull($consumableB->fresh()->deleted_at, 'Consumable unexpectedly deleted'); - $this->assertNotNull($consumableC->fresh()->deleted_at, 'Consumable was not deleted'); + $this->assertNotSoftDeleted($consumableA); + $this->assertNotSoftDeleted($consumableB); + $this->assertSoftDeleted($consumableC); } } From 1fddacd7d0440db2598c1d0e4aa0373de204bf53 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 14:25:11 -0700 Subject: [PATCH 27/32] Re-order test methods --- .../Accessories/Api/DeleteAccessoriesTest.php | 44 +++++++++--------- .../Api/DeleteAssetMaintenancesTest.php | 22 ++++----- .../AssetModels/Api/DeleteAssetModelsTest.php | 22 ++++----- tests/Feature/Assets/Api/DeleteAssetsTest.php | 32 ++++++------- .../Categories/Api/DeleteCategoriesTest.php | 22 ++++----- .../Companies/Api/DeleteCompaniesTest.php | 22 ++++----- .../Components/Api/DeleteComponentsTest.php | 22 ++++----- .../Consumables/Api/DeleteConsumablesTest.php | 22 ++++----- .../Api/DeleteCustomFieldsTest.php | 26 +++++------ .../Api/DeleteCustomFieldsetsTest.php | 26 +++++------ .../Departments/Api/DeleteDepartmentTest.php | 45 +++++++++---------- .../Api/DeleteDepreciationTest.php | 22 ++++----- .../Suppliers/Api/DeleteSupplierTest.php | 1 - 13 files changed, 163 insertions(+), 165 deletions(-) diff --git a/tests/Feature/Accessories/Api/DeleteAccessoriesTest.php b/tests/Feature/Accessories/Api/DeleteAccessoriesTest.php index f36e5fbedd..8f2fabac6d 100644 --- a/tests/Feature/Accessories/Api/DeleteAccessoriesTest.php +++ b/tests/Feature/Accessories/Api/DeleteAccessoriesTest.php @@ -20,28 +20,6 @@ class DeleteAccessoriesTest extends TestCase implements TestsMultipleFullCompany ->assertForbidden(); } - public function testCanDeleteAccessory() - { - $accessory = Accessory::factory()->create(); - - $this->actingAsForApi(User::factory()->deleteAccessories()->create()) - ->deleteJson(route('api.accessories.destroy', $accessory)) - ->assertStatusMessageIs('success'); - - $this->assertSoftDeleted($accessory); - } - - public function testCannotDeleteAccessoryThatHasCheckouts() - { - $accessory = Accessory::factory()->checkedOutToUser()->create(); - - $this->actingAsForApi(User::factory()->deleteAccessories()->create()) - ->deleteJson(route('api.accessories.destroy', $accessory)) - ->assertStatusMessageIs('error'); - - $this->assertNotSoftDeleted($accessory); - } - public function testAdheresToMultipleFullCompanySupportScoping() { [$companyA, $companyB] = Company::factory()->count(2)->create(); @@ -72,4 +50,26 @@ class DeleteAccessoriesTest extends TestCase implements TestsMultipleFullCompany $this->assertNotSoftDeleted($accessoryB); $this->assertSoftDeleted($accessoryC); } + + public function testCannotDeleteAccessoryThatHasCheckouts() + { + $accessory = Accessory::factory()->checkedOutToUser()->create(); + + $this->actingAsForApi(User::factory()->deleteAccessories()->create()) + ->deleteJson(route('api.accessories.destroy', $accessory)) + ->assertStatusMessageIs('error'); + + $this->assertNotSoftDeleted($accessory); + } + + public function testCanDeleteAccessory() + { + $accessory = Accessory::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteAccessories()->create()) + ->deleteJson(route('api.accessories.destroy', $accessory)) + ->assertStatusMessageIs('success'); + + $this->assertSoftDeleted($accessory); + } } diff --git a/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenancesTest.php b/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenancesTest.php index 80c570646e..06bf9bcd77 100644 --- a/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenancesTest.php +++ b/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenancesTest.php @@ -20,17 +20,6 @@ class DeleteAssetMaintenancesTest extends TestCase implements TestsMultipleFullC ->assertForbidden(); } - public function testCanDeleteAssetMaintenance() - { - $assetMaintenance = AssetMaintenance::factory()->create(); - - $this->actingAsForApi(User::factory()->editAssets()->create()) - ->deleteJson(route('api.maintenances.destroy', $assetMaintenance)) - ->assertStatusMessageIs('success'); - - $this->assertSoftDeleted($assetMaintenance); - } - public function testAdheresToMultipleFullCompanySupportScoping() { [$companyA, $companyB] = Company::factory()->count(2)->create(); @@ -65,4 +54,15 @@ class DeleteAssetMaintenancesTest extends TestCase implements TestsMultipleFullC $this->assertNotSoftDeleted($assetMaintenanceB); $this->assertSoftDeleted($assetMaintenanceC); } + + public function testCanDeleteAssetMaintenance() + { + $assetMaintenance = AssetMaintenance::factory()->create(); + + $this->actingAsForApi(User::factory()->editAssets()->create()) + ->deleteJson(route('api.maintenances.destroy', $assetMaintenance)) + ->assertStatusMessageIs('success'); + + $this->assertSoftDeleted($assetMaintenance); + } } diff --git a/tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php b/tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php index 8511d72d80..7de82db65b 100644 --- a/tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php +++ b/tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php @@ -19,17 +19,6 @@ class DeleteAssetModelsTest extends TestCase implements TestsPermissionsRequirem ->assertForbidden(); } - public function testCanDeleteAssetModel() - { - $assetModel = AssetModel::factory()->create(); - - $this->actingAsForApi(User::factory()->deleteAssetModels()->create()) - ->deleteJson(route('api.models.destroy', $assetModel)) - ->assertStatusMessageIs('success'); - - $this->assertSoftDeleted($assetModel); - } - public function testCannotDeleteAssetModelThatStillHasAssociatedAssets() { $assetModel = Asset::factory()->create()->model; @@ -40,4 +29,15 @@ class DeleteAssetModelsTest extends TestCase implements TestsPermissionsRequirem $this->assertNotSoftDeleted($assetModel); } + + public function testCanDeleteAssetModel() + { + $assetModel = AssetModel::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteAssetModels()->create()) + ->deleteJson(route('api.models.destroy', $assetModel)) + ->assertStatusMessageIs('success'); + + $this->assertSoftDeleted($assetModel); + } } diff --git a/tests/Feature/Assets/Api/DeleteAssetsTest.php b/tests/Feature/Assets/Api/DeleteAssetsTest.php index fb1fc0cad0..d09d7d4034 100644 --- a/tests/Feature/Assets/Api/DeleteAssetsTest.php +++ b/tests/Feature/Assets/Api/DeleteAssetsTest.php @@ -20,22 +20,6 @@ class DeleteAssetsTest extends TestCase implements TestsMultipleFullCompanySuppo ->assertForbidden(); } - public function testCanDeleteAsset() - { - $asset = Asset::factory()->create(); - - $this->actingAsForApi(User::factory()->deleteAssets()->create()) - ->deleteJson(route('api.assets.destroy', $asset)) - ->assertStatusMessageIs('success'); - - $this->assertSoftDeleted($asset); - } - - public function testCannotDeleteAssetThatIsCheckedOut() - { - $this->markTestSkipped('This behavior is not functioning yet.'); - } - public function testAdheresToMultipleFullCompanySupportScoping() { [$companyA, $companyB] = Company::factory()->count(2)->create(); @@ -66,4 +50,20 @@ class DeleteAssetsTest extends TestCase implements TestsMultipleFullCompanySuppo $this->assertNotSoftDeleted($assetB); $this->assertSoftDeleted($assetC); } + + public function testCannotDeleteAssetThatIsCheckedOut() + { + $this->markTestSkipped('This behavior is not functioning yet.'); + } + + public function testCanDeleteAsset() + { + $asset = Asset::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteAssets()->create()) + ->deleteJson(route('api.assets.destroy', $asset)) + ->assertStatusMessageIs('success'); + + $this->assertSoftDeleted($asset); + } } diff --git a/tests/Feature/Categories/Api/DeleteCategoriesTest.php b/tests/Feature/Categories/Api/DeleteCategoriesTest.php index e4a28b7b98..2da03c3ca3 100644 --- a/tests/Feature/Categories/Api/DeleteCategoriesTest.php +++ b/tests/Feature/Categories/Api/DeleteCategoriesTest.php @@ -19,17 +19,6 @@ class DeleteCategoriesTest extends TestCase implements TestsPermissionsRequireme ->assertForbidden(); } - public function testCanDeleteCategory() - { - $category = Category::factory()->create(); - - $this->actingAsForApi(User::factory()->deleteCategories()->create()) - ->deleteJson(route('api.categories.destroy', $category)) - ->assertStatusMessageIs('success'); - - $this->assertSoftDeleted($category); - } - public function testCannotDeleteCategoryThatStillHasAssociatedItems() { $asset = Asset::factory()->create(); @@ -41,4 +30,15 @@ class DeleteCategoriesTest extends TestCase implements TestsPermissionsRequireme $this->assertNotSoftDeleted($category); } + + public function testCanDeleteCategory() + { + $category = Category::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteCategories()->create()) + ->deleteJson(route('api.categories.destroy', $category)) + ->assertStatusMessageIs('success'); + + $this->assertSoftDeleted($category); + } } diff --git a/tests/Feature/Companies/Api/DeleteCompaniesTest.php b/tests/Feature/Companies/Api/DeleteCompaniesTest.php index 868dd36ab2..4ecc3c183f 100644 --- a/tests/Feature/Companies/Api/DeleteCompaniesTest.php +++ b/tests/Feature/Companies/Api/DeleteCompaniesTest.php @@ -18,17 +18,6 @@ class DeleteCompaniesTest extends TestCase implements TestsPermissionsRequiremen ->assertForbidden(); } - public function testCanDeleteCompany() - { - $company = Company::factory()->create(); - - $this->actingAsForApi(User::factory()->deleteCompanies()->create()) - ->deleteJson(route('api.companies.destroy', $company)) - ->assertStatusMessageIs('success'); - - $this->assertDatabaseMissing('companies', ['id' => $company->id]); - } - public function testCannotDeleteCompanyThatHasAssociatedItems() { $companyWithAssets = Company::factory()->hasAssets()->create(); @@ -51,4 +40,15 @@ class DeleteCompaniesTest extends TestCase implements TestsPermissionsRequiremen $this->assertDatabaseHas('companies', ['id' => $companyWithComponents->id]); $this->assertDatabaseHas('companies', ['id' => $companyWithUsers->id]); } + + public function testCanDeleteCompany() + { + $company = Company::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteCompanies()->create()) + ->deleteJson(route('api.companies.destroy', $company)) + ->assertStatusMessageIs('success'); + + $this->assertDatabaseMissing('companies', ['id' => $company->id]); + } } diff --git a/tests/Feature/Components/Api/DeleteComponentsTest.php b/tests/Feature/Components/Api/DeleteComponentsTest.php index 89bd8ffc8c..c25e8a5500 100644 --- a/tests/Feature/Components/Api/DeleteComponentsTest.php +++ b/tests/Feature/Components/Api/DeleteComponentsTest.php @@ -20,17 +20,6 @@ class DeleteComponentsTest extends TestCase implements TestsMultipleFullCompanyS ->assertForbidden(); } - public function testCanDeleteComponents() - { - $component = Component::factory()->create(); - - $this->actingAsForApi(User::factory()->deleteComponents()->create()) - ->deleteJson(route('api.components.destroy', $component)) - ->assertStatusMessageIs('success'); - - $this->assertSoftDeleted($component); - } - public function testAdheresToMultipleFullCompanySupportScoping() { [$companyA, $companyB] = Company::factory()->count(2)->create(); @@ -61,4 +50,15 @@ class DeleteComponentsTest extends TestCase implements TestsMultipleFullCompanyS $this->assertNotSoftDeleted($componentB); $this->assertSoftDeleted($componentC); } + + public function testCanDeleteComponents() + { + $component = Component::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteComponents()->create()) + ->deleteJson(route('api.components.destroy', $component)) + ->assertStatusMessageIs('success'); + + $this->assertSoftDeleted($component); + } } diff --git a/tests/Feature/Consumables/Api/DeleteConsumablesTest.php b/tests/Feature/Consumables/Api/DeleteConsumablesTest.php index 72666a5d1a..fa50207ebe 100644 --- a/tests/Feature/Consumables/Api/DeleteConsumablesTest.php +++ b/tests/Feature/Consumables/Api/DeleteConsumablesTest.php @@ -20,17 +20,6 @@ class DeleteConsumablesTest extends TestCase implements TestsMultipleFullCompany ->assertForbidden(); } - public function testCanDeleteConsumables() - { - $consumable = Consumable::factory()->create(); - - $this->actingAsForApi(User::factory()->deleteConsumables()->create()) - ->deleteJson(route('api.consumables.destroy', $consumable)) - ->assertStatusMessageIs('success'); - - $this->assertSoftDeleted($consumable); - } - public function testAdheresToMultipleFullCompanySupportScoping() { [$companyA, $companyB] = Company::factory()->count(2)->create(); @@ -61,4 +50,15 @@ class DeleteConsumablesTest extends TestCase implements TestsMultipleFullCompany $this->assertNotSoftDeleted($consumableB); $this->assertSoftDeleted($consumableC); } + + public function testCanDeleteConsumables() + { + $consumable = Consumable::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteConsumables()->create()) + ->deleteJson(route('api.consumables.destroy', $consumable)) + ->assertStatusMessageIs('success'); + + $this->assertSoftDeleted($consumable); + } } diff --git a/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php b/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php index 72d8a06953..ef124b8214 100644 --- a/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php +++ b/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php @@ -21,19 +21,6 @@ class DeleteCustomFieldsTest extends TestCase implements TestsPermissionsRequire ->assertForbidden(); } - public function testCustomFieldsCanBeDeleted() - { - $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); - - $customField = CustomField::factory()->create(); - - $this->actingAsForApi(User::factory()->deleteCustomFields()->create()) - ->deleteJson(route('api.customfields.destroy', $customField)) - ->assertStatusMessageIs('success'); - - $this->assertDatabaseMissing('custom_fields', ['id' => $customField->id]); - } - public function testCustomFieldsCannotBeDeletedIfTheyHaveAssociatedFieldsets() { $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); @@ -49,4 +36,17 @@ class DeleteCustomFieldsTest extends TestCase implements TestsPermissionsRequire $this->assertDatabaseHas('custom_fields', ['id' => $customField->id]); } + + public function testCustomFieldsCanBeDeleted() + { + $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); + + $customField = CustomField::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteCustomFields()->create()) + ->deleteJson(route('api.customfields.destroy', $customField)) + ->assertStatusMessageIs('success'); + + $this->assertDatabaseMissing('custom_fields', ['id' => $customField->id]); + } } diff --git a/tests/Feature/CustomFieldsets/Api/DeleteCustomFieldsetsTest.php b/tests/Feature/CustomFieldsets/Api/DeleteCustomFieldsetsTest.php index 2610b4cff5..161b1cd4b1 100644 --- a/tests/Feature/CustomFieldsets/Api/DeleteCustomFieldsetsTest.php +++ b/tests/Feature/CustomFieldsets/Api/DeleteCustomFieldsetsTest.php @@ -21,19 +21,6 @@ class DeleteCustomFieldsetsTest extends TestCase implements TestsPermissionsRequ ->assertForbidden(); } - public function testCanDeleteCustomFieldsets() - { - $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); - - $customFieldset = CustomFieldset::factory()->create(); - - $this->actingAsForApi(User::factory()->deleteCustomFieldsets()->create()) - ->deleteJson(route('api.fieldsets.destroy', $customFieldset)) - ->assertStatusMessageIs('success'); - - $this->assertDatabaseMissing('custom_fieldsets', ['id' => $customFieldset->id]); - } - public function testCannotDeleteCustomFieldsetWithAssociatedFields() { $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); @@ -62,4 +49,17 @@ class DeleteCustomFieldsetsTest extends TestCase implements TestsPermissionsRequ $this->assertDatabaseHas('custom_fieldsets', ['id' => $customFieldset->id]); } + + public function testCanDeleteCustomFieldsets() + { + $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); + + $customFieldset = CustomFieldset::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteCustomFieldsets()->create()) + ->deleteJson(route('api.fieldsets.destroy', $customFieldset)) + ->assertStatusMessageIs('success'); + + $this->assertDatabaseMissing('custom_fieldsets', ['id' => $customFieldset->id]); + } } diff --git a/tests/Feature/Departments/Api/DeleteDepartmentTest.php b/tests/Feature/Departments/Api/DeleteDepartmentTest.php index 8e064b1566..3318767095 100644 --- a/tests/Feature/Departments/Api/DeleteDepartmentTest.php +++ b/tests/Feature/Departments/Api/DeleteDepartmentTest.php @@ -11,7 +11,6 @@ use Tests\TestCase; class DeleteDepartmentTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement { - public function testRequiresPermission() { $department = Department::factory()->create(); @@ -21,28 +20,6 @@ class DeleteDepartmentTest extends TestCase implements TestsMultipleFullCompanyS ->assertForbidden(); } - public function testCanDeleteDepartment() - { - $department = Department::factory()->create(); - - $this->actingAsForApi(User::factory()->deleteDepartments()->create()) - ->deleteJson(route('api.departments.destroy', $department)) - ->assertStatusMessageIs('success'); - - $this->assertDatabaseMissing('departments', ['id' => $department->id]); - } - - public function testCannotDeleteDepartmentThatStillHasUsers() - { - $department = Department::factory()->hasUsers()->create(); - - $this->actingAsForApi(User::factory()->deleteDepartments()->create()) - ->deleteJson(route('api.departments.destroy', $department)) - ->assertStatusMessageIs('error'); - - $this->assertNotNull($department->fresh(), 'Department unexpectedly deleted'); - } - public function testAdheresToMultipleFullCompanySupportScoping() { [$companyA, $companyB] = Company::factory()->count(2)->create(); @@ -73,4 +50,26 @@ class DeleteDepartmentTest extends TestCase implements TestsMultipleFullCompanyS $this->assertNotNull($departmentB->fresh(), 'Department unexpectedly deleted'); $this->assertNull($departmentC->fresh(), 'Department was not deleted'); } + + public function testCannotDeleteDepartmentThatStillHasUsers() + { + $department = Department::factory()->hasUsers()->create(); + + $this->actingAsForApi(User::factory()->deleteDepartments()->create()) + ->deleteJson(route('api.departments.destroy', $department)) + ->assertStatusMessageIs('error'); + + $this->assertNotNull($department->fresh(), 'Department unexpectedly deleted'); + } + + public function testCanDeleteDepartment() + { + $department = Department::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteDepartments()->create()) + ->deleteJson(route('api.departments.destroy', $department)) + ->assertStatusMessageIs('success'); + + $this->assertDatabaseMissing('departments', ['id' => $department->id]); + } } diff --git a/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php b/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php index 2e93f92092..d45beb6aed 100644 --- a/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php +++ b/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php @@ -18,17 +18,6 @@ class DeleteDepreciationTest extends TestCase implements TestsPermissionsRequire ->assertForbidden(); } - public function testCanDeleteDepreciation() - { - $depreciation = Depreciation::factory()->create(); - - $this->actingAsForApi(User::factory()->deleteDepreciations()->create()) - ->deleteJson(route('api.depreciations.destroy', $depreciation)) - ->assertStatusMessageIs('success'); - - $this->assertDatabaseMissing('depreciations', ['id' => $depreciation->id]); - } - public function testCannotDeleteDepreciationThatHasAssociatedModels() { $depreciation = Depreciation::factory()->hasModels()->create(); @@ -39,4 +28,15 @@ class DeleteDepreciationTest extends TestCase implements TestsPermissionsRequire $this->assertNotNull($depreciation->fresh(), 'Depreciation unexpectedly deleted'); } + + public function testCanDeleteDepreciation() + { + $depreciation = Depreciation::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteDepreciations()->create()) + ->deleteJson(route('api.depreciations.destroy', $depreciation)) + ->assertStatusMessageIs('success'); + + $this->assertDatabaseMissing('depreciations', ['id' => $depreciation->id]); + } } diff --git a/tests/Feature/Suppliers/Api/DeleteSupplierTest.php b/tests/Feature/Suppliers/Api/DeleteSupplierTest.php index 795ff2b576..f1600a963c 100644 --- a/tests/Feature/Suppliers/Api/DeleteSupplierTest.php +++ b/tests/Feature/Suppliers/Api/DeleteSupplierTest.php @@ -2,7 +2,6 @@ namespace Tests\Feature\Suppliers\Api; -use App\Models\Asset; use App\Models\AssetMaintenance; use App\Models\Supplier; use App\Models\User; From f325c4afdb234ab4ba820caac45bdf599a550c2f Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 14:32:38 -0700 Subject: [PATCH 28/32] Unify assertion method --- tests/Feature/Accessories/Api/DeleteAccessoriesTest.php | 2 ++ .../AssetMaintenances/Api/DeleteAssetMaintenancesTest.php | 2 ++ tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php | 2 ++ tests/Feature/Assets/Api/DeleteAssetsTest.php | 2 ++ tests/Feature/Categories/Api/DeleteCategoriesTest.php | 2 ++ tests/Feature/Companies/Api/DeleteCompaniesTest.php | 2 ++ tests/Feature/Components/Api/DeleteComponentsTest.php | 2 ++ tests/Feature/Consumables/Api/DeleteConsumablesTest.php | 2 ++ tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php | 2 ++ tests/Feature/CustomFieldsets/Api/DeleteCustomFieldsetsTest.php | 2 ++ tests/Feature/Departments/Api/DeleteDepartmentTest.php | 2 ++ tests/Feature/Depreciations/Api/DeleteDepreciationTest.php | 2 ++ tests/Feature/Groups/Api/DeleteGroupTest.php | 2 ++ tests/Feature/Licenses/Api/DeleteLicenseTest.php | 2 ++ tests/Feature/PredefinedKits/Api/DeletePredefinedKitTest.php | 2 ++ tests/Feature/StatusLabels/Api/DeleteStatusLabelTest.php | 2 ++ tests/Feature/Suppliers/Api/DeleteSupplierTest.php | 2 ++ 17 files changed, 34 insertions(+) diff --git a/tests/Feature/Accessories/Api/DeleteAccessoriesTest.php b/tests/Feature/Accessories/Api/DeleteAccessoriesTest.php index 8f2fabac6d..aa5d7e5bcd 100644 --- a/tests/Feature/Accessories/Api/DeleteAccessoriesTest.php +++ b/tests/Feature/Accessories/Api/DeleteAccessoriesTest.php @@ -18,6 +18,8 @@ class DeleteAccessoriesTest extends TestCase implements TestsMultipleFullCompany $this->actingAsForApi(User::factory()->create()) ->deleteJson(route('api.accessories.destroy', $accessory)) ->assertForbidden(); + + $this->assertNotSoftDeleted($accessory); } public function testAdheresToMultipleFullCompanySupportScoping() diff --git a/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenancesTest.php b/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenancesTest.php index 06bf9bcd77..ed7ed44447 100644 --- a/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenancesTest.php +++ b/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenancesTest.php @@ -18,6 +18,8 @@ class DeleteAssetMaintenancesTest extends TestCase implements TestsMultipleFullC $this->actingAsForApi(User::factory()->create()) ->deleteJson(route('api.maintenances.destroy', $assetMaintenance)) ->assertForbidden(); + + $this->assertNotSoftDeleted($assetMaintenance); } public function testAdheresToMultipleFullCompanySupportScoping() diff --git a/tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php b/tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php index 7de82db65b..a079788651 100644 --- a/tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php +++ b/tests/Feature/AssetModels/Api/DeleteAssetModelsTest.php @@ -17,6 +17,8 @@ class DeleteAssetModelsTest extends TestCase implements TestsPermissionsRequirem $this->actingAsForApi(User::factory()->create()) ->deleteJson(route('api.models.destroy', $assetModel)) ->assertForbidden(); + + $this->assertNotSoftDeleted($assetModel); } public function testCannotDeleteAssetModelThatStillHasAssociatedAssets() diff --git a/tests/Feature/Assets/Api/DeleteAssetsTest.php b/tests/Feature/Assets/Api/DeleteAssetsTest.php index d09d7d4034..95640ed83f 100644 --- a/tests/Feature/Assets/Api/DeleteAssetsTest.php +++ b/tests/Feature/Assets/Api/DeleteAssetsTest.php @@ -18,6 +18,8 @@ class DeleteAssetsTest extends TestCase implements TestsMultipleFullCompanySuppo $this->actingAsForApi(User::factory()->create()) ->deleteJson(route('api.assets.destroy', $asset)) ->assertForbidden(); + + $this->assertNotSoftDeleted($asset); } public function testAdheresToMultipleFullCompanySupportScoping() diff --git a/tests/Feature/Categories/Api/DeleteCategoriesTest.php b/tests/Feature/Categories/Api/DeleteCategoriesTest.php index 2da03c3ca3..eb9b73b050 100644 --- a/tests/Feature/Categories/Api/DeleteCategoriesTest.php +++ b/tests/Feature/Categories/Api/DeleteCategoriesTest.php @@ -17,6 +17,8 @@ class DeleteCategoriesTest extends TestCase implements TestsPermissionsRequireme $this->actingAsForApi(User::factory()->create()) ->deleteJson(route('api.categories.destroy', $category)) ->assertForbidden(); + + $this->assertNotSoftDeleted($category); } public function testCannotDeleteCategoryThatStillHasAssociatedItems() diff --git a/tests/Feature/Companies/Api/DeleteCompaniesTest.php b/tests/Feature/Companies/Api/DeleteCompaniesTest.php index 4ecc3c183f..3dcdb4fd21 100644 --- a/tests/Feature/Companies/Api/DeleteCompaniesTest.php +++ b/tests/Feature/Companies/Api/DeleteCompaniesTest.php @@ -16,6 +16,8 @@ class DeleteCompaniesTest extends TestCase implements TestsPermissionsRequiremen $this->actingAsForApi(User::factory()->create()) ->deleteJson(route('api.companies.destroy', $company)) ->assertForbidden(); + + $this->assertDatabaseHas('companies', ['id' => $company->id]); } public function testCannotDeleteCompanyThatHasAssociatedItems() diff --git a/tests/Feature/Components/Api/DeleteComponentsTest.php b/tests/Feature/Components/Api/DeleteComponentsTest.php index c25e8a5500..df965d745c 100644 --- a/tests/Feature/Components/Api/DeleteComponentsTest.php +++ b/tests/Feature/Components/Api/DeleteComponentsTest.php @@ -18,6 +18,8 @@ class DeleteComponentsTest extends TestCase implements TestsMultipleFullCompanyS $this->actingAsForApi(User::factory()->create()) ->deleteJson(route('api.components.destroy', $component)) ->assertForbidden(); + + $this->assertNotSoftDeleted($component); } public function testAdheresToMultipleFullCompanySupportScoping() diff --git a/tests/Feature/Consumables/Api/DeleteConsumablesTest.php b/tests/Feature/Consumables/Api/DeleteConsumablesTest.php index fa50207ebe..c57e2e0df8 100644 --- a/tests/Feature/Consumables/Api/DeleteConsumablesTest.php +++ b/tests/Feature/Consumables/Api/DeleteConsumablesTest.php @@ -18,6 +18,8 @@ class DeleteConsumablesTest extends TestCase implements TestsMultipleFullCompany $this->actingAsForApi(User::factory()->create()) ->deleteJson(route('api.consumables.destroy', $consumable)) ->assertForbidden(); + + $this->assertNotSoftDeleted($consumable); } public function testAdheresToMultipleFullCompanySupportScoping() diff --git a/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php b/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php index ef124b8214..ab40591e90 100644 --- a/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php +++ b/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php @@ -19,6 +19,8 @@ class DeleteCustomFieldsTest extends TestCase implements TestsPermissionsRequire $this->actingAsForApi(User::factory()->create()) ->deleteJson(route('api.customfields.destroy', $customField)) ->assertForbidden(); + + $this->assertDatabaseHas('custom_fields', ['id' => $customField->id]); } public function testCustomFieldsCannotBeDeletedIfTheyHaveAssociatedFieldsets() diff --git a/tests/Feature/CustomFieldsets/Api/DeleteCustomFieldsetsTest.php b/tests/Feature/CustomFieldsets/Api/DeleteCustomFieldsetsTest.php index 161b1cd4b1..c464323653 100644 --- a/tests/Feature/CustomFieldsets/Api/DeleteCustomFieldsetsTest.php +++ b/tests/Feature/CustomFieldsets/Api/DeleteCustomFieldsetsTest.php @@ -19,6 +19,8 @@ class DeleteCustomFieldsetsTest extends TestCase implements TestsPermissionsRequ $this->actingAsForApi(User::factory()->create()) ->deleteJson(route('api.fieldsets.destroy', $customFieldset)) ->assertForbidden(); + + $this->assertDatabaseHas('custom_fieldsets', ['id' => $customFieldset->id]); } public function testCannotDeleteCustomFieldsetWithAssociatedFields() diff --git a/tests/Feature/Departments/Api/DeleteDepartmentTest.php b/tests/Feature/Departments/Api/DeleteDepartmentTest.php index 3318767095..5865a7f2e5 100644 --- a/tests/Feature/Departments/Api/DeleteDepartmentTest.php +++ b/tests/Feature/Departments/Api/DeleteDepartmentTest.php @@ -18,6 +18,8 @@ class DeleteDepartmentTest extends TestCase implements TestsMultipleFullCompanyS $this->actingAsForApi(User::factory()->create()) ->deleteJson(route('api.departments.destroy', $department)) ->assertForbidden(); + + $this->assertDatabaseHas('departments', ['id' => $department->id]); } public function testAdheresToMultipleFullCompanySupportScoping() diff --git a/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php b/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php index d45beb6aed..a8ec45f6ed 100644 --- a/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php +++ b/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php @@ -16,6 +16,8 @@ class DeleteDepreciationTest extends TestCase implements TestsPermissionsRequire $this->actingAsForApi(User::factory()->create()) ->deleteJson(route('api.depreciations.destroy', $depreciation)) ->assertForbidden(); + + $this->assertDatabaseHas('depreciations', ['id' => $depreciation->id]); } public function testCannotDeleteDepreciationThatHasAssociatedModels() diff --git a/tests/Feature/Groups/Api/DeleteGroupTest.php b/tests/Feature/Groups/Api/DeleteGroupTest.php index 8d057a7ed3..ec0766340b 100644 --- a/tests/Feature/Groups/Api/DeleteGroupTest.php +++ b/tests/Feature/Groups/Api/DeleteGroupTest.php @@ -16,6 +16,8 @@ class DeleteGroupTest extends TestCase implements TestsPermissionsRequirement $this->actingAsForApi(User::factory()->create()) ->deleteJson(route('api.groups.destroy', $group)) ->assertForbidden(); + + $this->assertDatabaseHas('permission_groups', ['id' => $group->id]); } public function testCanDeleteGroup() diff --git a/tests/Feature/Licenses/Api/DeleteLicenseTest.php b/tests/Feature/Licenses/Api/DeleteLicenseTest.php index c41c0eca88..5261c339e6 100644 --- a/tests/Feature/Licenses/Api/DeleteLicenseTest.php +++ b/tests/Feature/Licenses/Api/DeleteLicenseTest.php @@ -18,6 +18,8 @@ class DeleteLicenseTest extends TestCase implements TestsMultipleFullCompanySupp $this->actingAsForApi(User::factory()->create()) ->deleteJson(route('api.licenses.destroy', $license)) ->assertForbidden(); + + $this->assertNotSoftDeleted($license); } public function testAdheresToMultipleFullCompanySupportScoping() diff --git a/tests/Feature/PredefinedKits/Api/DeletePredefinedKitTest.php b/tests/Feature/PredefinedKits/Api/DeletePredefinedKitTest.php index e5eb544149..265278a302 100644 --- a/tests/Feature/PredefinedKits/Api/DeletePredefinedKitTest.php +++ b/tests/Feature/PredefinedKits/Api/DeletePredefinedKitTest.php @@ -17,6 +17,8 @@ class DeletePredefinedKitTest extends TestCase implements TestsPermissionsRequir $this->actingAsForApi(User::factory()->create()) ->deleteJson(route('api.kits.destroy', $predefinedKit)) ->assertForbidden(); + + $this->assertDatabaseHas('kits', ['id' => $predefinedKit->id]); } public function testCanDeletePredefinedKits() diff --git a/tests/Feature/StatusLabels/Api/DeleteStatusLabelTest.php b/tests/Feature/StatusLabels/Api/DeleteStatusLabelTest.php index b7e92dcf22..46b8c6a61d 100644 --- a/tests/Feature/StatusLabels/Api/DeleteStatusLabelTest.php +++ b/tests/Feature/StatusLabels/Api/DeleteStatusLabelTest.php @@ -16,6 +16,8 @@ class DeleteStatusLabelTest extends TestCase implements TestsPermissionsRequirem $this->actingAsForApi(User::factory()->create()) ->deleteJson(route('api.statuslabels.destroy', $statusLabel)) ->assertForbidden(); + + $this->assertNotSoftDeleted($statusLabel); } public function testCannotDeleteStatusLabelWhileStillAssociatedToAssets() diff --git a/tests/Feature/Suppliers/Api/DeleteSupplierTest.php b/tests/Feature/Suppliers/Api/DeleteSupplierTest.php index f1600a963c..fd619cf7bd 100644 --- a/tests/Feature/Suppliers/Api/DeleteSupplierTest.php +++ b/tests/Feature/Suppliers/Api/DeleteSupplierTest.php @@ -17,6 +17,8 @@ class DeleteSupplierTest extends TestCase implements TestsPermissionsRequirement $this->actingAsForApi(User::factory()->create()) ->deleteJson(route('api.suppliers.destroy', $supplier)) ->assertForbidden(); + + $this->assertNotSoftDeleted($supplier); } public function testCannotDeleteSupplierWithDataStillAssociated() From f5705a1dde7a736e167e27ec156f72351cfb47ec Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 14:34:55 -0700 Subject: [PATCH 29/32] More unification --- tests/Feature/Departments/Api/DeleteDepartmentTest.php | 8 ++++---- .../Feature/Depreciations/Api/DeleteDepreciationTest.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Feature/Departments/Api/DeleteDepartmentTest.php b/tests/Feature/Departments/Api/DeleteDepartmentTest.php index 5865a7f2e5..3dcdd70371 100644 --- a/tests/Feature/Departments/Api/DeleteDepartmentTest.php +++ b/tests/Feature/Departments/Api/DeleteDepartmentTest.php @@ -48,9 +48,9 @@ class DeleteDepartmentTest extends TestCase implements TestsMultipleFullCompanyS ->deleteJson(route('api.departments.destroy', $departmentC)) ->assertStatusMessageIs('success'); - $this->assertNotNull($departmentA->fresh(), 'Department unexpectedly deleted'); - $this->assertNotNull($departmentB->fresh(), 'Department unexpectedly deleted'); - $this->assertNull($departmentC->fresh(), 'Department was not deleted'); + $this->assertDatabaseHas('departments', ['id' => $departmentA->id]); + $this->assertDatabaseHas('departments', ['id' => $departmentB->id]); + $this->assertDatabaseMissing('departments', ['id' => $departmentC->id]); } public function testCannotDeleteDepartmentThatStillHasUsers() @@ -61,7 +61,7 @@ class DeleteDepartmentTest extends TestCase implements TestsMultipleFullCompanyS ->deleteJson(route('api.departments.destroy', $department)) ->assertStatusMessageIs('error'); - $this->assertNotNull($department->fresh(), 'Department unexpectedly deleted'); + $this->assertDatabaseHas('departments', ['id' => $department->id]); } public function testCanDeleteDepartment() diff --git a/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php b/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php index a8ec45f6ed..277eba534b 100644 --- a/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php +++ b/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php @@ -28,7 +28,7 @@ class DeleteDepreciationTest extends TestCase implements TestsPermissionsRequire ->deleteJson(route('api.depreciations.destroy', $depreciation)) ->assertStatusMessageIs('error'); - $this->assertNotNull($depreciation->fresh(), 'Depreciation unexpectedly deleted'); + $this->assertDatabaseHas('depreciations', ['id' => $depreciation->id]); } public function testCanDeleteDepreciation() From 9a13fcab23f76a7336ffe58e38f269bfd3cea4cd Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 14:38:38 -0700 Subject: [PATCH 30/32] Pluralize --- .../Api/{DeleteDepartmentTest.php => DeleteDepartmentsTest.php} | 2 +- .../{DeleteDepreciationTest.php => DeleteDepreciationsTest.php} | 2 +- .../Groups/Api/{DeleteGroupTest.php => DeleteGroupsTest.php} | 2 +- .../Api/{DeleteLicenseTest.php => DeleteLicensesTest.php} | 2 +- .../{DeleteManufacturerTest.php => DeleteManufacturersTest.php} | 2 +- ...DeletePredefinedKitTest.php => DeletePredefinedKitsTest.php} | 2 +- .../{DeleteStatusLabelTest.php => DeleteStatusLabelsTest.php} | 2 +- .../Api/{DeleteSupplierTest.php => DeleteSuppliersTest.php} | 2 +- .../Users/Api/{DeleteUserTest.php => DeleteUsersTest.php} | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) rename tests/Feature/Departments/Api/{DeleteDepartmentTest.php => DeleteDepartmentsTest.php} (96%) rename tests/Feature/Depreciations/Api/{DeleteDepreciationTest.php => DeleteDepreciationsTest.php} (94%) rename tests/Feature/Groups/Api/{DeleteGroupTest.php => DeleteGroupsTest.php} (91%) rename tests/Feature/Licenses/Api/{DeleteLicenseTest.php => DeleteLicensesTest.php} (96%) rename tests/Feature/Manufacturers/Api/{DeleteManufacturerTest.php => DeleteManufacturersTest.php} (97%) rename tests/Feature/PredefinedKits/Api/{DeletePredefinedKitTest.php => DeletePredefinedKitsTest.php} (96%) rename tests/Feature/StatusLabels/Api/{DeleteStatusLabelTest.php => DeleteStatusLabelsTest.php} (94%) rename tests/Feature/Suppliers/Api/{DeleteSupplierTest.php => DeleteSuppliersTest.php} (95%) rename tests/Feature/Users/Api/{DeleteUserTest.php => DeleteUsersTest.php} (99%) diff --git a/tests/Feature/Departments/Api/DeleteDepartmentTest.php b/tests/Feature/Departments/Api/DeleteDepartmentsTest.php similarity index 96% rename from tests/Feature/Departments/Api/DeleteDepartmentTest.php rename to tests/Feature/Departments/Api/DeleteDepartmentsTest.php index 3dcdd70371..82c6e72700 100644 --- a/tests/Feature/Departments/Api/DeleteDepartmentTest.php +++ b/tests/Feature/Departments/Api/DeleteDepartmentsTest.php @@ -9,7 +9,7 @@ use Tests\Concerns\TestsMultipleFullCompanySupport; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteDepartmentTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement +class DeleteDepartmentsTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement { public function testRequiresPermission() { diff --git a/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php b/tests/Feature/Depreciations/Api/DeleteDepreciationsTest.php similarity index 94% rename from tests/Feature/Depreciations/Api/DeleteDepreciationTest.php rename to tests/Feature/Depreciations/Api/DeleteDepreciationsTest.php index 277eba534b..d1b32079cc 100644 --- a/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php +++ b/tests/Feature/Depreciations/Api/DeleteDepreciationsTest.php @@ -7,7 +7,7 @@ use App\Models\User; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteDepreciationTest extends TestCase implements TestsPermissionsRequirement +class DeleteDepreciationsTest extends TestCase implements TestsPermissionsRequirement { public function testRequiresPermission() { diff --git a/tests/Feature/Groups/Api/DeleteGroupTest.php b/tests/Feature/Groups/Api/DeleteGroupsTest.php similarity index 91% rename from tests/Feature/Groups/Api/DeleteGroupTest.php rename to tests/Feature/Groups/Api/DeleteGroupsTest.php index ec0766340b..8259cec84c 100644 --- a/tests/Feature/Groups/Api/DeleteGroupTest.php +++ b/tests/Feature/Groups/Api/DeleteGroupsTest.php @@ -7,7 +7,7 @@ use App\Models\User; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteGroupTest extends TestCase implements TestsPermissionsRequirement +class DeleteGroupsTest extends TestCase implements TestsPermissionsRequirement { public function testRequiresPermission() { diff --git a/tests/Feature/Licenses/Api/DeleteLicenseTest.php b/tests/Feature/Licenses/Api/DeleteLicensesTest.php similarity index 96% rename from tests/Feature/Licenses/Api/DeleteLicenseTest.php rename to tests/Feature/Licenses/Api/DeleteLicensesTest.php index 5261c339e6..b0d72925bc 100644 --- a/tests/Feature/Licenses/Api/DeleteLicenseTest.php +++ b/tests/Feature/Licenses/Api/DeleteLicensesTest.php @@ -9,7 +9,7 @@ use Tests\Concerns\TestsMultipleFullCompanySupport; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteLicenseTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement +class DeleteLicensesTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement { public function testRequiresPermission() { diff --git a/tests/Feature/Manufacturers/Api/DeleteManufacturerTest.php b/tests/Feature/Manufacturers/Api/DeleteManufacturersTest.php similarity index 97% rename from tests/Feature/Manufacturers/Api/DeleteManufacturerTest.php rename to tests/Feature/Manufacturers/Api/DeleteManufacturersTest.php index 70495e62e3..a5fbb5ee0a 100644 --- a/tests/Feature/Manufacturers/Api/DeleteManufacturerTest.php +++ b/tests/Feature/Manufacturers/Api/DeleteManufacturersTest.php @@ -9,7 +9,7 @@ use App\Models\User; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteManufacturerTest extends TestCase implements TestsPermissionsRequirement +class DeleteManufacturersTest extends TestCase implements TestsPermissionsRequirement { public function testRequiresPermission() { diff --git a/tests/Feature/PredefinedKits/Api/DeletePredefinedKitTest.php b/tests/Feature/PredefinedKits/Api/DeletePredefinedKitsTest.php similarity index 96% rename from tests/Feature/PredefinedKits/Api/DeletePredefinedKitTest.php rename to tests/Feature/PredefinedKits/Api/DeletePredefinedKitsTest.php index 265278a302..b9ff6164a7 100644 --- a/tests/Feature/PredefinedKits/Api/DeletePredefinedKitTest.php +++ b/tests/Feature/PredefinedKits/Api/DeletePredefinedKitsTest.php @@ -8,7 +8,7 @@ use Illuminate\Support\Facades\DB; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeletePredefinedKitTest extends TestCase implements TestsPermissionsRequirement +class DeletePredefinedKitsTest extends TestCase implements TestsPermissionsRequirement { public function testRequiresPermission() { diff --git a/tests/Feature/StatusLabels/Api/DeleteStatusLabelTest.php b/tests/Feature/StatusLabels/Api/DeleteStatusLabelsTest.php similarity index 94% rename from tests/Feature/StatusLabels/Api/DeleteStatusLabelTest.php rename to tests/Feature/StatusLabels/Api/DeleteStatusLabelsTest.php index 46b8c6a61d..04728c7c41 100644 --- a/tests/Feature/StatusLabels/Api/DeleteStatusLabelTest.php +++ b/tests/Feature/StatusLabels/Api/DeleteStatusLabelsTest.php @@ -7,7 +7,7 @@ use App\Models\User; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteStatusLabelTest extends TestCase implements TestsPermissionsRequirement +class DeleteStatusLabelsTest extends TestCase implements TestsPermissionsRequirement { public function testRequiresPermission() { diff --git a/tests/Feature/Suppliers/Api/DeleteSupplierTest.php b/tests/Feature/Suppliers/Api/DeleteSuppliersTest.php similarity index 95% rename from tests/Feature/Suppliers/Api/DeleteSupplierTest.php rename to tests/Feature/Suppliers/Api/DeleteSuppliersTest.php index fd619cf7bd..7da8197bde 100644 --- a/tests/Feature/Suppliers/Api/DeleteSupplierTest.php +++ b/tests/Feature/Suppliers/Api/DeleteSuppliersTest.php @@ -8,7 +8,7 @@ use App\Models\User; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteSupplierTest extends TestCase implements TestsPermissionsRequirement +class DeleteSuppliersTest extends TestCase implements TestsPermissionsRequirement { public function testRequiresPermission() { diff --git a/tests/Feature/Users/Api/DeleteUserTest.php b/tests/Feature/Users/Api/DeleteUsersTest.php similarity index 99% rename from tests/Feature/Users/Api/DeleteUserTest.php rename to tests/Feature/Users/Api/DeleteUsersTest.php index 315bb56189..d62e4028d4 100644 --- a/tests/Feature/Users/Api/DeleteUserTest.php +++ b/tests/Feature/Users/Api/DeleteUsersTest.php @@ -8,7 +8,7 @@ use App\Models\Location; use App\Models\User; use Tests\TestCase; -class DeleteUserTest extends TestCase +class DeleteUsersTest extends TestCase { public function testErrorReturnedViaApiIfUserDoesNotExist() { From a629df07bfc25da2e71a158a09cee9f5e3b1a612 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 14:49:08 -0700 Subject: [PATCH 31/32] Implement interfaces on existing test classes --- .../Locations/Api/DeleteLocationsTest.php | 14 +++++- tests/Feature/Users/Api/DeleteUsersTest.php | 48 +++++++++++-------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/tests/Feature/Locations/Api/DeleteLocationsTest.php b/tests/Feature/Locations/Api/DeleteLocationsTest.php index c023b4b3ad..796b9a1977 100644 --- a/tests/Feature/Locations/Api/DeleteLocationsTest.php +++ b/tests/Feature/Locations/Api/DeleteLocationsTest.php @@ -5,10 +5,22 @@ namespace Tests\Feature\Locations\Api; use App\Models\Asset; use App\Models\Location; use App\Models\User; +use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteLocationsTest extends TestCase +class DeleteLocationsTest extends TestCase implements TestsPermissionsRequirement { + public function testRequiresPermission() + { + $location = Location::factory()->create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.locations.destroy', $location)) + ->assertForbidden(); + + $this->assertNotSoftDeleted($location); + } + public function testErrorReturnedViaApiIfLocationDoesNotExist() { $this->actingAsForApi(User::factory()->superuser()->create()) diff --git a/tests/Feature/Users/Api/DeleteUsersTest.php b/tests/Feature/Users/Api/DeleteUsersTest.php index d62e4028d4..1014634d4f 100644 --- a/tests/Feature/Users/Api/DeleteUsersTest.php +++ b/tests/Feature/Users/Api/DeleteUsersTest.php @@ -6,10 +6,23 @@ use App\Models\Company; use App\Models\LicenseSeat; use App\Models\Location; use App\Models\User; +use Tests\Concerns\TestsMultipleFullCompanySupport; +use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteUsersTest extends TestCase +class DeleteUsersTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement { + public function testRequiresPermission() + { + $user = User::factory()->create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.users.destroy', $user)) + ->assertForbidden(); + + $this->assertNotSoftDeleted($user); + } + public function testErrorReturnedViaApiIfUserDoesNotExist() { $this->actingAsForApi(User::factory()->deleteUsers()->create()) @@ -75,25 +88,19 @@ class DeleteUsersTest extends TestCase ->json(); } - public function testDeniedPermissionsForDeletingUserViaApi() + public function testUsersCannotDeleteThemselves() { - $this->actingAsForApi(User::factory()->create()) - ->deleteJson(route('api.users.destroy', User::factory()->create())) - ->assertStatus(403) - ->json(); - } - - public function testSuccessPermissionsForDeletingUserViaApi() - { - $this->actingAsForApi(User::factory()->deleteUsers()->create()) - ->deleteJson(route('api.users.destroy', User::factory()->create())) + $user = User::factory()->deleteUsers()->create(); + $this->actingAsForApi($user) + ->deleteJson(route('api.users.destroy', $user)) ->assertOk() ->assertStatus(200) - ->assertStatusMessageIs('success') + ->assertStatusMessageIs('error') ->json(); + } - public function testPermissionsForDeletingIfNotInSameCompanyAndNotSuperadmin() + public function testAdheresToMultipleFullCompanySupportScoping() { $this->settings->enableMultipleFullCompanySupport(); @@ -132,18 +139,17 @@ class DeleteUsersTest extends TestCase $userFromA->refresh(); $this->assertNotNull($userFromA->deleted_at); - } - public function testUsersCannotDeleteThemselves() + public function testCanDeleteUser() { - $user = User::factory()->deleteUsers()->create(); - $this->actingAsForApi($user) + $user = User::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteUsers()->create()) ->deleteJson(route('api.users.destroy', $user)) ->assertOk() - ->assertStatus(200) - ->assertStatusMessageIs('error') - ->json(); + ->assertStatusMessageIs('success'); + $this->assertSoftDeleted($user); } } From 3519a82dddedebfd518cbff61ddc1e1c979859fe Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 16:55:20 -0700 Subject: [PATCH 32/32] Fix name: TestsFullMultipleCompaniesSupport --- tests/Concerns/TestsFullMultipleCompaniesSupport.php | 8 ++++++++ tests/Concerns/TestsMultipleFullCompanySupport.php | 8 -------- tests/Feature/Accessories/Api/DeleteAccessoriesTest.php | 6 +++--- .../AssetMaintenances/Api/DeleteAssetMaintenancesTest.php | 6 +++--- tests/Feature/Assets/Api/DeleteAssetsTest.php | 6 +++--- tests/Feature/Components/Api/DeleteComponentsTest.php | 6 +++--- tests/Feature/Consumables/Api/DeleteConsumablesTest.php | 6 +++--- tests/Feature/Departments/Api/DeleteDepartmentsTest.php | 6 +++--- tests/Feature/Licenses/Api/DeleteLicensesTest.php | 6 +++--- tests/Feature/Users/Api/DeleteUsersTest.php | 6 +++--- 10 files changed, 32 insertions(+), 32 deletions(-) create mode 100644 tests/Concerns/TestsFullMultipleCompaniesSupport.php delete mode 100644 tests/Concerns/TestsMultipleFullCompanySupport.php diff --git a/tests/Concerns/TestsFullMultipleCompaniesSupport.php b/tests/Concerns/TestsFullMultipleCompaniesSupport.php new file mode 100644 index 0000000000..a52d27bd22 --- /dev/null +++ b/tests/Concerns/TestsFullMultipleCompaniesSupport.php @@ -0,0 +1,8 @@ +assertNotSoftDeleted($accessory); } - public function testAdheresToMultipleFullCompanySupportScoping() + public function testAdheresToFullMultipleCompaniesSupportScoping() { [$companyA, $companyB] = Company::factory()->count(2)->create(); diff --git a/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenancesTest.php b/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenancesTest.php index ed7ed44447..8a0189bc5b 100644 --- a/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenancesTest.php +++ b/tests/Feature/AssetMaintenances/Api/DeleteAssetMaintenancesTest.php @@ -5,11 +5,11 @@ namespace Tests\Feature\AssetMaintenances\Api; use App\Models\AssetMaintenance; use App\Models\Company; use App\Models\User; -use Tests\Concerns\TestsMultipleFullCompanySupport; +use Tests\Concerns\TestsFullMultipleCompaniesSupport; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteAssetMaintenancesTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement +class DeleteAssetMaintenancesTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement { public function testRequiresPermission() { @@ -22,7 +22,7 @@ class DeleteAssetMaintenancesTest extends TestCase implements TestsMultipleFullC $this->assertNotSoftDeleted($assetMaintenance); } - public function testAdheresToMultipleFullCompanySupportScoping() + public function testAdheresToFullMultipleCompaniesSupportScoping() { [$companyA, $companyB] = Company::factory()->count(2)->create(); diff --git a/tests/Feature/Assets/Api/DeleteAssetsTest.php b/tests/Feature/Assets/Api/DeleteAssetsTest.php index 95640ed83f..5a017e0f5d 100644 --- a/tests/Feature/Assets/Api/DeleteAssetsTest.php +++ b/tests/Feature/Assets/Api/DeleteAssetsTest.php @@ -5,11 +5,11 @@ namespace Tests\Feature\Assets\Api; use App\Models\Asset; use App\Models\Company; use App\Models\User; -use Tests\Concerns\TestsMultipleFullCompanySupport; +use Tests\Concerns\TestsFullMultipleCompaniesSupport; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteAssetsTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement +class DeleteAssetsTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement { public function testRequiresPermission() { @@ -22,7 +22,7 @@ class DeleteAssetsTest extends TestCase implements TestsMultipleFullCompanySuppo $this->assertNotSoftDeleted($asset); } - public function testAdheresToMultipleFullCompanySupportScoping() + public function testAdheresToFullMultipleCompaniesSupportScoping() { [$companyA, $companyB] = Company::factory()->count(2)->create(); diff --git a/tests/Feature/Components/Api/DeleteComponentsTest.php b/tests/Feature/Components/Api/DeleteComponentsTest.php index df965d745c..e95fe34559 100644 --- a/tests/Feature/Components/Api/DeleteComponentsTest.php +++ b/tests/Feature/Components/Api/DeleteComponentsTest.php @@ -5,11 +5,11 @@ namespace Tests\Feature\Components\Api; use App\Models\Company; use App\Models\Component; use App\Models\User; -use Tests\Concerns\TestsMultipleFullCompanySupport; +use Tests\Concerns\TestsFullMultipleCompaniesSupport; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteComponentsTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement +class DeleteComponentsTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement { public function testRequiresPermission() { @@ -22,7 +22,7 @@ class DeleteComponentsTest extends TestCase implements TestsMultipleFullCompanyS $this->assertNotSoftDeleted($component); } - public function testAdheresToMultipleFullCompanySupportScoping() + public function testAdheresToFullMultipleCompaniesSupportScoping() { [$companyA, $companyB] = Company::factory()->count(2)->create(); diff --git a/tests/Feature/Consumables/Api/DeleteConsumablesTest.php b/tests/Feature/Consumables/Api/DeleteConsumablesTest.php index c57e2e0df8..1ab91e07a0 100644 --- a/tests/Feature/Consumables/Api/DeleteConsumablesTest.php +++ b/tests/Feature/Consumables/Api/DeleteConsumablesTest.php @@ -5,11 +5,11 @@ namespace Tests\Feature\Consumables\Api; use App\Models\Company; use App\Models\Consumable; use App\Models\User; -use Tests\Concerns\TestsMultipleFullCompanySupport; +use Tests\Concerns\TestsFullMultipleCompaniesSupport; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteConsumablesTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement +class DeleteConsumablesTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement { public function testRequiresPermission() { @@ -22,7 +22,7 @@ class DeleteConsumablesTest extends TestCase implements TestsMultipleFullCompany $this->assertNotSoftDeleted($consumable); } - public function testAdheresToMultipleFullCompanySupportScoping() + public function testAdheresToFullMultipleCompaniesSupportScoping() { [$companyA, $companyB] = Company::factory()->count(2)->create(); diff --git a/tests/Feature/Departments/Api/DeleteDepartmentsTest.php b/tests/Feature/Departments/Api/DeleteDepartmentsTest.php index 82c6e72700..cf59b81510 100644 --- a/tests/Feature/Departments/Api/DeleteDepartmentsTest.php +++ b/tests/Feature/Departments/Api/DeleteDepartmentsTest.php @@ -5,11 +5,11 @@ namespace Tests\Feature\Departments\Api; use App\Models\Company; use App\Models\Department; use App\Models\User; -use Tests\Concerns\TestsMultipleFullCompanySupport; +use Tests\Concerns\TestsFullMultipleCompaniesSupport; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteDepartmentsTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement +class DeleteDepartmentsTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement { public function testRequiresPermission() { @@ -22,7 +22,7 @@ class DeleteDepartmentsTest extends TestCase implements TestsMultipleFullCompany $this->assertDatabaseHas('departments', ['id' => $department->id]); } - public function testAdheresToMultipleFullCompanySupportScoping() + public function testAdheresToFullMultipleCompaniesSupportScoping() { [$companyA, $companyB] = Company::factory()->count(2)->create(); diff --git a/tests/Feature/Licenses/Api/DeleteLicensesTest.php b/tests/Feature/Licenses/Api/DeleteLicensesTest.php index b0d72925bc..7db8e588d3 100644 --- a/tests/Feature/Licenses/Api/DeleteLicensesTest.php +++ b/tests/Feature/Licenses/Api/DeleteLicensesTest.php @@ -5,11 +5,11 @@ namespace Tests\Feature\Licenses\Api; use App\Models\Company; use App\Models\License; use App\Models\User; -use Tests\Concerns\TestsMultipleFullCompanySupport; +use Tests\Concerns\TestsFullMultipleCompaniesSupport; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteLicensesTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement +class DeleteLicensesTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement { public function testRequiresPermission() { @@ -22,7 +22,7 @@ class DeleteLicensesTest extends TestCase implements TestsMultipleFullCompanySup $this->assertNotSoftDeleted($license); } - public function testAdheresToMultipleFullCompanySupportScoping() + public function testAdheresToFullMultipleCompaniesSupportScoping() { [$companyA, $companyB] = Company::factory()->count(2)->create(); diff --git a/tests/Feature/Users/Api/DeleteUsersTest.php b/tests/Feature/Users/Api/DeleteUsersTest.php index 1014634d4f..9677e5f7d4 100644 --- a/tests/Feature/Users/Api/DeleteUsersTest.php +++ b/tests/Feature/Users/Api/DeleteUsersTest.php @@ -6,11 +6,11 @@ use App\Models\Company; use App\Models\LicenseSeat; use App\Models\Location; use App\Models\User; -use Tests\Concerns\TestsMultipleFullCompanySupport; +use Tests\Concerns\TestsFullMultipleCompaniesSupport; use Tests\Concerns\TestsPermissionsRequirement; use Tests\TestCase; -class DeleteUsersTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement +class DeleteUsersTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement { public function testRequiresPermission() { @@ -100,7 +100,7 @@ class DeleteUsersTest extends TestCase implements TestsMultipleFullCompanySuppor } - public function testAdheresToMultipleFullCompanySupportScoping() + public function testAdheresToFullMultipleCompaniesSupportScoping() { $this->settings->enableMultipleFullCompanySupport();