5
5
*/
6
6
namespace Magento \Search \Test \Unit \Model ;
7
7
8
- use Magento \Search \Helper \Data ;
9
8
use Magento \Framework \App \Helper \Context ;
10
9
use Magento \Framework \App \RequestInterface ;
11
10
use Magento \Framework \ObjectManagerInterface ;
12
- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
13
- use Magento \Search \Model \QueryFactory ;
14
11
use Magento \Framework \Stdlib \StringUtils ;
12
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
13
+ use Magento \Search \Helper \Data ;
15
14
use Magento \Search \Model \Query ;
15
+ use Magento \Search \Model \QueryFactory ;
16
16
17
17
/**
18
18
* Class QueryFactoryTest tests Magento\Search\Model\QueryFactory
@@ -67,7 +67,7 @@ protected function setUp()
67
67
->disableOriginalConstructor ()
68
68
->getMock ();
69
69
$ this ->query = $ this ->getMockBuilder (Query::class)
70
- ->setMethods (['setIsQueryTextExceeded ' , 'setIsQueryTextShort ' , 'loadByQueryText ' , 'getId ' , ' setQueryText ' ])
70
+ ->setMethods (['setIsQueryTextExceeded ' , 'setIsQueryTextShort ' , 'loadByQueryText ' , 'getId ' ])
71
71
->disableOriginalConstructor ()
72
72
->getMock ();
73
73
@@ -124,7 +124,6 @@ public function testGetNewQuery()
124
124
$ isQueryTextExceeded = false ;
125
125
$ isQueryTextShort = false ;
126
126
127
- $ this ->mockSetQueryTextNeverExecute ($ cleanedRawText );
128
127
$ this ->mockString ($ cleanedRawText );
129
128
$ this ->mockQueryLengths ($ maxQueryLength , $ minQueryLength );
130
129
$ this ->mockGetRawQueryText ($ rawQueryText );
@@ -135,6 +134,7 @@ public function testGetNewQuery()
135
134
$ result = $ this ->model ->get ();
136
135
137
136
$ this ->assertSame ($ this ->query , $ result );
137
+ $ this ->assertSearchQuery ($ cleanedRawText );
138
138
}
139
139
140
140
/**
@@ -150,7 +150,6 @@ public function testGetQueryTwice()
150
150
$ isQueryTextExceeded = false ;
151
151
$ isQueryTextShort = false ;
152
152
153
- $ this ->mockSetQueryTextNeverExecute ($ cleanedRawText );
154
153
$ this ->mockString ($ cleanedRawText );
155
154
$ this ->mockQueryLengths ($ maxQueryLength , $ minQueryLength );
156
155
$ this ->mockGetRawQueryText ($ rawQueryText );
@@ -163,6 +162,7 @@ public function testGetQueryTwice()
163
162
164
163
$ result = $ this ->model ->get ();
165
164
$ this ->assertSame ($ this ->query , $ result , 'After second execution queries are not same ' );
165
+ $ this ->assertSearchQuery ($ cleanedRawText );
166
166
}
167
167
168
168
/**
@@ -184,7 +184,6 @@ public function testGetTooLongQuery()
184
184
->withConsecutive ([$ cleanedRawText , 0 , $ maxQueryLength ])
185
185
->willReturn ($ subRawText );
186
186
187
- $ this ->mockSetQueryTextNeverExecute ($ cleanedRawText );
188
187
$ this ->mockString ($ cleanedRawText );
189
188
$ this ->mockQueryLengths ($ maxQueryLength , $ minQueryLength );
190
189
$ this ->mockGetRawQueryText ($ rawQueryText );
@@ -194,6 +193,7 @@ public function testGetTooLongQuery()
194
193
195
194
$ result = $ this ->model ->get ();
196
195
$ this ->assertSame ($ this ->query , $ result );
196
+ $ this ->assertSearchQuery ($ subRawText );
197
197
}
198
198
199
199
/**
@@ -209,7 +209,6 @@ public function testGetTooShortQuery()
209
209
$ isQueryTextExceeded = false ;
210
210
$ isQueryTextShort = true ;
211
211
212
- $ this ->mockSetQueryTextNeverExecute ($ cleanedRawText );
213
212
$ this ->mockString ($ cleanedRawText );
214
213
$ this ->mockQueryLengths ($ maxQueryLength , $ minQueryLength );
215
214
$ this ->mockGetRawQueryText ($ rawQueryText );
@@ -219,6 +218,7 @@ public function testGetTooShortQuery()
219
218
220
219
$ result = $ this ->model ->get ();
221
220
$ this ->assertSame ($ this ->query , $ result );
221
+ $ this ->assertSearchQuery ($ cleanedRawText );
222
222
}
223
223
224
224
/**
@@ -234,7 +234,6 @@ public function testGetQueryWithoutId()
234
234
$ isQueryTextExceeded = false ;
235
235
$ isQueryTextShort = false ;
236
236
237
- $ this ->mockSetQueryTextOnceExecute ($ cleanedRawText );
238
237
$ this ->mockString ($ cleanedRawText );
239
238
$ this ->mockQueryLengths ($ maxQueryLength , $ minQueryLength );
240
239
$ this ->mockGetRawQueryText ($ rawQueryText );
@@ -244,6 +243,35 @@ public function testGetQueryWithoutId()
244
243
245
244
$ result = $ this ->model ->get ();
246
245
$ this ->assertSame ($ this ->query , $ result );
246
+ $ this ->assertSearchQuery ($ cleanedRawText );
247
+ }
248
+
249
+ /**
250
+ * Test for inaccurate match of search query in query_text table
251
+ *
252
+ * Because of inaccurate string comparison of utf8_general_ci,
253
+ * the search_query result text may be different from the original text (e.g organos, Organos, Órganos)
254
+ */
255
+ public function testInaccurateQueryTextMatch ()
256
+ {
257
+ $ queryId = 1 ;
258
+ $ maxQueryLength = 100 ;
259
+ $ minQueryLength = 3 ;
260
+ $ rawQueryText = 'Órganos ' ;
261
+ $ cleanedRawText = 'Órganos ' ;
262
+ $ isQueryTextExceeded = false ;
263
+ $ isQueryTextShort = false ;
264
+
265
+ $ this ->mockString ($ cleanedRawText );
266
+ $ this ->mockQueryLengths ($ maxQueryLength , $ minQueryLength );
267
+ $ this ->mockGetRawQueryText ($ rawQueryText );
268
+ $ this ->mockSimpleQuery ($ cleanedRawText , $ queryId , $ isQueryTextExceeded , $ isQueryTextShort , 'Organos ' );
269
+
270
+ $ this ->mockCreateQuery ();
271
+
272
+ $ result = $ this ->model ->get ();
273
+ $ this ->assertSame ($ this ->query , $ result );
274
+ $ this ->assertSearchQuery ($ cleanedRawText );
247
275
}
248
276
249
277
/**
@@ -305,15 +333,25 @@ private function mockCreateQuery()
305
333
* @param int $queryId
306
334
* @param bool $isQueryTextExceeded
307
335
* @param bool $isQueryTextShort
336
+ * @param string $matchedQueryText
308
337
* @return void
309
338
*/
310
- private function mockSimpleQuery ($ cleanedRawText , $ queryId , $ isQueryTextExceeded , $ isQueryTextShort )
311
- {
339
+ private function mockSimpleQuery (
340
+ string $ cleanedRawText ,
341
+ ?int $ queryId ,
342
+ bool $ isQueryTextExceeded ,
343
+ bool $ isQueryTextShort ,
344
+ string $ matchedQueryText = null
345
+ ) {
346
+ if (null === $ matchedQueryText ) {
347
+ $ matchedQueryText = $ cleanedRawText ;
348
+ }
312
349
$ this ->query ->expects ($ this ->once ())
313
350
->method ('loadByQueryText ' )
314
351
->withConsecutive ([$ cleanedRawText ])
315
352
->willReturnSelf ();
316
- $ this ->query ->expects ($ this ->once ())
353
+ $ this ->query ->setData (['query_text ' => $ matchedQueryText ]);
354
+ $ this ->query ->expects ($ this ->any ())
317
355
->method ('getId ' )
318
356
->willReturn ($ queryId );
319
357
$ this ->query ->expects ($ this ->once ())
@@ -328,23 +366,8 @@ private function mockSimpleQuery($cleanedRawText, $queryId, $isQueryTextExceeded
328
366
* @param string $cleanedRawText
329
367
* @return void
330
368
*/
331
- private function mockSetQueryTextNeverExecute ($ cleanedRawText )
369
+ private function assertSearchQuery ($ cleanedRawText )
332
370
{
333
- $ this ->query ->expects ($ this ->never ())
334
- ->method ('setQueryText ' )
335
- ->withConsecutive ([$ cleanedRawText ])
336
- ->willReturnSelf ();
337
- }
338
-
339
- /**
340
- * @param string $cleanedRawText
341
- * @return void
342
- */
343
- private function mockSetQueryTextOnceExecute ($ cleanedRawText )
344
- {
345
- $ this ->query ->expects ($ this ->once ())
346
- ->method ('setQueryText ' )
347
- ->withConsecutive ([$ cleanedRawText ])
348
- ->willReturnSelf ();
371
+ $ this ->assertEquals ($ cleanedRawText , $ this ->query ->getQueryText ());
349
372
}
350
373
}
0 commit comments