Skip to content

Commit

Permalink
Added improvements to category url key validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaha committed Mar 25, 2020
1 parent 9ba1009 commit 577a27e
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

namespace Magento\CatalogUrlRewrite\Observer;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Category;
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
use Magento\CatalogUrlRewrite\Service\V1\StoreViewService;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\Store;

/**
Expand Down Expand Up @@ -83,7 +85,7 @@ public function __construct(
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
* @throws \Magento\Framework\Exception\LocalizedException
* @throws LocalizedException
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
Expand All @@ -106,34 +108,68 @@ public function execute(\Magento\Framework\Event\Observer $observer)
* Update Url Key
*
* @param Category $category
* @param string $urlKey
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @param string|null $urlKey
* @return void
* @throws LocalizedException
* @throws NoSuchEntityException
*/
private function updateUrlKey(Category $category, ?string $urlKey): void
{
$this->validateUrlKey($category, $urlKey);
$category->setUrlKey($urlKey)
->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
if (!$category->isObjectNew()) {
$category->getResource()->saveAttribute($category, 'url_path');
if ($category->dataHasChangedFor('url_path')) {
$this->updateUrlPathForChildren($category);
}
}
}

/**
* Validate URL key value
*
* @param Category $category
* @param string|null $urlKey
* @return void
* @throws LocalizedException
*/
private function updateUrlKey($category, $urlKey)
private function validateUrlKey(Category $category, ?string $urlKey): void
{
if (empty($urlKey) && !empty($category->getName()) && !empty($category->getUrlKey())) {
throw new LocalizedException(
__(
'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.',
$category->getUrlKey()
)
);
}

if (empty($urlKey) && !empty($category->getName())) {
throw new LocalizedException(
__(
'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.',
$category->getName()
)
);
}

if (empty($urlKey)) {
throw new \Magento\Framework\Exception\LocalizedException(__('Invalid URL key'));
throw new LocalizedException(__('Invalid URL key'));
}

if (in_array($urlKey, $this->getInvalidValues())) {
throw new \Magento\Framework\Exception\LocalizedException(
throw new LocalizedException(
__(
'URL key "%1" matches a reserved endpoint name (%2). Use another URL key.',
$urlKey,
implode(', ', $this->getInvalidValues())
)
);
}

$category->setUrlKey($urlKey)
->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
if (!$category->isObjectNew()) {
$category->getResource()->saveAttribute($category, 'url_path');
if ($category->dataHasChangedFor('url_path')) {
$this->updateUrlPathForChildren($category);
}
}
}

/**
Expand Down Expand Up @@ -199,7 +235,7 @@ protected function isGlobalScope($storeId)
* @param Category $category
* @param Category|null $parentCategory
* @return void
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws NoSuchEntityException
*/
protected function updateUrlPathForCategory(Category $category, Category $parentCategory = null)
{
Expand Down
2 changes: 2 additions & 0 deletions app/code/Magento/CatalogUrlRewrite/i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
"Create Permanent Redirect for URLs if URL Key Changed","Create Permanent Redirect for URLs if URL Key Changed"
"Generate ""category/product"" URL Rewrites","Generate ""category/product"" URL Rewrites"
"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."
"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."
"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."
"<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."
Original file line number Diff line number Diff line change
Expand Up @@ -890,4 +890,70 @@ public function testSaveWithCustomBackendNameAction(): void
MessageInterface::TYPE_ERROR
);
}

/**
* Verify that the category cannot be saved if category name can not be converted to Latin (like Thai)
*
* @return void
*/
public function testSaveWithThaiCategoryNameAction(): void
{
$categoryName = 'ประเภท';
$errorMessage = '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.';
$inputData = [
'name' => $categoryName,
'use_config' => [
'available_sort_by' => 1,
'default_sort_by' => 1
],
'is_active' => '1',
'include_in_menu' => '1',
];
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->getRequest()->setPostValue($inputData);
$this->dispatch('backend/catalog/category/save');
$this->assertSessionMessages(
$this->equalTo(
[
(string)__($errorMessage, $categoryName)
]
),
MessageInterface::TYPE_ERROR
);
}

/**
* Verify that the category cannot be saved if category URL key can not be converted to Latin (like Thai)
*
* @return void
*/
public function testSaveWithThaiCategoryUrlKeyAction(): void
{
$categoryUrlKey = 'ประเภท';
$errorMessage = '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.';
$inputData = [
'name' => 'category name',
'url_key' => $categoryUrlKey,
'use_config' => [
'available_sort_by' => 1,
'default_sort_by' => 1
],
'is_active' => '1',
'include_in_menu' => '1',
];
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->getRequest()->setPostValue($inputData);
$this->dispatch('backend/catalog/category/save');
$this->assertSessionMessages(
$this->equalTo(
[
(string)__($errorMessage, $categoryUrlKey)
]
),
MessageInterface::TYPE_ERROR
);
}
}

0 comments on commit 577a27e

Please sign in to comment.