Completed
Push — EZP-31226-indexing-depth-doesn... ( fa7ecc )
by
unknown
23:16 queued 03:55
created

fillMainArticleStruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
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\LocationQuery;
14
use eZ\Publish\API\Repository\Values\Content\Query;
15
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
16
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
17
use eZ\Publish\Core\FieldType\RichText\Value as RichTextValue;
18
use eZ\Publish\Core\Repository\Values\Content\Content;
19
use eZ\Publish\Core\Repository\Values\Content\ContentCreateStruct;
20
21
/**
22
 * Test case for full text search in the SearchService (for embed).
23
 *
24
 * @see \eZ\Publish\API\Repository\SearchService
25
 * @group integration
26
 * @group search
27
 * @group fulltext
28
 */
29
class SearchServiceFulltextEmbedTest extends BaseTest
30
{
31
    private const EMBEDDED_ARTICLE_NAME = 'test1';
32
33
    private static $createdIds = [];
34
35
    protected function setUp()
36
    {
37
        parent::setUp();
38
39
        $repository = $this->getRepository(false);
40
41
        if (false === $repository->getSearchService()->supports(SearchService::CAPABILITY_ADVANCED_FULLTEXT)) {
42
            $this->markTestSkipped('Advanced FullText search is not supported by the current search engine');
43
        }
44
    }
45
46
    public function testPrepareContent(): void
47
    {
48
        $contentService = $this->getRepository()->getContentService();
49
        $baseArticleStruct = $this->prepareBaseArticleStruct();
50
51
        $embeddedArticleStruct = $this->fillEmbeddedArticleStruct(clone $baseArticleStruct);
52
        $embeddedArticleContent = $contentService->publishVersion(
53
            $this->createContent($embeddedArticleStruct)->versionInfo
54
        );
55
56
        $mainArticleStruct = $this->fillMainArticleStruct(clone $baseArticleStruct, $embeddedArticleContent->id);
57
        $mainArticleContent = $contentService->publishVersion(
58
            $this->createContent($mainArticleStruct)->versionInfo
59
        );
60
61
        $this->refreshSearch($this->getRepository());
62
63
        self::$createdIds = [
64
            $embeddedArticleContent->id,
65
            $mainArticleContent->id,
66
        ];
67
    }
68
69
    /**
70
     * @depends testPrepareContent
71
     */
72
    public function testFulltextContentSearch(): void
73
    {
74
        $searchService = $this->getRepository()->getSearchService();
75
76
        $query = new Query([
77
            'query' => new Criterion\FullText(self::EMBEDDED_ARTICLE_NAME),
78
        ]);
79
80
        $searchResult = $searchService->findContent($query);
81
82
        $this->assertGreaterThanOrEqual(2, $searchResult->totalCount);
83
        $this->assertResults($searchResult->searchHits);
84
    }
85
86
    /**
87
     * @depends testPrepareContent
88
     */
89
    public function testFulltextLocationSearch(): void
90
    {
91
        $searchService = $this->getRepository()->getSearchService();
92
93
        $query = new LocationQuery([
94
            'query' => new Criterion\FullText(self::EMBEDDED_ARTICLE_NAME),
95
        ]);
96
97
        $searchResult = $searchService->findLocations($query);
98
99
        $this->assertGreaterThanOrEqual(2, $searchResult->totalCount);
100
        $this->assertResults($searchResult->searchHits);
101
    }
102
103
    private function prepareBaseArticleStruct(): ContentCreateStruct
104
    {
105
        $introDocument = new \DOMDocument();
106
        $introDocument->loadXML(
107
            <<<EOT
108
<?xml version="1.0" encoding="UTF-8"?>
109
<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">
110
<para>some paragraph</para>
111
</section>
112
EOT
113
        );
114
115
        $repository = $this->getRepository();
116
        $contentType = $repository->getContentTypeService()->loadContentTypeByIdentifier('article');
117
118
        /** @var \eZ\Publish\Core\Repository\Values\Content\ContentCreateStruct $articleStruct */
119
        $articleStruct = $repository->getContentService()->newContentCreateStruct($contentType, 'eng-GB');
120
        $articleStruct->setField('intro', new RichTextValue($introDocument), 'eng-GB');
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\FieldType\RichText\Value has been deprecated with message: since 7.4, use \EzSystems\EzPlatformRichText\eZ\FieldType\RichText\Value from EzPlatformRichTextBundle.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
121
122
        return $articleStruct;
123
    }
124
125
    private function fillEmbeddedArticleStruct(ContentCreateStruct $articleStruct): ContentCreateStruct
126
    {
127
        $articleBodyDoc = new \DOMDocument();
128
        $articleBodyDoc->loadXML(
129
            <<<EOT
130
<?xml version="1.0" encoding="UTF-8"?>
131
<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">
132
<para>body-content</para>
133
</section>
134
EOT
135
        );
136
137
        $articleStruct->setField('title', self::EMBEDDED_ARTICLE_NAME);
138
        $articleStruct->setField('body', new RichTextValue($articleBodyDoc), 'eng-GB');
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\FieldType\RichText\Value has been deprecated with message: since 7.4, use \EzSystems\EzPlatformRichText\eZ\FieldType\RichText\Value from EzPlatformRichTextBundle.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
139
140
        return $articleStruct;
141
    }
142
143
    private function fillMainArticleStruct(ContentCreateStruct $articleStruct, int $embedContentId): ContentCreateStruct
144
    {
145
        $mainArticleBodyDoc = new \DOMDocument();
146
        $mainArticleBodyDoc->loadXML(
147
            <<<EOT
148
<?xml version="1.0" encoding="UTF-8"?>
149
<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">
150
<para><ezembedinline xlink:href="ezcontent://{$embedContentId}" view="embed-inline"/></para>
151
</section>
152
EOT
153
        );
154
155
        $articleStruct->setField('title', 'test');
156
        $articleStruct->setField('body', new RichTextValue($mainArticleBodyDoc), 'eng-GB');
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\FieldType\RichText\Value has been deprecated with message: since 7.4, use \EzSystems\EzPlatformRichText\eZ\FieldType\RichText\Value from EzPlatformRichTextBundle.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
157
158
        return $articleStruct;
159
    }
160
161
    private function createContent(ContentCreateStruct $contentCreateStruct): Content
162
    {
163
        $repository = $this->getRepository();
164
165
        return $repository->getContentService()->createContent(
166
            $contentCreateStruct,
167
            [$repository->getLocationService()->newLocationCreateStruct(2)]
168
        );
169
    }
170
171
    private function assertResults(array $searchHits): void
172
    {
173
        $resultIds = [];
174
175
        /** @var SearchHit $contentItem */
176
        foreach ($searchHits as $contentItem) {
177
            $resultIds[] = $contentItem->valueObject->contentInfo->id;
178
        }
179
180
        $this->assertTrue(
181
            count(array_intersect($resultIds, self::$createdIds)) === 2
182
        );
183
    }
184
}
185