diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Attribute/Validate.php b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Attribute/Validate.php index a2be7db7e62be..47839941c5837 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Attribute/Validate.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Attribute/Validate.php @@ -33,6 +33,7 @@ class Validate extends AttributeAction implements HttpGetActionInterface, HttpPostActionInterface { const DEFAULT_MESSAGE_KEY = 'message'; + private const RESERVED_ATTRIBUTE_CODES = ['product_type', 'type_id']; /** * @var JsonFactory @@ -145,11 +146,16 @@ public function execute() ); } - if ($attribute->getId() && !$attributeId || $attributeCode === 'product_type' || $attributeCode === 'type_id') { + if (in_array($attributeCode, self::RESERVED_ATTRIBUTE_CODES, true)) { + $message = __('Code (%1) is a reserved key and cannot be used as attribute code.', $attributeCode); + $this->setMessageToResponse($response, [$message]); + $response->setError(true); + } + + if ($attribute->getId() && !$attributeId) { $message = strlen($this->getRequest()->getParam('attribute_code')) ? __('An attribute with this code already exists.') : __('An attribute with the same code (%1) already exists.', $attributeCode); - $this->setMessageToResponse($response, [$message]); $response->setError(true); diff --git a/app/code/Magento/Catalog/Test/Mftf/Data/ProductAttributeData.xml b/app/code/Magento/Catalog/Test/Mftf/Data/ProductAttributeData.xml index ffee02080503e..51658dca78c6a 100644 --- a/app/code/Magento/Catalog/Test/Mftf/Data/ProductAttributeData.xml +++ b/app/code/Magento/Catalog/Test/Mftf/Data/ProductAttributeData.xml @@ -385,6 +385,20 @@ Attribute Store label <span> text + + Type id + type_id + text + No + No + + + Product type + product_type + text + No + No + attribute diff --git a/app/code/Magento/Catalog/Test/Mftf/Test/CreateProductAttributeEntityTest/CreateProductAttributeEntityWithReservedKeysTest.xml b/app/code/Magento/Catalog/Test/Mftf/Test/CreateProductAttributeEntityTest/CreateProductAttributeEntityWithReservedKeysTest.xml new file mode 100644 index 0000000000000..591454ad4421c --- /dev/null +++ b/app/code/Magento/Catalog/Test/Mftf/Test/CreateProductAttributeEntityTest/CreateProductAttributeEntityWithReservedKeysTest.xml @@ -0,0 +1,47 @@ + + + + + + + + + <description value="Admin should not be able to create product attribute with reserved codes"/> + <severity value="MINOR"/> + <testCaseId value="MC-37806"/> + <group value="catalog"/> + </annotations> + + <before> + <actionGroup ref="AdminLoginActionGroup" stepKey="loginAsAdmin"/> + </before> + <after> + <actionGroup ref="AdminLogoutActionGroup" stepKey="logout"/> + </after> + + <!--Navigate to Stores > Attributes > Product.--> + <amOnPage url="{{AdminProductAttributeGridPage.url}}" stepKey="goToProductAttributesGrid"/> + + <!--Create new Product Attribute as TextField, with type_id code.--> + <actionGroup ref="CreateProductAttributeActionGroup" stepKey="createAttribute"> + <argument name="attribute" value="ProductTypeIdAttribute"/> + </actionGroup> + <see stepKey="seeErrorMessage" selector="{{AdminMessagesSection.errorMessage}}" userInput="Code (type_id) is a reserved key and cannot be used as attribute code."/> + + <!--Navigate to Stores > Attributes > Product.--> + <amOnPage url="{{AdminProductAttributeGridPage.url}}" stepKey="backToProductAttributesGrid"/> + + <!--Create new Product Attribute as TextField, with product_type code.--> + <actionGroup ref="CreateProductAttributeActionGroup" stepKey="createAttribute2"> + <argument name="attribute" value="ProductProductTypeAttribute"/> + </actionGroup> + + <see stepKey="seeErrorMessage2" selector="{{AdminMessagesSection.errorMessage}}" userInput="Code (product_type) is a reserved key and cannot be used as attribute code."/> + </test> +</tests> diff --git a/app/code/Magento/Catalog/i18n/en_US.csv b/app/code/Magento/Catalog/i18n/en_US.csv index 555871ef32c26..7b9d4baacffe5 100644 --- a/app/code/Magento/Catalog/i18n/en_US.csv +++ b/app/code/Magento/Catalog/i18n/en_US.csv @@ -209,6 +209,7 @@ Catalog,Catalog "You saved the product attribute.","You saved the product attribute." "An attribute with this code already exists.","An attribute with this code already exists." "An attribute with the same code (%1) already exists.","An attribute with the same code (%1) already exists." +"Code (%1) is a reserved key and cannot be used as attribute code.","Code (%1) is a reserved key and cannot be used as attribute code." "The value of Admin must be unique.","The value of Admin must be unique." "The value of Admin scope can't be empty.","The value of Admin scope can't be empty." "You duplicated the product.","You duplicated the product."