Skip to content

A free open source IT asset/license management system

Fixed flaky test

snipee346a4490c2f
diff --git a/database/factories/MaintenanceFactory.php b/database/factories/MaintenanceFactory.php
index c0129c780c..bee951c903 100644
--- a/database/factories/MaintenanceFactory.php
+++ b/database/factories/MaintenanceFactory.php
@@ -72,16 +72,23 @@ class MaintenanceFactory extends Factory
// Use item_id + item_type (the polymorphic FK) rather than the
// legacy asset_id so callers can override the target with their
// own asset instance and skip the default factory-created one.
- // The legacy asset_id path still routes through the model's
- // setAssetIdAttribute mutator, but the default path here no
- // longer spawns an asset when a caller passes item_id, which
- // was leaking spurious asset rows into cross-cutting counts
- // (assetsPastEol on the Needs Attention widget hit this).
- 'item_id' => Asset::factory()->laptopZenbook()->afterMaking(function (Asset $asset) {
- if ($asset->location_id === null) {
- $asset->location_id = $asset->rtd_location_id;
+ // A supplied asset_id routes through the model's
+ // setAssetIdAttribute mutator at save time. The closure below
+ // detects that up front and skips the default asset spawn so
+ // we don't leak a spurious asset row for cross-cutting counts
+ // to trip over (assetsPastEol on the Needs Attention widget,
+ // source_id collisions in the calendar events API test, etc.).
+ 'item_id' => function (array $attributes) {
+ if (array_key_exists('asset_id', $attributes)) {
+ return null;
}
- }),
+
+ return Asset::factory()->laptopZenbook()->afterMaking(function (Asset $asset) {
+ if ($asset->location_id === null) {
+ $asset->location_id = $asset->rtd_location_id;
+ }
+ });
+ },
'item_type' => Asset::class,
'supplier_id' => Supplier::factory(),
'maintenance_type_id' => $maintenanceType->id,
diff --git a/tests/Feature/CalendarEvents/CalendarEventsApiTest.php b/tests/Feature/CalendarEvents/CalendarEventsApiTest.php
index 014b44bfa6..2b71041295 100644
--- a/tests/Feature/CalendarEvents/CalendarEventsApiTest.php
+++ b/tests/Feature/CalendarEvents/CalendarEventsApiTest.php
@@ -79,6 +79,7 @@ class CalendarEventsApiTest extends TestCase implements TestsFullMultipleCompani
]))
->assertOk()
->json('events'))
+ ->filter(fn ($row) => $row['extendedProps']['source_type'] === Maintenance::class)
->pluck('extendedProps.source_id')
->all();
@@ -102,6 +103,7 @@ class CalendarEventsApiTest extends TestCase implements TestsFullMultipleCompani
->getJson(route('api.calendar.events', ['event_type' => ['maintenance.start']]))
->assertOk()
->json('events'))
+ ->filter(fn ($row) => $row['extendedProps']['source_type'] === Maintenance::class)
->pluck('extendedProps.source_id')
->all();
$this->assertContains($maintenance->id, $ids);
@@ -111,6 +113,7 @@ class CalendarEventsApiTest extends TestCase implements TestsFullMultipleCompani
->getJson(route('api.calendar.events', ['event_type' => ['some.other.type']]))
->assertOk()
->json('events'))
+ ->filter(fn ($row) => $row['extendedProps']['source_type'] === Maintenance::class)
->pluck('extendedProps.source_id')
->all();
$this->assertNotContains($maintenance->id, $ids);
@@ -150,6 +153,7 @@ class CalendarEventsApiTest extends TestCase implements TestsFullMultipleCompani
->getJson(route('api.calendar.events'))
->assertOk()
->json('events'))
+ ->filter(fn ($row) => $row['extendedProps']['source_type'] === Maintenance::class)
->pluck('extendedProps.source_id')
->all();