Skip to content
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

change error to code is reserved key instead of code already exists #28663

Merged
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 @@ -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
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions app/code/Magento/Catalog/Test/Mftf/Data/ProductAttributeData.xml
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,20 @@
<data key="default_store_label" unique="suffix">Attribute Store label &lt;span&gt; </data>
<data key="frontend_input">text</data>
</entity>
<entity name="ProductTypeIdAttribute" type="ProductAttribute">
<data key="frontend_label">Type id</data>
<data key="attribute_code">type_id</data>
<data key="frontend_input">text</data>
<data key="is_required">No</data>
<data key="is_required_admin">No</data>
</entity>
<entity name="ProductProductTypeAttribute" type="ProductAttribute">
<data key="frontend_label">Product type</data>
<data key="attribute_code">product_type</data>
<data key="frontend_input">text</data>
<data key="is_required">No</data>
<data key="is_required_admin">No</data>
</entity>
<!-- Product attribute from file "export_import_configurable_product.csv" -->
<entity name="ProductAttributeWithTwoOptionsForExportImport" extends="productAttributeDropdownTwoOptions" type="ProductAttribute">
<data key="attribute_code">attribute</data>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
<test name="CreateProductAttributeEntityWithReservedKeysTest">
<annotations>
<features value="Catalog"/>
<stories value="Create Product Attributes"/>
<title value="Attributess with reserved codes should not be created"/>
<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>
1 change: 1 addition & 0 deletions app/code/Magento/Catalog/i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down