From 0a474f48ad6843a62cd9b293687bc118abd674f6 Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Wed, 25 Jun 2025 19:38:19 +0100 Subject: [PATCH] WIP: Adding various tests of the contents of ActionLogs for lots of events --- tests/Feature/Assets/Api/StoreAssetTest.php | 10 +++++++ .../Checkins/Api/AccessoryCheckinTest.php | 4 +++ .../Feature/Checkins/Api/AssetCheckinTest.php | 6 +++++ .../Checkins/Api/ComponentCheckinTest.php | 5 ++++ .../Checkins/Api/LicenseCheckInTest.php | 3 +++ .../Feature/Checkins/Ui/AssetCheckinTest.php | 3 +++ .../Checkins/Ui/ComponentCheckinTest.php | 6 +++++ .../Checkouts/Api/AccessoryCheckoutTest.php | 8 ++++++ .../Checkouts/Api/AssetCheckoutTest.php | 4 +++ tests/Support/AssertsActionLogs.php | 26 +++++++++++++++++++ tests/Support/CustomTestMacros.php | 1 + 11 files changed, 76 insertions(+) create mode 100644 tests/Support/AssertsActionLogs.php diff --git a/tests/Feature/Assets/Api/StoreAssetTest.php b/tests/Feature/Assets/Api/StoreAssetTest.php index 1af3569f75..1ac234e027 100644 --- a/tests/Feature/Assets/Api/StoreAssetTest.php +++ b/tests/Feature/Assets/Api/StoreAssetTest.php @@ -13,10 +13,13 @@ 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() { $this->actingAsForApi(User::factory()->create()) @@ -82,6 +85,8 @@ class StoreAssetTest extends TestCase $this->assertTrue($asset->assetstatus->is($status)); $this->assertTrue($asset->supplier->is($supplier)); $this->assertEquals(10, $asset->warranty_months); + + $this->assertHasTheseActionLogs($asset, ['create', 'checkout']); } public function testSetsLastAuditDateToMidnightOfProvidedDate() @@ -180,6 +185,8 @@ class StoreAssetTest extends TestCase $asset = Asset::find($response->json()['payload']['id']); $this->assertEquals($user->id, $asset->assigned_to); $this->assertEquals('Asset created successfully. :)', $response->json('messages')); + + $this->assertHasTheseActionLogs($asset, ['create', 'checkout']); } @@ -571,6 +578,7 @@ class StoreAssetTest extends TestCase $this->assertTrue($asset->adminuser->is($user)); $this->assertTrue($asset->checkedOutToUser()); $this->assertTrue($asset->assignedTo->is($userAssigned)); + $this->assertHasTheseActionLogs($asset, ['create', 'checkout']); } public static function checkoutTargets() @@ -655,6 +663,7 @@ class StoreAssetTest extends TestCase $this->assertTrue($asset->adminuser->is($user)); $this->assertTrue($asset->checkedOutToLocation()); $this->assertTrue($asset->location->is($location)); + $this->assertHasTheseActionLogs($asset, ['create', 'checkout']); } public function testAnAssetCanBeCheckedOutToAssetOnStore() @@ -682,6 +691,7 @@ class StoreAssetTest extends TestCase $this->assertTrue($apiAsset->checkedOutToAsset()); // I think this makes sense, but open to a sanity check $this->assertTrue($asset->assignedAssets()->find($response['payload']['id'])->is($apiAsset)); + $this->assertHasTheseActionLogs($asset, ['create', 'checkout']); } /** diff --git a/tests/Feature/Checkins/Api/AccessoryCheckinTest.php b/tests/Feature/Checkins/Api/AccessoryCheckinTest.php index d5f45f22ae..fefca0efff 100644 --- a/tests/Feature/Checkins/Api/AccessoryCheckinTest.php +++ b/tests/Feature/Checkins/Api/AccessoryCheckinTest.php @@ -7,10 +7,12 @@ 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(); @@ -45,6 +47,7 @@ class AccessoryCheckinTest extends TestCase implements TestsFullMultipleCompanie $this->assertEquals(1, $accessoryForCompanyB->fresh()->checkouts->count(), 'Accessory should not be checked in'); $this->assertEquals(0, $anotherAccessoryForCompanyB->fresh()->checkouts->count(), 'Accessory should be checked in'); + $this->assertHasTheseActionLogs($anotherAccessoryForCompanyB, ['create', 'checkin from']); } public function testCanCheckinAccessory() @@ -60,6 +63,7 @@ class AccessoryCheckinTest extends TestCase implements TestsFullMultipleCompanie ->assertStatusMessageIs('success'); $this->assertEquals(0, $accessory->fresh()->checkouts->count(), 'Accessory should be checked in'); + $this->assertHasTheseActionLogs($accessory, ['create', 'checkout', 'checkin from']); // fixme? } public function testCheckinIsLogged() diff --git a/tests/Feature/Checkins/Api/AssetCheckinTest.php b/tests/Feature/Checkins/Api/AssetCheckinTest.php index 7ce3d05619..a6b342b773 100644 --- a/tests/Feature/Checkins/Api/AssetCheckinTest.php +++ b/tests/Feature/Checkins/Api/AssetCheckinTest.php @@ -11,10 +11,12 @@ 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()) @@ -69,6 +71,8 @@ class AssetCheckinTest extends TestCase $this->assertEquals('Changed Name', $asset->name); $this->assertEquals($status->id, $asset->status_id); $this->assertTrue($asset->location()->is($location)); + $this->assertHasTheseActionLogs($asset, [/*'create', 'checkout', */ 'checkin from']); //FIXME? + Event::assertDispatched(function (CheckoutableCheckedIn $event) use ($currentTimestamp) { // this could be better mocked but is ok for now. @@ -88,6 +92,7 @@ class AssetCheckinTest extends TestCase ->postJson(route('api.asset.checkin', $asset->id)); $this->assertTrue($asset->refresh()->location()->is($rtdLocation)); + $this->assertHasTheseActionLogs($asset, ['create', /*'checkout',*/ 'checkin from']); //FIXME? } public function testDefaultLocationCanBeUpdatedUponCheckin() @@ -102,6 +107,7 @@ class AssetCheckinTest extends TestCase ]); $this->assertTrue($asset->refresh()->defaultLoc()->is($location)); + $this->assertHasTheseActionLogs($asset, ['create', /*'checkout',*/ 'checkin from']); //FIXME? } public function testAssetsLicenseSeatsAreClearedUponCheckin() diff --git a/tests/Feature/Checkins/Api/ComponentCheckinTest.php b/tests/Feature/Checkins/Api/ComponentCheckinTest.php index 0497a8135c..51ad4a0910 100644 --- a/tests/Feature/Checkins/Api/ComponentCheckinTest.php +++ b/tests/Feature/Checkins/Api/ComponentCheckinTest.php @@ -10,10 +10,12 @@ 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(); @@ -82,6 +84,8 @@ class ComponentCheckinTest extends TestCase implements TestsFullMultipleCompanie ->assertStatusMessageIs('success'); $this->assertEquals(1, $component->fresh()->assets->first()->pivot->assigned_qty); + $this->assertHasTheseActionLogs($component, ['create']); //FIXME? + Event::assertDispatched(function (CheckoutableCheckedIn $event) use ($user, $component) { return $event->checkoutable->is($component) @@ -160,5 +164,6 @@ class ComponentCheckinTest extends TestCase implements TestsFullMultipleCompanie 'item_id' => $component->id, 'item_type' => Component::class, ]); + $this->assertHasTheseActionLogs($component, ['create', /*'checkout',*/ 'checkin from']); //FIXME? } } diff --git a/tests/Feature/Checkins/Api/LicenseCheckInTest.php b/tests/Feature/Checkins/Api/LicenseCheckInTest.php index 385933655c..abe03e4061 100644 --- a/tests/Feature/Checkins/Api/LicenseCheckInTest.php +++ b/tests/Feature/Checkins/Api/LicenseCheckInTest.php @@ -4,9 +4,11 @@ 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(); @@ -41,5 +43,6 @@ class LicenseCheckInTest extends TestCase { $this->assertNull($licenseSeat->asset_id); $this->assertEquals('Checking in the seat', $licenseSeat->notes); + $this->assertHasTheseActionLogs($license, ['add seats', 'create', 'checkin from']); //FIXME - bad order! } } \ No newline at end of file diff --git a/tests/Feature/Checkins/Ui/AssetCheckinTest.php b/tests/Feature/Checkins/Ui/AssetCheckinTest.php index 4f1c62cf2c..98ac9999a5 100644 --- a/tests/Feature/Checkins/Ui/AssetCheckinTest.php +++ b/tests/Feature/Checkins/Ui/AssetCheckinTest.php @@ -11,10 +11,12 @@ 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()) @@ -102,6 +104,7 @@ class AssetCheckinTest extends TestCase ->post(route('hardware.checkin.store', [$asset])); $this->assertTrue($asset->refresh()->location()->is($rtdLocation)); + $this->assertHasTheseActionLogs($asset, ['create', 'checkin from']); } public function testDefaultLocationCanBeUpdatedUponCheckin() diff --git a/tests/Feature/Checkins/Ui/ComponentCheckinTest.php b/tests/Feature/Checkins/Ui/ComponentCheckinTest.php index 7da3879ea7..99a8b3eb4b 100644 --- a/tests/Feature/Checkins/Ui/ComponentCheckinTest.php +++ b/tests/Feature/Checkins/Ui/ComponentCheckinTest.php @@ -5,10 +5,13 @@ 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() { $component = Component::factory()->checkedOutToAsset()->create(); @@ -47,6 +50,7 @@ class ComponentCheckinTest extends TestCase ]) ->assertStatus(302) ->assertRedirect(route('components.index')); + $this->assertHasTheseActionLogs($component, ['create', 'checkin from']); } public function testComponentCheckinPagePostIsRedirectedIfRedirectSelectionIsItem() @@ -66,5 +70,7 @@ class ComponentCheckinTest extends TestCase ->assertStatus(302) ->assertSessionHasNoErrors() ->assertRedirect(route('components.show', $component)); + $this->assertHasTheseActionLogs($component, ['create', 'checkin from']); + } } diff --git a/tests/Feature/Checkouts/Api/AccessoryCheckoutTest.php b/tests/Feature/Checkouts/Api/AccessoryCheckoutTest.php index 7adab09c03..a974f455ab 100644 --- a/tests/Feature/Checkouts/Api/AccessoryCheckoutTest.php +++ b/tests/Feature/Checkouts/Api/AccessoryCheckoutTest.php @@ -10,10 +10,13 @@ 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() { $this->actingAsForApi(User::factory()->create()) @@ -91,6 +94,7 @@ class AccessoryCheckoutTest extends TestCase implements TestsPermissionsRequirem 'created_by' => $admin->id, ])->count(),'Log entry either does not exist or there are more than expected' ); + $this->assertHasTheseActionLogs($accessory, ['create', 'checkout']); } public function testAccessoryCanBeCheckedOutWithQty() @@ -125,6 +129,8 @@ class AccessoryCheckoutTest extends TestCase implements TestsPermissionsRequirem ])->count(), 'Log entry either does not exist or there are more than expected' ); + $this->assertHasTheseActionLogs($accessory, ['create', 'checkout']); + } public function testAccessoryCannotBeCheckedOutToInvalidUser() @@ -190,5 +196,7 @@ class AccessoryCheckoutTest extends TestCase implements TestsPermissionsRequirem ])->count(), 'Log entry either does not exist or there are more than expected' ); + $this->assertHasTheseActionLogs($accessory, ['create', 'checkout']); + } } diff --git a/tests/Feature/Checkouts/Api/AssetCheckoutTest.php b/tests/Feature/Checkouts/Api/AssetCheckoutTest.php index e83ad7d30d..359572ef4a 100644 --- a/tests/Feature/Checkouts/Api/AssetCheckoutTest.php +++ b/tests/Feature/Checkouts/Api/AssetCheckoutTest.php @@ -12,10 +12,12 @@ 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(); @@ -37,6 +39,8 @@ class AssetCheckoutTest extends TestCase ->post(route('api.assets.requests.store', $nonRequestable->id)) ->assertStatusMessageIs('error'); + $this->assertHasTheseActionLogs($requestable, ['create', 'requested', 'update']); //FIXME - is this right?! + } public function testCheckingOutAssetRequiresCorrectPermission() diff --git a/tests/Support/AssertsActionLogs.php b/tests/Support/AssertsActionLogs.php new file mode 100644 index 0000000000..1a7868ab37 --- /dev/null +++ b/tests/Support/AssertsActionLogs.php @@ -0,0 +1,26 @@ + $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/Support/CustomTestMacros.php b/tests/Support/CustomTestMacros.php index 956235f461..edfa7c5b6a 100644 --- a/tests/Support/CustomTestMacros.php +++ b/tests/Support/CustomTestMacros.php @@ -2,6 +2,7 @@ namespace Tests\Support; +use App\Models\Actionlog; use Illuminate\Database\Eloquent\Model; use Illuminate\Testing\TestResponse; use PHPUnit\Framework\Assert;