diff --git a/app/code/Magento/CatalogRule/Controller/Adminhtml/Promo/Catalog/Save.php b/app/code/Magento/CatalogRule/Controller/Adminhtml/Promo/Catalog/Save.php index 46a8744cf4ce5..d03e7ac48afea 100644 --- a/app/code/Magento/CatalogRule/Controller/Adminhtml/Promo/Catalog/Save.php +++ b/app/code/Magento/CatalogRule/Controller/Adminhtml/Promo/Catalog/Save.php @@ -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', @@ -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; diff --git a/app/code/Magento/CatalogRule/Model/Rule.php b/app/code/Magento/CatalogRule/Model/Rule.php index 1bdab229ba82a..859dbb110dec4 100644 --- a/app/code/Magento/CatalogRule/Model/Rule.php +++ b/app/code/Magento/CatalogRule/Model/Rule.php @@ -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; + } } diff --git a/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php b/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php index 1d41561cc8cbf..285429e8af872 100644 --- a/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php @@ -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], + ]; + } }