Skip to content

Commit 9d6d4e2

Browse files
authored
Merge pull request #3937 from magento-mpi/pr_2019_03_20
[mpi] bugfixes
2 parents 677f1b6 + 267a2b9 commit 9d6d4e2

File tree

30 files changed

+830
-243
lines changed

30 files changed

+830
-243
lines changed

app/code/Magento/Backend/Block/Widget/Grid/Massaction/AbstractMassaction.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -282,25 +282,23 @@ public function getGridIdsJson()
282282
if (!$this->getUseSelectAll()) {
283283
return '';
284284
}
285-
/** @var \Magento\Framework\Data\Collection $allIdsCollection */
286-
$allIdsCollection = clone $this->getParentBlock()->getCollection();
287285

288-
if ($this->getMassactionIdField()) {
289-
$massActionIdField = $this->getMassactionIdField();
286+
/** @var \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection $collection */
287+
$collection = clone $this->getParentBlock()->getCollection();
288+
289+
if ($collection instanceof AbstractDb) {
290+
$idsSelect = clone $collection->getSelect();
291+
$idsSelect->reset(\Magento\Framework\DB\Select::ORDER);
292+
$idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_COUNT);
293+
$idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);
294+
$idsSelect->reset(\Magento\Framework\DB\Select::COLUMNS);
295+
$idsSelect->columns($this->getMassactionIdField(), 'main_table');
296+
$idList = $collection->getConnection()->fetchCol($idsSelect);
290297
} else {
291-
$massActionIdField = $this->getParentBlock()->getMassactionIdField();
298+
$idList = $collection->setPageSize(0)->getColumnValues($this->getMassactionIdField());
292299
}
293300

294-
if ($allIdsCollection instanceof AbstractDb) {
295-
$allIdsCollection->getSelect()->limit();
296-
$allIdsCollection->clear();
297-
}
298-
299-
$gridIds = $allIdsCollection->setPageSize(0)->getColumnValues($massActionIdField);
300-
if (!empty($gridIds)) {
301-
return join(",", $gridIds);
302-
}
303-
return '';
301+
return implode(',', $idList);
304302
}
305303

306304
/**

app/code/Magento/Backend/Test/Unit/Block/Widget/Grid/MassactionTest.php

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -269,62 +269,6 @@ public function testGetGridIdsJsonWithoutUseSelectAll()
269269
$this->assertEmpty($this->_block->getGridIdsJson());
270270
}
271271

272-
/**
273-
* @param array $items
274-
* @param string $result
275-
*
276-
* @dataProvider dataProviderGetGridIdsJsonWithUseSelectAll
277-
*/
278-
public function testGetGridIdsJsonWithUseSelectAll(array $items, $result)
279-
{
280-
$this->_block->setUseSelectAll(true);
281-
282-
if ($this->_block->getMassactionIdField()) {
283-
$massActionIdField = $this->_block->getMassactionIdField();
284-
} else {
285-
$massActionIdField = $this->_block->getParentBlock()->getMassactionIdField();
286-
}
287-
288-
$collectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection::class)
289-
->disableOriginalConstructor()
290-
->getMock();
291-
292-
$this->_gridMock->expects($this->once())
293-
->method('getCollection')
294-
->willReturn($collectionMock);
295-
$collectionMock->expects($this->once())
296-
->method('setPageSize')
297-
->with(0)
298-
->willReturnSelf();
299-
$collectionMock->expects($this->once())
300-
->method('getColumnValues')
301-
->with($massActionIdField)
302-
->willReturn($items);
303-
304-
$this->assertEquals($result, $this->_block->getGridIdsJson());
305-
}
306-
307-
/**
308-
* @return array
309-
*/
310-
public function dataProviderGetGridIdsJsonWithUseSelectAll()
311-
{
312-
return [
313-
[
314-
[],
315-
'',
316-
],
317-
[
318-
[1],
319-
'1',
320-
],
321-
[
322-
[1, 2, 3],
323-
'1,2,3',
324-
],
325-
];
326-
}
327-
328272
/**
329273
* @param string $itemId
330274
* @param array|\Magento\Framework\DataObject $item

app/code/Magento/Cms/Model/Wysiwyg/Images/Storage.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ public function getDirsCollection($path)
270270
$collection = $this->getCollection($path)
271271
->setCollectDirs(true)
272272
->setCollectFiles(false)
273-
->setCollectRecursively(false);
273+
->setCollectRecursively(false)
274+
->setOrder('basename', \Magento\Framework\Data\Collection\Filesystem::SORT_ORDER_ASC);
274275

275276
$conditions = $this->getConditionsForExcludeDirs();
276277

app/code/Magento/Cms/Test/Unit/Model/Wysiwyg/Images/StorageTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,10 @@ protected function generalTestGetDirsCollection($path, $collectionArray = [], $e
417417
->method('setCollectRecursively')
418418
->with(false)
419419
->willReturnSelf();
420+
$storageCollectionMock->expects($this->once())
421+
->method('setOrder')
422+
->with('basename', \Magento\Framework\Data\Collection\Filesystem::SORT_ORDER_ASC)
423+
->willReturnSelf();
420424
$storageCollectionMock->expects($this->once())
421425
->method('getIterator')
422426
->willReturn(new \ArrayIterator($collectionArray));

app/code/Magento/Eav/Model/Entity/AbstractEntity.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,14 +1683,16 @@ public function saveAttribute(DataObject $object, $attributeCode)
16831683
$connection->beginTransaction();
16841684

16851685
try {
1686-
$select = $connection->select()->from($table, 'value_id')->where($where);
1687-
$origValueId = $connection->fetchOne($select);
1686+
$select = $connection->select()->from($table, ['value_id', 'value'])->where($where);
1687+
$origRow = $connection->fetchRow($select);
1688+
$origValueId = $origRow['value_id'] ?? false;
1689+
$origValue = $origRow['value'] ?? null;
16881690

16891691
if ($origValueId === false && $newValue !== null) {
16901692
$this->_insertAttribute($object, $attribute, $newValue);
16911693
} elseif ($origValueId !== false && $newValue !== null) {
16921694
$this->_updateAttribute($object, $attribute, $origValueId, $newValue);
1693-
} elseif ($origValueId !== false && $newValue === null) {
1695+
} elseif ($origValueId !== false && $newValue === null && $origValue !== null) {
16941696
$connection->delete($table, $where);
16951697
}
16961698
$this->_processAttributeValues();

app/code/Magento/Elasticsearch/SearchAdapter/Aggregation/Builder/Term.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ public function build(
2222
array $queryResult,
2323
DataProviderInterface $dataProvider
2424
) {
25+
$buckets = $queryResult['aggregations'][$bucket->getName()]['buckets'] ?? [];
2526
$values = [];
26-
foreach ($queryResult['aggregations'][$bucket->getName()]['buckets'] as $resultBucket) {
27+
foreach ($buckets as $resultBucket) {
2728
$values[$resultBucket['key']] = [
2829
'value' => $resultBucket['key'],
2930
'count' => $resultBucket['doc_count'],
3031
];
3132
}
33+
3234
return $values;
3335
}
3436
}

app/code/Magento/Elasticsearch/SearchAdapter/Query/Builder/Match.php

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
*/
66
namespace Magento\Elasticsearch\SearchAdapter\Query\Builder;
77

8+
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\AttributeProvider;
9+
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldType\ResolverInterface as TypeResolver;
10+
use Magento\Elasticsearch\SearchAdapter\Query\ValueTransformerPool;
11+
use Magento\Framework\App\ObjectManager;
812
use Magento\Framework\Search\Request\Query\BoolExpression;
913
use Magento\Framework\Search\Request\QueryInterface as RequestQueryInterface;
1014
use Magento\Elasticsearch\Model\Adapter\FieldMapperInterface;
@@ -26,20 +30,49 @@ class Match implements QueryInterface
2630
private $fieldMapper;
2731

2832
/**
33+
* @deprecated
34+
* @see \Magento\Elasticsearch\SearchAdapter\Query\ValueTransformer\TextTransformer
2935
* @var PreprocessorInterface[]
3036
*/
3137
protected $preprocessorContainer;
3238

39+
/**
40+
* @var AttributeProvider
41+
*/
42+
private $attributeProvider;
43+
44+
/**
45+
* @var TypeResolver
46+
*/
47+
private $fieldTypeResolver;
48+
49+
/**
50+
* @var ValueTransformerPool
51+
*/
52+
private $valueTransformerPool;
53+
3354
/**
3455
* @param FieldMapperInterface $fieldMapper
3556
* @param PreprocessorInterface[] $preprocessorContainer
57+
* @param AttributeProvider|null $attributeProvider
58+
* @param TypeResolver|null $fieldTypeResolver
59+
* @param ValueTransformerPool|null $valueTransformerPool
3660
*/
3761
public function __construct(
3862
FieldMapperInterface $fieldMapper,
39-
array $preprocessorContainer
63+
array $preprocessorContainer,
64+
AttributeProvider $attributeProvider = null,
65+
TypeResolver $fieldTypeResolver = null,
66+
ValueTransformerPool $valueTransformerPool = null
4067
) {
4168
$this->fieldMapper = $fieldMapper;
4269
$this->preprocessorContainer = $preprocessorContainer;
70+
$this->attributeProvider = $attributeProvider ?? ObjectManager::getInstance()
71+
->get(AttributeProvider::class);
72+
$this->fieldTypeResolver = $fieldTypeResolver ?? ObjectManager::getInstance()
73+
->get(TypeResolver::class);
74+
$this->valueTransformerPool = $valueTransformerPool ?? ObjectManager::getInstance()
75+
->get(ValueTransformerPool::class);
4376
}
4477

4578
/**
@@ -72,10 +105,6 @@ public function build(array $selectQuery, RequestQueryInterface $requestQuery, $
72105
*/
73106
protected function prepareQuery($queryValue, $conditionType)
74107
{
75-
$queryValue = $this->escape($queryValue);
76-
foreach ($this->preprocessorContainer as $preprocessor) {
77-
$queryValue = $preprocessor->process($queryValue);
78-
}
79108
$condition = $conditionType === BoolExpression::QUERY_CONDITION_NOT ?
80109
self::QUERY_CONDITION_MUST_NOT : $conditionType;
81110
return [
@@ -104,10 +133,24 @@ protected function buildQueries(array $matches, array $queryValue)
104133

105134
// Checking for quoted phrase \"phrase test\", trim escaped surrounding quotes if found
106135
$count = 0;
107-
$value = preg_replace('#^\\\\"(.*)\\\\"$#m', '$1', $queryValue['value'], -1, $count);
136+
$value = preg_replace('#^"(.*)"$#m', '$1', $queryValue['value'], -1, $count);
108137
$condition = ($count) ? 'match_phrase' : 'match';
109138

139+
$transformedTypes = [];
110140
foreach ($matches as $match) {
141+
$attributeAdapter = $this->attributeProvider->getByAttributeCode($match['field']);
142+
$fieldType = $this->fieldTypeResolver->getFieldType($attributeAdapter);
143+
$valueTransformer = $this->valueTransformerPool->get($fieldType ?? 'text');
144+
$valueTransformerHash = \spl_object_hash($valueTransformer);
145+
if (!isset($transformedTypes[$valueTransformerHash])) {
146+
$transformedTypes[$valueTransformerHash] = $valueTransformer->transform($value);
147+
}
148+
$transformedValue = $transformedTypes[$valueTransformerHash];
149+
if (null === $transformedValue) {
150+
//Value is incompatible with this field type.
151+
continue;
152+
}
153+
111154
$resolvedField = $this->fieldMapper->getFieldName(
112155
$match['field'],
113156
['type' => FieldMapperInterface::TYPE_QUERY]
@@ -117,8 +160,8 @@ protected function buildQueries(array $matches, array $queryValue)
117160
'body' => [
118161
$condition => [
119162
$resolvedField => [
120-
'query' => $value,
121-
'boost' => isset($match['boost']) ? $match['boost'] : 1,
163+
'query' => $transformedValue,
164+
'boost' => $match['boost'] ?? 1,
122165
],
123166
],
124167
],
@@ -131,16 +174,13 @@ protected function buildQueries(array $matches, array $queryValue)
131174
/**
132175
* Escape a value for special query characters such as ':', '(', ')', '*', '?', etc.
133176
*
134-
* Cut trailing plus or minus sign, and @ symbol, using of which causes InnoDB to report a syntax error.
135-
* https://dev.mysql.com/doc/refman/5.7/en/fulltext-boolean.html Fulltext-boolean search docs.
136-
*
177+
* @deprecated
178+
* @see \Magento\Elasticsearch\SearchAdapter\Query\ValueTransformer\TextTransformer
137179
* @param string $value
138180
* @return string
139181
*/
140182
protected function escape($value)
141183
{
142-
$value = preg_replace('/@+|[@+-]+$/', '', $value);
143-
144184
$pattern = '/(\+|-|&&|\|\||!|\(|\)|\{|}|\[|]|\^|"|~|\*|\?|:|\\\)/';
145185
$replace = '\\\$1';
146186

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Elasticsearch\SearchAdapter\Query\ValueTransformer;
9+
10+
use Magento\Elasticsearch\Model\Adapter\FieldType\Date;
11+
use Magento\Elasticsearch\SearchAdapter\Query\ValueTransformerInterface;
12+
13+
/**
14+
* Value transformer for date type fields.
15+
*/
16+
class DateTransformer implements ValueTransformerInterface
17+
{
18+
/**
19+
* @var Date
20+
*/
21+
private $dateFieldType;
22+
23+
/**
24+
* @param Date $dateFieldType
25+
*/
26+
public function __construct(Date $dateFieldType)
27+
{
28+
$this->dateFieldType = $dateFieldType;
29+
}
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
public function transform(string $value): ?string
35+
{
36+
try {
37+
$formattedDate = $this->dateFieldType->formatDate(null, $value);
38+
} catch (\Exception $e) {
39+
$formattedDate = null;
40+
}
41+
42+
return $formattedDate;
43+
}
44+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Elasticsearch\SearchAdapter\Query\ValueTransformer;
9+
10+
use Magento\Elasticsearch\SearchAdapter\Query\ValueTransformerInterface;
11+
12+
/**
13+
* Value transformer for float type fields.
14+
*/
15+
class FloatTransformer implements ValueTransformerInterface
16+
{
17+
/**
18+
* @inheritdoc
19+
*/
20+
public function transform(string $value): ?float
21+
{
22+
return \is_numeric($value) ? (float) $value : null;
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Elasticsearch\SearchAdapter\Query\ValueTransformer;
9+
10+
use Magento\Elasticsearch\SearchAdapter\Query\ValueTransformerInterface;
11+
12+
/**
13+
* Value transformer for integer type fields.
14+
*/
15+
class IntegerTransformer implements ValueTransformerInterface
16+
{
17+
/**
18+
* @inheritdoc
19+
*/
20+
public function transform(string $value): ?int
21+
{
22+
return \is_numeric($value) ? (int) $value : null;
23+
}
24+
}

0 commit comments

Comments
 (0)