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