diff --git a/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FieldValue/Handler/Keyword.php b/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FieldValue/Handler/Keyword.php index 700aaf1156..8b9704a6d0 100644 --- a/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FieldValue/Handler/Keyword.php +++ b/src/lib/Search/Legacy/Content/Common/Gateway/CriterionHandler/FieldValue/Handler/Keyword.php @@ -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', diff --git a/tests/integration/Core/Repository/KeywordFieldCriterionTest.php b/tests/integration/Core/Repository/KeywordFieldCriterionTest.php new file mode 100644 index 0000000000..845402ec6f --- /dev/null +++ b/tests/integration/Core/Repository/KeywordFieldCriterionTest.php @@ -0,0 +1,105 @@ +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; + } +}