diff --git a/eZ/Publish/API/Repository/Tests/SearchService/SearchServiceFullTextEmbedTest.php b/eZ/Publish/API/Repository/Tests/SearchService/SearchServiceFullTextEmbedTest.php new file mode 100644 index 00000000000..ff223a05049 --- /dev/null +++ b/eZ/Publish/API/Repository/Tests/SearchService/SearchServiceFullTextEmbedTest.php @@ -0,0 +1,196 @@ +getRepository(false); + + if ( + false === $repository->getSearchService()->supports( + SearchService::CAPABILITY_ADVANCED_FULLTEXT + ) + ) { + $this->markTestSkipped( + 'Advanced FullText search is not supported by the current search engine' + ); + } + } + + public function testFullTextContentSearch(): void + { + $this->prepareTestContent(); + + $searchService = $this->getRepository()->getSearchService(); + + $query = new Query([ + 'query' => new Criterion\FullText(self::EMBEDDED_ARTICLE_NAME), + ]); + + $searchResult = $searchService->findContent($query); + + $this->assertGreaterThanOrEqual(2, $searchResult->totalCount); + $this->assertResults($searchResult->searchHits); + } + + public function testFullTextLocationSearch(): void + { + $this->prepareTestContent(); + + $searchService = $this->getRepository()->getSearchService(); + + $query = new LocationQuery([ + 'query' => new Criterion\FullText(self::EMBEDDED_ARTICLE_NAME), + ]); + + $searchResult = $searchService->findLocations($query); + + $this->assertGreaterThanOrEqual(2, $searchResult->totalCount); + $this->assertResults($searchResult->searchHits); + } + + private function hasTestPreparedContent(): bool + { + return !empty(self::$createdIds); + } + + private function prepareTestContent(): void + { + if ($this->hasTestPreparedContent()) { + return; + } + + $contentService = $this->getRepository()->getContentService(); + $baseArticleStruct = $this->prepareBaseArticleStruct(); + + $embeddedArticleStruct = $this->fillEmbeddedArticleStruct(clone $baseArticleStruct); + $embeddedArticleContent = $contentService->publishVersion( + $this->createContent($embeddedArticleStruct)->versionInfo + ); + + $mainArticleStruct = $this->fillMainArticleStruct(clone $baseArticleStruct, $embeddedArticleContent->id); + $mainArticleContent = $contentService->publishVersion( + $this->createContent($mainArticleStruct)->versionInfo + ); + + $this->refreshSearch($this->getRepository()); + + self::$createdIds = [ + $embeddedArticleContent->id, + $mainArticleContent->id, + ]; + } + + private function prepareBaseArticleStruct(): ContentCreateStruct + { + $introDocument = new \DOMDocument(); + $introDocument->loadXML( + << +
+some paragraph +
+EOT + ); + + $repository = $this->getRepository(); + $contentType = $repository->getContentTypeService()->loadContentTypeByIdentifier('article'); + + $articleStruct = $repository->getContentService()->newContentCreateStruct($contentType, 'eng-GB'); + $articleStruct->setField('intro', new RichTextValue($introDocument), 'eng-GB'); + + return $articleStruct; + } + + private function fillEmbeddedArticleStruct( + ContentCreateStruct $articleStruct + ): ContentCreateStruct { + $articleBodyDoc = new \DOMDocument(); + $articleBodyDoc->loadXML( + << +
+body-content +
+EOT + ); + + $articleStruct->setField('title', self::EMBEDDED_ARTICLE_NAME); + $articleStruct->setField('body', new RichTextValue($articleBodyDoc), 'eng-GB'); + + return $articleStruct; + } + + private function fillMainArticleStruct( + ContentCreateStruct $articleStruct, + int $embedContentId + ): ContentCreateStruct { + $mainArticleBodyDoc = new \DOMDocument(); + $mainArticleBodyDoc->loadXML( + << +
+ +
+EOT + ); + + $articleStruct->setField('title', 'test'); + $articleStruct->setField('body', new RichTextValue($mainArticleBodyDoc), 'eng-GB'); + + return $articleStruct; + } + + private function createContent(ContentCreateStruct $contentCreateStruct): Content + { + $repository = $this->getRepository(); + + return $repository->getContentService()->createContent( + $contentCreateStruct, + [$repository->getLocationService()->newLocationCreateStruct(2)] + ); + } + + private function assertResults(array $searchHits): void + { + $resultIds = []; + + /** @var \eZ\Publish\API\Repository\Values\Content\Search\SearchHit $contentItem */ + foreach ($searchHits as $contentItem) { + $resultIds[] = $contentItem->valueObject->contentInfo->id; + } + + self::assertCount(2, array_intersect($resultIds, self::$createdIds)); + } +} diff --git a/phpunit-integration-legacy-solr.xml b/phpunit-integration-legacy-solr.xml index 3335138287f..b1c6a46ea0b 100644 --- a/phpunit-integration-legacy-solr.xml +++ b/phpunit-integration-legacy-solr.xml @@ -27,6 +27,7 @@ --> eZ/Publish/API/Repository/Tests/Values/User/Limitation eZ/Publish/API/Repository/Tests/FieldType/ + eZ/Publish/API/Repository/Tests/SearchService/ eZ/Publish/API/Repository/Tests/RepositoryTest.php eZ/Publish/API/Repository/Tests/SectionServiceTest.php eZ/Publish/API/Repository/Tests/LanguageServiceTest.php