Skip to content

Commit 9223ab3

Browse files
authored
Merge pull request #1458 from magento-plankton/MAGETWO-70819
[plankton] MAGETWO-70819: Widget with condition "special date to"
2 parents 5712618 + 5a587aa commit 9223ab3

File tree

4 files changed

+98
-14
lines changed

4 files changed

+98
-14
lines changed

app/code/Magento/CatalogWidget/Block/Product/ProductsList.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,14 @@ protected function getConditions()
259259
$conditions = $this->conditionsHelper->decode($conditions);
260260
}
261261

262+
foreach ($conditions as $key => $condition) {
263+
if (!empty($condition['attribute'])
264+
&& in_array($condition['attribute'], ['special_from_date', 'special_to_date'])
265+
) {
266+
$conditions[$key]['value'] = date('Y-m-d H:i:s', strtotime($condition['value']));
267+
}
268+
}
269+
262270
$this->rule->loadPost(['conditions' => $conditions]);
263271
return $this->rule->getConditions();
264272
}

app/code/Magento/CatalogWidget/Test/Unit/Block/Product/ProductsListTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ public function testCreateCollection($pagerEnable, $productsCount, $productsPerP
288288
$this->collectionFactory->expects($this->once())->method('create')->willReturn($collection);
289289
$this->productsList->setData('conditions_encoded', 'some_serialized_conditions');
290290

291+
$this->widgetConditionsHelper->expects($this->once())
292+
->method('decode')
293+
->with('some_serialized_conditions')
294+
->willReturn([]);
295+
291296
$this->builder->expects($this->once())->method('attachConditionToCollection')
292297
->with($collection, $this->getConditionsForCollection($collection))
293298
->willReturnSelf();

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ protected function _joinTablesToCollection(
108108
}
109109

110110
/**
111+
* Returns sql expression based on rule condition.
112+
*
111113
* @param AbstractCondition $condition
112114
* @param string $value
113115
* @return string
@@ -116,24 +118,27 @@ protected function _joinTablesToCollection(
116118
protected function _getMappedSqlCondition(AbstractCondition $condition, $value = '')
117119
{
118120
$argument = $condition->getMappedSqlField();
119-
if ($argument) {
120-
$conditionOperator = $condition->getOperatorForValidate();
121121

122-
if (!isset($this->_conditionOperatorMap[$conditionOperator])) {
123-
throw new \Magento\Framework\Exception\LocalizedException(__('Unknown condition operator'));
124-
}
122+
// If rule hasn't valid argument - create negative expression to prevent incorrect rule behavior.
123+
if (empty($argument)) {
124+
return $this->_expressionFactory->create(['expression' => '1 = -1']);
125+
}
125126

126-
$sql = str_replace(
127-
':field',
128-
$this->_connection->getIfNullSql($this->_connection->quoteIdentifier($argument), 0),
129-
$this->_conditionOperatorMap[$conditionOperator]
130-
);
127+
$conditionOperator = $condition->getOperatorForValidate();
131128

132-
return $this->_expressionFactory->create(
133-
['expression' => $value . $this->_connection->quoteInto($sql, $condition->getBindArgumentValue())]
134-
);
129+
if (!isset($this->_conditionOperatorMap[$conditionOperator])) {
130+
throw new \Magento\Framework\Exception\LocalizedException(__('Unknown condition operator'));
135131
}
136-
return '';
132+
133+
$sql = str_replace(
134+
':field',
135+
$this->_connection->getIfNullSql($this->_connection->quoteIdentifier($argument), 0),
136+
$this->_conditionOperatorMap[$conditionOperator]
137+
);
138+
139+
return $this->_expressionFactory->create(
140+
['expression' => $value . $this->_connection->quoteInto($sql, $condition->getBindArgumentValue())]
141+
);
137142
}
138143

139144
/**
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Rule\Model\Condition\Sql;
8+
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
11+
class BuilderTest extends \PHPUnit\Framework\TestCase
12+
{
13+
/**
14+
* @var \Magento\Rule\Model\Condition\Sql\Builder
15+
*/
16+
private $model;
17+
18+
protected function setUp()
19+
{
20+
$this->model = Bootstrap::getObjectManager()->create(\Magento\Rule\Model\Condition\Sql\Builder::class);
21+
}
22+
23+
public function testAttachConditionToCollection()
24+
{
25+
/** @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory */
26+
$collectionFactory = Bootstrap::getObjectManager()->create(
27+
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class
28+
);
29+
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
30+
$collection = $collectionFactory->create();
31+
32+
/** @var \Magento\CatalogWidget\Model\RuleFactory $ruleFactory */
33+
$ruleFactory = Bootstrap::getObjectManager()->create(\Magento\CatalogWidget\Model\RuleFactory::class);
34+
/** @var \Magento\CatalogWidget\Model\Rule $rule */
35+
$rule = $ruleFactory->create();
36+
37+
$ruleConditionArray = [
38+
'conditions' => [
39+
'1' => [
40+
'type' => \Magento\CatalogWidget\Model\Rule\Condition\Combine::class,
41+
'aggregator' => 'all',
42+
'value' => '1',
43+
'new_child' => ''
44+
],
45+
'1--1' => [
46+
'type' => \Magento\CatalogWidget\Model\Rule\Condition\Product::class,
47+
'attribute' => 'category_ids',
48+
'operator' => '==',
49+
'value' => '3'
50+
],
51+
'1--2' => [
52+
'type' => \Magento\CatalogWidget\Model\Rule\Condition\Product::class,
53+
'attribute' => 'special_to_date',
54+
'operator' => '==',
55+
'value' => '2017-09-15'
56+
],
57+
]
58+
];
59+
60+
$rule->loadPost($ruleConditionArray);
61+
$this->model->attachConditionToCollection($collection, $rule->getConditions());
62+
63+
$whereString = 'WHERE (category_id IN (\'3\')))) AND(IFNULL(`e`.`entity_id`, 0) = \'2017-09-15\') ))';
64+
$this->assertNotFalse(strpos($collection->getSelectSql(true), $whereString));
65+
}
66+
}

0 commit comments

Comments
 (0)