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.
This commit is contained in:
jekkos authored and GitHub committed 2026-09-08 21:49:28 +02:00
1 parent 9ecabf6f41
commit 28755dfd50
21 files changed
+562 -375

No files matched your search

+52 -140
View File
@@ -10,6 +10,7 @@ use CodeIgniter\Config\Services;
use App\Models\Employee;
use Config\Database;
use Tests\Support\ItemFixtureTrait;
use Tests\Support\EmployeeFixtureTrait;
use Tests\Support\SaleFixtureTrait;
/**
@@ -29,6 +30,7 @@ class SalesControllerTest extends CIUnitTestCase
use DatabaseTestTrait;
use FeatureTestTrait;
use ItemFixtureTrait;
use EmployeeFixtureTrait;
use SaleFixtureTrait;
protected $migrate = true;
@@ -56,160 +58,70 @@ class SalesControllerTest extends CIUnitTestCase
parent::tearDown();
}
/**
* Cashier with bare "sales" + "sales_stock" grants. "sales_stock" is
* required alongside "sales": Employee::has_module_grant('sales', ...)
* treats the bare "sales" grant as insufficient once any sales_*
* submodule permission exists in the permissions table (see
* has_subpermissions()). Deliberately NOT "reports_sales" — that is the
* grant this test exercises the gate on.
*/
protected function createCashierEmployee(): int
{
$unique = uniqid();
$personData = [
'first_name' => 'Cashier',
'last_name' => 'NoReports',
'email' => "cashier.$unique@test.com",
'phone_number' => '555-0001',
'address_1' => '',
'address_2' => '',
'city' => '',
'state' => '',
'zip' => '',
'country' => '',
'comments' => '',
];
$employeeData = [
'username' => "cashier.$unique",
'password' => password_hash('password123', PASSWORD_DEFAULT),
'hash_version' => 2,
'language_code' => 'en',
'language' => 'english'
];
// Deliberately grants "sales" (register access) but NOT "reports_sales".
// "sales_stock" is also required: Employee::has_module_grant('sales', ...)
// treats the bare "sales" grant as insufficient once any sales_* submodule
// permission exists in the permissions table (see has_subpermissions()).
$grantsData = [
['permission_id' => 'sales', 'menu_group' => 'home'],
['permission_id' => 'sales_stock', 'menu_group' => 'home']
];
$employeeModel = model(Employee::class);
$this->assertTrue($employeeModel->save_employee($personData, $employeeData, $grantsData, NEW_ENTRY));
return (int) $personData['person_id'];
return $this->createEmployee(
first_name: 'Cashier',
last_name: 'NoReports',
email: 'cashier.' . uniqid() . '@test.com',
username: 'cashier_' . uniqid(),
grants: [
['permission_id' => 'sales', 'menu_group' => 'home'],
['permission_id' => 'sales_stock', 'menu_group' => 'home'],
],
);
}
protected function createReportsSalesEmployee(): int
{
$unique = uniqid();
$personData = [
'first_name' => 'Supervisor',
'last_name' => 'WithReports',
'email' => "supervisor.$unique@test.com",
'phone_number' => '555-0002',
'address_1' => '',
'address_2' => '',
'city' => '',
'state' => '',
'zip' => '',
'country' => '',
'comments' => '',
];
$employeeData = [
'username' => "supervisor.$unique",
'password' => password_hash('password123', PASSWORD_DEFAULT),
'hash_version' => 2,
'language_code' => 'en',
'language' => 'english'
];
$grantsData = [
['permission_id' => 'sales', 'menu_group' => 'home'],
['permission_id' => 'sales_stock', 'menu_group' => 'home'],
['permission_id' => 'reports_sales', 'menu_group' => 'home']
];
$employeeModel = model(Employee::class);
$this->assertTrue($employeeModel->save_employee($personData, $employeeData, $grantsData, NEW_ENTRY));
return (int) $personData['person_id'];
return $this->createEmployee(
first_name: 'Supervisor',
last_name: 'WithReports',
email: 'supervisor.' . uniqid() . '@test.com',
username: 'supervisor_' . uniqid(),
grants: [
['permission_id' => 'sales', 'menu_group' => 'home'],
['permission_id' => 'sales_stock', 'menu_group' => 'home'],
['permission_id' => 'reports_sales', 'menu_group' => 'home'],
],
);
}
protected function createCashierWithoutChangePriceGrant(): int
{
$unique = uniqid();
$personData = [
'first_name' => 'Cashier',
'last_name' => 'NoChangePrice',
'email' => "cashier-nochangeprice.$unique@test.com",
'phone_number' => '555-0001',
'address_1' => '',
'address_2' => '',
'city' => '',
'state' => '',
'zip' => '',
'country' => '',
'comments' => '',
];
$employeeData = [
'username' => "cashier_nochangeprice.$unique",
'password' => password_hash('password123', PASSWORD_DEFAULT),
'hash_version' => 2,
'language_code' => 'en',
'language' => 'english'
];
// "sales_stock" is required alongside "sales": see the has_module_grant/
// has_subpermissions note on createCashierEmployee() above.
$grantsData = [
['permission_id' => 'sales', 'menu_group' => 'home'],
['permission_id' => 'sales_stock', 'menu_group' => 'home'],
];
$employeeModel = model(Employee::class);
$this->assertTrue($employeeModel->save_employee($personData, $employeeData, $grantsData, NEW_ENTRY));
return (int) $personData['person_id'];
return $this->createEmployee(
first_name: 'Cashier',
last_name: 'NoChangePrice',
email: 'cashier-nochangeprice.' . uniqid() . '@test.com',
username: 'cashier_nochangeprice_' . uniqid(),
grants: [
['permission_id' => 'sales', 'menu_group' => 'home'],
['permission_id' => 'sales_stock', 'menu_group' => 'home'],
],
);
}
protected function createCashierWithChangePriceGrant(): int
{
$unique = uniqid();
$personData = [
'first_name' => 'Cashier',
'last_name' => 'ChangePrice',
'email' => "cashier-changeprice.$unique@test.com",
'phone_number' => '555-0002',
'address_1' => '',
'address_2' => '',
'city' => '',
'state' => '',
'zip' => '',
'country' => '',
'comments' => '',
];
$employeeData = [
'username' => "cashier_changeprice.$unique",
'password' => password_hash('password123', PASSWORD_DEFAULT),
'hash_version' => 2,
'language_code' => 'en',
'language' => 'english'
];
$grantsData = [
['permission_id' => 'sales', 'menu_group' => 'home'],
['permission_id' => 'sales_stock', 'menu_group' => 'home'],
['permission_id' => 'sales_change_price', 'menu_group' => 'home'],
];
$employeeModel = model(Employee::class);
$this->assertTrue($employeeModel->save_employee($personData, $employeeData, $grantsData, NEW_ENTRY));
return (int) $personData['person_id'];
return $this->createEmployee(
first_name: 'Cashier',
last_name: 'ChangePrice',
email: 'cashier-changeprice.' . uniqid() . '@test.com',
username: 'cashier_changeprice_' . uniqid(),
grants: [
['permission_id' => 'sales', 'menu_group' => 'home'],
['permission_id' => 'sales_stock', 'menu_group' => 'home'],
['permission_id' => 'sales_change_price', 'menu_group' => 'home'],
],
);
}
protected function loginAs(int $personId): void