Skip to content

Commit

Permalink
Merged branch '1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
alongosz committed Sep 9, 2020
2 parents cb160c5 + 313acea commit 2b64f12
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 56 deletions.
2 changes: 2 additions & 0 deletions eZ/Publish/API/Repository/LocationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ public function unhideLocation(Location $location): Location;
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to move this location to the target
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user does not have read access to the whole source subtree
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the new parent is in a subtree of the location
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the new parent location is the same as current
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the new parent location is not a container
*
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
* @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation
Expand Down
138 changes: 101 additions & 37 deletions eZ/Publish/API/Repository/Tests/LocationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Exception;
use eZ\Publish\API\Repository\Exceptions\BadStateException;
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\URLAliasService as URLAliasServiceInterface;
use eZ\Publish\API\Repository\Values\Content\Content;
Expand Down Expand Up @@ -2533,10 +2534,10 @@ public function testCopySubtreeThrowsInvalidArgumentException()
/**
* Test for the moveSubtree() method.
*
* @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
* @covers \eZ\Publish\API\Repository\LocationService::moveSubtree
* @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
*/
public function testMoveSubtree()
public function testMoveSubtree(): void
{
$repository = $this->getRepository();

Expand All @@ -2553,19 +2554,19 @@ public function testMoveSubtree()
$locationService = $repository->getLocationService();

// Load location to move
$locationToMove = $locationService->loadLocation($mediaLocationId);
$locationToMove = $locationService->loadLocation($demoDesignLocationId);

// Load new parent location
$newParentLocation = $locationService->loadLocation($demoDesignLocationId);
$newParentLocation = $locationService->loadLocation($mediaLocationId);

// Move location from "Home" to "Demo Design"
// Move location from "Home" to "Media"
$locationService->moveSubtree(
$locationToMove,
$newParentLocation
);

// Load moved location
$movedLocation = $locationService->loadLocation($mediaLocationId);
$movedLocation = $locationService->loadLocation($demoDesignLocationId);
/* END: Use Case */

$this->assertPropertiesCorrect(
Expand All @@ -2583,10 +2584,10 @@ public function testMoveSubtree()
/**
* Test for the moveSubtree() method.
*
* @see \eZ\Publish\API\Repository\LocationService::moveSubtree()
* @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
* @covers \eZ\Publish\API\Repository\LocationService::moveSubtree
* @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
*/
public function testMoveSubtreeHidden()
public function testMoveSubtreeThrowsExceptionOnMoveNotIntoContainer(): void
{
$repository = $this->getRepository();

Expand All @@ -2608,17 +2609,79 @@ public function testMoveSubtreeHidden()
// Load new parent location
$newParentLocation = $locationService->loadLocation($demoDesignLocationId);

// Move location from "Home" to "Demo Design" (not container)
$this->expectException(InvalidArgumentException::class);
$locationService->moveSubtree($locationToMove, $newParentLocation);
}

/**
* Test for the moveSubtree() method.
*
* @covers \eZ\Publish\API\Repository\LocationService::moveSubtree
* @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocation
*/
public function testMoveSubtreeThrowsExceptionOnMoveToSame(): void
{
$repository = $this->getRepository();

$mediaLocationId = $this->generateId('location', 43);
/* BEGIN: Use Case */
// $mediaLocationId is the ID of the "Media" page location in
// an eZ Publish demo installation

// Load the location service
$locationService = $repository->getLocationService();

// Load location to move
$locationToMove = $locationService->loadLocation($mediaLocationId);

// Load parent location
$newParentLocation = $locationService->loadLocation($locationToMove->parentLocationId);

// Move location from "Home" to "Home"
$this->expectException(InvalidArgumentException::class);
$locationService->moveSubtree($locationToMove, $newParentLocation);
}

/**
* Test for the moveSubtree() method.
*
* @covers \eZ\Publish\API\Repository\LocationService::moveSubtree
* @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
*/
public function testMoveSubtreeHidden(): void
{
$repository = $this->getRepository();

$mediaLocationId = $this->generateId('location', 43);
$demoDesignLocationId = $this->generateId('location', 56);
/* BEGIN: Use Case */
// $mediaLocationId is the ID of the "Media" page location in
// an eZ Publish demo installation

// $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
// Publish demo installation

// Load the location service
$locationService = $repository->getLocationService();

// Load location to move
$locationToMove = $locationService->loadLocation($demoDesignLocationId);

// Load new parent location
$newParentLocation = $locationService->loadLocation($mediaLocationId);

// Hide the target location before we move
$newParentLocation = $locationService->hideLocation($newParentLocation);

// Move location from "Home" to "Demo Design"
// Move location from "Demo Design" to "Home"
$locationService->moveSubtree(
$locationToMove,
$newParentLocation
);

// Load moved location
$movedLocation = $locationService->loadLocation($mediaLocationId);
$movedLocation = $locationService->loadLocation($demoDesignLocationId);
/* END: Use Case */

$this->assertPropertiesCorrect(
Expand All @@ -2644,8 +2707,8 @@ public function testMoveSubtreeUpdatesSubtreeProperties()
$repository = $this->getRepository();
$locationService = $repository->getLocationService();

$locationToMove = $locationService->loadLocation($this->generateId('location', 43));
$newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
$locationToMove = $locationService->loadLocation($this->generateId('location', 56));
$newParentLocation = $locationService->loadLocation($this->generateId('location', 43));

// Load Subtree properties before move
$expected = $this->loadSubtreeProperties($locationToMove);
Expand All @@ -2671,19 +2734,19 @@ public function testMoveSubtreeUpdatesSubtreeProperties()
$locationService = $repository->getLocationService();

// Load location to move
$locationToMove = $locationService->loadLocation($mediaLocationId);
$locationToMove = $locationService->loadLocation($demoDesignLocationId);

// Load new parent location
$newParentLocation = $locationService->loadLocation($demoDesignLocationId);
$newParentLocation = $locationService->loadLocation($mediaLocationId);

// Move location from "Home" to "Demo Design"
// Move location from "Demo Design" to "Home"
$locationService->moveSubtree(
$locationToMove,
$newParentLocation
);

// Load moved location
$movedLocation = $locationService->loadLocation($mediaLocationId);
$movedLocation = $locationService->loadLocation($demoDesignLocationId);
/* END: Use Case */

$this->refreshSearch($repository);
Expand All @@ -2705,8 +2768,8 @@ public function testMoveSubtreeUpdatesSubtreePropertiesHidden()
$repository = $this->getRepository();
$locationService = $repository->getLocationService();

$locationToMove = $locationService->loadLocation($this->generateId('location', 43));
$newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
$locationToMove = $locationService->loadLocation($this->generateId('location', 2));
$newParentLocation = $locationService->loadLocation($this->generateId('location', 43));

// Hide the target location before we move
$newParentLocation = $locationService->hideLocation($newParentLocation);
Expand All @@ -2715,31 +2778,31 @@ public function testMoveSubtreeUpdatesSubtreePropertiesHidden()
$expected = $this->loadSubtreeProperties($locationToMove);
foreach ($expected as $id => $properties) {
$expected[$id]['invisible'] = true;
$expected[$id]['depth'] = $properties['depth'] + 2;
$expected[$id]['depth'] = $properties['depth'] + 1;
$expected[$id]['pathString'] = str_replace(
$locationToMove->pathString,
$newParentLocation->pathString . $this->parseId('location', $locationToMove->id) . '/',
$properties['pathString']
);
}

$homeLocationId = $this->generateId('location', 2);
$mediaLocationId = $this->generateId('location', 43);
$demoDesignLocationId = $this->generateId('location', 56);
/* BEGIN: Use Case */
// $mediaLocationId is the ID of the "Media" page location in
// an eZ Publish demo installation

// $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
// $homeLocationId is the ID of the "Home" page location in an eZ
// Publish demo installation

// Load the location service
$locationService = $repository->getLocationService();

// Load location to move
$locationToMove = $locationService->loadLocation($mediaLocationId);
$locationToMove = $locationService->loadLocation($homeLocationId);

// Load new parent location
$newParentLocation = $locationService->loadLocation($demoDesignLocationId);
$newParentLocation = $locationService->loadLocation($mediaLocationId);

// Move location from "Home" to "Demo Design"
$locationService->moveSubtree(
Expand All @@ -2748,7 +2811,7 @@ public function testMoveSubtreeUpdatesSubtreePropertiesHidden()
);

// Load moved location
$movedLocation = $locationService->loadLocation($mediaLocationId);
$movedLocation = $locationService->loadLocation($homeLocationId);
/* END: Use Case */

$this->refreshSearch($repository);
Expand All @@ -2770,7 +2833,7 @@ public function testMoveSubtreeIncrementsChildCountOfNewParent()
$repository = $this->getRepository();
$locationService = $repository->getLocationService();

$newParentLocation = $locationService->loadLocation($this->generateId('location', 56));
$newParentLocation = $locationService->loadLocation($this->generateId('location', 43));

// Load expected properties before move
$expected = $this->loadLocationProperties($newParentLocation);
Expand All @@ -2789,22 +2852,22 @@ public function testMoveSubtreeIncrementsChildCountOfNewParent()
$locationService = $repository->getLocationService();

// Load location to move
$locationToMove = $locationService->loadLocation($mediaLocationId);
$locationToMove = $locationService->loadLocation($demoDesignLocationId);

// Load new parent location
$newParentLocation = $locationService->loadLocation($demoDesignLocationId);
$newParentLocation = $locationService->loadLocation($mediaLocationId);

// Move location from "Home" to "Demo Design"
// Move location from "Demo Design" to "Home"
$locationService->moveSubtree(
$locationToMove,
$newParentLocation
);

// Load moved location
$movedLocation = $locationService->loadLocation($mediaLocationId);
$movedLocation = $locationService->loadLocation($demoDesignLocationId);

// Reload new parent location
$newParentLocation = $locationService->loadLocation($demoDesignLocationId);
$newParentLocation = $locationService->loadLocation($mediaLocationId);
/* END: Use Case */

$this->refreshSearch($repository);
Expand Down Expand Up @@ -2834,13 +2897,13 @@ public function testMoveSubtreeDecrementsChildCountOfOldParent()
$expected = $this->loadLocationProperties($oldParentLocation);
$childCountBefore = $locationService->getLocationChildCount($oldParentLocation);

$homeLocationId = $this->generateId('location', 2);
$mediaLocationId = $this->generateId('location', 43);
$demoDesignLocationId = $this->generateId('location', 56);
/* BEGIN: Use Case */
// $mediaLocationId is the ID of the "Media" page location in
// $homeLocationId is the ID of the "Home" page location in
// an eZ Publish demo installation

// $demoDesignLocationId is the ID of the "Demo Design" page location in an eZ
// $mediaLocationId is the ID of the "Media" page location in an eZ
// Publish demo installation

// Load the location service
Expand All @@ -2853,9 +2916,9 @@ public function testMoveSubtreeDecrementsChildCountOfOldParent()
$oldParentLocationId = $locationToMove->parentLocationId;

// Load new parent location
$newParentLocation = $locationService->loadLocation($demoDesignLocationId);
$newParentLocation = $locationService->loadLocation($homeLocationId);

// Move location from "Home" to "Demo Design"
// Move location from "Demo Design" to "Home"
$locationService->moveSubtree(
$locationToMove,
$newParentLocation
Expand Down Expand Up @@ -2921,7 +2984,7 @@ public function testMoveInvisibleSubtree()
*
* @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testMoveSubtree
*/
public function testMoveSubtreeThrowsInvalidArgumentException()
public function testMoveSubtreeThrowsInvalidArgumentException(): void
{
$this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);

Expand All @@ -2946,6 +3009,7 @@ public function testMoveSubtreeThrowsInvalidArgumentException()
$newParentLocation = $locationService->loadLocation($multimediaLocationId);

// Throws an exception because new parent location is placed below location to move
$this->expectException(InvalidArgumentException::class);
$locationService->moveSubtree(
$locationToMove,
$newParentLocation
Expand Down
4 changes: 3 additions & 1 deletion eZ/Publish/Core/Persistence/Cache/UrlAliasHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ public function locationMoved($locationId, $oldParentId, $newParentId)

$return = $this->persistenceHandler->urlAliasHandler()->locationMoved($locationId, $oldParentId, $newParentId);

$this->cache->invalidateTags(['urlAlias-location-' . $locationId, 'urlAlias-location-path-' . $locationId]);
if ($oldParentId !== $newParentId) {
$this->cache->invalidateTags(['urlAlias-location-' . $locationId, 'urlAlias-location-path-' . $locationId]);
}

return $return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@ private function removeTranslation(int $parentId, string $textMD5, int $language

public function historizeId(int $id, int $link): void
{
if ($id === $link) {
return;
}

$query = $this->connection->createQueryBuilder();
$query->select(
'parent',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,10 @@ public function loadUrlAlias($id)
*/
public function locationMoved($locationId, $oldParentId, $newParentId)
{
if ($oldParentId === $newParentId) {
return $newParentId;
}

// @todo optimize: $newLocationAliasId is already available in self::publishUrlAliasForLocation() as $newId
$newParentLocationAliasId = $this->getRealAliasId($newParentId);
$newLocationAlias = $this->gateway->loadAutogeneratedEntry(
Expand Down
Loading

0 comments on commit 2b64f12

Please sign in to comment.