Completed
Push — refactor-content-service-integ... ( 5a024e...9a41f2 )
by
unknown
19:04
created

setRestrictedEditorUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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