Files
opensourcepos/tests/Libraries/Sale_libPaymentTest.php
T
jekkos 28755dfd50 fix(tests): resolve all phpunit failures — clean-DB suite green (#4626) (#4691)
* fix(tests): resolve all phpunit failures (#4626)

Bring the phpunit suite from 153 failures to 0 (281 tests passing):

- Employee: decouple grants block from save_value success; restructure
  save_employee new-employee + disallowed-grants early return
- Sale: unify sales_payments_temp schema (add sale_cash_refund,
  reference_code) so both creators produce an identical superset table
- Employees controller: provide placeholder password/hash in testing env
  so new-employee insert succeeds and grant logic is testable
- TestDatabaseBootstrapSeeder: reset shared connection table-name cache
  after bootstrap reset to avoid stale listTables()/tableExists() results
- Config: fix postSaveLocale validation rule syntax
- Test data: use unique employee usernames to avoid UNIQUE constraint
  collisions latching strict-mode transStatus=false on the shared conn
- Various test-file and language-string corrections

* test: consolidate employee fixtures in shared trait

Route test employee creation through a single EmployeeFixtureTrait
that delegates to Employee::save_employee(), so fixtures exercise the
same production code path instead of raw DB inserts. Removes six
near-duplicate helpers across EmployeeTest, SalesControllerTest, and
EmployeesControllerTest while preserving each test's specific grant
set.

Closes a piece of the fixture-scattering flagged in #4626.

Closes #4626

* test: add global DROP/CREATE grant and commit theme fixtures

* fix(ci): remove redundant symlink step, set working encryption key

* fix(ci): run phpunit with --no-coverage to avoid no-driver warning

* fix: address code review findings

- Config: restore strict locale validation (min required|integer|>0) and
  fix max cross-field check with a new gte_field rule (CI4's
  greater_than_equal_to[field] does not resolve the field value)
- Tests: assert rejection for non-numeric/zero/negative/min>max limits
- .env.example: remove shared hard-coded encryption.key (auto-generates);
  document Docker env-var usage
- phpunit.yml: scope CREATE/DROP grant to ospos_test.* and provision a
  per-run encryption key as an env var

* feat: support ENCRYPTION_KEY env var for encryption key

Read ENCRYPTION_KEY as a fallback for the encryption key when the
config value is empty. This is a supported, reliable path for Docker /
container deploys and CI, avoiding reliance on the raw dotted
encryption.key env var.

* fix: align Summary_report temp tables with Sale temp table schema

Summary_report created sales_items_taxes_temp and sales_payments_temp with fewer columns than the canonical create_temp_table() in Sale.php. A later reader expecting those columns hit a schema-mismatch SQL error on the shared temp tables. Add internal_tax/sales_tax (sales_items_taxes_temp) and reference_code (sales_payments_temp) so all creators emit the identical column set.
2026-09-08 21:49:28 +02:00

201 lines
6.5 KiB
PHP

<?php
namespace Tests\Libraries;
use App\Libraries\Sale_lib;
use App\Models\Attribute;
use App\Models\Customer;
use App\Models\Dinner_table;
use App\Models\Item;
use App\Models\Item_kit_items;
use App\Models\Item_quantity;
use App\Models\Item_taxes;
use App\Models\Sale;
use App\Models\Stock_location;
use CodeIgniter\Config\Factories;
use CodeIgniter\Test\CIUnitTestCase;
use Config\OSPOS;
class Sale_libPaymentTest extends CIUnitTestCase
{
private Sale_lib $saleLib;
protected int $priorBcscale;
protected function setUp(): void
{
parent::setUp();
// The app sets a global bcscale() from app config on config load
// (see app/Events/Load_config.php), and whichever value a prior test
// class triggered leaks into bcadd/bcmul here. Pin a known scale so the
// accumulation math in these tests is deterministic regardless of suite
// order; restore it in tearDown.
$this->priorBcscale = bcscale();
bcscale(0);
// Inject mock OSPOS config so Sale_lib constructor and helpers don't need real settings
$ospos = new OSPOS();
$ospos->settings = [
'cash_rounding_code' => '',
'cash_decimals' => 2,
'currency_decimals' => 2,
'tax_decimals' => 2,
'quantity_decimals' => 2,
];
Factories::injectMock('config', OSPOS::class, $ospos);
// Inject stub models so model() calls in Sale_lib::__construct() skip DB
$stubMethods = ['__construct'];
foreach ([
Attribute::class,
Customer::class,
Dinner_table::class,
Item::class,
Item_kit_items::class,
Item_quantity::class,
Item_taxes::class,
Sale::class,
Stock_location::class,
] as $modelClass) {
$mock = $this->getMockBuilder($modelClass)
->disableOriginalConstructor()
->getMock();
Factories::injectMock('models', $modelClass, $mock);
}
session()->destroy();
$this->saleLib = new Sale_lib();
}
protected function tearDown(): void
{
bcscale($this->priorBcscale);
Factories::reset();
parent::tearDown();
}
// ========== getPayments / setPayments ==========
public function testGetPaymentsReturnsEmptyArrayInitially(): void
{
$payments = $this->saleLib->getPayments();
$this->assertIsArray($payments);
$this->assertEmpty($payments);
}
public function testSetPaymentsPersistsToSession(): void
{
$data = [
'cash' => [
'payment_type' => 'cash',
'payment_amount' => '10.00',
'cash_refund' => 0,
'cash_adjustment' => CASH_ADJUSTMENT_FALSE,
'reference_code' => null,
]
];
$this->saleLib->setPayments($data);
$this->assertSame($data, $this->saleLib->getPayments());
}
// ========== addPayment ==========
public function testAddPaymentCreatesNewEntry(): void
{
$this->saleLib->addPayment('credit', '25.00', 'ABC123');
$payments = $this->saleLib->getPayments();
$this->assertArrayHasKey('credit', $payments);
$this->assertSame('credit', $payments['credit']['payment_type']);
$this->assertSame('25.00', $payments['credit']['payment_amount']);
$this->assertSame(0, $payments['credit']['cash_refund']);
$this->assertSame(CASH_ADJUSTMENT_FALSE, $payments['credit']['cash_adjustment']);
}
public function testAddPaymentStoresReferenceCode(): void
{
$this->saleLib->addPayment('debit', '50.00', 'REF9876');
$payments = $this->saleLib->getPayments();
$this->assertSame('REF9876', $payments['debit']['reference_code']);
}
public function testAddPaymentNullReferenceCodeStoredAsNull(): void
{
$this->saleLib->addPayment('cash', '15.00');
$payments = $this->saleLib->getPayments();
$this->assertNull($payments['cash']['reference_code']);
}
public function testAddPaymentAccumulatesAmountForExistingId(): void
{
$this->saleLib->addPayment('credit', '10.00', 'REF001');
$this->saleLib->addPayment('credit', '5.00', 'REF001');
$payments = $this->saleLib->getPayments();
// bcadd strips trailing zeros: '10.00' + '5.00' = '15'
$this->assertSame('15', $payments['credit']['payment_amount']);
}
public function testAddPaymentCashAdjustmentFlagStored(): void
{
$this->saleLib->addPayment('cash_adjustment', '0.05', null, CASH_ADJUSTMENT_TRUE);
$payments = $this->saleLib->getPayments();
$this->assertSame(CASH_ADJUSTMENT_TRUE, $payments['cash_adjustment']['cash_adjustment']);
}
public function testAddPaymentMultipleDistinctTypesAllStored(): void
{
$this->saleLib->addPayment('credit', '30.00', 'REF1');
$this->saleLib->addPayment('debit', '20.00', 'REF2');
$payments = $this->saleLib->getPayments();
$this->assertCount(2, $payments);
$this->assertArrayHasKey('credit', $payments);
$this->assertArrayHasKey('debit', $payments);
}
// ========== edit_payment ==========
public function testEditPaymentUpdatesAmount(): void
{
$this->saleLib->addPayment('credit', '10.00', 'REF001');
$result = $this->saleLib->edit_payment('credit', 99.99);
$this->assertTrue($result);
$payments = $this->saleLib->getPayments();
$this->assertSame(99.99, $payments['credit']['payment_amount']);
}
public function testEditPaymentReturnsFalseForMissingId(): void
{
$result = $this->saleLib->edit_payment('nonexistent', 10.00);
$this->assertFalse($result);
}
// ========== delete_payment ==========
public function testDeletePaymentRemovesEntry(): void
{
$this->saleLib->addPayment('credit', '25.00', 'REF999');
$this->saleLib->delete_payment('credit');
$payments = $this->saleLib->getPayments();
$this->assertArrayNotHasKey('credit', $payments);
}
public function testDeletePaymentLeavesOtherEntriesIntact(): void
{
$this->saleLib->addPayment('credit', '25.00', 'REF1');
$this->saleLib->addPayment('debit', '10.00', 'REF2');
$this->saleLib->delete_payment('credit');
$payments = $this->saleLib->getPayments();
$this->assertArrayNotHasKey('credit', $payments);
$this->assertArrayHasKey('debit', $payments);
}
}