Code Duplication    Length = 47-49 lines in 2 locations

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

@@ 911-957 (lines=47) @@
908
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
909
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
910
     */
911
    public function testCanUserWithMultipleTargetsNo()
912
    {
913
        $repository = $this->getRepository();
914
915
        $homeLocationId = $this->generateId('location', 2);
916
        $administratorUsersLocationId = $this->generateId('location', 13);
917
918
        /* BEGIN: Use Case */
919
        // $homeLocationId contains the ID of the "Home" location
920
        // $administratorUsersLocationId contains the ID of the "Administrator users" location
921
922
        $user = $this->createUserVersion1();
923
924
        // Set created user as current user
925
        $repository->setCurrentUser($user);
926
927
        $contentTypeService = $repository->getContentTypeService();
928
929
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forums');
930
931
        $contentService = $repository->getContentService();
932
933
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
934
        $contentCreateStruct->setField('name', 'My awesome forums');
935
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
936
        $contentCreateStruct->alwaysAvailable = true;
937
938
        $locationService = $repository->getLocationService();
939
        $locationCreateStruct1 = $locationService->newLocationCreateStruct($homeLocationId);
940
        $locationCreateStruct2 = $locationService->newLocationCreateStruct($administratorUsersLocationId);
941
        $locationCreateStructs = array($locationCreateStruct1, $locationCreateStruct2);
942
943
        // This call will return false because user with Editor role does not have permission to
944
        // create content in the "Administrator users" location subtree
945
        $canUser = $repository->canUser(
946
            'content',
947
            'create',
948
            $contentCreateStruct,
949
            $locationCreateStructs
950
        );
951
952
        // Performing an action without necessary permissions will fail with "UnauthorizedException"
953
        if (!$canUser) {
954
            $contentDraft = $contentService->createContent($contentCreateStruct, $locationCreateStructs);
955
        }
956
        /* END: Use Case */
957
    }
958
959
    /**
960
     * Test for the canUser() method.

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

@@ 650-698 (lines=49) @@
647
     * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited
648
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
649
     */
650
    public function testCanUserWithMultipleTargetsNo()
651
    {
652
        $repository = $this->getRepository();
653
654
        $homeLocationId = $this->generateId('location', 2);
655
        $administratorUsersLocationId = $this->generateId('location', 13);
656
657
        /* BEGIN: Use Case */
658
        // $homeLocationId contains the ID of the "Home" location
659
        // $administratorUsersLocationId contains the ID of the "Administrator users" location
660
661
        $user = $this->createUserVersion1();
662
663
        $permissionResolver = $repository->getPermissionResolver();
664
665
        // Set created user as current user reference
666
        $permissionResolver->setCurrentUserReference($user);
667
668
        $contentTypeService = $repository->getContentTypeService();
669
670
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forums');
671
672
        $contentService = $repository->getContentService();
673
674
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
675
        $contentCreateStruct->setField('name', 'My awesome forums');
676
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
677
        $contentCreateStruct->alwaysAvailable = true;
678
679
        $locationService = $repository->getLocationService();
680
        $locationCreateStruct1 = $locationService->newLocationCreateStruct($homeLocationId);
681
        $locationCreateStruct2 = $locationService->newLocationCreateStruct($administratorUsersLocationId);
682
        $locationCreateStructs = array($locationCreateStruct1, $locationCreateStruct2);
683
684
        // This call will return false because user with Editor role does not have permission to
685
        // create content in the "Administrator users" location subtree
686
        $canUser = $permissionResolver->canUser(
687
            'content',
688
            'create',
689
            $contentCreateStruct,
690
            $locationCreateStructs
691
        );
692
693
        // Performing an action without necessary permissions will fail with "UnauthorizedException"
694
        if (!$canUser) {
695
            $contentDraft = $contentService->createContent($contentCreateStruct, $locationCreateStructs);
696
        }
697
        /* END: Use Case */
698
    }
699
700
    /**
701
     * Test for the canUser() method.