Skip to content

Commit 2212175

Browse files
ENGCOM-5543: Avoid reindexing inactive catalog rules #23859
2 parents 7d5f1b0 + 287658a commit 2212175

File tree

2 files changed

+61
-32
lines changed

2 files changed

+61
-32
lines changed

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

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* @SuppressWarnings(PHPMD.TooManyFields)
4747
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
4848
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
49+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
4950
*/
5051
class Rule extends \Magento\Rule\Model\AbstractModel implements RuleInterface, IdentityInterface
5152
{
@@ -433,7 +434,7 @@ protected function _getWebsitesMap()
433434
}
434435

435436
/**
436-
* {@inheritdoc}
437+
* @inheritdoc
437438
*/
438439
public function validateData(DataObject $dataObject)
439440
{
@@ -582,12 +583,16 @@ protected function _invalidateCache()
582583
}
583584

584585
/**
585-
* {@inheritdoc}
586+
* @inheritdoc
586587
*
587588
* @return $this
588589
*/
589590
public function afterSave()
590591
{
592+
if (!$this->getIsActive()) {
593+
return parent::afterSave();
594+
}
595+
591596
if ($this->isObjectNew() && !$this->_ruleProductProcessor->isIndexerScheduled()) {
592597
$productIds = $this->getMatchingProductIds();
593598
if (!empty($productIds) && is_array($productIds)) {
@@ -606,14 +611,19 @@ public function afterSave()
606611
*/
607612
public function reindex()
608613
{
609-
$productIds = $this->_productIds ? array_keys(array_filter($this->_productIds, function (array $data) {
610-
return array_filter($data);
611-
})) : [];
614+
$productIds = $this->_productIds ? array_keys(
615+
array_filter(
616+
$this->_productIds,
617+
function (array $data) {
618+
return array_filter($data);
619+
}
620+
)
621+
) : [];
612622
$this->_ruleProductProcessor->reindexList($productIds);
613623
}
614624

615625
/**
616-
* {@inheritdoc}
626+
* @inheritdoc
617627
*
618628
* @return $this
619629
*/
@@ -643,6 +653,7 @@ public function isRuleBehaviorChanged()
643653

644654
/**
645655
* Get array with data differences
656+
*
646657
* @param array $array1
647658
* @param array $array2
648659
*
@@ -664,6 +675,8 @@ protected function dataDiff($array1, $array2)
664675
}
665676

666677
/**
678+
* Getter for conditions field set ID
679+
*
667680
* @param string $formName
668681
* @return string
669682
*/
@@ -672,82 +685,80 @@ public function getConditionsFieldSetId($formName = '')
672685
return $formName . 'rule_conditions_fieldset_' . $this->getId();
673686
}
674687

675-
//@codeCoverageIgnoreStart
676-
677688
/**
678-
* {@inheritdoc}
689+
* @inheritdoc
679690
*/
680691
public function getRuleId()
681692
{
682693
return $this->getData(self::RULE_ID);
683694
}
684695

685696
/**
686-
* {@inheritdoc}
697+
* @inheritdoc
687698
*/
688699
public function setRuleId($ruleId)
689700
{
690701
return $this->setData(self::RULE_ID, $ruleId);
691702
}
692703

693704
/**
694-
* {@inheritdoc}
705+
* @inheritdoc
695706
*/
696707
public function getName()
697708
{
698709
return $this->getData(self::NAME);
699710
}
700711

701712
/**
702-
* {@inheritdoc}
713+
* @inheritdoc
703714
*/
704715
public function setName($name)
705716
{
706717
return $this->setData(self::NAME, $name);
707718
}
708719

709720
/**
710-
* {@inheritdoc}
721+
* @inheritdoc
711722
*/
712723
public function getDescription()
713724
{
714725
return $this->getData(self::DESCRIPTION);
715726
}
716727

717728
/**
718-
* {@inheritdoc}
729+
* @inheritdoc
719730
*/
720731
public function setDescription($description)
721732
{
722733
return $this->setData(self::DESCRIPTION, $description);
723734
}
724735

725736
/**
726-
* {@inheritdoc}
737+
* @inheritdoc
727738
*/
728739
public function getIsActive()
729740
{
730741
return $this->getData(self::IS_ACTIVE);
731742
}
732743

733744
/**
734-
* {@inheritdoc}
745+
* @inheritdoc
735746
*/
736747
public function setIsActive($isActive)
737748
{
738749
return $this->setData(self::IS_ACTIVE, $isActive);
739750
}
740751

741752
/**
742-
* {@inheritdoc}
753+
* @inheritdoc
743754
*/
744755
public function getRuleCondition()
745756
{
746757
return $this->getRuleConditionConverter()->arrayToDataModel($this->getConditions()->asArray());
747758
}
748759

749760
/**
750-
* {@inheritdoc}
761+
* @inheritdoc
751762
*/
752763
public function setRuleCondition($condition)
753764
{
@@ -758,70 +769,72 @@ public function setRuleCondition($condition)
758769
}
759770

760771
/**
761-
* {@inheritdoc}
772+
* @inheritdoc
762773
*/
763774
public function getStopRulesProcessing()
764775
{
765776
return $this->getData(self::STOP_RULES_PROCESSING);
766777
}
767778

768779
/**
769-
* {@inheritdoc}
780+
* @inheritdoc
770781
*/
771782
public function setStopRulesProcessing($isStopProcessing)
772783
{
773784
return $this->setData(self::STOP_RULES_PROCESSING, $isStopProcessing);
774785
}
775786

776787
/**
777-
* {@inheritdoc}
788+
* @inheritdoc
778789
*/
779790
public function getSortOrder()
780791
{
781792
return $this->getData(self::SORT_ORDER);
782793
}
783794

784795
/**
785-
* {@inheritdoc}
796+
* @inheritdoc
786797
*/
787798
public function setSortOrder($sortOrder)
788799
{
789800
return $this->setData(self::SORT_ORDER, $sortOrder);
790801
}
791802

792803
/**
793-
* {@inheritdoc}
804+
* @inheritdoc
794805
*/
795806
public function getSimpleAction()
796807
{
797808
return $this->getData(self::SIMPLE_ACTION);
798809
}
799810

800811
/**
801-
* {@inheritdoc}
812+
* @inheritdoc
802813
*/
803814
public function setSimpleAction($action)
804815
{
805816
return $this->setData(self::SIMPLE_ACTION, $action);
806817
}
807818

808819
/**
809-
* {@inheritdoc}
820+
* @inheritdoc
810821
*/
811822
public function getDiscountAmount()
812823
{
813824
return $this->getData(self::DISCOUNT_AMOUNT);
814825
}
815826

816827
/**
817-
* {@inheritdoc}
828+
* @inheritdoc
818829
*/
819830
public function setDiscountAmount($amount)
820831
{
821832
return $this->setData(self::DISCOUNT_AMOUNT, $amount);
822833
}
823834

824835
/**
836+
* Get from date
837+
*
825838
* @return string
826839
*/
827840
public function getFromDate()
@@ -830,6 +843,8 @@ public function getFromDate()
830843
}
831844

832845
/**
846+
* Get to date
847+
*
833848
* @return string
834849
*/
835850
public function getToDate()
@@ -838,7 +853,7 @@ public function getToDate()
838853
}
839854

840855
/**
841-
* {@inheritdoc}
856+
* @inheritdoc
842857
*
843858
* @return \Magento\CatalogRule\Api\Data\RuleExtensionInterface|null
844859
*/
@@ -848,7 +863,7 @@ public function getExtensionAttributes()
848863
}
849864

850865
/**
851-
* {@inheritdoc}
866+
* @inheritdoc
852867
*
853868
* @param \Magento\CatalogRule\Api\Data\RuleExtensionInterface $extensionAttributes
854869
* @return $this
@@ -859,6 +874,8 @@ public function setExtensionAttributes(RuleExtensionInterface $extensionAttribut
859874
}
860875

861876
/**
877+
* Getter for the rule condition converter
878+
*
862879
* @return Data\Condition\Converter
863880
* @deprecated 100.1.0
864881
*/
@@ -871,8 +888,6 @@ private function getRuleConditionConverter()
871888
return $this->ruleConditionConverter;
872889
}
873890

874-
//@codeCoverageIgnoreEnd
875-
876891
/**
877892
* @inheritDoc
878893
*/

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,27 @@ public function testAfterDelete()
311311
}
312312

313313
/**
314-
* Test after update action
314+
* Test after update action for inactive rule
315315
*
316316
* @return void
317317
*/
318-
public function testAfterUpdate()
318+
public function testAfterUpdateInactive()
319319
{
320320
$this->rule->isObjectNew(false);
321+
$this->rule->setIsActive(0);
322+
$this->_ruleProductProcessor->expects($this->never())->method('getIndexer');
323+
$this->rule->afterSave();
324+
}
325+
326+
/**
327+
* Test after update action for active rule
328+
*
329+
* @return void
330+
*/
331+
public function testAfterUpdateActive()
332+
{
333+
$this->rule->isObjectNew(false);
334+
$this->rule->setIsActive(1);
321335
$indexer = $this->createMock(\Magento\Framework\Indexer\IndexerInterface::class);
322336
$indexer->expects($this->once())->method('invalidate');
323337
$this->_ruleProductProcessor->expects($this->once())->method('getIndexer')->will($this->returnValue($indexer));

0 commit comments

Comments
 (0)