Code Duplication    Length = 46-48 lines in 2 locations

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

@@ 820-865 (lines=46) @@
817
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
818
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
819
     */
820
    public function testCanUserWithTargetNo()
821
    {
822
        $repository = $this->getRepository();
823
824
        $homeLocationId = $this->generateId('location', 2);
825
826
        /* BEGIN: Use Case */
827
        // $homeLocationId contains the ID of the "Home" frontpage location
828
829
        $user = $this->createUserVersion1();
830
831
        // Set created user as current user
832
        $repository->setCurrentUser($user);
833
834
        $contentTypeService = $repository->getContentTypeService();
835
836
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
837
838
        $contentService = $repository->getContentService();
839
840
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
841
        $contentCreateStruct->setField('name', 'My awesome forum');
842
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
843
        $contentCreateStruct->alwaysAvailable = true;
844
845
        $locationService = $repository->getLocationService();
846
        $locationCreateStruct = $locationService->newLocationCreateStruct($homeLocationId);
847
848
        // This call will return false because user with Editor role has permission to
849
        // create "forum" type content only under "folder" type content.
850
        $canUser = $repository->canUser(
851
            'content',
852
            'create',
853
            $contentCreateStruct,
854
            $locationCreateStruct
855
        );
856
857
        // Performing an action without necessary permissions will fail with "UnauthorizedException"
858
        if (!$canUser) {
859
            $contentDraft = $contentService->createContent(
860
                $contentCreateStruct,
861
                array($locationCreateStruct)
862
            );
863
        }
864
        /* END: Use Case */
865
    }
866
867
    /**
868
     * Test for the canUser() method.

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

@@ 533-580 (lines=48) @@
530
     * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited
531
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
532
     */
533
    public function testCanUserWithTargetNo()
534
    {
535
        $repository = $this->getRepository();
536
537
        $homeLocationId = $this->generateId('location', 2);
538
539
        /* BEGIN: Use Case */
540
        // $homeLocationId contains the ID of the "Home" frontpage location
541
542
        $user = $this->createUserVersion1();
543
544
        $permissionResolver = $repository->getPermissionResolver();
545
546
        // Set created user as current user reference
547
        $permissionResolver->setCurrentUserReference($user);
548
549
        $contentTypeService = $repository->getContentTypeService();
550
551
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
552
553
        $contentService = $repository->getContentService();
554
555
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
556
        $contentCreateStruct->setField('name', 'My awesome forum');
557
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
558
        $contentCreateStruct->alwaysAvailable = true;
559
560
        $locationService = $repository->getLocationService();
561
        $locationCreateStruct = $locationService->newLocationCreateStruct($homeLocationId);
562
563
        // This call will return false because user with Editor role has permission to
564
        // create "forum" type content only under "folder" type content.
565
        $canUser = $permissionResolver->canUser(
566
            'content',
567
            'create',
568
            $contentCreateStruct,
569
            [$locationCreateStruct]
570
        );
571
572
        // Performing an action without necessary permissions will fail with "UnauthorizedException"
573
        if (!$canUser) {
574
            $contentDraft = $contentService->createContent(
575
                $contentCreateStruct,
576
                array($locationCreateStruct)
577
            );
578
        }
579
        /* END: Use Case */
580
    }
581
582
    /**
583
     * Test for the canUser() method.