Files
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

259 lines
8.0 KiB
PHP

<?php
namespace Tests\Models;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use App\Models\Employee;
use Tests\Support\EmployeeFixtureTrait;
class EmployeeTest extends CIUnitTestCase
{
use DatabaseTestTrait;
use EmployeeFixtureTrait;
protected $migrate = true;
protected $migrateOnce = true;
protected $refresh = false;
protected $namespace = null;
protected function setUp(): void
{
parent::setUp();
}
public function testIsAdminReturnsTrueForPersonId1(): void
{
$employeeModel = model(Employee::class);
$result = $employeeModel->isAdmin(1);
$this->assertTrue($result);
}
public function testIsAdminReturnsTrueForEmployeeWithAllPermissions(): void
{
$employeeModel = $this->getMockBuilder(Employee::class)
->onlyMethods(['has_grant'])
->getMock();
$employeeModel->method('has_grant')
->willReturn(true);
$result = $employeeModel->isAdmin(2);
$this->assertTrue($result);
}
public function testIsAdminReturnsFalseWhenMissingPermissions(): void
{
$employeeModel = $this->getMockBuilder(Employee::class)
->onlyMethods(['has_grant'])
->getMock();
$employeeModel->method('has_grant')
->willReturnCallback(function($permissionId, $personId) {
return $permissionId !== 'config';
});
$result = $employeeModel->isAdmin(3);
$this->assertFalse($result);
}
public function testCanModifyEmployeeReturnsTrueForOwnAccount(): void
{
$employeeModel = $this->getMockBuilder(Employee::class)
->onlyMethods(['isAdmin'])
->getMock();
$employeeModel->method('isAdmin')
->willReturn(false);
$result = $employeeModel->canModifyEmployee(1, 1);
$this->assertTrue($result);
}
public function testCanModifyEmployeeReturnsTrueForOwnAdminAccount(): void
{
$employeeModel = $this->getMockBuilder(Employee::class)
->onlyMethods(['isAdmin'])
->getMock();
$employeeModel->method('isAdmin')
->willReturn(true);
$result = $employeeModel->canModifyEmployee(1, 1);
$this->assertTrue($result);
}
public function testCanModifyEmployeeReturnsFalseWhenNonAdminModifiesAdmin(): void
{
$employeeModel = $this->getMockBuilder(Employee::class)
->onlyMethods(['isAdmin'])
->getMock();
$employeeModel->method('isAdmin')
->willReturnCallback(function($personId) {
return $personId === 1;
});
$result = $employeeModel->canModifyEmployee(1, 2);
$this->assertFalse($result);
}
public function testCanModifyEmployeeReturnsTrueWhenAdminModifiesNonAdmin(): void
{
$employeeModel = $this->getMockBuilder(Employee::class)
->onlyMethods(['isAdmin'])
->getMock();
$employeeModel->method('isAdmin')
->willReturnCallback(function($personId) {
return $personId === 1;
});
$result = $employeeModel->canModifyEmployee(2, 1);
$this->assertTrue($result);
}
public function testCanModifyEmployeeReturnsTrueWhenNonAdminModifiesNonAdmin(): void
{
$employeeModel = $this->getMockBuilder(Employee::class)
->onlyMethods(['isAdmin'])
->getMock();
$employeeModel->method('isAdmin')
->willReturn(false);
$result = $employeeModel->canModifyEmployee(2, 3);
$this->assertTrue($result);
}
public function testCanModifyEmployeeReturnsFalseForNonAdminEditingAdmin(): void
{
$employeeModel = $this->getMockBuilder(Employee::class)
->onlyMethods(['isAdmin'])
->getMock();
$employeeModel->method('isAdmin')
->willReturnCallback(function($personId) {
return $personId === 1;
});
$result = $employeeModel->canModifyEmployee(1, 2);
$this->assertFalse($result);
}
public function testHasGrantReturnsTrueForActualGrant(): void
{
$employeeModel = model(Employee::class);
$result = $employeeModel->has_grant('employees', 1);
$this->assertTrue($result);
}
public function testHasGrantReturnsFalseForMissingGrant(): void
{
$employeeModel = model(Employee::class);
$result = $employeeModel->has_grant('nonexistent_permission', 1);
$this->assertFalse($result);
}
public function testExistingEmployeeKeepsOriginalGrantsWhenGrantChangeDisallowed(): void
{
$employeeId = $this->createEmployee(
first_name: 'Grant',
last_name: 'Tester',
grants: [
['permission_id' => 'customers', 'menu_group' => 'home']
],
);
$originalDisallowGrantChange = getenv('DISALLOW_GRANT_CHANGE');
putenv('DISALLOW_GRANT_CHANGE=true');
try {
$employeeModel = model(Employee::class);
$personData = ['first_name' => 'Grant', 'last_name' => 'Tester', 'email' => "granttester_upd_{$employeeId}@test.com"];
$employeeData = ['username' => "granttester_upd_{$employeeId}", 'language_code' => 'en', 'language' => 'english'];
$newGrantsData = [['permission_id' => 'sales', 'menu_group' => 'home']];
$saveEmployeeResult = $employeeModel->save_employee($personData, $employeeData, $newGrantsData, $employeeId);
$this->assertTrue($saveEmployeeResult);
$this->assertTrue($employeeModel->has_grant('customers', $employeeId));
$this->assertFalse($employeeModel->has_grant('sales', $employeeId));
} finally {
$originalDisallowGrantChange === false
? putenv('DISALLOW_GRANT_CHANGE')
: putenv("DISALLOW_GRANT_CHANGE={$originalDisallowGrantChange}");
}
}
public function testNewEmployeeCreationWithGrantsRejectedWhenGrantChangeDisallowed(): void
{
$originalDisallowGrantChange = getenv('DISALLOW_GRANT_CHANGE');
putenv('DISALLOW_GRANT_CHANGE=true');
try {
$result = $this->createEmployeeExpectingFailure(
first_name: 'Rejected',
last_name: 'Tester',
grants: [
['permission_id' => 'customers', 'menu_group' => 'home']
],
);
$this->assertFalse($result);
} finally {
$originalDisallowGrantChange === false
? putenv('DISALLOW_GRANT_CHANGE')
: putenv("DISALLOW_GRANT_CHANGE={$originalDisallowGrantChange}");
}
}
public function testExistingEmployeeGrantsUpdateWhenGrantChangeAllowed(): void
{
$employeeId = $this->createEmployee(
first_name: 'Grant',
last_name: 'Tester',
grants: [
['permission_id' => 'customers', 'menu_group' => 'home']
],
);
$employeeModel = model(Employee::class);
$personData = ['first_name' => 'Grant', 'last_name' => 'Tester', 'email' => "granttester_upd_{$employeeId}@test.com"];
$employeeData = ['username' => "granttester_upd_{$employeeId}", 'language_code' => 'en', 'language' => 'english'];
$newGrantsData = [['permission_id' => 'sales', 'menu_group' => 'home']];
$employeeModel->save_employee($personData, $employeeData, $newGrantsData, $employeeId);
$this->assertFalse($employeeModel->has_grant('customers', $employeeId));
$this->assertTrue($employeeModel->has_grant('sales', $employeeId));
}
public function testNewEmployeeCreationWithGrantsSucceedsWhenGrantChangeAllowed(): void
{
$result = $this->createEmployeeExpectingFailure(
first_name: 'Granted',
last_name: 'Tester',
grants: [
['permission_id' => 'customers', 'menu_group' => 'home']
],
);
$this->assertTrue((bool) $result);
}
}