Skip to content

Commit 9bc6cc9

Browse files
add constant array and provide MFTF test
1 parent 580ffe5 commit 9bc6cc9

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
class Validate extends AttributeAction implements HttpGetActionInterface, HttpPostActionInterface
2424
{
2525
const DEFAULT_MESSAGE_KEY = 'message';
26+
const RESERVED_CODES = ['product_type', 'type_id'];
2627

2728
/**
2829
* @var \Magento\Framework\Controller\Result\JsonFactory
@@ -127,8 +128,8 @@ public function execute()
127128
$attributeCode
128129
);
129130

130-
if ($attribute->getId() && !$attributeId || $attributeCode === 'product_type' || $attributeCode === 'type_id') {
131-
if ($attributeCode === 'product_type' || $attributeCode === 'type_id') {
131+
if ($attribute->getId() && !$attributeId || in_array($attributeCode, static::RESERVED_CODES)) {
132+
if (in_array($attributeCode, static::RESERVED_CODES)) {
132133
$message = __('Code (%1) is a reserved key and cannot be used as attribute code.', $attributeCode);
133134
} else {
134135
$message = strlen($this->getRequest()->getParam('attribute_code'))

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,50 @@
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="scope">Global</data>
393+
<data key="is_required">No</data>
394+
<data key="is_required_admin">No</data>
395+
<data key="is_unique">No</data>
396+
<data key="is_searchable">No</data>
397+
<data key="is_visible">No</data>
398+
<data key="is_visible_in_advanced_search">No</data>
399+
<data key="is_visible_on_front">No</data>
400+
<data key="is_filterable">No</data>
401+
<data key="is_filterable_in_search">No</data>
402+
<data key="used_in_product_listing">No</data>
403+
<data key="is_used_for_promo_rules">No</data>
404+
<data key="is_comparable">No</data>
405+
<data key="is_used_in_grid">No</data>
406+
<data key="is_visible_in_grid">No</data>
407+
<data key="is_filterable_in_grid">No</data>
408+
<data key="used_for_sort_by">No</data>
409+
</entity>
410+
<entity name="ProductProductTypeAttribute" type="ProductAttribute">
411+
<data key="frontend_label">Product type</data>
412+
<data key="attribute_code">product_type</data>
413+
<data key="frontend_input">text</data>
414+
<data key="scope">Global</data>
415+
<data key="is_required">No</data>
416+
<data key="is_required_admin">No</data>
417+
<data key="is_unique">No</data>
418+
<data key="is_searchable">No</data>
419+
<data key="is_visible">No</data>
420+
<data key="is_visible_in_advanced_search">No</data>
421+
<data key="is_visible_on_front">No</data>
422+
<data key="is_filterable">No</data>
423+
<data key="is_filterable_in_search">No</data>
424+
<data key="used_in_product_listing">No</data>
425+
<data key="is_used_for_promo_rules">No</data>
426+
<data key="is_comparable">No</data>
427+
<data key="is_used_in_grid">No</data>
428+
<data key="is_visible_in_grid">No</data>
429+
<data key="is_filterable_in_grid">No</data>
430+
<data key="used_for_sort_by">No</data>
431+
</entity>
388432
<!-- Product attribute from file "export_import_configurable_product.csv" -->
389433
<entity name="ProductAttributeWithTwoOptionsForExportImport" extends="productAttributeDropdownTwoOptions" type="ProductAttribute">
390434
<data key="attribute_code">attribute</data>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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="BLOCKER"/>
17+
<group value="Catalog"/>
18+
</annotations>
19+
20+
<before>
21+
<actionGroup ref="AdminLoginActionGroup" stepKey="loginAsAdmin"/>
22+
</before>
23+
<after>
24+
<actionGroup ref="AdminLogoutActionGroup" stepKey="logout"/>
25+
</after>
26+
27+
<!--Navigate to Stores > Attributes > Product.-->
28+
<amOnPage url="{{AdminProductAttributeGridPage.url}}" stepKey="goToProductAttributesGrid"/>
29+
30+
<!--Create new Product Attribute as TextField, with type_id code.-->
31+
<actionGroup ref="CreateProductAttributeActionGroup" stepKey="createAttribute">
32+
<argument name="attribute" value="ProductTypeIdAttribute"/>
33+
</actionGroup>
34+
<see stepKey="seeErrorMessage" selector="{{AdminMessagesSection.errorMessage}}" userInput="Code (type_id) is a reserved key and cannot be used as attribute code."/>
35+
36+
<!--Navigate to Stores > Attributes > Product.-->
37+
<amOnPage url="{{AdminProductAttributeGridPage.url}}" stepKey="backToProductAttributesGrid"/>
38+
39+
<!--Create new Product Attribute as TextField, with product_type code.-->
40+
<actionGroup ref="CreateProductAttributeActionGroup" stepKey="createAttribute2">
41+
<argument name="attribute" value="ProductProductTypeAttribute"/>
42+
</actionGroup>
43+
44+
<see stepKey="seeErrorMessage2" selector="{{AdminMessagesSection.errorMessage}}" userInput="Code (product_type) is a reserved key and cannot be used as attribute code."/>
45+
</test>
46+
</tests>

0 commit comments

Comments
 (0)