From 118afaadfb30cf8995666c5437c5a9dc81d7cc85 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Tue, 22 Oct 2024 11:19:16 +0200 Subject: [PATCH 01/10] Added IsUserEnabled --- .../Common/CriterionVisitor/IsUserEnabled.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/lib/Query/Common/CriterionVisitor/IsUserEnabled.php 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]); + } +} From db662af6b4c0ef6edf70fd0be43221725e01d3c5 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Tue, 22 Oct 2024 11:19:39 +0200 Subject: [PATCH 02/10] [DI] Added service definition --- .../Resources/config/container/solr/criterion_visitors.yml | 4 ++++ 1 file changed, 4 insertions(+) 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 } From f8cf23787e6f8f5f3802d0656c87a21af73b6ee8 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Tue, 22 Oct 2024 11:19:56 +0200 Subject: [PATCH 03/10] Added index field user_is_enabled --- .../FieldMapper/ContentFieldMapper/UserDocumentFields.php | 8 ++++++++ 1 file changed, 8 insertions(+) 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; } From d8da5ed495e1b4b10c22f2371a7a1fb128f81030 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Tue, 22 Oct 2024 11:20:18 +0200 Subject: [PATCH 04/10] [Tests] Added BaseCriterionVisitorTestCase --- .../Query/BaseCriterionVisitorTestCase.php | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 tests/lib/Search/Query/BaseCriterionVisitorTestCase.php diff --git a/tests/lib/Search/Query/BaseCriterionVisitorTestCase.php b/tests/lib/Search/Query/BaseCriterionVisitorTestCase.php new file mode 100644 index 00000000..a2472435 --- /dev/null +++ b/tests/lib/Search/Query/BaseCriterionVisitorTestCase.php @@ -0,0 +1,73 @@ + + */ + 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 Criterion\ContentId(123), + ]; + + yield 'Supported criterion' => [ + true, + $this->getSupportedCriterion(), + ]; + } + + /** + * @dataProvider provideDataForTestVisit + */ + public function testVisit( + string $expectedQuery, + Criterion $criterion + ): void { + self::assertSame( + $expectedQuery, + $this->getVisitor()->visit($criterion) + ); + } +} From f5e1e2f64d658eab3bcbc565bf93cf351dfd4990 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Tue, 22 Oct 2024 11:20:53 +0200 Subject: [PATCH 05/10] [Tests] Added IsUserEnabledTest --- .../CriterionVisitor/IsUserEnabledTest.php | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 tests/lib/Search/Query/Common/CriterionVisitor/IsUserEnabledTest.php 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), + ]; + } +} From 193fce6ed89036ef092358bff737081045f84edb Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Tue, 22 Oct 2024 11:21:08 +0200 Subject: [PATCH 06/10] [Tests] Adjusted IsBookmarkedTest --- .../Location/IsBookmarkedTest.php | 50 ++++++------------- 1 file changed, 14 insertions(+), 36 deletions(-) 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(); + } } From 05ed457bcf8b674ff91854fc2a3ddeda79cac246 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Tue, 22 Oct 2024 11:34:34 +0200 Subject: [PATCH 07/10] [Tests] Added TestCriterion --- .../Search/Query/Utils/Stub/TestCriterion.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/lib/Search/Query/Utils/Stub/TestCriterion.php 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..71fcad10 --- /dev/null +++ b/tests/lib/Search/Query/Utils/Stub/TestCriterion.php @@ -0,0 +1,21 @@ + Date: Tue, 22 Oct 2024 11:34:50 +0200 Subject: [PATCH 08/10] [Tests] Adjusted BaseCriterionVisitorTestCase --- tests/lib/Search/Query/BaseCriterionVisitorTestCase.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/lib/Search/Query/BaseCriterionVisitorTestCase.php b/tests/lib/Search/Query/BaseCriterionVisitorTestCase.php index a2472435..a740a7be 100644 --- a/tests/lib/Search/Query/BaseCriterionVisitorTestCase.php +++ b/tests/lib/Search/Query/BaseCriterionVisitorTestCase.php @@ -10,6 +10,7 @@ use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; use Ibexa\Contracts\Solr\Query\CriterionVisitor; +use Ibexa\Tests\Solr\Search\Query\Utils\Stub\TestCriterion; use PHPUnit\Framework\TestCase; abstract class BaseCriterionVisitorTestCase extends TestCase @@ -49,7 +50,7 @@ public function provideDataForTestCanVisit(): iterable { yield 'Not supported criterion' => [ false, - new Criterion\ContentId(123), + new TestCriterion(), ]; yield 'Supported criterion' => [ From de0b6364eccc4a607b405c1a8a9ef16e89c59249 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Thu, 24 Oct 2024 09:13:41 +0200 Subject: [PATCH 09/10] Fixed cs --- tests/lib/Search/Query/Utils/Stub/TestCriterion.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/lib/Search/Query/Utils/Stub/TestCriterion.php b/tests/lib/Search/Query/Utils/Stub/TestCriterion.php index 71fcad10..8bd4112a 100644 --- a/tests/lib/Search/Query/Utils/Stub/TestCriterion.php +++ b/tests/lib/Search/Query/Utils/Stub/TestCriterion.php @@ -12,7 +12,9 @@ final class TestCriterion extends Criterion { - public function __construct() {} + public function __construct() + { + } public function getSpecifications(): array { From 2e80804d2bf44f45a44d5d34956fcf7cb4a29c00 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Thu, 24 Oct 2024 09:17:39 +0200 Subject: [PATCH 10/10] [SonarCloud] Fixed reported issue --- tests/lib/Search/Query/Utils/Stub/TestCriterion.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/lib/Search/Query/Utils/Stub/TestCriterion.php b/tests/lib/Search/Query/Utils/Stub/TestCriterion.php index 8bd4112a..5cfbecaa 100644 --- a/tests/lib/Search/Query/Utils/Stub/TestCriterion.php +++ b/tests/lib/Search/Query/Utils/Stub/TestCriterion.php @@ -14,6 +14,7 @@ final class TestCriterion extends Criterion { public function __construct() { + // No implementation needed. Used for test purposes only. } public function getSpecifications(): array