Code Duplication    Length = 19-27 lines in 11 locations

eZ/Publish/API/Repository/Tests/ContentServiceAuthorizationTest.php 6 locations

@@ 395-420 (lines=26) @@
392
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
393
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfo
394
     */
395
    public function testLoadContentByVersionInfoThrowsUnauthorizedException()
396
    {
397
        $repository = $this->getRepository();
398
399
        $anonymousUserId = $this->generateId('user', 10);
400
        /* BEGIN: Use Case */
401
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
402
        // demo installation
403
404
        $contentService = $repository->getContentService();
405
406
        // Load the ContentInfo for "Anonymous User"
407
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
408
409
        // Load the current VersionInfo
410
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
411
412
        $pseudoEditor = $this->createAnonymousWithEditorRole();
413
414
        // Set restricted editor user
415
        $repository->setCurrentUser($pseudoEditor);
416
417
        // This call will fail with a "UnauthorizedException"
418
        $contentService->loadContentByVersionInfo($versionInfo);
419
        /* END: Use Case */
420
    }
421
422
    /**
423
     * Test for the loadContentByVersionInfo() method.
@@ 429-454 (lines=26) @@
426
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
427
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfoWithSecondParameter
428
     */
429
    public function testLoadContentByVersionInfoThrowsUnauthorizedExceptionWithSecondParameter()
430
    {
431
        $repository = $this->getRepository();
432
433
        $anonymousUserId = $this->generateId('user', 10);
434
        /* BEGIN: Use Case */
435
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
436
        // demo installation
437
438
        $contentService = $repository->getContentService();
439
440
        // Load the ContentInfo for "Anonymous User"
441
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
442
443
        // Load the current VersionInfo
444
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
445
446
        $pseudoEditor = $this->createAnonymousWithEditorRole();
447
448
        // Set restricted editor user
449
        $repository->setCurrentUser($pseudoEditor);
450
451
        // This call will fail with a "UnauthorizedException"
452
        $contentService->loadContentByVersionInfo($versionInfo, ['eng-US']);
453
        /* END: Use Case */
454
    }
455
456
    /**
457
     * Test for the loadContent() method.
@@ 763-786 (lines=24) @@
760
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
761
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
762
     */
763
    public function testDeleteContentThrowsUnauthorizedException()
764
    {
765
        $repository = $this->getRepository();
766
        $contentService = $repository->getContentService();
767
768
        $anonymousUserId = $this->generateId('user', 10);
769
        /* BEGIN: Use Case */
770
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
771
        // demo installation
772
        $contentVersion2 = $this->createContentVersion2();
773
774
        // Get ContentInfo instance
775
        $contentInfo = $contentVersion2->contentInfo;
776
777
        // Load the user service
778
        $userService = $repository->getUserService();
779
780
        // Set anonymous user
781
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
782
783
        // This call will fail with a "UnauthorizedException"
784
        $contentService->deleteContent($contentInfo);
785
        /* END: Use Case */
786
    }
787
788
    /**
789
     * Test for the createContentDraft() method.
@@ 795-819 (lines=25) @@
792
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
793
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
794
     */
795
    public function testCreateContentDraftThrowsUnauthorizedException()
796
    {
797
        $repository = $this->getRepository();
798
799
        $contentService = $repository->getContentService();
800
801
        $anonymousUserId = $this->generateId('user', 10);
802
        /* BEGIN: Use Case */
803
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
804
        // demo installation
805
        $content = $this->createContentVersion1();
806
807
        // Get ContentInfo instance
808
        $contentInfo = $content->contentInfo;
809
810
        // Load the user service
811
        $userService = $repository->getUserService();
812
813
        // Set anonymous user
814
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
815
816
        // This call will fail with a "UnauthorizedException"
817
        $contentService->createContentDraft($contentInfo);
818
        /* END: Use Case */
819
    }
820
821
    /**
822
     * Test for the createContentDraft() method.
@@ 891-917 (lines=27) @@
888
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
889
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
890
     */
891
    public function testLoadContentDraftsThrowsUnauthorizedExceptionWithFirstParameter()
892
    {
893
        $repository = $this->getRepository();
894
895
        $administratorUserId = $this->generateId('user', 14);
896
        $anonymousUserId = $this->generateId('user', 10);
897
        /* BEGIN: Use Case */
898
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
899
        // demo installation
900
        // $administratorUserId is  the ID of the "Administrator" user in a eZ
901
        // Publish demo installation.
902
903
        $contentService = $repository->getContentService();
904
905
        // Load the user service
906
        $userService = $repository->getUserService();
907
908
        // Load the "Administrator" user
909
        $administratorUser = $userService->loadUser($administratorUserId);
910
911
        // Set anonymous user
912
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
913
914
        // This call will fail with a "UnauthorizedException"
915
        $contentService->loadContentDrafts($administratorUser);
916
        /* END: Use Case */
917
    }
918
919
    /**
920
     * Test for the updateContent() method.
@@ 1027-1051 (lines=25) @@
1024
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1025
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersions
1026
     */
1027
    public function testLoadVersionsThrowsUnauthorizedException()
1028
    {
1029
        $repository = $this->getRepository();
1030
1031
        $contentService = $repository->getContentService();
1032
1033
        $anonymousUserId = $this->generateId('user', 10);
1034
        /* BEGIN: Use Case */
1035
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1036
        // demo installation
1037
        $contentVersion2 = $this->createContentVersion2();
1038
1039
        // Get ContentInfo instance of version 2
1040
        $contentInfo = $contentVersion2->contentInfo;
1041
1042
        // Load the user service
1043
        $userService = $repository->getUserService();
1044
1045
        // Set anonymous user
1046
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
1047
1048
        // This call will fail with a "UnauthorizedException"
1049
        $contentService->loadVersions($contentInfo);
1050
        /* END: Use Case */
1051
    }
1052
1053
    /**
1054
     * Test for the copyContent() method.

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

@@ 661-685 (lines=25) @@
658
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo()
659
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
660
     */
661
    public function testLoadContentByVersionInfo()
662
    {
663
        $repository = $this->getRepository();
664
665
        $mediaFolderId = $this->generateId('object', 41);
666
        /* BEGIN: Use Case */
667
        // $mediaFolderId contains the ID of the "Media" folder
668
669
        $contentService = $repository->getContentService();
670
671
        // Load the ContentInfo for "Media" folder
672
        $contentInfo = $contentService->loadContentInfo($mediaFolderId);
673
674
        // Load the current VersionInfo
675
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
676
677
        // Now load the current content version for the info instance
678
        $content = $contentService->loadContentByVersionInfo($versionInfo);
679
        /* END: Use Case */
680
681
        $this->assertInstanceOf(
682
            '\\eZ\\Publish\\API\\Repository\\Values\\Content\\Content',
683
            $content
684
        );
685
    }
686
687
    /**
688
     * Test for the loadContent() method.

eZ/Publish/API/Repository/Tests/LocationServiceTest.php 3 locations

@@ 167-191 (lines=25) @@
164
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
165
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
166
     */
167
    public function testCreateLocationThrowsInvalidArgumentExceptionContentAlreadyBelowParent()
168
    {
169
        $repository = $this->getRepository();
170
171
        $contentId = $this->generateId('object', 11);
172
        $parentLocationId = $this->generateId('location', 5);
173
        /* BEGIN: Use Case */
174
        // $contentId is the ID of an existing content object
175
        // $parentLocationId is the ID of an existing location which already
176
        // has the content assigned to one of its descendant locations
177
        $contentService = $repository->getContentService();
178
        $locationService = $repository->getLocationService();
179
180
        // ContentInfo for "How to use eZ Publish"
181
        $contentInfo = $contentService->loadContentInfo($contentId);
182
183
        $locationCreate = $locationService->newLocationCreateStruct($parentLocationId);
184
185
        // Throws exception, since content is already located at "/1/2/107/110/"
186
        $locationService->createLocation(
187
            $contentInfo,
188
            $locationCreate
189
        );
190
        /* END: Use Case */
191
    }
192
193
    /**
194
     * Test for the createLocation() method.
@@ 200-224 (lines=25) @@
197
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testNewLocationCreateStruct
198
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
199
     */
200
    public function testCreateLocationThrowsInvalidArgumentExceptionParentIsSubLocationOfContent()
201
    {
202
        $repository = $this->getRepository();
203
204
        $contentId = $this->generateId('object', 4);
205
        $parentLocationId = $this->generateId('location', 12);
206
        /* BEGIN: Use Case */
207
        // $contentId is the ID of an existing content object
208
        // $parentLocationId is the ID of an existing location which is below a
209
        // location that is assigned to the content
210
        $contentService = $repository->getContentService();
211
        $locationService = $repository->getLocationService();
212
213
        // ContentInfo for "How to use eZ Publish"
214
        $contentInfo = $contentService->loadContentInfo($contentId);
215
216
        $locationCreate = $locationService->newLocationCreateStruct($parentLocationId);
217
218
        // Throws exception, since content is already located at "/1/2/"
219
        $locationService->createLocation(
220
            $contentInfo,
221
            $locationCreate
222
        );
223
        /* END: Use Case */
224
    }
225
226
    /**
227
     * Test for the createLocation() method.
@@ 497-515 (lines=19) @@
494
     * @see \eZ\Publish\API\Repository\LocationService::loadLocations()
495
     * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testCreateLocation
496
     */
497
    public function testLoadLocations()
498
    {
499
        $repository = $this->getRepository();
500
501
        $contentId = $this->generateId('object', 4);
502
        /* BEGIN: Use Case */
503
        // $contentId contains the ID of an existing content object
504
        $contentService = $repository->getContentService();
505
        $locationService = $repository->getLocationService();
506
507
        $contentInfo = $contentService->loadContentInfo($contentId);
508
509
        $locations = $locationService->loadLocations($contentInfo);
510
        /* END: Use Case */
511
512
        $this->assertInternalType('array', $locations);
513
514
        return $locations;
515
    }
516
517
    /**
518
     * Test for the loadLocations() method.

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

@@ 622-646 (lines=25) @@
619
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
620
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
621
     */
622
    public function testCanUserWithLimitationYes()
623
    {
624
        $repository = $this->getRepository();
625
626
        $imagesFolderId = $this->generateId('object', 49);
627
628
        /* BEGIN: Use Case */
629
        // $imagesFolderId contains the ID of the "Images" folder
630
631
        $user = $this->createUserVersion1();
632
633
        // Set created user as current user
634
        $repository->setCurrentUser($user);
635
636
        $contentService = $repository->getContentService();
637
638
        // Performing an action having necessary permissions will succeed
639
        $imagesFolder = $contentService->loadContent($imagesFolderId);
640
641
        // This call will return true
642
        $canUser = $repository->canUser('content', 'read', $imagesFolder);
643
        /* END: Use Case */
644
645
        $this->assertTrue($canUser);
646
    }
647
648
    /**
649
     * Test for the canUser() method.