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 |
||
| 15 | class RoleTest extends RESTFunctionalTestCase |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @covers POST /user/roles |
||
| 19 | * |
||
| 20 | * BC compatible mode, will return a role |
||
| 21 | * |
||
| 22 | * @return string The created role href |
||
| 23 | */ |
||
| 24 | View Code Duplication | public function testCreateRole() |
|
| 25 | { |
||
| 26 | $xml = <<< XML |
||
| 27 | <?xml version="1.0" encoding="UTF-8"?> |
||
| 28 | <RoleInput> |
||
| 29 | <identifier>testCreateRole</identifier> |
||
| 30 | <mainLanguageCode>eng-GB</mainLanguageCode> |
||
| 31 | <names> |
||
| 32 | <value languageCode="eng-GB">testCreateRole</value> |
||
| 33 | </names> |
||
| 34 | <descriptions> |
||
| 35 | <value languageCode="eng-GB">testCreateRole description</value> |
||
| 36 | </descriptions> |
||
| 37 | </RoleInput> |
||
| 38 | XML; |
||
| 39 | $request = $this->createHttpRequest('POST', '/api/ezp/v2/user/roles', 'RoleInput+xml', 'Role+json'); |
||
| 40 | $request->setContent($xml); |
||
| 41 | $response = $this->sendHttpRequest($request); |
||
| 42 | |||
| 43 | self::assertHttpResponseCodeEquals($response, 201); |
||
| 44 | self::assertHttpResponseHasHeader($response, 'Location'); |
||
| 45 | |||
| 46 | $href = $response->getHeader('Location'); |
||
| 47 | $this->addCreatedElement($href); |
||
| 48 | |||
| 49 | return $href; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @covers POST /user/roles |
||
| 54 | * |
||
| 55 | * BC incompatible mode, will return a role draft |
||
| 56 | * |
||
| 57 | * @return string The created role draft href |
||
| 58 | */ |
||
| 59 | View Code Duplication | public function testCreateRoleWithDraft() |
|
| 60 | { |
||
| 61 | $xml = <<< XML |
||
| 62 | <?xml version="1.0" encoding="UTF-8"?> |
||
| 63 | <RoleInput> |
||
| 64 | <identifier>testCreateRoleDraft</identifier> |
||
| 65 | <mainLanguageCode>eng-GB</mainLanguageCode> |
||
| 66 | <names> |
||
| 67 | <value languageCode="eng-GB">testCreateRoleDraft</value> |
||
| 68 | </names> |
||
| 69 | <descriptions> |
||
| 70 | <value languageCode="eng-GB">testCreateRoleDraft description</value> |
||
| 71 | </descriptions> |
||
| 72 | </RoleInput> |
||
| 73 | XML; |
||
| 74 | $request = $this->createHttpRequest( |
||
| 75 | 'POST', |
||
| 76 | '/api/ezp/v2/user/roles?publish=false', |
||
| 77 | 'RoleInput+xml', |
||
| 78 | 'RoleDraft+json' |
||
| 79 | ); |
||
| 80 | $request->setContent($xml); |
||
| 81 | $response = $this->sendHttpRequest($request); |
||
| 82 | |||
| 83 | self::assertHttpResponseCodeEquals($response, 201); |
||
| 84 | self::assertHttpResponseHasHeader($response, 'Location'); |
||
| 85 | |||
| 86 | $href = $response->getHeader('Location'); |
||
| 87 | $this->addCreatedElement($href); |
||
| 88 | |||
| 89 | return $href . '/draft'; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @covers GET /user/roles |
||
| 94 | */ |
||
| 95 | public function testListRoles() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @depends testCreateRole |
||
| 106 | * @covers GET /user/roles/{roleId} |
||
| 107 | */ |
||
| 108 | public function testLoadRole($roleHref) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @depends testCreateRole |
||
| 119 | * @covers POST /user/roles/{roleId} |
||
| 120 | * |
||
| 121 | * @return string The created role draft href |
||
| 122 | */ |
||
| 123 | View Code Duplication | public function testCreateRoleDraft($roleHref) |
|
| 124 | { |
||
| 125 | $xml = <<< XML |
||
| 126 | <?xml version="1.0" encoding="UTF-8"?> |
||
| 127 | <RoleInput> |
||
| 128 | <identifier>testCreateRoleDraft</identifier> |
||
| 129 | <mainLanguageCode>eng-GB</mainLanguageCode> |
||
| 130 | <names> |
||
| 131 | <value languageCode="eng-GB">testCreateRoleDraft</value> |
||
| 132 | </names> |
||
| 133 | <descriptions> |
||
| 134 | <value languageCode="eng-GB">testCreateRoleDraft description</value> |
||
| 135 | </descriptions> |
||
| 136 | </RoleInput> |
||
| 137 | XML; |
||
| 138 | $request = $this->createHttpRequest( |
||
| 139 | 'POST', |
||
| 140 | $roleHref, |
||
| 141 | 'RoleInput+xml', |
||
| 142 | 'RoleDraft+json' |
||
| 143 | ); |
||
| 144 | $request->setContent($xml); |
||
| 145 | $response = $this->sendHttpRequest($request); |
||
| 146 | |||
| 147 | self::assertHttpResponseCodeEquals($response, 201); |
||
| 148 | self::assertHttpResponseHasHeader($response, 'Location'); |
||
| 149 | |||
| 150 | $href = $response->getHeader('Location'); |
||
| 151 | $this->addCreatedElement($href); |
||
| 152 | |||
| 153 | return $href . '/draft'; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @depends testCreateRoleDraft |
||
| 158 | * @covers GET /user/roles/{roleId}/draft |
||
| 159 | */ |
||
| 160 | public function testLoadRoleDraft($roleDraftHref) |
||
| 161 | { |
||
| 162 | $response = $this->sendHttpRequest( |
||
| 163 | $this->createHttpRequest('GET', $roleDraftHref) |
||
| 164 | ); |
||
| 165 | |||
| 166 | self::assertHttpResponseCodeEquals($response, 200); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @depends testCreateRole |
||
| 171 | * @covers GET /user/roles/{roleId}/draftByRoleId |
||
| 172 | */ |
||
| 173 | public function testLoadRoleDraftByRoleId($roleHref) |
||
| 174 | { |
||
| 175 | $response = $this->sendHttpRequest( |
||
| 176 | $this->createHttpRequest('GET', $roleHref) |
||
| 177 | ); |
||
| 178 | |||
| 179 | self::assertHttpResponseCodeEquals($response, 200); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @depends testCreateRole |
||
| 184 | * @covers PATCH /user/roles/{roleId} |
||
| 185 | */ |
||
| 186 | View Code Duplication | public function testUpdateRole($roleHref) |
|
| 187 | { |
||
| 188 | $xml = <<< XML |
||
| 189 | <?xml version="1.0" encoding="UTF-8"?> |
||
| 190 | <RoleInput> |
||
| 191 | <identifier>testUpdateRole</identifier> |
||
| 192 | <mainLanguageCode>eng-GB</mainLanguageCode> |
||
| 193 | <names> |
||
| 194 | <value languageCode="eng-GB">testUpdateRole</value> |
||
| 195 | </names> |
||
| 196 | <descriptions> |
||
| 197 | <value languageCode="eng-GB">testUpdateRole description</value> |
||
| 198 | </descriptions> |
||
| 199 | </RoleInput> |
||
| 200 | XML; |
||
| 201 | |||
| 202 | $request = $this->createHttpRequest('PATCH', $roleHref, 'RoleInput+xml', 'Role+json'); |
||
| 203 | $request->setContent($xml); |
||
| 204 | $response = $this->sendHttpRequest($request); |
||
| 205 | |||
| 206 | // @todo Fix failure Notice: Trying to get property of non-object in \/home\/bertrand\/www\/ezpublish-kernel\/eZ\/Publish\/Core\/Persistence\/Cache\/UserHandler.php line 174 |
||
| 207 | self::assertHttpResponseCodeEquals($response, 200); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @depends testCreateRoleDraft |
||
| 212 | * @covers PATCH /user/roles/{roleId}/draft |
||
| 213 | */ |
||
| 214 | View Code Duplication | public function testUpdateRoleDraft($roleDraftHref) |
|
| 215 | { |
||
| 216 | $xml = <<< XML |
||
| 217 | <?xml version="1.0" encoding="UTF-8"?> |
||
| 218 | <RoleInput> |
||
| 219 | <identifier>testUpdateRoleDraft</identifier> |
||
| 220 | <mainLanguageCode>eng-GB</mainLanguageCode> |
||
| 221 | <names> |
||
| 222 | <value languageCode="eng-GB">testUpdateRoleDraft</value> |
||
| 223 | </names> |
||
| 224 | <descriptions> |
||
| 225 | <value languageCode="eng-GB">testUpdateRoleDraft description</value> |
||
| 226 | </descriptions> |
||
| 227 | </RoleInput> |
||
| 228 | XML; |
||
| 229 | |||
| 230 | $request = $this->createHttpRequest('PATCH', $roleDraftHref, 'RoleInput+xml', 'RoleDraft+json'); |
||
| 231 | $request->setContent($xml); |
||
| 232 | $response = $this->sendHttpRequest($request); |
||
| 233 | |||
| 234 | self::assertHttpResponseCodeEquals($response, 200); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @covers POST /user/roles/{roleId}/policies |
||
| 239 | * @depends testCreateRole |
||
| 240 | * |
||
| 241 | * @return string The created policy href |
||
| 242 | */ |
||
| 243 | View Code Duplication | public function testAddPolicy($roleHref) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * @covers POST /user/roles/{roleId}/policiesByRoleDraft |
||
| 275 | * @depends testCreateRoleDraft |
||
| 276 | * |
||
| 277 | * @return string The created policy href |
||
| 278 | */ |
||
| 279 | View Code Duplication | public function testAddPolicyByRoleDraft($roleDraftHref) |
|
| 280 | { |
||
| 281 | $xml = <<< XML |
||
| 282 | <?xml version="1.0" encoding="UTF-8"?> |
||
| 283 | <PolicyCreate> |
||
| 284 | <module>content</module> |
||
| 285 | <function>create</function> |
||
| 286 | <limitations> |
||
| 287 | <limitation identifier="Class"> |
||
| 288 | <values> |
||
| 289 | <ref href="1"/> |
||
| 290 | </values> |
||
| 291 | </limitation> |
||
| 292 | </limitations> |
||
| 293 | </PolicyCreate> |
||
| 294 | XML; |
||
| 295 | $request = $this->createHttpRequest( |
||
| 296 | 'POST', |
||
| 297 | $this->roleDraftHrefToRoleHref($roleDraftHref) . '/policiesByRoleDraft', |
||
| 298 | 'PolicyCreate+xml', |
||
| 299 | 'Policy+json' |
||
| 300 | ); |
||
| 301 | $request->setContent($xml); |
||
| 302 | |||
| 303 | $response = $this->sendHttpRequest($request); |
||
| 304 | self::assertHttpResponseCodeEquals($response, 201); |
||
| 305 | self::assertHttpResponseHasHeader($response, 'Location'); |
||
| 306 | |||
| 307 | $href = $response->getHeader('Location'); |
||
| 308 | $this->addCreatedElement($href); |
||
| 309 | |||
| 310 | return $href; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @covers GET /user/roles/{roleId}/policies/{policyId} |
||
| 315 | * @depends testAddPolicy |
||
| 316 | */ |
||
| 317 | public function testLoadPolicy($policyHref) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @covers GET /user/roles/{roleId}/policies |
||
| 328 | * @depends testCreateRole |
||
| 329 | */ |
||
| 330 | public function testLoadPolicies($roleHref) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @covers PATCH /user/roles/{roleId}/policies/{policyId} |
||
| 341 | * @depends testAddPolicy |
||
| 342 | */ |
||
| 343 | View Code Duplication | public function testUpdatePolicy($policyHref) |
|
| 344 | { |
||
| 345 | $xml = <<< XML |
||
| 346 | <?xml version="1.0" encoding="UTF-8"?> |
||
| 347 | <PolicyUpdate> |
||
| 348 | <limitations> |
||
| 349 | <limitation identifier="Class"> |
||
| 350 | <values> |
||
| 351 | <ref href="1"/> |
||
| 352 | </values> |
||
| 353 | </limitation> |
||
| 354 | </limitations> |
||
| 355 | </PolicyUpdate> |
||
| 356 | XML; |
||
| 357 | |||
| 358 | $request = $this->createHttpRequest('PATCH', $policyHref, 'PolicyUpdate+xml', 'Policy+json'); |
||
| 359 | $request->setContent($xml); |
||
| 360 | $response = $this->sendHttpRequest($request); |
||
| 361 | |||
| 362 | self::assertHttpResponseCodeEquals($response, 200); |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @covers PATCH /user/roles/{roleId}/policiesByRoleDraft/{policyId} |
||
| 367 | * @depends testAddPolicyByRoleDraft |
||
| 368 | */ |
||
| 369 | View Code Duplication | public function testUpdatePolicyByRoleDraft($policyHref) |
|
| 370 | { |
||
| 371 | $xml = <<< XML |
||
| 372 | <?xml version="1.0" encoding="UTF-8"?> |
||
| 373 | <PolicyUpdate> |
||
| 374 | <limitations> |
||
| 375 | <limitation identifier="Class"> |
||
| 376 | <values> |
||
| 377 | <ref href="1"/> |
||
| 378 | </values> |
||
| 379 | </limitation> |
||
| 380 | </limitations> |
||
| 381 | </PolicyUpdate> |
||
| 382 | XML; |
||
| 383 | |||
| 384 | $request = $this->createHttpRequest( |
||
| 385 | 'PATCH', |
||
| 386 | $this->policyHrefToPolicyDraftHref($policyHref), |
||
| 387 | 'PolicyUpdate+xml', |
||
| 388 | 'Policy+json' |
||
| 389 | ); |
||
| 390 | $request->setContent($xml); |
||
| 391 | $response = $this->sendHttpRequest($request); |
||
| 392 | |||
| 393 | self::assertHttpResponseCodeEquals($response, 200); |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @depends testCreateRole |
||
| 398 | * @covers POST /user/users/{userId}/roles |
||
| 399 | * |
||
| 400 | * @return string assigned role href |
||
| 401 | * |
||
| 402 | * @todo stop using the anonymous user, this is dangerous... |
||
| 403 | */ |
||
| 404 | View Code Duplication | public function testAssignRoleToUser($roleHref) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * @covers GET /user/users/{userId}/roles/{roleId} |
||
| 437 | * @depends testAssignRoleToUser |
||
| 438 | */ |
||
| 439 | public function testLoadRoleAssignmentForUser($roleAssignmentHref) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @covers DELETE /user/users/{userId}/roles/{roleId} |
||
| 450 | * @depends testAssignRoleToUser |
||
| 451 | */ |
||
| 452 | public function testUnassignRoleFromUser($roleAssignmentHref) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @depends testCreateRole |
||
| 463 | * @covers POST /user/groups/{groupId}/roles |
||
| 464 | * |
||
| 465 | * @return string role assignment href |
||
| 466 | */ |
||
| 467 | View Code Duplication | public function testAssignRoleToUserGroup($roleHref) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * @covers GET /user/groups/{groupId}/roles/{roleId} |
||
| 500 | * @depends testAssignRoleToUserGroup |
||
| 501 | */ |
||
| 502 | View Code Duplication | public function testLoadRoleAssignmentForUserGroup($roleAssignmentHref) |
|
| 511 | |||
| 512 | /** |
||
| 513 | * @covers DELETE /user/groups/{groupId}/roles/{roleId} |
||
| 514 | * @depends testAssignRoleToUserGroup |
||
| 515 | */ |
||
| 516 | View Code Duplication | public function testUnassignRoleFromUserGroup($roleAssignmentHref) |
|
| 525 | |||
| 526 | /** |
||
| 527 | * @covers GET /user/users/{userId}/roles |
||
| 528 | */ |
||
| 529 | public function testLoadRoleAssignmentsForUser() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @covers GET /user/groups/{groupPath}/roles |
||
| 540 | */ |
||
| 541 | public function testLoadRoleAssignmentsForUserGroup() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @covers GET /user/policies?userId={userId} |
||
| 552 | */ |
||
| 553 | public function testListPoliciesForUser() |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @covers DELETE /user/roles/{roleId}/policies/{policyId} |
||
| 564 | * @depends testAddPolicy |
||
| 565 | */ |
||
| 566 | public function testDeletePolicy($policyHref) |
||
| 574 | |||
| 575 | /** |
||
| 576 | * @covers DELETE /user/roles/{roleId}/policiesByRoleDraft/{policyId} |
||
| 577 | * @depends testAddPolicyByRoleDraft |
||
| 578 | */ |
||
| 579 | public function testRemovePolicyByRoleDraft($policyHref) |
||
| 580 | { |
||
| 581 | $response = $this->sendHttpRequest( |
||
| 582 | $this->createHttpRequest('DELETE', $this->policyHrefToPolicyDraftHref($policyHref)) |
||
| 583 | ); |
||
| 584 | |||
| 585 | self::assertHttpResponseCodeEquals($response, 204); |
||
| 586 | } |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @covers DELETE /user/roles/{roleId}/policies |
||
| 590 | * @depends testCreateRole |
||
| 591 | */ |
||
| 592 | public function testDeletePolicies($roleHref) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @covers DELETE /user/roles/{roleId} |
||
| 603 | * @depends testCreateRole |
||
| 604 | */ |
||
| 605 | public function testDeleteRole($roleHref) |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @covers PUBLISH /user/roles/{roleId}/draft |
||
| 616 | * @depends testCreateRoleDraft |
||
| 617 | */ |
||
| 618 | public function testPublishRoleDraft($roleDraftHref) |
||
| 619 | { |
||
| 620 | $response = $this->sendHttpRequest( |
||
| 621 | $this->createHttpRequest('PUBLISH', $roleDraftHref) |
||
| 622 | ); |
||
| 623 | |||
| 624 | self::assertHttpResponseCodeEquals($response, 201); |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @covers DELETE /user/roles/{roleId}/draft |
||
| 629 | * @depends testCreateRoleDraft |
||
| 630 | */ |
||
| 631 | public function testDeleteRoleDraft($roleDraftHref) |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Helper method for changing a roledraft href to a role href. |
||
| 645 | * |
||
| 646 | * @param string $roleDraftHref Role draft href |
||
| 647 | * |
||
| 648 | * @return string Role href |
||
| 649 | */ |
||
| 650 | private function roleDraftHrefToRoleHref($roleDraftHref) |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Helper method for changing a policy href to a policy by role draft href. |
||
| 657 | * |
||
| 658 | * @param string $policyHref Policy href |
||
| 659 | * |
||
| 660 | * @return string Policy by role draft href |
||
| 661 | */ |
||
| 662 | private function policyHrefToPolicyDraftHref($policyHref) |
||
| 666 | } |
||
| 667 |