Skip to content

Commit a2feeb3

Browse files
committed
MAGETWO-94060: [2.3.x] Unlink CatalogWidget from EAV indexer
1 parent f1154d4 commit a2feeb3

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

app/code/Magento/CatalogWidget/Model/Rule/Condition/Product.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,6 @@ protected function addGlobalAttribute(
163163
\Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute,
164164
\Magento\Catalog\Model\ResourceModel\Product\Collection $collection
165165
) {
166-
$storeId = $this->storeManager->getStore()->getId();
167-
168166
switch ($attribute->getBackendType()) {
169167
case 'decimal':
170168
case 'datetime':
@@ -174,9 +172,14 @@ protected function addGlobalAttribute(
174172
break;
175173
default:
176174
$alias = 'at_' . md5($this->getId()) . $attribute->getAttributeCode();
175+
176+
$connection = $this->_productResource->getConnection();
177+
$storeId = $connection->getIfNullSql($alias . '.store_id', $this->storeManager->getStore()->getId());
178+
$linkField = $attribute->getEntity()->getLinkField();
179+
177180
$collection->getSelect()->join(
178-
[$alias => $collection->getTable('catalog_product_index_eav')],
179-
"($alias.entity_id = e.entity_id) AND ($alias.store_id = $storeId)" .
181+
[$alias => $collection->getTable('catalog_product_entity_varchar')],
182+
"($alias.$linkField = e.$linkField) AND ($alias.store_id = $storeId)" .
180183
" AND ($alias.attribute_id = {$attribute->getId()})",
181184
[]
182185
);

app/code/Magento/CatalogWidget/Test/Unit/Model/Rule/Condition/ProductTest.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ class ProductTest extends \PHPUnit\Framework\TestCase
1717
*/
1818
private $model;
1919

20+
/**
21+
* @var \Magento\Catalog\Model\ResourceModel\Product|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
private $productResource;
24+
2025
/**
2126
* @var \PHPUnit_Framework_MockObject_MockObject
2227
*/
@@ -33,9 +38,9 @@ protected function setUp()
3338
$storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
3439
$storeMock = $this->createMock(\Magento\Store\Api\Data\StoreInterface::class);
3540
$storeManager->expects($this->any())->method('getStore')->willReturn($storeMock);
36-
$productResource = $this->createMock(\Magento\Catalog\Model\ResourceModel\Product::class);
37-
$productResource->expects($this->once())->method('loadAllAttributes')->willReturnSelf();
38-
$productResource->expects($this->once())->method('getAttributesByCode')->willReturn([]);
41+
$this->productResource = $this->createMock(\Magento\Catalog\Model\ResourceModel\Product::class);
42+
$this->productResource->expects($this->once())->method('loadAllAttributes')->willReturnSelf();
43+
$this->productResource->expects($this->once())->method('getAttributesByCode')->willReturn([]);
3944
$productCategoryList = $this->getMockBuilder(\Magento\Catalog\Model\ProductCategoryList::class)
4045
->disableOriginalConstructor()
4146
->getMock();
@@ -45,7 +50,7 @@ protected function setUp()
4550
[
4651
'config' => $eavConfig,
4752
'storeManager' => $storeManager,
48-
'productResource' => $productResource,
53+
'productResource' => $this->productResource,
4954
'productCategoryList' => $productCategoryList,
5055
'data' => [
5156
'rule' => $ruleMock,
@@ -67,6 +72,14 @@ public function testAddToCollection()
6772
$this->attributeMock->expects($this->once())->method('isScopeGlobal')->willReturn(true);
6873
$this->attributeMock->expects($this->once())->method('isScopeGlobal')->willReturn(true);
6974
$this->attributeMock->expects($this->once())->method('getBackendType')->willReturn('multiselect');
75+
76+
$entityMock = $this->createMock(\Magento\Eav\Model\Entity\AbstractEntity::class);
77+
$entityMock->expects($this->once())->method('getLinkField')->willReturn('entitiy_id');
78+
$this->attributeMock->expects($this->once())->method('getEntity')->willReturn($entityMock);
79+
$connection = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
80+
81+
$this->productResource->expects($this->atLeastOnce())->method('getConnection')->willReturn($connection);
82+
7083
$this->model->addToCollection($collectionMock);
7184
}
7285

app/code/Magento/Rule/Model/Condition/AbstractCondition.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,10 @@ public function getValueParsed()
355355
{
356356
if (!$this->hasValueParsed()) {
357357
$value = $this->getData('value');
358-
if (is_array($value) && isset($value[0]) && is_string($value[0])) {
359-
$value = $value[0];
358+
if (is_array($value) && count($value) === 1) {
359+
$value = reset($value);
360360
}
361-
if ($this->isArrayOperatorType() && $value) {
361+
if (!is_array($value) && $this->isArrayOperatorType() && $value) {
362362
$value = preg_split('#\s*[,;]\s*#', $value, null, PREG_SPLIT_NO_EMPTY);
363363
}
364364
$this->setValueParsed($value);

app/code/Magento/Rule/Model/Condition/Sql/Builder.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,22 @@ protected function _getMappedSqlCondition(
163163
$this->_conditionOperatorMap[$conditionOperator]
164164
);
165165

166+
$bindValue = $condition->getBindArgumentValue();
167+
$expression = $value . $this->_connection->quoteInto($sql, $bindValue);
168+
169+
// values for multiselect attributes can be saved in comma separated format
170+
// below is a solution for matching such conditions with selected values
171+
if (in_array($conditionOperator, ['()', '{}']) && is_array($bindValue)) {
172+
foreach ($bindValue as $item) {
173+
$expression .= $this->_connection->quoteInto(
174+
" OR (FIND_IN_SET (?, {$this->_connection->quoteIdentifier($argument)}) > 0)",
175+
$item
176+
);
177+
}
178+
}
179+
166180
return $this->_expressionFactory->create(
167-
['expression' => $value . $this->_connection->quoteInto($sql, $condition->getBindArgumentValue())]
181+
['expression' => $expression]
168182
);
169183
}
170184

0 commit comments

Comments
 (0)