From 90818bb1479aeb2bc7a7ff2409707f9ba41422a1 Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Mon, 3 Jun 2024 15:58:57 +0100 Subject: [PATCH 1/2] Don't save next autoincrement base if it's going to fail, next --- app/Observers/AssetObserver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Observers/AssetObserver.php b/app/Observers/AssetObserver.php index 8fcf2485a3..704eb52e42 100644 --- a/app/Observers/AssetObserver.php +++ b/app/Observers/AssetObserver.php @@ -92,7 +92,7 @@ class AssetObserver // only modify the 'next' one if it's *bigger* than the stored base // - if($next_asset_tag > $settings->next_auto_tag_base) { + if ($next_asset_tag > $settings->next_auto_tag_base && $next_asset_tag < PHP_INT_MAX) { $settings->next_auto_tag_base = $next_asset_tag; $settings->save(); } From e827bc9a07e882c643746c7abbdaf5dcf1b277a6 Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Mon, 3 Jun 2024 17:31:56 +0100 Subject: [PATCH 2/2] Tests on asset tags that are maximum integers and *almost* maximum... --- tests/Unit/AssetTest.php | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/Unit/AssetTest.php b/tests/Unit/AssetTest.php index 9c3a76af69..ef0da1a1b8 100644 --- a/tests/Unit/AssetTest.php +++ b/tests/Unit/AssetTest.php @@ -6,6 +6,7 @@ use App\Models\AssetModel; use App\Models\Category; use Carbon\Carbon; use Tests\TestCase; +use App\Models\Setting; class AssetTest extends TestCase { @@ -135,6 +136,40 @@ class AssetTest extends TestCase $this->assertEquals($final->asset_tag, $final_result); } + public function testAutoIncrementBIG() + { + $this->settings->enableAutoIncrement(); + + // we have to do this by hand to 'simulate' two web pages being open at the same time + $a = Asset::factory()->make(['asset_tag' => Asset::autoincrement_asset()]); + $b = Asset::factory()->make(['asset_tag' => 'ABCD' . (PHP_INT_MAX - 1)]); + + $this->assertTrue($a->save()); + $this->assertTrue($b->save()); + \Log::error("A asset tag is: " . $a->asset_tag); + $matches = []; + preg_match('/\d+/', $a->asset_tag, $matches); + \Log::error("digits are: " . $matches[0]); + $this->assertEquals(Setting::getSettings()->next_auto_tag_base, $matches[0] + 1, "Next auto increment number should be the last normally-saved one plus one, but isn't"); + } + + public function testAutoIncrementAlmostBIG() + { + // TODO: this looks pretty close to the one above, could we maybe squish them together? + $this->settings->enableAutoIncrement(); + + // we have to do this by hand to 'simulate' two web pages being open at the same time + $a = Asset::factory()->make(['asset_tag' => Asset::autoincrement_asset()]); + $b = Asset::factory()->make(['asset_tag' => 'ABCD' . (PHP_INT_MAX - 2)]); + + $this->assertTrue($a->save()); + $this->assertTrue($b->save()); + $matches = []; + preg_match('/\d+/', $b->asset_tag, $matches); //this is *b*, not *a* - slight difference from above test + $this->assertEquals(Setting::getSettings()->next_auto_tag_base, $matches[0] + 1, "Next auto increment number should be the last normally-saved one plus one, but isn't"); + } + + public function testWarrantyExpiresAttribute() {