Merge pull request #14816 from uberbrady/hotfix_autoincrement_workaround

Don't save next autoincrement base if it's going to fail, next
This commit is contained in:
snipe 2024-06-05 09:47:40 +01:00 committed by GitHub
commit 9fccafa3ac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 1 deletions

View file

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

View file

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