1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace SprykerFeatureTest\Client\SelfServicePortal\Plugin\Elasticsearch\Query; |
11
|
|
|
|
12
|
|
|
use Codeception\Test\Unit; |
13
|
|
|
use Elastica\Query; |
14
|
|
|
use Elastica\Query\BoolQuery; |
15
|
|
|
use Generated\Shared\Transfer\SearchContextTransfer; |
16
|
|
|
use SprykerFeature\Client\SelfServicePortal\Plugin\Elasticsearch\Query\SspAssetSearchQueryPlugin; |
17
|
|
|
use SprykerFeature\Client\SelfServicePortal\SelfServicePortalConfig; |
18
|
|
|
use SprykerFeatureTest\Client\SelfServicePortal\SelfServicePortalClientTester; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @group SprykerFeatureTest |
22
|
|
|
* @group Client |
23
|
|
|
* @group SelfServicePortal |
24
|
|
|
* @group Plugin |
25
|
|
|
* @group Elasticsearch |
26
|
|
|
* @group Query |
27
|
|
|
* @group SspAssetSearchQueryPluginTest |
28
|
|
|
*/ |
29
|
|
|
class SspAssetSearchQueryPluginTest extends Unit |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected const SOURCE_IDENTIFIER = 'ssp_asset'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected const TEST_SEARCH_STRING = 'test search'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected const EMPTY_SEARCH_STRING = ''; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var \SprykerFeatureTest\Client\SelfServicePortal\SelfServicePortalClientTester |
48
|
|
|
*/ |
49
|
|
|
protected SelfServicePortalClientTester $tester; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var \SprykerFeature\Client\SelfServicePortal\Plugin\Elasticsearch\Query\SspAssetSearchQueryPlugin |
53
|
|
|
*/ |
54
|
|
|
protected SspAssetSearchQueryPlugin $plugin; |
55
|
|
|
|
56
|
|
|
protected function setUp(): void |
57
|
|
|
{ |
58
|
|
|
parent::setUp(); |
59
|
|
|
|
60
|
|
|
$this->setupMockDependencies(); |
61
|
|
|
$this->plugin = new SspAssetSearchQueryPlugin(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testGetSearchQueryReturnsValidQueryObject(): void |
65
|
|
|
{ |
66
|
|
|
// Act |
67
|
|
|
$result = $this->plugin->getSearchQuery(); |
68
|
|
|
|
69
|
|
|
// Assert |
70
|
|
|
$this->assertInstanceOf(Query::class, $result); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testGetSearchContextReturnsDefaultContextWhenNotSet(): void |
74
|
|
|
{ |
75
|
|
|
// Act |
76
|
|
|
$result = $this->plugin->getSearchContext(); |
77
|
|
|
|
78
|
|
|
// Assert |
79
|
|
|
$this->assertInstanceOf(SearchContextTransfer::class, $result); |
80
|
|
|
$this->assertSame(static::SOURCE_IDENTIFIER, $result->getSourceIdentifier()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testSetSearchContextSetsTheContextCorrectly(): void |
84
|
|
|
{ |
85
|
|
|
// Arrange |
86
|
|
|
$expectedContext = (new SearchContextTransfer()) |
87
|
|
|
->setSourceIdentifier('custom_identifier'); |
88
|
|
|
|
89
|
|
|
// Act |
90
|
|
|
$this->plugin->setSearchContext($expectedContext); |
91
|
|
|
$result = $this->plugin->getSearchContext(); |
92
|
|
|
|
93
|
|
|
// Assert |
94
|
|
|
$this->assertSame($expectedContext, $result); |
95
|
|
|
$this->assertSame('custom_identifier', $result->getSourceIdentifier()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testGetSearchStringReturnsNullByDefault(): void |
99
|
|
|
{ |
100
|
|
|
// Act |
101
|
|
|
$result = $this->plugin->getSearchString(); |
102
|
|
|
|
103
|
|
|
// Assert |
104
|
|
|
$this->assertNull($result); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testSetSearchStringWithValidString(): void |
108
|
|
|
{ |
109
|
|
|
// Act |
110
|
|
|
$this->plugin->setSearchString(static::TEST_SEARCH_STRING); |
111
|
|
|
$result = $this->plugin->getSearchString(); |
112
|
|
|
|
113
|
|
|
// Assert |
114
|
|
|
$this->assertSame(static::TEST_SEARCH_STRING, $result); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testSetSearchStringWithEmptyString(): void |
118
|
|
|
{ |
119
|
|
|
// Act |
120
|
|
|
$this->plugin->setSearchString(static::EMPTY_SEARCH_STRING); |
121
|
|
|
$result = $this->plugin->getSearchString(); |
122
|
|
|
|
123
|
|
|
// Assert |
124
|
|
|
$this->assertSame(static::EMPTY_SEARCH_STRING, $result); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testSetSearchStringWithNullValue(): void |
128
|
|
|
{ |
129
|
|
|
// Arrange |
130
|
|
|
$this->plugin->setSearchString(static::TEST_SEARCH_STRING); |
131
|
|
|
|
132
|
|
|
// Act |
133
|
|
|
$this->plugin->setSearchString(null); |
134
|
|
|
$result = $this->plugin->getSearchString(); |
|
|
|
|
135
|
|
|
|
136
|
|
|
// Assert |
137
|
|
|
$this->assertNull($result); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testSetSearchStringRecreatesQuery(): void |
141
|
|
|
{ |
142
|
|
|
// Arrange |
143
|
|
|
$originalQuery = $this->plugin->getSearchQuery(); |
144
|
|
|
|
145
|
|
|
// Act |
146
|
|
|
$this->plugin->setSearchString(static::TEST_SEARCH_STRING); |
147
|
|
|
$newQuery = $this->plugin->getSearchQuery(); |
148
|
|
|
|
149
|
|
|
// Assert |
150
|
|
|
$this->assertInstanceOf(Query::class, $newQuery); |
151
|
|
|
$this->assertNotSame($originalQuery, $newQuery, 'Query should be recreated when search string is set'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testConstructorInitializesQueryCorrectly(): void |
155
|
|
|
{ |
156
|
|
|
// Act |
157
|
|
|
$plugin = new SspAssetSearchQueryPlugin(); |
158
|
|
|
$query = $plugin->getSearchQuery(); |
159
|
|
|
|
160
|
|
|
// Assert |
161
|
|
|
$this->assertInstanceOf(Query::class, $query); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testGetSearchContextReturnsSameInstanceOnMultipleCalls(): void |
165
|
|
|
{ |
166
|
|
|
// Act |
167
|
|
|
$firstCall = $this->plugin->getSearchContext(); |
168
|
|
|
$secondCall = $this->plugin->getSearchContext(); |
169
|
|
|
|
170
|
|
|
// Assert |
171
|
|
|
$this->assertSame($firstCall, $secondCall); |
172
|
|
|
$this->assertSame(static::SOURCE_IDENTIFIER, $firstCall->getSourceIdentifier()); |
173
|
|
|
$this->assertSame(static::SOURCE_IDENTIFIER, $secondCall->getSourceIdentifier()); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function testMultipleCallsToGetSearchQueryReturnSameInstanceWhenSearchStringUnchanged(): void |
177
|
|
|
{ |
178
|
|
|
// Act |
179
|
|
|
$firstQuery = $this->plugin->getSearchQuery(); |
180
|
|
|
$secondQuery = $this->plugin->getSearchQuery(); |
181
|
|
|
|
182
|
|
|
// Assert |
183
|
|
|
$this->assertSame($firstQuery, $secondQuery, 'Multiple calls without search string change should return same instance'); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function testSetSearchStringWithSameValueDoesNotRecreateQuery(): void |
187
|
|
|
{ |
188
|
|
|
// Arrange |
189
|
|
|
$this->plugin->setSearchString(static::TEST_SEARCH_STRING); |
190
|
|
|
$firstQuery = $this->plugin->getSearchQuery(); |
191
|
|
|
|
192
|
|
|
// Act |
193
|
|
|
$this->plugin->setSearchString(static::TEST_SEARCH_STRING); |
194
|
|
|
$secondQuery = $this->plugin->getSearchQuery(); |
195
|
|
|
|
196
|
|
|
// Assert |
197
|
|
|
$this->assertNotSame($firstQuery, $secondQuery, 'Query is recreated even when search string is the same'); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testGetSearchContextCreatesNewInstanceOnlyOnce(): void |
201
|
|
|
{ |
202
|
|
|
// Arrange |
203
|
|
|
$plugin = new SspAssetSearchQueryPlugin(); |
204
|
|
|
|
205
|
|
|
// Act |
206
|
|
|
$firstContext = $plugin->getSearchContext(); |
207
|
|
|
$secondContext = $plugin->getSearchContext(); |
208
|
|
|
|
209
|
|
|
// Assert |
210
|
|
|
$this->assertInstanceOf(SearchContextTransfer::class, $firstContext); |
211
|
|
|
$this->assertInstanceOf(SearchContextTransfer::class, $secondContext); |
212
|
|
|
$this->assertSame($firstContext, $secondContext); |
213
|
|
|
$this->assertSame(static::SOURCE_IDENTIFIER, $firstContext->getSourceIdentifier()); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function testSearchContextCanBeOverridden(): void |
217
|
|
|
{ |
218
|
|
|
// Arrange |
219
|
|
|
$originalContext = $this->plugin->getSearchContext(); |
220
|
|
|
$customContext = (new SearchContextTransfer())->setSourceIdentifier('custom'); |
221
|
|
|
|
222
|
|
|
// Act |
223
|
|
|
$this->plugin->setSearchContext($customContext); |
224
|
|
|
$retrievedContext = $this->plugin->getSearchContext(); |
225
|
|
|
|
226
|
|
|
// Assert |
227
|
|
|
$this->assertNotSame($originalContext, $retrievedContext); |
228
|
|
|
$this->assertSame($customContext, $retrievedContext); |
229
|
|
|
$this->assertSame('custom', $retrievedContext->getSourceIdentifier()); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function testSetSearchStringWithMultipleChangesUpdatesCorrectly(): void |
233
|
|
|
{ |
234
|
|
|
// Arrange |
235
|
|
|
$searches = ['first', 'second', 'third', null, '']; |
236
|
|
|
|
237
|
|
|
foreach ($searches as $searchString) { |
238
|
|
|
// Act |
239
|
|
|
$this->plugin->setSearchString($searchString); |
240
|
|
|
|
241
|
|
|
// Assert |
242
|
|
|
$this->assertSame($searchString, $this->plugin->getSearchString()); |
243
|
|
|
$this->assertInstanceOf(Query::class, $this->plugin->getSearchQuery()); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function testGetSearchQueryCreatesValidElasticsearchQuery(): void |
248
|
|
|
{ |
249
|
|
|
// Act |
250
|
|
|
$query = $this->plugin->getSearchQuery(); |
251
|
|
|
$queryArray = $query->toArray(); |
252
|
|
|
|
253
|
|
|
// Assert |
254
|
|
|
$this->assertInstanceOf(Query::class, $query); |
255
|
|
|
$this->assertInstanceOf(BoolQuery::class, $query->getQuery()); |
256
|
|
|
$this->assertArrayHasKey('suggest', $queryArray); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function testGetSearchQueryWithSearchStringCreatesFullTextQuery(): void |
260
|
|
|
{ |
261
|
|
|
// Arrange |
262
|
|
|
$this->plugin->setSearchString(static::TEST_SEARCH_STRING); |
263
|
|
|
|
264
|
|
|
// Act |
265
|
|
|
$query = $this->plugin->getSearchQuery(); |
266
|
|
|
$queryArray = $query->toArray(); |
267
|
|
|
|
268
|
|
|
// Assert |
269
|
|
|
$this->assertInstanceOf(Query::class, $query); |
270
|
|
|
$this->assertArrayHasKey('query', $queryArray); |
271
|
|
|
$this->assertArrayHasKey('bool', $queryArray['query']); |
272
|
|
|
$this->assertArrayHasKey('must', $queryArray['query']['bool']); |
273
|
|
|
|
274
|
|
|
$mustClauses = $queryArray['query']['bool']['must']; |
275
|
|
|
$this->assertGreaterThanOrEqual(2, count($mustClauses)); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function testGetSearchQueryWithoutSearchStringCreatesBasicQuery(): void |
279
|
|
|
{ |
280
|
|
|
// Act |
281
|
|
|
$query = $this->plugin->getSearchQuery(); |
282
|
|
|
$queryArray = $query->toArray(); |
283
|
|
|
|
284
|
|
|
// Assert |
285
|
|
|
$this->assertInstanceOf(Query::class, $query); |
286
|
|
|
$this->assertArrayHasKey('query', $queryArray); |
287
|
|
|
$this->assertArrayHasKey('bool', $queryArray['query']); |
288
|
|
|
$this->assertArrayHasKey('must', $queryArray['query']['bool']); |
289
|
|
|
|
290
|
|
|
$mustClauses = $queryArray['query']['bool']['must']; |
291
|
|
|
$this->assertCount(1, $mustClauses); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
public function testGetSearchQueryWithEmptyStringCreatesBasicQuery(): void |
295
|
|
|
{ |
296
|
|
|
// Arrange |
297
|
|
|
$this->plugin->setSearchString(static::EMPTY_SEARCH_STRING); |
298
|
|
|
|
299
|
|
|
// Act |
300
|
|
|
$query = $this->plugin->getSearchQuery(); |
301
|
|
|
$queryArray = $query->toArray(); |
302
|
|
|
|
303
|
|
|
// Assert |
304
|
|
|
$this->assertInstanceOf(Query::class, $query); |
305
|
|
|
$this->assertArrayHasKey('query', $queryArray); |
306
|
|
|
$this->assertArrayHasKey('bool', $queryArray['query']); |
307
|
|
|
$this->assertArrayHasKey('must', $queryArray['query']['bool']); |
308
|
|
|
|
309
|
|
|
$mustClauses = $queryArray['query']['bool']['must']; |
310
|
|
|
$this->assertCount(1, $mustClauses); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
public function testGetSearchQueryIncludesTypeFilter(): void |
314
|
|
|
{ |
315
|
|
|
// Act |
316
|
|
|
$query = $this->plugin->getSearchQuery(); |
317
|
|
|
$queryArray = $query->toArray(); |
318
|
|
|
|
319
|
|
|
// Assert |
320
|
|
|
$mustClauses = $queryArray['query']['bool']['must']; |
321
|
|
|
$typeClause = null; |
322
|
|
|
|
323
|
|
|
foreach ($mustClauses as $clause) { |
324
|
|
|
if (isset($clause['term'])) { |
325
|
|
|
$typeClause = $clause; |
326
|
|
|
|
327
|
|
|
break; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
$this->assertNotNull($typeClause, 'Type filter should be present in query'); |
332
|
|
|
$this->assertArrayHasKey('term', $typeClause); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function testGetSearchQueryWithSearchStringIncludesWildcardQueries(): void |
336
|
|
|
{ |
337
|
|
|
// Arrange |
338
|
|
|
$this->plugin->setSearchString(static::TEST_SEARCH_STRING); |
339
|
|
|
|
340
|
|
|
// Act |
341
|
|
|
$query = $this->plugin->getSearchQuery(); |
342
|
|
|
$queryArray = $query->toArray(); |
343
|
|
|
|
344
|
|
|
// Assert |
345
|
|
|
$mustClauses = $queryArray['query']['bool']['must']; |
346
|
|
|
$fullTextClause = null; |
347
|
|
|
|
348
|
|
|
foreach ($mustClauses as $clause) { |
349
|
|
|
if (isset($clause['bool']['should'])) { |
350
|
|
|
$fullTextClause = $clause; |
351
|
|
|
|
352
|
|
|
break; |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
$this->assertNotNull($fullTextClause, 'Full text search clause should be present'); |
357
|
|
|
$this->assertArrayHasKey('bool', $fullTextClause); |
358
|
|
|
$this->assertArrayHasKey('should', $fullTextClause['bool']); |
359
|
|
|
|
360
|
|
|
$shouldClauses = $fullTextClause['bool']['should']; |
361
|
|
|
$this->assertGreaterThanOrEqual(2, count($shouldClauses), 'Should have wildcard and multi-match queries'); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
public function testGetSearchQueryIncludesSuggest(): void |
365
|
|
|
{ |
366
|
|
|
// Arrange |
367
|
|
|
$this->plugin->setSearchString(static::TEST_SEARCH_STRING); |
368
|
|
|
|
369
|
|
|
// Act |
370
|
|
|
$query = $this->plugin->getSearchQuery(); |
371
|
|
|
$queryArray = $query->toArray(); |
372
|
|
|
|
373
|
|
|
// Assert |
374
|
|
|
$this->assertArrayHasKey('suggest', $queryArray); |
375
|
|
|
$this->assertArrayHasKey('text', $queryArray['suggest']); |
376
|
|
|
$this->assertSame(static::TEST_SEARCH_STRING, $queryArray['suggest']['text']); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
public function testGetSearchQueryWithNullSearchStringHasEmptySuggest(): void |
380
|
|
|
{ |
381
|
|
|
// Act |
382
|
|
|
$query = $this->plugin->getSearchQuery(); |
383
|
|
|
$queryArray = $query->toArray(); |
384
|
|
|
|
385
|
|
|
// Assert |
386
|
|
|
$this->assertArrayHasKey('suggest', $queryArray); |
387
|
|
|
$this->assertArrayHasKey('text', $queryArray['suggest']); |
388
|
|
|
$this->assertSame('', $queryArray['suggest']['text']); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
public function testGetSearchQueryIncludesSourceFieldsConfiguration(): void |
392
|
|
|
{ |
393
|
|
|
// Act |
394
|
|
|
$query = $this->plugin->getSearchQuery(); |
395
|
|
|
$queryArray = $query->toArray(); |
396
|
|
|
|
397
|
|
|
// Assert |
398
|
|
|
$this->assertArrayHasKey('_source', $queryArray); |
399
|
|
|
$this->assertNotEmpty($queryArray['_source'], 'Source fields should be configured'); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
protected function setupMockDependencies(): void |
403
|
|
|
{ |
404
|
|
|
$configMock = $this->createMock(SelfServicePortalConfig::class); |
405
|
|
|
$configMock->method('getElasticsearchFullTextBoostedBoostingValue')->willReturn(3); |
406
|
|
|
|
407
|
|
|
$this->tester->mockFactoryMethod('getConfig', $configMock, 'SelfServicePortal'); |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.