Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 20 | class PermissionResolverTest extends BaseTest |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Test for the getCurrentUser() method. |
||
| 24 | * |
||
| 25 | * @see \eZ\Publish\API\Repository\PermissionResolver::getCurrentUserReference() |
||
| 26 | */ |
||
| 27 | public function testGetCurrentUserReferenceReturnsAnonymousUserReference() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Test for the setCurrentUser() method. |
||
| 56 | * |
||
| 57 | * @see \eZ\Publish\API\Repository\PermissionResolver::setCurrentUserReference() |
||
| 58 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
| 59 | */ |
||
| 60 | View Code Duplication | public function testSetCurrentUserReference() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Test for the hasAccess() method. |
||
| 98 | * |
||
| 99 | * @see \eZ\Publish\API\Repository\PermissionResolver::hasAccess() |
||
| 100 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
| 101 | */ |
||
| 102 | public function testHasAccessWithAnonymousUserNo() |
||
| 103 | { |
||
| 104 | $repository = $this->getRepository(); |
||
| 105 | |||
| 106 | $anonymousUserId = $this->generateId('user', 10); |
||
| 107 | |||
| 108 | /* BEGIN: Use Case */ |
||
| 109 | // $anonymousUserId is the ID of the "Anonymous" user in a eZ |
||
| 110 | // Publish demo installation. |
||
| 111 | |||
| 112 | $userService = $repository->getUserService(); |
||
| 113 | $permissionResolver = $repository->getPermissionResolver(); |
||
| 114 | |||
| 115 | // Load anonymous user |
||
| 116 | $anonymousUser = $userService->loadUser($anonymousUserId); |
||
| 117 | |||
| 118 | // This call will return false because anonymous user does not have access |
||
| 119 | // to content removal |
||
| 120 | $hasAccess = $permissionResolver->hasAccess('content', 'remove', $anonymousUser); |
||
| 121 | /* END: Use Case */ |
||
| 122 | |||
| 123 | $this->assertFalse($hasAccess); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Test for the hasAccess() method. |
||
| 128 | * |
||
| 129 | * @see \eZ\Publish\API\Repository\PermissionResolver::hasAccess() |
||
| 130 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
| 131 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessWithAnonymousUserNo |
||
| 132 | */ |
||
| 133 | View Code Duplication | public function testHasAccessForCurrentUserNo() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Test for the hasAccess() method. |
||
| 162 | * |
||
| 163 | * @see \eZ\Publish\API\Repository\PermissionResolver::hasAccess() |
||
| 164 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
| 165 | */ |
||
| 166 | public function testHasAccessWithAdministratorUser() |
||
| 167 | { |
||
| 168 | $repository = $this->getRepository(); |
||
| 169 | |||
| 170 | $administratorUserId = $this->generateId('user', 14); |
||
| 171 | |||
| 172 | /* BEGIN: Use Case */ |
||
| 173 | // $administratorUserId contains the ID of the administrator user |
||
| 174 | |||
| 175 | $userService = $repository->getUserService(); |
||
| 176 | $permissionResolver = $repository->getPermissionResolver(); |
||
| 177 | |||
| 178 | // Load administrator user |
||
| 179 | $administratorUser = $userService->loadUser($administratorUserId); |
||
| 180 | |||
| 181 | // This call will return true |
||
| 182 | $hasAccess = $permissionResolver->hasAccess('content', 'read', $administratorUser); |
||
| 183 | /* END: Use Case */ |
||
| 184 | |||
| 185 | $this->assertTrue($hasAccess); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Test for the hasAccess() method. |
||
| 190 | * |
||
| 191 | * @see \eZ\Publish\API\Repository\PermissionResolver::hasAccess() |
||
| 192 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
| 193 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testSetCurrentUserReference |
||
| 194 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessWithAdministratorUser |
||
| 195 | */ |
||
| 196 | View Code Duplication | public function testHasAccessForCurrentUserYes() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Test for the hasAccess() method. |
||
| 223 | * |
||
| 224 | * @see \eZ\Publish\API\Repository\PermissionResolver::hasAccess() |
||
| 225 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
| 226 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testSetCurrentUserReference |
||
| 227 | */ |
||
| 228 | View Code Duplication | public function testHasAccessLimited() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Test for the canUser() method. |
||
| 254 | * |
||
| 255 | * @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
||
| 256 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
| 257 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
||
| 258 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessForCurrentUserNo |
||
| 259 | * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
| 260 | */ |
||
| 261 | public function testCanUserForAnonymousUserNo() |
||
| 262 | { |
||
| 263 | $repository = $this->getRepository(); |
||
| 264 | |||
| 265 | $homeId = $this->generateId('object', 57); |
||
| 266 | |||
| 267 | $anonymousUserId = $this->generateId('user', 10); |
||
| 268 | /* BEGIN: Use Case */ |
||
| 269 | // $anonymousUserId is the ID of the "Anonymous" user in a eZ |
||
| 270 | // Publish demo installation. |
||
| 271 | // $homeId contains the ID of the "Home" frontpage |
||
| 272 | |||
| 273 | $contentService = $repository->getContentService(); |
||
| 274 | $userService = $repository->getUserService(); |
||
| 275 | $permissionResolver = $repository->getPermissionResolver(); |
||
| 276 | |||
| 277 | // Load anonymous user |
||
| 278 | $anonymousUser = $userService->loadUser($anonymousUserId); |
||
| 279 | |||
| 280 | // Set anonymous user as current user reference |
||
| 281 | $permissionResolver->setCurrentUserReference($anonymousUser); |
||
| 282 | |||
| 283 | // Load the ContentInfo for "Home" frontpage |
||
| 284 | $contentInfo = $contentService->loadContentInfo($homeId); |
||
| 285 | |||
| 286 | // This call will return false because anonymous user does not have access |
||
| 287 | // to content removal and hence no permission to remove given content |
||
| 288 | $canUser = $permissionResolver->canUser('content', 'remove', $contentInfo); |
||
| 289 | |||
| 290 | // Performing an action without necessary permissions will fail with "UnauthorizedException" |
||
| 291 | if (!$canUser) { |
||
| 292 | $contentService->deleteContent($contentInfo); |
||
| 293 | } |
||
| 294 | /* END: Use Case */ |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Test for the canUser() method. |
||
| 299 | * |
||
| 300 | * @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
||
| 301 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
| 302 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
||
| 303 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessForCurrentUserYes |
||
| 304 | * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 305 | */ |
||
| 306 | View Code Duplication | public function testCanUserForAdministratorUser() |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Test for the canUser() method. |
||
| 343 | * |
||
| 344 | * @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
||
| 345 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
| 346 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
||
| 347 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
||
| 348 | */ |
||
| 349 | View Code Duplication | 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. |
||
| 379 | * |
||
| 380 | * @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
||
| 381 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
| 382 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
||
| 383 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
||
| 384 | * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
| 385 | */ |
||
| 386 | public function testCanUserWithLimitationNo() |
||
| 387 | { |
||
| 388 | $repository = $this->getRepository(); |
||
| 389 | |||
| 390 | $administratorUserId = $this->generateId('user', 14); |
||
| 391 | |||
| 392 | /* BEGIN: Use Case */ |
||
| 393 | // $administratorUserId contains the ID of the administrator user |
||
| 394 | |||
| 395 | $user = $this->createUserVersion1(); |
||
| 396 | |||
| 397 | $permissionResolver = $repository->getPermissionResolver(); |
||
| 398 | |||
| 399 | // Set created user as current user reference |
||
| 400 | $permissionResolver->setCurrentUserReference($user); |
||
| 401 | |||
| 402 | $userService = $repository->getUserService(); |
||
| 403 | |||
| 404 | // Load administrator user using UserService, this does not check for permissions |
||
| 405 | $administratorUser = $userService->loadUser($administratorUserId); |
||
| 406 | |||
| 407 | // This call will return false as user with Editor role does not have |
||
| 408 | // permission to read "Users" subtree |
||
| 409 | $canUser = $permissionResolver->canUser('content', 'read', $administratorUser); |
||
| 410 | |||
| 411 | $contentService = $repository->getContentService(); |
||
| 412 | |||
| 413 | // Performing an action without necessary permissions will fail with "UnauthorizedException" |
||
| 414 | if (!$canUser) { |
||
| 415 | $content = $contentService->loadContent($administratorUserId); |
||
|
|
|||
| 416 | } |
||
| 417 | /* END: Use Case */ |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Test for the canUser() method. |
||
| 422 | * |
||
| 423 | * @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
||
| 424 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
| 425 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService |
||
| 426 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testSetCurrentUserReference |
||
| 427 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
||
| 428 | * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 429 | */ |
||
| 430 | View Code Duplication | 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. |
||
| 460 | * |
||
| 461 | * @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
||
| 462 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
| 463 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
||
| 464 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService |
||
| 465 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
||
| 466 | */ |
||
| 467 | View Code Duplication | public function testCanUserWithTargetYes() |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Test for the canUser() method. |
||
| 521 | * |
||
| 522 | * @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
||
| 523 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
| 524 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
||
| 525 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService |
||
| 526 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
||
| 527 | * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
| 528 | */ |
||
| 529 | View Code Duplication | public function testCanUserWithTargetNo() |
|
| 577 | |||
| 578 | /** |
||
| 579 | * Test for the canUser() method. |
||
| 580 | * |
||
| 581 | * @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
||
| 582 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
| 583 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
||
| 584 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService |
||
| 585 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
||
| 586 | */ |
||
| 587 | View Code Duplication | public function testCanUserWithMultipleTargetsYes() |
|
| 639 | |||
| 640 | /** |
||
| 641 | * Test for the canUser() method. |
||
| 642 | * |
||
| 643 | * @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
||
| 644 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
| 645 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
||
| 646 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService |
||
| 647 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
||
| 648 | * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
| 649 | */ |
||
| 650 | View Code Duplication | public function testCanUserWithMultipleTargetsNo() |
|
| 699 | |||
| 700 | /** |
||
| 701 | * Test for the canUser() method. |
||
| 702 | * |
||
| 703 | * @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
||
| 704 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
| 705 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
||
| 706 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService |
||
| 707 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetURLAliasService |
||
| 708 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testSetCurrentUserReference |
||
| 709 | * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
||
| 710 | * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 711 | */ |
||
| 712 | View Code Duplication | public function testCanUserWithTargetThrowsInvalidArgumentException() |
|
| 747 | |||
| 748 | /** |
||
| 749 | * Test for the canUser() method. |
||
| 750 | * |
||
| 751 | * @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
||
| 752 | * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
| 753 | */ |
||
| 754 | public function testCanUserThrowsBadStateException() |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Test PermissionResolver::canUser for Users with different Limitations. |
||
| 763 | * |
||
| 764 | * @covers \eZ\Publish\API\Repository\PermissionResolver::canUser |
||
| 765 | * |
||
| 766 | * @dataProvider getDataForTestCanUserWithLimitations |
||
| 767 | * |
||
| 768 | * @param \eZ\Publish\API\Repository\Values\User\Limitation $limitation |
||
| 769 | * @param string $module |
||
| 770 | * @param string $function |
||
| 771 | * @param \eZ\Publish\API\Repository\Values\ValueObject $object |
||
| 772 | * @param array $targets |
||
| 773 | * @param bool $expectedResult expected result of canUser check |
||
| 774 | * |
||
| 775 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
| 776 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 777 | */ |
||
| 778 | public function testCanUserWithLimitations( |
||
| 809 | |||
| 810 | /** |
||
| 811 | * Data provider for testCanUserWithLimitations. |
||
| 812 | * @see testCanUserWithLimitations |
||
| 813 | * |
||
| 814 | * @return array |
||
| 815 | * |
||
| 816 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 817 | */ |
||
| 818 | public function getDataForTestCanUserWithLimitations() |
||
| 865 | } |
||
| 866 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.