Skip to content

Commit

Permalink
Removed "preparing test", removed tests depends, small changes after CR
Browse files Browse the repository at this point in the history
  • Loading branch information
Michał Myszka committed Jan 31, 2020
1 parent 32ec531 commit f80b953
Showing 1 changed file with 54 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@

use eZ\Publish\API\Repository\SearchService;
use eZ\Publish\API\Repository\Tests\BaseTest;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\ContentCreateStruct;
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
use eZ\Publish\Core\FieldType\RichText\Value as RichTextValue;
use eZ\Publish\Core\Repository\Values\Content\Content;
use eZ\Publish\Core\Repository\Values\Content\ContentCreateStruct;

/**
* Test case for full text search in the SearchService (for embed).
Expand All @@ -26,55 +25,33 @@
* @group search
* @group fulltext
*/
class SearchServiceFulltextEmbedTest extends BaseTest
class SearchServiceFullTextEmbedTest extends BaseTest
{
private const EMBEDDED_ARTICLE_NAME = 'test1';

private static $createdIds = [];

protected function setUp()
protected function setUp(): void
{
parent::setUp();

$repository = $this->getRepository(false);

if (
false === $repository->getSearchService()->supports(
SearchService::CAPABILITY_ADVANCED_FULLTEXT
SearchService::CAPABILITY_ADVANCED_FULLTEXT
)
) {
$this->markTestSkipped('Advanced FullText search is not supported by the current search engine');
$this->markTestSkipped(
'Advanced FullText search is not supported by the current search engine'
);
}
}

public function testPrepareContent(): void
public function testFullTextContentSearch(): void
{
$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,
];
}
$this->prepareTestContent();

/**
* @depends testPrepareContent
*/
public function testFulltextContentSearch(): void
{
$searchService = $this->getRepository()->getSearchService();

$query = new Query([
Expand All @@ -87,11 +64,10 @@ public function testFulltextContentSearch(): void
$this->assertResults($searchResult->searchHits);
}

/**
* @depends testPrepareContent
*/
public function testFulltextLocationSearch(): void
public function testFullTextLocationSearch(): void
{
$this->prepareTestContent();

$searchService = $this->getRepository()->getSearchService();

$query = new LocationQuery([
Expand All @@ -104,6 +80,38 @@ public function testFulltextLocationSearch(): void
$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();
Expand All @@ -119,15 +127,15 @@ private function prepareBaseArticleStruct(): ContentCreateStruct
$repository = $this->getRepository();
$contentType = $repository->getContentTypeService()->loadContentTypeByIdentifier('article');

/** @var \eZ\Publish\Core\Repository\Values\Content\ContentCreateStruct $articleStruct */
$articleStruct = $repository->getContentService()->newContentCreateStruct($contentType, 'eng-GB');
$articleStruct->setField('intro', new RichTextValue($introDocument), 'eng-GB');

return $articleStruct;
}

private function fillEmbeddedArticleStruct(ContentCreateStruct $articleStruct): ContentCreateStruct
{
private function fillEmbeddedArticleStruct(
ContentCreateStruct $articleStruct
): ContentCreateStruct {
$articleBodyDoc = new \DOMDocument();
$articleBodyDoc->loadXML(
<<<EOT
Expand All @@ -144,8 +152,10 @@ private function fillEmbeddedArticleStruct(ContentCreateStruct $articleStruct):
return $articleStruct;
}

private function fillMainArticleStruct(ContentCreateStruct $articleStruct, int $embedContentId): ContentCreateStruct
{
private function fillMainArticleStruct(
ContentCreateStruct $articleStruct,
int $embedContentId
): ContentCreateStruct {
$mainArticleBodyDoc = new \DOMDocument();
$mainArticleBodyDoc->loadXML(
<<<EOT
Expand Down Expand Up @@ -176,13 +186,11 @@ private function assertResults(array $searchHits): void
{
$resultIds = [];

/** @var SearchHit $contentItem */
/** @var \eZ\Publish\API\Repository\Values\Content\Search\SearchHit $contentItem */
foreach ($searchHits as $contentItem) {
$resultIds[] = $contentItem->valueObject->contentInfo->id;
}

$this->assertTrue(
count(array_intersect($resultIds, self::$createdIds)) === 2
);
self::assertCount(2, array_intersect($resultIds, self::$createdIds));
}
}

0 comments on commit f80b953

Please sign in to comment.