-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPermissionTest.php
More file actions
125 lines (102 loc) · 4.41 KB
/
Copy pathPermissionTest.php
File metadata and controls
125 lines (102 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
declare(strict_types=1);
namespace Alchemy\AclBundle\Tests;
use Alchemy\AclBundle\Entity\AccessControlEntry;
use Alchemy\AclBundle\Mapping\ObjectMapping;
use Alchemy\AclBundle\Repository\PermissionRepositoryInterface;
use Alchemy\AclBundle\Repository\UserRepositoryInterface;
use Alchemy\AclBundle\Security\PermissionInterface;
use Alchemy\AclBundle\Security\PermissionManager;
use Alchemy\AclBundle\Tests\Mock\AclUserMock;
use Alchemy\AclBundle\Tests\Mock\ObjectMock;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class PermissionTest extends TestCase
{
#[DataProvider('permissionProvider')]
public function testPermissionsWithUser(array $acePermissions, int $permissionToTest, bool $expectedResult)
{
$ace = $this->createAce(AccessControlEntry::TYPE_USER_VALUE, '123', 'pub', '42', $acePermissions);
$objectMapper = $this->createMock(ObjectMapping::class);
$objectMapper
->expects($this->once())
->method('getObjectKey')
->willReturn('pub');
$permissionRepo = $this->createMock(PermissionRepositoryInterface::class);
$permissionRepo
->expects($this->once())
->method('getAces')
->willReturn([$ace]);
$user = new AclUserMock('123');
$userRepo = $this->createMock(UserRepositoryInterface::class);
$userRepo
->expects($this->once())
->method('getAclGroupsId')
->with($this->equalTo($user))
->willReturn([]);
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$permissionManager = new PermissionManager($objectMapper, $permissionRepo, $eventDispatcher, $userRepo);
$object = new ObjectMock('42');
$this->assertEquals($expectedResult, $permissionManager->isGranted($user, $object, $permissionToTest));
}
#[DataProvider('permissionProvider')]
public function testPermissionsWithGroup(array $acePermissions, int $permissionToTest, bool $expectedResult)
{
$userAce = $this->createAce(AccessControlEntry::TYPE_USER_VALUE, '123', 'pub', '42', []);
$ace = $this->createAce(AccessControlEntry::TYPE_GROUP_VALUE, 'group1', 'pub', '42', $acePermissions);
$objectMapper = $this->createMock(ObjectMapping::class);
$objectMapper
->expects($this->once())
->method('getObjectKey')
->willReturn('pub');
$permissionRepo = $this->createMock(PermissionRepositoryInterface::class);
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$permissionRepo
->expects($this->once())
->method('getAces')
->with($this->equalTo('123'), $this->equalTo(['group1']), $this->equalTo('pub'), $this->equalTo('42'))
->willReturn([
$userAce,
$ace,
]);
$user = new AclUserMock('123');
$userRepo = $this->createMock(UserRepositoryInterface::class);
$userRepo
->expects($this->once())
->method('getAclGroupsId')
->with($this->equalTo($user))
->willReturn([
'group1',
]);
$permissionManager = new PermissionManager($objectMapper, $permissionRepo, $eventDispatcher, $userRepo);
$object = new ObjectMock('42');
$this->assertEquals($expectedResult, $permissionManager->isGranted($user, $object, $permissionToTest));
}
public static function permissionProvider(): array
{
return [
[[], PermissionInterface::VIEW, false],
[[
PermissionInterface::DELETE,
PermissionInterface::EDIT,
], PermissionInterface::EDIT, true],
[[
PermissionInterface::DELETE,
PermissionInterface::EDIT,
], PermissionInterface::VIEW, false],
];
}
private function createAce(int $userType, string $userId, string $objectType, string $objectId, array $permissions): AccessControlEntry
{
$ace = new AccessControlEntry();
$ace->setObjectType($objectType);
$ace->setObjectId($objectId);
$ace->setUserType($userType);
$ace->setUserId($userId);
foreach ($permissions as $permission) {
$ace->addPermission($permission);
}
return $ace;
}
}