Code Duplication    Length = 47-49 lines in 2 locations

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

@@ 937-983 (lines=47) @@
934
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
935
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
936
     */
937
    public function testCanUserWithMultipleTargetsNo()
938
    {
939
        $repository = $this->getRepository();
940
941
        $homeLocationId = $this->generateId('location', 2);
942
        $administratorUsersLocationId = $this->generateId('location', 13);
943
944
        /* BEGIN: Use Case */
945
        // $homeLocationId contains the ID of the "Home" location
946
        // $administratorUsersLocationId contains the ID of the "Administrator users" location
947
948
        $user = $this->createUserVersion1();
949
950
        // Set created user as current user
951
        $repository->setCurrentUser($user);
952
953
        $contentTypeService = $repository->getContentTypeService();
954
955
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forums');
956
957
        $contentService = $repository->getContentService();
958
959
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
960
        $contentCreateStruct->setField('name', 'My awesome forums');
961
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
962
        $contentCreateStruct->alwaysAvailable = true;
963
964
        $locationService = $repository->getLocationService();
965
        $locationCreateStruct1 = $locationService->newLocationCreateStruct($homeLocationId);
966
        $locationCreateStruct2 = $locationService->newLocationCreateStruct($administratorUsersLocationId);
967
        $locationCreateStructs = [$locationCreateStruct1, $locationCreateStruct2];
968
969
        // This call will return false because user with Editor role does not have permission to
970
        // create content in the "Administrator users" location subtree
971
        $canUser = $repository->canUser(
972
            'content',
973
            'create',
974
            $contentCreateStruct,
975
            $locationCreateStructs
976
        );
977
978
        // Performing an action without necessary permissions will fail with "UnauthorizedException"
979
        if (!$canUser) {
980
            $contentDraft = $contentService->createContent($contentCreateStruct, $locationCreateStructs);
981
        }
982
        /* END: Use Case */
983
    }
984
985
    /**
986
     * Test for the canUser() method.

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

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