Code Duplication    Length = 24-29 lines in 7 locations

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

@@ 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.
@@ 699-723 (lines=25) @@
696
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
697
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
698
     */
699
    public function testCanUserThrowsInvalidArgumentException()
700
    {
701
        $repository = $this->getRepository();
702
703
        $userGroupContentTypeId = $this->generateId('type', 3);
704
705
        /* BEGIN: Use Case */
706
        // $userGroupContentTypeId contains the ID of the "UserGroup" ContentType
707
708
        $user = $this->createUserVersion1();
709
710
        // Set created user as current user
711
        $repository->setCurrentUser($user);
712
713
        $contentTypeService = $repository->getContentTypeService();
714
715
        // Load the "UserGroup" ContentType
716
        $userGroupContentType = $contentTypeService->loadContentType($userGroupContentTypeId);
717
718
        // This call will throw "InvalidArgumentException" because $userGroupContentType
719
        // is an instance of \eZ\Publish\API\Repository\Values\ContentType\ContentType,
720
        // which can not be checked for user access
721
        $canUser = $repository->canUser('content', 'create', $userGroupContentType);
722
        /* END: Use Case */
723
    }
724
725
    /**
726
     * Test for the canUser() method.
@@ 969-997 (lines=29) @@
966
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
967
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
968
     */
969
    public function testCanUserWithTargetThrowsInvalidArgumentException()
970
    {
971
        $repository = $this->getRepository();
972
973
        $homeId = $this->generateId('object', 57);
974
975
        /* BEGIN: Use Case */
976
        // $homeId contains the ID of the "Home" frontpage
977
978
        $user = $this->createUserVersion1();
979
980
        // Set created user as current user
981
        $repository->setCurrentUser($user);
982
983
        $contentService = $repository->getContentService();
984
985
        // Load the ContentInfo for "Home" frontpage
986
        $contentInfo = $contentService->loadContentInfo($homeId);
987
988
        // This call will throw "InvalidArgumentException" because $targets argument must be an
989
        // instance of \eZ\Publish\API\Repository\Values\ValueObject class or an array of the same
990
        $canUser = $repository->canUser(
991
            'content',
992
            'remove',
993
            $contentInfo,
994
            new \stdClass()
995
        );
996
        /* END: Use Case */
997
    }
998
999
    /**
1000
     * Test for the canUser() method.

eZ/Publish/API/Repository/Tests/PermissionResolverTest.php 4 locations

@@ 133-158 (lines=26) @@
130
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
131
     * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessWithAnonymousUserNo
132
     */
133
    public function testHasAccessForCurrentUserNo()
134
    {
135
        $repository = $this->getRepository();
136
137
        $anonymousUserId = $this->generateId('user', 10);
138
139
        /* BEGIN: Use Case */
140
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
141
        // Publish demo installation.
142
143
        $userService = $repository->getUserService();
144
        $permissionResolver = $repository->getPermissionResolver();
145
146
        // Load anonymous user
147
        $anonymousUser = $userService->loadUser($anonymousUserId);
148
149
        // Set anonymous user as current user reference
150
        $permissionResolver->setCurrentUserReference($anonymousUser);
151
152
        // This call will return false because anonymous user does not have access
153
        // to content removal
154
        $hasAccess = $permissionResolver->hasAccess('content', 'remove');
155
        /* END: Use Case */
156
157
        $this->assertFalse($hasAccess);
158
    }
159
160
    /**
161
     * Test for the hasAccess() method.
@@ 196-219 (lines=24) @@
193
     * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testSetCurrentUserReference
194
     * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessWithAdministratorUser
195
     */
196
    public function testHasAccessForCurrentUserYes()
197
    {
198
        $repository = $this->getRepository();
199
200
        $administratorUserId = $this->generateId('user', 14);
201
202
        /* BEGIN: Use Case */
203
        // $administratorUserId contains the ID of the administrator user
204
205
        $userService = $repository->getUserService();
206
        $permissionResolver = $repository->getPermissionResolver();
207
208
        // Load administrator user
209
        $administratorUser = $userService->loadUser($administratorUserId);
210
211
        // Set administrator user as current user reference
212
        $permissionResolver->setCurrentUserReference($administratorUser);
213
214
        // This call will return true
215
        $hasAccess = $permissionResolver->hasAccess('content', 'read');
216
        /* END: Use Case */
217
218
        $this->assertTrue($hasAccess);
219
    }
220
221
    /**
222
     * Test for the hasAccess() method.
@@ 349-375 (lines=27) @@
346
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
347
     * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited
348
     */
349
    public function testCanUserWithLimitationYes()
350
    {
351
        $repository = $this->getRepository();
352
353
        $imagesFolderId = $this->generateId('object', 49);
354
355
        /* BEGIN: Use Case */
356
        // $imagesFolderId contains the ID of the "Images" folder
357
358
        $user = $this->createUserVersion1();
359
360
        $permissionResolver = $repository->getPermissionResolver();
361
362
        // Set created user as current user reference
363
        $permissionResolver->setCurrentUserReference($user);
364
365
        $contentService = $repository->getContentService();
366
367
        // Performing an action having necessary permissions will succeed
368
        $imagesFolder = $contentService->loadContent($imagesFolderId);
369
370
        // This call will return true
371
        $canUser = $permissionResolver->canUser('content', 'read', $imagesFolder);
372
        /* END: Use Case */
373
374
        $this->assertTrue($canUser);
375
    }
376
377
    /**
378
     * Test for the canUser() method.
@@ 430-456 (lines=27) @@
427
     * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited
428
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
429
     */
430
    public function testCanUserThrowsInvalidArgumentException()
431
    {
432
        $repository = $this->getRepository();
433
434
        $userGroupContentTypeId = $this->generateId('type', 3);
435
436
        /* BEGIN: Use Case */
437
        // $userGroupContentTypeId contains the ID of the "UserGroup" ContentType
438
439
        $user = $this->createUserVersion1();
440
441
        $permissionResolver = $repository->getPermissionResolver();
442
443
        // Set created user as current user reference
444
        $permissionResolver->setCurrentUserReference($user);
445
446
        $contentTypeService = $repository->getContentTypeService();
447
448
        // Load the "UserGroup" ContentType
449
        $userGroupContentType = $contentTypeService->loadContentType($userGroupContentTypeId);
450
451
        // This call will throw "InvalidArgumentException" because $userGroupContentType
452
        // is an instance of \eZ\Publish\API\Repository\Values\ContentType\ContentType,
453
        // which can not be checked for user access
454
        $canUser = $permissionResolver->canUser('content', 'create', $userGroupContentType);
455
        /* END: Use Case */
456
    }
457
458
    /**
459
     * Test for the canUser() method.