Completed
Push — EZP-30427 ( 7404d5...9ad846 )
by
unknown
24:21 queued 06:29
created

testLoadContentInfoListSkipsUnauthorizedItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
cc 1
nc 1
nop 0
rs 10
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;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
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 countContentDrafts() method.
636
     *
637
     * @see \eZ\Publish\API\Repository\ContentService::countContentDrafts()
638
     */
639
    public function testCountContentDraftsReturnZero()
640
    {
641
        $this->permissionResolver->setCurrentUserReference($this->anonymousUser);
642
643
        $this->assertSame(0, $this->contentService->countContentDrafts());
644
    }
645
646
    /**
647
     * Test for the loadContentDrafts() method.
648
     *
649
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
650
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
651
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
652
     */
653
    public function testLoadContentDraftsThrowsUnauthorizedException()
654
    {
655
        $this->permissionResolver->setCurrentUserReference($this->anonymousUser);
656
657
        $this->expectException(UnauthorizedException::class);
658
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
659
660
        $this->contentService->loadContentDrafts();
661
    }
662
663
    /**
664
     * Test for the loadContentDrafts() method.
665
     *
666
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts($user)
667
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
668
     */
669
    public function testLoadContentDraftsThrowsUnauthorizedExceptionWithUser()
670
    {
671
        $this->permissionResolver->setCurrentUserReference($this->anonymousUser);
672
673
        $this->expectException(UnauthorizedException::class);
674
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
675
676
        $this->contentService->loadContentDrafts($this->administratorUser);
677
    }
678
679
    /**
680
     * Test for the updateContent() method.
681
     *
682
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
683
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
684
     */
685
    public function testUpdateContentThrowsUnauthorizedException()
686
    {
687
        $draftVersion2 = $this->createContentDraftVersion2();
688
689
        $versionInfo = $draftVersion2->getVersionInfo();
690
691
        $this->permissionResolver->setCurrentUserReference($this->anonymousUser);
692
693
        // Create an update struct and modify some fields
694
        $contentUpdate = $this->contentService->newContentUpdateStruct();
695
        $contentUpdate->setField('name', 'An awesome² story about ezp.');
696
        $contentUpdate->setField('name', 'An awesome²³ story about ezp.', 'eng-GB');
697
698
        $contentUpdate->initialLanguageCode = 'eng-US';
699
700
        $this->expectException(UnauthorizedException::class);
701
        /* TODO - the `content/edit` policy should be probably needed */
702
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
703
704
        $this->contentService->updateContent($versionInfo, $contentUpdate);
705
    }
706
707
    /**
708
     * Test for the publishVersion() method.
709
     *
710
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
711
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
712
     */
713 View Code Duplication
    public function testPublishVersionThrowsUnauthorizedException()
714
    {
715
        $draft = $this->createContentDraftVersion1();
716
717
        $this->permissionResolver->setCurrentUserReference($this->anonymousUser);
718
719
        $this->expectException(UnauthorizedException::class);
720
        $this->expectExceptionMessageRegExp('/\'publish\' \'content\'/');
721
722
        $this->contentService->publishVersion($draft->getVersionInfo());
723
    }
724
725
    /**
726
     * Test for the deleteVersion() method.
727
     *
728
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
729
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteVersion
730
     */
731 View Code Duplication
    public function testDeleteVersionThrowsUnauthorizedException()
732
    {
733
        $draft = $this->createContentDraftVersion1();
734
735
        $this->permissionResolver->setCurrentUserReference($this->anonymousUser);
736
737
        $this->expectException(UnauthorizedException::class);
738
        $this->expectExceptionMessageRegExp('/\'versionremove\' \'content\'/');
739
740
        $this->contentService->deleteVersion($draft->getVersionInfo());
741
    }
742
743
    /**
744
     * Test for the loadVersions() method.
745
     *
746
     * @see \eZ\Publish\API\Repository\ContentService::loadVersions()
747
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersions
748
     */
749 View Code Duplication
    public function testLoadVersionsThrowsUnauthorizedException()
750
    {
751
        $contentVersion2 = $this->createContentVersion2();
752
753
        $contentInfo = $contentVersion2->contentInfo;
754
755
        $this->permissionResolver->setCurrentUserReference($this->anonymousUser);
756
757
        $this->expectException(UnauthorizedException::class);
758
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
759
760
        $this->contentService->loadVersions($contentInfo);
761
    }
762
763
    /**
764
     * Test for the copyContent() method.
765
     *
766
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
767
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
768
     */
769
    public function testCopyContentThrowsUnauthorizedException()
770
    {
771
        $parentLocationId = $this->generateId('location', 52);
772
773
        $locationService = $this->repository->getLocationService();
774
775
        $contentVersion2 = $this->createMultipleLanguageContentVersion2();
776
777
        $contentInfo = $contentVersion2->contentInfo;
778
779
        $this->permissionResolver->setCurrentUserReference($this->anonymousUser);
780
781
        // Configure new target location
782
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
783
784
        $targetLocationCreate->priority = 42;
785
        $targetLocationCreate->hidden = true;
786
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
787
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
788
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
789
790
        $this->expectException(UnauthorizedException::class);
791
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
792
793
        $this->contentService->copyContent(
794
            $contentInfo,
795
            $targetLocationCreate
796
        );
797
    }
798
799
    /**
800
     * Test for the copyContent() method.
801
     *
802
     * @see \eZ\Publish\API\Repository\ContentService::copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo)
803
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContentWithGivenVersion
804
     */
805
    public function testCopyContentThrowsUnauthorizedExceptionWithGivenVersion()
806
    {
807
        $parentLocationId = $this->generateId('location', 52);
808
809
        $contentVersion2 = $this->createContentVersion2();
810
811
        $this->permissionResolver->setCurrentUserReference($this->anonymousUser);
812
813
        // Configure new target location
814
        $targetLocationCreate = $this->repository->getLocationService()->newLocationCreateStruct($parentLocationId);
815
816
        $targetLocationCreate->priority = 42;
817
        $targetLocationCreate->hidden = true;
818
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
819
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
820
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
821
822
        $this->expectException(UnauthorizedException::class);
823
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
824
825
        $this->contentService->copyContent(
826
            $contentVersion2->contentInfo,
827
            $targetLocationCreate,
828
            $this->contentService->loadVersionInfo($contentVersion2->contentInfo, 1)
829
        );
830
    }
831
832
    /**
833
     * Test for the loadRelations() method.
834
     *
835
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
836
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
837
     */
838 View Code Duplication
    public function testLoadRelationsThrowsUnauthorizedException()
839
    {
840
        $mediaEditor = $this->createMediaUserVersion1();
841
842
        $setupRemoteId = '241d538ce310074e602f29f49e44e938';
843
844
        $versionInfo = $this->contentService->loadVersionInfo(
845
            $this->contentService->loadContentInfoByRemoteId(
846
                $setupRemoteId
847
            )
848
        );
849
850
        $this->permissionResolver->setCurrentUserReference($mediaEditor);
851
852
        $this->expectException(UnauthorizedException::class);
853
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
854
855
        $this->contentService->loadRelations($versionInfo);
856
    }
857
858
    /**
859
     * Test for the loadRelations() method.
860
     *
861
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
862
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
863
     */
864 View Code Duplication
    public function testLoadRelationsForDraftVersionThrowsUnauthorizedException()
865
    {
866
        $draft = $this->createContentDraftVersion1();
867
868
        $this->permissionResolver->setCurrentUserReference($this->anonymousUser);
869
870
        $this->expectException(UnauthorizedException::class);
871
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
872
873
        $this->contentService->loadRelations($draft->versionInfo);
874
    }
875
876
    /**
877
     * Test for the loadReverseRelations() method.
878
     *
879
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
880
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
881
     */
882
    public function testLoadReverseRelationsThrowsUnauthorizedException()
883
    {
884
        $mediaEditor = $this->createMediaUserVersion1();
885
886
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
887
888
        $contentInfo = $this->contentService->loadContentInfoByRemoteId($mediaRemoteId);
889
890
        $this->permissionResolver->setCurrentUserReference($mediaEditor);
891
892
        $this->expectException(UnauthorizedException::class);
893
        $this->expectExceptionMessageRegExp('/\'reverserelatedlist\' \'content\'/');
894
895
        $this->contentService->loadReverseRelations($contentInfo);
896
    }
897
898
    /**
899
     * Test for the addRelation() method.
900
     *
901
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
902
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
903
     */
904 View Code Duplication
    public function testAddRelationThrowsUnauthorizedException()
905
    {
906
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
907
908
        $draft = $this->createContentDraftVersion1();
909
910
        $versionInfo = $draft->getVersionInfo();
911
912
        $media = $this->contentService->loadContentInfoByRemoteId($mediaRemoteId);
913
914
        $this->permissionResolver->setCurrentUserReference($this->anonymousUser);
915
916
        $this->expectException(UnauthorizedException::class);
917
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
918
919
        $this->contentService->addRelation(
920
            $versionInfo,
921
            $media
922
        );
923
    }
924
925
    /**
926
     * Test for the deleteRelation() method.
927
     *
928
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
929
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
930
     */
931
    public function testDeleteRelationThrowsUnauthorizedException()
932
    {
933
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
934
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
935
936
        $draft = $this->createContentDraftVersion1();
937
938
        $versionInfo = $draft->getVersionInfo();
939
940
        $media = $this->contentService->loadContentInfoByRemoteId($mediaRemoteId);
941
        $demoDesign = $this->contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
942
943
        // Establish some relations
944
        $this->contentService->addRelation($draft->getVersionInfo(), $media);
945
        $this->contentService->addRelation($draft->getVersionInfo(), $demoDesign);
946
947
        $this->permissionResolver->setCurrentUserReference($this->anonymousUser);
948
949
        $this->expectException(UnauthorizedException::class);
950
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
951
952
        $this->contentService->deleteRelation($versionInfo, $media);
953
    }
954
955
    /**
956
     * Creates a pseudo editor with a limitation to objects in the "Media/Images"
957
     * subtree.
958
     *
959
     * @return \eZ\Publish\API\Repository\Values\User\User
960
     */
961
    private function createAnonymousWithEditorRole()
962
    {
963
        $roleService = $this->repository->getRoleService();
964
965
        $user = $this->anonymousUser;
966
        $role = $roleService->loadRoleByIdentifier('Editor');
967
968
        // Assign "Editor" role with limitation to "Media/Images"
969
        $roleService->assignRoleToUser(
970
            $role,
971
            $user,
972
            new \eZ\Publish\API\Repository\Values\User\Limitation\SubtreeLimitation(
973
                [
974
                    'limitationValues' => ['/1/43/51/'],
975
                ]
976
            )
977
        );
978
979
        return $this->userService->loadUser($user->id);
980
    }
981
982
    /**
983
     * Test that for an user that doesn't have access (read permissions) to an
984
     * related object, executing loadRelations() would not throw any exception,
985
     * only that the non-readable related object(s) won't be loaded.
986
     *
987
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
988
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
989
     */
990
    public function testLoadRelationsWithUnauthorizedRelations()
991
    {
992
        $mainLanguage = 'eng-GB';
993
994
        $contentTypeService = $this->repository->getContentTypeService();
995
        $locationService = $this->repository->getLocationService();
996
        $sectionService = $this->repository->getSectionService();
997
998
        // set the current user as admin to create the environment to test
999
        $this->permissionResolver->setCurrentUserReference($this->administratorUser);
1000
1001
        // create section
1002
        // since anonymous users have their read permissions to specific sections
1003
        // the created section will be non-readable to them
1004
        $sectionCreate = $sectionService->newSectionCreateStruct();
1005
        $sectionCreate->identifier = 'private';
1006
        $sectionCreate->name = 'Private Section';
1007
        $section = $sectionService->createSection($sectionCreate);
1008
1009
        // create objects for testing
1010
        // here we will create 4 objects which 2 will be readable by an anonymous
1011
        // user, and the other 2 wont these last 2 will go to a private section
1012
        // where anonymous can't read, just like:
1013
        // readable object 1 -> /Main Folder
1014
        // readable object 2 -> /Main Folder/Available Folder
1015
        // non-readable object 1 -> /Restricted Folder
1016
        // non-readable object 2 -> /Restricted Folder/Unavailable Folder
1017
        //
1018
        // here is created - readable object 1 -> /Main Folder
1019
        $mainFolder = $this->createFolder([$mainLanguage => 'Main Folder'], 2);
1020
1021
        // here is created readable object 2 -> /Main Folder/Available Folder
1022
        $availableFolder = $this->createFolder(
1023
            [$mainLanguage => 'Avaliable Folder'],
1024
            $mainFolder->contentInfo->mainLocationId
1025
        );
1026
1027
        // here is created the non-readable object 1 -> /Restricted Folder
1028
        $restrictedFolderCreate = $this->contentService->newContentCreateStruct(
1029
            $contentTypeService->loadContentTypeByIdentifier('folder'),
1030
            $mainLanguage
1031
        );
1032
        $restrictedFolderCreate->setField('name', 'Restricted Folder');
1033
        $restrictedFolderCreate->sectionId = $section->id;
1034
        $restrictedFolder = $this->contentService->publishVersion(
1035
            $this->contentService->createContent(
1036
                $restrictedFolderCreate,
1037
                [$locationService->newLocationCreateStruct(2)]
1038
            )->versionInfo
1039
        );
1040
1041
        // here is created non-readable object 2 -> /Restricted Folder/Unavailable Folder
1042
        $unavailableFolder = $this->createFolder(
1043
            [$mainLanguage => 'Unavailable Folder'],
1044
            $restrictedFolder->contentInfo->mainLocationId
1045
        );
1046
1047
        // this will be our test object, which will have all the relations (as source)
1048
        // and it is readable by the anonymous user
1049
        $testFolderCreate = $this->contentService->newContentCreateStruct(
1050
            $contentTypeService->loadContentTypeByIdentifier('folder'),
1051
            $mainLanguage
1052
        );
1053
        $testFolderCreate->setField('name', 'Test Folder');
1054
        $testFolderDraft = $this->contentService->createContent(
1055
            $testFolderCreate,
1056
            [$locationService->newLocationCreateStruct(2)]
1057
        )->versionInfo;
1058
1059
        // add relations to test folder (as source)
1060
        // the first 2 will be read by the user
1061
        // and the other 2 wont
1062
        //
1063
        // create relation from Test Folder to Main Folder
1064
        $mainRelation = $this->contentService->addRelation(
1065
            $testFolderDraft,
1066
            $mainFolder->getVersionInfo()->getContentInfo()
1067
        );
1068
        // create relation from Test Folder to Available Folder
1069
        $availableRelation = $this->contentService->addRelation(
1070
            $testFolderDraft,
1071
            $availableFolder->getVersionInfo()->getContentInfo()
1072
        );
1073
        // create relation from Test Folder to Restricted Folder
1074
        $this->contentService->addRelation(
1075
            $testFolderDraft,
1076
            $restrictedFolder->getVersionInfo()->getContentInfo()
1077
        );
1078
        //create relation from Test Folder to Unavailable Folder
1079
        $this->contentService->addRelation(
1080
            $testFolderDraft,
1081
            $unavailableFolder->getVersionInfo()->getContentInfo()
1082
        );
1083
1084
        // publish Test Folder
1085
        $testFolder = $this->contentService->publishVersion($testFolderDraft);
1086
1087
        // set the current user to be an anonymous user since we want to test that
1088
        // if the user doesn't have access to an related object that object wont
1089
        // be loaded and no exception will be thrown
1090
        $this->permissionResolver->setCurrentUserReference($this->anonymousUser);
1091
1092
        // finaly load relations ( verify no exception is thrown )
1093
        $actualRelations = $this->contentService->loadRelations($testFolder->getVersionInfo());
1094
1095
        // assert results
1096
        // verify that the only expected relations are from the 2 readable objects
1097
        // Main Folder and Available Folder
1098
        $expectedRelations = [
1099
            $mainRelation->destinationContentInfo->id => $mainRelation,
1100
            $availableRelation->destinationContentInfo->id => $availableRelation,
1101
        ];
1102
1103
        // assert there are as many expected relations as actual ones
1104
        $this->assertEquals(
1105
            count($expectedRelations),
1106
            count($actualRelations),
1107
            "Expected '" . count($expectedRelations)
1108
            . "' relations found '" . count($actualRelations) . "'"
1109
        );
1110
1111
        // assert each relation
1112
        foreach ($actualRelations as $relation) {
1113
            $destination = $relation->destinationContentInfo;
1114
            $expected = $expectedRelations[$destination->id]->destinationContentInfo;
1115
            $this->assertNotEmpty($expected, "Non expected relation with '{$destination->id}' id found");
1116
            $this->assertEquals(
1117
                $expected->id,
1118
                $destination->id,
1119
                "Expected relation with '{$expected->id}' id found '{$destination->id}' id"
1120
            );
1121
            $this->assertEquals(
1122
                $expected->name,
1123
                $destination->name,
1124
                "Expected relation with '{$expected->name}' name found '{$destination->name}' name"
1125
            );
1126
1127
            // remove from list
1128
            unset($expectedRelations[$destination->id]);
1129
        }
1130
1131
        // verify all expected relations were found
1132
        $this->assertEquals(
1133
            0,
1134
            count($expectedRelations),
1135
            "Expected to find '" . (count($expectedRelations) + count($actualRelations))
1136
            . "' relations found '" . count($actualRelations) . "'"
1137
        );
1138
    }
1139
1140
    /**
1141
     * Test copying Content to the authorized Location (limited by policies).
1142
     */
1143
    public function testCopyContentToAuthorizedLocation()
1144
    {
1145
        $locationService = $this->repository->getLocationService();
1146
        $roleService = $this->repository->getRoleService();
1147
1148
        // Create and publish folders for the test case
1149
        $folderDraft = $this->createContentDraft('folder', 2, ['name' => 'Folder1']);
1150
        $this->contentService->publishVersion($folderDraft->versionInfo);
1151
        $authorizedFolderDraft = $this->createContentDraft('folder', 2, ['name' => 'AuthorizedFolder']);
1152
        $authorizedFolder = $this->contentService->publishVersion($authorizedFolderDraft->versionInfo);
1153
1154
        // Prepare Role for the test case
1155
        $roleIdentifier = 'authorized_folder';
1156
        $roleCreateStruct = $roleService->newRoleCreateStruct($roleIdentifier);
1157
        $locationLimitation = new LocationLimitation(
1158
            ['limitationValues' => [$authorizedFolder->contentInfo->mainLocationId]]
1159
        );
1160
        $roleCreateStruct->addPolicy($roleService->newPolicyCreateStruct('content', 'read'));
1161
        $roleCreateStruct->addPolicy($roleService->newPolicyCreateStruct('content', 'versionread'));
1162
        $roleCreateStruct->addPolicy($roleService->newPolicyCreateStruct('content', 'manage_locations'));
1163
1164
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'create');
1165
        $policyCreateStruct->addLimitation($locationLimitation);
1166
        $roleCreateStruct->addPolicy($policyCreateStruct);
1167
1168
        $roleDraft = $roleService->createRole($roleCreateStruct);
1169
        $roleService->publishRoleDraft($roleDraft);
1170
1171
        // Create a user with that Role
1172
        $user = $this->createCustomUserVersion1('Users', $roleIdentifier);
1173
        $this->permissionResolver->setCurrentUserReference($user);
1174
1175
        // Test copying Content to the authorized Location
1176
        $this->contentService->copyContent(
1177
            $authorizedFolder->contentInfo,
1178
            $locationService->newLocationCreateStruct(
1179
                $authorizedFolder->contentInfo->mainLocationId
1180
            )
1181
        );
1182
    }
1183
1184
    /**
1185
     * Test copying Content to the authorized Location (limited by policies).
1186
     */
1187
    public function testCopyContentToAuthorizedLocationWithSubtreeLimitation()
1188
    {
1189
        $locationService = $this->repository->getLocationService();
1190
1191
        // Create and publish folders for the test case
1192
        $folderDraft = $this->createContentDraft('folder', 2, ['name' => 'Folder1']);
1193
        $this->contentService->publishVersion($folderDraft->versionInfo);
1194
        $authorizedFolderDraft = $this->createContentDraft('folder', 2, ['name' => 'AuthorizedFolder']);
1195
        $authorizedFolder = $this->contentService->publishVersion($authorizedFolderDraft->versionInfo);
1196
1197
        // Prepare Role for the test case
1198
        $roleIdentifier = 'authorized_subree';
1199
        $subtreeLimitation = new SubtreeLimitation(
1200
            ['limitationValues' => ['/1/2']]
1201
        );
1202
        $policiesData = [
1203
            [
1204
                'module' => 'content',
1205
                'function' => 'read',
1206
                'limitations' => [$subtreeLimitation],
1207
            ],
1208
            [
1209
                'module' => 'content',
1210
                'function' => 'versionread',
1211
                'limitations' => [$subtreeLimitation],
1212
            ],
1213
            [
1214
                'module' => 'content',
1215
                'function' => 'create',
1216
                'limitations' => [$subtreeLimitation],
1217
            ],
1218
            [
1219
                'module' => 'content',
1220
                'function' => 'manage_locations',
1221
            ],
1222
        ];
1223
1224
        $this->createRoleWithPolicies($roleIdentifier, $policiesData);
1225
1226
        // Create a user with that Role
1227
        $user = $this->createCustomUserVersion1('Users', $roleIdentifier);
1228
        $this->permissionResolver->setCurrentUserReference($user);
1229
1230
        // Test copying Content to the authorized Location
1231
        $this->contentService->copyContent(
1232
            $authorizedFolder->contentInfo,
1233
            $locationService->newLocationCreateStruct(
1234
                $authorizedFolder->contentInfo->mainLocationId
1235
            )
1236
        );
1237
    }
1238
1239
    /**
1240
     * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo
1241
     *
1242
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
1243
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1244
     */
1245
    private function getContentInfoForAnonymousUser(): ContentInfo
1246
    {
1247
        $anonymousUserId = $this->generateId('user', 10);
1248
1249
        return $this->contentService->loadContentInfo($anonymousUserId);
1250
    }
1251
1252
    private function setRestrictedEditorUser(): void
1253
    {
1254
        $this->permissionResolver->setCurrentUserReference($this->createAnonymousWithEditorRole());
1255
    }
1256
}
1257