Skip to content

Commit 615dee1

Browse files
author
Mohan Ahuja
committed
ACP2E-345: Unable to save product URL key with a hyphen "-" at end.
- Added message for user to let them know allowed URL Key characters - Added validation so that not allowed characters are not passed from the UI itself - Created MFTF test
1 parent 81340cb commit 615dee1

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Setup\Patch\Data;
8+
9+
use Magento\Eav\Setup\EavSetup;
10+
use Magento\Eav\Setup\EavSetupFactory;
11+
use Magento\Framework\Setup\ModuleDataSetupInterface;
12+
use Magento\Framework\Setup\Patch\DataPatchInterface;
13+
use Magento\Framework\Setup\Patch\PatchVersionInterface;
14+
15+
/**
16+
* Class UpdateProductUrlKeyDescription
17+
*
18+
* @package Magento\Catalog\Setup\Patch
19+
*/
20+
class UpdateProductUrlKeyDescription implements DataPatchInterface, PatchVersionInterface
21+
{
22+
/**
23+
* @var ModuleDataSetupInterface
24+
*/
25+
private $moduleDataSetup;
26+
27+
/**
28+
* @var EavSetupFactory
29+
*/
30+
private $eavSetupFactory;
31+
32+
/**
33+
* PatchInitial constructor.
34+
* @param ModuleDataSetupInterface $moduleDataSetup
35+
* @param EavSetupFactory $eavSetupFactory
36+
*/
37+
public function __construct(
38+
ModuleDataSetupInterface $moduleDataSetup,
39+
EavSetupFactory $eavSetupFactory
40+
) {
41+
$this->moduleDataSetup = $moduleDataSetup;
42+
$this->eavSetupFactory = $eavSetupFactory;
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function apply()
49+
{
50+
/** @var EavSetup $eavSetup */
51+
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
52+
53+
$eavSetup->updateAttribute(
54+
\Magento\Catalog\Model\Product::ENTITY,
55+
'url_key',
56+
[
57+
'note' => 'The URL key should consist of lowercase characters with hyphens to separate words',
58+
'frontend_class' => 'validate-identifier-clean'
59+
]
60+
);
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public static function getDependencies()
67+
{
68+
return [
69+
UpdateProductAttributes::class,
70+
];
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public static function getVersion()
77+
{
78+
return '2.4.5';
79+
}
80+
81+
/**
82+
* {@inheritdoc}
83+
*/
84+
public function getAliases()
85+
{
86+
return [];
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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="AdminCreateSimpleProductUrlKeyTest">
11+
<annotations>
12+
<features value="Catalog"/>
13+
<stories value="Validate URL key while creating Product via Admin"/>
14+
<title value="Admin user should be informed and restricted on allowed characters in URL Key"/>
15+
<description value="User was not being informed about the allowed characters in the UI for URL Key, but later the URL key was getting trimmed in backend"/>
16+
<severity value="AVERAGE"/>
17+
<testCaseId value="AC-2585"/>
18+
<useCaseId value="ACP2E-345"/>
19+
<group value="product"/>
20+
</annotations>
21+
22+
<actionGroup ref="AdminLoginActionGroup" stepKey="loginAsAdmin"/>
23+
<actionGroup ref="AdminOpenNewProductFormPageActionGroup" stepKey="goToCreateProduct"/>
24+
25+
<!--Trigger SEO drop down-->
26+
<scrollTo selector="{{AdminProductFormBundleSection.seoDropdown}}" stepKey="moveToSEOSection"/>
27+
<click selector="{{AdminProductFormBundleSection.seoDropdown}}" stepKey="openDropDownIfClosed"/>
28+
<waitForPageLoad stepKey="WaitForDropDownSEO"/>
29+
30+
<fillField stepKey="fillUrlKey1" selector="{{AdminProductFormBundleSection.urlKey}}" userInput="example-page-"/>
31+
<see selector="{{AdminProductFormSection.fieldError('uid')}}" userInput="Please enter a valid URL Key (Ex: example-page)." stepKey="seeLastHyphenErrorMessage"/>
32+
33+
<clearField selector="{{AdminProductFormBundleSection.urlKey}}" stepKey="clearFieldUrlKey2"/>
34+
<fillField stepKey="fillUrlKey2" selector="{{AdminProductFormBundleSection.urlKey}}" userInput="Example-Page-"/>
35+
<see selector="{{AdminProductFormSection.fieldError('uid')}}" userInput="Please enter a valid URL Key (Ex: example-page)." stepKey="seeCapitalLetterErrorMessage"/>
36+
37+
<clearField selector="{{AdminProductFormBundleSection.urlKey}}" stepKey="clearFieldUrlKey3"/>
38+
<fillField stepKey="fillUrlKey3" selector="{{AdminProductFormBundleSection.urlKey}}" userInput="example_page."/>
39+
<see selector="{{AdminProductFormSection.fieldError('uid')}}" userInput="Please enter a valid URL Key (Ex: example-page)." stepKey="seeNotAllowedCharactersErrorMessage"/>
40+
41+
<clearField selector="{{AdminProductFormBundleSection.urlKey}}" stepKey="clearFieldUrlKey4"/>
42+
<fillField stepKey="fillUrlKey4" selector="{{AdminProductFormBundleSection.urlKey}}" userInput="example-page-test"/>
43+
<dontSee selector="{{AdminProductFormSection.fieldError('uid')}}" userInput="Please enter a valid URL Key (Ex: example-page)." stepKey="dontSeeErrorMessage"/>
44+
</test>
45+
</tests>

app/code/Magento/Catalog/Ui/DataProvider/CatalogEavValidationRules.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ protected function mapRules($class, array $rules)
7070
case 'validate-digits':
7171
case 'validate-email':
7272
case 'validate-url':
73+
case 'validate-identifier-clean':
7374
case 'validate-alpha':
7475
case 'validate-alphanum':
7576
$rules = array_merge($rules, [$class => true]);

app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,12 @@ define([
817817
},
818818
$.mage.__('Please enter a valid URL Key (Ex: "example-page", "example-page.html" or "anotherlevel/example-page").')//eslint-disable-line max-len
819819
],
820+
'validate-identifier-clean': [
821+
function (value) {
822+
return utils.isEmptyNoTrim(value) || /^[a-z0-9]+(-[a-z0-9]+)*$/.test(value);
823+
},
824+
$.mage.__('Please enter a valid URL Key (Ex: example-page).')
825+
],
820826
'validate-zip-international': [
821827

822828
/*function(v) {

0 commit comments

Comments
 (0)