Skip to content

Commit eff3f78

Browse files
committed
Merge branch 'MAGETWO-39708' of github.corp.ebay.com:magento-troll/magento2ce into Troll_Bugfixing
2 parents 473d647 + c66a7ea commit eff3f78

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

app/code/Magento/CatalogRule/Controller/Adminhtml/Promo/Catalog/Save.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function execute()
1818
{
1919
if ($this->getRequest()->getPostValue()) {
2020
try {
21+
/** @var \Magento\CatalogRule\Model\Rule $model */
2122
$model = $this->_objectManager->create('Magento\CatalogRule\Model\Rule');
2223
$this->_eventManager->dispatch(
2324
'adminhtml_controller_catalogrule_prepare_save',
@@ -63,7 +64,13 @@ public function execute()
6364
$this->getRequest()->setParam('rule_id', $model->getId());
6465
$this->_forward('applyRules');
6566
} else {
66-
$this->_objectManager->create('Magento\CatalogRule\Model\Flag')->loadSelf()->setState(1)->save();
67+
if ($model->isRuleBehaviorChanged()) {
68+
$this->_objectManager
69+
->create('Magento\CatalogRule\Model\Flag')
70+
->loadSelf()
71+
->setState(1)
72+
->save();
73+
}
6774
if ($this->getRequest()->getParam('back')) {
6875
$this->_redirect('catalog_rule/*/edit', ['id' => $model->getId()]);
6976
return;

app/code/Magento/CatalogRule/Model/Rule.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,4 +485,50 @@ public function afterDelete()
485485
$this->_ruleProductProcessor->getIndexer()->invalidate();
486486
return parent::afterDelete();
487487
}
488+
489+
/**
490+
* Check if rule behavior changed
491+
*
492+
* @return bool
493+
*/
494+
public function isRuleBehaviorChanged()
495+
{
496+
if (!$this->isObjectNew()) {
497+
$arrayDiff = $this->dataDiff($this->getOrigData(), $this->getStoredData());
498+
unset($arrayDiff['name']);
499+
unset($arrayDiff['description']);
500+
if (empty($arrayDiff)) {
501+
return false;
502+
}
503+
}
504+
return true;
505+
}
506+
507+
/**
508+
* Get array with data differences
509+
* @param array $array1
510+
* @param array $array2
511+
*
512+
* @return array
513+
*/
514+
protected function dataDiff($array1, $array2)
515+
{
516+
$result = [];
517+
foreach ($array1 as $key => $value) {
518+
if (array_key_exists($key, $array2)) {
519+
if (is_array($value)) {
520+
if ($value != $array2[$key]) {
521+
$result[$key] = true;
522+
}
523+
} else {
524+
if ($value != $array2[$key]) {
525+
$result[$key] = true;
526+
}
527+
}
528+
} else {
529+
$result[$key] = true;
530+
}
531+
}
532+
return $result;
533+
}
488534
}

app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,51 @@ public function testAfterUpdate()
226226
$this->_ruleProductProcessor->expects($this->once())->method('getIndexer')->will($this->returnValue($indexer));
227227
$this->rule->afterSave();
228228
}
229+
230+
/**
231+
* Test IsRuleBehaviorChanged action
232+
*
233+
* @dataProvider ruleData
234+
* @param array $dataArray
235+
* @param array $originDataArray
236+
* @param bool $isObjectNew
237+
* @param bool $result
238+
*
239+
* @return void
240+
*/
241+
public function testIsRuleBehaviorChanged($dataArray, $originDataArray, $isObjectNew, $result)
242+
{
243+
$this->rule->setData('website_ids', []);
244+
$this->rule->isObjectNew($isObjectNew);
245+
$indexer = $this->getMock('\Magento\Indexer\Model\IndexerInterface');
246+
$indexer->expects($this->any())->method('invalidate');
247+
$this->_ruleProductProcessor->expects($this->any())->method('getIndexer')->will($this->returnValue($indexer));
248+
249+
foreach ($dataArray as $data) {
250+
$this->rule->setData($data);
251+
}
252+
$this->rule->afterSave();
253+
254+
foreach ($originDataArray as $data) {
255+
$this->rule->setOrigData($data);
256+
}
257+
$this->assertEquals($result, $this->rule->isRuleBehaviorChanged());
258+
}
259+
260+
/**
261+
* Data provider for isRuleBehaviorChanged test
262+
*
263+
* @return array
264+
*/
265+
public function ruleData()
266+
{
267+
return [
268+
[['new name', 'new description'], ['name', 'description'], false, false],
269+
[['name', 'description'], ['name', 'description'], false, false],
270+
[['name', 'important_data'], ['name', 'important_data'], false, false],
271+
[['name', 'new important_data'], ['name', 'important_data'], false, true],
272+
[['name', 'description'], ['name', 'description'], true, true],
273+
[['name', 'description'], ['name', 'important_data'], true, true],
274+
];
275+
}
229276
}

0 commit comments

Comments
 (0)