Files
opensourcepos/tests/Models/ReceivingTest.php
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

109 lines
3.2 KiB
PHP

<?php
namespace Tests\Models;
use App\Models\Receiving;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use Tests\Support\EmployeeFixtureTrait;
use Tests\Support\ItemFixtureTrait;
/**
* Regression tests for GHSA-995p-52qw-5hh2: Receiving::delete_value() must
* correctly reverse the stock quantity change it applied via
* Item_quantity::changeQuantity(), using the same atomic upsert as the
* sale-checkout and sale-cancel paths.
*/
class ReceivingTest extends CIUnitTestCase
{
use DatabaseTestTrait;
use EmployeeFixtureTrait;
use ItemFixtureTrait;
protected $migrate = true;
protected $migrateOnce = true;
protected $refresh = true;
protected $namespace = null;
private const LOCATION_ID = 1;
protected function setUp(): void
{
parent::setUp();
}
protected function createSupplier(): int
{
$db = \Config\Database::connect();
$db->table('people')->insert([
'first_name' => 'Test',
'last_name' => 'Supplier',
'phone_number' => '555-0400',
'email' => 'supplier-' . uniqid() . '@test.com',
'address_1' => '',
'address_2' => '',
'city' => '',
'state' => '',
'zip' => '',
'country' => '',
'comments' => '',
]);
$personId = (int) $db->insertID();
$db->table('suppliers')->insert([
'person_id' => $personId,
'deleted' => 0,
]);
return $personId;
}
protected function buildReceivingLine(int $itemId, float $quantity, float $costPrice): array
{
return [
0 => [
'item_id' => $itemId,
'line' => 0,
'description' => 'Test Item',
'serialnumber' => '',
'quantity' => $quantity,
'receiving_quantity' => 1,
'discount' => 0,
'discount_type' => 0,
'price' => $costPrice,
'item_location' => self::LOCATION_ID,
],
];
}
public function testDeleteValueRestoresStockAfterReceiving(): void
{
$employeeId = $this->createEmployee();
$supplierId = $this->createSupplier();
$itemId = $this->createTestItem(HAS_STOCK);
// Deliberately no pre-existing item_quantities row for this item/location.
$receivingModel = model(Receiving::class);
$items = $this->buildReceivingLine($itemId, 5, 1.00);
$receivingId = $receivingModel->save_value(
$items,
$supplierId,
$employeeId,
'test receiving',
'REF-' . uniqid(),
'Cash'
);
$this->assertGreaterThan(0, $receivingId);
$this->assertEqualsWithDelta(5.0, $this->getItemQuantity($itemId, self::LOCATION_ID), 0.001);
$deleteResult = $receivingModel->delete_value($receivingId, $employeeId, true);
$this->assertTrue($deleteResult);
$this->assertEqualsWithDelta(0.0, $this->getItemQuantity($itemId, self::LOCATION_ID), 0.001);
}
}