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-7172: Fixed Repository Filtering by multiple ObjectStateId criteria #401

68 changes: 68 additions & 0 deletions eZ/Publish/API/Repository/Tests/Filtering/ContentFilteringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
use function count;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\ContentList;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
use eZ\Publish\API\Repository\Values\Filter\Filter;
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct;
use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct;
use eZ\Publish\Core\FieldType\Keyword;
use eZ\Publish\SPI\Repository\Values\Filter\FilteringSortClause;
use IteratorAggregate;
Expand All @@ -27,6 +30,13 @@
*/
final class ContentFilteringTest extends BaseRepositoryFilteringTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->getSetupFactory()->getRepository(true);
$this->contentProvider = new TestContentProvider($this->getRepository(true), $this);
alongosz marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Test that special cases of Location Sort Clauses are working correctly.
*
Expand Down Expand Up @@ -332,6 +342,64 @@ private function performAndAssertSimpleSortClauseQuery(FilteringSortClause $sort
self::assertSame(57, $contentList->getIterator()[0]->id);
}

public function testObjectStateIdCriterionOnMultipleObjectStates(): void
vidarl marked this conversation as resolved.
Show resolved Hide resolved
{
$contentService = $this->getRepository()->getContentService();
$contentTypeService = $this->getRepository()->getContentTypeService();
$locationService = $this->getRepository()->getLocationService();
$objectStateService = $this->getRepository()->getObjectStateService();

// Create additional Object States
$objectStateGroupStruct = new ObjectStateGroupCreateStruct();
$objectStateGroupStruct->identifier = 'domain';
$objectStateGroupStruct->names = ['eng-GB' => 'Domain'];
$objectStateGroupStruct->defaultLanguageCode = 'eng-GB';
$objectStateGroup = $objectStateService->createObjectStateGroup($objectStateGroupStruct);

$objectStateCreateStruct = new ObjectStateCreateStruct();
$objectStateCreateStruct->identifier = 'public';
$objectStateCreateStruct->names = ['eng-GB' => 'Public'];
$objectStateCreateStruct->defaultLanguageCode = 'eng-GB';
$objectStatePublic = $objectStateService->createObjectState($objectStateGroup, $objectStateCreateStruct);

$objectStateCreateStruct->identifier = 'private';
$objectStateCreateStruct->names = ['eng-GB' => 'Private'];
$objectStatePrivate = $objectStateService->createObjectState($objectStateGroup, $objectStateCreateStruct);

// Create a new content object and assign object state "Private" to it:
$contentCreate = $contentService->newContentCreateStruct(
$contentTypeService->loadContentTypeByIdentifier('folder'),
'eng-GB'
);
$contentCreate->setField('name', 'Private Folder');
$content = $contentService->createContent(
$contentCreate,
[$locationService->newLocationCreateStruct(2)]
);
$contentService->publishVersion(
$content->getVersionInfo()
);
$objectStateService->setContentState($content->contentInfo, $objectStateGroup, $objectStatePrivate);
vidarl marked this conversation as resolved.
Show resolved Hide resolved

$filter = new Filter();
$filter
->withCriterion(new Criterion\LogicalAnd([
new Criterion\ParentLocationId(2),
new Criterion\LogicalAnd([
new Criterion\ObjectStateId(1),
new Criterion\ObjectStateId(4),
]),
]));

$results = $this->find($filter);

self::assertEquals(1, $results->getTotalCount(), 'Expected to find only one object which has state "not_locked" and "private"');
/** @var \eZ\Publish\API\Repository\Values\Content\Location $result */
vidarl marked this conversation as resolved.
Show resolved Hide resolved
foreach ($results as $result) {
self::assertEquals($result->id, $content->id, 'Expected to find "Private Folder"');
}
}

public function getListOfSupportedSortClauses(): iterable
{
yield 'Content\\Id' => [SortClause\ContentId::class];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,23 @@ public function buildQueryConstraint(
FilteringQueryBuilder $queryBuilder,
FilteringCriterion $criterion
): ?string {
static $counter = 1;
$tableAlias = 'object_state_link_' . $counter;
++$counter;
vidarl marked this conversation as resolved.
Show resolved Hide resolved

/** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion\ObjectStateId $criterion */
$queryBuilder
->joinOnce(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider it as a non merge blocking comment:

Just a remark - if we will always generate custom table alias for each ObjectStateId criteria, then it would make sense to switch into simpler QueryBuilder::join, which does not contain additional checks against table uniqueness.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.. Changed joinOnce() into a join()

'content',
Gateway::OBJECT_STATE_LINK_TABLE,
'object_state_link',
'content.id = object_state_link.contentobject_id',
$tableAlias,
'content.id = ' . $tableAlias . '.contentobject_id',
);

$value = (array)$criterion->value;

return $queryBuilder->expr()->in(
'object_state_link.contentobject_state_id',
$tableAlias . '.contentobject_state_id',
$queryBuilder->createNamedParameter($value, Connection::PARAM_INT_ARRAY)
);
}
Expand Down
Loading