Files
opensourcepos/tests/Models/EmployeeTest.php
jekkos f25a0f5b09 Refactor: Move ADMIN_MODULES to constants, rename methods to camelCase
- Move admin modules list from is_admin method to ADMIN_MODULES constant
- Rename is_admin() to isAdmin() following CodeIgniter naming conventions
- Rename can_modify_employee() to canModifyEmployee() following conventions
- Update all callers in Employees controller and tests
2026-03-06 17:25:25 +01:00

169 lines
4.7 KiB
PHP

<?php
namespace Tests\Models;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use App\Models\Employee;
class EmployeeTest extends CIUnitTestCase
{
use DatabaseTestTrait;
protected $migrate = true;
protected $migrateOnce = true;
protected $refresh = true;
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);
}
}