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-9121: Added support for IsUserEnabled criterion #77

Merged
merged 10 commits into from
Oct 30, 2024
8 changes: 8 additions & 0 deletions src/lib/FieldMapper/ContentFieldMapper/UserDocumentFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
38 changes: 38 additions & 0 deletions src/lib/Query/Common/CriterionVisitor/IsUserEnabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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\Common\CriterionVisitor;

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Solr\Query\CriterionVisitor;

final class IsUserEnabled extends CriterionVisitor
{
private const SEARCH_FIELD = 'user_is_enabled_b';

public function canVisit(Criterion $criterion): bool
{
return $criterion instanceof Criterion\IsUserEnabled;
}

public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null): string
{
$value = $criterion->value;
if (!is_array($value) || !is_bool($value[0])) {
throw new \LogicException(
sprintf(
'%s value should be of type array<bool>, received %s.',
Criterion\IsUserEnabled::class,
get_debug_type($value),
)
);
}

return self::SEARCH_FIELD . ':' . $this->toString($value[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
konradoboza marked this conversation as resolved.
Show resolved Hide resolved
74 changes: 74 additions & 0 deletions tests/lib/Search/Query/BaseCriterionVisitorTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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;

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
{
abstract protected function getVisitor(): CriterionVisitor;

abstract protected function getSupportedCriterion(): Criterion;

/**
* @return iterable<array{
* string,
* \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion
* }>
*/
abstract protected function provideDataForTestVisit(): iterable;

/**
* @dataProvider provideDataForTestCanVisit
*/
public function testCanVisit(
bool $expected,
Criterion $criterion
): void {
self::assertSame(
$expected,
$this->getVisitor()->canVisit($criterion)
);
}

/**
* @return iterable<array{
* bool,
* \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion
* }>
*/
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)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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\Common\CriterionVisitor;

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Solr\Query\CriterionVisitor;
use Ibexa\Solr\Query\Common\CriterionVisitor\IsUserEnabled;
use Ibexa\Tests\Solr\Search\Query\BaseCriterionVisitorTestCase;

/**
* @covers \Ibexa\Solr\Query\Common\CriterionVisitor\IsUserEnabled
*/
final class IsUserEnabledTest extends BaseCriterionVisitorTestCase
{
private CriterionVisitor $criterionVisitor;

protected function setUp(): void
{
$this->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),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<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,
string $expectedQuery,
Criterion $criterion
): void {
$this->mockPermissionResolverGetCurrentUserReference();

self::assertSame(
$expected,
$expectedQuery,
$this->visitor->visit($criterion)
);
}
Expand Down Expand Up @@ -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();
}
}
24 changes: 24 additions & 0 deletions tests/lib/Search/Query/Utils/Stub/TestCriterion.php
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\Tests\Solr\Search\Query\Utils\Stub;

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

final class TestCriterion extends Criterion
{
public function __construct()
{
// No implementation needed. Used for test purposes only.
}

public function getSpecifications(): array
{
return [];
}
}
Loading