| @@ 648-672 (lines=25) @@ | ||
| 645 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
|
| 646 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited |
|
| 647 | */ |
|
| 648 | public function testCanUserWithLimitationYes() |
|
| 649 | { |
|
| 650 | $repository = $this->getRepository(); |
|
| 651 | ||
| 652 | $imagesFolderId = $this->generateId('object', 49); |
|
| 653 | ||
| 654 | /* BEGIN: Use Case */ |
|
| 655 | // $imagesFolderId contains the ID of the "Images" folder |
|
| 656 | ||
| 657 | $user = $this->createUserVersion1(); |
|
| 658 | ||
| 659 | // Set created user as current user |
|
| 660 | $repository->setCurrentUser($user); |
|
| 661 | ||
| 662 | $contentService = $repository->getContentService(); |
|
| 663 | ||
| 664 | // Performing an action having necessary permissions will succeed |
|
| 665 | $imagesFolder = $contentService->loadContent($imagesFolderId); |
|
| 666 | ||
| 667 | // This call will return true |
|
| 668 | $canUser = $repository->canUser('content', 'read', $imagesFolder); |
|
| 669 | /* END: Use Case */ |
|
| 670 | ||
| 671 | $this->assertTrue($canUser); |
|
| 672 | } |
|
| 673 | ||
| 674 | /** |
|
| 675 | * Test for the canUser() method. |
|
| @@ 725-749 (lines=25) @@ | ||
| 722 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited |
|
| 723 | * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
|
| 724 | */ |
|
| 725 | public function testCanUserThrowsInvalidArgumentException() |
|
| 726 | { |
|
| 727 | $repository = $this->getRepository(); |
|
| 728 | ||
| 729 | $userGroupContentTypeId = $this->generateId('type', 3); |
|
| 730 | ||
| 731 | /* BEGIN: Use Case */ |
|
| 732 | // $userGroupContentTypeId contains the ID of the "UserGroup" ContentType |
|
| 733 | ||
| 734 | $user = $this->createUserVersion1(); |
|
| 735 | ||
| 736 | // Set created user as current user |
|
| 737 | $repository->setCurrentUser($user); |
|
| 738 | ||
| 739 | $contentTypeService = $repository->getContentTypeService(); |
|
| 740 | ||
| 741 | // Load the "UserGroup" ContentType |
|
| 742 | $userGroupContentType = $contentTypeService->loadContentType($userGroupContentTypeId); |
|
| 743 | ||
| 744 | // This call will throw "InvalidArgumentException" because $userGroupContentType |
|
| 745 | // is an instance of \eZ\Publish\API\Repository\Values\ContentType\ContentType, |
|
| 746 | // which can not be checked for user access |
|
| 747 | $canUser = $repository->canUser('content', 'create', $userGroupContentType); |
|
| 748 | /* END: Use Case */ |
|
| 749 | } |
|
| 750 | ||
| 751 | /** |
|
| 752 | * Test for the canUser() method. |
|
| @@ 995-1023 (lines=29) @@ | ||
| 992 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited |
|
| 993 | * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
|
| 994 | */ |
|
| 995 | public function testCanUserWithTargetThrowsInvalidArgumentException() |
|
| 996 | { |
|
| 997 | $repository = $this->getRepository(); |
|
| 998 | ||
| 999 | $homeId = $this->generateId('object', 57); |
|
| 1000 | ||
| 1001 | /* BEGIN: Use Case */ |
|
| 1002 | // $homeId contains the ID of the "Home" frontpage |
|
| 1003 | ||
| 1004 | $user = $this->createUserVersion1(); |
|
| 1005 | ||
| 1006 | // Set created user as current user |
|
| 1007 | $repository->setCurrentUser($user); |
|
| 1008 | ||
| 1009 | $contentService = $repository->getContentService(); |
|
| 1010 | ||
| 1011 | // Load the ContentInfo for "Home" frontpage |
|
| 1012 | $contentInfo = $contentService->loadContentInfo($homeId); |
|
| 1013 | ||
| 1014 | // This call will throw "InvalidArgumentException" because $targets argument must be an |
|
| 1015 | // instance of \eZ\Publish\API\Repository\Values\ValueObject class or an array of the same |
|
| 1016 | $canUser = $repository->canUser( |
|
| 1017 | 'content', |
|
| 1018 | 'remove', |
|
| 1019 | $contentInfo, |
|
| 1020 | new \stdClass() |
|
| 1021 | ); |
|
| 1022 | /* END: Use Case */ |
|
| 1023 | } |
|
| 1024 | ||
| 1025 | /** |
|
| 1026 | * Test for the canUser() method. |
|
| @@ 137-162 (lines=26) @@ | ||
| 134 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
|
| 135 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessWithAnonymousUserNo |
|
| 136 | */ |
|
| 137 | public function testHasAccessForCurrentUserNo() |
|
| 138 | { |
|
| 139 | $repository = $this->getRepository(); |
|
| 140 | ||
| 141 | $anonymousUserId = $this->generateId('user', 10); |
|
| 142 | ||
| 143 | /* BEGIN: Use Case */ |
|
| 144 | // $anonymousUserId is the ID of the "Anonymous" user in a eZ |
|
| 145 | // Publish demo installation. |
|
| 146 | ||
| 147 | $userService = $repository->getUserService(); |
|
| 148 | $permissionResolver = $repository->getPermissionResolver(); |
|
| 149 | ||
| 150 | // Load anonymous user |
|
| 151 | $anonymousUser = $userService->loadUser($anonymousUserId); |
|
| 152 | ||
| 153 | // Set anonymous user as current user reference |
|
| 154 | $permissionResolver->setCurrentUserReference($anonymousUser); |
|
| 155 | ||
| 156 | // This call will return false because anonymous user does not have access |
|
| 157 | // to content removal |
|
| 158 | $hasAccess = $permissionResolver->hasAccess('content', 'remove'); |
|
| 159 | /* END: Use Case */ |
|
| 160 | ||
| 161 | $this->assertFalse($hasAccess); |
|
| 162 | } |
|
| 163 | ||
| 164 | /** |
|
| 165 | * Test for the hasAccess() method. |
|
| @@ 200-223 (lines=24) @@ | ||
| 197 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testSetCurrentUserReference |
|
| 198 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessWithAdministratorUser |
|
| 199 | */ |
|
| 200 | public function testHasAccessForCurrentUserYes() |
|
| 201 | { |
|
| 202 | $repository = $this->getRepository(); |
|
| 203 | ||
| 204 | $administratorUserId = $this->generateId('user', 14); |
|
| 205 | ||
| 206 | /* BEGIN: Use Case */ |
|
| 207 | // $administratorUserId contains the ID of the administrator user |
|
| 208 | ||
| 209 | $userService = $repository->getUserService(); |
|
| 210 | $permissionResolver = $repository->getPermissionResolver(); |
|
| 211 | ||
| 212 | // Load administrator user |
|
| 213 | $administratorUser = $userService->loadUser($administratorUserId); |
|
| 214 | ||
| 215 | // Set administrator user as current user reference |
|
| 216 | $permissionResolver->setCurrentUserReference($administratorUser); |
|
| 217 | ||
| 218 | // This call will return true |
|
| 219 | $hasAccess = $permissionResolver->hasAccess('content', 'read'); |
|
| 220 | /* END: Use Case */ |
|
| 221 | ||
| 222 | $this->assertTrue($hasAccess); |
|
| 223 | } |
|
| 224 | ||
| 225 | /** |
|
| 226 | * Test for the hasAccess() method. |
|
| @@ 353-379 (lines=27) @@ | ||
| 350 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
|
| 351 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
|
| 352 | */ |
|
| 353 | public function testCanUserWithLimitationYes() |
|
| 354 | { |
|
| 355 | $repository = $this->getRepository(); |
|
| 356 | ||
| 357 | $imagesFolderId = $this->generateId('object', 49); |
|
| 358 | ||
| 359 | /* BEGIN: Use Case */ |
|
| 360 | // $imagesFolderId contains the ID of the "Images" folder |
|
| 361 | ||
| 362 | $user = $this->createUserVersion1(); |
|
| 363 | ||
| 364 | $permissionResolver = $repository->getPermissionResolver(); |
|
| 365 | ||
| 366 | // Set created user as current user reference |
|
| 367 | $permissionResolver->setCurrentUserReference($user); |
|
| 368 | ||
| 369 | $contentService = $repository->getContentService(); |
|
| 370 | ||
| 371 | // Performing an action having necessary permissions will succeed |
|
| 372 | $imagesFolder = $contentService->loadContent($imagesFolderId); |
|
| 373 | ||
| 374 | // This call will return true |
|
| 375 | $canUser = $permissionResolver->canUser('content', 'read', $imagesFolder); |
|
| 376 | /* END: Use Case */ |
|
| 377 | ||
| 378 | $this->assertTrue($canUser); |
|
| 379 | } |
|
| 380 | ||
| 381 | /** |
|
| 382 | * Test for the canUser() method. |
|
| @@ 434-460 (lines=27) @@ | ||
| 431 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
|
| 432 | * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
|
| 433 | */ |
|
| 434 | public function testCanUserThrowsInvalidArgumentException() |
|
| 435 | { |
|
| 436 | $repository = $this->getRepository(); |
|
| 437 | ||
| 438 | $userGroupContentTypeId = $this->generateId('type', 3); |
|
| 439 | ||
| 440 | /* BEGIN: Use Case */ |
|
| 441 | // $userGroupContentTypeId contains the ID of the "UserGroup" ContentType |
|
| 442 | ||
| 443 | $user = $this->createUserVersion1(); |
|
| 444 | ||
| 445 | $permissionResolver = $repository->getPermissionResolver(); |
|
| 446 | ||
| 447 | // Set created user as current user reference |
|
| 448 | $permissionResolver->setCurrentUserReference($user); |
|
| 449 | ||
| 450 | $contentTypeService = $repository->getContentTypeService(); |
|
| 451 | ||
| 452 | // Load the "UserGroup" ContentType |
|
| 453 | $userGroupContentType = $contentTypeService->loadContentType($userGroupContentTypeId); |
|
| 454 | ||
| 455 | // This call will throw "InvalidArgumentException" because $userGroupContentType |
|
| 456 | // is an instance of \eZ\Publish\API\Repository\Values\ContentType\ContentType, |
|
| 457 | // which can not be checked for user access |
|
| 458 | $canUser = $permissionResolver->canUser('content', 'create', $userGroupContentType); |
|
| 459 | /* END: Use Case */ |
|
| 460 | } |
|
| 461 | ||
| 462 | /** |
|
| 463 | * Test for the canUser() method. |
|