1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File contains: eZ\Publish\Core\Repository\Tests\Service\Mock\PermissionCriterionHandlerTest 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\Repository\Permission; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Avoid test failure caused by time passing between generating expected & actual object. |
13
|
|
|
* |
14
|
|
|
* @return int |
15
|
|
|
*/ |
16
|
|
|
function time() |
17
|
|
|
{ |
18
|
|
|
static $time = 1417624981; |
19
|
|
|
|
20
|
|
|
return ++$time; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
namespace eZ\Publish\Core\Repository\Tests\Permission; |
24
|
|
|
|
25
|
|
|
use eZ\Publish\API\Repository\Repository; |
26
|
|
|
use eZ\Publish\API\Repository\PermissionResolver; |
27
|
|
|
use eZ\Publish\API\Repository\PermissionCriterionResolver; |
28
|
|
|
use eZ\Publish\API\Repository\Values\ValueObject; |
29
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion; |
30
|
|
|
use eZ\Publish\API\Repository\Values\User\UserReference; |
31
|
|
|
use eZ\Publish\Core\Repository\Permission\CachedPermissionService; |
32
|
|
|
use PHPUnit\Framework\TestCase; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Mock test case for CachedPermissionService. |
36
|
|
|
*/ |
37
|
|
|
class CachedPermissionServiceTest extends TestCase |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* Test for the __construct() method. |
41
|
|
|
*/ |
42
|
|
|
public function testConstructor() |
43
|
|
|
{ |
44
|
|
|
$permissionResolverMock = $this->getPermissionResolverMock(); |
45
|
|
|
$criterionResolverMock = $this->getPermissionCriterionResolverMock(); |
46
|
|
|
$cachedService = $this->getCachedPermissionService(10); |
47
|
|
|
|
48
|
|
|
$this->assertAttributeSame( |
49
|
|
|
$permissionResolverMock, |
50
|
|
|
'permissionResolver', |
51
|
|
|
$cachedService |
52
|
|
|
); |
53
|
|
|
$this->assertAttributeSame( |
54
|
|
|
$criterionResolverMock, |
55
|
|
|
'permissionCriterionResolver', |
56
|
|
|
$cachedService |
57
|
|
|
); |
58
|
|
|
$this->assertAttributeEquals( |
59
|
|
|
10, |
60
|
|
|
'cacheTTL', |
61
|
|
|
$cachedService |
62
|
|
|
); |
63
|
|
|
$this->assertAttributeEmpty( |
64
|
|
|
'permissionCriterion', |
65
|
|
|
$cachedService |
66
|
|
|
); |
67
|
|
|
$this->assertAttributeEmpty( |
68
|
|
|
'permissionCriterionTs', |
69
|
|
|
$cachedService |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function providerForTestPermissionResolverPassTrough() |
74
|
|
|
{ |
75
|
|
|
$valueObject = $this |
76
|
|
|
->getMockBuilder(ValueObject::class) |
77
|
|
|
->disableOriginalConstructor() |
78
|
|
|
->getMockForAbstractClass(); |
79
|
|
|
|
80
|
|
|
$userRef = $this |
81
|
|
|
->getMockBuilder(UserReference::class) |
82
|
|
|
->disableOriginalConstructor() |
83
|
|
|
->getMockForAbstractClass(); |
84
|
|
|
|
85
|
|
|
$repository = $this |
86
|
|
|
->getMockBuilder(Repository::class) |
87
|
|
|
->disableOriginalConstructor() |
88
|
|
|
->getMockForAbstractClass(); |
89
|
|
|
|
90
|
|
|
return [ |
91
|
|
|
['getCurrentUserReference', [], $userRef], |
92
|
|
|
['setCurrentUserReference', [$userRef], null], |
93
|
|
|
['hasAccess', ['content', 'remove', $userRef], false], |
94
|
|
|
['canUser', ['content', 'remove', $valueObject, [new \stdClass()]], true], |
95
|
|
|
['sudo', [function () {}, $repository], null], |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Test for all PermissionResolver methods when they just pass true to underlying service. |
101
|
|
|
* |
102
|
|
|
* @dataProvider providerForTestPermissionResolverPassTrough |
103
|
|
|
* |
104
|
|
|
* @param $method |
105
|
|
|
* @param array $arguments |
106
|
|
|
* @param $return |
107
|
|
|
*/ |
108
|
|
|
public function testPermissionResolverPassTrough($method, array $arguments, $expectedReturn) |
109
|
|
|
{ |
110
|
|
|
$this->getPermissionResolverMock([$method]) |
111
|
|
|
->expects($this->once()) |
112
|
|
|
->method($method) |
113
|
|
|
->with(...$arguments) |
114
|
|
|
->willReturn($expectedReturn); |
115
|
|
|
|
116
|
|
|
$cachedService = $this->getCachedPermissionService(); |
117
|
|
|
|
118
|
|
|
$actualReturn = $cachedService->$method(...$arguments); |
119
|
|
|
$this->assertSame($expectedReturn, $actualReturn); |
120
|
|
|
|
121
|
|
|
// Make sure no cache properties where set |
122
|
|
|
$this->assertAttributeEmpty('permissionCriterion', $cachedService); |
123
|
|
|
$this->assertAttributeEmpty('permissionCriterionTs', $cachedService); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testGetPermissionsCriterionPassTrough() |
127
|
|
|
{ |
128
|
|
|
$criterionMock = $this |
129
|
|
|
->getMockBuilder(Criterion::class) |
130
|
|
|
->disableOriginalConstructor() |
131
|
|
|
->getMockForAbstractClass(); |
132
|
|
|
|
133
|
|
|
$this->getPermissionCriterionResolverMock(['getPermissionsCriterion']) |
134
|
|
|
->expects($this->once()) |
135
|
|
|
->method('getPermissionsCriterion') |
136
|
|
|
->with('content', 'remove') |
137
|
|
|
->willReturn($criterionMock); |
138
|
|
|
|
139
|
|
|
$cachedService = $this->getCachedPermissionService(); |
140
|
|
|
|
141
|
|
|
$actualReturn = $cachedService->getPermissionsCriterion('content', 'remove'); |
142
|
|
|
$this->assertSame($criterionMock, $actualReturn); |
143
|
|
|
|
144
|
|
|
// Make sure no cache properties where set |
145
|
|
|
$this->assertAttributeEmpty('permissionCriterion', $cachedService); |
146
|
|
|
$this->assertAttributeEmpty('permissionCriterionTs', $cachedService); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testGetPermissionsCriterionCaching() |
150
|
|
|
{ |
151
|
|
|
$criterionMock = $this |
152
|
|
|
->getMockBuilder(Criterion::class) |
153
|
|
|
->disableOriginalConstructor() |
154
|
|
|
->getMockForAbstractClass(); |
155
|
|
|
|
156
|
|
|
$this->getPermissionCriterionResolverMock(['getPermissionsCriterion']) |
157
|
|
|
->expects($this->exactly(2)) |
158
|
|
|
->method('getPermissionsCriterion') |
159
|
|
|
->with('content', 'read') |
160
|
|
|
->willReturn($criterionMock); |
161
|
|
|
|
162
|
|
|
$cachedService = $this->getCachedPermissionService(2); |
163
|
|
|
|
164
|
|
|
$actualReturn = $cachedService->getPermissionsCriterion('content', 'read'); |
165
|
|
|
$this->assertSame($criterionMock, $actualReturn); |
166
|
|
|
$this->assertAttributeSame($criterionMock, 'permissionCriterion', $cachedService); |
167
|
|
|
$this->assertAttributeEquals(1417624982, 'permissionCriterionTs', $cachedService); |
168
|
|
|
|
169
|
|
|
// +1 |
170
|
|
|
$actualReturn = $cachedService->getPermissionsCriterion('content', 'read'); |
171
|
|
|
$this->assertSame($criterionMock, $actualReturn); |
172
|
|
|
$this->assertAttributeSame($criterionMock, 'permissionCriterion', $cachedService); |
173
|
|
|
$this->assertAttributeEquals(1417624982, 'permissionCriterionTs', $cachedService); |
174
|
|
|
|
175
|
|
|
// +3, time() will be called twice and cache will be updated |
176
|
|
|
$actualReturn = $cachedService->getPermissionsCriterion('content', 'read'); |
177
|
|
|
$this->assertSame($criterionMock, $actualReturn); |
178
|
|
|
$this->assertAttributeSame($criterionMock, 'permissionCriterion', $cachedService); |
179
|
|
|
$this->assertAttributeEquals(1417624985, 'permissionCriterionTs', $cachedService); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function testSetCurrentUserReferenceCacheClear() |
183
|
|
|
{ |
184
|
|
|
$criterionMock = $this |
185
|
|
|
->getMockBuilder(Criterion::class) |
186
|
|
|
->disableOriginalConstructor() |
187
|
|
|
->getMockForAbstractClass(); |
188
|
|
|
|
189
|
|
|
$this->getPermissionCriterionResolverMock(['getPermissionsCriterion']) |
190
|
|
|
->expects($this->once()) |
191
|
|
|
->method('getPermissionsCriterion') |
192
|
|
|
->with('content', 'read') |
193
|
|
|
->willReturn($criterionMock); |
194
|
|
|
|
195
|
|
|
$cachedService = $this->getCachedPermissionService(2); |
196
|
|
|
|
197
|
|
|
$actualReturn = $cachedService->getPermissionsCriterion('content', 'read'); |
198
|
|
|
$this->assertSame($criterionMock, $actualReturn); |
199
|
|
|
$this->assertAttributeSame($criterionMock, 'permissionCriterion', $cachedService); |
200
|
|
|
|
201
|
|
|
$userRef = $this |
202
|
|
|
->getMockBuilder(UserReference::class) |
203
|
|
|
->getMockForAbstractClass(); |
204
|
|
|
$cachedService->setCurrentUserReference($userRef); |
205
|
|
|
$this->assertAttributeEmpty('permissionCriterion', $cachedService); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Returns the CachedPermissionService to test against. |
210
|
|
|
* |
211
|
|
|
* @param int $ttl |
212
|
|
|
* |
213
|
|
|
* @return \eZ\Publish\Core\Repository\Permission\CachedPermissionService |
214
|
|
|
*/ |
215
|
|
|
protected function getCachedPermissionService($ttl = 5) |
216
|
|
|
{ |
217
|
|
|
return new CachedPermissionService( |
218
|
|
|
$this->getPermissionResolverMock(), |
219
|
|
|
$this->getPermissionCriterionResolverMock(), |
220
|
|
|
$ttl |
221
|
|
|
); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
protected $permissionResolverMock; |
225
|
|
|
|
226
|
|
View Code Duplication |
protected function getPermissionResolverMock($methods = []) |
227
|
|
|
{ |
228
|
|
|
// Tests first calls here with methods set before initiating PermissionCriterionResolver with same instance. |
229
|
|
|
if ($this->permissionResolverMock !== null) { |
230
|
|
|
return $this->permissionResolverMock; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $this->permissionResolverMock = $this |
234
|
|
|
->getMockBuilder(PermissionResolver::class) |
235
|
|
|
->setMethods($methods) |
236
|
|
|
->disableOriginalConstructor() |
237
|
|
|
->getMockForAbstractClass(); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
protected $permissionCriterionResolverMock; |
241
|
|
|
|
242
|
|
View Code Duplication |
protected function getPermissionCriterionResolverMock($methods = []) |
243
|
|
|
{ |
244
|
|
|
// Tests first calls here with methods set before initiating PermissionCriterionResolver with same instance. |
245
|
|
|
if ($this->permissionCriterionResolverMock !== null) { |
246
|
|
|
return $this->permissionCriterionResolverMock; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return $this->permissionCriterionResolverMock = $this |
250
|
|
|
->getMockBuilder(PermissionCriterionResolver::class) |
251
|
|
|
->setMethods($methods) |
252
|
|
|
->disableOriginalConstructor() |
253
|
|
|
->getMockForAbstractClass(); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|