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

Feature/NGSTACK-595 tag id query condition/parameter #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/Core/Site/QueryType/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ protected function configureBaseOptions(OptionsResolver $resolver): void
'section',
'state',
'visible',
'tag_id',
]);
$resolver->setDefaults([
'sort' => [],
Expand All @@ -214,6 +215,8 @@ protected function configureBaseOptions(OptionsResolver $resolver): void
$resolver->setAllowedTypes('creation_date', ['int', 'string', 'array']);
$resolver->setAllowedTypes('modification_date', ['int', 'string', 'array']);
$resolver->setAllowedTypes('state', ['array']);
$resolver->setAllowedTypes('tag_id', ['int', 'array']);

$resolver->setAllowedValues('visible', [true, false, null]);

$resolver->setNormalizer('limit', static fn (Options $options, $value) => $value ?? 25);
Expand Down Expand Up @@ -301,6 +304,9 @@ private function resolveCriterionDefinitions(string $name, $parameters): array
case 'state':
case 'is_field_empty':
return $criterionDefinitionResolver->resolveTargets($name, $parameters);

case 'tag_id':
return $criterionDefinitionResolver->resolve($name, $parameters);
}

return [];
Expand Down
13 changes: 13 additions & 0 deletions lib/Core/Site/QueryType/CriteriaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Netgen\IbexaSearchExtra\API\Values\Content\Query\Criterion\ObjectStateIdentifier;
use Netgen\IbexaSearchExtra\API\Values\Content\Query\Criterion\SectionIdentifier;
use Netgen\IbexaSearchExtra\API\Values\Content\Query\Criterion\Visible;
use Netgen\TagsBundle\API\Repository\Values\Content\Query\Criterion\TagId;
use function count;
use function is_array;
use function is_int;
Expand Down Expand Up @@ -108,6 +109,9 @@ private function dispatchBuild(CriterionDefinition $definition): ?Criterion

case 'is_field_empty':
return $this->buildIsFieldEmpty($definition);

case 'tag_id':
return $this->buildTag($definition);
}

throw new InvalidArgumentException(
Expand Down Expand Up @@ -336,4 +340,13 @@ private function buildIsFieldEmpty(CriterionDefinition $definition): ?IsFieldEmp

return new IsFieldEmpty((string) $definition->target, (bool) $definition->value);
}

private function buildTag(CriterionDefinition $definition): ?TagId
{
if ($definition->value === null) {
return null;
}

return new TagId($definition->value);
}
}
47 changes: 47 additions & 0 deletions tests/lib/Unit/Core/Site/QueryType/Base/BaseQueryTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Netgen\IbexaSiteApi\Core\Site\QueryType\QueryType;
use Netgen\IbexaSiteApi\Core\Site\Settings;
use Netgen\IbexaSiteApi\Tests\Unit\Core\Site\QueryType\QueryTypeBaseTest;
use Netgen\TagsBundle\API\Repository\Values\Content\Query\Criterion\TagId;

/**
* Base QueryType stub test case.
Expand Down Expand Up @@ -243,6 +244,46 @@ public function providerForTestGetQuery(): array
],
]),
],
[
true,
[
'tag_id' => 223,
],
new Query([
'filter' => new TagId(223),
]),
],
[
true,
[
'tag_id' => [223, 224, 1],
],
new Query([
'filter' => new TagId([223, 224, 1]),
]),
],
[
true,
[
'tag_id' => [
'eq' => 225,
],
],
new Query([
'filter' => new TagId(225),
]),
],
[
true,
[
'tag_id' => [
'in' => [225, 226],
],
],
new Query([
'filter' => new TagId([225, 226]),
]),
],
];
}

Expand Down Expand Up @@ -286,6 +327,11 @@ public function providerForTestGetQueryWithInvalidOptions(): array
'is_field_empty' => [true],
],
],
[
[
'tag_id' => 'ten',
],
],
];
}

Expand Down Expand Up @@ -342,6 +388,7 @@ protected function getSupportedParameters(): array
'section',
'state',
'visible',
'tag_id',
'sort',
'limit',
'offset',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function testGetSupportedParameters(): void
'section',
'state',
'visible',
'tag_id',
'sort',
'limit',
'offset',
Expand Down
47 changes: 47 additions & 0 deletions tests/lib/Unit/Core/Site/QueryType/Content/FetchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Netgen\IbexaSiteApi\Core\Site\QueryType\QueryType;
use Netgen\IbexaSiteApi\Core\Site\Settings;
use Netgen\IbexaSiteApi\Tests\Unit\Core\Site\QueryType\QueryTypeBaseTest;
use Netgen\TagsBundle\API\Repository\Values\Content\Query\Criterion\TagId;

/**
* Fetch Content QueryType test case.
Expand Down Expand Up @@ -291,6 +292,46 @@ public function providerForTestGetQuery(): array
],
]),
],
[
true,
[
'tag_id' => 223,
],
new Query([
'filter' => new TagId(223),
]),
],
[
true,
[
'tag_id' => [223, 224, 1],
],
new Query([
'filter' => new TagId([223, 224, 1]),
]),
],
[
true,
[
'tag_id' => [
'eq' => 225,
],
],
new Query([
'filter' => new TagId(225),
]),
],
[
true,
[
'tag_id' => [
'in' => [225, 226],
],
],
new Query([
'filter' => new TagId([225, 226]),
]),
],
];
}

Expand Down Expand Up @@ -329,6 +370,11 @@ public function providerForTestGetQueryWithInvalidOptions(): array
],
],
],
[
[
'tag_id' => '223',
],
],
];
}

Expand Down Expand Up @@ -385,6 +431,7 @@ protected function getSupportedParameters(): array
'section',
'state',
'visible',
'tag_id',
'sort',
'limit',
'offset',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,66 @@ public function providerForTestGetQuery(): array
],
]),
],
[
true,
[
'content' => $contentWithTags,
'tag_id' => 223,
],
new Query([
'filter' => new LogicalAnd([
new TagId(223),
new TagId([1, 2, 3, 4]),
new LogicalNot(new ContentId(42)),
]),
]),
],
[
true,
[
'content' => $contentWithTags,
'tag_id' => [223, 224, 1],
],
new Query([
'filter' => new LogicalAnd([
new TagId([223, 224, 1]),
new TagId([1, 2, 3, 4]),
new LogicalNot(new ContentId(42)),
]),
]),
],
[
true,
[
'content' => $contentWithTags,
'tag_id' => [
'eq' => 225,
],
],
new Query([
'filter' => new LogicalAnd([
new TagId(225),
new TagId([1, 2, 3, 4]),
new LogicalNot(new ContentId(42)),
]),
]),
],
[
true,
[
'content' => $contentWithTags,
'tag_id' => [
'in' => [225, 226],
],
],
new Query([
'filter' => new LogicalAnd([
new TagId([225, 226]),
new TagId([1, 2, 3, 4]),
new LogicalNot(new ContentId(42)),
]),
]),
],
];
}

Expand Down Expand Up @@ -260,6 +320,12 @@ public function providerForTestGetQueryWithInvalidOptions(): array
'offset' => 'ten',
],
],
[
[
'content' => $content,
'tag_id' => 'ten',
],
],
];
}

Expand Down Expand Up @@ -427,6 +493,7 @@ protected function getSupportedParameters(): array
'section',
'state',
'visible',
'tag_id',
'sort',
'limit',
'offset',
Expand Down
Loading