Code Duplication    Length = 46-48 lines in 2 locations

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

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