Completed
Push — qa-cumulative-ezp-31278-31279-... ( cb92e6...b59938 )
by
unknown
21:37
created

prepareTestContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\API\Repository\Tests\SearchService;
10
11
use eZ\Publish\API\Repository\SearchService;
12
use eZ\Publish\API\Repository\Tests\BaseTest;
13
use eZ\Publish\API\Repository\Values\Content\Content;
14
use eZ\Publish\API\Repository\Values\Content\ContentCreateStruct;
15
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
16
use eZ\Publish\API\Repository\Values\Content\Query;
17
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
18
use eZ\Publish\Core\FieldType\RichText\Value as RichTextValue;
19
20
/**
21
 * Test case for full text search in the SearchService (for embed).
22
 *
23
 * @see \eZ\Publish\API\Repository\SearchService
24
 * @group integration
25
 * @group search
26
 * @group fulltext
27
 */
28
class SearchServiceFullTextEmbedTest extends BaseTest
29
{
30
    private const EMBEDDED_ARTICLE_NAME = 'test1';
31
32
    private static $createdIds = [];
33
34
    protected function setUp(): void
35
    {
36
        self::markTestIncomplete(
37
            'Requires EZP-31337 due to RichText being no longer available in Kernel'
38
        );
39
40
        parent::setUp();
41
42
        $repository = $this->getRepository(false);
43
44
        if (
45
            false === $repository->getSearchService()->supports(
46
                SearchService::CAPABILITY_ADVANCED_FULLTEXT
47
            )
48
        ) {
49
            $this->markTestSkipped(
50
                'Advanced FullText search is not supported by the current search engine'
51
            );
52
        }
53
    }
54
55
    public function testFullTextContentSearch(): void
56
    {
57
        $this->prepareTestContent();
58
59
        $searchService = $this->getRepository()->getSearchService();
60
61
        $query = new Query([
62
            'query' => new Criterion\FullText(self::EMBEDDED_ARTICLE_NAME),
63
        ]);
64
65
        $searchResult = $searchService->findContent($query);
66
67
        $this->assertGreaterThanOrEqual(2, $searchResult->totalCount);
68
        $this->assertResults($searchResult->searchHits);
69
    }
70
71
    public function testFullTextLocationSearch(): void
72
    {
73
        $this->prepareTestContent();
74
75
        $searchService = $this->getRepository()->getSearchService();
76
77
        $query = new LocationQuery([
78
            'query' => new Criterion\FullText(self::EMBEDDED_ARTICLE_NAME),
79
        ]);
80
81
        $searchResult = $searchService->findLocations($query);
82
83
        $this->assertGreaterThanOrEqual(2, $searchResult->totalCount);
84
        $this->assertResults($searchResult->searchHits);
85
    }
86
87
    private function hasTestPreparedContent(): bool
88
    {
89
        return !empty(self::$createdIds);
90
    }
91
92
    private function prepareTestContent(): void
93
    {
94
        if ($this->hasTestPreparedContent()) {
95
            return;
96
        }
97
98
        $contentService = $this->getRepository()->getContentService();
99
        $baseArticleStruct = $this->prepareBaseArticleStruct();
100
101
        $embeddedArticleStruct = $this->fillEmbeddedArticleStruct(clone $baseArticleStruct);
102
        $embeddedArticleContent = $contentService->publishVersion(
103
            $this->createContent($embeddedArticleStruct)->versionInfo
104
        );
105
106
        $mainArticleStruct = $this->fillMainArticleStruct(clone $baseArticleStruct, $embeddedArticleContent->id);
107
        $mainArticleContent = $contentService->publishVersion(
108
            $this->createContent($mainArticleStruct)->versionInfo
109
        );
110
111
        $this->refreshSearch($this->getRepository());
112
113
        self::$createdIds = [
114
            $embeddedArticleContent->id,
115
            $mainArticleContent->id,
116
        ];
117
    }
118
119
    private function prepareBaseArticleStruct(): ContentCreateStruct
120
    {
121
        $introDocument = new \DOMDocument();
122
        $introDocument->loadXML(
123
            <<<EOT
124
<?xml version="1.0" encoding="UTF-8"?>
125
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
126
<para>some paragraph</para>
127
</section>
128
EOT
129
        );
130
131
        $repository = $this->getRepository();
132
        $contentType = $repository->getContentTypeService()->loadContentTypeByIdentifier('article');
133
134
        $articleStruct = $repository->getContentService()->newContentCreateStruct($contentType, 'eng-GB');
135
        $articleStruct->setField('intro', new RichTextValue($introDocument), 'eng-GB');
136
137
        return $articleStruct;
138
    }
139
140
    private function fillEmbeddedArticleStruct(
141
        ContentCreateStruct $articleStruct
142
    ): ContentCreateStruct {
143
        $articleBodyDoc = new \DOMDocument();
144
        $articleBodyDoc->loadXML(
145
            <<<EOT
146
<?xml version="1.0" encoding="UTF-8"?>
147
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
148
<para>body-content</para>
149
</section>
150
EOT
151
        );
152
153
        $articleStruct->setField('title', self::EMBEDDED_ARTICLE_NAME);
154
        $articleStruct->setField('body', new RichTextValue($articleBodyDoc), 'eng-GB');
155
156
        return $articleStruct;
157
    }
158
159
    private function fillMainArticleStruct(
160
        ContentCreateStruct $articleStruct,
161
        int $embedContentId
162
    ): ContentCreateStruct {
163
        $mainArticleBodyDoc = new \DOMDocument();
164
        $mainArticleBodyDoc->loadXML(
165
            <<<EOT
166
<?xml version="1.0" encoding="UTF-8"?>
167
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
168
<para><ezembedinline xlink:href="ezcontent://{$embedContentId}" view="embed-inline"/></para>
169
</section>
170
EOT
171
        );
172
173
        $articleStruct->setField('title', 'test');
174
        $articleStruct->setField('body', new RichTextValue($mainArticleBodyDoc), 'eng-GB');
175
176
        return $articleStruct;
177
    }
178
179
    private function createContent(ContentCreateStruct $contentCreateStruct): Content
180
    {
181
        $repository = $this->getRepository();
182
183
        return $repository->getContentService()->createContent(
184
            $contentCreateStruct,
185
            [$repository->getLocationService()->newLocationCreateStruct(2)]
186
        );
187
    }
188
189
    private function assertResults(array $searchHits): void
190
    {
191
        $resultIds = [];
192
193
        /** @var \eZ\Publish\API\Repository\Values\Content\Search\SearchHit $contentItem */
194
        foreach ($searchHits as $contentItem) {
195
            $resultIds[] = $contentItem->valueObject->contentInfo->id;
196
        }
197
198
        self::assertCount(2, array_intersect($resultIds, self::$createdIds));
199
    }
200
}
201