-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
For more details see https://issues.ibexa.co/browse/IBX-8726 and #75 Key changes: * Added IsBookmarked criterion visitor * Added location_bookmarked_user_ids search field * [Tests] Added IsBookmarkedTest --------- Co-Authored-By: Konrad Oboza <[email protected]> Co-Authored-By: Paweł Niedzielski <[email protected]>
- Loading branch information
1 parent
c2333a0
commit 6dbe045
Showing
6 changed files
with
200 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/lib/Query/Location/CriterionVisitor/Location/IsBookmarked.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?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\Solr\Query\Location\CriterionVisitor\Location; | ||
|
||
use Ibexa\Contracts\Core\Repository\PermissionResolver; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; | ||
use Ibexa\Contracts\Solr\Query\CriterionVisitor; | ||
use LogicException; | ||
|
||
final class IsBookmarked extends CriterionVisitor | ||
{ | ||
private const SEARCH_FIELD = 'location_bookmarked_user_ids_mid'; | ||
|
||
private PermissionResolver $permissionResolver; | ||
|
||
public function __construct(PermissionResolver $permissionResolver) | ||
{ | ||
$this->permissionResolver = $permissionResolver; | ||
} | ||
|
||
public function canVisit(Criterion $criterion): bool | ||
{ | ||
return $criterion instanceof Criterion\Location\IsBookmarked | ||
&& $criterion->operator === Criterion\Operator::EQ; | ||
} | ||
|
||
public function visit( | ||
Criterion $criterion, | ||
CriterionVisitor $subVisitor = null | ||
): string { | ||
if (!is_array($criterion->value)) { | ||
throw new LogicException(sprintf( | ||
'Expected %s Criterion value to be an array, received %s', | ||
Criterion\Location\IsBookmarked::class, | ||
get_debug_type($criterion->value), | ||
)); | ||
} | ||
|
||
$userId = $this->permissionResolver | ||
->getCurrentUserReference() | ||
->getUserId(); | ||
|
||
$query = self::SEARCH_FIELD . ':"' . $userId . '"'; | ||
|
||
if (!$criterion->value[0]) { | ||
$query = 'NOT ' . $query; | ||
} | ||
|
||
return $query; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
tests/lib/Search/Query/Location/CriterionVisitor/Location/IsBookmarkedTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?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\Tests\Solr\Search\Query\Location\CriterionVisitor\Location; | ||
|
||
use Ibexa\Contracts\Core\Repository\PermissionResolver; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; | ||
use Ibexa\Contracts\Solr\Query\CriterionVisitor; | ||
use Ibexa\Core\Repository\Values\User\UserReference; | ||
use Ibexa\Solr\Query\Location\CriterionVisitor\Location\IsBookmarked; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \Ibexa\Solr\Query\Location\CriterionVisitor\Location\IsBookmarked | ||
*/ | ||
final class IsBookmarkedTest extends TestCase | ||
{ | ||
private const USER_ID = 123; | ||
|
||
private CriterionVisitor $visitor; | ||
|
||
/** @var \Ibexa\Contracts\Core\Repository\PermissionResolver&\PHPUnit\Framework\MockObject\MockObject */ | ||
private PermissionResolver $permissionResolver; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->permissionResolver = $this->createMock(PermissionResolver::class); | ||
$this->visitor = new IsBookmarked($this->permissionResolver); | ||
} | ||
|
||
/** | ||
* @dataProvider provideDataForTestCanVisit | ||
*/ | ||
public function testCanVisit( | ||
bool $expected, | ||
Criterion $criterion | ||
): void { | ||
self::assertSame( | ||
$expected, | ||
$this->visitor->canVisit($criterion) | ||
); | ||
} | ||
|
||
/** | ||
* @return iterable<array{ | ||
* bool, | ||
* \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion | ||
* }> | ||
*/ | ||
public function provideDataForTestCanVisit(): iterable | ||
{ | ||
yield 'Not supported criterion' => [ | ||
false, | ||
new Criterion\ContentId(123), | ||
]; | ||
|
||
yield 'Supported criterion' => [ | ||
true, | ||
new Criterion\Location\IsBookmarked(), | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideDataForTestVisit | ||
*/ | ||
public function testVisit( | ||
string $expected, | ||
Criterion $criterion | ||
): void { | ||
$this->mockPermissionResolverGetCurrentUserReference(); | ||
|
||
self::assertSame( | ||
$expected, | ||
$this->visitor->visit($criterion) | ||
); | ||
} | ||
|
||
/** | ||
* @return iterable<array{ | ||
* string, | ||
* \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion | ||
* }> | ||
*/ | ||
public function provideDataForTestVisit(): iterable | ||
{ | ||
yield 'Query for bookmarked locations' => [ | ||
'location_bookmarked_user_ids_mid:"123"', | ||
new Criterion\Location\IsBookmarked(), | ||
]; | ||
|
||
yield 'Query for not bookmarked locations' => [ | ||
'NOT location_bookmarked_user_ids_mid:"123"', | ||
new Criterion\Location\IsBookmarked(false), | ||
]; | ||
} | ||
|
||
private function mockPermissionResolverGetCurrentUserReference(): void | ||
{ | ||
$this->permissionResolver | ||
->method('getCurrentUserReference') | ||
->willReturn(new UserReference(self::USER_ID)); | ||
} | ||
} |