From 7b8522c85d101ccf7a6aa867956b9c1075c89bf9 Mon Sep 17 00:00:00 2001 From: Yaroslav Zenin Date: Tue, 13 Oct 2020 10:41:31 +0300 Subject: [PATCH 1/3] fix undefined method isAllowedForRuleCondition --- app/code/Magento/SalesRule/Model/Rule/Condition/Product.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/SalesRule/Model/Rule/Condition/Product.php b/app/code/Magento/SalesRule/Model/Rule/Condition/Product.php index ff83bb1ee9129..0429786caffad 100644 --- a/app/code/Magento/SalesRule/Model/Rule/Condition/Product.php +++ b/app/code/Magento/SalesRule/Model/Rule/Condition/Product.php @@ -68,9 +68,8 @@ public function loadAttributeOptions() $attributes = []; foreach ($productAttributes as $attribute) { /* @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */ - if (!$attribute->isAllowedForRuleCondition() - || !$attribute->getDataUsingMethod($this->_isUsedForRuleProperty) - ) { + if (!$attribute->getDataUsingMethod($this->_isUsedForRuleProperty) + || !$attribute->isAllowedForRuleCondition()) { continue; } $frontLabel = $attribute->getFrontendLabel(); From 981cd295b4e8eb5c7a318f7abc77098ea182c985 Mon Sep 17 00:00:00 2001 From: "vadim.malesh" Date: Mon, 26 Oct 2020 12:12:30 +0200 Subject: [PATCH 2/3] add unit test --- .../Unit/Model/Rule/Condition/ProductTest.php | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/app/code/Magento/SalesRule/Test/Unit/Model/Rule/Condition/ProductTest.php b/app/code/Magento/SalesRule/Test/Unit/Model/Rule/Condition/ProductTest.php index 22627717a47f2..def46fc1f2dd3 100644 --- a/app/code/Magento/SalesRule/Test/Unit/Model/Rule/Condition/ProductTest.php +++ b/app/code/Magento/SalesRule/Test/Unit/Model/Rule/Condition/ProductTest.php @@ -11,6 +11,7 @@ use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Catalog\Model\ProductCategoryList; use Magento\Catalog\Model\ProductFactory; +use Magento\Catalog\Model\ResourceModel\Eav\Attribute; use Magento\Catalog\Model\ResourceModel\Product; use Magento\Directory\Model\CurrencyFactory; use Magento\Eav\Model\Config; @@ -319,6 +320,73 @@ public function testQuoteLocaleFormatPrice($isValid, $conditionValue, $operator $this->assertEquals($isValid, $this->model->setValue($conditionValue)->validate($item)); } + /** + * Test for loadAttributeOptions + * + * @return void + */ + public function testLoadAttributeOptions(): void + { + $secondAttributeCode = 'second_attribute'; + + $attribute = $this->getMockBuilder(Attribute::class) + ->onlyMethods(['getDataUsingMethod']) + ->disableOriginalConstructor() + ->getMock(); + $attribute->expects($this->atLeastOnce()) + ->method('getDataUsingMethod') + ->with('is_used_for_promo_rules') + ->willReturn(false); + + $attributeSecond = $this->getMockBuilder(Attribute::class) + ->onlyMethods(['getDataUsingMethod', 'isAllowedForRuleCondition', 'getAttributeCode']) + ->addMethods(['getFrontendLabel']) + ->disableOriginalConstructor() + ->getMock(); + $attributeSecond->expects($this->atLeastOnce()) + ->method('getDataUsingMethod') + ->with('is_used_for_promo_rules') + ->willReturn(true); + $attributeSecond->expects($this->atLeastOnce()) + ->method('isAllowedForRuleCondition') + ->willReturn(true); + $attributeSecond->expects($this->atLeastOnce()) + ->method('getFrontendLabel') + ->willReturn('Second Attribute'); + $attributeSecond->expects($this->atLeastOnce()) + ->method('getAttributeCode') + ->willReturn($secondAttributeCode); + + $attributeLoaderInterfaceMock = $this->createMock(AbstractEntity::class); + $attributeLoaderInterfaceMock->expects($this->atLeastOnce()) + ->method('getAttributesByCode') + ->willReturn([$attribute, $attributeSecond]); + + $productResourceMock = $this->createMock(Product::class); + $productResourceMock->expects($this->atLeastOnce()) + ->method('loadAllAttributes') + ->willReturn($attributeLoaderInterfaceMock); + + $model = new SalesRuleProduct( + $this->contextMock, + $this->backendHelperMock, + $this->configMock, + $this->productFactoryMock, + $this->productRepositoryMock, + $productResourceMock, + $this->collectionMock, + $this->format, + [], + $this->productCategoryListMock + ); + + $model->loadAttributeOptions(); + + $this->assertArrayHasKey($secondAttributeCode, $model->getAttributeOption()); + $this->assertArrayHasKey('children::' . $secondAttributeCode, $model->getAttributeOption()); + $this->assertArrayHasKey('parent::' . $secondAttributeCode, $model->getAttributeOption()); + } + /** * DataProvider for testQuoteLocaleFormatPrice * From e3cd5ff131a7c591e6d5d501a0e1e6230d4483b5 Mon Sep 17 00:00:00 2001 From: Indrani Sonawane Date: Tue, 18 Jun 2024 13:00:17 +0530 Subject: [PATCH 3/3] Fix for static test failure --- app/code/Magento/SalesRule/Model/Rule/Condition/Product.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/code/Magento/SalesRule/Model/Rule/Condition/Product.php b/app/code/Magento/SalesRule/Model/Rule/Condition/Product.php index ce34ce4fb7b91..f97376b081327 100644 --- a/app/code/Magento/SalesRule/Model/Rule/Condition/Product.php +++ b/app/code/Magento/SalesRule/Model/Rule/Condition/Product.php @@ -7,8 +7,6 @@ /** * Product rule condition data model - * - * @author Magento Core Team */ class Product extends \Magento\Rule\Model\Condition\Product\AbstractProduct {