Skip to content

A free open source IT asset/license management system

Update tests

Marcus Moore1951d3a83dd2
diff --git a/tests/Feature/Checkins/Ui/AccessoryCheckinTest.php b/tests/Feature/Checkins/Ui/AccessoryCheckinTest.php
index 812445b462..9d453c4f3c 100644
--- a/tests/Feature/Checkins/Ui/AccessoryCheckinTest.php
+++ b/tests/Feature/Checkins/Ui/AccessoryCheckinTest.php
@@ -7,9 +7,9 @@ use App\Mail\CheckinAccessoryMail;
use App\Models\Accessory;
use App\Models\Asset;
use App\Models\CheckoutAcceptance;
-use App\Models\Setting;
use App\Models\User;
use App\Notifications\CheckinAccessoryNotification;
+use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;
@@ -123,7 +123,6 @@ class AccessoryCheckinTest extends TestCase
]);
$this->assertTrue($acceptance->isPending());
- $this->assertNoNotificationChannelIsConfigured($accessory);
$this->checkInAccessoryFrom($accessory, $user);
@@ -182,13 +181,18 @@ class AccessoryCheckinTest extends TestCase
$accessory->category->update(['checkin_email' => true]);
$this->assertTrue((bool) $accessory->fresh()->checkin_email());
- $accessoryAcceptance = CheckoutAcceptance::factory()->forAccessory()->pending()->create([
- 'checkoutable_id' => $accessory->id,
+ // The asset's acceptance is created FIRST so it is the older row.
+ // Accessory checkins retire only the oldest matching pending row, so
+ // with the accessory's own row older this test would pass whether or
+ // not the query filters on checkoutable_type — the asset's row would
+ // never be reached either way.
+ $assetAcceptance = CheckoutAcceptance::factory()->pending()->create([
+ 'checkoutable_id' => $asset->id,
'assigned_to_id' => $user->id,
]);
- $assetAcceptance = CheckoutAcceptance::factory()->pending()->create([
- 'checkoutable_id' => $asset->id,
+ $accessoryAcceptance = CheckoutAcceptance::factory()->forAccessory()->pending()->create([
+ 'checkoutable_id' => $accessory->id,
'assigned_to_id' => $user->id,
]);
@@ -221,40 +225,29 @@ class AccessoryCheckinTest extends TestCase
$this->assertDatabaseMissing('accessories_checkout', ['id' => $checkout->id]);
}
- /**
- * The missing morph filter only shows up when an accessory and an asset
- * share a primary key, and factories won't hand out a matching pair by
- * accident. Nudge whichever sequence is behind until the ids line up.
- *
- * @return array{0: Accessory, 1: Asset}
- */
private function createAccessoryAndAssetSharingAnId(User $user): array
{
- $accessory = Accessory::factory()->checkedOutToUser($user)->create();
- $asset = Asset::factory()->create();
-
- for ($attempt = 0; $accessory->id !== $asset->id && $attempt < 25; $attempt++) {
- if ($accessory->id < $asset->id) {
- $accessory = Accessory::factory()->checkedOutToUser($user)->create();
- } else {
- $asset = Asset::factory()->create();
- }
- }
-
- $this->assertSame(
- $accessory->id,
- $asset->id,
- 'Fixture setup failed: could not get an accessory and an asset onto the same id.'
- );
+ $sharedId = $this->firstIdFreeInBoth('accessories', 'assets');
+
+ $accessory = Accessory::factory()->checkedOutToUser($user)->create(['id' => $sharedId]);
+ $asset = Asset::factory()->create(['id' => $sharedId]);
+
+ // Without the collision there is no morph bug to trigger and the test
+ // passes for the wrong reason, so prove the ids really do line up.
+ $this->assertSame($accessory->id, $asset->id);
return [$accessory, $asset];
}
- private function assertNoNotificationChannelIsConfigured(Accessory $accessory): void
+ /**
+ * An id no row in either table holds, so both can be created with it.
+ */
+ private function firstIdFreeInBoth(string $firstTable, string $secondtable): int
{
- $this->assertFalse((bool) $accessory->fresh()->checkin_email());
- $this->assertEmpty(Setting::getSettings()->admin_cc_email);
- $this->assertEmpty(Setting::getSettings()->webhook_endpoint);
+ return max(
+ (int) DB::table($firstTable)->max('id'),
+ (int) DB::table($secondtable)->max('id'),
+ ) + 1;
}
private function assertAcceptanceWasSoftDeleted(CheckoutAcceptance $acceptance, string $message): void
diff --git a/tests/Feature/Checkins/Ui/LicenseCheckinTest.php b/tests/Feature/Checkins/Ui/LicenseCheckinTest.php
index ed27e15684..55f3597ee8 100644
--- a/tests/Feature/Checkins/Ui/LicenseCheckinTest.php
+++ b/tests/Feature/Checkins/Ui/LicenseCheckinTest.php
@@ -12,6 +12,7 @@ use App\Models\LicenseSeat;
use App\Models\Setting;
use App\Models\User;
use App\Notifications\CheckinLicenseSeatNotification;
+use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;
@@ -228,9 +229,9 @@ class LicenseCheckinTest extends TestCase
* Acceptance required, checkin email off — the configuration where a stale
* pending acceptance can outlive the assignment it belongs to.
*/
- private function seatAssignedTo(User $user): LicenseSeat
+ private function licenseRequiringAcceptanceWithoutCheckinEmail(): License
{
- $license = License::factory()->create([
+ return License::factory()->create([
'reassignable' => 1,
'category_id' => Category::factory()->create([
'category_type' => 'license',
@@ -238,10 +239,13 @@ class LicenseCheckinTest extends TestCase
'checkin_email' => 0,
])->id,
]);
+ }
+ private function seatAssignedTo(User $user): LicenseSeat
+ {
return LicenseSeat::factory()
->assignedToUser($user)
- ->create(['license_id' => $license->id]);
+ ->create(['license_id' => $this->licenseRequiringAcceptanceWithoutCheckinEmail()->id]);
}
/**
@@ -276,35 +280,40 @@ class LicenseCheckinTest extends TestCase
$this->assertNull($seat->refresh()->assigned_to, 'The seat was not checked in, so nothing downstream ran.');
}
- /**
- * The missing morph filter only shows up when a seat and an asset share a
- * primary key, and factories won't hand out a matching pair by accident.
- * Nudge whichever sequence is behind until the ids line up.
- *
- * @return array{0: LicenseSeat, 1: Asset}
- */
private function createSeatAndAssetSharingAnId(User $user): array
{
- $seat = $this->seatAssignedTo($user);
- $asset = Asset::factory()->create();
+ // A license spawns its own seats when it saves, so the id is only free
+ // to claim once those exist.
+ $license = $this->licenseRequiringAcceptanceWithoutCheckinEmail();
- for ($attempt = 0; $seat->id !== $asset->id && $attempt < 25; $attempt++) {
- if ($seat->id < $asset->id) {
- $seat = $this->seatAssignedTo($user);
- } else {
- $asset = Asset::factory()->create();
- }
- }
-
- $this->assertSame(
- $seat->id,
- $asset->id,
- 'Fixture setup failed: could not get a license seat and an asset onto the same id.'
- );
+ $sharedId = $this->firstIdFreeInBoth('license_seats', 'assets');
+
+ $seat = LicenseSeat::factory()
+ ->assignedToUser($user)
+ ->create(['license_id' => $license->id, 'id' => $sharedId]);
+
+ $asset = Asset::factory()->create(['id' => $sharedId]);
+
+ // Without the collision there is no morph bug to trigger and the test
+ // passes for the wrong reason, so prove the ids really do line up.
+ $this->assertSame($seat->id, $asset->id);
return [$seat, $asset];
}
+ /**
+ * An id no row in either table holds, so both can be created with it.
+ * Taken above both maxima rather than read from the auto-increment
+ * counters, which are engine-specific to query.
+ */
+ private function firstIdFreeInBoth(string $table, string $otherTable): int
+ {
+ return max(
+ (int) DB::table($table)->max('id'),
+ (int) DB::table($otherTable)->max('id'),
+ ) + 1;
+ }
+
private function assertNoNotificationChannelIsConfigured(LicenseSeat $seat): void
{
$this->assertFalse((bool) $seat->license->checkin_email());