Skip to content

Commit

Permalink
EZP-29202: Swapping a bookmarked locations (ezsystems#2335)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs authored and Łukasz Serwatka committed May 25, 2018
1 parent b2a1ffd commit 4b823cb
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 8 deletions.
35 changes: 35 additions & 0 deletions eZ/Publish/API/Repository/Tests/LocationServiceTest.php
Expand Up @@ -1318,6 +1318,41 @@ public function testSwapLocationUpdatesMainLocation()
self::assertEquals($mainLocation->id, $reloadedContent->contentInfo->mainLocationId);
}

/**
* Test if location swap affects related bookmarks.
*
* @covers \eZ\Publish\API\Repository\LocationService::swapLocation
*/
public function testBookmarksAreSwappedAfterSwapLocation()
{
$repository = $this->getRepository();

$mediaLocationId = $this->generateId('location', 43);
$demoDesignLocationId = $this->generateId('location', 56);

/* BEGIN: Use Case */
$locationService = $repository->getLocationService();
$bookmarkService = $repository->getBookmarkService();

$mediaLocation = $locationService->loadLocation($mediaLocationId);
$demoDesignLocation = $locationService->loadLocation($demoDesignLocationId);

// Bookmark locations
$bookmarkService->createBookmark($mediaLocation);
$bookmarkService->createBookmark($demoDesignLocation);

$beforeSwap = $bookmarkService->loadBookmarks();

// Swaps the content referred to by the locations
$locationService->swapLocation($mediaLocation, $demoDesignLocation);

$afterSwap = $bookmarkService->loadBookmarks();
/* END: Use Case */

$this->assertEquals($beforeSwap->items[0]->id, $afterSwap->items[1]->id);
$this->assertEquals($beforeSwap->items[1]->id, $afterSwap->items[0]->id);
}

/**
* Test for the hideLocation() method.
*
Expand Down
14 changes: 14 additions & 0 deletions eZ/Publish/Core/Persistence/Cache/BookmarkHandler.php
Expand Up @@ -93,4 +93,18 @@ public function countUserBookmarks(int $userId): int

return $this->persistenceHandler->bookmarkHandler()->countUserBookmarks($userId);
}

/**
* {@inheritdoc}
*/
public function locationSwapped(int $location1Id, int $location2Id): void
{
$this->logger->logCall(__METHOD__, [
'location1Id' => $location1Id,
'location2Id' => $location2Id,
]);

// Cache clearing is already done by locationHandler
$this->persistenceHandler->bookmarkHandler()->locationSwapped($location1Id, $location2Id);
}
}
Expand Up @@ -36,6 +36,7 @@ public function providerForUnCachedMethods(): array
['delete', [1], ['bookmark-1']],
['loadUserBookmarks', [3, 2, 1], null, null, []],
['countUserBookmarks', [3], null, null, 1],
['locationSwapped', [1, 2], null, null],
];
}

Expand Down
8 changes: 8 additions & 0 deletions eZ/Publish/Core/Persistence/Legacy/Bookmark/Gateway.php
Expand Up @@ -60,4 +60,12 @@ abstract public function loadUserBookmarks(int $userId, int $offset = 0, int $li
* @return int
*/
abstract public function countUserBookmarks(int $userId): int;

/**
* Updates related bookmarks when location was swapped.
*
* @param int $location1Id ID of first location
* @param int $location2Id ID of second location
*/
abstract public function locationSwapped(int $location1Id, int $location2Id): void;
}
Expand Up @@ -139,6 +139,26 @@ public function countUserBookmarks(int $userId): int
return (int) $query->execute()->fetchColumn();
}

/**
* {@inheritdoc}
*/
public function locationSwapped(int $location1Id, int $location2Id): void
{
$query = $this->connection->createQueryBuilder();
$query
->update(self::TABLE_BOOKMARKS)
->set(self::COLUMN_LOCATION_ID, '(CASE WHEN node_id = :source_id THEN :target_id ELSE :source_id END)')
->where($query->expr()->orX(
$query->expr()->eq(self::COLUMN_LOCATION_ID, ':source_id'),
$query->expr()->eq(self::COLUMN_LOCATION_ID, ':target_id')
));

$stmt = $this->connection->prepare($query->getSQL());
$stmt->bindValue('source_id', $location1Id, PDO::PARAM_INT);
$stmt->bindValue('target_id', $location2Id, PDO::PARAM_INT);
$stmt->execute();
}

private function getColumns(): array
{
return [
Expand Down
Expand Up @@ -92,4 +92,16 @@ public function countUserBookmarks(int $userId): int
throw new RuntimeException('Database error', 0, $e);
}
}

/**
* {@inheritdoc}
*/
public function locationSwapped(int $location1Id, int $location2Id): void
{
try {
$this->innerGateway->locationSwapped($location1Id, $location2Id);
} catch (DBALException | PDOException $e) {
throw new RuntimeException('Database error', 0, $e);
}
}
}
8 changes: 8 additions & 0 deletions eZ/Publish/Core/Persistence/Legacy/Bookmark/Handler.php
Expand Up @@ -90,4 +90,12 @@ public function countUserBookmarks(int $userId): int
{
return $this->gateway->countUserBookmarks($userId);
}

/**
* {@inheritdoc}
*/
public function locationSwapped(int $location1Id, int $location2Id): void
{
$this->gateway->locationSwapped($location1Id, $location2Id);
}
}
Expand Up @@ -41,9 +41,7 @@ public function testInsertBookmark()
'name' => 'Lorem ipsum dolor...',
]));

$data = $this->connection
->executeQuery('SELECT * FROM ezcontentbrowsebookmark WHERE id = :id', ['id' => $id])
->fetch(PDO::FETCH_ASSOC);
$data = $this->loadBookmark($id);

$this->assertEquals([
'id' => $id,
Expand All @@ -60,11 +58,7 @@ public function testDeleteBookmark()
{
$this->getGateway()->deleteBookmark(self::EXISTING_BOOKMARK_ID);

$data = $this->connection
->executeQuery('SELECT * FROM ezcontentbrowsebookmark WHERE id = :id', ['id' => self::EXISTING_BOOKMARK_ID])
->fetch(PDO::FETCH_ASSOC);

$this->assertEmpty($data);
$this->assertEmpty($this->loadBookmark(self::EXISTING_BOOKMARK_ID));
}

/**
Expand Down Expand Up @@ -134,6 +128,29 @@ public function dataProviderForLoadUserBookmarks(): array
];
}

/**
* @covers \eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway\DoctrineDatabase::locationSwapped
*/
public function testLocationSwapped()
{
$bookmark1Id = 3;
$bookmark2Id = 4;

$bookmark1BeforeSwap = $this->loadBookmark($bookmark1Id);
$bookmark2BeforeSwap = $this->loadBookmark($bookmark2Id);

$this->getGateway()->locationSwapped(
(int) $bookmark1BeforeSwap['node_id'],
(int) $bookmark2BeforeSwap['node_id']
);

$bookmark1AfterSwap = $this->loadBookmark($bookmark1Id);
$bookmark2AfterSwap = $this->loadBookmark($bookmark2Id);

$this->assertEquals($bookmark1BeforeSwap['node_id'], $bookmark2AfterSwap['node_id']);
$this->assertEquals($bookmark2BeforeSwap['node_id'], $bookmark1AfterSwap['node_id']);
}

/**
* Return a ready to test DoctrineStorage gateway.
*
Expand All @@ -145,4 +162,13 @@ protected function getGateway(): DoctrineDatabase
$this->getDatabaseHandler()->getConnection()
);
}

private function loadBookmark(int $id): array
{
$data = $this->connection
->executeQuery('SELECT * FROM ezcontentbrowsebookmark WHERE id = :id', ['id' => $id])
->fetch(PDO::FETCH_ASSOC);

return is_array($data) ? $data : [];
}
}
16 changes: 16 additions & 0 deletions eZ/Publish/Core/Persistence/Legacy/Tests/Bookmark/HandlerTest.php
Expand Up @@ -197,4 +197,20 @@ public function testLoadUserBookmarks()

$this->assertEquals($objects, $this->handler->loadUserBookmarks($userId, $offset, $limit));
}

/**
* @covers \eZ\Publish\Core\Persistence\Legacy\Bookmark\Handler::locationSwapped
*/
public function testLocationSwapped()
{
$location1Id = 1;
$location2Id = 2;

$this->gateway
->expects($this->once())
->method('locationSwapped')
->with($location1Id, $location2Id);

$this->handler->locationSwapped($location1Id, $location2Id);
}
}
1 change: 1 addition & 0 deletions eZ/Publish/Core/Repository/LocationService.php
Expand Up @@ -537,6 +537,7 @@ public function swapLocation(APILocation $location1, APILocation $location2)
$location2->id,
$location2->parentLocationId
);
$this->persistenceHandler->bookmarkHandler()->locationSwapped($loadedLocation1->id, $loadedLocation2->id);
$this->repository->commit();
} catch (Exception $e) {
$this->repository->rollback();
Expand Down
10 changes: 10 additions & 0 deletions eZ/Publish/SPI/Persistence/Bookmark/Handler.php
Expand Up @@ -55,4 +55,14 @@ public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1)
* @return int
*/
public function countUserBookmarks(int $userId): int;

/**
* Notifies the underlying engine that a location was swapped.
*
* This method triggers the change of the bookmarked locations.
*
* @param int $location1Id ID of first location
* @param int $location2Id ID of second location
*/
public function locationSwapped(int $location1Id, int $location2Id): void;
}

0 comments on commit 4b823cb

Please sign in to comment.