Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function handle(
'f_def',
'ezkeyword_attribute_link',
'kwd_lnk',
'f_def.id = kwd_lnk.objectattribute_id'
'f_def.id = kwd_lnk.objectattribute_id AND kwd_lnk.version = f_def.version'
)
->innerJoin(
'kwd_lnk',
Expand Down
105 changes: 105 additions & 0 deletions tests/integration/Core/Repository/KeywordFieldCriterionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?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\Integration\Core\Repository;

use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Core\FieldType\Keyword\Value as KeywordValue;
use Ibexa\Tests\Integration\Core\RepositorySearchTestCase;

final class KeywordFieldCriterionTest extends RepositorySearchTestCase
{
private const CONTENT_TYPE_IDENTIFIER = 'keyword_criterion_test';
private const FIELD_IDENTIFIER = 'tags';
private const TEST_TAG = 'test-tag-1';

private ContentType $contentType;

protected function setUp(): void
{
parent::setUp();
$this->contentType = $this->createKeywordContentType();
}

public function testFindsContentWithKeywordInCurrentVersion(): void
{
$this->publishContentWithKeyword(self::TEST_TAG);
$this->refreshSearch();

self::assertSame(1, $this->countByKeyword(self::TEST_TAG));
}

public function testIgnoresKeywordFromArchivedVersion(): void
{
$published = $this->publishContentWithKeyword(self::TEST_TAG);
$this->removeKeywordAndPublish($published);
$this->refreshSearch();

self::assertSame(0, $this->countByKeyword(self::TEST_TAG));
}

private function createKeywordContentType(): ContentType
{
$contentTypeService = self::getContentTypeService();

$typeStruct = $contentTypeService->newContentTypeCreateStruct(self::CONTENT_TYPE_IDENTIFIER);
$typeStruct->mainLanguageCode = 'eng-GB';
$typeStruct->names = ['eng-GB' => 'Keyword criterion test'];

$fieldDef = $contentTypeService->newFieldDefinitionCreateStruct(self::FIELD_IDENTIFIER, 'ezkeyword');
$fieldDef->names = ['eng-GB' => 'Tags'];
$fieldDef->isSearchable = true;
$typeStruct->addFieldDefinition($fieldDef);

$draft = $contentTypeService->createContentType(
$typeStruct,
[$contentTypeService->loadContentTypeGroupByIdentifier('Content')]
);
$contentTypeService->publishContentTypeDraft($draft);

return $contentTypeService->loadContentTypeByIdentifier(self::CONTENT_TYPE_IDENTIFIER);
}

private function publishContentWithKeyword(string $keyword): Content
{
$contentService = self::getContentService();

$createStruct = $contentService->newContentCreateStruct($this->contentType, 'eng-GB');
$createStruct->setField(self::FIELD_IDENTIFIER, new KeywordValue([$keyword]));

return $contentService->publishVersion(
$contentService->createContent(
$createStruct,
[self::getLocationService()->newLocationCreateStruct(2)]
)->getVersionInfo()
);
}

private function removeKeywordAndPublish(Content $content): void
{
$contentService = self::getContentService();

$draft = $contentService->createContentDraft($content->getContentInfo());
$updateStruct = $contentService->newContentUpdateStruct();
$updateStruct->setField(self::FIELD_IDENTIFIER, new KeywordValue([]));
$contentService->updateContent($draft->getVersionInfo(), $updateStruct);
$contentService->publishVersion($draft->getVersionInfo());
}

private function countByKeyword(string $keyword): ?int
{
$query = new Query([
'filter' => new Criterion\Field(self::FIELD_IDENTIFIER, Criterion\Operator::EQ, $keyword),
]);

return self::getSearchService()->findContent($query)->totalCount;
}
}
Loading