Skip to content

fix undefined method isAllowedForRuleCondition #30479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: 2.4-develop
Choose a base branch
from
7 changes: 2 additions & 5 deletions app/code/Magento/SalesRule/Model/Rule/Condition/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

/**
* Product rule condition data model
*
* @author Magento Core Team <core@magentocommerce.com>
*/
class Product extends \Magento\Rule\Model\Condition\Product\AbstractProduct
{
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down