diff --git a/app/code/Magento/SalesRule/Model/Rule/Condition/Product.php b/app/code/Magento/SalesRule/Model/Rule/Condition/Product.php index cc65876fb513f..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 { @@ -67,9 +65,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(); 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 2ed42e2a9d3b2..2d4917061f655 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; @@ -321,6 +322,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 *