-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSearchServiceContentNameTest.php
More file actions
308 lines (270 loc) · 8.83 KB
/
SearchServiceContentNameTest.php
File metadata and controls
308 lines (270 loc) · 8.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\Tests\Integration\Core\Repository;
use Ibexa\Contracts\Core\Repository\Exceptions\BadStateException;
use Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException;
use Ibexa\Contracts\Core\Repository\Exceptions\ContentValidationException;
use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
use Ibexa\Contracts\Core\Repository\Exceptions\InvalidCriterionArgumentException;
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException;
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Search\SearchHit;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Core\FieldType\TextLine\Value;
use Ibexa\Tests\Integration\Core\RepositorySearchTestCase;
final class SearchServiceContentNameTest extends RepositorySearchTestCase
{
private const TOTAL_COUNT = 20;
private const LANGUAGE_CODE_ENG = 'eng-GB';
private const LANGUAGE_CODE_GER = 'ger-DE';
private const CAR_ENG = 'Car';
private const SPORTS_CAR_ENG = 'Sports car';
private const TRUCK_ENG = 'TRUCK';
private const CAR_GER = 'auto';
private const SPORTS_CAR_GER = 'Sportwagen';
private const TRUCK_GER = 'LASTWAGEN';
private const CONTENT_ITEMS_MAP = [
[
'mainLanguageCode' => self::LANGUAGE_CODE_ENG,
'name' => self::CAR_ENG,
'translations' => [
self::LANGUAGE_CODE_GER => self::CAR_GER,
],
],
[
'mainLanguageCode' => self::LANGUAGE_CODE_ENG,
'name' => self::SPORTS_CAR_ENG,
'translations' => [
self::LANGUAGE_CODE_GER => self::SPORTS_CAR_GER,
],
],
[
'mainLanguageCode' => self::LANGUAGE_CODE_ENG,
'name' => self::TRUCK_ENG,
'translations' => [
self::LANGUAGE_CODE_GER => self::TRUCK_GER,
],
],
];
protected function setUp(): void
{
parent::setUp();
$this->createTestContentItems();
$this->refreshSearch();
}
/**
* @throws InvalidArgumentException
*/
public function testCriterionFindAllContentItems(): void
{
$query = $this->createQuery(
$this->createContentNameCriterion('*')
);
self::assertSame(
self::TOTAL_COUNT,
self::getSearchService()->findContent($query)->totalCount
);
}
/**
* @dataProvider provideDataForTestCriterion
*
* @param array<string> $expectedContentItemTitles
*
* @throws InvalidArgumentException
* @throws InvalidCriterionArgumentException
*/
public function testCriterion(
Criterion $criterion,
?string $languageCode,
array $expectedContentItemTitles,
int $expectedCount
): void {
$result = self::getSearchService()->findContent(
$this->createQuery($criterion),
$this->getLanguageFilter($languageCode)
);
self::assertEquals(
$expectedContentItemTitles,
array_map(
static function (SearchHit $searchHit) use ($languageCode): ?string {
$content = $searchHit->valueObject;
if ($content instanceof Content) {
return $content->getName($languageCode);
}
return null;
},
$result->searchHits
)
);
self::assertSame(
$expectedCount,
$result->totalCount
);
}
/**
* @return iterable<array{
* Criterion,
* ?string,
* array<string>,
* int,
* }>
*/
public function provideDataForTestCriterion(): iterable
{
yield 'Content items not found' => [
$this->createContentNameCriterion('foo'),
self::LANGUAGE_CODE_ENG,
[],
0,
];
yield 'Return content items in default language (English) that contain "car" in name' => [
$this->createContentNameCriterion('*car*'),
null,
[
self::CAR_ENG,
self::SPORTS_CAR_ENG,
],
2,
];
yield 'Return content item in default language (English) whose name starts with "car"' => [
$this->createContentNameCriterion('Car*'),
null,
[
self::CAR_ENG,
],
1,
];
yield 'Return content item in English that contain "Spo*t*" in name' => [
$this->createContentNameCriterion('Spo*t*'),
self::LANGUAGE_CODE_ENG,
[
self::SPORTS_CAR_ENG,
],
1,
];
yield 'Return content item in English with name "sports car"' => [
$this->createContentNameCriterion('sports car'),
self::LANGUAGE_CODE_ENG,
[
self::SPORTS_CAR_ENG,
],
1,
];
yield 'Return content item in English that contain "**ruc*" in name' => [
$this->createContentNameCriterion('**ruc*'),
self::LANGUAGE_CODE_ENG,
[
self::TRUCK_ENG,
],
1,
];
yield 'Return content item in German that contain "aut*" in name' => [
$this->createContentNameCriterion('aut*'),
self::LANGUAGE_CODE_GER,
[
self::CAR_GER,
],
1,
];
yield 'Return content items in German that contain "*wagen" in name' => [
$this->createContentNameCriterion('*wagen'),
self::LANGUAGE_CODE_GER,
[
self::SPORTS_CAR_GER,
self::TRUCK_GER,
],
2,
];
yield 'Return content item in German with name "lastwagen"' => [
$this->createContentNameCriterion('lastwagen'),
self::LANGUAGE_CODE_GER,
[
self::TRUCK_GER,
],
1,
];
}
private function createTestContentItems(): void
{
foreach (self::CONTENT_ITEMS_MAP as $contentItem) {
$this->createContent(
$contentItem['name'],
$contentItem['mainLanguageCode'],
$contentItem['translations']
);
}
}
/**
* @param array<string> $translations
*
* @return Content
*
* @throws BadStateException
* @throws ContentFieldValidationException
* @throws ContentValidationException
* @throws InvalidArgumentException
* @throws NotFoundException
* @throws UnauthorizedException
*/
private function createContent(
string $title,
string $mainLanguageCode,
array $translations
): Content {
$contentService = self::getContentService();
$createStruct = $contentService->newContentCreateStruct(
$this->loadContentType('article'),
$mainLanguageCode
);
$createStruct->setField('title', new Value($title));
if (!empty($translations)) {
foreach ($translations as $languageCode => $translatedName) {
$createStruct->setField('title', new Value($translatedName), $languageCode);
}
}
$content = $contentService->createContent($createStruct);
$contentService->publishVersion($content->getVersionInfo());
return $content;
}
/**
* @throws NotFoundException
*/
private function loadContentType(string $contentTypeIdentifier): ContentType
{
return self::getContentTypeService()
->loadContentTypeByIdentifier($contentTypeIdentifier);
}
private function createContentNameCriterion(string $value): Criterion
{
return new Criterion\ContentName($value);
}
/**
* @throws InvalidCriterionArgumentException
*/
private function createQuery(Criterion $criterion): Query
{
$query = new Query();
$query->filter = new Criterion\LogicalAnd(
[$criterion]
);
return $query;
}
/**
* @return array{}|array{
* languages: array<string>
* }
*/
public function getLanguageFilter(?string $languageCode): array
{
return null !== $languageCode
? ['languages' => [$languageCode]]
: [];
}
}