A free open source IT asset/license management system
Add more action logs tests everywhere I can think of it.
Brady Wetherington7c5110ed5d78
diff --git a/tests/Feature/Assets/Api/AuditAssetTest.php b/tests/Feature/Assets/Api/AuditAssetTest.phpindex f93a65ae6d..b114cf8503 100644--- a/tests/Feature/Assets/Api/AuditAssetTest.php+++ b/tests/Feature/Assets/Api/AuditAssetTest.php@@ -71,7 +71,7 @@ class AuditAssetTest extends TestCase],])->assertStatus(200);-+ $this->assertHasTheseActionLogs($asset, ['create', 'audit']);}diff --git a/tests/Feature/Assets/Api/DeleteAssetsTest.php b/tests/Feature/Assets/Api/DeleteAssetsTest.phpindex 5a017e0f5d..e6739956cc 100644--- a/tests/Feature/Assets/Api/DeleteAssetsTest.php+++ b/tests/Feature/Assets/Api/DeleteAssetsTest.php@@ -67,5 +67,6 @@ class DeleteAssetsTest extends TestCase implements TestsFullMultipleCompaniesSup->assertStatusMessageIs('success');$this->assertSoftDeleted($asset);+ $this->assertHasTheseActionLogs($asset, ['create', 'delete']);}}diff --git a/tests/Feature/Assets/Api/StoreAssetTest.php b/tests/Feature/Assets/Api/StoreAssetTest.phpindex 1ac234e027..7401be7c62 100644--- a/tests/Feature/Assets/Api/StoreAssetTest.php+++ b/tests/Feature/Assets/Api/StoreAssetTest.php@@ -13,12 +13,10 @@ use App\Models\User;use Illuminate\Support\Facades\Crypt;use Illuminate\Testing\Fluent\AssertableJson;use PHPUnit\Framework\Attributes\DataProvider;-use Tests\Support\AssertsActionLogs;use Tests\TestCase;class StoreAssetTest extends TestCase{- use AssertsActionLogs;public function testRequiresPermissionToCreateAsset(){diff --git a/tests/Feature/Assets/Ui/AuditAssetTest.php b/tests/Feature/Assets/Ui/AuditAssetTest.phpindex f25ea5e2be..7749054ce9 100644--- a/tests/Feature/Assets/Ui/AuditAssetTest.php+++ b/tests/Feature/Assets/Ui/AuditAssetTest.php@@ -36,6 +36,7 @@ class AuditAssetTest extends TestCase->assertRedirect(route('hardware.index'));$this->followRedirects($response)->assertSee('success');+ $this->assertHasTheseActionLogs($asset, ['create', 'audit']);}public function testAssetAuditPostIsRedirectedToAssetPageIfRedirectSelectionIsAsset()@@ -51,6 +52,7 @@ class AuditAssetTest extends TestCase->assertStatus(302)->assertRedirect(route('hardware.show', $asset));$this->followRedirects($response)->assertSee('success');+ $this->assertHasTheseActionLogs($asset, ['create', 'audit']);}public function testAssetAuditPostIsRedirectedToAuditDuePageIfRedirectSelectionIsList()@@ -66,6 +68,7 @@ class AuditAssetTest extends TestCase->assertStatus(302)->assertRedirect(route('assets.audit.due'));$this->followRedirects($response)->assertSee('success');+ $this->assertHasTheseActionLogs($asset, ['create', 'audit']);}}diff --git a/tests/Feature/Assets/Ui/BulkDeleteAssetsTest.php b/tests/Feature/Assets/Ui/BulkDeleteAssetsTest.phpindex ec8ed33ba8..eebdd074a4 100644--- a/tests/Feature/Assets/Ui/BulkDeleteAssetsTest.php+++ b/tests/Feature/Assets/Ui/BulkDeleteAssetsTest.php@@ -84,6 +84,7 @@ class BulkDeleteAssetsTest extends TestCaseAsset::findMany($id_array)->each(function (Asset $asset) {$this->assertNotNull($asset->deleted_at);+ $this->assertHasTheseActionLogs($asset, ['create', 'delete']);});$this->followRedirects($response)->assertSee('alert-success');@@ -93,13 +94,17 @@ class BulkDeleteAssetsTest extends TestCase{$user = User::factory()->viewAssets()->deleteAssets()->editAssets()->create();$asset = Asset::factory()->deleted()->create();+ $this->assertNotNull($asset);$asset->refresh();- $id_array = $asset->pluck('id')->toArray();+ $id_array = [$asset->id];+ $this->assertEquals(1, count($id_array));// Check that the assets are deleted- Asset::findMany($id_array)->each(function (Asset $asset) {- $this->assertNull($asset->deleted_at);+ Asset::whereIn('id', $id_array)->withTrashed()->each(function (Asset $asset) {+ $this->fail('ppoooo');+ $this->assertFalse(true);+ $this->assertNotNull($asset->deleted_at);});$response = $this->actingAs($user)@@ -112,7 +117,9 @@ class BulkDeleteAssetsTest extends TestCaseAsset::findMany($id_array)->each(function (Asset $asset) {$this->assertNull($asset->deleted_at);+ $this->assertHasTheseActionLogs($asset, ['create', 'delete', 'restore', 'fart']); //SHIT});+ $this->assertTrue(false);}diff --git a/tests/Feature/Checkins/Api/AccessoryCheckinTest.php b/tests/Feature/Checkins/Api/AccessoryCheckinTest.phpindex fefca0efff..d672744b52 100644--- a/tests/Feature/Checkins/Api/AccessoryCheckinTest.php+++ b/tests/Feature/Checkins/Api/AccessoryCheckinTest.php@@ -7,12 +7,10 @@ use App\Models\Company;use App\Models\User;use Tests\Concerns\TestsFullMultipleCompaniesSupport;use Tests\Concerns\TestsPermissionsRequirement;-use Tests\Support\AssertsActionLogs;use Tests\TestCase;class AccessoryCheckinTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement{- use AssertsActionLogs;public function testRequiresPermission(){$accessory = Accessory::factory()->checkedOutToUser()->create();diff --git a/tests/Feature/Checkins/Api/AssetCheckinTest.php b/tests/Feature/Checkins/Api/AssetCheckinTest.phpindex a6b342b773..977386938a 100644--- a/tests/Feature/Checkins/Api/AssetCheckinTest.php+++ b/tests/Feature/Checkins/Api/AssetCheckinTest.php@@ -11,12 +11,10 @@ use App\Models\Statuslabel;use App\Models\User;use Illuminate\Support\Carbon;use Illuminate\Support\Facades\Event;-use Tests\Support\AssertsActionLogs;use Tests\TestCase;class AssetCheckinTest extends TestCase{- use AssertsActionLogs;public function testCheckingInAssetRequiresCorrectPermission(){$this->actingAsForApi(User::factory()->create())diff --git a/tests/Feature/Checkins/Api/ComponentCheckinTest.php b/tests/Feature/Checkins/Api/ComponentCheckinTest.phpindex 51ad4a0910..58a56ba9ad 100644--- a/tests/Feature/Checkins/Api/ComponentCheckinTest.php+++ b/tests/Feature/Checkins/Api/ComponentCheckinTest.php@@ -10,12 +10,10 @@ use App\Models\User;use Illuminate\Support\Facades\Event;use Tests\Concerns\TestsFullMultipleCompaniesSupport;use Tests\Concerns\TestsPermissionsRequirement;-use Tests\Support\AssertsActionLogs;use Tests\TestCase;class ComponentCheckinTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement{- use AssertsActionLogs;public function testRequiresPermission(){$component = Component::factory()->checkedOutToAsset()->create();diff --git a/tests/Feature/Checkins/Api/LicenseCheckInTest.php b/tests/Feature/Checkins/Api/LicenseCheckInTest.phpindex abe03e4061..0fdcab4704 100644--- a/tests/Feature/Checkins/Api/LicenseCheckInTest.php+++ b/tests/Feature/Checkins/Api/LicenseCheckInTest.php@@ -4,11 +4,9 @@ namespace Tests\Feature\Checkins\Api;use App\Models\License;use App\Models\LicenseSeat;use App\Models\User;-use Tests\Support\AssertsActionLogs;use Tests\TestCase;class LicenseCheckInTest extends TestCase {- use AssertsActionLogs;public function testLicenseCheckin(){$authUser = User::factory()->superuser()->create();diff --git a/tests/Feature/Checkins/Ui/AssetCheckinTest.php b/tests/Feature/Checkins/Ui/AssetCheckinTest.phpindex 98ac9999a5..bb0fe79298 100644--- a/tests/Feature/Checkins/Ui/AssetCheckinTest.php+++ b/tests/Feature/Checkins/Ui/AssetCheckinTest.php@@ -11,12 +11,10 @@ use App\Models\Statuslabel;use App\Models\User;use Illuminate\Support\Carbon;use Illuminate\Support\Facades\Event;-use Tests\Support\AssertsActionLogs;use Tests\TestCase;class AssetCheckinTest extends TestCase{- use AssertsActionLogs;public function testCheckingInAssetRequiresCorrectPermission(){$this->actingAs(User::factory()->create())diff --git a/tests/Feature/Checkins/Ui/ComponentCheckinTest.php b/tests/Feature/Checkins/Ui/ComponentCheckinTest.phpindex 99a8b3eb4b..4672b870e4 100644--- a/tests/Feature/Checkins/Ui/ComponentCheckinTest.php+++ b/tests/Feature/Checkins/Ui/ComponentCheckinTest.php@@ -5,12 +5,10 @@ namespace Tests\Feature\Checkins\Ui;use App\Models\Component;use App\Models\User;use Illuminate\Support\Facades\DB;-use Tests\Support\AssertsActionLogs;use Tests\TestCase;class ComponentCheckinTest extends TestCase{- use AssertsActionLogs;public function testCheckingInComponentRequiresCorrectPermission(){diff --git a/tests/Feature/Checkouts/Api/AccessoryCheckoutTest.php b/tests/Feature/Checkouts/Api/AccessoryCheckoutTest.phpindex a974f455ab..2af6cf49a2 100644--- a/tests/Feature/Checkouts/Api/AccessoryCheckoutTest.php+++ b/tests/Feature/Checkouts/Api/AccessoryCheckoutTest.php@@ -10,12 +10,10 @@ use App\Notifications\CheckoutAccessoryNotification;use Illuminate\Support\Facades\Mail;use Illuminate\Support\Facades\Notification;use Tests\Concerns\TestsPermissionsRequirement;-use Tests\Support\AssertsActionLogs;use Tests\TestCase;class AccessoryCheckoutTest extends TestCase implements TestsPermissionsRequirement{- use AssertsActionLogs;public function testRequiresPermission(){diff --git a/tests/Feature/Checkouts/Api/AssetCheckoutTest.php b/tests/Feature/Checkouts/Api/AssetCheckoutTest.phpindex 359572ef4a..d7096a5288 100644--- a/tests/Feature/Checkouts/Api/AssetCheckoutTest.php+++ b/tests/Feature/Checkouts/Api/AssetCheckoutTest.php@@ -12,12 +12,10 @@ use App\Models\Statuslabel;use App\Models\User;use Illuminate\Support\Carbon;use Illuminate\Support\Facades\Event;-use Tests\Support\AssertsActionLogs;use Tests\TestCase;class AssetCheckoutTest extends TestCase{- use AssertsActionLogs;protected function setUp(): void{parent::setUp();diff --git a/tests/Feature/Checkouts/Api/ComponentCheckoutTest.php b/tests/Feature/Checkouts/Api/ComponentCheckoutTest.phpindex 179e9329e2..50f9df8641 100644--- a/tests/Feature/Checkouts/Api/ComponentCheckoutTest.php+++ b/tests/Feature/Checkouts/Api/ComponentCheckoutTest.php@@ -96,6 +96,7 @@ class ComponentCheckoutTest extends TestCase implements TestsFullMultipleCompani->assertStatusMessageIs('success');$this->assertTrue($component->assets->first()->is($asset));+ $this->assertHasTheseActionLogs($component, ['create', 'checkout']);}public function testComponentCheckoutIsLogged()diff --git a/tests/Feature/Checkouts/Api/ConsumableCheckoutTest.php b/tests/Feature/Checkouts/Api/ConsumableCheckoutTest.phpindex fba2c50df3..44aa5e11ea 100644--- a/tests/Feature/Checkouts/Api/ConsumableCheckoutTest.php+++ b/tests/Feature/Checkouts/Api/ConsumableCheckoutTest.php@@ -49,6 +49,7 @@ class ConsumableCheckoutTest extends TestCase]);$this->assertTrue($user->consumables->contains($consumable));+ $this->assertHasTheseActionLogs($consumable, ['create', 'checkout']);}public function testUserSentNotificationUponCheckout()diff --git a/tests/Feature/Checkouts/Api/LicenseCheckOutTest.php b/tests/Feature/Checkouts/Api/LicenseCheckOutTest.phpindex 7a069b7d28..a8cb2db12c 100644--- a/tests/Feature/Checkouts/Api/LicenseCheckOutTest.php+++ b/tests/Feature/Checkouts/Api/LicenseCheckOutTest.php@@ -37,5 +37,6 @@ class LicenseCheckOutTest extends TestCase {$this->assertEquals($targetUser->id, $licenseSeat->assigned_to);$this->assertEquals('Checking out the seat to a user', $licenseSeat->notes);+ $this->assertHasTheseActionLogs($license, ['add seats', 'create', 'checkout']); //FIXME - backwards}}\ No newline at end of filediff --git a/tests/Feature/Checkouts/Ui/AccessoryCheckoutTest.php b/tests/Feature/Checkouts/Ui/AccessoryCheckoutTest.phpindex 19b0d1ce72..f9e84015e2 100644--- a/tests/Feature/Checkouts/Ui/AccessoryCheckoutTest.php+++ b/tests/Feature/Checkouts/Ui/AccessoryCheckoutTest.php@@ -83,6 +83,7 @@ class AccessoryCheckoutTest extends TestCase'item_type' => Accessory::class,'note' => 'oh hi there',]);+ $this->assertHasTheseActionLogs($accessory, ['create', 'checkout']);}public function testAccessoryCanBeCheckedOutWithQuantity()@@ -109,6 +110,7 @@ class AccessoryCheckoutTest extends TestCase'item_type' => Accessory::class,'note' => 'oh hi there',]);+ $this->assertHasTheseActionLogs($accessory, ['create', 'checkout']);}public function testAccessoryCanBeCheckedOutToLocationWithQuantity()@@ -135,6 +137,7 @@ class AccessoryCheckoutTest extends TestCase'item_type' => Accessory::class,'note' => 'oh hi there',]);+ $this->assertHasTheseActionLogs($accessory, ['create', 'checkout']);}public function testAccessoryCanBeCheckedOutToAssetWithQuantity()@@ -161,6 +164,7 @@ class AccessoryCheckoutTest extends TestCase'item_type' => Accessory::class,'note' => 'oh hi there',]);+ $this->assertHasTheseActionLogs($accessory, ['create', 'checkout']);}public function testUserSentNotificationUponCheckout()@@ -209,6 +213,7 @@ class AccessoryCheckoutTest extends TestCase])->count(),'Log entry either does not exist or there are more than expected');+ $this->assertHasTheseActionLogs($accessory, ['create', 'checkout']);}public function testAccessoryCheckoutPagePostIsRedirectedIfRedirectSelectionIsIndex()diff --git a/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php b/tests/Feature/Checkouts/Ui/AssetCheckoutTest.phpindex 0019791752..c8bc5e47f5 100644--- a/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php+++ b/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php@@ -218,6 +218,7 @@ class AssetCheckoutTest extends TestCasereturn true;});+ $this->assertHasTheseActionLogs($asset, ['create', 'checkout']); //FIXME - only getting one?}public function testLicenseSeatsAreAssignedToUserUponCheckout()diff --git a/tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.php b/tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.phpindex 08d6cf6841..f05c4389e6 100644--- a/tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.php+++ b/tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.php@@ -55,6 +55,7 @@ class BulkAssetCheckoutTest extends TestCase$asset->assignedTo()->is($user);$asset->last_checkout = $checkoutAt;$asset->expected_checkin = $expectedCheckin;+ $this->assertHasTheseActionLogs($asset, ['create', 'checkout']); //Note: '$this' gets auto-bound in closures, so this does work.});Mail::assertSent(CheckoutAssetMail::class, 2);diff --git a/tests/Feature/Checkouts/Ui/ComponentsCheckoutTest.php b/tests/Feature/Checkouts/Ui/ComponentsCheckoutTest.phpindex 18568d1985..96c7b89322 100644--- a/tests/Feature/Checkouts/Ui/ComponentsCheckoutTest.php+++ b/tests/Feature/Checkouts/Ui/ComponentsCheckoutTest.php@@ -62,6 +62,7 @@ class ComponentsCheckoutTest extends TestCase])->assertStatus(302)->assertRedirect(route('components.index'));+ $this->assertHasTheseActionLogs($component, ['create', 'checkout']);}public function testComponentCheckoutPagePostIsRedirectedIfRedirectSelectionIsItem()@@ -77,6 +78,7 @@ class ComponentsCheckoutTest extends TestCase])->assertStatus(302)->assertRedirect(route('components.show', $component));+ $this->assertHasTheseActionLogs($component, ['create', 'checkout']);}public function testComponentCheckoutPagePostIsRedirectedIfRedirectSelectionIsTarget()@@ -93,5 +95,6 @@ class ComponentsCheckoutTest extends TestCase])->assertStatus(302)->assertRedirect(route('hardware.show', $asset));+ $this->assertHasTheseActionLogs($component, ['create', 'checkout']);}}diff --git a/tests/Feature/Checkouts/Ui/ConsumableCheckoutTest.php b/tests/Feature/Checkouts/Ui/ConsumableCheckoutTest.phpindex 2c74e4d0cb..fa92cb7417 100644--- a/tests/Feature/Checkouts/Ui/ConsumableCheckoutTest.php+++ b/tests/Feature/Checkouts/Ui/ConsumableCheckoutTest.php@@ -57,6 +57,7 @@ class ConsumableCheckoutTest extends TestCase]);$this->assertTrue($user->consumables->contains($consumable));+ $this->assertHasTheseActionLogs($consumable, ['create', 'checkout']);}public function testUserSentNotificationUponCheckout()diff --git a/tests/Feature/Checkouts/Ui/LicenseCheckoutTest.php b/tests/Feature/Checkouts/Ui/LicenseCheckoutTest.phpindex 1750b75647..35645074b9 100644--- a/tests/Feature/Checkouts/Ui/LicenseCheckoutTest.php+++ b/tests/Feature/Checkouts/Ui/LicenseCheckoutTest.php@@ -39,6 +39,7 @@ class LicenseCheckoutTest extends TestCase'item_type' => License::class,'note' => 'oh hi there',]);+ $this->assertHasTheseActionLogs($licenseSeat->license, ['create', 'add seats', 'checkout']); //FIXME - out-of-order}public function testNotesAreStoredInActionLogOnCheckoutToUser()@@ -62,6 +63,7 @@ class LicenseCheckoutTest extends TestCase'item_type' => License::class,'note' => 'oh hi there',]);+ $this->assertHasTheseActionLogs($licenseSeat->license, ['create', 'add seats', 'checkout']); //FIXME - out-of-order}public function testLicenseCheckoutPagePostIsRedirectedIfRedirectSelectionIsIndex()diff --git a/tests/Support/AssertsActionLogs.php b/tests/Support/AssertsActionLogs.phpdeleted file mode 100644index 1a7868ab37..0000000000--- a/tests/Support/AssertsActionLogs.php+++ /dev/null@@ -1,26 +0,0 @@-<?php--namespace Tests\Support;--use App\Models\Actionlog;-use Illuminate\Database\Eloquent\Model;-use Illuminate\Testing\TestResponse;-use PHPUnit\Framework\Assert;--trait AssertsActionLogs-{-- public function assertHasTheseActionLogs(Model $item, array $statuses)- {- \Log::error("Okay, we're running the test macro now?");- $logs = Actionlog::where(['item_id' => $item->id, 'item_type' => get_class($item)])->orderBy('id')->get();- Assert::assertEquals(count($statuses), count($logs), "Wrong count of logs expected - expecting " . count($statuses) . ", got " . count($logs));- $i = 0;- foreach ($statuses as $status) {- Assert::assertEquals($status, $logs[$i]->action_type, "Unexpected action type - " . $logs[$i]->action_type . " - expecting $status");- $i++;- }-- }-}-diff --git a/tests/TestCase.php b/tests/TestCase.phpindex dfff2427af..0177767e67 100644--- a/tests/TestCase.php+++ b/tests/TestCase.php@@ -3,8 +3,11 @@namespace Tests;use App\Http\Middleware\SecurityHeaders;+use App\Models\Actionlog;+use Illuminate\Database\Eloquent\Model;use Illuminate\Foundation\Testing\LazilyRefreshDatabase;use Illuminate\Foundation\Testing\TestCase as BaseTestCase;+use PHPUnit\Framework\Assert;use RuntimeException;use Tests\Support\AssertsAgainstSlackNotifications;use Tests\Support\CanSkipTests;@@ -47,4 +50,17 @@ abstract class TestCase extends BaseTestCase);}}++ public function assertHasTheseActionLogs(Model $item, array $statuses)+ {+ \Log::error("Okay, we're running the test macro now?");+ $logs = Actionlog::where(['item_id' => $item->id, 'item_type' => get_class($item)])->orderBy('id')->get();+ Assert::assertEquals(count($statuses), count($logs), "Wrong count of logs expected - expecting " . count($statuses) . ", got " . count($logs));+ $i = 0;+ foreach ($statuses as $status) {+ Assert::assertEquals($status, $logs[$i]->action_type, "Unexpected action type - " . $logs[$i]->action_type . " - expecting $status");+ $i++;+ }++ }}