Skip to content

Commit

Permalink
Merge pull request #1458 from magento-plankton/MAGETWO-70819
Browse files Browse the repository at this point in the history
[plankton] MAGETWO-70819: Widget with condition "special date to"
  • Loading branch information
vzabaznov authored Sep 5, 2017
2 parents 5712618 + 5a587aa commit 9223ab3
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 14 deletions.
8 changes: 8 additions & 0 deletions app/code/Magento/CatalogWidget/Block/Product/ProductsList.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,14 @@ protected function getConditions()
$conditions = $this->conditionsHelper->decode($conditions);
}

foreach ($conditions as $key => $condition) {
if (!empty($condition['attribute'])
&& in_array($condition['attribute'], ['special_from_date', 'special_to_date'])
) {
$conditions[$key]['value'] = date('Y-m-d H:i:s', strtotime($condition['value']));
}
}

$this->rule->loadPost(['conditions' => $conditions]);
return $this->rule->getConditions();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ public function testCreateCollection($pagerEnable, $productsCount, $productsPerP
$this->collectionFactory->expects($this->once())->method('create')->willReturn($collection);
$this->productsList->setData('conditions_encoded', 'some_serialized_conditions');

$this->widgetConditionsHelper->expects($this->once())
->method('decode')
->with('some_serialized_conditions')
->willReturn([]);

$this->builder->expects($this->once())->method('attachConditionToCollection')
->with($collection, $this->getConditionsForCollection($collection))
->willReturnSelf();
Expand Down
33 changes: 19 additions & 14 deletions app/code/Magento/Rule/Model/Condition/Sql/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ protected function _joinTablesToCollection(
}

/**
* Returns sql expression based on rule condition.
*
* @param AbstractCondition $condition
* @param string $value
* @return string
Expand All @@ -116,24 +118,27 @@ protected function _joinTablesToCollection(
protected function _getMappedSqlCondition(AbstractCondition $condition, $value = '')
{
$argument = $condition->getMappedSqlField();
if ($argument) {
$conditionOperator = $condition->getOperatorForValidate();

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

$sql = str_replace(
':field',
$this->_connection->getIfNullSql($this->_connection->quoteIdentifier($argument), 0),
$this->_conditionOperatorMap[$conditionOperator]
);
$conditionOperator = $condition->getOperatorForValidate();

return $this->_expressionFactory->create(
['expression' => $value . $this->_connection->quoteInto($sql, $condition->getBindArgumentValue())]
);
if (!isset($this->_conditionOperatorMap[$conditionOperator])) {
throw new \Magento\Framework\Exception\LocalizedException(__('Unknown condition operator'));
}
return '';

$sql = str_replace(
':field',
$this->_connection->getIfNullSql($this->_connection->quoteIdentifier($argument), 0),
$this->_conditionOperatorMap[$conditionOperator]
);

return $this->_expressionFactory->create(
['expression' => $value . $this->_connection->quoteInto($sql, $condition->getBindArgumentValue())]
);
}

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

namespace Magento\Rule\Model\Condition\Sql;

use Magento\TestFramework\Helper\Bootstrap;

class BuilderTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Rule\Model\Condition\Sql\Builder
*/
private $model;

protected function setUp()
{
$this->model = Bootstrap::getObjectManager()->create(\Magento\Rule\Model\Condition\Sql\Builder::class);
}

public function testAttachConditionToCollection()
{
/** @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory */
$collectionFactory = Bootstrap::getObjectManager()->create(
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class
);
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
$collection = $collectionFactory->create();

/** @var \Magento\CatalogWidget\Model\RuleFactory $ruleFactory */
$ruleFactory = Bootstrap::getObjectManager()->create(\Magento\CatalogWidget\Model\RuleFactory::class);
/** @var \Magento\CatalogWidget\Model\Rule $rule */
$rule = $ruleFactory->create();

$ruleConditionArray = [
'conditions' => [
'1' => [
'type' => \Magento\CatalogWidget\Model\Rule\Condition\Combine::class,
'aggregator' => 'all',
'value' => '1',
'new_child' => ''
],
'1--1' => [
'type' => \Magento\CatalogWidget\Model\Rule\Condition\Product::class,
'attribute' => 'category_ids',
'operator' => '==',
'value' => '3'
],
'1--2' => [
'type' => \Magento\CatalogWidget\Model\Rule\Condition\Product::class,
'attribute' => 'special_to_date',
'operator' => '==',
'value' => '2017-09-15'
],
]
];

$rule->loadPost($ruleConditionArray);
$this->model->attachConditionToCollection($collection, $rule->getConditions());

$whereString = 'WHERE (category_id IN (\'3\')))) AND(IFNULL(`e`.`entity_id`, 0) = \'2017-09-15\') ))';
$this->assertNotFalse(strpos($collection->getSelectSql(true), $whereString));
}
}

0 comments on commit 9223ab3

Please sign in to comment.