Code Duplication    Length = 46-48 lines in 2 locations

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

@@ 794-839 (lines=46) @@
791
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
792
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
793
     */
794
    public function testCanUserWithTargetNo()
795
    {
796
        $repository = $this->getRepository();
797
798
        $homeLocationId = $this->generateId('location', 2);
799
800
        /* BEGIN: Use Case */
801
        // $homeLocationId contains the ID of the "Home" frontpage location
802
803
        $user = $this->createUserVersion1();
804
805
        // Set created user as current user
806
        $repository->setCurrentUser($user);
807
808
        $contentTypeService = $repository->getContentTypeService();
809
810
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
811
812
        $contentService = $repository->getContentService();
813
814
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
815
        $contentCreateStruct->setField('name', 'My awesome forum');
816
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
817
        $contentCreateStruct->alwaysAvailable = true;
818
819
        $locationService = $repository->getLocationService();
820
        $locationCreateStruct = $locationService->newLocationCreateStruct($homeLocationId);
821
822
        // This call will return false because user with Editor role has permission to
823
        // create "forum" type content only under "folder" type content.
824
        $canUser = $repository->canUser(
825
            'content',
826
            'create',
827
            $contentCreateStruct,
828
            $locationCreateStruct
829
        );
830
831
        // Performing an action without necessary permissions will fail with "UnauthorizedException"
832
        if (!$canUser) {
833
            $contentDraft = $contentService->createContent(
834
                $contentCreateStruct,
835
                array($locationCreateStruct)
836
            );
837
        }
838
        /* END: Use Case */
839
    }
840
841
    /**
842
     * Test for the canUser() method.

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

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