Files
opensourcepos/tests/Models/GiftcardTest.php
T
objecttothis 29a9b1a7e7 Bugfix: Resolve Race Condition in Rewards and Gift Card Spending (#4640)
* Implement atomic updates for gift card and reward point decrements, enhance error handling for insufficient balances, and add regression tests for concurrency safety.

* Add translations for insufficient gift card balance and reward points error messages across all supported languages.

* Reorder `clear_suspended_sale_detail` call to ensure transactional consistency.

* Reorder `clear_all` call to align with success and error handling logic.

* Ensure soft-deleted gift cards are excluded in balance updates.

* Refactor change_quantity logic with atomic upserts, improve error handling for insufficient stock, and update related tests and constants.

* Added check for NEW_ENTRY

* Added unit tests to test changes.

* Fix class name casing in ItemQuantityTest for consistency.

* Fix Bulgarian translations for insufficient balance error messages in Sales module.

* Fix Greek translations for insufficient balance error messages in Sales module.

* Fix Armenian translations for insufficient balance error messages in Sales module.

* Fix Tamil translations for insufficient balance error messages in Sales module.

* Implement race condition testing for database methods with concurrent process support.

* Fix class name casing in ItemTest for consistency.

* Improve concurrent process handling in race condition tests; add readiness and synchronization barriers.

* Improve handling of process I/O streams and timeout management in race condition tests.

* Add test for decrementing gift card value when marked as deleted

* Add `finally` block to ensure proper cleanup in async database race condition tests

* Improve error handling and timeout management in async database race condition tests.

* Refactor test utilities to use shared `EmployeeFixtureTrait` and `ItemFixtureTrait`.

* Track process exit codes explicitly in race condition tests for improved error detection and debugging.

* Improve error handling in `ConcurrentDbRaceTrait` by adding exceptions for `mysqli_poll` and `mysqli_reap_async_query`.

Signed-off-by: objecttothis <17935339+objecttothis@users.noreply.github.com>

---------

Signed-off-by: objecttothis <17935339+objecttothis@users.noreply.github.com>
2026-08-20 02:24:23 +04:00

125 lines
3.9 KiB
PHP

<?php
namespace Tests\Models;
use App\Models\Giftcard;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use Config\Database;
use Tests\Support\ConcurrentDbRaceTrait;
/**
* Regression tests for GHSA-995p-52qw-5hh2: decrementGiftcardValue() must
* apply its balance check and its write in a single atomic UPDATE, so that
* two concurrent decrements against the same gift card can never both read
* the same stale balance and double-spend it.
*/
class GiftcardTest extends CIUnitTestCase
{
use DatabaseTestTrait;
use ConcurrentDbRaceTrait;
protected $migrate = true;
protected $migrateOnce = true;
protected $refresh = false;
protected $namespace = null;
public static function setUpBeforeClass(): void
{
$seeder = Database::seeder('tests');
$seeder->call('TestDatabaseBootstrapSeeder');
}
protected function setUp(): void
{
parent::setUp();
}
protected function createGiftcard(float $value): int
{
$giftcardNumber = random_int(1000000, 9999999);
\Config\Database::connect()->table('giftcards')->insert([
'giftcard_number' => $giftcardNumber,
'value' => $value,
'deleted' => 0,
'person_id' => null,
]);
return $giftcardNumber;
}
protected function getGiftcardValue(int $giftcardNumber): float
{
$row = \Config\Database::connect()->table('giftcards')
->where('giftcard_number', $giftcardNumber)
->get()
->getRow();
return (float) $row->value;
}
public function testDecrementGiftcardValueSucceedsWithSufficientBalance(): void
{
$giftcardNumber = $this->createGiftcard(100.00);
$giftcardModel = model(Giftcard::class);
$result = $giftcardModel->decrementGiftcardValue((string) $giftcardNumber, 60.00);
$this->assertTrue($result);
$this->assertEqualsWithDelta(40.00, $this->getGiftcardValue($giftcardNumber), 0.001);
}
public function testDecrementGiftcardValueFailsWithInsufficientBalance(): void
{
$giftcardNumber = $this->createGiftcard(50.00);
$giftcardModel = model(Giftcard::class);
$result = $giftcardModel->decrementGiftcardValue((string) $giftcardNumber, 60.00);
$this->assertFalse($result);
$this->assertEqualsWithDelta(50.00, $this->getGiftcardValue($giftcardNumber), 0.001);
}
public function testDecrementGiftcardValueExactBalanceSucceeds(): void
{
$giftcardNumber = $this->createGiftcard(60.00);
$giftcardModel = model(Giftcard::class);
$result = $giftcardModel->decrementGiftcardValue((string) $giftcardNumber, 60.00);
$this->assertTrue($result);
$this->assertEqualsWithDelta(0.00, $this->getGiftcardValue($giftcardNumber), 0.001);
}
public function testDecrementGiftcardValueFailsWhenDeleted(): void
{
$giftcardNumber = $this->createGiftcard(100.00);
\Config\Database::connect()->table('giftcards')
->where('giftcard_number', $giftcardNumber)
->update(['deleted' => 1]);
$giftcardModel = model(Giftcard::class);
$result = $giftcardModel->decrementGiftcardValue((string) $giftcardNumber, 60.00);
$this->assertFalse($result);
$this->assertEqualsWithDelta(100.00, $this->getGiftcardValue($giftcardNumber), 0.001);
}
public function testDecrementGiftcardValueConcurrentDecrementsOneWins(): void
{
$giftcardNumber = $this->createGiftcard(100.00);
[$result1, $result2] = $this->raceTwoProcesses(
'giftcard',
[$giftcardNumber, 60.00],
[$giftcardNumber, 60.00]
);
$this->assertSame(1, (int) $result1 + (int) $result2);
$this->assertEqualsWithDelta(40.00, $this->getGiftcardValue($giftcardNumber), 0.001);
}
}