diff --git a/eZ/Publish/API/Repository/Tests/LocationServiceTest.php b/eZ/Publish/API/Repository/Tests/LocationServiceTest.php index 153f768ba7..4238ae4671 100644 --- a/eZ/Publish/API/Repository/Tests/LocationServiceTest.php +++ b/eZ/Publish/API/Repository/Tests/LocationServiceTest.php @@ -1965,6 +1965,7 @@ public function testBookmarksAreSwappedAfterSwapLocation() $mediaLocationId = $this->generateId('location', 43); $demoDesignLocationId = $this->generateId('location', 56); + $contactUsLocationId = $this->generateId('location', 60); /* BEGIN: Use Case */ $locationService = $repository->getLocationService(); @@ -1972,6 +1973,7 @@ public function testBookmarksAreSwappedAfterSwapLocation() $mediaLocation = $locationService->loadLocation($mediaLocationId); $demoDesignLocation = $locationService->loadLocation($demoDesignLocationId); + $contactUsLocation = $locationService->loadLocation($contactUsLocationId); // Bookmark locations $bookmarkService->createBookmark($mediaLocation); @@ -1980,13 +1982,24 @@ public function testBookmarksAreSwappedAfterSwapLocation() $beforeSwap = $bookmarkService->loadBookmarks(); // Swaps the content referred to by the locations - $locationService->swapLocation($mediaLocation, $demoDesignLocation); + $locationService->swapLocation($demoDesignLocation, $contactUsLocation); $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); + $expectedIdsAfter = array_map(static function (Location $item) use ($demoDesignLocationId, $contactUsLocationId) { + if ($item->id === $demoDesignLocationId) { + return $contactUsLocationId; + } + + return $item->id; + }, $beforeSwap->items); + + $actualIdsAfter = array_map(static function (Location $item) use ($demoDesignLocationId, $contactUsLocationId) { + return $item->id; + }, $afterSwap->items); + + $this->assertEquals($expectedIdsAfter, $actualIdsAfter); } /** diff --git a/eZ/Publish/API/Repository/Values/Content/Query/Criterion/Bookmark.php b/eZ/Publish/API/Repository/Values/Content/Query/Criterion/Bookmark.php new file mode 100644 index 0000000000..faf57568af --- /dev/null +++ b/eZ/Publish/API/Repository/Values/Content/Query/Criterion/Bookmark.php @@ -0,0 +1,43 @@ +joinOnce( + 'location', + DoctrineDatabase::TABLE_BOOKMARKS, + 'bookmark', + 'location.node_id = bookmark.node_id' + ); + + return $queryBuilder->expr()->eq( + 'bookmark.user_id', + $queryBuilder->createNamedParameter( + (int)$criterion->value[0], + ParameterType::INTEGER + ) + ); + } +} diff --git a/eZ/Publish/Core/Persistence/Legacy/Filter/SortClauseQueryBuilder/Bookmark/IdSortClauseQueryBuilder.php b/eZ/Publish/Core/Persistence/Legacy/Filter/SortClauseQueryBuilder/Bookmark/IdSortClauseQueryBuilder.php new file mode 100644 index 0000000000..ac2c8ea4e0 --- /dev/null +++ b/eZ/Publish/Core/Persistence/Legacy/Filter/SortClauseQueryBuilder/Bookmark/IdSortClauseQueryBuilder.php @@ -0,0 +1,31 @@ +addSelect('bookmark.id'); + $queryBuilder->addOrderBy('bookmark.id', $sortClause->direction); + } +} diff --git a/eZ/Publish/Core/Persistence/Legacy/Tests/Filter/CriterionQueryBuilder/Location/BookmarkQueryBuilderTest.php b/eZ/Publish/Core/Persistence/Legacy/Tests/Filter/CriterionQueryBuilder/Location/BookmarkQueryBuilderTest.php new file mode 100644 index 0000000000..eb3e52bae0 --- /dev/null +++ b/eZ/Publish/Core/Persistence/Legacy/Tests/Filter/CriterionQueryBuilder/Location/BookmarkQueryBuilderTest.php @@ -0,0 +1,45 @@ + [ + new Criterion\Bookmark(14), + 'bookmark.user_id = :dcValue1', + ['dcValue1' => 14], + ]; + + yield 'Bookmarks locations for user_id=14 OR user_id=7' => [ + new Criterion\LogicalOr( + [ + new Criterion\Bookmark(14), + new Criterion\Bookmark(7), + ] + ), + '(bookmark.user_id = :dcValue1) OR (bookmark.user_id = :dcValue2)', + ['dcValue1' => 14, 'dcValue2' => 7], + ]; + } + + protected function getCriterionQueryBuilders(): iterable + { + return [new BookmarkQueryBuilder()]; + } +} diff --git a/eZ/Publish/Core/Repository/BookmarkService.php b/eZ/Publish/Core/Repository/BookmarkService.php index 05d5d08cde..bf9914289f 100644 --- a/eZ/Publish/Core/Repository/BookmarkService.php +++ b/eZ/Publish/Core/Repository/BookmarkService.php @@ -13,8 +13,11 @@ use eZ\Publish\API\Repository\Repository as RepositoryInterface; use eZ\Publish\API\Repository\Values\Bookmark\BookmarkList; use eZ\Publish\API\Repository\Values\Content\Location; +use eZ\Publish\API\Repository\Values\Content\Query; +use eZ\Publish\API\Repository\Values\Content\Query\Criterion; +use eZ\Publish\API\Repository\Values\Content\Query\SortClause; +use eZ\Publish\API\Repository\Values\Filter\Filter; use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; -use eZ\Publish\SPI\Persistence\Bookmark\Bookmark; use eZ\Publish\SPI\Persistence\Bookmark\CreateStruct; use eZ\Publish\SPI\Persistence\Bookmark\Handler as BookmarkHandler; @@ -97,16 +100,22 @@ public function loadBookmarks(int $offset = 0, int $limit = 25): BookmarkList { $currentUserId = $this->getCurrentUserId(); - $list = new BookmarkList(); - $list->totalCount = $this->bookmarkHandler->countUserBookmarks($currentUserId); - if ($list->totalCount > 0) { - $bookmarks = $this->bookmarkHandler->loadUserBookmarks($currentUserId, $offset, $limit); - - $list->items = array_map(function (Bookmark $bookmark) { - return $this->repository->getLocationService()->loadLocation($bookmark->locationId); - }, $bookmarks); + $filter = new Filter(); + try { + $filter + ->withCriterion(new Criterion\Bookmark($currentUserId)) + ->withSortClause(new SortClause\BookmarkId(Query::SORT_DESC)) + ->sliceBy($limit, $offset); + + $result = $this->repository->getlocationService()->find($filter, []); + } catch (\eZ\Publish\API\Repository\Exceptions\BadStateException $e) { + return new BookmarkList(); } + $list = new BookmarkList(); + $list->totalCount = $result->totalCount; + $list->items = $result->locations; + return $list; } diff --git a/eZ/Publish/Core/Repository/Tests/Service/Mock/BookmarkTest.php b/eZ/Publish/Core/Repository/Tests/Service/Mock/BookmarkTest.php index 5c23d98b3e..c21f197492 100644 --- a/eZ/Publish/Core/Repository/Tests/Service/Mock/BookmarkTest.php +++ b/eZ/Publish/Core/Repository/Tests/Service/Mock/BookmarkTest.php @@ -12,6 +12,7 @@ use eZ\Publish\API\Repository\LocationService; use eZ\Publish\API\Repository\PermissionResolver; use eZ\Publish\API\Repository\Values\Content\ContentInfo; +use eZ\Publish\API\Repository\Values\Content\LocationList; use eZ\Publish\Core\Repository\BookmarkService; use eZ\Publish\Core\Repository\Tests\Service\Mock\Base as BaseServiceMockTest; use eZ\Publish\Core\Repository\Values\Content\Location; @@ -219,28 +220,13 @@ public function testLoadBookmarks() $expectedItems = array_map(function ($locationId) { return $this->createLocation($locationId); }, range(1, $expectedTotalCount)); - - $this->bookmarkHandler - ->expects($this->once()) - ->method('countUserBookmarks') - ->with(self::CURRENT_USER_ID) - ->willReturn($expectedTotalCount); - - $this->bookmarkHandler - ->expects($this->once()) - ->method('loadUserBookmarks') - ->with(self::CURRENT_USER_ID, $offset, $limit) - ->willReturn(array_map(static function ($locationId) { - return new Bookmark(['locationId' => $locationId]); - }, range(1, $expectedTotalCount))); + $locationList = new LocationList(['totalCount' => $expectedTotalCount, 'locations' => $expectedItems]); $locationServiceMock = $this->createMock(LocationService::class); $locationServiceMock - ->expects($this->exactly($expectedTotalCount)) - ->method('loadLocation') - ->willReturnCallback(function ($locationId) { - return $this->createLocation($locationId); - }); + ->expects($this->once()) + ->method('find') + ->willReturn($locationList); $repository = $this->getRepositoryMock(); $repository @@ -254,27 +240,6 @@ public function testLoadBookmarks() $this->assertEquals($expectedItems, $bookmarks->items); } - /** - * @covers \eZ\Publish\Core\Repository\BookmarkService::loadBookmarks - */ - public function testLoadBookmarksEmptyList() - { - $this->bookmarkHandler - ->expects($this->once()) - ->method('countUserBookmarks') - ->with(self::CURRENT_USER_ID) - ->willReturn(0); - - $this->bookmarkHandler - ->expects($this->never()) - ->method('loadUserBookmarks'); - - $bookmarks = $this->createBookmarkService()->loadBookmarks(0, 10); - - $this->assertEquals(0, $bookmarks->totalCount); - $this->assertEmpty($bookmarks->items); - } - /** * @covers \eZ\Publish\Core\Repository\BookmarkService::isBookmarked */ diff --git a/eZ/Publish/SPI/Persistence/Bookmark/Handler.php b/eZ/Publish/SPI/Persistence/Bookmark/Handler.php index c335f40297..8a515d187c 100644 --- a/eZ/Publish/SPI/Persistence/Bookmark/Handler.php +++ b/eZ/Publish/SPI/Persistence/Bookmark/Handler.php @@ -41,6 +41,8 @@ public function loadByUserIdAndLocationId(int $userId, array $locationIds): arra /** * Loads bookmarks owned by user. * + * @deprecated Please use LocationService::find() and Criterion\Bookmark instead. + * * @param int $userId * @param int $offset the start offset for paging * @param int $limit the number of bookmarked locations returned @@ -52,6 +54,8 @@ public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1) /** * Count bookmarks owned by user. * + * @deprecated Please use LocationService::count() and Criterion\Bookmark instead. + * * @param int $userId * * @return int