Code Duplication    Length = 26-36 lines in 4 locations

eZ/Publish/API/Repository/Tests/ContentServiceAuthorizationTest.php 3 locations

@@ 644-675 (lines=32) @@
641
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
642
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
643
     */
644
    public function testLoadContentThrowsUnauthorizedExceptionOnDrafts()
645
    {
646
        /** @var $repository \eZ\Publish\API\Repository\Repository */
647
        $repository = $this->getRepository();
648
649
        $anonymousUserId = $this->generateId('user', 10);
650
        /* BEGIN: Use Case */
651
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
652
        // demo installation
653
        $user = $this->createUserVersion1();
654
655
        // Set new editor as a content owner
656
        $repository->setCurrentUser($user);
657
658
        // Create draft with this user
659
        $draft = $this->createContentDraftVersion1(2, 'folder');
660
661
        // Load anonymous user
662
        $userService = $repository->getUserService();
663
        $user = $userService->loadUser($anonymousUserId);
664
        $repository->setCurrentUser($user);
665
666
        // Try to load the draft with anonymous user to make sure access won't be allowed by throwing an exception
667
        $contentService = $repository->getContentService();
668
669
        $this->expectException(UnauthorizedException::class);
670
        // content versionread policy is needed because it is a draft
671
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
672
673
        $contentService->loadContent($draft->id);
674
        /* END: Use Case */
675
    }
676
677
    /**
678
     * Test for the ContentService::loadContent() method on an archive.
@@ 937-964 (lines=28) @@
934
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft($contentInfo, $versionInfo)
935
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithSecondParameter
936
     */
937
    public function testCreateContentDraftThrowsUnauthorizedExceptionWithSecondParameter()
938
    {
939
        $repository = $this->getRepository();
940
941
        $contentService = $repository->getContentService();
942
943
        $anonymousUserId = $this->generateId('user', 10);
944
        /* BEGIN: Use Case */
945
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
946
        // demo installation
947
        $content = $this->createContentVersion1();
948
949
        // Get ContentInfo and VersionInfo instances
950
        $contentInfo = $content->contentInfo;
951
        $versionInfo = $content->getVersionInfo();
952
953
        // Load the user service
954
        $userService = $repository->getUserService();
955
956
        // Set anonymous user
957
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
958
959
        $this->expectException(UnauthorizedException::class);
960
        $this->expectExceptionMessageRegExp('/\'edit\' \'content\'/');
961
962
        $contentService->createContentDraft($contentInfo, $versionInfo);
963
        /* END: Use Case */
964
    }
965
966
    /**
967
     * Test for the loadContentDrafts() method.
@@ 1338-1373 (lines=36) @@
1335
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
1336
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
1337
     */
1338
    public function testAddRelationThrowsUnauthorizedException()
1339
    {
1340
        $repository = $this->getRepository();
1341
1342
        $contentService = $repository->getContentService();
1343
1344
        $anonymousUserId = $this->generateId('user', 10);
1345
        /* BEGIN: Use Case */
1346
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1347
        // demo installation
1348
        // Remote id of the "Media" page of a eZ Publish demo installation.
1349
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
1350
1351
        $draft = $this->createContentDraftVersion1();
1352
1353
        // Get the draft's version info
1354
        $versionInfo = $draft->getVersionInfo();
1355
1356
        // Load other content object
1357
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
1358
1359
        // Load the user service
1360
        $userService = $repository->getUserService();
1361
1362
        // Set anonymous user
1363
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
1364
1365
        $this->expectException(UnauthorizedException::class);
1366
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
1367
1368
        $contentService->addRelation(
1369
            $versionInfo,
1370
            $media
1371
        );
1372
        /* END: Use Case */
1373
    }
1374
1375
    /**
1376
     * Test for the deleteRelation() method.

eZ/Publish/API/Repository/Tests/SectionServiceAuthorizationTest.php 1 location

@@ 208-233 (lines=26) @@
205
     *
206
     * @see \eZ\Publish\API\Repository\SectionService::loadSectionByIdentifier()
207
     */
208
    public function testLoadSectionByIdentifierThrowsUnauthorizedException()
209
    {
210
        $repository = $this->getRepository();
211
212
        $anonymousUserId = $this->generateId('user', 10);
213
        /* BEGIN: Use Case */
214
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
215
        // Publish demo installation.
216
        $userService = $repository->getUserService();
217
        $sectionService = $repository->getSectionService();
218
219
        $sectionCreate = $sectionService->newSectionCreateStruct();
220
        $sectionCreate->name = 'Test Section';
221
        $sectionCreate->identifier = 'uniqueKey';
222
223
        $sectionService->createSection($sectionCreate);
224
225
        // Set anonymous user
226
        $repository->getPermissionResolver()->setCurrentUserReference($userService->loadUser($anonymousUserId));
227
228
        $this->expectException(UnauthorizedException::class);
229
        $this->expectExceptionMessage("User does not have access to 'view' 'section");
230
231
        $sectionService->loadSectionByIdentifier('uniqueKey');
232
        /* END: Use Case */
233
    }
234
235
    /**
236
     * Test for the assignSection() method.