Skip to content

Commit

Permalink
IBX-7172: Fixed Repository Filtering by multiple ObjectStateId criteria
Browse files Browse the repository at this point in the history
For more details see https://issues.ibexa.co/browse/IBX-7172 and #401

Key changes:

* Fixed Repository Filtering not working with multiple object state permissions

* Added integration test coverage

---------

Co-authored-by: Andrew Longosz <[email protected]>
  • Loading branch information
vidarl and alongosz authored Feb 19, 2024
1 parent 6e5fa6f commit aa31bb2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
72 changes: 72 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 Down Expand Up @@ -332,6 +335,75 @@ private function performAndAssertSimpleSortClauseQuery(FilteringSortClause $sort
self::assertSame(57, $contentList->getIterator()[0]->id);
}

/**
* @throws \eZ\Publish\API\Repository\Exceptions\Exception
*/
public function testObjectStateIdCriterionOnMultipleObjectStates(): void
{
$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';
$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->getVersionInfo()->getContentInfo(),
$objectStateGroup,
$objectStatePrivate
);

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

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

self::assertEquals(
1,
$results->getTotalCount(),
'Expected to find only one object which has state "not_locked" and "private"'
);

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,21 @@ public function buildQueryConstraint(
FilteringQueryBuilder $queryBuilder,
FilteringCriterion $criterion
): ?string {
$tableAlias = uniqid('osl_');

/** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion\ObjectStateId $criterion */
$queryBuilder
->joinOnce(
->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

0 comments on commit aa31bb2

Please sign in to comment.