| @@ 309-331 (lines=23) @@ | ||
| 306 | * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
|
| 307 | * @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testRemoveFieldDefinition |
|
| 308 | */ |
|
| 309 | public function testRemoveFieldDefinitionThrowsUnauthorizedException() |
|
| 310 | { |
|
| 311 | $repository = $this->getRepository(); |
|
| 312 | $contentTypeService = $repository->getContentTypeService(); |
|
| 313 | ||
| 314 | $anonymousUserId = $this->generateId('user', 10); |
|
| 315 | /* BEGIN: Use Case */ |
|
| 316 | // $anonymousUserId is the ID of the "Anonymous" user in a eZ |
|
| 317 | // Publish demo installation. |
|
| 318 | $contentTypeDraft = $this->createContentTypeDraft(); |
|
| 319 | ||
| 320 | // Load the user service |
|
| 321 | $userService = $repository->getUserService(); |
|
| 322 | ||
| 323 | // Set anonymous user |
|
| 324 | $repository->setCurrentUser($userService->loadUser($anonymousUserId)); |
|
| 325 | ||
| 326 | $bodyField = $contentTypeDraft->getFieldDefinition('body'); |
|
| 327 | ||
| 328 | // This call will fail with a "UnauthorizedException" |
|
| 329 | $contentTypeService->removeFieldDefinition($contentTypeDraft, $bodyField); |
|
| 330 | /* END: Use Case */ |
|
| 331 | } |
|
| 332 | ||
| 333 | /** |
|
| 334 | * Test for the updateFieldDefinition() method. |
|
| @@ 425-446 (lines=22) @@ | ||
| 422 | * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
|
| 423 | * @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testCreateContentTypeDraft |
|
| 424 | */ |
|
| 425 | public function testCreateContentTypeDraftThrowsUnauthorizedException() |
|
| 426 | { |
|
| 427 | $repository = $this->getRepository(); |
|
| 428 | ||
| 429 | $anonymousUserId = $this->generateId('user', 10); |
|
| 430 | /* BEGIN: Use Case */ |
|
| 431 | // $anonymousUserId is the ID of the "Anonymous" user in a eZ |
|
| 432 | // Publish demo installation. |
|
| 433 | $contentTypeService = $repository->getContentTypeService(); |
|
| 434 | ||
| 435 | // Load the user service |
|
| 436 | $userService = $repository->getUserService(); |
|
| 437 | ||
| 438 | // Set anonymous user |
|
| 439 | $repository->setCurrentUser($userService->loadUser($anonymousUserId)); |
|
| 440 | ||
| 441 | $commentType = $contentTypeService->loadContentTypeByIdentifier('comment'); |
|
| 442 | ||
| 443 | // This call will fail with a "UnauthorizedException" |
|
| 444 | $contentTypeService->createContentTypeDraft($commentType); |
|
| 445 | /* END: Use Case */ |
|
| 446 | } |
|
| 447 | ||
| 448 | /** |
|
| 449 | * Test for the deleteContentType() method. |
|
| @@ 455-476 (lines=22) @@ | ||
| 452 | * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
|
| 453 | * @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testDeleteContentType |
|
| 454 | */ |
|
| 455 | public function testDeleteContentTypeThrowsUnauthorizedException() |
|
| 456 | { |
|
| 457 | $repository = $this->getRepository(); |
|
| 458 | ||
| 459 | $anonymousUserId = $this->generateId('user', 10); |
|
| 460 | /* BEGIN: Use Case */ |
|
| 461 | // $anonymousUserId is the ID of the "Anonymous" user in a eZ |
|
| 462 | // Publish demo installation. |
|
| 463 | $contentTypeService = $repository->getContentTypeService(); |
|
| 464 | ||
| 465 | // Load the user service |
|
| 466 | $userService = $repository->getUserService(); |
|
| 467 | ||
| 468 | // Set anonymous user |
|
| 469 | $repository->setCurrentUser($userService->loadUser($anonymousUserId)); |
|
| 470 | ||
| 471 | $commentType = $contentTypeService->loadContentTypeByIdentifier('comment'); |
|
| 472 | ||
| 473 | // This call will fail with a "UnauthorizedException" |
|
| 474 | $contentTypeService->deleteContentType($commentType); |
|
| 475 | /* END: Use Case */ |
|
| 476 | } |
|
| 477 | ||
| 478 | /** |
|
| 479 | * Test for the copyContentType() method. |
|
| @@ 485-506 (lines=22) @@ | ||
| 482 | * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
|
| 483 | * @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testCopyContentType |
|
| 484 | */ |
|
| 485 | public function testCopyContentTypeThrowsUnauthorizedException() |
|
| 486 | { |
|
| 487 | $repository = $this->getRepository(); |
|
| 488 | ||
| 489 | $anonymousUserId = $this->generateId('user', 10); |
|
| 490 | /* BEGIN: Use Case */ |
|
| 491 | // $anonymousUserId is the ID of the "Anonymous" user in a eZ |
|
| 492 | // Publish demo installation. |
|
| 493 | $contentTypeService = $repository->getContentTypeService(); |
|
| 494 | ||
| 495 | // Load the user service |
|
| 496 | $userService = $repository->getUserService(); |
|
| 497 | ||
| 498 | // Set anonymous user |
|
| 499 | $repository->setCurrentUser($userService->loadUser($anonymousUserId)); |
|
| 500 | ||
| 501 | $commentType = $contentTypeService->loadContentTypeByIdentifier('comment'); |
|
| 502 | ||
| 503 | // This call will fail with a "UnauthorizedException" |
|
| 504 | $contentTypeService->copyContentType($commentType); |
|
| 505 | /* END: Use Case */ |
|
| 506 | } |
|
| 507 | ||
| 508 | /** |
|
| 509 | * Test for the assignContentTypeGroup() method. |
|
| @@ 138-163 (lines=26) @@ | ||
| 135 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
|
| 136 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessWithAnonymousUserNo |
|
| 137 | */ |
|
| 138 | public function testHasAccessForCurrentUserNo() |
|
| 139 | { |
|
| 140 | $repository = $this->getRepository(); |
|
| 141 | ||
| 142 | $anonymousUserId = $this->generateId('user', 10); |
|
| 143 | ||
| 144 | /* BEGIN: Use Case */ |
|
| 145 | // $anonymousUserId is the ID of the "Anonymous" user in a eZ |
|
| 146 | // Publish demo installation. |
|
| 147 | ||
| 148 | $userService = $repository->getUserService(); |
|
| 149 | $permissionResolver = $repository->getPermissionResolver(); |
|
| 150 | ||
| 151 | // Load anonymous user |
|
| 152 | $anonymousUser = $userService->loadUser($anonymousUserId); |
|
| 153 | ||
| 154 | // Set anonymous user as current user reference |
|
| 155 | $permissionResolver->setCurrentUserReference($anonymousUser); |
|
| 156 | ||
| 157 | // This call will return false because anonymous user does not have access |
|
| 158 | // to content removal |
|
| 159 | $hasAccess = $permissionResolver->hasAccess('content', 'remove'); |
|
| 160 | /* END: Use Case */ |
|
| 161 | ||
| 162 | $this->assertFalse($hasAccess); |
|
| 163 | } |
|
| 164 | ||
| 165 | /** |
|
| 166 | * Test for the hasAccess() method. |
|
| @@ 201-224 (lines=24) @@ | ||
| 198 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testSetCurrentUserReference |
|
| 199 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessWithAdministratorUser |
|
| 200 | */ |
|
| 201 | public function testHasAccessForCurrentUserYes() |
|
| 202 | { |
|
| 203 | $repository = $this->getRepository(); |
|
| 204 | ||
| 205 | $administratorUserId = $this->generateId('user', 14); |
|
| 206 | ||
| 207 | /* BEGIN: Use Case */ |
|
| 208 | // $administratorUserId contains the ID of the administrator user |
|
| 209 | ||
| 210 | $userService = $repository->getUserService(); |
|
| 211 | $permissionResolver = $repository->getPermissionResolver(); |
|
| 212 | ||
| 213 | // Load administrator user |
|
| 214 | $administratorUser = $userService->loadUser($administratorUserId); |
|
| 215 | ||
| 216 | // Set administrator user as current user reference |
|
| 217 | $permissionResolver->setCurrentUserReference($administratorUser); |
|
| 218 | ||
| 219 | // This call will return true |
|
| 220 | $hasAccess = $permissionResolver->hasAccess('content', 'read'); |
|
| 221 | /* END: Use Case */ |
|
| 222 | ||
| 223 | $this->assertTrue($hasAccess); |
|
| 224 | } |
|
| 225 | ||
| 226 | /** |
|
| 227 | * Test for the hasAccess() method. |
|
| @@ 354-380 (lines=27) @@ | ||
| 351 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
|
| 352 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
|
| 353 | */ |
|
| 354 | public function testCanUserWithLimitationYes() |
|
| 355 | { |
|
| 356 | $repository = $this->getRepository(); |
|
| 357 | ||
| 358 | $imagesFolderId = $this->generateId('object', 49); |
|
| 359 | ||
| 360 | /* BEGIN: Use Case */ |
|
| 361 | // $imagesFolderId contains the ID of the "Images" folder |
|
| 362 | ||
| 363 | $user = $this->createUserVersion1(); |
|
| 364 | ||
| 365 | $permissionResolver = $repository->getPermissionResolver(); |
|
| 366 | ||
| 367 | // Set created user as current user reference |
|
| 368 | $permissionResolver->setCurrentUserReference($user); |
|
| 369 | ||
| 370 | $contentService = $repository->getContentService(); |
|
| 371 | ||
| 372 | // Performing an action having necessary permissions will succeed |
|
| 373 | $imagesFolder = $contentService->loadContent($imagesFolderId); |
|
| 374 | ||
| 375 | // This call will return true |
|
| 376 | $canUser = $permissionResolver->canUser('content', 'read', $imagesFolder); |
|
| 377 | /* END: Use Case */ |
|
| 378 | ||
| 379 | $this->assertTrue($canUser); |
|
| 380 | } |
|
| 381 | ||
| 382 | /** |
|
| 383 | * Test for the canUser() method. |
|
| @@ 435-461 (lines=27) @@ | ||
| 432 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
|
| 433 | * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
|
| 434 | */ |
|
| 435 | public function testCanUserThrowsInvalidArgumentException() |
|
| 436 | { |
|
| 437 | $repository = $this->getRepository(); |
|
| 438 | ||
| 439 | $userGroupContentTypeId = $this->generateId('type', 3); |
|
| 440 | ||
| 441 | /* BEGIN: Use Case */ |
|
| 442 | // $userGroupContentTypeId contains the ID of the "UserGroup" ContentType |
|
| 443 | ||
| 444 | $user = $this->createUserVersion1(); |
|
| 445 | ||
| 446 | $permissionResolver = $repository->getPermissionResolver(); |
|
| 447 | ||
| 448 | // Set created user as current user reference |
|
| 449 | $permissionResolver->setCurrentUserReference($user); |
|
| 450 | ||
| 451 | $contentTypeService = $repository->getContentTypeService(); |
|
| 452 | ||
| 453 | // Load the "UserGroup" ContentType |
|
| 454 | $userGroupContentType = $contentTypeService->loadContentType($userGroupContentTypeId); |
|
| 455 | ||
| 456 | // This call will throw "InvalidArgumentException" because $userGroupContentType |
|
| 457 | // is an instance of \eZ\Publish\API\Repository\Values\ContentType\ContentType, |
|
| 458 | // which can not be checked for user access |
|
| 459 | $canUser = $permissionResolver->canUser('content', 'create', $userGroupContentType); |
|
| 460 | /* END: Use Case */ |
|
| 461 | } |
|
| 462 | ||
| 463 | /** |
|
| 464 | * Test for the canUser() method. |
|
| @@ 104-123 (lines=20) @@ | ||
| 101 | * @see \eZ\Publish\API\Repository\URLService::loadById |
|
| 102 | * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
|
| 103 | */ |
|
| 104 | public function testLoadByUrlThrowsUnauthorizedException() |
|
| 105 | { |
|
| 106 | $repository = $this->getRepository(); |
|
| 107 | ||
| 108 | $anonymousUserId = $this->generateId('user', 10); |
|
| 109 | $url = '/content/view/sitemap/2'; |
|
| 110 | ||
| 111 | /* BEGIN: Use Case */ |
|
| 112 | // $anonymousUserId is the ID of the "Anonymous" user in a eZ |
|
| 113 | // Publish demo installation. |
|
| 114 | ||
| 115 | $userService = $repository->getUserService(); |
|
| 116 | $urlService = $repository->getURLService(); |
|
| 117 | ||
| 118 | $repository->getPermissionResolver()->setCurrentUserReference($userService->loadUser($anonymousUserId)); |
|
| 119 | ||
| 120 | // This call will fail with an UnauthorizedException |
|
| 121 | $urlService->loadByUrl($url); |
|
| 122 | /* END: Use Case */ |
|
| 123 | } |
|
| 124 | } |
|
| 125 | ||
| @@ 441-463 (lines=23) @@ | ||
| 438 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
|
| 439 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessWithAnonymousUserNo |
|
| 440 | */ |
|
| 441 | public function testHasAccessForCurrentUserNo() |
|
| 442 | { |
|
| 443 | $repository = $this->getRepository(); |
|
| 444 | ||
| 445 | $anonymousUserId = $this->generateId('user', 10); |
|
| 446 | /* BEGIN: Use Case */ |
|
| 447 | // $anonymousUserId is the ID of the "Anonymous" user in a eZ |
|
| 448 | // Publish demo installation. |
|
| 449 | $userService = $repository->getUserService(); |
|
| 450 | ||
| 451 | // Load anonymous user |
|
| 452 | $anonymousUser = $userService->loadUser($anonymousUserId); |
|
| 453 | ||
| 454 | // Set anonymous user as current user |
|
| 455 | $repository->setCurrentUser($anonymousUser); |
|
| 456 | ||
| 457 | // This call will return false because anonymous user does not have access |
|
| 458 | // to content removal |
|
| 459 | $hasAccess = $repository->hasAccess('content', 'remove'); |
|
| 460 | /* END: Use Case */ |
|
| 461 | ||
| 462 | $this->assertFalse($hasAccess); |
|
| 463 | } |
|
| 464 | ||
| 465 | /** |
|
| 466 | * Test for the hasAccess() method. |
|
| @@ 500-522 (lines=23) @@ | ||
| 497 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testSetCurrentUser |
|
| 498 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessWithAdministratorUser |
|
| 499 | */ |
|
| 500 | public function testHasAccessForCurrentUserYes() |
|
| 501 | { |
|
| 502 | $repository = $this->getRepository(); |
|
| 503 | ||
| 504 | $administratorUserId = $this->generateId('user', 14); |
|
| 505 | ||
| 506 | /* BEGIN: Use Case */ |
|
| 507 | // $administratorUserId contains the ID of the administrator user |
|
| 508 | ||
| 509 | $userService = $repository->getUserService(); |
|
| 510 | ||
| 511 | // Load administrator user |
|
| 512 | $administratorUser = $userService->loadUser($administratorUserId); |
|
| 513 | ||
| 514 | // Set administrator user as current user |
|
| 515 | $repository->setCurrentUser($administratorUser); |
|
| 516 | ||
| 517 | // This call will return true |
|
| 518 | $hasAccess = $repository->hasAccess('content', 'read'); |
|
| 519 | /* END: Use Case */ |
|
| 520 | ||
| 521 | $this->assertTrue($hasAccess); |
|
| 522 | } |
|
| 523 | ||
| 524 | /** |
|
| 525 | * Test for the hasAccess() method. |
|