Add more action logs tests everywhere I can think of it.

This commit is contained in:
Brady Wetherington
2025-06-30 12:04:20 +01:00
parent 0a474f48ad
commit 7c5110ed5d
24 changed files with 47 additions and 48 deletions

View File

@@ -71,7 +71,7 @@ class AuditAssetTest extends TestCase
],
])
->assertStatus(200);
$this->assertHasTheseActionLogs($asset, ['create', 'audit']);
}

View File

@@ -67,5 +67,6 @@ class DeleteAssetsTest extends TestCase implements TestsFullMultipleCompaniesSup
->assertStatusMessageIs('success');
$this->assertSoftDeleted($asset);
$this->assertHasTheseActionLogs($asset, ['create', 'delete']);
}
}

View File

@@ -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()
{

View File

@@ -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']);
}
}

View File

@@ -84,6 +84,7 @@ class BulkDeleteAssetsTest extends TestCase
Asset::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 TestCase
Asset::findMany($id_array)->each(function (Asset $asset) {
$this->assertNull($asset->deleted_at);
$this->assertHasTheseActionLogs($asset, ['create', 'delete', 'restore', 'fart']); //SHIT
});
$this->assertTrue(false);
}

View File

@@ -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();

View File

@@ -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())

View File

@@ -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();

View File

@@ -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();

View File

@@ -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())

View File

@@ -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()
{

View File

@@ -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()
{

View File

@@ -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();

View File

@@ -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()

View File

@@ -49,6 +49,7 @@ class ConsumableCheckoutTest extends TestCase
]);
$this->assertTrue($user->consumables->contains($consumable));
$this->assertHasTheseActionLogs($consumable, ['create', 'checkout']);
}
public function testUserSentNotificationUponCheckout()

View File

@@ -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
}
}

View File

@@ -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()

View File

@@ -218,6 +218,7 @@ class AssetCheckoutTest extends TestCase
return true;
});
$this->assertHasTheseActionLogs($asset, ['create', 'checkout']); //FIXME - only getting one?
}
public function testLicenseSeatsAreAssignedToUserUponCheckout()

View File

@@ -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);

View File

@@ -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']);
}
}

View File

@@ -57,6 +57,7 @@ class ConsumableCheckoutTest extends TestCase
]);
$this->assertTrue($user->consumables->contains($consumable));
$this->assertHasTheseActionLogs($consumable, ['create', 'checkout']);
}
public function testUserSentNotificationUponCheckout()

View File

@@ -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()

View File

@@ -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++;
}
}
}

View File

@@ -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++;
}
}
}