Skip to content

Issue 36327: Different validation between attribute creation and update via REST API #36354

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 7 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,11 @@ public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attrib
$this->validateCode($attribute->getAttributeCode());
$this->validateFrontendInput($attribute->getFrontendInput());

$attribute->setBackendType(
$attribute->getBackendTypeByInput($attribute->getFrontendInput())
);
$backendType = $attribute->getBackendTypeByInput($attribute->getFrontendInput());
if ($attribute->getBackendType() && $attribute->getBackendType() !== $backendType) {
throw InputException::invalidFieldValue('backend_type', $attribute->getBackendType());
}
$attribute->setBackendType($backendType);
$attribute->setSourceModel(
$this->productHelper->getAttributeSourceModelByInputType($attribute->getFrontendInput())
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class MultiselectAttribute extends SelectAttribute
{
private const DEFAULT_DATA = [
'frontend_input' => 'multiselect',
'backend_type' => 'text'
];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@
<data key="is_unique">false</data>
<data key="is_searchable">false</data>
<data key="is_visible">true</data>
<data key="backend_type">text</data>
<data key="backend_type">varchar</data>
<data key="is_wysiwyg_enabled">false</data>
<data key="is_visible_in_advanced_search">false</data>
<data key="is_visible_on_front">true</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@

use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductAttributeOptionManagementInterface;
use Magento\Catalog\Helper\Product;
use Magento\Catalog\Model\Product\Attribute\Repository;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Eav\Api\AttributeOptionManagementInterface;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Eav\Api\Data\AttributeFrontendLabelInterface;
use Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\Validator;
use Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory;
use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Attribute\FrontendLabel;
Expand Down Expand Up @@ -328,4 +327,39 @@ public function testSaveSavesDefaultFrontendLabelIfItIsPresentInPayload()

$this->model->save($attributeMock);
}

/**
* @return void
*/
public function testSaveInputExceptionInvalidBackendType()
{
$this->expectException('Magento\Framework\Exception\InputException');
$this->expectExceptionMessage('Invalid value of "decimal" provided for the backend_type field.');
$attributeMock = $this->createPartialMock(
Attribute::class,
[
'getFrontendLabels',
'getDefaultFrontendLabel',
'getAttributeId',
'setAttributeId',
'getAttributeCode',
'getBackendTypeByInput',
'getBackendType'
]
);
$attributeMock->expects($this->once())->method('getAttributeId')->willReturn(null);
$attributeMock->expects($this->once())->method('setAttributeId')->with(null)->willReturnSelf();
$labelMock = $this->createMock(FrontendLabel::class);
$attributeMock->expects($this->any())->method('getFrontendLabels')->willReturn([$labelMock]);
$attributeMock->expects($this->any())->method('getDefaultFrontendLabel')->willReturn('default_label');
$attributeMock->expects($this->any())->method('getAttributeCode')->willReturn('attribute_code');
$attributeMock->expects($this->any())->method('getBackendTypeByInput')->willReturn('varchar');
$attributeMock->expects($this->any())->method('getBackendType')->willReturn('decimal');

$validateMock = $this->createMock(Validator::class);
$this->validatorFactoryMock->expects($this->any())->method('create')->willReturn($validateMock);
$validateMock->expects($this->any())->method('isValid')->willReturn(true);

$this->model->save($attributeMock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Attribute extends \Magento\Catalog\Test\Fixture\Attribute
]
],
'scope' => 'global',
'backend_type' => 'int'
];

/**
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Weee/Test/Fixture/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Attribute extends \Magento\Catalog\Test\Fixture\Attribute
{
private const DEFAULT_DATA = [
'frontend_input' => 'weee',
'backend_type' => null
];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,21 @@ public static function tearDownAfterClass(): void
DataFixture(StoreGroupFixture::class, ['website_id' => '$website2.id$'], 'store_group2'),
DataFixture(StoreFixture::class, ['store_group_id' => '$store_group2.id$'], 'store2'),
DataFixture(StoreFixture::class, ['store_group_id' => '$store_group2.id$'], 'store3'),
DataFixture(AttributeFixture::class, ['frontend_input' => 'price', 'is_filterable' => 1], 'attr1'),
DataFixture(AttributeFixture::class, ['frontend_input' => 'price', 'is_filterable' => 1], 'attr2'),
DataFixture(AttributeFixture::class, ['frontend_input' => 'price', 'is_filterable' => 1], 'attr3'),
DataFixture(
AttributeFixture::class,
['frontend_input' => 'price', 'backend_type' => 'decimal', 'is_filterable' => 1],
'attr1'
),
DataFixture(
AttributeFixture::class,
['frontend_input' => 'price', 'backend_type' => 'decimal', 'is_filterable' => 1],
'attr2'
),
DataFixture(
AttributeFixture::class,
['frontend_input' => 'price', 'backend_type' => 'decimal', 'is_filterable' => 1],
'attr3'
),
DataFixture(ProductFixture::class, ['website_ids' => [1, '$website2.id']], 'product'),
]
public function testSaveCustomPriceAttribute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private function getAttributeData(): array
'used_in_product_listing' => 1,
'used_for_sort_by' => 0,
'frontend_label' => ['Drop-Down Attribute'],
'backend_type' => 'varchar',
'backend_type' => 'int',
'option' => [
'value' => [
'option_1' => ['Option 1'],
Expand Down