55 */
66namespace Magento \Search \Test \Unit \Model ;
77
8- use Magento \Search \Helper \Data ;
98use Magento \Framework \App \Helper \Context ;
109use Magento \Framework \App \RequestInterface ;
1110use Magento \Framework \ObjectManagerInterface ;
12- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
13- use Magento \Search \Model \QueryFactory ;
1411use Magento \Framework \Stdlib \StringUtils ;
12+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
13+ use Magento \Search \Helper \Data ;
1514use Magento \Search \Model \Query ;
15+ use Magento \Search \Model \QueryFactory ;
1616
1717/**
1818 * Class QueryFactoryTest tests Magento\Search\Model\QueryFactory
@@ -67,7 +67,7 @@ protected function setUp()
6767 ->disableOriginalConstructor ()
6868 ->getMock ();
6969 $ this ->query = $ this ->getMockBuilder (Query::class)
70- ->setMethods (['setIsQueryTextExceeded ' , 'setIsQueryTextShort ' , 'loadByQueryText ' , 'getId ' , ' setQueryText ' ])
70+ ->setMethods (['setIsQueryTextExceeded ' , 'setIsQueryTextShort ' , 'loadByQueryText ' , 'getId ' ])
7171 ->disableOriginalConstructor ()
7272 ->getMock ();
7373
@@ -124,7 +124,6 @@ public function testGetNewQuery()
124124 $ isQueryTextExceeded = false ;
125125 $ isQueryTextShort = false ;
126126
127- $ this ->mockSetQueryTextNeverExecute ($ cleanedRawText );
128127 $ this ->mockString ($ cleanedRawText );
129128 $ this ->mockQueryLengths ($ maxQueryLength , $ minQueryLength );
130129 $ this ->mockGetRawQueryText ($ rawQueryText );
@@ -135,6 +134,7 @@ public function testGetNewQuery()
135134 $ result = $ this ->model ->get ();
136135
137136 $ this ->assertSame ($ this ->query , $ result );
137+ $ this ->assertSearchQuery ($ cleanedRawText );
138138 }
139139
140140 /**
@@ -150,7 +150,6 @@ public function testGetQueryTwice()
150150 $ isQueryTextExceeded = false ;
151151 $ isQueryTextShort = false ;
152152
153- $ this ->mockSetQueryTextNeverExecute ($ cleanedRawText );
154153 $ this ->mockString ($ cleanedRawText );
155154 $ this ->mockQueryLengths ($ maxQueryLength , $ minQueryLength );
156155 $ this ->mockGetRawQueryText ($ rawQueryText );
@@ -163,6 +162,7 @@ public function testGetQueryTwice()
163162
164163 $ result = $ this ->model ->get ();
165164 $ this ->assertSame ($ this ->query , $ result , 'After second execution queries are not same ' );
165+ $ this ->assertSearchQuery ($ cleanedRawText );
166166 }
167167
168168 /**
@@ -184,7 +184,6 @@ public function testGetTooLongQuery()
184184 ->withConsecutive ([$ cleanedRawText , 0 , $ maxQueryLength ])
185185 ->willReturn ($ subRawText );
186186
187- $ this ->mockSetQueryTextNeverExecute ($ cleanedRawText );
188187 $ this ->mockString ($ cleanedRawText );
189188 $ this ->mockQueryLengths ($ maxQueryLength , $ minQueryLength );
190189 $ this ->mockGetRawQueryText ($ rawQueryText );
@@ -194,6 +193,7 @@ public function testGetTooLongQuery()
194193
195194 $ result = $ this ->model ->get ();
196195 $ this ->assertSame ($ this ->query , $ result );
196+ $ this ->assertSearchQuery ($ subRawText );
197197 }
198198
199199 /**
@@ -209,7 +209,6 @@ public function testGetTooShortQuery()
209209 $ isQueryTextExceeded = false ;
210210 $ isQueryTextShort = true ;
211211
212- $ this ->mockSetQueryTextNeverExecute ($ cleanedRawText );
213212 $ this ->mockString ($ cleanedRawText );
214213 $ this ->mockQueryLengths ($ maxQueryLength , $ minQueryLength );
215214 $ this ->mockGetRawQueryText ($ rawQueryText );
@@ -219,6 +218,7 @@ public function testGetTooShortQuery()
219218
220219 $ result = $ this ->model ->get ();
221220 $ this ->assertSame ($ this ->query , $ result );
221+ $ this ->assertSearchQuery ($ cleanedRawText );
222222 }
223223
224224 /**
@@ -234,7 +234,6 @@ public function testGetQueryWithoutId()
234234 $ isQueryTextExceeded = false ;
235235 $ isQueryTextShort = false ;
236236
237- $ this ->mockSetQueryTextOnceExecute ($ cleanedRawText );
238237 $ this ->mockString ($ cleanedRawText );
239238 $ this ->mockQueryLengths ($ maxQueryLength , $ minQueryLength );
240239 $ this ->mockGetRawQueryText ($ rawQueryText );
@@ -244,6 +243,35 @@ public function testGetQueryWithoutId()
244243
245244 $ result = $ this ->model ->get ();
246245 $ 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 );
247275 }
248276
249277 /**
@@ -305,15 +333,25 @@ private function mockCreateQuery()
305333 * @param int $queryId
306334 * @param bool $isQueryTextExceeded
307335 * @param bool $isQueryTextShort
336+ * @param string $matchedQueryText
308337 * @return void
309338 */
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+ }
312349 $ this ->query ->expects ($ this ->once ())
313350 ->method ('loadByQueryText ' )
314351 ->withConsecutive ([$ cleanedRawText ])
315352 ->willReturnSelf ();
316- $ this ->query ->expects ($ this ->once ())
353+ $ this ->query ->setData (['query_text ' => $ matchedQueryText ]);
354+ $ this ->query ->expects ($ this ->any ())
317355 ->method ('getId ' )
318356 ->willReturn ($ queryId );
319357 $ this ->query ->expects ($ this ->once ())
@@ -328,23 +366,8 @@ private function mockSimpleQuery($cleanedRawText, $queryId, $isQueryTextExceeded
328366 * @param string $cleanedRawText
329367 * @return void
330368 */
331- private function mockSetQueryTextNeverExecute ($ cleanedRawText )
369+ private function assertSearchQuery ($ cleanedRawText )
332370 {
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 ());
349372 }
350373}
0 commit comments