|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the RoleIdTest class. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace eZ\Publish\Core\MVC\Symfony\Security\Tests\User\Identifier; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\RoleService; |
|
12
|
|
|
use eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation; |
|
13
|
|
|
use eZ\Publish\API\Repository\Values\User\Role; |
|
14
|
|
|
use eZ\Publish\API\Repository\Values\User\User as APIUser; |
|
15
|
|
|
use eZ\Publish\API\Repository\Values\User\UserReference; |
|
16
|
|
|
use eZ\Publish\Core\MVC\Symfony\Security\User\ContextProvider\RoleContextProvider; |
|
17
|
|
|
use eZ\Publish\Core\Repository\Helper\LimitationService; |
|
18
|
|
|
use eZ\Publish\Core\Repository\Helper\RoleDomainMapper; |
|
19
|
|
|
use eZ\Publish\Core\Repository\Permission\PermissionResolver; |
|
20
|
|
|
use eZ\Publish\Core\Repository\Repository; |
|
21
|
|
|
use eZ\Publish\Core\Repository\Values\User\UserRoleAssignment; |
|
22
|
|
|
use eZ\Publish\SPI\Persistence\User\Handler as SPIUserHandler; |
|
23
|
|
|
use FOS\HttpCache\UserContext\UserContext; |
|
24
|
|
|
use PHPUnit\Framework\TestCase; |
|
25
|
|
|
|
|
26
|
|
|
class RoleContextProviderTest extends TestCase |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
|
30
|
|
|
*/ |
|
31
|
|
|
private $repositoryMock; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
|
35
|
|
|
*/ |
|
36
|
|
|
private $roleServiceMock; |
|
37
|
|
|
|
|
38
|
|
|
protected function setUp() |
|
39
|
|
|
{ |
|
40
|
|
|
parent::setUp(); |
|
41
|
|
|
$this->repositoryMock = $this |
|
42
|
|
|
->getMockBuilder(Repository::class) |
|
43
|
|
|
->disableOriginalConstructor() |
|
44
|
|
|
->setMethods(array('getRoleService', 'getCurrentUser', 'getPermissionResolver')) |
|
45
|
|
|
->getMock(); |
|
46
|
|
|
|
|
47
|
|
|
$this->roleServiceMock = $this->createMock(RoleService::class); |
|
48
|
|
|
|
|
49
|
|
|
$this->repositoryMock |
|
50
|
|
|
->expects($this->any()) |
|
51
|
|
|
->method('getRoleService') |
|
52
|
|
|
->will($this->returnValue($this->roleServiceMock)); |
|
53
|
|
|
$this->repositoryMock |
|
54
|
|
|
->expects($this->any()) |
|
55
|
|
|
->method('getPermissionResolver') |
|
56
|
|
|
->will($this->returnValue($this->getPermissionResolverMock())); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testSetIdentity() |
|
60
|
|
|
{ |
|
61
|
|
|
$user = $this->createMock(APIUser::class); |
|
62
|
|
|
$userContext = new UserContext(); |
|
63
|
|
|
|
|
64
|
|
|
$this->repositoryMock |
|
65
|
|
|
->expects($this->once()) |
|
66
|
|
|
->method('getCurrentUser') |
|
67
|
|
|
->will($this->returnValue($user)); |
|
68
|
|
|
|
|
69
|
|
|
$roleId1 = 123; |
|
70
|
|
|
$roleId2 = 456; |
|
71
|
|
|
$roleId3 = 789; |
|
72
|
|
|
$limitationForRole2 = $this->generateLimitationMock( |
|
73
|
|
|
array( |
|
74
|
|
|
'limitationValues' => array('/1/2', '/1/2/43'), |
|
75
|
|
|
) |
|
76
|
|
|
); |
|
77
|
|
|
$limitationForRole3 = $this->generateLimitationMock( |
|
78
|
|
|
array( |
|
79
|
|
|
'limitationValues' => array('foo', 'bar'), |
|
80
|
|
|
) |
|
81
|
|
|
); |
|
82
|
|
|
$returnedRoleAssignments = array( |
|
83
|
|
|
$this->generateRoleAssignmentMock( |
|
84
|
|
|
array( |
|
85
|
|
|
'role' => $this->generateRoleMock( |
|
86
|
|
|
array( |
|
87
|
|
|
'id' => $roleId1, |
|
88
|
|
|
) |
|
89
|
|
|
), |
|
90
|
|
|
) |
|
91
|
|
|
), |
|
92
|
|
|
$this->generateRoleAssignmentMock( |
|
93
|
|
|
array( |
|
94
|
|
|
'role' => $this->generateRoleMock( |
|
95
|
|
|
array( |
|
96
|
|
|
'id' => $roleId2, |
|
97
|
|
|
) |
|
98
|
|
|
), |
|
99
|
|
|
'limitation' => $limitationForRole2, |
|
100
|
|
|
) |
|
101
|
|
|
), |
|
102
|
|
|
$this->generateRoleAssignmentMock( |
|
103
|
|
|
array( |
|
104
|
|
|
'role' => $this->generateRoleMock( |
|
105
|
|
|
array( |
|
106
|
|
|
'id' => $roleId3, |
|
107
|
|
|
) |
|
108
|
|
|
), |
|
109
|
|
|
'limitation' => $limitationForRole3, |
|
110
|
|
|
) |
|
111
|
|
|
), |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
$this->roleServiceMock |
|
115
|
|
|
->expects($this->once()) |
|
116
|
|
|
->method('getRoleAssignmentsForUser') |
|
117
|
|
|
->with($user, true) |
|
118
|
|
|
->will($this->returnValue($returnedRoleAssignments)); |
|
119
|
|
|
|
|
120
|
|
|
$this->assertSame(array(), $userContext->getParameters()); |
|
121
|
|
|
$contextProvider = new RoleContextProvider($this->repositoryMock); |
|
122
|
|
|
$contextProvider->updateUserContext($userContext); |
|
123
|
|
|
$userContextParams = $userContext->getParameters(); |
|
124
|
|
|
$this->assertArrayHasKey('roleIdList', $userContextParams); |
|
125
|
|
|
$this->assertSame(array($roleId1, $roleId2, $roleId3), $userContextParams['roleIdList']); |
|
126
|
|
|
$this->assertArrayHasKey('roleLimitationList', $userContextParams); |
|
127
|
|
|
$limitationIdentifierForRole2 = get_class($limitationForRole2); |
|
128
|
|
|
$limitationIdentifierForRole3 = get_class($limitationForRole3); |
|
129
|
|
|
$this->assertSame( |
|
130
|
|
|
array( |
|
131
|
|
|
"$roleId2-$limitationIdentifierForRole2" => array('/1/2', '/1/2/43'), |
|
132
|
|
|
"$roleId3-$limitationIdentifierForRole3" => array('foo', 'bar'), |
|
133
|
|
|
), |
|
134
|
|
|
$userContextParams['roleLimitationList'] |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
private function generateRoleAssignmentMock(array $properties = array()) |
|
139
|
|
|
{ |
|
140
|
|
|
return $this |
|
141
|
|
|
->getMockBuilder(UserRoleAssignment::class) |
|
142
|
|
|
->setConstructorArgs(array($properties)) |
|
143
|
|
|
->getMockForAbstractClass(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
private function generateRoleMock(array $properties = array()) |
|
147
|
|
|
{ |
|
148
|
|
|
return $this |
|
149
|
|
|
->getMockBuilder(Role::class) |
|
150
|
|
|
->setConstructorArgs(array($properties)) |
|
151
|
|
|
->getMockForAbstractClass(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
private function generateLimitationMock(array $properties = array()) |
|
155
|
|
|
{ |
|
156
|
|
|
$limitationMock = $this |
|
157
|
|
|
->getMockBuilder(RoleLimitation::class) |
|
158
|
|
|
->setConstructorArgs(array($properties)) |
|
159
|
|
|
->getMockForAbstractClass(); |
|
160
|
|
|
$limitationMock |
|
161
|
|
|
->expects($this->any()) |
|
162
|
|
|
->method('getIdentifier') |
|
163
|
|
|
->will($this->returnValue(get_class($limitationMock))); |
|
164
|
|
|
|
|
165
|
|
|
return $limitationMock; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
protected function getPermissionResolverMock() |
|
169
|
|
|
{ |
|
170
|
|
|
return $this |
|
171
|
|
|
->getMockBuilder(PermissionResolver::class) |
|
172
|
|
|
->setMethods(null) |
|
173
|
|
|
->setConstructorArgs( |
|
174
|
|
|
[ |
|
175
|
|
|
$this->createMock(RoleDomainMapper::class), |
|
176
|
|
|
$this->createMock(LimitationService::class), |
|
177
|
|
|
$this->createMock(SPIUserHandler::class), |
|
178
|
|
|
$this->createMock(UserReference::class), |
|
179
|
|
|
] |
|
180
|
|
|
) |
|
181
|
|
|
->getMock(); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|