Skip to content

Commit a02c878

Browse files
ENGCOM-7208: Added improvements to category url key validation logic #27412
- Merge Pull Request #27412 from sergiy-v/magento2:13689-category-url-key-validation-improvements - Merged commits: 1. 577a27e 2. 7689069
2 parents f3df323 + 7689069 commit a02c878

File tree

3 files changed

+123
-19
lines changed

3 files changed

+123
-19
lines changed

app/code/Magento/CatalogUrlRewrite/Observer/CategoryUrlPathAutogeneratorObserver.php

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77

88
namespace Magento\CatalogUrlRewrite\Observer;
99

10+
use Magento\Catalog\Api\CategoryRepositoryInterface;
1011
use Magento\Catalog\Model\Category;
12+
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
1113
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
1214
use Magento\CatalogUrlRewrite\Service\V1\StoreViewService;
13-
use Magento\Catalog\Api\CategoryRepositoryInterface;
14-
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
1515
use Magento\Framework\Event\ObserverInterface;
16+
use Magento\Framework\Exception\LocalizedException;
17+
use Magento\Framework\Exception\NoSuchEntityException;
1618
use Magento\Store\Model\Store;
1719

1820
/**
@@ -83,7 +85,7 @@ public function __construct(
8385
*
8486
* @param \Magento\Framework\Event\Observer $observer
8587
* @return void
86-
* @throws \Magento\Framework\Exception\LocalizedException
88+
* @throws LocalizedException
8789
*/
8890
public function execute(\Magento\Framework\Event\Observer $observer)
8991
{
@@ -106,34 +108,68 @@ public function execute(\Magento\Framework\Event\Observer $observer)
106108
* Update Url Key
107109
*
108110
* @param Category $category
109-
* @param string $urlKey
110-
* @throws \Magento\Framework\Exception\LocalizedException
111-
* @throws \Magento\Framework\Exception\NoSuchEntityException
111+
* @param string|null $urlKey
112+
* @return void
113+
* @throws LocalizedException
114+
* @throws NoSuchEntityException
115+
*/
116+
private function updateUrlKey(Category $category, ?string $urlKey): void
117+
{
118+
$this->validateUrlKey($category, $urlKey);
119+
$category->setUrlKey($urlKey)
120+
->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
121+
if (!$category->isObjectNew()) {
122+
$category->getResource()->saveAttribute($category, 'url_path');
123+
if ($category->dataHasChangedFor('url_path')) {
124+
$this->updateUrlPathForChildren($category);
125+
}
126+
}
127+
}
128+
129+
/**
130+
* Validate URL key value
131+
*
132+
* @param Category $category
133+
* @param string|null $urlKey
134+
* @return void
135+
* @throws LocalizedException
112136
*/
113-
private function updateUrlKey($category, $urlKey)
137+
private function validateUrlKey(Category $category, ?string $urlKey): void
114138
{
139+
if (empty($urlKey) && !empty($category->getName()) && !empty($category->getUrlKey())) {
140+
throw new LocalizedException(
141+
__(
142+
'Invalid URL key. The "%1" URL key can not be used to generate Latin URL key. ' .
143+
'Please use Latin letters and numbers to avoid generating URL key issues.',
144+
$category->getUrlKey()
145+
)
146+
);
147+
}
148+
149+
if (empty($urlKey) && !empty($category->getName())) {
150+
throw new LocalizedException(
151+
__(
152+
'Invalid URL key. The "%1" category name can not be used to generate Latin URL key. ' .
153+
'Please add URL key or change category name using Latin letters and numbers to avoid generating ' .
154+
'URL key issues.',
155+
$category->getName()
156+
)
157+
);
158+
}
159+
115160
if (empty($urlKey)) {
116-
throw new \Magento\Framework\Exception\LocalizedException(__('Invalid URL key'));
161+
throw new LocalizedException(__('Invalid URL key'));
117162
}
118163

119164
if (in_array($urlKey, $this->getInvalidValues())) {
120-
throw new \Magento\Framework\Exception\LocalizedException(
165+
throw new LocalizedException(
121166
__(
122167
'URL key "%1" matches a reserved endpoint name (%2). Use another URL key.',
123168
$urlKey,
124169
implode(', ', $this->getInvalidValues())
125170
)
126171
);
127172
}
128-
129-
$category->setUrlKey($urlKey)
130-
->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
131-
if (!$category->isObjectNew()) {
132-
$category->getResource()->saveAttribute($category, 'url_path');
133-
if ($category->dataHasChangedFor('url_path')) {
134-
$this->updateUrlPathForChildren($category);
135-
}
136-
}
137173
}
138174

139175
/**
@@ -199,7 +235,7 @@ protected function isGlobalScope($storeId)
199235
* @param Category $category
200236
* @param Category|null $parentCategory
201237
* @return void
202-
* @throws \Magento\Framework\Exception\NoSuchEntityException
238+
* @throws NoSuchEntityException
203239
*/
204240
protected function updateUrlPathForCategory(Category $category, Category $parentCategory = null)
205241
{

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
"Create Permanent Redirect for URLs if URL Key Changed","Create Permanent Redirect for URLs if URL Key Changed"
88
"Generate ""category/product"" URL Rewrites","Generate ""category/product"" URL Rewrites"
99
"URL key ""%1"" matches a reserved endpoint name (%2). Use another URL key.","URL key ""%1"" matches a reserved endpoint name (%2). Use another URL key."
10+
"Invalid URL key. The ""%1"" URL key can not be used to generate Latin URL key. Please use Latin letters and numbers to avoid generating URL key issues.","Invalid URL key. The ""%1"" URL key can not be used to generate Latin URL key. Please use Latin letters and numbers to avoid generating URL key issues."
11+
"Invalid URL key. The ""%1"" category name can not be used to generate Latin URL key. Please add URL key or change category name using Latin letters and numbers to avoid generating URL key issues.","Invalid URL key. The ""%1"" category name can not be used to generate Latin URL key. Please add URL key or change category name using Latin letters and numbers to avoid generating URL key issues."
1012
"<strong style=""color:red"">Warning!</strong> Turning this option off will result in permanent removal of category/product URL rewrites without an ability to restore them.","<strong style=""color:red"">Warning!</strong> Turning this option off will result in permanent removal of category/product URL rewrites without an ability to restore them."

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/CategoryTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,4 +890,70 @@ public function testSaveWithCustomBackendNameAction(): void
890890
MessageInterface::TYPE_ERROR
891891
);
892892
}
893+
894+
/**
895+
* Verify that the category cannot be saved if category name can not be converted to Latin (like Thai)
896+
*
897+
* @return void
898+
*/
899+
public function testSaveWithThaiCategoryNameAction(): void
900+
{
901+
$categoryName = 'ประเภท';
902+
$errorMessage = 'Invalid URL key. The "%1" category name can not be used to generate Latin URL key. ' .
903+
'Please add URL key or change category name using Latin letters and numbers to avoid generating ' .
904+
'URL key issues.';
905+
$inputData = [
906+
'name' => $categoryName,
907+
'use_config' => [
908+
'available_sort_by' => 1,
909+
'default_sort_by' => 1
910+
],
911+
'is_active' => '1',
912+
'include_in_menu' => '1',
913+
];
914+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
915+
$this->getRequest()->setPostValue($inputData);
916+
$this->dispatch('backend/catalog/category/save');
917+
$this->assertSessionMessages(
918+
$this->equalTo(
919+
[
920+
(string)__($errorMessage, $categoryName)
921+
]
922+
),
923+
MessageInterface::TYPE_ERROR
924+
);
925+
}
926+
927+
/**
928+
* Verify that the category cannot be saved if category URL key can not be converted to Latin (like Thai)
929+
*
930+
* @return void
931+
*/
932+
public function testSaveWithThaiCategoryUrlKeyAction(): void
933+
{
934+
$categoryUrlKey = 'ประเภท';
935+
$errorMessage = 'Invalid URL key. The "%1" URL key can not be used to generate Latin URL key. ' .
936+
'Please use Latin letters and numbers to avoid generating URL key issues.';
937+
$inputData = [
938+
'name' => 'category name',
939+
'url_key' => $categoryUrlKey,
940+
'use_config' => [
941+
'available_sort_by' => 1,
942+
'default_sort_by' => 1
943+
],
944+
'is_active' => '1',
945+
'include_in_menu' => '1',
946+
];
947+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
948+
$this->getRequest()->setPostValue($inputData);
949+
$this->dispatch('backend/catalog/category/save');
950+
$this->assertSessionMessages(
951+
$this->equalTo(
952+
[
953+
(string)__($errorMessage, $categoryUrlKey)
954+
]
955+
),
956+
MessageInterface::TYPE_ERROR
957+
);
958+
}
893959
}

0 commit comments

Comments
 (0)