Code Duplication    Length = 47-49 lines in 2 locations

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

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