Skip to content

Commit 3b44471

Browse files
committed
Merge remote-tracking branch 'trigger/MAGETWO-87454' into BugFixPR
2 parents 8e2b6f9 + dfb25c5 commit 3b44471

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ class Builder
4141
'!()' => ':field NOT IN (?)',
4242
];
4343

44+
/**
45+
* @var array
46+
*/
47+
private $stringConditionOperatorMap = [
48+
'{}' => ':field LIKE ?',
49+
'!{}' => ':field NOT LIKE ?',
50+
];
51+
4452
/**
4553
* @var \Magento\Rule\Model\Condition\Sql\ExpressionFactory
4654
*/
@@ -152,15 +160,27 @@ protected function _getMappedSqlCondition(
152160
}
153161

154162
$defaultValue = 0;
155-
$sql = str_replace(
156-
':field',
157-
$this->_connection->getIfNullSql($this->_connection->quoteIdentifier($argument), $defaultValue),
158-
$this->_conditionOperatorMap[$conditionOperator]
159-
);
160-
161-
$bindValue = $condition->getBindArgumentValue();
162-
$expression = $value . $this->_connection->quoteInto($sql, $bindValue);
163-
163+
//operator 'contains {}' is mapped to 'IN()' query that cannot work with substrings
164+
// adding mapping to 'LIKE %%'
165+
if ($condition->getInputType() === 'string'
166+
&& in_array($conditionOperator, array_keys($this->stringConditionOperatorMap), true)
167+
) {
168+
$sql = str_replace(
169+
':field',
170+
$this->_connection->getIfNullSql($this->_connection->quoteIdentifier($argument), $defaultValue),
171+
$this->stringConditionOperatorMap[$conditionOperator]
172+
);
173+
$bindValue = $condition->getBindArgumentValue();
174+
$expression = $value . $this->_connection->quoteInto($sql, "%$bindValue%");
175+
} else {
176+
$sql = str_replace(
177+
':field',
178+
$this->_connection->getIfNullSql($this->_connection->quoteIdentifier($argument), $defaultValue),
179+
$this->_conditionOperatorMap[$conditionOperator]
180+
);
181+
$bindValue = $condition->getBindArgumentValue();
182+
$expression = $value . $this->_connection->quoteInto($sql, $bindValue);
183+
}
164184
// values for multiselect attributes can be saved in comma-separated format
165185
// below is a solution for matching such conditions with selected values
166186
if (is_array($bindValue) && \in_array($conditionOperator, ['()', '{}'], true)) {

dev/tests/integration/testsuite/Magento/CatalogWidget/Block/Product/ProductListTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,45 @@ private function performAssertions(int $count)
131131
"Product collection was not filtered according to the widget condition."
132132
);
133133
}
134+
135+
/**
136+
* Check that collection returns correct result if use not contains operator for string attribute
137+
*
138+
* @magentoDbIsolation disabled
139+
* @magentoDataFixture Magento/Catalog/_files/product_simple_xss.php
140+
* @magentoDataFixture Magento/Catalog/_files/product_virtual.php
141+
* @dataProvider createCollectionForSkuDataProvider
142+
* @param string $encodedConditions
143+
* @param string $sku
144+
* @return void
145+
*/
146+
public function testCreateCollectionForSku($encodedConditions, $sku)
147+
{
148+
$this->block->setData('conditions_encoded', $encodedConditions);
149+
$productCollection = $this->block->createCollection();
150+
$productCollection->load();
151+
$this->assertEquals(
152+
1,
153+
$productCollection->count(),
154+
"Product collection was not filtered according to the widget condition."
155+
);
156+
$this->assertEquals($sku, $productCollection->getFirstItem()->getSku());
157+
}
158+
159+
/**
160+
* @return array
161+
*/
162+
public function createCollectionForSkuDataProvider()
163+
{
164+
return [
165+
'contains' => ['^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,'
166+
. '`aggregator`:`all`,`value`:`1`,`new_child`:``^],'
167+
. '`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,'
168+
. '`attribute`:`sku`,`operator`:`^[^]`,`value`:`virtual`^]^]' , 'virtual-product'],
169+
'not contains' => ['^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,'
170+
. '`aggregator`:`all`,`value`:`1`,`new_child`:``^],'
171+
. '`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,'
172+
. '`attribute`:`sku`,`operator`:`!^[^]`,`value`:`virtual`^]^]', 'product-with-xss']
173+
];
174+
}
134175
}

0 commit comments

Comments
 (0)