1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the ContentServiceAuthorizationTest 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\API\Repository\Tests; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException; |
12
|
|
|
use eZ\Publish\API\Repository\Repository; |
13
|
|
|
use eZ\Publish\API\Repository\Values\Content\ContentInfo; |
14
|
|
|
use eZ\Publish\API\Repository\Values\Content\Location; |
15
|
|
|
use eZ\Publish\API\Repository\Values\User\Limitation\LocationLimitation; |
16
|
|
|
use eZ\Publish\API\Repository\Values\User\Limitation\SubtreeLimitation; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Test case for operations in the ContentServiceAuthorization using in memory storage. |
20
|
|
|
* |
21
|
|
|
* @see eZ\Publish\API\Repository\ContentService |
22
|
|
|
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadAnonymousUser |
23
|
|
|
* @group integration |
24
|
|
|
* @group authorization |
25
|
|
|
*/ |
26
|
|
|
class ContentServiceAuthorizationTest extends BaseContentServiceTest |
27
|
|
|
{ |
28
|
|
|
/** @var \eZ\Publish\API\Repository\Values\User\User */ |
29
|
|
|
private $administratorUser; |
30
|
|
|
|
31
|
|
|
/** @var \eZ\Publish\API\Repository\Values\User\User */ |
32
|
|
|
private $anonymousUser; |
33
|
|
|
|
34
|
|
|
/** @var \eZ\Publish\API\Repository\Repository */ |
35
|
|
|
private $repository; |
|
|
|
|
36
|
|
|
|
37
|
|
|
/** @var \eZ\Publish\API\Repository\PermissionResolver */ |
38
|
|
|
private $permissionResolver; |
39
|
|
|
|
40
|
|
|
/** @var \eZ\Publish\API\Repository\UserService */ |
41
|
|
|
private $userService; |
42
|
|
|
|
43
|
|
|
/** @var \eZ\Publish\API\Repository\ContentService */ |
44
|
|
|
private $contentService; |
45
|
|
|
|
46
|
|
|
public function setUp(): void |
47
|
|
|
{ |
48
|
|
|
parent::setUp(); |
49
|
|
|
|
50
|
|
|
$anonymousUserId = $this->generateId('user', 10); |
51
|
|
|
$administratorUserId = $this->generateId('user', 14); |
52
|
|
|
|
53
|
|
|
$this->repository = $this->getRepository(); |
54
|
|
|
$this->permissionResolver = $this->repository->getPermissionResolver(); |
55
|
|
|
$this->userService = $this->repository->getUserService(); |
56
|
|
|
$this->contentService = $this->repository->getContentService(); |
57
|
|
|
|
58
|
|
|
$this->administratorUser = $this->userService->loadUser($administratorUserId); |
59
|
|
|
$this->anonymousUser = $this->userService->loadUser($anonymousUserId); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Test for the createContent() method. |
64
|
|
|
* |
65
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::createContent() |
66
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent |
67
|
|
|
*/ |
68
|
|
|
public function testCreateContentThrowsUnauthorizedException() |
69
|
|
|
{ |
70
|
|
|
if ($this->isVersion4()) { |
71
|
|
|
$this->markTestSkipped('This test requires eZ Publish 5'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
75
|
|
|
|
76
|
|
|
$contentTypeService = $this->getRepository()->getContentTypeService(); |
77
|
|
|
|
78
|
|
|
$contentType = $contentTypeService->loadContentTypeByIdentifier('forum'); |
79
|
|
|
|
80
|
|
|
$contentCreate = $this->contentService->newContentCreateStruct($contentType, 'eng-US'); |
81
|
|
|
$contentCreate->setField('name', 'Awesome Sindelfingen forum'); |
82
|
|
|
|
83
|
|
|
$contentCreate->remoteId = 'abcdef0123456789abcdef0123456789'; |
84
|
|
|
$contentCreate->alwaysAvailable = true; |
85
|
|
|
|
86
|
|
|
$this->expectException(UnauthorizedException::class); |
87
|
|
|
$this->expectExceptionMessageRegExp('/\'create\' \'content\'/'); |
88
|
|
|
|
89
|
|
|
$this->contentService->createContent($contentCreate); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Test for the createContent() method. |
94
|
|
|
* |
95
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs) |
96
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent |
97
|
|
|
*/ |
98
|
|
|
public function testCreateContentThrowsUnauthorizedExceptionWithSecondParameter() |
99
|
|
|
{ |
100
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
101
|
|
|
|
102
|
|
|
$this->expectException(UnauthorizedException::class); |
103
|
|
|
$this->expectExceptionMessageRegExp('/\'create\' \'content\'/'); |
104
|
|
|
|
105
|
|
|
$this->createContentDraftVersion1(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Test for the loadContentInfo() method. |
110
|
|
|
* |
111
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadContentInfo() |
112
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public function testLoadContentInfoThrowsUnauthorizedException() |
115
|
|
|
{ |
116
|
|
|
$contentId = $this->generateId('object', 10); |
117
|
|
|
$this->setRestrictedEditorUser(); |
118
|
|
|
|
119
|
|
|
$this->expectException(UnauthorizedException::class); |
120
|
|
|
$this->expectExceptionMessageRegExp('/\'read\' \'content\'/'); |
121
|
|
|
|
122
|
|
|
// $contentId contains a content object ID not accessible for anonymous |
123
|
|
|
$this->contentService->loadContentInfo($contentId); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Test for the sudo() method. |
128
|
|
|
* |
129
|
|
|
* @see \eZ\Publish\API\Repository\Repository::sudo() |
130
|
|
|
* @depends testLoadContentInfoThrowsUnauthorizedException |
131
|
|
|
*/ |
132
|
|
|
public function testSudo() |
133
|
|
|
{ |
134
|
|
|
$repository = $this->getRepository(); |
135
|
|
|
$contentId = $this->generateId('object', 10); |
136
|
|
|
$this->setRestrictedEditorUser(); |
137
|
|
|
|
138
|
|
|
$contentInfo = $repository->sudo(function (Repository $repository) use ($contentId) { |
139
|
|
|
return $repository->getContentService()->loadContentInfo($contentId); |
140
|
|
|
}); |
141
|
|
|
|
142
|
|
|
$this->assertInstanceOf( |
143
|
|
|
ContentInfo::class, |
144
|
|
|
$contentInfo |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Test for the loadContentInfoList() method. |
150
|
|
|
* |
151
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadContentInfoList() |
152
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoList |
153
|
|
|
*/ |
154
|
|
|
public function testLoadContentInfoListSkipsUnauthorizedItems() |
155
|
|
|
{ |
156
|
|
|
$contentId = $this->generateId('object', 10); |
157
|
|
|
$this->setRestrictedEditorUser(); |
158
|
|
|
|
159
|
|
|
$this->assertCount(0, $this->contentService->loadContentInfoList([$contentId])); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Test for the loadContentInfoByRemoteId() method. |
164
|
|
|
* |
165
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId() |
166
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId |
167
|
|
|
*/ |
168
|
|
View Code Duplication |
public function testLoadContentInfoByRemoteIdThrowsUnauthorizedException() |
169
|
|
|
{ |
170
|
|
|
$anonymousRemoteId = 'faaeb9be3bd98ed09f606fc16d144eca'; |
171
|
|
|
|
172
|
|
|
$this->setRestrictedEditorUser(); |
173
|
|
|
|
174
|
|
|
$this->expectException(UnauthorizedException::class); |
175
|
|
|
$this->expectExceptionMessageRegExp('/\'read\' \'content\'/'); |
176
|
|
|
|
177
|
|
|
$this->contentService->loadContentInfoByRemoteId($anonymousRemoteId); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Test for the loadVersionInfo() method. |
182
|
|
|
* |
183
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo() |
184
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo |
185
|
|
|
*/ |
186
|
|
|
public function testLoadVersionInfoThrowsUnauthorizedException() |
187
|
|
|
{ |
188
|
|
|
$contentInfo = $this->getContentInfoForAnonymousUser(); |
189
|
|
|
|
190
|
|
|
$this->setRestrictedEditorUser(); |
191
|
|
|
|
192
|
|
|
$this->expectException(UnauthorizedException::class); |
193
|
|
|
$this->expectExceptionMessageRegExp('/\'read\' \'content\'/'); |
194
|
|
|
|
195
|
|
|
$this->contentService->loadVersionInfo($contentInfo); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Test for the loadVersionInfo() method. |
200
|
|
|
* |
201
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo) |
202
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter |
203
|
|
|
*/ |
204
|
|
|
public function testLoadVersionInfoThrowsUnauthorizedExceptionWithSecondParameter() |
205
|
|
|
{ |
206
|
|
|
$contentInfo = $this->getContentInfoForAnonymousUser(); |
207
|
|
|
|
208
|
|
|
$this->setRestrictedEditorUser(); |
209
|
|
|
|
210
|
|
|
$this->expectException(UnauthorizedException::class); |
211
|
|
|
$this->expectExceptionMessageRegExp('/\'read\' \'content\'/'); |
212
|
|
|
|
213
|
|
|
$this->contentService->loadVersionInfo($contentInfo, 2); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Test for the loadVersionInfoById() method. |
218
|
|
|
* |
219
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById() |
220
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById |
221
|
|
|
*/ |
222
|
|
View Code Duplication |
public function testLoadVersionInfoByIdThrowsUnauthorizedException() |
223
|
|
|
{ |
224
|
|
|
$anonymousUserId = $this->generateId('user', 10); |
225
|
|
|
$this->setRestrictedEditorUser(); |
226
|
|
|
|
227
|
|
|
$this->expectException(UnauthorizedException::class); |
228
|
|
|
$this->expectExceptionMessageRegExp('/\'read\' \'content\'/'); |
229
|
|
|
|
230
|
|
|
$this->contentService->loadVersionInfoById($anonymousUserId); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Test for the loadVersionInfoById() method. |
235
|
|
|
* |
236
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo) |
237
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoByIdWithSecondParameter |
238
|
|
|
*/ |
239
|
|
View Code Duplication |
public function testLoadVersionInfoByIdThrowsUnauthorizedExceptionWithSecondParameter() |
240
|
|
|
{ |
241
|
|
|
$anonymousUserId = $this->generateId('user', 10); |
242
|
|
|
$this->setRestrictedEditorUser(); |
243
|
|
|
|
244
|
|
|
$this->expectException(UnauthorizedException::class); |
245
|
|
|
$this->expectExceptionMessageRegExp('/\'read\' \'content\'/'); |
246
|
|
|
|
247
|
|
|
$this->contentService->loadVersionInfoById($anonymousUserId, 2); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Test for the loadVersionInfoById() method. |
252
|
|
|
* |
253
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo) |
254
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById |
255
|
|
|
*/ |
256
|
|
|
public function testLoadVersionInfoByIdThrowsUnauthorizedExceptionForFirstDraft() |
257
|
|
|
{ |
258
|
|
|
$contentDraft = $this->createContentDraftVersion1(); |
259
|
|
|
|
260
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
261
|
|
|
|
262
|
|
|
$this->expectException(UnauthorizedException::class); |
263
|
|
|
// content versionread policy is needed because it is a draft |
264
|
|
|
$this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/'); |
265
|
|
|
|
266
|
|
|
$this->contentService->loadVersionInfoById( |
267
|
|
|
$contentDraft->id, |
268
|
|
|
$contentDraft->contentInfo->currentVersionNo |
269
|
|
|
); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Test for the loadContentByContentInfo() method. |
274
|
|
|
* |
275
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo() |
276
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo |
277
|
|
|
*/ |
278
|
|
|
public function testLoadContentByContentInfoThrowsUnauthorizedException() |
279
|
|
|
{ |
280
|
|
|
$contentInfo = $this->getContentInfoForAnonymousUser(); |
281
|
|
|
|
282
|
|
|
$this->setRestrictedEditorUser(); |
283
|
|
|
|
284
|
|
|
$this->expectException(UnauthorizedException::class); |
285
|
|
|
$this->expectExceptionMessageRegExp('/\'read\' \'content\'/'); |
286
|
|
|
|
287
|
|
|
$this->contentService->loadContentByContentInfo($contentInfo); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Test for the loadContentByContentInfo() method. |
292
|
|
|
* |
293
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages) |
294
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithLanguageParameters |
295
|
|
|
*/ |
296
|
|
View Code Duplication |
public function testLoadContentByContentInfoThrowsUnauthorizedExceptionWithSecondParameter() |
297
|
|
|
{ |
298
|
|
|
$contentInfo = $this->getContentInfoForAnonymousUser(); |
299
|
|
|
|
300
|
|
|
$this->setRestrictedEditorUser(); |
301
|
|
|
|
302
|
|
|
$this->expectException(UnauthorizedException::class); |
303
|
|
|
$this->expectExceptionMessageRegExp('/\'read\' \'content\'/'); |
304
|
|
|
|
305
|
|
|
$this->contentService->loadContentByContentInfo($contentInfo, ['eng-US']); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Test for the loadContentByContentInfo() method. |
310
|
|
|
* |
311
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo) |
312
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithVersionNumberParameter |
313
|
|
|
*/ |
314
|
|
View Code Duplication |
public function testLoadContentByContentInfoThrowsUnauthorizedExceptionWithThirdParameter() |
315
|
|
|
{ |
316
|
|
|
$contentInfo = $this->getContentInfoForAnonymousUser(); |
317
|
|
|
|
318
|
|
|
$this->setRestrictedEditorUser(); |
319
|
|
|
|
320
|
|
|
$this->expectException(UnauthorizedException::class); |
321
|
|
|
$this->expectExceptionMessageRegExp('/\'read\' \'content\'/'); |
322
|
|
|
|
323
|
|
|
$this->contentService->loadContentByContentInfo($contentInfo, ['eng-US'], 2); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Test for the loadContentByVersionInfo() method. |
328
|
|
|
* |
329
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo() |
330
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfo |
331
|
|
|
*/ |
332
|
|
View Code Duplication |
public function testLoadContentByVersionInfoThrowsUnauthorizedException() |
333
|
|
|
{ |
334
|
|
|
$contentInfo = $this->getContentInfoForAnonymousUser(); |
335
|
|
|
|
336
|
|
|
$versionInfo = $this->contentService->loadVersionInfo($contentInfo); |
337
|
|
|
|
338
|
|
|
$this->setRestrictedEditorUser(); |
339
|
|
|
|
340
|
|
|
$this->expectException(UnauthorizedException::class); |
341
|
|
|
$this->expectExceptionMessageRegExp('/\'read\' \'content\'/'); |
342
|
|
|
|
343
|
|
|
$this->contentService->loadContentByVersionInfo($versionInfo); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Test for the loadContentByVersionInfo() method. |
348
|
|
|
* |
349
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo($versionInfo, $languages) |
350
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfoWithSecondParameter |
351
|
|
|
*/ |
352
|
|
View Code Duplication |
public function testLoadContentByVersionInfoThrowsUnauthorizedExceptionWithSecondParameter() |
353
|
|
|
{ |
354
|
|
|
$contentInfo = $this->getContentInfoForAnonymousUser(); |
355
|
|
|
|
356
|
|
|
$versionInfo = $this->contentService->loadVersionInfo($contentInfo); |
357
|
|
|
|
358
|
|
|
$this->setRestrictedEditorUser(); |
359
|
|
|
|
360
|
|
|
$this->expectException(UnauthorizedException::class); |
361
|
|
|
$this->expectExceptionMessageRegExp('/\'read\' \'content\'/'); |
362
|
|
|
|
363
|
|
|
$this->contentService->loadContentByVersionInfo($versionInfo, ['eng-US']); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Test for the loadContent() method. |
368
|
|
|
* |
369
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadContent() |
370
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent |
371
|
|
|
*/ |
372
|
|
View Code Duplication |
public function testLoadContentThrowsUnauthorizedException() |
373
|
|
|
{ |
374
|
|
|
$anonymousUserId = $this->generateId('user', 10); |
375
|
|
|
$this->setRestrictedEditorUser(); |
376
|
|
|
|
377
|
|
|
$this->expectException(UnauthorizedException::class); |
378
|
|
|
$this->expectExceptionMessageRegExp('/\'read\' \'content\'/'); |
379
|
|
|
|
380
|
|
|
$this->contentService->loadContent($anonymousUserId); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Test for the loadContent() method. |
385
|
|
|
* |
386
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages) |
387
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithSecondParameter |
388
|
|
|
*/ |
389
|
|
View Code Duplication |
public function testLoadContentThrowsUnauthorizedExceptionWithSecondParameter() |
390
|
|
|
{ |
391
|
|
|
$anonymousUserId = $this->generateId('user', 10); |
392
|
|
|
$this->setRestrictedEditorUser(); |
393
|
|
|
|
394
|
|
|
$this->expectException(UnauthorizedException::class); |
395
|
|
|
$this->expectExceptionMessageRegExp('/\'read\' \'content\'/'); |
396
|
|
|
|
397
|
|
|
$this->contentService->loadContent($anonymousUserId, ['eng-US']); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Test for the loadContent() method. |
402
|
|
|
* |
403
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo) |
404
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithThirdParameter |
405
|
|
|
*/ |
406
|
|
View Code Duplication |
public function testLoadContentThrowsUnauthorizedExceptionWithThirdParameter() |
407
|
|
|
{ |
408
|
|
|
$anonymousUserId = $this->generateId('user', 10); |
409
|
|
|
$this->setRestrictedEditorUser(); |
410
|
|
|
|
411
|
|
|
$this->expectException(UnauthorizedException::class); |
412
|
|
|
$this->expectExceptionMessageRegExp('/\'read\' \'content\'/'); |
413
|
|
|
|
414
|
|
|
$this->contentService->loadContent($anonymousUserId, ['eng-US'], 2); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Test for the loadContent() method on a draft. |
419
|
|
|
* |
420
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadContent() |
421
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent |
422
|
|
|
*/ |
423
|
|
|
public function testLoadContentThrowsUnauthorizedExceptionOnDrafts() |
424
|
|
|
{ |
425
|
|
|
$editorUser = $this->createUserVersion1(); |
426
|
|
|
|
427
|
|
|
$this->permissionResolver->setCurrentUserReference($editorUser); |
428
|
|
|
|
429
|
|
|
// Create draft with this user |
430
|
|
|
$draft = $this->createContentDraftVersion1(2, 'folder'); |
431
|
|
|
|
432
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
433
|
|
|
|
434
|
|
|
// Try to load the draft with anonymous user to make sure access won't be allowed by throwing an exception |
435
|
|
|
$this->expectException(UnauthorizedException::class); |
436
|
|
|
// content versionread policy is needed because it is a draft |
437
|
|
|
$this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/'); |
438
|
|
|
|
439
|
|
|
$this->contentService->loadContent($draft->id); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Test for the ContentService::loadContent() method on an archive. |
444
|
|
|
* |
445
|
|
|
* This test the version permission on loading archived versions |
446
|
|
|
* |
447
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadContent() |
448
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent |
449
|
|
|
*/ |
450
|
|
|
public function testLoadContentThrowsUnauthorizedExceptionsOnArchives() |
451
|
|
|
{ |
452
|
|
|
$contentTypeService = $this->getRepository()->getContentTypeService(); |
453
|
|
|
|
454
|
|
|
// set admin as current user |
455
|
|
|
$this->permissionResolver->setCurrentUserReference($this->administratorUser); |
456
|
|
|
|
457
|
|
|
// create folder |
458
|
|
|
$newStruct = $this->contentService->newContentCreateStruct( |
459
|
|
|
$contentTypeService->loadContentTypeByIdentifier('folder'), |
460
|
|
|
'eng-US' |
461
|
|
|
); |
462
|
|
|
$newStruct->setField('name', 'Test Folder'); |
463
|
|
|
$draft = $this->contentService->createContent( |
464
|
|
|
$newStruct, |
465
|
|
|
[$this->repository->getLocationService()->newLocationCreateStruct(2)] |
466
|
|
|
); |
467
|
|
|
$object = $this->contentService->publishVersion($draft->versionInfo); |
468
|
|
|
|
469
|
|
|
// update folder to make an archived version |
470
|
|
|
$updateStruct = $this->contentService->newContentUpdateStruct(); |
471
|
|
|
$updateStruct->setField('name', 'Test Folder Updated'); |
472
|
|
|
$draftUpdated = $this->contentService->updateContent( |
473
|
|
|
$this->contentService->createContentDraft($object->contentInfo)->versionInfo, |
474
|
|
|
$updateStruct |
475
|
|
|
); |
476
|
|
|
$objectUpdated = $this->contentService->publishVersion($draftUpdated->versionInfo); |
477
|
|
|
|
478
|
|
|
// set an anonymous as current user |
479
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
480
|
|
|
|
481
|
|
|
$this->expectException(UnauthorizedException::class); |
482
|
|
|
// content versionread policy is needed because it is a draft |
483
|
|
|
$this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/'); |
484
|
|
|
|
485
|
|
|
$this->contentService->loadContent($objectUpdated->id, null, 1); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* Test for the loadContentByRemoteId() method. |
490
|
|
|
* |
491
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId() |
492
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId |
493
|
|
|
*/ |
494
|
|
View Code Duplication |
public function testLoadContentByRemoteIdThrowsUnauthorizedException() |
495
|
|
|
{ |
496
|
|
|
$anonymousRemoteId = 'faaeb9be3bd98ed09f606fc16d144eca'; |
497
|
|
|
|
498
|
|
|
$this->setRestrictedEditorUser(); |
499
|
|
|
|
500
|
|
|
$this->expectException(UnauthorizedException::class); |
501
|
|
|
$this->expectExceptionMessageRegExp('/\'read\' \'content\'/'); |
502
|
|
|
|
503
|
|
|
$this->contentService->loadContentByRemoteId($anonymousRemoteId); |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Test for the loadContentByRemoteId() method. |
508
|
|
|
* |
509
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages) |
510
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteIdWithSecondParameter |
511
|
|
|
*/ |
512
|
|
View Code Duplication |
public function testLoadContentByRemoteIdThrowsUnauthorizedExceptionWithSecondParameter() |
513
|
|
|
{ |
514
|
|
|
$anonymousRemoteId = 'faaeb9be3bd98ed09f606fc16d144eca'; |
515
|
|
|
|
516
|
|
|
$this->setRestrictedEditorUser(); |
517
|
|
|
|
518
|
|
|
$this->expectException(UnauthorizedException::class); |
519
|
|
|
$this->expectExceptionMessageRegExp('/\'read\' \'content\'/'); |
520
|
|
|
|
521
|
|
|
$this->contentService->loadContentByRemoteId($anonymousRemoteId, ['eng-US']); |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
/** |
525
|
|
|
* Test for the loadContentByRemoteId() method. |
526
|
|
|
* |
527
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo) |
528
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteIdWithThirdParameter |
529
|
|
|
*/ |
530
|
|
View Code Duplication |
public function testLoadContentByRemoteIdThrowsUnauthorizedExceptionWithThirdParameter() |
531
|
|
|
{ |
532
|
|
|
$anonymousRemoteId = 'faaeb9be3bd98ed09f606fc16d144eca'; |
533
|
|
|
|
534
|
|
|
$this->setRestrictedEditorUser(); |
535
|
|
|
|
536
|
|
|
$this->expectException(UnauthorizedException::class); |
537
|
|
|
$this->expectExceptionMessageRegExp('/\'read\' \'content\'/'); |
538
|
|
|
|
539
|
|
|
$this->contentService->loadContentByRemoteId($anonymousRemoteId, ['eng-US'], 2); |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
/** |
543
|
|
|
* Test for the updateContentMetadata() method. |
544
|
|
|
* |
545
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata() |
546
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata |
547
|
|
|
*/ |
548
|
|
|
public function testUpdateContentMetadataThrowsUnauthorizedException() |
549
|
|
|
{ |
550
|
|
|
$content = $this->createContentVersion1(); |
551
|
|
|
|
552
|
|
|
$contentInfo = $content->contentInfo; |
553
|
|
|
|
554
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
555
|
|
|
|
556
|
|
|
$metadataUpdate = $this->contentService->newContentMetadataUpdateStruct(); |
557
|
|
|
|
558
|
|
|
$metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222'; |
559
|
|
|
$metadataUpdate->mainLanguageCode = 'eng-US'; |
560
|
|
|
$metadataUpdate->alwaysAvailable = false; |
561
|
|
|
$metadataUpdate->publishedDate = $this->createDateTime(); |
562
|
|
|
$metadataUpdate->modificationDate = $this->createDateTime(); |
563
|
|
|
|
564
|
|
|
$this->expectException(UnauthorizedException::class); |
565
|
|
|
$this->expectExceptionMessageRegExp('/\'edit\' \'content\'/'); |
566
|
|
|
|
567
|
|
|
$this->contentService->updateContentMetadata( |
568
|
|
|
$contentInfo, |
569
|
|
|
$metadataUpdate |
570
|
|
|
); |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
/** |
574
|
|
|
* Test for the deleteContent() method. |
575
|
|
|
* |
576
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::deleteContent() |
577
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent |
578
|
|
|
*/ |
579
|
|
View Code Duplication |
public function testDeleteContentThrowsUnauthorizedException() |
580
|
|
|
{ |
581
|
|
|
$contentVersion2 = $this->createContentVersion2(); |
582
|
|
|
|
583
|
|
|
$contentInfo = $contentVersion2->contentInfo; |
584
|
|
|
|
585
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
586
|
|
|
|
587
|
|
|
$this->expectException(UnauthorizedException::class); |
588
|
|
|
$this->expectExceptionMessageRegExp('/\'remove\' \'content\'/'); |
589
|
|
|
|
590
|
|
|
$this->contentService->deleteContent($contentInfo); |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
/** |
594
|
|
|
* Test for the createContentDraft() method. |
595
|
|
|
* |
596
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::createContentDraft() |
597
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft |
598
|
|
|
*/ |
599
|
|
View Code Duplication |
public function testCreateContentDraftThrowsUnauthorizedException() |
600
|
|
|
{ |
601
|
|
|
$content = $this->createContentVersion1(); |
602
|
|
|
|
603
|
|
|
$contentInfo = $content->contentInfo; |
604
|
|
|
|
605
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
606
|
|
|
|
607
|
|
|
$this->expectException(UnauthorizedException::class); |
608
|
|
|
$this->expectExceptionMessageRegExp('/\'edit\' \'content\'/'); |
609
|
|
|
|
610
|
|
|
$this->contentService->createContentDraft($contentInfo); |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
/** |
614
|
|
|
* Test for the createContentDraft() method. |
615
|
|
|
* |
616
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::createContentDraft($contentInfo, $versionInfo) |
617
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithSecondParameter |
618
|
|
|
*/ |
619
|
|
|
public function testCreateContentDraftThrowsUnauthorizedExceptionWithSecondParameter() |
620
|
|
|
{ |
621
|
|
|
$content = $this->createContentVersion1(); |
622
|
|
|
|
623
|
|
|
$contentInfo = $content->contentInfo; |
624
|
|
|
$versionInfo = $content->getVersionInfo(); |
625
|
|
|
|
626
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
627
|
|
|
|
628
|
|
|
$this->expectException(UnauthorizedException::class); |
629
|
|
|
$this->expectExceptionMessageRegExp('/\'edit\' \'content\'/'); |
630
|
|
|
|
631
|
|
|
$this->contentService->createContentDraft($contentInfo, $versionInfo); |
632
|
|
|
} |
633
|
|
|
|
634
|
|
|
/** |
635
|
|
|
* Test for the loadContentDrafts() method. |
636
|
|
|
* |
637
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts() |
638
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts |
639
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts |
640
|
|
|
*/ |
641
|
|
|
public function testLoadContentDraftsThrowsUnauthorizedException() |
642
|
|
|
{ |
643
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
644
|
|
|
|
645
|
|
|
$this->expectException(UnauthorizedException::class); |
646
|
|
|
$this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/'); |
647
|
|
|
|
648
|
|
|
$this->contentService->loadContentDrafts(); |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
/** |
652
|
|
|
* Test for the loadContentDrafts() method. |
653
|
|
|
* |
654
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts($user) |
655
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts |
656
|
|
|
*/ |
657
|
|
|
public function testLoadContentDraftsThrowsUnauthorizedExceptionWithUser() |
658
|
|
|
{ |
659
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
660
|
|
|
|
661
|
|
|
$this->expectException(UnauthorizedException::class); |
662
|
|
|
$this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/'); |
663
|
|
|
|
664
|
|
|
$this->contentService->loadContentDrafts($this->administratorUser); |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
/** |
668
|
|
|
* Test for the updateContent() method. |
669
|
|
|
* |
670
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::updateContent() |
671
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent |
672
|
|
|
*/ |
673
|
|
|
public function testUpdateContentThrowsUnauthorizedException() |
674
|
|
|
{ |
675
|
|
|
$draftVersion2 = $this->createContentDraftVersion2(); |
676
|
|
|
|
677
|
|
|
$versionInfo = $draftVersion2->getVersionInfo(); |
678
|
|
|
|
679
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
680
|
|
|
|
681
|
|
|
// Create an update struct and modify some fields |
682
|
|
|
$contentUpdate = $this->contentService->newContentUpdateStruct(); |
683
|
|
|
$contentUpdate->setField('name', 'An awesome² story about ezp.'); |
684
|
|
|
$contentUpdate->setField('name', 'An awesome²³ story about ezp.', 'eng-GB'); |
685
|
|
|
|
686
|
|
|
$contentUpdate->initialLanguageCode = 'eng-US'; |
687
|
|
|
|
688
|
|
|
$this->expectException(UnauthorizedException::class); |
689
|
|
|
/* TODO - the `content/edit` policy should be probably needed */ |
690
|
|
|
$this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/'); |
691
|
|
|
|
692
|
|
|
$this->contentService->updateContent($versionInfo, $contentUpdate); |
693
|
|
|
} |
694
|
|
|
|
695
|
|
|
/** |
696
|
|
|
* Test for the publishVersion() method. |
697
|
|
|
* |
698
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::publishVersion() |
699
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion |
700
|
|
|
*/ |
701
|
|
View Code Duplication |
public function testPublishVersionThrowsUnauthorizedException() |
702
|
|
|
{ |
703
|
|
|
$draft = $this->createContentDraftVersion1(); |
704
|
|
|
|
705
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
706
|
|
|
|
707
|
|
|
$this->expectException(UnauthorizedException::class); |
708
|
|
|
$this->expectExceptionMessageRegExp('/\'publish\' \'content\'/'); |
709
|
|
|
|
710
|
|
|
$this->contentService->publishVersion($draft->getVersionInfo()); |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
/** |
714
|
|
|
* Test for the deleteVersion() method. |
715
|
|
|
* |
716
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::deleteVersion() |
717
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteVersion |
718
|
|
|
*/ |
719
|
|
View Code Duplication |
public function testDeleteVersionThrowsUnauthorizedException() |
720
|
|
|
{ |
721
|
|
|
$draft = $this->createContentDraftVersion1(); |
722
|
|
|
|
723
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
724
|
|
|
|
725
|
|
|
$this->expectException(UnauthorizedException::class); |
726
|
|
|
$this->expectExceptionMessageRegExp('/\'versionremove\' \'content\'/'); |
727
|
|
|
|
728
|
|
|
$this->contentService->deleteVersion($draft->getVersionInfo()); |
729
|
|
|
} |
730
|
|
|
|
731
|
|
|
/** |
732
|
|
|
* Test for the loadVersions() method. |
733
|
|
|
* |
734
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadVersions() |
735
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersions |
736
|
|
|
*/ |
737
|
|
View Code Duplication |
public function testLoadVersionsThrowsUnauthorizedException() |
738
|
|
|
{ |
739
|
|
|
$contentVersion2 = $this->createContentVersion2(); |
740
|
|
|
|
741
|
|
|
$contentInfo = $contentVersion2->contentInfo; |
742
|
|
|
|
743
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
744
|
|
|
|
745
|
|
|
$this->expectException(UnauthorizedException::class); |
746
|
|
|
$this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/'); |
747
|
|
|
|
748
|
|
|
$this->contentService->loadVersions($contentInfo); |
749
|
|
|
} |
750
|
|
|
|
751
|
|
|
/** |
752
|
|
|
* Test for the copyContent() method. |
753
|
|
|
* |
754
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::copyContent() |
755
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent |
756
|
|
|
*/ |
757
|
|
|
public function testCopyContentThrowsUnauthorizedException() |
758
|
|
|
{ |
759
|
|
|
$parentLocationId = $this->generateId('location', 52); |
760
|
|
|
|
761
|
|
|
$locationService = $this->repository->getLocationService(); |
762
|
|
|
|
763
|
|
|
$contentVersion2 = $this->createMultipleLanguageContentVersion2(); |
764
|
|
|
|
765
|
|
|
$contentInfo = $contentVersion2->contentInfo; |
766
|
|
|
|
767
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
768
|
|
|
|
769
|
|
|
// Configure new target location |
770
|
|
|
$targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId); |
771
|
|
|
|
772
|
|
|
$targetLocationCreate->priority = 42; |
773
|
|
|
$targetLocationCreate->hidden = true; |
774
|
|
|
$targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789'; |
775
|
|
|
$targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID; |
776
|
|
|
$targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC; |
777
|
|
|
|
778
|
|
|
$this->expectException(UnauthorizedException::class); |
779
|
|
|
$this->expectExceptionMessageRegExp('/\'read\' \'content\'/'); |
780
|
|
|
|
781
|
|
|
$this->contentService->copyContent( |
782
|
|
|
$contentInfo, |
783
|
|
|
$targetLocationCreate |
784
|
|
|
); |
785
|
|
|
} |
786
|
|
|
|
787
|
|
|
/** |
788
|
|
|
* Test for the copyContent() method. |
789
|
|
|
* |
790
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo) |
791
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContentWithGivenVersion |
792
|
|
|
*/ |
793
|
|
|
public function testCopyContentThrowsUnauthorizedExceptionWithGivenVersion() |
794
|
|
|
{ |
795
|
|
|
$parentLocationId = $this->generateId('location', 52); |
796
|
|
|
|
797
|
|
|
$contentVersion2 = $this->createContentVersion2(); |
798
|
|
|
|
799
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
800
|
|
|
|
801
|
|
|
// Configure new target location |
802
|
|
|
$targetLocationCreate = $this->repository->getLocationService()->newLocationCreateStruct($parentLocationId); |
803
|
|
|
|
804
|
|
|
$targetLocationCreate->priority = 42; |
805
|
|
|
$targetLocationCreate->hidden = true; |
806
|
|
|
$targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789'; |
807
|
|
|
$targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID; |
808
|
|
|
$targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC; |
809
|
|
|
|
810
|
|
|
$this->expectException(UnauthorizedException::class); |
811
|
|
|
$this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/'); |
812
|
|
|
|
813
|
|
|
$this->contentService->copyContent( |
814
|
|
|
$contentVersion2->contentInfo, |
815
|
|
|
$targetLocationCreate, |
816
|
|
|
$this->contentService->loadVersionInfo($contentVersion2->contentInfo, 1) |
817
|
|
|
); |
818
|
|
|
} |
819
|
|
|
|
820
|
|
|
/** |
821
|
|
|
* Test for the loadRelations() method. |
822
|
|
|
* |
823
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadRelations() |
824
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations |
825
|
|
|
*/ |
826
|
|
View Code Duplication |
public function testLoadRelationsThrowsUnauthorizedException() |
827
|
|
|
{ |
828
|
|
|
$mediaEditor = $this->createMediaUserVersion1(); |
829
|
|
|
|
830
|
|
|
$setupRemoteId = '241d538ce310074e602f29f49e44e938'; |
831
|
|
|
|
832
|
|
|
$versionInfo = $this->contentService->loadVersionInfo( |
833
|
|
|
$this->contentService->loadContentInfoByRemoteId( |
834
|
|
|
$setupRemoteId |
835
|
|
|
) |
836
|
|
|
); |
837
|
|
|
|
838
|
|
|
$this->permissionResolver->setCurrentUserReference($mediaEditor); |
839
|
|
|
|
840
|
|
|
$this->expectException(UnauthorizedException::class); |
841
|
|
|
$this->expectExceptionMessageRegExp('/\'read\' \'content\'/'); |
842
|
|
|
|
843
|
|
|
$this->contentService->loadRelations($versionInfo); |
844
|
|
|
} |
845
|
|
|
|
846
|
|
|
/** |
847
|
|
|
* Test for the loadRelations() method. |
848
|
|
|
* |
849
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadRelations() |
850
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations |
851
|
|
|
*/ |
852
|
|
View Code Duplication |
public function testLoadRelationsForDraftVersionThrowsUnauthorizedException() |
853
|
|
|
{ |
854
|
|
|
$draft = $this->createContentDraftVersion1(); |
855
|
|
|
|
856
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
857
|
|
|
|
858
|
|
|
$this->expectException(UnauthorizedException::class); |
859
|
|
|
$this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/'); |
860
|
|
|
|
861
|
|
|
$this->contentService->loadRelations($draft->versionInfo); |
862
|
|
|
} |
863
|
|
|
|
864
|
|
|
/** |
865
|
|
|
* Test for the loadReverseRelations() method. |
866
|
|
|
* |
867
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations() |
868
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations |
869
|
|
|
*/ |
870
|
|
|
public function testLoadReverseRelationsThrowsUnauthorizedException() |
871
|
|
|
{ |
872
|
|
|
$mediaEditor = $this->createMediaUserVersion1(); |
873
|
|
|
|
874
|
|
|
$mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262'; |
875
|
|
|
|
876
|
|
|
$contentInfo = $this->contentService->loadContentInfoByRemoteId($mediaRemoteId); |
877
|
|
|
|
878
|
|
|
$this->permissionResolver->setCurrentUserReference($mediaEditor); |
879
|
|
|
|
880
|
|
|
$this->expectException(UnauthorizedException::class); |
881
|
|
|
$this->expectExceptionMessageRegExp('/\'reverserelatedlist\' \'content\'/'); |
882
|
|
|
|
883
|
|
|
$this->contentService->loadReverseRelations($contentInfo); |
884
|
|
|
} |
885
|
|
|
|
886
|
|
|
/** |
887
|
|
|
* Test for the addRelation() method. |
888
|
|
|
* |
889
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::addRelation() |
890
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation |
891
|
|
|
*/ |
892
|
|
View Code Duplication |
public function testAddRelationThrowsUnauthorizedException() |
893
|
|
|
{ |
894
|
|
|
$mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262'; |
895
|
|
|
|
896
|
|
|
$draft = $this->createContentDraftVersion1(); |
897
|
|
|
|
898
|
|
|
$versionInfo = $draft->getVersionInfo(); |
899
|
|
|
|
900
|
|
|
$media = $this->contentService->loadContentInfoByRemoteId($mediaRemoteId); |
901
|
|
|
|
902
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
903
|
|
|
|
904
|
|
|
$this->expectException(UnauthorizedException::class); |
905
|
|
|
$this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/'); |
906
|
|
|
|
907
|
|
|
$this->contentService->addRelation( |
908
|
|
|
$versionInfo, |
909
|
|
|
$media |
910
|
|
|
); |
911
|
|
|
} |
912
|
|
|
|
913
|
|
|
/** |
914
|
|
|
* Test for the deleteRelation() method. |
915
|
|
|
* |
916
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::deleteRelation() |
917
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation |
918
|
|
|
*/ |
919
|
|
|
public function testDeleteRelationThrowsUnauthorizedException() |
920
|
|
|
{ |
921
|
|
|
$mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262'; |
922
|
|
|
$demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f'; |
923
|
|
|
|
924
|
|
|
$draft = $this->createContentDraftVersion1(); |
925
|
|
|
|
926
|
|
|
$versionInfo = $draft->getVersionInfo(); |
927
|
|
|
|
928
|
|
|
$media = $this->contentService->loadContentInfoByRemoteId($mediaRemoteId); |
929
|
|
|
$demoDesign = $this->contentService->loadContentInfoByRemoteId($demoDesignRemoteId); |
930
|
|
|
|
931
|
|
|
// Establish some relations |
932
|
|
|
$this->contentService->addRelation($draft->getVersionInfo(), $media); |
933
|
|
|
$this->contentService->addRelation($draft->getVersionInfo(), $demoDesign); |
934
|
|
|
|
935
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
936
|
|
|
|
937
|
|
|
$this->expectException(UnauthorizedException::class); |
938
|
|
|
$this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/'); |
939
|
|
|
|
940
|
|
|
$this->contentService->deleteRelation($versionInfo, $media); |
941
|
|
|
} |
942
|
|
|
|
943
|
|
|
/** |
944
|
|
|
* Creates a pseudo editor with a limitation to objects in the "Media/Images" |
945
|
|
|
* subtree. |
946
|
|
|
* |
947
|
|
|
* @return \eZ\Publish\API\Repository\Values\User\User |
948
|
|
|
*/ |
949
|
|
|
private function createAnonymousWithEditorRole() |
950
|
|
|
{ |
951
|
|
|
$roleService = $this->repository->getRoleService(); |
952
|
|
|
|
953
|
|
|
$user = $this->anonymousUser; |
954
|
|
|
$role = $roleService->loadRoleByIdentifier('Editor'); |
955
|
|
|
|
956
|
|
|
// Assign "Editor" role with limitation to "Media/Images" |
957
|
|
|
$roleService->assignRoleToUser( |
958
|
|
|
$role, |
959
|
|
|
$user, |
960
|
|
|
new \eZ\Publish\API\Repository\Values\User\Limitation\SubtreeLimitation( |
961
|
|
|
[ |
962
|
|
|
'limitationValues' => ['/1/43/51/'], |
963
|
|
|
] |
964
|
|
|
) |
965
|
|
|
); |
966
|
|
|
|
967
|
|
|
return $this->userService->loadUser($user->id); |
968
|
|
|
} |
969
|
|
|
|
970
|
|
|
/** |
971
|
|
|
* Test that for an user that doesn't have access (read permissions) to an |
972
|
|
|
* related object, executing loadRelations() would not throw any exception, |
973
|
|
|
* only that the non-readable related object(s) won't be loaded. |
974
|
|
|
* |
975
|
|
|
* @see \eZ\Publish\API\Repository\ContentService::loadRelations() |
976
|
|
|
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation |
977
|
|
|
*/ |
978
|
|
|
public function testLoadRelationsWithUnauthorizedRelations() |
979
|
|
|
{ |
980
|
|
|
$mainLanguage = 'eng-GB'; |
981
|
|
|
|
982
|
|
|
$contentTypeService = $this->repository->getContentTypeService(); |
983
|
|
|
$locationService = $this->repository->getLocationService(); |
984
|
|
|
$sectionService = $this->repository->getSectionService(); |
985
|
|
|
|
986
|
|
|
// set the current user as admin to create the environment to test |
987
|
|
|
$this->permissionResolver->setCurrentUserReference($this->administratorUser); |
988
|
|
|
|
989
|
|
|
// create section |
990
|
|
|
// since anonymous users have their read permissions to specific sections |
991
|
|
|
// the created section will be non-readable to them |
992
|
|
|
$sectionCreate = $sectionService->newSectionCreateStruct(); |
993
|
|
|
$sectionCreate->identifier = 'private'; |
994
|
|
|
$sectionCreate->name = 'Private Section'; |
995
|
|
|
$section = $sectionService->createSection($sectionCreate); |
996
|
|
|
|
997
|
|
|
// create objects for testing |
998
|
|
|
// here we will create 4 objects which 2 will be readable by an anonymous |
999
|
|
|
// user, and the other 2 wont these last 2 will go to a private section |
1000
|
|
|
// where anonymous can't read, just like: |
1001
|
|
|
// readable object 1 -> /Main Folder |
1002
|
|
|
// readable object 2 -> /Main Folder/Available Folder |
1003
|
|
|
// non-readable object 1 -> /Restricted Folder |
1004
|
|
|
// non-readable object 2 -> /Restricted Folder/Unavailable Folder |
1005
|
|
|
// |
1006
|
|
|
// here is created - readable object 1 -> /Main Folder |
1007
|
|
|
$mainFolder = $this->createFolder([$mainLanguage => 'Main Folder'], 2); |
1008
|
|
|
|
1009
|
|
|
// here is created readable object 2 -> /Main Folder/Available Folder |
1010
|
|
|
$availableFolder = $this->createFolder( |
1011
|
|
|
[$mainLanguage => 'Avaliable Folder'], |
1012
|
|
|
$mainFolder->contentInfo->mainLocationId |
1013
|
|
|
); |
1014
|
|
|
|
1015
|
|
|
// here is created the non-readable object 1 -> /Restricted Folder |
1016
|
|
|
$restrictedFolderCreate = $this->contentService->newContentCreateStruct( |
1017
|
|
|
$contentTypeService->loadContentTypeByIdentifier('folder'), |
1018
|
|
|
$mainLanguage |
1019
|
|
|
); |
1020
|
|
|
$restrictedFolderCreate->setField('name', 'Restricted Folder'); |
1021
|
|
|
$restrictedFolderCreate->sectionId = $section->id; |
1022
|
|
|
$restrictedFolder = $this->contentService->publishVersion( |
1023
|
|
|
$this->contentService->createContent( |
1024
|
|
|
$restrictedFolderCreate, |
1025
|
|
|
[$locationService->newLocationCreateStruct(2)] |
1026
|
|
|
)->versionInfo |
1027
|
|
|
); |
1028
|
|
|
|
1029
|
|
|
// here is created non-readable object 2 -> /Restricted Folder/Unavailable Folder |
1030
|
|
|
$unavailableFolder = $this->createFolder( |
1031
|
|
|
[$mainLanguage => 'Unavailable Folder'], |
1032
|
|
|
$restrictedFolder->contentInfo->mainLocationId |
1033
|
|
|
); |
1034
|
|
|
|
1035
|
|
|
// this will be our test object, which will have all the relations (as source) |
1036
|
|
|
// and it is readable by the anonymous user |
1037
|
|
|
$testFolderCreate = $this->contentService->newContentCreateStruct( |
1038
|
|
|
$contentTypeService->loadContentTypeByIdentifier('folder'), |
1039
|
|
|
$mainLanguage |
1040
|
|
|
); |
1041
|
|
|
$testFolderCreate->setField('name', 'Test Folder'); |
1042
|
|
|
$testFolderDraft = $this->contentService->createContent( |
1043
|
|
|
$testFolderCreate, |
1044
|
|
|
[$locationService->newLocationCreateStruct(2)] |
1045
|
|
|
)->versionInfo; |
1046
|
|
|
|
1047
|
|
|
// add relations to test folder (as source) |
1048
|
|
|
// the first 2 will be read by the user |
1049
|
|
|
// and the other 2 wont |
1050
|
|
|
// |
1051
|
|
|
// create relation from Test Folder to Main Folder |
1052
|
|
|
$mainRelation = $this->contentService->addRelation( |
1053
|
|
|
$testFolderDraft, |
1054
|
|
|
$mainFolder->getVersionInfo()->getContentInfo() |
1055
|
|
|
); |
1056
|
|
|
// create relation from Test Folder to Available Folder |
1057
|
|
|
$availableRelation = $this->contentService->addRelation( |
1058
|
|
|
$testFolderDraft, |
1059
|
|
|
$availableFolder->getVersionInfo()->getContentInfo() |
1060
|
|
|
); |
1061
|
|
|
// create relation from Test Folder to Restricted Folder |
1062
|
|
|
$this->contentService->addRelation( |
1063
|
|
|
$testFolderDraft, |
1064
|
|
|
$restrictedFolder->getVersionInfo()->getContentInfo() |
1065
|
|
|
); |
1066
|
|
|
//create relation from Test Folder to Unavailable Folder |
1067
|
|
|
$this->contentService->addRelation( |
1068
|
|
|
$testFolderDraft, |
1069
|
|
|
$unavailableFolder->getVersionInfo()->getContentInfo() |
1070
|
|
|
); |
1071
|
|
|
|
1072
|
|
|
// publish Test Folder |
1073
|
|
|
$testFolder = $this->contentService->publishVersion($testFolderDraft); |
1074
|
|
|
|
1075
|
|
|
// set the current user to be an anonymous user since we want to test that |
1076
|
|
|
// if the user doesn't have access to an related object that object wont |
1077
|
|
|
// be loaded and no exception will be thrown |
1078
|
|
|
$this->permissionResolver->setCurrentUserReference($this->anonymousUser); |
1079
|
|
|
|
1080
|
|
|
// finaly load relations ( verify no exception is thrown ) |
1081
|
|
|
$actualRelations = $this->contentService->loadRelations($testFolder->getVersionInfo()); |
1082
|
|
|
|
1083
|
|
|
// assert results |
1084
|
|
|
// verify that the only expected relations are from the 2 readable objects |
1085
|
|
|
// Main Folder and Available Folder |
1086
|
|
|
$expectedRelations = [ |
1087
|
|
|
$mainRelation->destinationContentInfo->id => $mainRelation, |
1088
|
|
|
$availableRelation->destinationContentInfo->id => $availableRelation, |
1089
|
|
|
]; |
1090
|
|
|
|
1091
|
|
|
// assert there are as many expected relations as actual ones |
1092
|
|
|
$this->assertEquals( |
1093
|
|
|
count($expectedRelations), |
1094
|
|
|
count($actualRelations), |
1095
|
|
|
"Expected '" . count($expectedRelations) |
1096
|
|
|
. "' relations found '" . count($actualRelations) . "'" |
1097
|
|
|
); |
1098
|
|
|
|
1099
|
|
|
// assert each relation |
1100
|
|
|
foreach ($actualRelations as $relation) { |
1101
|
|
|
$destination = $relation->destinationContentInfo; |
1102
|
|
|
$expected = $expectedRelations[$destination->id]->destinationContentInfo; |
1103
|
|
|
$this->assertNotEmpty($expected, "Non expected relation with '{$destination->id}' id found"); |
1104
|
|
|
$this->assertEquals( |
1105
|
|
|
$expected->id, |
1106
|
|
|
$destination->id, |
1107
|
|
|
"Expected relation with '{$expected->id}' id found '{$destination->id}' id" |
1108
|
|
|
); |
1109
|
|
|
$this->assertEquals( |
1110
|
|
|
$expected->name, |
1111
|
|
|
$destination->name, |
1112
|
|
|
"Expected relation with '{$expected->name}' name found '{$destination->name}' name" |
1113
|
|
|
); |
1114
|
|
|
|
1115
|
|
|
// remove from list |
1116
|
|
|
unset($expectedRelations[$destination->id]); |
1117
|
|
|
} |
1118
|
|
|
|
1119
|
|
|
// verify all expected relations were found |
1120
|
|
|
$this->assertEquals( |
1121
|
|
|
0, |
1122
|
|
|
count($expectedRelations), |
1123
|
|
|
"Expected to find '" . (count($expectedRelations) + count($actualRelations)) |
1124
|
|
|
. "' relations found '" . count($actualRelations) . "'" |
1125
|
|
|
); |
1126
|
|
|
} |
1127
|
|
|
|
1128
|
|
|
/** |
1129
|
|
|
* Test copying Content to the authorized Location (limited by policies). |
1130
|
|
|
*/ |
1131
|
|
|
public function testCopyContentToAuthorizedLocation() |
1132
|
|
|
{ |
1133
|
|
|
$locationService = $this->repository->getLocationService(); |
1134
|
|
|
$roleService = $this->repository->getRoleService(); |
1135
|
|
|
|
1136
|
|
|
// Create and publish folders for the test case |
1137
|
|
|
$folderDraft = $this->createContentDraft('folder', 2, ['name' => 'Folder1']); |
1138
|
|
|
$this->contentService->publishVersion($folderDraft->versionInfo); |
1139
|
|
|
$authorizedFolderDraft = $this->createContentDraft('folder', 2, ['name' => 'AuthorizedFolder']); |
1140
|
|
|
$authorizedFolder = $this->contentService->publishVersion($authorizedFolderDraft->versionInfo); |
1141
|
|
|
|
1142
|
|
|
// Prepare Role for the test case |
1143
|
|
|
$roleIdentifier = 'authorized_folder'; |
1144
|
|
|
$roleCreateStruct = $roleService->newRoleCreateStruct($roleIdentifier); |
1145
|
|
|
$locationLimitation = new LocationLimitation( |
1146
|
|
|
['limitationValues' => [$authorizedFolder->contentInfo->mainLocationId]] |
1147
|
|
|
); |
1148
|
|
|
$roleCreateStruct->addPolicy($roleService->newPolicyCreateStruct('content', 'read')); |
1149
|
|
|
$roleCreateStruct->addPolicy($roleService->newPolicyCreateStruct('content', 'versionread')); |
1150
|
|
|
$roleCreateStruct->addPolicy($roleService->newPolicyCreateStruct('content', 'manage_locations')); |
1151
|
|
|
|
1152
|
|
|
$policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'create'); |
1153
|
|
|
$policyCreateStruct->addLimitation($locationLimitation); |
1154
|
|
|
$roleCreateStruct->addPolicy($policyCreateStruct); |
1155
|
|
|
|
1156
|
|
|
$roleDraft = $roleService->createRole($roleCreateStruct); |
1157
|
|
|
$roleService->publishRoleDraft($roleDraft); |
1158
|
|
|
|
1159
|
|
|
// Create a user with that Role |
1160
|
|
|
$user = $this->createCustomUserVersion1('Users', $roleIdentifier); |
1161
|
|
|
$this->permissionResolver->setCurrentUserReference($user); |
1162
|
|
|
|
1163
|
|
|
// Test copying Content to the authorized Location |
1164
|
|
|
$this->contentService->copyContent( |
1165
|
|
|
$authorizedFolder->contentInfo, |
1166
|
|
|
$locationService->newLocationCreateStruct( |
1167
|
|
|
$authorizedFolder->contentInfo->mainLocationId |
1168
|
|
|
) |
1169
|
|
|
); |
1170
|
|
|
} |
1171
|
|
|
|
1172
|
|
|
/** |
1173
|
|
|
* Test copying Content to the authorized Location (limited by policies). |
1174
|
|
|
*/ |
1175
|
|
|
public function testCopyContentToAuthorizedLocationWithSubtreeLimitation() |
1176
|
|
|
{ |
1177
|
|
|
$locationService = $this->repository->getLocationService(); |
1178
|
|
|
|
1179
|
|
|
// Create and publish folders for the test case |
1180
|
|
|
$folderDraft = $this->createContentDraft('folder', 2, ['name' => 'Folder1']); |
1181
|
|
|
$this->contentService->publishVersion($folderDraft->versionInfo); |
1182
|
|
|
$authorizedFolderDraft = $this->createContentDraft('folder', 2, ['name' => 'AuthorizedFolder']); |
1183
|
|
|
$authorizedFolder = $this->contentService->publishVersion($authorizedFolderDraft->versionInfo); |
1184
|
|
|
|
1185
|
|
|
// Prepare Role for the test case |
1186
|
|
|
$roleIdentifier = 'authorized_subree'; |
1187
|
|
|
$subtreeLimitation = new SubtreeLimitation( |
1188
|
|
|
['limitationValues' => ['/1/2']] |
1189
|
|
|
); |
1190
|
|
|
$policiesData = [ |
1191
|
|
|
[ |
1192
|
|
|
'module' => 'content', |
1193
|
|
|
'function' => 'read', |
1194
|
|
|
'limitations' => [$subtreeLimitation], |
1195
|
|
|
], |
1196
|
|
|
[ |
1197
|
|
|
'module' => 'content', |
1198
|
|
|
'function' => 'versionread', |
1199
|
|
|
'limitations' => [$subtreeLimitation], |
1200
|
|
|
], |
1201
|
|
|
[ |
1202
|
|
|
'module' => 'content', |
1203
|
|
|
'function' => 'create', |
1204
|
|
|
'limitations' => [$subtreeLimitation], |
1205
|
|
|
], |
1206
|
|
|
[ |
1207
|
|
|
'module' => 'content', |
1208
|
|
|
'function' => 'manage_locations', |
1209
|
|
|
], |
1210
|
|
|
]; |
1211
|
|
|
|
1212
|
|
|
$this->createRoleWithPolicies($roleIdentifier, $policiesData); |
1213
|
|
|
|
1214
|
|
|
// Create a user with that Role |
1215
|
|
|
$user = $this->createCustomUserVersion1('Users', $roleIdentifier); |
1216
|
|
|
$this->permissionResolver->setCurrentUserReference($user); |
1217
|
|
|
|
1218
|
|
|
// Test copying Content to the authorized Location |
1219
|
|
|
$this->contentService->copyContent( |
1220
|
|
|
$authorizedFolder->contentInfo, |
1221
|
|
|
$locationService->newLocationCreateStruct( |
1222
|
|
|
$authorizedFolder->contentInfo->mainLocationId |
1223
|
|
|
) |
1224
|
|
|
); |
1225
|
|
|
} |
1226
|
|
|
|
1227
|
|
|
/** |
1228
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
1229
|
|
|
* |
1230
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
1231
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
1232
|
|
|
*/ |
1233
|
|
|
private function getContentInfoForAnonymousUser(): ContentInfo |
1234
|
|
|
{ |
1235
|
|
|
$anonymousUserId = $this->generateId('user', 10); |
1236
|
|
|
|
1237
|
|
|
return $this->contentService->loadContentInfo($anonymousUserId); |
1238
|
|
|
} |
1239
|
|
|
|
1240
|
|
|
private function setRestrictedEditorUser(): void |
1241
|
|
|
{ |
1242
|
|
|
$this->permissionResolver->setCurrentUserReference($this->createAnonymousWithEditorRole()); |
1243
|
|
|
} |
1244
|
|
|
} |
1245
|
|
|
|