Skip to content

Commit

Permalink
Merge pull request #482 from magento-troll/Troll_Bugfixing
Browse files Browse the repository at this point in the history
[Troll] Bugfixing
  • Loading branch information
Ganin, Roman(rganin) committed Jul 21, 2015
2 parents 98120e1 + eff3f78 commit 043b721
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function execute()
{
if ($this->getRequest()->getPostValue()) {
try {
/** @var \Magento\CatalogRule\Model\Rule $model */
$model = $this->_objectManager->create('Magento\CatalogRule\Model\Rule');
$this->_eventManager->dispatch(
'adminhtml_controller_catalogrule_prepare_save',
Expand Down Expand Up @@ -63,7 +64,13 @@ public function execute()
$this->getRequest()->setParam('rule_id', $model->getId());
$this->_forward('applyRules');
} else {
$this->_objectManager->create('Magento\CatalogRule\Model\Flag')->loadSelf()->setState(1)->save();
if ($model->isRuleBehaviorChanged()) {
$this->_objectManager
->create('Magento\CatalogRule\Model\Flag')
->loadSelf()
->setState(1)
->save();
}
if ($this->getRequest()->getParam('back')) {
$this->_redirect('catalog_rule/*/edit', ['id' => $model->getId()]);
return;
Expand Down
46 changes: 46 additions & 0 deletions app/code/Magento/CatalogRule/Model/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,4 +485,50 @@ public function afterDelete()
$this->_ruleProductProcessor->getIndexer()->invalidate();
return parent::afterDelete();
}

/**
* Check if rule behavior changed
*
* @return bool
*/
public function isRuleBehaviorChanged()
{
if (!$this->isObjectNew()) {
$arrayDiff = $this->dataDiff($this->getOrigData(), $this->getStoredData());
unset($arrayDiff['name']);
unset($arrayDiff['description']);
if (empty($arrayDiff)) {
return false;
}
}
return true;
}

/**
* Get array with data differences
* @param array $array1
* @param array $array2
*
* @return array
*/
protected function dataDiff($array1, $array2)
{
$result = [];
foreach ($array1 as $key => $value) {
if (array_key_exists($key, $array2)) {
if (is_array($value)) {
if ($value != $array2[$key]) {
$result[$key] = true;
}
} else {
if ($value != $array2[$key]) {
$result[$key] = true;
}
}
} else {
$result[$key] = true;
}
}
return $result;
}
}
47 changes: 47 additions & 0 deletions app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,51 @@ public function testAfterUpdate()
$this->_ruleProductProcessor->expects($this->once())->method('getIndexer')->will($this->returnValue($indexer));
$this->rule->afterSave();
}

/**
* Test IsRuleBehaviorChanged action
*
* @dataProvider ruleData
* @param array $dataArray
* @param array $originDataArray
* @param bool $isObjectNew
* @param bool $result
*
* @return void
*/
public function testIsRuleBehaviorChanged($dataArray, $originDataArray, $isObjectNew, $result)
{
$this->rule->setData('website_ids', []);
$this->rule->isObjectNew($isObjectNew);
$indexer = $this->getMock('\Magento\Indexer\Model\IndexerInterface');
$indexer->expects($this->any())->method('invalidate');
$this->_ruleProductProcessor->expects($this->any())->method('getIndexer')->will($this->returnValue($indexer));

foreach ($dataArray as $data) {
$this->rule->setData($data);
}
$this->rule->afterSave();

foreach ($originDataArray as $data) {
$this->rule->setOrigData($data);
}
$this->assertEquals($result, $this->rule->isRuleBehaviorChanged());
}

/**
* Data provider for isRuleBehaviorChanged test
*
* @return array
*/
public function ruleData()
{
return [
[['new name', 'new description'], ['name', 'description'], false, false],
[['name', 'description'], ['name', 'description'], false, false],
[['name', 'important_data'], ['name', 'important_data'], false, false],
[['name', 'new important_data'], ['name', 'important_data'], false, true],
[['name', 'description'], ['name', 'description'], true, true],
[['name', 'description'], ['name', 'important_data'], true, true],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
<legend class="legend"><span><?php echo $_element->getLegend() ?></span></legend>
<br>
<?php if ($_element->getComment()): ?>
<p class="comment"><?php echo $block->escapeHtml($_element->getComment()) ?></p>
<div class="messages">
<div class="message message-notice"><?php echo $block->escapeHtml($_element->getComment()) ?></div>
</div>
<?php endif; ?>
<div class="rule-tree-wrapper">
<?php echo $_element->getChildrenHtml() ?>
Expand Down

0 comments on commit 043b721

Please sign in to comment.