Skip to content

Commit b87c1b7

Browse files
authored
ENGCOM-8178: change error to code is reserved key instead of code already exists #28663
2 parents d29db46 + 6e8f85f commit b87c1b7

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Attribute/Validate.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
class Validate extends AttributeAction implements HttpGetActionInterface, HttpPostActionInterface
3434
{
3535
const DEFAULT_MESSAGE_KEY = 'message';
36+
private const RESERVED_ATTRIBUTE_CODES = ['product_type', 'type_id'];
3637

3738
/**
3839
* @var JsonFactory
@@ -145,11 +146,16 @@ public function execute()
145146
);
146147
}
147148

148-
if ($attribute->getId() && !$attributeId || $attributeCode === 'product_type' || $attributeCode === 'type_id') {
149+
if (in_array($attributeCode, self::RESERVED_ATTRIBUTE_CODES, true)) {
150+
$message = __('Code (%1) is a reserved key and cannot be used as attribute code.', $attributeCode);
151+
$this->setMessageToResponse($response, [$message]);
152+
$response->setError(true);
153+
}
154+
155+
if ($attribute->getId() && !$attributeId) {
149156
$message = strlen($this->getRequest()->getParam('attribute_code'))
150157
? __('An attribute with this code already exists.')
151158
: __('An attribute with the same code (%1) already exists.', $attributeCode);
152-
153159
$this->setMessageToResponse($response, [$message]);
154160

155161
$response->setError(true);

app/code/Magento/Catalog/Test/Mftf/Data/ProductAttributeData.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,20 @@
385385
<data key="default_store_label" unique="suffix">Attribute Store label &lt;span&gt; </data>
386386
<data key="frontend_input">text</data>
387387
</entity>
388+
<entity name="ProductTypeIdAttribute" type="ProductAttribute">
389+
<data key="frontend_label">Type id</data>
390+
<data key="attribute_code">type_id</data>
391+
<data key="frontend_input">text</data>
392+
<data key="is_required">No</data>
393+
<data key="is_required_admin">No</data>
394+
</entity>
395+
<entity name="ProductProductTypeAttribute" type="ProductAttribute">
396+
<data key="frontend_label">Product type</data>
397+
<data key="attribute_code">product_type</data>
398+
<data key="frontend_input">text</data>
399+
<data key="is_required">No</data>
400+
<data key="is_required_admin">No</data>
401+
</entity>
388402
<!-- Product attribute from file "export_import_configurable_product.csv" -->
389403
<entity name="ProductAttributeWithTwoOptionsForExportImport" extends="productAttributeDropdownTwoOptions" type="ProductAttribute">
390404
<data key="attribute_code">attribute</data>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
10+
<test name="CreateProductAttributeEntityWithReservedKeysTest">
11+
<annotations>
12+
<features value="Catalog"/>
13+
<stories value="Create Product Attributes"/>
14+
<title value="Attributess with reserved codes should not be created"/>
15+
<description value="Admin should not be able to create product attribute with reserved codes"/>
16+
<severity value="MINOR"/>
17+
<testCaseId value="MC-37806"/>
18+
<group value="catalog"/>
19+
</annotations>
20+
21+
<before>
22+
<actionGroup ref="AdminLoginActionGroup" stepKey="loginAsAdmin"/>
23+
</before>
24+
<after>
25+
<actionGroup ref="AdminLogoutActionGroup" stepKey="logout"/>
26+
</after>
27+
28+
<!--Navigate to Stores > Attributes > Product.-->
29+
<amOnPage url="{{AdminProductAttributeGridPage.url}}" stepKey="goToProductAttributesGrid"/>
30+
31+
<!--Create new Product Attribute as TextField, with type_id code.-->
32+
<actionGroup ref="CreateProductAttributeActionGroup" stepKey="createAttribute">
33+
<argument name="attribute" value="ProductTypeIdAttribute"/>
34+
</actionGroup>
35+
<see stepKey="seeErrorMessage" selector="{{AdminMessagesSection.errorMessage}}" userInput="Code (type_id) is a reserved key and cannot be used as attribute code."/>
36+
37+
<!--Navigate to Stores > Attributes > Product.-->
38+
<amOnPage url="{{AdminProductAttributeGridPage.url}}" stepKey="backToProductAttributesGrid"/>
39+
40+
<!--Create new Product Attribute as TextField, with product_type code.-->
41+
<actionGroup ref="CreateProductAttributeActionGroup" stepKey="createAttribute2">
42+
<argument name="attribute" value="ProductProductTypeAttribute"/>
43+
</actionGroup>
44+
45+
<see stepKey="seeErrorMessage2" selector="{{AdminMessagesSection.errorMessage}}" userInput="Code (product_type) is a reserved key and cannot be used as attribute code."/>
46+
</test>
47+
</tests>

app/code/Magento/Catalog/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ Catalog,Catalog
209209
"You saved the product attribute.","You saved the product attribute."
210210
"An attribute with this code already exists.","An attribute with this code already exists."
211211
"An attribute with the same code (%1) already exists.","An attribute with the same code (%1) already exists."
212+
"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."
212213
"The value of Admin must be unique.","The value of Admin must be unique."
213214
"The value of Admin scope can't be empty.","The value of Admin scope can't be empty."
214215
"You duplicated the product.","You duplicated the product."

0 commit comments

Comments
 (0)