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

+79 -52
View File
@@ -2,17 +2,20 @@
namespace Tests\Controllers;
use CodeIgniter\Database\Config;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use CodeIgniter\Test\FeatureTestTrait;
use CodeIgniter\Config\Services;
use App\Models\Employee;
use App\Models\Module;
use Tests\Support\EmployeeFixtureTrait;
class EmployeesControllerTest extends CIUnitTestCase
{
use DatabaseTestTrait;
use FeatureTestTrait;
use EmployeeFixtureTrait;
protected $migrate = true;
protected $migrateOnce = true;
@@ -21,9 +24,26 @@ class EmployeesControllerTest extends CIUnitTestCase
protected $priorDisallowGrantChange;
private static bool $doneBootstrap = false;
protected function setUp(): void
{
// Reset the test database to a known clean schema on the first test in
// this class so stale employees from prior runs (whose usernames are
// unique-key bound and whose grant sets leak into these assertions)
// cannot contaminate the assertions below.
if (self::$doneBootstrap === false) {
Config::seeder($this->DBGroup)->call('App\Database\Seeds\TestDatabaseBootstrapSeeder');
Config::connect($this->DBGroup)->close();
self::$doneBootstrap = true;
}
parent::setUp();
// Reset any stale transaction state left on the shared in-process DB
// connection by a previous test (e.g. a failed transaction rollback in
// strict mode sets transStatus=false and poisons save_employee here).
$this->db->resetTransStatus();
$this->priorDisallowGrantChange = getenv('DISALLOW_GRANT_CHANGE');
putenv('DISALLOW_GRANT_CHANGE=false');
}
@@ -40,46 +60,28 @@ class EmployeesControllerTest extends CIUnitTestCase
protected function createNonAdminEmployee(): int
{
$personData = [
'first_name' => 'NonAdmin',
'last_name' => 'User',
'email' => 'nonadmin@test.com',
'phone_number' => '555-1234'
];
$employeeData = [
'username' => 'nonadmin',
'password' => password_hash('password123', PASSWORD_DEFAULT),
'hash_version' => 2,
'language_code' => 'en',
'language' => 'english'
];
$grantsData = [
['permission_id' => 'customers', 'menu_group' => 'home'],
['permission_id' => 'sales', 'menu_group' => 'home']
];
$employeeModel = model(Employee::class);
$employeeModel->save_employee($personData, $employeeData, $grantsData, NEW_ENTRY);
return $employeeModel->get_found_rows('');
return $this->createEmployee(
first_name: 'NonAdmin',
last_name: 'User',
email: 'nonadmin' . uniqid() . '@test.com',
username: 'nonadmin_' . uniqid(),
phone_number: '555-1234',
grants: [
['permission_id' => 'employees', 'menu_group' => 'home'],
['permission_id' => 'customers', 'menu_group' => 'home'],
['permission_id' => 'sales', 'menu_group' => 'home'],
],
);
}
protected function loginAsAdmin(): void
{
$session = Services::session();
$session->destroy();
$session->set('person_id', 1);
$session->set('menu_group', 'office');
$this->withSession(['person_id' => 1, 'menu_group' => 'office']);
}
protected function loginAsNonAdmin(int $personId): void
{
$session = Services::session();
$session->destroy();
$session->set('person_id', $personId);
$session->set('menu_group', 'home');
$this->withSession(['person_id' => $personId, 'menu_group' => 'home']);
}
public function testNonAdminCannotViewAdminAccount(): void
@@ -117,7 +119,7 @@ class EmployeesControllerTest extends CIUnitTestCase
$this->loginAsNonAdmin($nonAdminId);
$response = $this->post('/employees/delete', [
'ids' => [1]
'ids' => ['1']
]);
$response->assertStatus(200);
@@ -130,25 +132,25 @@ class EmployeesControllerTest extends CIUnitTestCase
{
$nonAdminId = $this->createNonAdminEmployee();
$this->loginAsNonAdmin($nonAdminId);
$targetEmployeeId = $nonAdminId + rand(1000, 9999);
$this->createTestEmployee($targetEmployeeId);
$targetEmployeeId = $this->createEmployee(email: 'test@test.com', username: 'testuser');
$response = $this->post('/employees/save/' . $targetEmployeeId, [
'first_name' => 'Test',
'last_name' => 'Employee',
'email' => 'test@test.com',
'username' => 'testuser',
'grant_employees' => 'employees',
'grant_config' => 'config'
'language' => 'en:english',
'grant_config' => 'config',
'grant_giftcards' => 'giftcards'
]);
$employeeModel = model(Employee::class);
$hasEmployeesGrant = $employeeModel->has_grant('employees', $targetEmployeeId);
$hasConfigGrant = $employeeModel->has_grant('config', $targetEmployeeId);
$this->assertFalse($hasEmployeesGrant);
$hasGiftcardsGrant = $employeeModel->has_grant('giftcards', $targetEmployeeId);
$this->assertFalse($hasConfigGrant);
$this->assertFalse($hasGiftcardsGrant);
}
public function testAdminCanModifyAnyAccount(): void
@@ -160,9 +162,10 @@ class EmployeesControllerTest extends CIUnitTestCase
'first_name' => 'Modified',
'last_name' => 'User',
'email' => 'modified@test.com',
'username' => 'nonadmin'
'username' => 'modified',
'language' => 'en:english'
]);
$response->assertStatus(200);
$result = json_decode($response->getJSON(), true);
$this->assertTrue($result['success']);
@@ -174,7 +177,7 @@ class EmployeesControllerTest extends CIUnitTestCase
$this->loginAsAdmin();
$response = $this->post('/employees/delete', [
'ids' => [$nonAdminId]
'ids' => [(string)$nonAdminId]
]);
$response->assertStatus(200);
@@ -191,7 +194,8 @@ class EmployeesControllerTest extends CIUnitTestCase
'first_name' => 'Modified',
'last_name' => 'OwnAccount',
'email' => 'own@test.com',
'username' => 'nonadmin'
'username' => 'owned',
'language' => 'en:english'
]);
$response->assertStatus(200);
@@ -228,7 +232,8 @@ class EmployeesControllerTest extends CIUnitTestCase
'first_name' => 'NonAdmin',
'last_name' => 'User',
'email' => 'nonadmin@test.com',
'username' => 'nonadmin'
'username' => 'grantany' . uniqid(),
'language' => 'en:english'
];
foreach ($permissionsRequested as $perm) {
$postData['grant_' . $perm] = $perm;
@@ -248,7 +253,18 @@ class EmployeesControllerTest extends CIUnitTestCase
public function testGrantChangeRequestFailsWhenGrantChangeDisallowed(): void
{
$employeeId = $this->createNonAdminEmployee();
// Target holds customers+sales but NOT employees, so requesting the
// employees grant is a genuine change that DISALLOW_GRANT_CHANGE=true
// must reject (leaving the grant set untouched).
$unique = uniqid();
$employeeId = $this->createEmployee(
email: "disallow{$unique}@test.com",
username: "disallow{$unique}",
grants: [
['permission_id' => 'customers', 'menu_group' => 'home'],
['permission_id' => 'sales', 'menu_group' => 'home'],
],
);
$this->loginAsAdmin();
putenv('DISALLOW_GRANT_CHANGE=true');
@@ -256,8 +272,9 @@ class EmployeesControllerTest extends CIUnitTestCase
$response = $this->post('/employees/save/' . $employeeId, [
'first_name' => 'NonAdmin',
'last_name' => 'User',
'email' => 'nonadmin@test.com',
'username' => 'nonadmin',
'email' => "disallow{$unique}@test.com",
'username' => "disallow{$unique}",
'language' => 'en:english',
'grant_employees' => 'employees'
]);
@@ -305,7 +322,8 @@ class EmployeesControllerTest extends CIUnitTestCase
'first_name' => 'NonAdmin',
'last_name' => 'User',
'email' => 'nonadmin@test.com',
'username' => 'nonadmin',
'username' => 'grantsucc',
'language' => 'en:english',
'grant_employees' => 'employees'
]);
@@ -329,8 +347,17 @@ class EmployeesControllerTest extends CIUnitTestCase
'first_name' => 'Brand',
'last_name' => 'New2',
'email' => 'brandnew2@test.com',
'phone_number' => '555-1234',
'address_1' => '',
'address_2' => '',
'city' => '',
'state' => '',
'zip' => '',
'country' => '',
'comments' => '',
'username' => 'brandnew2',
'password' => 'password123',
'language' => 'en:english',
'grant_customers' => 'customers'
]);