2026-02-24 08:49:40 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MintyPHP\Tests\Service\Access;
|
|
|
|
|
|
|
|
|
|
use MintyPHP\Service\Access\DepartmentAuthorizationPolicy;
|
|
|
|
|
use MintyPHP\Service\Access\PermissionService;
|
2026-03-13 11:31:33 +01:00
|
|
|
use MintyPHP\Service\Tenant\TenantScopeService;
|
2026-02-24 08:49:40 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
|
|
class DepartmentAuthorizationPolicyTest extends TestCase
|
|
|
|
|
{
|
2026-03-04 15:56:58 +01:00
|
|
|
use AuthorizationPolicyTestSupport;
|
|
|
|
|
|
2026-02-24 08:49:40 +01:00
|
|
|
public function testViewRequiresDepartmentsViewPermission(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->createMock(PermissionService::class);
|
|
|
|
|
$permissionService->expects($this->once())
|
2026-02-24 08:49:40 +01:00
|
|
|
->method('userHas')
|
|
|
|
|
->with(5, PermissionService::DEPARTMENTS_VIEW)
|
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->never())->method('getUserTenantIds');
|
|
|
|
|
$scopeGateway->expects($this->never())->method('isStrict');
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new DepartmentAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_VIEW, [
|
|
|
|
|
'actor_user_id' => 5,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$this->assertForbiddenDecision($decision);
|
2026-02-24 08:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testViewReturnsCreateCapabilityAndTenantScopeData(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->permissionGatewayAllowing([
|
2026-03-04 15:56:58 +01:00
|
|
|
5 => [PermissionService::DEPARTMENTS_VIEW, PermissionService::DEPARTMENTS_CREATE],
|
|
|
|
|
]);
|
2026-02-24 08:49:40 +01:00
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('getUserTenantIds')
|
|
|
|
|
->with(5)
|
|
|
|
|
->willReturn([2, 3]);
|
|
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('isStrict')
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new DepartmentAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_VIEW, [
|
|
|
|
|
'actor_user_id' => 5,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$capabilities = $decision->attribute('capabilities', []);
|
|
|
|
|
$this->assertTrue($decision->isAllowed());
|
|
|
|
|
$this->assertTrue((bool) ($capabilities['can_create_department'] ?? false));
|
|
|
|
|
$this->assertSame([2, 3], $capabilities['allowed_tenant_ids'] ?? []);
|
|
|
|
|
$this->assertTrue((bool) ($capabilities['is_strict_scope'] ?? false));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCreateRequiresDepartmentsCreatePermission(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->createMock(PermissionService::class);
|
|
|
|
|
$permissionService->expects($this->once())
|
2026-02-24 08:49:40 +01:00
|
|
|
->method('userHas')
|
|
|
|
|
->with(7, PermissionService::DEPARTMENTS_CREATE)
|
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->never())->method('getUserTenantIds');
|
|
|
|
|
$scopeGateway->expects($this->never())->method('isStrict');
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new DepartmentAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_CREATE, [
|
|
|
|
|
'actor_user_id' => 7,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$this->assertDeniedDecision($decision, 403, 'forbidden');
|
2026-02-24 08:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCreateDeniedInStrictScopeWithoutTenantAssignments(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->permissionGatewayAllowing([
|
2026-03-04 15:56:58 +01:00
|
|
|
7 => [PermissionService::DEPARTMENTS_CREATE],
|
|
|
|
|
]);
|
2026-02-24 08:49:40 +01:00
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('getUserTenantIds')
|
|
|
|
|
->with(7)
|
|
|
|
|
->willReturn([]);
|
|
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('isStrict')
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new DepartmentAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_CREATE, [
|
|
|
|
|
'actor_user_id' => 7,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$this->assertDeniedDecision($decision, 403, 'permission_denied');
|
2026-02-24 08:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCreateReturnsScopeCapabilitiesWhenAllowed(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->permissionGatewayAllowing([
|
2026-03-04 15:56:58 +01:00
|
|
|
7 => [PermissionService::DEPARTMENTS_CREATE],
|
|
|
|
|
]);
|
2026-02-24 08:49:40 +01:00
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('getUserTenantIds')
|
|
|
|
|
->with(7)
|
|
|
|
|
->willReturn([4]);
|
|
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('isStrict')
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new DepartmentAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_CREATE, [
|
|
|
|
|
'actor_user_id' => 7,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$capabilities = $decision->attribute('capabilities', []);
|
|
|
|
|
$this->assertTrue($decision->isAllowed());
|
|
|
|
|
$this->assertSame([4], $capabilities['allowed_tenant_ids'] ?? []);
|
|
|
|
|
$this->assertTrue((bool) ($capabilities['is_strict_scope'] ?? false));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testEditContextRequiresDepartmentsViewPermission(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->createMock(PermissionService::class);
|
|
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->never())->method('canAccess');
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new DepartmentAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_EDIT_CONTEXT, [
|
|
|
|
|
'actor_user_id' => 5,
|
|
|
|
|
'target_department_id' => 17,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$this->assertForbiddenDecision($decision);
|
2026-02-24 08:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testEditContextDeniesOutOfScopeDepartment(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->permissionGatewayAllowing([
|
2026-03-04 15:56:58 +01:00
|
|
|
5 => [PermissionService::DEPARTMENTS_VIEW],
|
|
|
|
|
]);
|
2026-02-24 08:49:40 +01:00
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('canAccess')
|
|
|
|
|
->with('departments', 17, 5)
|
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new DepartmentAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_EDIT_CONTEXT, [
|
|
|
|
|
'actor_user_id' => 5,
|
|
|
|
|
'target_department_id' => 17,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$this->assertDeniedDecision($decision, 403, 'permission_denied');
|
2026-02-24 08:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-12 13:01:06 +01:00
|
|
|
public function testEditContextExposesDeleteCapabilityWhenDeletePermissionIsGranted(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->permissionGatewayAllowing([
|
2026-03-12 13:01:06 +01:00
|
|
|
5 => [PermissionService::DEPARTMENTS_VIEW, PermissionService::DEPARTMENTS_DELETE],
|
|
|
|
|
]);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-03-12 13:01:06 +01:00
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('canAccess')
|
|
|
|
|
->with('departments', 17, 5)
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('getUserTenantIds')
|
|
|
|
|
->with(5)
|
|
|
|
|
->willReturn([2]);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new DepartmentAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-03-12 13:01:06 +01:00
|
|
|
$decision = $policy->authorize(DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_EDIT_CONTEXT, [
|
|
|
|
|
'actor_user_id' => 5,
|
|
|
|
|
'target_department_id' => 17,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$capabilities = $decision->attribute('capabilities', []);
|
|
|
|
|
$this->assertTrue($decision->isAllowed());
|
|
|
|
|
$this->assertTrue((bool) ($capabilities['can_delete_department'] ?? false));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testEditContextExposesDeleteCapabilityAsFalseWithoutDeletePermission(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->permissionGatewayAllowing([
|
2026-03-12 13:01:06 +01:00
|
|
|
5 => [PermissionService::DEPARTMENTS_VIEW],
|
|
|
|
|
]);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-03-12 13:01:06 +01:00
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('canAccess')
|
|
|
|
|
->with('departments', 17, 5)
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('getUserTenantIds')
|
|
|
|
|
->with(5)
|
|
|
|
|
->willReturn([2]);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new DepartmentAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-03-12 13:01:06 +01:00
|
|
|
$decision = $policy->authorize(DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_EDIT_CONTEXT, [
|
|
|
|
|
'actor_user_id' => 5,
|
|
|
|
|
'target_department_id' => 17,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$capabilities = $decision->attribute('capabilities', []);
|
|
|
|
|
$this->assertTrue($decision->isAllowed());
|
|
|
|
|
$this->assertFalse((bool) ($capabilities['can_delete_department'] ?? true));
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 08:49:40 +01:00
|
|
|
public function testEditSubmitRequiresDepartmentsUpdatePermission(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->permissionGatewayAllowing([
|
2026-03-04 15:56:58 +01:00
|
|
|
5 => [PermissionService::DEPARTMENTS_VIEW],
|
|
|
|
|
]);
|
2026-02-24 08:49:40 +01:00
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('canAccess')
|
|
|
|
|
->with('departments', 17, 5)
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('getUserTenantIds')
|
|
|
|
|
->with(5)
|
|
|
|
|
->willReturn([2]);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new DepartmentAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_EDIT_SUBMIT, [
|
|
|
|
|
'actor_user_id' => 5,
|
|
|
|
|
'target_department_id' => 17,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$this->assertDeniedDecision($decision, 403, 'forbidden');
|
2026-02-24 08:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testDeleteRequiresPermissionAndScope(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->permissionGatewayAllowing([
|
2026-03-04 15:56:58 +01:00
|
|
|
9 => [PermissionService::DEPARTMENTS_DELETE],
|
|
|
|
|
]);
|
2026-02-24 08:49:40 +01:00
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('canAccess')
|
|
|
|
|
->with('departments', 44, 9)
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new DepartmentAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_DELETE, [
|
|
|
|
|
'actor_user_id' => 9,
|
|
|
|
|
'target_department_id' => 44,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($decision->isAllowed());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testDeleteDeniesOutOfScopeDepartment(): void
|
|
|
|
|
{
|
2026-03-13 11:31:33 +01:00
|
|
|
$permissionService = $this->permissionGatewayAllowing([
|
2026-03-04 15:56:58 +01:00
|
|
|
9 => [PermissionService::DEPARTMENTS_DELETE],
|
|
|
|
|
]);
|
2026-02-24 08:49:40 +01:00
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$scopeGateway = $this->createMock(TenantScopeService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$scopeGateway->expects($this->once())
|
|
|
|
|
->method('canAccess')
|
|
|
|
|
->with('departments', 44, 9)
|
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
2026-03-13 11:31:33 +01:00
|
|
|
$policy = new DepartmentAuthorizationPolicy($permissionService, $scopeGateway);
|
2026-02-24 08:49:40 +01:00
|
|
|
$decision = $policy->authorize(DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_DELETE, [
|
|
|
|
|
'actor_user_id' => 9,
|
|
|
|
|
'target_department_id' => 44,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$this->assertDeniedDecision($decision, 403, 'permission_denied');
|
2026-02-24 08:49:40 +01:00
|
|
|
}
|
|
|
|
|
}
|