Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-6773: Bookmarks for non accessible contents cause exception #408

Open
wants to merge 10 commits into
base: 1.3
Choose a base branch
from
Open
8 changes: 5 additions & 3 deletions eZ/Publish/API/Repository/Tests/LocationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1965,13 +1965,15 @@ public function testBookmarksAreSwappedAfterSwapLocation()

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

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

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

// Bookmark locations
$bookmarkService->createBookmark($mediaLocation);
Expand All @@ -1980,13 +1982,13 @@ 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);
$this->assertEquals($contactUsLocationId, $afterSwap->items[0]->id);
$this->assertEquals($beforeSwap->items[1]->id, $afterSwap->items[1]->id);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions eZ/Publish/Core/Persistence/Cache/BookmarkHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ function (Bookmark $bookmark) {
}

/**
* @deprecated The "BookmarkHandler::loadUserBookmarks()" method is deprecated, will be removed in 5.0.0. Use "LocationService::find()" and "Criterion\IsBookmarked" instead.
*
* {@inheritdoc}
*/
public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1): array
Expand All @@ -99,6 +101,8 @@ public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1)
}

/**
* @deprecated The "BookmarkHandler::countUserBookmarks()" method is deprecated, will be removed in 5.0.0. Use "LocationService::count()" and "Criterion\IsBookmarked" instead.
*
* {@inheritdoc}
*/
public function countUserBookmarks(int $userId): int
Expand Down
4 changes: 4 additions & 0 deletions eZ/Publish/Core/Persistence/Legacy/Bookmark/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ abstract public function loadBookmarkDataByUserIdAndLocationId(int $userId, arra
/**
* Load data for all bookmarks owned by given $userId.
*
* @deprecated Gateway::loadUserBookmarks()" method is deprecated, will be removed in 5.0.0. Use "LocationService::find()" and "Criterion\IsBookmarked" instead.
*
* @param int $userId ID of user
* @param int $offset Offset to start listing from, 0 by default
* @param int $limit Limit for the listing. -1 by default (no limit)
Expand All @@ -55,6 +57,8 @@ abstract public function loadUserBookmarks(int $userId, int $offset = 0, int $li
/**
* Count bookmarks owned by given $userId.
*
* @deprecated The "Gateway::countUserBookmarks()" method is deprecated, will be removed in 5.0.0. Use "LocationService::count()" and "Criterion\IsBookmarked" instead.
*
* @param int $userId ID of user
*
* @return int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public function loadBookmarkDataByUserIdAndLocationId(int $userId, array $locati
}

/**
* @deprecated The "DoctrineDatabase::loadUserBookmarks()" method is deprecated, will be removed in 5.0.0. Use "LocationService::find()" and "Criterion\IsBookmarked" instead.
*
* {@inheritdoc}
*/
public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1): array
Expand All @@ -123,6 +125,8 @@ public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1)
}

/**
* @deprecated The "DoctrineDatabase::countUserBookmarks()" method is deprecated, will be removed in 5.0.0. Use "LocationService::count()" and "Criterion\IsBookmarked" instead.
*
* {@inheritdoc}
*/
public function countUserBookmarks(int $userId): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public function loadBookmarkDataByUserIdAndLocationId(int $userId, array $locati
}
}

/**
* @deprecated The "ExceptionConversion::loadUserBookmarks()" method is deprecated, will be removed in 5.0.0. Use "LocationService::find()" and "Criterion\IsBookmarked" instead.
*
* @return array
*/
public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1): array
{
try {
Expand All @@ -65,6 +70,9 @@ public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1)
}
}

/**
* @deprecated The "ExceptionConversion::countUserBookmarks()" method is deprecated, will be removed in 5.0.0. Use "LocationService::count()" and "Criterion\IsBookmarked" instead.
*/
public function countUserBookmarks(int $userId): int
{
try {
Expand Down
4 changes: 4 additions & 0 deletions eZ/Publish/Core/Persistence/Legacy/Bookmark/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public function loadByUserIdAndLocationId(int $userId, array $locationIds): arra
}

/**
* @deprecated The "Handler::loadUserBookmarks()" method is deprecated, will be removed in 5.0.0. Use "LocationService::find()" and "Criterion\IsBookmarked" instead.
*
* {@inheritdoc}
*/
public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1): array
Expand All @@ -84,6 +86,8 @@ public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1)
}

/**
* @deprecated The "Handler::countUserBookmarks()" method is deprecated, will be removed in 5.0.0. Use "LocationService::count()" and "Criterion\IsBookmarked" instead.
*
* {@inheritdoc}
*/
public function countUserBookmarks(int $userId): int
Expand Down
28 changes: 19 additions & 9 deletions eZ/Publish/Core/Repository/BookmarkService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@

use Exception;
use eZ\Publish\API\Repository\BookmarkService as BookmarkServiceInterface;
use eZ\Publish\API\Repository\Exceptions\BadStateException;
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\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;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause;

class BookmarkService implements BookmarkServiceInterface
{
Expand Down Expand Up @@ -97,16 +101,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\IsBookmarked($currentUserId))
->withSortClause(new SortClause\BookmarkId(Query::SORT_DESC))
->sliceBy($limit, $offset);

$result = $this->repository->getlocationService()->find($filter, []);
} catch (BadStateException $e) {
return new BookmarkList();
}
Comment on lines +112 to 114
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exception should probably be logged. We had plenty of situations in the past where people asked why something isn't working as it should, while we "swallowed" the exception.


$list = new BookmarkList();
$list->totalCount = $result->totalCount;
$list->items = $result->locations;

return $list;
}

Expand Down
48 changes: 5 additions & 43 deletions eZ/Publish/Core/Repository/Tests/Service/Mock/BookmarkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
*/
Expand All @@ -289,9 +254,6 @@ public function testLocationShouldNotBeBookmarked()
$this->assertFalse($this->createBookmarkService()->isBookmarked($this->createLocation(self::LOCATION_ID)));
}

/**
* @covers \eZ\Publish\Core\Repository\BookmarkService::isBookmarked
*/
public function testLocationShouldBeBookmarked()
{
$this->bookmarkHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ services:
eZ\Publish\Core\Persistence\Legacy\Filter\CriterionQueryBuilder\:
resource: '../../../../Persistence/Legacy/Filter/CriterionQueryBuilder/*'

Ibexa\Core\Persistence\Legacy\Filter\CriterionQueryBuilder\:
resource: '../../../../../../../src/lib/Persistence/Legacy/Filter/CriterionQueryBuilder/*'

eZ\Publish\Core\Persistence\Legacy\Filter\SortClauseQueryBuilder\:
resource: '../../../../Persistence/Legacy/Filter/SortClauseQueryBuilder/*'

Ibexa\Core\Persistence\Legacy\Filter\SortClauseQueryBuilder\:
resource: '../../../../../../../src/lib/Persistence/Legacy/Filter/SortClauseQueryBuilder/*'
4 changes: 4 additions & 0 deletions eZ/Publish/SPI/Persistence/Bookmark/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function loadByUserIdAndLocationId(int $userId, array $locationIds): arra
/**
* Loads bookmarks owned by user.
*
* @deprecated The "Handler::loadUserBookmarks()" method is deprecated, will be removed in 5.0.0. Use "LocationService::find()" and "Criterion\IsBookmarked" instead.
*
* @param int $userId
* @param int $offset the start offset for paging
* @param int $limit the number of bookmarked locations returned
Expand All @@ -52,6 +54,8 @@ public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1)
/**
* Count bookmarks owned by user.
*
* @deprecated The "Handler::countUserBookmarks()" method is deprecated, will be removed in 5.0.0. Use "LocationService::count()" and "Criterion\IsBookmarked" instead.
*
* @param int $userId
*
* @return int
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;

use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator\Specifications;
use eZ\Publish\SPI\Repository\Values\Filter\FilteringCriterion;

/**
* A criterion that matches locations of bookmarks for a given user id.
*
*
* Supported operators:
* - EQ: matches against a unique user id
*/
class IsBookmarked extends Criterion implements FilteringCriterion
{
/**
* Creates a new IsBookmarked criterion.
*
* @param int $value UserID for which bookmarked locations must be matched against
*
* @throws \InvalidArgumentException if a non numeric id is given
* @throws \InvalidArgumentException if the value type doesn't match the operator
*/
public function __construct($value)
{
parent::__construct(null, null, $value);
}

public function getSpecifications(): array
{
return [
new Specifications(Operator::EQ, Specifications::FORMAT_SINGLE, Specifications::TYPE_INTEGER),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause;

use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
use eZ\Publish\SPI\Repository\Values\Filter\FilteringSortClause;

/**
* Sets sort direction on the Bookmark ID for a location query containing a IsBookmarked criterion.
*/
class BookmarkId extends SortClause implements FilteringSortClause
{
public function __construct(string $sortDirection = Query::SORT_ASC)
{
parent::__construct('id', $sortDirection);
}
}
Loading
Loading