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 RoleServiceAuthorizationTest extends BaseTest |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Test for the createRole() method. |
||
| 24 | * |
||
| 25 | * @see \eZ\Publish\API\Repository\RoleService::createRole() |
||
| 26 | * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole |
||
| 27 | * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
||
| 28 | */ |
||
| 29 | public function testCreateRoleThrowsUnauthorizedException() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Test for the loadRole() method. |
||
| 55 | * |
||
| 56 | * @see \eZ\Publish\API\Repository\RoleService::loadRole() |
||
| 57 | * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRole |
||
| 58 | * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
||
| 59 | */ |
||
| 60 | public function testLoadRoleThrowsUnauthorizedException() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Test for the loadRoleByIdentifier() method. |
||
| 83 | * |
||
| 84 | * @see \eZ\Publish\API\Repository\RoleService::loadRoleByIdentifier() |
||
| 85 | * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier |
||
| 86 | * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
||
| 87 | */ |
||
| 88 | public function testLoadRoleByIdentifierThrowsUnauthorizedException() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Test for the loadRoles() method. |
||
| 111 | * |
||
| 112 | * @see \eZ\Publish\API\Repository\RoleService::loadRoles() |
||
| 113 | */ |
||
| 114 | View Code Duplication | public function testLoadRolesLoadsEmptyListForAnonymousUser() |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Test for the loadRoles() method. |
||
| 134 | * |
||
| 135 | * @see \eZ\Publish\API\Repository\RoleService::loadRoles() |
||
| 136 | */ |
||
| 137 | public function testLoadRolesForUserWithSubtreeLimitation() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Test for the deleteRole() method. |
||
| 164 | * |
||
| 165 | * @see \eZ\Publish\API\Repository\RoleService::deleteRole() |
||
| 166 | * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testDeleteRole |
||
| 167 | * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
||
| 168 | */ |
||
| 169 | public function testDeleteRoleThrowsUnauthorizedException() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Test for the updatePolicy() method. |
||
| 192 | * |
||
| 193 | * @see \eZ\Publish\API\Repository\RoleService::updatePolicyByRoleDraft() |
||
| 194 | * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdatePolicyByRoleDraft |
||
| 195 | * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
||
| 196 | */ |
||
| 197 | public function testUpdatePolicyByRoleDraftThrowsUnauthorizedException() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Test for the removePolicy() method. |
||
| 239 | * |
||
| 240 | * @see \eZ\Publish\API\Repository\RoleService::removePolicy() |
||
| 241 | * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testRemovePolicyByRoleDraft |
||
| 242 | * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
||
| 243 | */ |
||
| 244 | public function testRemovePolicyThrowsUnauthorizedException() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Test for the removePolicyByRoleDraft() method. |
||
| 281 | * |
||
| 282 | * @see \eZ\Publish\API\Repository\RoleService::removePolicyByRoleDraft() |
||
| 283 | * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testRemovePolicyByRoleDraft |
||
| 284 | * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
||
| 285 | */ |
||
| 286 | public function testDeletePolicyThrowsUnauthorizedException() |
||
| 287 | { |
||
| 288 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
||
| 289 | |||
| 290 | $repository = $this->getRepository(); |
||
| 291 | $roleService = $repository->getRoleService(); |
||
| 292 | $permissionResolver = $repository->getPermissionResolver(); |
||
| 293 | |||
| 294 | /* BEGIN: Use Case */ |
||
| 295 | $user = $this->createUserVersion1(); |
||
| 296 | |||
| 297 | $role = $this->createRole(); |
||
| 298 | |||
| 299 | // Get first role policy |
||
| 300 | $roleDraft = $roleService->createRoleDraft($role); |
||
| 301 | $policiesDrafts = $roleDraft->getPolicies(); |
||
| 302 | $policyDraft = reset($policiesDrafts); |
||
| 303 | |||
| 304 | // Set "Editor" user as current user. |
||
| 305 | $permissionResolver->setCurrentUserReference($user); |
||
| 306 | |||
| 307 | // This call will fail with an "UnauthorizedException" |
||
| 308 | $roleService->removePolicyByRoleDraft($roleDraft, $policyDraft); |
||
| 309 | /* END: Use Case */ |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Test for the assignRoleToUserGroup() method. |
||
| 314 | * |
||
| 315 | * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup() |
||
| 316 | * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup |
||
| 317 | * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
||
| 318 | */ |
||
| 319 | public function testAssignRoleToUserGroupThrowsUnauthorizedException() |
||
| 320 | { |
||
| 321 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
||
| 322 | |||
| 323 | $repository = $this->getRepository(); |
||
| 324 | $userService = $repository->getUserService(); |
||
| 325 | $roleService = $repository->getRoleService(); |
||
| 326 | $permissionResolver = $repository->getPermissionResolver(); |
||
| 327 | |||
| 328 | $editorsGroupId = $this->generateId('group', 13); |
||
| 329 | |||
| 330 | /* BEGIN: Use Case */ |
||
| 331 | $user = $this->createUserVersion1(); |
||
| 332 | |||
| 333 | $role = $this->createRole(); |
||
| 334 | |||
| 335 | // Load the "Editors" user group |
||
| 336 | $userGroup = $userService->loadUserGroup($editorsGroupId); |
||
| 337 | |||
| 338 | // Set "Editor" user as current user. |
||
| 339 | $permissionResolver->setCurrentUserReference($user); |
||
| 340 | |||
| 341 | // This call will fail with an "UnauthorizedException" |
||
| 342 | $roleService->assignRoleToUserGroup($role, $userGroup); |
||
| 343 | /* END: Use Case */ |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Test for the assignRoleToUserGroup() method. |
||
| 348 | * |
||
| 349 | * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation) |
||
| 350 | * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup |
||
| 351 | * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
||
| 352 | */ |
||
| 353 | public function testAssignRoleToUserGroupThrowsUnauthorizedExceptionWithRoleLimitationParameter() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Test for the removeRoleAssignment() method. |
||
| 389 | * |
||
| 390 | * @see \eZ\Publish\API\Repository\RoleService::removeRoleAssignment() |
||
| 391 | * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testRemoveRoleAssignmentFromUserGroup |
||
| 392 | * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
||
| 393 | */ |
||
| 394 | public function testRemoveRoleAssignmentFromUserGroupThrowsUnauthorizedException() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Test for the assignRoleToUser() method. |
||
| 432 | * |
||
| 433 | * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser() |
||
| 434 | * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser |
||
| 435 | * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
||
| 436 | */ |
||
| 437 | public function testAssignRoleToUserThrowsUnauthorizedException() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Test for the assignRoleToUser() method. |
||
| 460 | * |
||
| 461 | * @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation) |
||
| 462 | * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser |
||
| 463 | * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
||
| 464 | */ |
||
| 465 | public function testAssignRoleToUserThrowsUnauthorizedExceptionWithRoleLimitationParameter() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Test for the removeRoleAssignment() method. |
||
| 495 | * |
||
| 496 | * @see \eZ\Publish\API\Repository\RoleService::removeRoleAssignment() |
||
| 497 | * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testRemoveRoleAssignment |
||
| 498 | * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
||
| 499 | */ |
||
| 500 | View Code Duplication | public function testRemoveRoleAssignmentThrowsUnauthorizedException() |
|
| 501 | { |
||
| 502 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
||
| 503 | |||
| 504 | $repository = $this->getRepository(); |
||
| 505 | $roleService = $repository->getRoleService(); |
||
| 506 | $permissionResolver = $repository->getPermissionResolver(); |
||
| 507 | |||
| 508 | /* BEGIN: Use Case */ |
||
| 509 | $user = $this->createUserVersion1(); |
||
| 510 | |||
| 511 | $role = $this->createRole(); |
||
| 512 | |||
| 513 | // Assign new role to "Editor" user |
||
| 514 | $roleService->assignRoleToUser($role, $user); |
||
| 515 | |||
| 516 | // Set "Editor" user as current user. |
||
| 517 | $permissionResolver->setCurrentUserReference($user); |
||
| 518 | $roleAssignments = $roleService->getRoleAssignmentsForUser($user); |
||
| 519 | |||
| 520 | // This call will fail with an "UnauthorizedException" |
||
| 521 | foreach ($roleAssignments as $roleAssignment) { |
||
| 522 | if ($roleAssignment->role->id === $role->id) { |
||
| 523 | $roleService->removeRoleAssignment($roleAssignment); |
||
| 524 | } |
||
| 525 | } |
||
| 526 | /* END: Use Case */ |
||
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Test for the getRoleAssignments() method. |
||
| 531 | * |
||
| 532 | * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignments() |
||
| 533 | * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignments |
||
| 534 | * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
||
| 535 | */ |
||
| 536 | public function testGetRoleAssignmentsThrowsUnauthorizedException() |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Test for the getRoleAssignmentsForUser() method. |
||
| 559 | * |
||
| 560 | * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser() |
||
| 561 | */ |
||
| 562 | View Code Duplication | public function testGetRoleAssignmentsForUserLoadsEmptyListForAnonymousUser() |
|
| 578 | |||
| 579 | /** |
||
| 580 | * Test for the getRoleAssignmentsForUser() method. |
||
| 581 | * |
||
| 582 | * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser() |
||
| 583 | */ |
||
| 584 | public function testGetRoleAssignmentsForUserWithSubtreeLimitation() |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Test for the getRoleAssignmentsForUserGroup() method. |
||
| 610 | * |
||
| 611 | * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUserGroup() |
||
| 612 | * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignmentsForUserGroup |
||
| 613 | * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
||
| 614 | */ |
||
| 615 | public function testGetRoleAssignmentsForUserGroupThrowsUnauthorizedException() |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Create a role fixture in a variable named <b>$role</b>,. |
||
| 644 | * |
||
| 645 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
| 646 | */ |
||
| 647 | View Code Duplication | private function createRole() |
|
| 674 | } |
||
| 675 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.