Skip to content

Commit

Permalink
Merge pull request #3937 from magento-mpi/pr_2019_03_20
Browse files Browse the repository at this point in the history
[mpi] bugfixes
  • Loading branch information
dhorytskyi authored Mar 25, 2019
2 parents 677f1b6 + 267a2b9 commit 9d6d4e2
Show file tree
Hide file tree
Showing 30 changed files with 830 additions and 243 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,25 +282,23 @@ public function getGridIdsJson()
if (!$this->getUseSelectAll()) {
return '';
}
/** @var \Magento\Framework\Data\Collection $allIdsCollection */
$allIdsCollection = clone $this->getParentBlock()->getCollection();

if ($this->getMassactionIdField()) {
$massActionIdField = $this->getMassactionIdField();
/** @var \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection $collection */
$collection = clone $this->getParentBlock()->getCollection();

if ($collection instanceof AbstractDb) {
$idsSelect = clone $collection->getSelect();
$idsSelect->reset(\Magento\Framework\DB\Select::ORDER);
$idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_COUNT);
$idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);
$idsSelect->reset(\Magento\Framework\DB\Select::COLUMNS);
$idsSelect->columns($this->getMassactionIdField(), 'main_table');
$idList = $collection->getConnection()->fetchCol($idsSelect);
} else {
$massActionIdField = $this->getParentBlock()->getMassactionIdField();
$idList = $collection->setPageSize(0)->getColumnValues($this->getMassactionIdField());
}

if ($allIdsCollection instanceof AbstractDb) {
$allIdsCollection->getSelect()->limit();
$allIdsCollection->clear();
}

$gridIds = $allIdsCollection->setPageSize(0)->getColumnValues($massActionIdField);
if (!empty($gridIds)) {
return join(",", $gridIds);
}
return '';
return implode(',', $idList);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,62 +269,6 @@ public function testGetGridIdsJsonWithoutUseSelectAll()
$this->assertEmpty($this->_block->getGridIdsJson());
}

/**
* @param array $items
* @param string $result
*
* @dataProvider dataProviderGetGridIdsJsonWithUseSelectAll
*/
public function testGetGridIdsJsonWithUseSelectAll(array $items, $result)
{
$this->_block->setUseSelectAll(true);

if ($this->_block->getMassactionIdField()) {
$massActionIdField = $this->_block->getMassactionIdField();
} else {
$massActionIdField = $this->_block->getParentBlock()->getMassactionIdField();
}

$collectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection::class)
->disableOriginalConstructor()
->getMock();

$this->_gridMock->expects($this->once())
->method('getCollection')
->willReturn($collectionMock);
$collectionMock->expects($this->once())
->method('setPageSize')
->with(0)
->willReturnSelf();
$collectionMock->expects($this->once())
->method('getColumnValues')
->with($massActionIdField)
->willReturn($items);

$this->assertEquals($result, $this->_block->getGridIdsJson());
}

/**
* @return array
*/
public function dataProviderGetGridIdsJsonWithUseSelectAll()
{
return [
[
[],
'',
],
[
[1],
'1',
],
[
[1, 2, 3],
'1,2,3',
],
];
}

/**
* @param string $itemId
* @param array|\Magento\Framework\DataObject $item
Expand Down
3 changes: 2 additions & 1 deletion app/code/Magento/Cms/Model/Wysiwyg/Images/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ public function getDirsCollection($path)
$collection = $this->getCollection($path)
->setCollectDirs(true)
->setCollectFiles(false)
->setCollectRecursively(false);
->setCollectRecursively(false)
->setOrder('basename', \Magento\Framework\Data\Collection\Filesystem::SORT_ORDER_ASC);

$conditions = $this->getConditionsForExcludeDirs();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,10 @@ protected function generalTestGetDirsCollection($path, $collectionArray = [], $e
->method('setCollectRecursively')
->with(false)
->willReturnSelf();
$storageCollectionMock->expects($this->once())
->method('setOrder')
->with('basename', \Magento\Framework\Data\Collection\Filesystem::SORT_ORDER_ASC)
->willReturnSelf();
$storageCollectionMock->expects($this->once())
->method('getIterator')
->willReturn(new \ArrayIterator($collectionArray));
Expand Down
8 changes: 5 additions & 3 deletions app/code/Magento/Eav/Model/Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -1683,14 +1683,16 @@ public function saveAttribute(DataObject $object, $attributeCode)
$connection->beginTransaction();

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

if ($origValueId === false && $newValue !== null) {
$this->_insertAttribute($object, $attribute, $newValue);
} elseif ($origValueId !== false && $newValue !== null) {
$this->_updateAttribute($object, $attribute, $origValueId, $newValue);
} elseif ($origValueId !== false && $newValue === null) {
} elseif ($origValueId !== false && $newValue === null && $origValue !== null) {
$connection->delete($table, $where);
}
$this->_processAttributeValues();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ public function build(
array $queryResult,
DataProviderInterface $dataProvider
) {
$buckets = $queryResult['aggregations'][$bucket->getName()]['buckets'] ?? [];
$values = [];
foreach ($queryResult['aggregations'][$bucket->getName()]['buckets'] as $resultBucket) {
foreach ($buckets as $resultBucket) {
$values[$resultBucket['key']] = [
'value' => $resultBucket['key'],
'count' => $resultBucket['doc_count'],
];
}

return $values;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
*/
namespace Magento\Elasticsearch\SearchAdapter\Query\Builder;

use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\AttributeProvider;
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldType\ResolverInterface as TypeResolver;
use Magento\Elasticsearch\SearchAdapter\Query\ValueTransformerPool;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Search\Request\Query\BoolExpression;
use Magento\Framework\Search\Request\QueryInterface as RequestQueryInterface;
use Magento\Elasticsearch\Model\Adapter\FieldMapperInterface;
Expand All @@ -26,20 +30,49 @@ class Match implements QueryInterface
private $fieldMapper;

/**
* @deprecated
* @see \Magento\Elasticsearch\SearchAdapter\Query\ValueTransformer\TextTransformer
* @var PreprocessorInterface[]
*/
protected $preprocessorContainer;

/**
* @var AttributeProvider
*/
private $attributeProvider;

/**
* @var TypeResolver
*/
private $fieldTypeResolver;

/**
* @var ValueTransformerPool
*/
private $valueTransformerPool;

/**
* @param FieldMapperInterface $fieldMapper
* @param PreprocessorInterface[] $preprocessorContainer
* @param AttributeProvider|null $attributeProvider
* @param TypeResolver|null $fieldTypeResolver
* @param ValueTransformerPool|null $valueTransformerPool
*/
public function __construct(
FieldMapperInterface $fieldMapper,
array $preprocessorContainer
array $preprocessorContainer,
AttributeProvider $attributeProvider = null,
TypeResolver $fieldTypeResolver = null,
ValueTransformerPool $valueTransformerPool = null
) {
$this->fieldMapper = $fieldMapper;
$this->preprocessorContainer = $preprocessorContainer;
$this->attributeProvider = $attributeProvider ?? ObjectManager::getInstance()
->get(AttributeProvider::class);
$this->fieldTypeResolver = $fieldTypeResolver ?? ObjectManager::getInstance()
->get(TypeResolver::class);
$this->valueTransformerPool = $valueTransformerPool ?? ObjectManager::getInstance()
->get(ValueTransformerPool::class);
}

/**
Expand Down Expand Up @@ -72,10 +105,6 @@ public function build(array $selectQuery, RequestQueryInterface $requestQuery, $
*/
protected function prepareQuery($queryValue, $conditionType)
{
$queryValue = $this->escape($queryValue);
foreach ($this->preprocessorContainer as $preprocessor) {
$queryValue = $preprocessor->process($queryValue);
}
$condition = $conditionType === BoolExpression::QUERY_CONDITION_NOT ?
self::QUERY_CONDITION_MUST_NOT : $conditionType;
return [
Expand Down Expand Up @@ -104,10 +133,24 @@ protected function buildQueries(array $matches, array $queryValue)

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

$transformedTypes = [];
foreach ($matches as $match) {
$attributeAdapter = $this->attributeProvider->getByAttributeCode($match['field']);
$fieldType = $this->fieldTypeResolver->getFieldType($attributeAdapter);
$valueTransformer = $this->valueTransformerPool->get($fieldType ?? 'text');
$valueTransformerHash = \spl_object_hash($valueTransformer);
if (!isset($transformedTypes[$valueTransformerHash])) {
$transformedTypes[$valueTransformerHash] = $valueTransformer->transform($value);
}
$transformedValue = $transformedTypes[$valueTransformerHash];
if (null === $transformedValue) {
//Value is incompatible with this field type.
continue;
}

$resolvedField = $this->fieldMapper->getFieldName(
$match['field'],
['type' => FieldMapperInterface::TYPE_QUERY]
Expand All @@ -117,8 +160,8 @@ protected function buildQueries(array $matches, array $queryValue)
'body' => [
$condition => [
$resolvedField => [
'query' => $value,
'boost' => isset($match['boost']) ? $match['boost'] : 1,
'query' => $transformedValue,
'boost' => $match['boost'] ?? 1,
],
],
],
Expand All @@ -131,16 +174,13 @@ protected function buildQueries(array $matches, array $queryValue)
/**
* Escape a value for special query characters such as ':', '(', ')', '*', '?', etc.
*
* Cut trailing plus or minus sign, and @ symbol, using of which causes InnoDB to report a syntax error.
* https://dev.mysql.com/doc/refman/5.7/en/fulltext-boolean.html Fulltext-boolean search docs.
*
* @deprecated
* @see \Magento\Elasticsearch\SearchAdapter\Query\ValueTransformer\TextTransformer
* @param string $value
* @return string
*/
protected function escape($value)
{
$value = preg_replace('/@+|[@+-]+$/', '', $value);

$pattern = '/(\+|-|&&|\|\||!|\(|\)|\{|}|\[|]|\^|"|~|\*|\?|:|\\\)/';
$replace = '\\\$1';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Elasticsearch\SearchAdapter\Query\ValueTransformer;

use Magento\Elasticsearch\Model\Adapter\FieldType\Date;
use Magento\Elasticsearch\SearchAdapter\Query\ValueTransformerInterface;

/**
* Value transformer for date type fields.
*/
class DateTransformer implements ValueTransformerInterface
{
/**
* @var Date
*/
private $dateFieldType;

/**
* @param Date $dateFieldType
*/
public function __construct(Date $dateFieldType)
{
$this->dateFieldType = $dateFieldType;
}

/**
* @inheritdoc
*/
public function transform(string $value): ?string
{
try {
$formattedDate = $this->dateFieldType->formatDate(null, $value);
} catch (\Exception $e) {
$formattedDate = null;
}

return $formattedDate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Elasticsearch\SearchAdapter\Query\ValueTransformer;

use Magento\Elasticsearch\SearchAdapter\Query\ValueTransformerInterface;

/**
* Value transformer for float type fields.
*/
class FloatTransformer implements ValueTransformerInterface
{
/**
* @inheritdoc
*/
public function transform(string $value): ?float
{
return \is_numeric($value) ? (float) $value : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Elasticsearch\SearchAdapter\Query\ValueTransformer;

use Magento\Elasticsearch\SearchAdapter\Query\ValueTransformerInterface;

/**
* Value transformer for integer type fields.
*/
class IntegerTransformer implements ValueTransformerInterface
{
/**
* @inheritdoc
*/
public function transform(string $value): ?int
{
return \is_numeric($value) ? (int) $value : null;
}
}
Loading

0 comments on commit 9d6d4e2

Please sign in to comment.