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 |
||
| 31 | class TrashServiceTest extends BaseTrashServiceTest |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * Test for the trash() method. |
||
| 35 | * |
||
| 36 | * @see \eZ\Publish\API\Repository\TrashService::trash() |
||
| 37 | * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationByRemoteId |
||
| 38 | */ |
||
| 39 | public function testTrash() |
||
| 40 | { |
||
| 41 | /* BEGIN: Use Case */ |
||
| 42 | $trashItem = $this->createTrashItem(); |
||
| 43 | /* END: Use Case */ |
||
| 44 | |||
| 45 | $this->assertInstanceOf( |
||
| 46 | '\\eZ\\Publish\\API\\Repository\\Values\\Content\\TrashItem', |
||
| 47 | $trashItem |
||
| 48 | ); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Test for the trash() method. |
||
| 53 | * |
||
| 54 | * @see \eZ\Publish\API\Repository\TrashService::trash() |
||
| 55 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
| 56 | */ |
||
| 57 | public function testTrashSetsExpectedTrashItemProperties() |
||
| 58 | { |
||
| 59 | $repository = $this->getRepository(); |
||
| 60 | |||
| 61 | $mediaRemoteId = '75c715a51699d2d309a924eca6a95145'; |
||
| 62 | |||
| 63 | // Load the location that will be trashed |
||
| 64 | $location = $repository->getLocationService() |
||
| 65 | ->loadLocationByRemoteId($mediaRemoteId); |
||
| 66 | |||
| 67 | $expected = array( |
||
| 68 | 'id' => $location->id, |
||
| 69 | 'depth' => $location->depth, |
||
| 70 | 'hidden' => $location->hidden, |
||
| 71 | 'invisible' => $location->invisible, |
||
| 72 | 'parentLocationId' => $location->parentLocationId, |
||
| 73 | 'pathString' => $location->pathString, |
||
| 74 | 'priority' => $location->priority, |
||
| 75 | 'remoteId' => $location->remoteId, |
||
| 76 | 'sortField' => $location->sortField, |
||
| 77 | 'sortOrder' => $location->sortOrder, |
||
| 78 | ); |
||
| 79 | |||
| 80 | $trashItem = $this->createTrashItem(); |
||
| 81 | |||
| 82 | $this->assertPropertiesCorrect($expected, $trashItem); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Test for the trash() method. |
||
| 87 | * |
||
| 88 | * @see \eZ\Publish\API\Repository\TrashService::trash() |
||
| 89 | * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 90 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
| 91 | */ |
||
| 92 | public function testTrashRemovesLocationFromMainStorage() |
||
| 93 | { |
||
| 94 | $repository = $this->getRepository(); |
||
| 95 | |||
| 96 | $mediaRemoteId = '75c715a51699d2d309a924eca6a95145'; |
||
| 97 | |||
| 98 | /* BEGIN: Use Case */ |
||
| 99 | $this->createTrashItem(); |
||
| 100 | |||
| 101 | // Load the location service |
||
| 102 | $locationService = $repository->getLocationService(); |
||
| 103 | |||
| 104 | // This call will fail with a "NotFoundException", because the media |
||
| 105 | // location was marked as trashed in the main storage |
||
| 106 | $locationService->loadLocationByRemoteId($mediaRemoteId); |
||
| 107 | /* END: Use Case */ |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Test for the trash() method. |
||
| 112 | * |
||
| 113 | * @see \eZ\Publish\API\Repository\TrashService::trash() |
||
| 114 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
| 115 | */ |
||
| 116 | public function testTrashRemovesChildLocationsFromMainStorage() |
||
| 117 | { |
||
| 118 | $repository = $this->getRepository(); |
||
| 119 | |||
| 120 | /* BEGIN: Use Case */ |
||
| 121 | $remoteIds = $this->createRemoteIdList(); |
||
| 122 | |||
| 123 | $this->createTrashItem(); |
||
| 124 | |||
| 125 | // All invocations to loadLocationByRemoteId() to one of the above |
||
| 126 | // collected remoteIds will return in an "NotFoundException" |
||
| 127 | /* END: Use Case */ |
||
| 128 | |||
| 129 | $locationService = $repository->getLocationService(); |
||
| 130 | foreach ($remoteIds as $remoteId) { |
||
| 131 | try { |
||
| 132 | $locationService->loadLocationByRemoteId($remoteId); |
||
| 133 | $this->fail("Location '{$remoteId}' should exist.'"); |
||
| 134 | } catch (NotFoundException $e) { |
||
| 135 | // echo $e->getFile(), ' +', $e->getLine(), PHP_EOL; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | $this->assertGreaterThan( |
||
| 140 | 0, |
||
| 141 | count($remoteIds), |
||
| 142 | "There should be at least one 'Community' child location." |
||
| 143 | ); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Test for the trash() method. |
||
| 148 | * |
||
| 149 | * @see \eZ\Publish\API\Repository\TrashService::trash() |
||
| 150 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
| 151 | */ |
||
| 152 | public function testTrashDecrementsChildCountOnParentLocation() |
||
| 153 | { |
||
| 154 | $repository = $this->getRepository(); |
||
| 155 | $locationService = $repository->getLocationService(); |
||
| 156 | |||
| 157 | $baseLocationId = $this->generateId('location', 1); |
||
| 158 | |||
| 159 | $location = $locationService->loadLocation($baseLocationId); |
||
| 160 | |||
| 161 | $childCount = $locationService->getLocationChildCount($location); |
||
| 162 | |||
| 163 | $this->createTrashItem(); |
||
| 164 | |||
| 165 | $this->refreshSearch($repository); |
||
| 166 | |||
| 167 | $this->assertEquals( |
||
| 168 | $childCount - 1, |
||
| 169 | $locationService->getLocationChildCount($location) |
||
| 170 | ); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Test sending a location to trash updates Content mainLocation. |
||
| 175 | * |
||
| 176 | * @covers \eZ\Publish\API\Repository\TrashService::trash |
||
| 177 | */ |
||
| 178 | public function testTrashUpdatesMainLocation() |
||
| 179 | { |
||
| 180 | $repository = $this->getRepository(); |
||
| 181 | $contentService = $repository->getContentService(); |
||
| 182 | $locationService = $repository->getLocationService(); |
||
| 183 | $trashService = $repository->getTrashService(); |
||
| 184 | |||
| 185 | $contentInfo = $contentService->loadContentInfo(42); |
||
| 186 | |||
| 187 | // Create additional location that will become new main location |
||
| 188 | $location = $locationService->createLocation( |
||
| 189 | $contentInfo, |
||
| 190 | new LocationCreateStruct(['parentLocationId' => 2]) |
||
| 191 | ); |
||
| 192 | |||
| 193 | $trashService->trash( |
||
| 194 | $locationService->loadLocation($contentInfo->mainLocationId) |
||
| 195 | ); |
||
| 196 | |||
| 197 | self::assertEquals( |
||
| 198 | $location->id, |
||
| 199 | $contentService->loadContentInfo(42)->mainLocationId |
||
| 200 | ); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Test sending a location to trash. |
||
| 205 | * |
||
| 206 | * @covers \eZ\Publish\API\Repository\TrashService::trash |
||
| 207 | */ |
||
| 208 | public function testTrashReturnsNull() |
||
| 209 | { |
||
| 210 | $repository = $this->getRepository(); |
||
| 211 | $contentService = $repository->getContentService(); |
||
| 212 | $locationService = $repository->getLocationService(); |
||
| 213 | $trashService = $repository->getTrashService(); |
||
| 214 | |||
| 215 | // Create additional location to trash |
||
| 216 | $location = $locationService->createLocation( |
||
| 217 | $contentService->loadContentInfo(42), |
||
| 218 | new LocationCreateStruct(['parentLocationId' => 2]) |
||
| 219 | ); |
||
| 220 | |||
| 221 | $trashItem = $trashService->trash($location); |
||
| 222 | |||
| 223 | self::assertNull($trashItem); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Test for the loadTrashItem() method. |
||
| 228 | * |
||
| 229 | * @covers \eZ\Publish\API\Repository\TrashService::loadTrashItem |
||
| 230 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
| 231 | */ |
||
| 232 | public function testLoadTrashItem() |
||
| 233 | { |
||
| 234 | $repository = $this->getRepository(); |
||
| 235 | $trashService = $repository->getTrashService(); |
||
| 236 | |||
| 237 | /* BEGIN: Use Case */ |
||
| 238 | $trashItem = $this->createTrashItem(); |
||
| 239 | |||
| 240 | // Reload the trash item |
||
| 241 | $trashItemReloaded = $trashService->loadTrashItem($trashItem->id); |
||
| 242 | /* END: Use Case */ |
||
| 243 | |||
| 244 | $this->assertInstanceOf( |
||
| 245 | APITrashItem::class, |
||
| 246 | $trashItemReloaded |
||
| 247 | ); |
||
| 248 | |||
| 249 | $this->assertEquals( |
||
| 250 | $trashItem->pathString, |
||
| 251 | $trashItemReloaded->pathString |
||
| 252 | ); |
||
| 253 | |||
| 254 | $this->assertEquals( |
||
| 255 | $trashItem, |
||
| 256 | $trashItemReloaded |
||
| 257 | ); |
||
| 258 | |||
| 259 | $this->assertInstanceOf( |
||
| 260 | Content::class, |
||
| 261 | $content = $trashItemReloaded->getContent() |
||
| 262 | ); |
||
| 263 | $this->assertEquals($trashItem->contentId, $content->contentInfo->id); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Test for the loadTrashItem() method. |
||
| 268 | * |
||
| 269 | * @see \eZ\Publish\API\Repository\TrashService::loadTrashItem() |
||
| 270 | * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 271 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testLoadTrashItem |
||
| 272 | */ |
||
| 273 | public function testLoadTrashItemThrowsNotFoundException() |
||
| 274 | { |
||
| 275 | $repository = $this->getRepository(); |
||
| 276 | |||
| 277 | $nonExistingTrashId = $this->generateId('trash', 2342); |
||
| 278 | /* BEGIN: Use Case */ |
||
| 279 | $trashService = $repository->getTrashService(); |
||
| 280 | |||
| 281 | // This call will fail with a "NotFoundException", because no trash item |
||
| 282 | // with the ID 1342 should exist in an eZ Publish demo installation |
||
| 283 | $trashService->loadTrashItem($nonExistingTrashId); |
||
| 284 | /* END: Use Case */ |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Test for the recover() method. |
||
| 289 | * |
||
| 290 | * @covers \eZ\Publish\API\Repository\TrashService::recover |
||
| 291 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
| 292 | */ |
||
| 293 | public function testRecover() |
||
| 294 | { |
||
| 295 | $repository = $this->getRepository(); |
||
| 296 | $trashService = $repository->getTrashService(); |
||
| 297 | $locationService = $repository->getLocationService(); |
||
| 298 | |||
| 299 | $mediaRemoteId = '75c715a51699d2d309a924eca6a95145'; |
||
| 300 | |||
| 301 | /* BEGIN: Use Case */ |
||
| 302 | $trashItem = $this->createTrashItem(); |
||
| 303 | |||
| 304 | // Recover the trashed item |
||
| 305 | $location = $trashService->recover($trashItem); |
||
| 306 | |||
| 307 | // Load the recovered location |
||
| 308 | $locationReloaded = $locationService->loadLocationByRemoteId( |
||
| 309 | $mediaRemoteId |
||
| 310 | ); |
||
| 311 | /* END: Use Case */ |
||
| 312 | |||
| 313 | $this->assertInstanceOf( |
||
| 314 | APILocation::class, |
||
| 315 | $location |
||
| 316 | ); |
||
| 317 | |||
| 318 | $this->assertEquals( |
||
| 319 | $location, |
||
| 320 | $locationReloaded |
||
| 321 | ); |
||
| 322 | |||
| 323 | try { |
||
| 324 | $trashService->loadTrashItem($trashItem->id); |
||
| 325 | $this->fail('Trash item was not removed after being recovered.'); |
||
| 326 | } catch (NotFoundException $e) { |
||
| 327 | // All well |
||
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Test recovering a non existing trash item results in a NotFoundException. |
||
| 333 | * |
||
| 334 | * @covers \eZ\Publish\API\Repository\TrashService::recover |
||
| 335 | * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 336 | */ |
||
| 337 | public function testRecoverThrowsNotFoundExceptionForNonExistingTrashItem() |
||
| 338 | { |
||
| 339 | $repository = $this->getRepository(); |
||
| 340 | $trashService = $repository->getTrashService(); |
||
| 341 | |||
| 342 | $trashService->recover( |
||
| 343 | $this->getTrashItemDuble( |
||
| 344 | 12364, |
||
| 345 | 12345, |
||
| 346 | 12363 |
||
| 347 | ) |
||
| 348 | ); |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Test for the trash() method. |
||
| 353 | * |
||
| 354 | * @see \eZ\Publish\API\Repository\TrashService::recover() |
||
| 355 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
| 356 | * |
||
| 357 | * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 358 | */ |
||
| 359 | public function testNotFoundAliasAfterRemoveIt() |
||
| 360 | { |
||
| 361 | $mediaRemoteId = '75c715a51699d2d309a924eca6a95145'; |
||
| 362 | |||
| 363 | $repository = $this->getRepository(); |
||
| 364 | $trashService = $repository->getTrashService(); |
||
| 365 | $urlAliasService = $repository->getURLAliasService(); |
||
| 366 | $locationService = $repository->getLocationService(); |
||
| 367 | |||
| 368 | // Double ->lookup() call because there where issue that one call was not enough to spot bug |
||
| 369 | $urlAliasService->lookup('/Media'); |
||
| 370 | $urlAliasService->lookup('/Media'); |
||
| 371 | |||
| 372 | $mediaLocation = $locationService->loadLocationByRemoteId($mediaRemoteId); |
||
| 373 | $trashService->trash($mediaLocation); |
||
| 374 | |||
| 375 | $urlAliasService->lookup('/Media'); |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Test for the recover() method. |
||
| 380 | * |
||
| 381 | * @see \eZ\Publish\API\Repository\TrashService::recover() |
||
| 382 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
| 383 | */ |
||
| 384 | public function testAliasesForRemovedItems() |
||
| 385 | { |
||
| 386 | $mediaRemoteId = '75c715a51699d2d309a924eca6a95145'; |
||
| 387 | |||
| 388 | $repository = $this->getRepository(); |
||
| 389 | $trashService = $repository->getTrashService(); |
||
| 390 | $urlAliasService = $repository->getURLAliasService(); |
||
| 391 | $locationService = $repository->getLocationService(); |
||
| 392 | |||
| 393 | // Double ->lookup() call because there where issue that one call was not enough to spot bug |
||
| 394 | $urlAliasService->lookup('/Media'); |
||
| 395 | $trashedLocationAlias = $urlAliasService->lookup('/Media'); |
||
| 396 | |||
| 397 | $mediaLocation = $locationService->loadLocationByRemoteId($mediaRemoteId); |
||
| 398 | $trashItem = $trashService->trash($mediaLocation); |
||
| 399 | $this->assertAliasNotExists($urlAliasService, '/Media'); |
||
| 400 | |||
| 401 | $this->createNewContentInPlaceTrashedOne($repository, $mediaLocation->parentLocationId); |
||
| 402 | |||
| 403 | $createdLocationAlias = $urlAliasService->lookup('/Media'); |
||
| 404 | |||
| 405 | $this->assertNotEquals( |
||
| 406 | $trashedLocationAlias->destination, |
||
| 407 | $createdLocationAlias->destination, |
||
| 408 | 'Destination for /media url should changed' |
||
| 409 | ); |
||
| 410 | |||
| 411 | $recoveredLocation = $trashService->recover($trashItem); |
||
| 412 | $recoveredLocationAlias = $urlAliasService->lookup('/Media2'); |
||
| 413 | $recoveredLocationAliasReverse = $urlAliasService->reverseLookup($recoveredLocation); |
||
| 414 | |||
| 415 | $this->assertEquals($recoveredLocationAlias->destination, $recoveredLocationAliasReverse->destination); |
||
| 416 | |||
| 417 | $this->assertNotEquals($recoveredLocationAliasReverse->destination, $trashedLocationAlias->destination); |
||
| 418 | $this->assertNotEquals($recoveredLocationAliasReverse->destination, $createdLocationAlias->destination); |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Test for the recover() method. |
||
| 423 | * |
||
| 424 | * @see \eZ\Publish\API\Repository\TrashService::recover() |
||
| 425 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecover |
||
| 426 | */ |
||
| 427 | public function testRecoverDoesNotRestoreChildLocations() |
||
| 428 | { |
||
| 429 | $repository = $this->getRepository(); |
||
| 430 | $trashService = $repository->getTrashService(); |
||
| 431 | $locationService = $repository->getLocationService(); |
||
| 432 | |||
| 433 | $remoteIds = $this->createRemoteIdList(); |
||
| 434 | |||
| 435 | // Unset remote ID of actually restored location |
||
| 436 | unset($remoteIds[array_search('3f6d92f8044aed134f32153517850f5a', $remoteIds)]); |
||
| 437 | |||
| 438 | $trashItem = $this->createTrashItem(); |
||
| 439 | |||
| 440 | $trashService->recover($trashItem); |
||
| 441 | |||
| 442 | $this->assertGreaterThan( |
||
| 443 | 0, |
||
| 444 | count($remoteIds), |
||
| 445 | "There should be at least one 'Community' child location." |
||
| 446 | ); |
||
| 447 | |||
| 448 | // None of the child locations will be available again |
||
| 449 | foreach ($remoteIds as $remoteId) { |
||
| 450 | try { |
||
| 451 | $locationService->loadLocationByRemoteId($remoteId); |
||
| 452 | $this->fail( |
||
| 453 | sprintf( |
||
| 454 | 'Location with remote ID "%s" unexpectedly restored.', |
||
| 455 | $remoteId |
||
| 456 | ) |
||
| 457 | ); |
||
| 458 | } catch (NotFoundException $e) { |
||
| 459 | // All well |
||
| 460 | } |
||
| 461 | } |
||
| 462 | |||
| 463 | try { |
||
| 464 | $trashService->loadTrashItem($trashItem->id); |
||
| 465 | $this->fail('Trash item was not removed after being recovered.'); |
||
| 466 | } catch (NotFoundException $e) { |
||
| 467 | // All well |
||
| 468 | } |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Test for the recover() method. |
||
| 473 | * |
||
| 474 | * @see \eZ\Publish\API\Repository\TrashService::recover($trashItem, $newParentLocation) |
||
| 475 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecover |
||
| 476 | * |
||
| 477 | * @todo Fix naming |
||
| 478 | */ |
||
| 479 | public function testRecoverWithLocationCreateStructParameter() |
||
| 480 | { |
||
| 481 | $repository = $this->getRepository(); |
||
| 482 | $trashService = $repository->getTrashService(); |
||
| 483 | $locationService = $repository->getLocationService(); |
||
| 484 | |||
| 485 | $homeLocationId = $this->generateId('location', 2); |
||
| 486 | /* BEGIN: Use Case */ |
||
| 487 | // $homeLocationId is the ID of the "Home" location in an eZ Publish |
||
| 488 | // demo installation |
||
| 489 | |||
| 490 | $trashItem = $this->createTrashItem(); |
||
| 491 | |||
| 492 | // Get the new parent location |
||
| 493 | $newParentLocation = $locationService->loadLocation($homeLocationId); |
||
| 494 | |||
| 495 | // Recover location with new location |
||
| 496 | $location = $trashService->recover($trashItem, $newParentLocation); |
||
| 497 | /* END: Use Case */ |
||
| 498 | |||
| 499 | $this->assertPropertiesCorrect( |
||
| 500 | array( |
||
| 501 | 'remoteId' => $trashItem->remoteId, |
||
| 502 | 'parentLocationId' => $homeLocationId, |
||
| 503 | // Not the full sub tree is restored |
||
| 504 | 'depth' => $newParentLocation->depth + 1, |
||
| 505 | 'hidden' => false, |
||
| 506 | 'invisible' => $trashItem->invisible, |
||
| 507 | 'pathString' => $newParentLocation->pathString . $this->parseId('location', $location->id) . '/', |
||
| 508 | 'priority' => 0, |
||
| 509 | 'sortField' => APILocation::SORT_FIELD_NAME, |
||
| 510 | 'sortOrder' => APILocation::SORT_ORDER_ASC, |
||
| 511 | ), |
||
| 512 | $location |
||
| 513 | ); |
||
| 514 | |||
| 515 | try { |
||
| 516 | $trashService->loadTrashItem($trashItem->id); |
||
| 517 | $this->fail('Trash item was not removed after being recovered.'); |
||
| 518 | } catch (NotFoundException $e) { |
||
| 519 | // All well |
||
| 520 | } |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Test for the recover() method. |
||
| 525 | * |
||
| 526 | * @see \eZ\Publish\API\Repository\TrashService::recover($trashItem) |
||
| 527 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecover |
||
| 528 | */ |
||
| 529 | public function testRecoverIncrementsChildCountOnOriginalParent() |
||
| 530 | { |
||
| 531 | $repository = $this->getRepository(); |
||
| 532 | $trashService = $repository->getTrashService(); |
||
| 533 | $locationService = $repository->getLocationService(); |
||
| 534 | |||
| 535 | $location = $locationService->loadLocation($this->generateId('location', 1)); |
||
| 536 | |||
| 537 | $trashItem = $this->createTrashItem(); |
||
| 538 | |||
| 539 | $this->refreshSearch($repository); |
||
| 540 | |||
| 541 | /* BEGIN: Use Case */ |
||
| 542 | $childCount = $locationService->getLocationChildCount($location); |
||
| 543 | |||
| 544 | // Recover location with new location |
||
| 545 | $trashService->recover($trashItem); |
||
| 546 | /* END: Use Case */ |
||
| 547 | |||
| 548 | $this->refreshSearch($repository); |
||
| 549 | |||
| 550 | $this->assertEquals( |
||
| 551 | $childCount + 1, |
||
| 552 | $locationService->getLocationChildCount($location) |
||
| 553 | ); |
||
| 554 | |||
| 555 | try { |
||
| 556 | $trashService->loadTrashItem($trashItem->id); |
||
| 557 | $this->fail('Trash item was not removed after being recovered.'); |
||
| 558 | } catch (NotFoundException $e) { |
||
| 559 | // All well |
||
| 560 | } |
||
| 561 | } |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Test for the recover() method. |
||
| 565 | * |
||
| 566 | * @see \eZ\Publish\API\Repository\TrashService::recover($trashItem, $newParentLocation) |
||
| 567 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecoverWithLocationCreateStructParameter |
||
| 568 | */ |
||
| 569 | public function testRecoverWithLocationCreateStructParameterIncrementsChildCountOnNewParent() |
||
| 570 | { |
||
| 571 | $repository = $this->getRepository(); |
||
| 572 | $trashService = $repository->getTrashService(); |
||
| 573 | $locationService = $repository->getLocationService(); |
||
| 574 | |||
| 575 | $homeLocationId = $this->generateId('location', 2); |
||
| 576 | |||
| 577 | $location = $locationService->loadLocation($homeLocationId); |
||
| 578 | |||
| 579 | $childCount = $locationService->getLocationChildCount($location); |
||
| 580 | |||
| 581 | /* BEGIN: Use Case */ |
||
| 582 | // $homeLocationId is the ID of the "Home" location in an eZ Publish |
||
| 583 | // demo installation |
||
| 584 | |||
| 585 | $trashItem = $this->createTrashItem(); |
||
| 586 | |||
| 587 | // Get the new parent location |
||
| 588 | $newParentLocation = $locationService->loadLocation($homeLocationId); |
||
| 589 | |||
| 590 | // Recover location with new location |
||
| 591 | $trashService->recover($trashItem, $newParentLocation); |
||
| 592 | /* END: Use Case */ |
||
| 593 | |||
| 594 | $this->refreshSearch($repository); |
||
| 595 | |||
| 596 | $this->assertEquals( |
||
| 597 | $childCount + 1, |
||
| 598 | $locationService->getLocationChildCount($location) |
||
| 599 | ); |
||
| 600 | |||
| 601 | try { |
||
| 602 | $trashService->loadTrashItem($trashItem->id); |
||
| 603 | $this->fail('Trash item was not removed after being recovered.'); |
||
| 604 | } catch (NotFoundException $e) { |
||
| 605 | // All well |
||
| 606 | } |
||
| 607 | } |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Test recovering a location from trash to non existing location. |
||
| 611 | * |
||
| 612 | * @covers \eZ\Publish\API\Repository\TrashService::recover |
||
| 613 | * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 614 | */ |
||
| 615 | public function testRecoverToNonExistingLocation() |
||
| 616 | { |
||
| 617 | $repository = $this->getRepository(); |
||
| 618 | $trashService = $repository->getTrashService(); |
||
| 619 | $locationService = $repository->getLocationService(); |
||
| 620 | |||
| 621 | $location = $locationService->loadLocation(44); |
||
| 622 | $trashItem = $trashService->trash($location); |
||
| 623 | |||
| 624 | $newParentLocation = new Location( |
||
| 625 | array( |
||
| 626 | 'id' => 123456, |
||
| 627 | 'parentLocationId' => 123455, |
||
| 628 | ) |
||
| 629 | ); |
||
| 630 | $trashService->recover($trashItem, $newParentLocation); |
||
| 631 | } |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Test for the findTrashItems() method. |
||
| 635 | * |
||
| 636 | * @see \eZ\Publish\API\Repository\TrashService::findTrashItems() |
||
| 637 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
| 638 | */ |
||
| 639 | public function testFindTrashItems() |
||
| 640 | { |
||
| 641 | $repository = $this->getRepository(); |
||
| 642 | $trashService = $repository->getTrashService(); |
||
| 643 | |||
| 644 | /* BEGIN: Use Case */ |
||
| 645 | $this->createTrashItem(); |
||
| 646 | |||
| 647 | // Create a search query for all trashed items |
||
| 648 | $query = new Query(); |
||
| 649 | $query->filter = new Criterion\LogicalAnd( |
||
| 650 | array( |
||
| 651 | new Criterion\Field('title', Criterion\Operator::LIKE, '*'), |
||
| 652 | ) |
||
| 653 | ); |
||
| 654 | |||
| 655 | // Load all trashed locations |
||
| 656 | $searchResult = $trashService->findTrashItems($query); |
||
| 657 | /* END: Use Case */ |
||
| 658 | |||
| 659 | $this->assertInstanceOf( |
||
| 660 | SearchResult::class, |
||
| 661 | $searchResult |
||
| 662 | ); |
||
| 663 | |||
| 664 | // 4 trashed locations from the sub tree |
||
| 665 | $this->assertEquals(4, $searchResult->count); |
||
| 666 | $this->assertEquals(4, $searchResult->totalCount); |
||
| 667 | } |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Test for the findTrashItems() method. |
||
| 671 | * |
||
| 672 | * @see \eZ\Publish\API\Repository\TrashService::findTrashItems() |
||
| 673 | * @depends \eZ\Publish\API\Repository\Tests\TrashServiceTest::testFindTrashItems |
||
| 674 | */ |
||
| 675 | public function testFindTrashItemsLimitedAccess() |
||
| 676 | { |
||
| 677 | $repository = $this->getRepository(); |
||
| 678 | $trashService = $repository->getTrashService(); |
||
| 679 | |||
| 680 | /* BEGIN: Use Case */ |
||
| 681 | $this->createTrashItem(); |
||
| 682 | |||
| 683 | // Create a search query for all trashed items |
||
| 684 | $query = new Query(); |
||
| 685 | $query->filter = new Criterion\LogicalAnd( |
||
| 686 | array( |
||
| 687 | new Criterion\Field('title', Criterion\Operator::LIKE, '*'), |
||
| 688 | ) |
||
| 689 | ); |
||
| 690 | |||
| 691 | // Create a user in the Editor user group. |
||
| 692 | $user = $this->createUserVersion1(); |
||
| 693 | |||
| 694 | // Set the Editor user as current user, these users have no access to Trash by default. |
||
| 695 | $repository->getPermissionResolver()->setCurrentUserReference($user); |
||
| 696 | |||
| 697 | // Load all trashed locations |
||
| 698 | $searchResult = $trashService->findTrashItems($query); |
||
| 699 | /* END: Use Case */ |
||
| 700 | |||
| 701 | $this->assertInstanceOf( |
||
| 702 | '\\eZ\\Publish\\API\\Repository\\Values\\Content\\SearchResult', |
||
| 703 | $searchResult |
||
| 704 | ); |
||
| 705 | |||
| 706 | // 0 trashed locations found, though 4 exist |
||
| 707 | $this->assertEquals(0, $searchResult->count); |
||
| 708 | } |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Test for the emptyTrash() method. |
||
| 712 | * |
||
| 713 | * @see \eZ\Publish\API\Repository\TrashService::emptyTrash() |
||
| 714 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testFindTrashItems |
||
| 715 | */ |
||
| 716 | public function testEmptyTrash() |
||
| 717 | { |
||
| 718 | $repository = $this->getRepository(); |
||
| 719 | $trashService = $repository->getTrashService(); |
||
| 720 | |||
| 721 | /* BEGIN: Use Case */ |
||
| 722 | $this->createTrashItem(); |
||
| 723 | |||
| 724 | // Empty the trash |
||
| 725 | $trashService->emptyTrash(); |
||
| 726 | |||
| 727 | // Create a search query for all trashed items |
||
| 728 | $query = new Query(); |
||
| 729 | $query->filter = new Criterion\LogicalAnd( |
||
| 730 | array( |
||
| 731 | new Criterion\Field('title', Criterion\Operator::LIKE, '*'), |
||
| 732 | ) |
||
| 733 | ); |
||
| 734 | |||
| 735 | // Load all trashed locations, search result should be empty |
||
| 736 | $searchResult = $trashService->findTrashItems($query); |
||
| 737 | /* END: Use Case */ |
||
| 738 | |||
| 739 | $this->assertEquals(0, $searchResult->count); |
||
| 740 | } |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Test for the deleteTrashItem() method. |
||
| 744 | * |
||
| 745 | * @see \eZ\Publish\API\Repository\TrashService::deleteTrashItem() |
||
| 746 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testFindTrashItems |
||
| 747 | */ |
||
| 748 | public function testDeleteTrashItem() |
||
| 749 | { |
||
| 750 | $repository = $this->getRepository(); |
||
| 751 | $trashService = $repository->getTrashService(); |
||
| 752 | $locationService = $repository->getLocationService(); |
||
| 753 | |||
| 754 | $demoDesignLocationId = $this->generateId('location', 56); |
||
| 755 | /* BEGIN: Use Case */ |
||
| 756 | // $demoDesignLocationId is the ID of the "Demo Design" location in an eZ |
||
| 757 | // Publish demo installation |
||
| 758 | |||
| 759 | $trashItem = $this->createTrashItem(); |
||
| 760 | |||
| 761 | // Trash one more location |
||
| 762 | $trashService->trash( |
||
| 763 | $locationService->loadLocation($demoDesignLocationId) |
||
| 764 | ); |
||
| 765 | |||
| 766 | // Empty the trash |
||
| 767 | $trashService->deleteTrashItem($trashItem); |
||
| 768 | |||
| 769 | // Create a search query for all trashed items |
||
| 770 | $query = new Query(); |
||
| 771 | $query->filter = new Criterion\LogicalAnd( |
||
| 772 | array( |
||
| 773 | new Criterion\Field('title', Criterion\Operator::LIKE, '*'), |
||
| 774 | ) |
||
| 775 | ); |
||
| 776 | |||
| 777 | // Load all trashed locations, should only contain the Demo Design location |
||
| 778 | $searchResult = $trashService->findTrashItems($query); |
||
| 779 | /* END: Use Case */ |
||
| 780 | |||
| 781 | $foundIds = array_map( |
||
| 782 | function ($trashItem) { |
||
| 783 | return $trashItem->id; |
||
| 784 | }, |
||
| 785 | $searchResult->items |
||
| 786 | ); |
||
| 787 | |||
| 788 | $this->assertEquals(4, $searchResult->count); |
||
| 789 | $this->assertTrue( |
||
| 790 | in_array($demoDesignLocationId, $foundIds) |
||
| 791 | ); |
||
| 792 | } |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Test deleting a non existing trash item. |
||
| 796 | * |
||
| 797 | * @covers \eZ\Publish\API\Repository\TrashService::deleteTrashItem |
||
| 798 | * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 799 | */ |
||
| 800 | public function testDeleteThrowsNotFoundExceptionForNonExistingTrashItem() |
||
| 801 | { |
||
| 802 | $repository = $this->getRepository(); |
||
| 803 | $trashService = $repository->getTrashService(); |
||
| 804 | |||
| 805 | $trashService->deleteTrashItem($this->getTrashItemDuble( |
||
| 806 | 12364, |
||
| 807 | 12345, |
||
| 808 | 12363 |
||
| 809 | )); |
||
| 810 | } |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Returns an array with the remoteIds of all child locations of the |
||
| 814 | * <b>Community</b> location. It is stored in a local variable named |
||
| 815 | * <b>$remoteIds</b>. |
||
| 816 | * |
||
| 817 | * @return string[] |
||
| 818 | */ |
||
| 819 | private function createRemoteIdList() |
||
| 820 | { |
||
| 821 | $repository = $this->getRepository(); |
||
| 822 | |||
| 823 | /* BEGIN: Inline */ |
||
| 824 | // remoteId of the "Community" location in an eZ Publish demo installation |
||
| 825 | $mediaRemoteId = '75c715a51699d2d309a924eca6a95145'; |
||
| 826 | |||
| 827 | // Load the location service |
||
| 828 | $locationService = $repository->getLocationService(); |
||
| 829 | |||
| 830 | $remoteIds = array(); |
||
| 831 | $children = $locationService->loadLocationChildren($locationService->loadLocationByRemoteId($mediaRemoteId)); |
||
| 832 | foreach ($children->locations as $child) { |
||
| 833 | $remoteIds[] = $child->remoteId; |
||
| 834 | foreach ($locationService->loadLocationChildren($child)->locations as $grandChild) { |
||
| 835 | $remoteIds[] = $grandChild->remoteId; |
||
| 836 | } |
||
| 837 | } |
||
| 838 | /* END: Inline */ |
||
| 839 | |||
| 840 | return $remoteIds; |
||
| 841 | } |
||
| 842 | |||
| 843 | /** |
||
| 844 | * @param Repository $repository |
||
| 845 | * @param int $parentLocationId |
||
| 846 | * |
||
| 847 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 848 | */ |
||
| 849 | protected function createNewContentInPlaceTrashedOne(Repository $repository, $parentLocationId) |
||
| 850 | { |
||
| 851 | $contentService = $repository->getContentService(); |
||
| 852 | $locationService = $repository->getLocationService(); |
||
| 853 | $contentTypeService = $repository->getContentTypeService(); |
||
| 854 | |||
| 855 | $contentType = $contentTypeService->loadContentTypeByIdentifier('forum'); |
||
| 856 | $newContent = $contentService->newContentCreateStruct($contentType, 'eng-US'); |
||
| 857 | $newContent->setField('name', 'Media'); |
||
| 858 | |||
| 859 | $location = $locationService->newLocationCreateStruct($parentLocationId); |
||
| 860 | |||
| 861 | $draftContent = $contentService->createContent($newContent, [$location]); |
||
| 862 | |||
| 863 | return $contentService->publishVersion($draftContent->versionInfo); |
||
| 864 | } |
||
| 865 | |||
| 866 | /** |
||
| 867 | * @param URLAliasService $urlAliasService |
||
| 868 | * @param string $urlPath Url alias path |
||
| 869 | * |
||
| 870 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias |
||
| 871 | */ |
||
| 872 | private function assertAliasExists(URLAliasService $urlAliasService, $urlPath) |
||
| 873 | { |
||
| 874 | $urlAlias = $urlAliasService->lookup($urlPath); |
||
| 875 | |||
| 876 | $this->assertInstanceOf('\eZ\Publish\API\Repository\Values\Content\URLAlias', $urlAlias); |
||
| 877 | |||
| 878 | return $urlAlias; |
||
| 879 | } |
||
| 880 | |||
| 881 | /** |
||
| 882 | * @param URLAliasService $urlAliasService |
||
| 883 | * @param string $urlPath Url alias path |
||
| 884 | */ |
||
| 885 | private function assertAliasNotExists(URLAliasService $urlAliasService, $urlPath) |
||
| 886 | { |
||
| 887 | try { |
||
| 914 |