From b68ebb959906c4432d4531a18e3b5510db825122 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Wed, 30 Oct 2024 09:27:36 +0100 Subject: [PATCH] IBX-9121: Added support for IsUserEnabled criterion --- .../ContentFieldMapper/UserDocumentFields.php | 8 ++ .../Common/CriterionVisitor/IsUserEnabled.php | 38 ++++++++++ .../container/solr/criterion_visitors.yml | 4 + .../Query/BaseCriterionVisitorTestCase.php | 74 +++++++++++++++++++ .../CriterionVisitor/IsUserEnabledTest.php | 50 +++++++++++++ .../Location/IsBookmarkedTest.php | 50 ++++--------- .../Search/Query/Utils/Stub/TestCriterion.php | 24 ++++++ 7 files changed, 212 insertions(+), 36 deletions(-) create mode 100644 src/lib/Query/Common/CriterionVisitor/IsUserEnabled.php create mode 100644 tests/lib/Search/Query/BaseCriterionVisitorTestCase.php create mode 100644 tests/lib/Search/Query/Common/CriterionVisitor/IsUserEnabledTest.php create mode 100644 tests/lib/Search/Query/Utils/Stub/TestCriterion.php diff --git a/src/lib/FieldMapper/ContentFieldMapper/UserDocumentFields.php b/src/lib/FieldMapper/ContentFieldMapper/UserDocumentFields.php index e635c036..95b62170 100644 --- a/src/lib/FieldMapper/ContentFieldMapper/UserDocumentFields.php +++ b/src/lib/FieldMapper/ContentFieldMapper/UserDocumentFields.php @@ -48,6 +48,14 @@ public function mapFields(SPIContent $content): array ); } + if (isset($userField->value->externalData['enabled'])) { + $fields[] = new Field( + 'user_is_enabled', + $userField->value->externalData['enabled'], + new FieldType\BooleanField() + ); + } + return $fields; } diff --git a/src/lib/Query/Common/CriterionVisitor/IsUserEnabled.php b/src/lib/Query/Common/CriterionVisitor/IsUserEnabled.php new file mode 100644 index 00000000..43b6819e --- /dev/null +++ b/src/lib/Query/Common/CriterionVisitor/IsUserEnabled.php @@ -0,0 +1,38 @@ +value; + if (!is_array($value) || !is_bool($value[0])) { + throw new \LogicException( + sprintf( + '%s value should be of type array, received %s.', + Criterion\IsUserEnabled::class, + get_debug_type($value), + ) + ); + } + + return self::SEARCH_FIELD . ':' . $this->toString($value[0]); + } +} diff --git a/src/lib/Resources/config/container/solr/criterion_visitors.yml b/src/lib/Resources/config/container/solr/criterion_visitors.yml index 177cedee..9fb662e2 100644 --- a/src/lib/Resources/config/container/solr/criterion_visitors.yml +++ b/src/lib/Resources/config/container/solr/criterion_visitors.yml @@ -348,3 +348,7 @@ services: tags: - { name: ibexa.search.solr.query.content.criterion.visitor } - { name: ibexa.search.solr.query.location.criterion.visitor } + + Ibexa\Solr\Query\Common\CriterionVisitor\IsUserEnabled: + tags: + - { name: ibexa.search.solr.query.content.criterion.visitor } diff --git a/tests/lib/Search/Query/BaseCriterionVisitorTestCase.php b/tests/lib/Search/Query/BaseCriterionVisitorTestCase.php new file mode 100644 index 00000000..a740a7be --- /dev/null +++ b/tests/lib/Search/Query/BaseCriterionVisitorTestCase.php @@ -0,0 +1,74 @@ + + */ + abstract protected function provideDataForTestVisit(): iterable; + + /** + * @dataProvider provideDataForTestCanVisit + */ + public function testCanVisit( + bool $expected, + Criterion $criterion + ): void { + self::assertSame( + $expected, + $this->getVisitor()->canVisit($criterion) + ); + } + + /** + * @return iterable + */ + public function provideDataForTestCanVisit(): iterable + { + yield 'Not supported criterion' => [ + false, + new TestCriterion(), + ]; + + yield 'Supported criterion' => [ + true, + $this->getSupportedCriterion(), + ]; + } + + /** + * @dataProvider provideDataForTestVisit + */ + public function testVisit( + string $expectedQuery, + Criterion $criterion + ): void { + self::assertSame( + $expectedQuery, + $this->getVisitor()->visit($criterion) + ); + } +} diff --git a/tests/lib/Search/Query/Common/CriterionVisitor/IsUserEnabledTest.php b/tests/lib/Search/Query/Common/CriterionVisitor/IsUserEnabledTest.php new file mode 100644 index 00000000..378dd30f --- /dev/null +++ b/tests/lib/Search/Query/Common/CriterionVisitor/IsUserEnabledTest.php @@ -0,0 +1,50 @@ +criterionVisitor = new IsUserEnabled(); + } + + protected function getVisitor(): CriterionVisitor + { + return $this->criterionVisitor; + } + + protected function getSupportedCriterion(): Criterion + { + return new Criterion\IsUserEnabled(); + } + + protected function provideDataForTestVisit(): iterable + { + yield 'Query for enabled user' => [ + 'user_is_enabled_b:true', + new Criterion\IsUserEnabled(), + ]; + + yield 'Query for disabled user' => [ + 'user_is_enabled_b:false', + new Criterion\IsUserEnabled(false), + ]; + } +} diff --git a/tests/lib/Search/Query/Location/CriterionVisitor/Location/IsBookmarkedTest.php b/tests/lib/Search/Query/Location/CriterionVisitor/Location/IsBookmarkedTest.php index aa242b96..8066f670 100644 --- a/tests/lib/Search/Query/Location/CriterionVisitor/Location/IsBookmarkedTest.php +++ b/tests/lib/Search/Query/Location/CriterionVisitor/Location/IsBookmarkedTest.php @@ -13,12 +13,12 @@ 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; +use Ibexa\Tests\Solr\Search\Query\BaseCriterionVisitorTestCase; /** * @covers \Ibexa\Solr\Query\Location\CriterionVisitor\Location\IsBookmarked */ -final class IsBookmarkedTest extends TestCase +final class IsBookmarkedTest extends BaseCriterionVisitorTestCase { private const USER_ID = 123; @@ -33,49 +33,17 @@ protected function setUp(): void $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 - */ - 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, + string $expectedQuery, Criterion $criterion ): void { $this->mockPermissionResolverGetCurrentUserReference(); self::assertSame( - $expected, + $expectedQuery, $this->visitor->visit($criterion) ); } @@ -105,4 +73,14 @@ private function mockPermissionResolverGetCurrentUserReference(): void ->method('getCurrentUserReference') ->willReturn(new UserReference(self::USER_ID)); } + + protected function getVisitor(): CriterionVisitor + { + return $this->visitor; + } + + protected function getSupportedCriterion(): Criterion + { + return new Criterion\Location\IsBookmarked(); + } } diff --git a/tests/lib/Search/Query/Utils/Stub/TestCriterion.php b/tests/lib/Search/Query/Utils/Stub/TestCriterion.php new file mode 100644 index 00000000..5cfbecaa --- /dev/null +++ b/tests/lib/Search/Query/Utils/Stub/TestCriterion.php @@ -0,0 +1,24 @@ +