Skip to content

Commit d354290

Browse files
authored
Merge pull request #4781 from magento-chaika/Chaika-PR-2019-09-15
Chaika-PR-2019-09-15
2 parents 5ee35e5 + 9fe3763 commit d354290

File tree

5 files changed

+152
-67
lines changed

5 files changed

+152
-67
lines changed

app/code/Magento/Search/Model/QueryFactory.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
*/
66
namespace Magento\Search\Model;
77

8-
use Magento\Search\Helper\Data;
98
use Magento\Framework\App\Config\ScopeConfigInterface;
109
use Magento\Framework\App\Helper\Context;
1110
use Magento\Framework\ObjectManagerInterface;
1211
use Magento\Framework\Stdlib\StringUtils as StdlibString;
12+
use Magento\Search\Helper\Data;
1313

1414
/**
15+
* Search Query Factory
16+
*
1517
* @api
1618
* @since 100.0.2
1719
*/
@@ -72,7 +74,7 @@ public function __construct(
7274
}
7375

7476
/**
75-
* {@inheritdoc}
77+
* @inheritdoc
7678
*/
7779
public function get()
7880
{
@@ -82,9 +84,7 @@ public function get()
8284
$rawQueryText = $this->getRawQueryText();
8385
$preparedQueryText = $this->getPreparedQueryText($rawQueryText, $maxQueryLength);
8486
$query = $this->create()->loadByQueryText($preparedQueryText);
85-
if (!$query->getId()) {
86-
$query->setQueryText($preparedQueryText);
87-
}
87+
$query->setQueryText($preparedQueryText);
8888
$query->setIsQueryTextExceeded($this->isQueryTooLong($rawQueryText, $maxQueryLength));
8989
$query->setIsQueryTextShort($this->isQueryTooShort($rawQueryText, $minQueryLength));
9090
$this->query = $query;
@@ -117,6 +117,8 @@ private function getRawQueryText()
117117
}
118118

119119
/**
120+
* Prepare query text
121+
*
120122
* @param string $queryText
121123
* @param int|string $maxQueryLength
122124
* @return string
@@ -130,6 +132,8 @@ private function getPreparedQueryText($queryText, $maxQueryLength)
130132
}
131133

132134
/**
135+
* Check if the provided text exceeds the provided length
136+
*
133137
* @param string $queryText
134138
* @param int|string $maxQueryLength
135139
* @return bool
@@ -140,6 +144,8 @@ private function isQueryTooLong($queryText, $maxQueryLength)
140144
}
141145

142146
/**
147+
* Check if the provided text is shorter than the provided length
148+
*
143149
* @param string $queryText
144150
* @param int|string $minQueryLength
145151
* @return bool

app/code/Magento/Search/Test/Unit/Model/QueryFactoryTest.php

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
*/
66
namespace Magento\Search\Test\Unit\Model;
77

8-
use Magento\Search\Helper\Data;
98
use Magento\Framework\App\Helper\Context;
109
use Magento\Framework\App\RequestInterface;
1110
use Magento\Framework\ObjectManagerInterface;
12-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13-
use Magento\Search\Model\QueryFactory;
1411
use Magento\Framework\Stdlib\StringUtils;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
use Magento\Search\Helper\Data;
1514
use 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
}

app/code/Magento/Ui/Component/Form/Element/DataType/Date.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,7 @@ public function getComponentName()
111111
public function convertDate($date, $hour = 0, $minute = 0, $second = 0, $setUtcTimeZone = true)
112112
{
113113
try {
114-
$dateObj = $this->localeDate->date(
115-
new \DateTime(
116-
$date,
117-
new \DateTimeZone($this->localeDate->getConfigTimezone())
118-
),
119-
$this->getLocale(),
120-
true
121-
);
114+
$dateObj = $this->localeDate->date($date, $this->getLocale(), true);
122115
$dateObj->setTime($hour, $minute, $second);
123116
//convert store date to default date in UTC timezone without DST
124117
if ($setUtcTimeZone) {

lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,31 +195,37 @@ public function date($date = null, $locale = null, $useTimezone = true, $include
195195
*/
196196
public function scopeDate($scope = null, $date = null, $includeTime = false)
197197
{
198-
$timezone = $this->_scopeConfig->getValue($this->getDefaultTimezonePath(), $this->_scopeType, $scope);
198+
$timezone = new \DateTimeZone(
199+
$this->_scopeConfig->getValue($this->getDefaultTimezonePath(), $this->_scopeType, $scope)
200+
);
199201
switch (true) {
200202
case (empty($date)):
201-
$date = new \DateTime('now', new \DateTimeZone($timezone));
203+
$date = new \DateTime('now', $timezone);
202204
break;
203205
case ($date instanceof \DateTime):
204-
$date = $date->setTimezone(new \DateTimeZone($timezone));
205-
break;
206206
case ($date instanceof \DateTimeImmutable):
207-
$date = new \DateTime($date->format('Y-m-d H:i:s'), $date->getTimezone());
207+
$date = $date->setTimezone($timezone);
208208
break;
209209
case (!is_numeric($date)):
210210
$timeType = $includeTime ? \IntlDateFormatter::SHORT : \IntlDateFormatter::NONE;
211211
$formatter = new \IntlDateFormatter(
212212
$this->_localeResolver->getLocale(),
213213
\IntlDateFormatter::SHORT,
214214
$timeType,
215-
new \DateTimeZone($timezone)
215+
$timezone
216216
);
217-
$date = $formatter->parse($date) ?: (new \DateTime($date))->getTimestamp();
218-
$date = (new \DateTime(null, new \DateTimeZone($timezone)))->setTimestamp($date);
217+
$timestamp = $formatter->parse($date);
218+
$date = $timestamp
219+
? (new \DateTime('@' . $timestamp))->setTimezone($timezone)
220+
: new \DateTime($date, $timezone);
221+
break;
222+
case (is_numeric($date)):
223+
$date = new \DateTime('@' . $date);
224+
$date = $date->setTimezone($timezone);
219225
break;
220226
default:
221-
$date = new \DateTime(is_numeric($date) ? '@' . $date : $date);
222-
$date->setTimezone(new \DateTimeZone($timezone));
227+
$date = new \DateTime($date, $timezone);
228+
break;
223229
}
224230

225231
if (!$includeTime) {

0 commit comments

Comments
 (0)