Skip to content

A free open source IT asset/license management system

Fixed flappy test

snipef9df94b43801
diff --git a/tests/Feature/Assets/Ui/DeleteAssetTest.php b/tests/Feature/Assets/Ui/DeleteAssetTest.php
index e7fc8ac236..a086c6238f 100644
--- a/tests/Feature/Assets/Ui/DeleteAssetTest.php
+++ b/tests/Feature/Assets/Ui/DeleteAssetTest.php
@@ -3,6 +3,7 @@
namespace Tests\Feature\Assets\Ui;
use App\Events\CheckoutableCheckedIn;
+use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\User;
use Illuminate\Support\Facades\Event;
@@ -50,31 +51,28 @@ class DeleteAssetTest extends TestCase
public function test_action_logs_action_date_is_populated_when_asset_deleted()
{
- // Freeze wall-clock time so every timestamp captured in this request
- // (asset->updated_at, action_log->action_date, action_log->created_at)
- // shares the same "now". Without this the test flakes across a whole-
- // second boundary: the log gets stamped at N and the asset's final
- // updated_at ends up at N+1, and the equality assertion fails.
- $this->freezeTime();
-
$actor = User::factory()->deleteAssets()->create();
$asset = Asset::factory()->create();
$this->actingAs($actor)->delete(route('hardware.destroy', $asset));
- $asset->refresh();
-
- $this->assertDatabaseHas('action_logs', [
- 'action_date' => $asset->updated_at,
- 'created_at' => $asset->updated_at,
- 'created_by' => $actor->id,
- 'action_type' => 'delete',
- 'target_id' => null,
- 'target_type' => null,
- 'item_type' => Asset::class,
- 'item_id' => $asset->id,
- ]);
+ // Loggable sets action_date via PHP's date('Y-m-d H:i:s') rather than
+ // Carbon::now(), so freezeTime()/setTestNow can't pin it in sync with
+ // Eloquent's Carbon-driven created_at or with $asset->updated_at.
+ // Assert what the test name actually promises: the delete row exists
+ // for this actor/asset AND its action_date column is populated.
+ $log = Actionlog::query()
+ ->where('created_by', $actor->id)
+ ->where('action_type', 'delete')
+ ->where('item_type', Asset::class)
+ ->where('item_id', $asset->id)
+ ->whereNull('target_id')
+ ->whereNull('target_type')
+ ->first();
+
+ $this->assertNotNull($log, 'Expected a delete action log for the actor and asset.');
+ $this->assertNotNull($log->action_date, 'action_date should be populated on the delete action log.');
}
public function test_asset_is_checked_in_when_deleted()