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
                [$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

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