From a54403ef0150b3ee7398729b15d9b9208c7b5368 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 19 Jul 2024 22:26:52 +0100 Subject: [PATCH 1/3] Added bulk asset delete test Signed-off-by: snipe --- .../Assets/Ui/BulkDeleteAssetsTest.php | 166 ++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 tests/Feature/Assets/Ui/BulkDeleteAssetsTest.php diff --git a/tests/Feature/Assets/Ui/BulkDeleteAssetsTest.php b/tests/Feature/Assets/Ui/BulkDeleteAssetsTest.php new file mode 100644 index 0000000000..d1375c5393 --- /dev/null +++ b/tests/Feature/Assets/Ui/BulkDeleteAssetsTest.php @@ -0,0 +1,166 @@ +viewAssets()->deleteAssets()->editAssets()->create(); + $assets = Asset::factory()->count(2)->create(); + + $id_array = $assets->pluck('id')->toArray(); + + $this->actingAs($user)->post('/hardware/bulkedit', [ + 'ids' => $id_array, + 'order' => 'asc', + 'bulk_actions' => 'delete', + 'sort' => 'id' + ])->assertStatus(200); + } + + public function testStandardUserCannotAccessPage() + { + $user = User::factory()->create(); + $assets = Asset::factory()->count(2)->create(); + + $id_array = $assets->pluck('id')->toArray(); + + $this->actingAs($user)->post('/hardware/bulkdelete', [ + 'ids' => $id_array, + 'bulk_actions' => 'delete', + ])->assertStatus(403); + } + + public function testPageRedirectFromInterstitialIfNoAssetsSelectedToDelete() + { + $user = User::factory()->viewAssets()->deleteAssets()->editAssets()->create(); + $response = $this->actingAs($user) + ->post('/hardware/bulkdelete', [ + 'ids' => null, + 'bulk_actions' => 'delete', + ]) + ->assertStatus(302) + ->assertRedirect(route('hardware.index')); + + $this->followRedirects($response)->assertSee('alert-danger'); + } + + public function testPageRedirectFromInterstitialIfNoAssetsSelectedToRestore() + { + $user = User::factory()->viewAssets()->deleteAssets()->editAssets()->create(); + $response = $this->actingAs($user) + ->from(route('hardware.index')) + ->post('/hardware/bulkrestore', [ + 'ids' => null, + 'bulk_actions' => 'delete', + ]) + ->assertStatus(302) + ->assertRedirect(route('hardware.index')); + + $this->followRedirects($response)->assertSee('alert-danger'); + } + + + public function testBulkDeleteSelectedAssetsFromInterstitial() + { + $user = User::factory()->viewAssets()->deleteAssets()->editAssets()->create(); + $assets = Asset::factory()->count(2)->create(); + + $id_array = $assets->pluck('id')->toArray(); + + $response = $this->actingAs($user) + ->from(route('hardware/bulkedit')) + ->post('/hardware/bulkdelete', [ + 'ids' => $id_array, + 'bulk_actions' => 'delete', + ])->assertStatus(302); + + Asset::findMany($id_array)->each(function (Asset $asset) { + $this->assertNotNull($asset->deleted_at); + }); + + $this->followRedirects($response)->assertSee('alert-success'); + } + + public function testBulkRestoreSelectedAssetsFromInterstitial() + { + $user = User::factory()->viewAssets()->deleteAssets()->editAssets()->create(); + $asset = Asset::factory()->deleted()->create(); + + $asset->refresh(); + $id_array = $asset->pluck('id')->toArray(); + + // Check that the assets are deleted + Asset::findMany($id_array)->each(function (Asset $asset) { + $this->assertNull($asset->deleted_at); + }); + + $response = $this->actingAs($user) + ->from(route('hardware/bulkedit')) + ->post(route('hardware/bulkrestore'), [ + 'ids' => [$asset->id], + ])->assertStatus(302); + + $this->followRedirects($response)->assertSee('alert-success'); + + Asset::findMany($id_array)->each(function (Asset $asset) { + $this->assertNull($asset->deleted_at); + }); + } + + + public function testActionLogCreatedUponBulkDelete() + { + $user = User::factory()->viewAssets()->deleteAssets()->editAssets()->create(); + $asset = Asset::factory()->create(); + + $this->actingAs($user) + ->from(route('hardware/bulkedit')) + ->post('/hardware/bulkdelete', [ + 'ids' => [$asset->id], + 'bulk_actions' => 'delete', + ]); + + $this->assertDatabaseHas('action_logs', + [ + 'action_type' => 'delete', + 'target_id' => null, + 'target_type' => null, + 'item_id' => $asset->id, + 'item_type' => Asset::class, + ] + ); + } + + public function testActionLogCreatedUponBulkRestore() + { + $user = User::factory()->viewAssets()->deleteAssets()->editAssets()->create(); + $asset = Asset::factory()->deleted()->create(); + + $this->actingAs($user) + ->from(route('hardware/bulkedit')) + ->post(route('hardware/bulkrestore'), [ + 'ids' => [$asset->id], + 'bulk_actions' => 'restore', + ]); + + $this->assertDatabaseHas('action_logs', + [ + 'action_type' => 'restore', + 'target_id' => null, + 'target_type' => null, + 'item_id' => $asset->id, + 'item_type' => Asset::class, + ] + ); + } + + +} From 4b5bd76225fcb59b84655cd817857078bf170e88 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 19 Jul 2024 22:27:03 +0100 Subject: [PATCH 2/3] Fixed notification use statement Signed-off-by: snipe --- .../Feature/CheckoutAcceptances/Ui/AccessoryAcceptanceTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/CheckoutAcceptances/Ui/AccessoryAcceptanceTest.php b/tests/Feature/CheckoutAcceptances/Ui/AccessoryAcceptanceTest.php index 2390b16808..fef38623f8 100644 --- a/tests/Feature/CheckoutAcceptances/Ui/AccessoryAcceptanceTest.php +++ b/tests/Feature/CheckoutAcceptances/Ui/AccessoryAcceptanceTest.php @@ -8,7 +8,7 @@ use App\Models\CheckoutAcceptance; use App\Models\User; use App\Notifications\AcceptanceAssetAcceptedNotification; use App\Notifications\AcceptanceAssetDeclinedNotification; -use Notification; +use Illuminate\Support\Facades\Notification; use Tests\TestCase; class AccessoryAcceptanceTest extends TestCase From 793cf2731843dad54ba5ff676a72555884a7b1f3 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 19 Jul 2024 22:50:24 +0100 Subject: [PATCH 3/3] Fixed use statement Signed-off-by: snipe --- app/Importer/AssetImporter.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/app/Importer/AssetImporter.php b/app/Importer/AssetImporter.php index c32a9fb4c6..4bb887bcd8 100644 --- a/app/Importer/AssetImporter.php +++ b/app/Importer/AssetImporter.php @@ -3,14 +3,10 @@ namespace App\Importer; use App\Models\Asset; -use App\Models\AssetModel; use App\Models\Statuslabel; use App\Models\User; use App\Events\CheckoutableCheckedIn; -use Carbon\CarbonImmutable; -use Illuminate\Support\Facades\Auth; -use Carbon\Carbon; -use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\Crypt; class AssetImporter extends ItemImporter {