Skip to content

Commit 78e5040

Browse files
committed
MAGETWO-60037: User is able to create empty URL key for category which leads to numerous errors
2 parents fa0ed96 + 149fe69 commit 78e5040

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,18 @@ public function __construct(
4242
/**
4343
* @param \Magento\Framework\Event\Observer $observer
4444
* @return void
45+
* @throws \Magento\Framework\Exception\LocalizedException
4546
*/
4647
public function execute(\Magento\Framework\Event\Observer $observer)
4748
{
4849
/** @var Category $category */
4950
$category = $observer->getEvent()->getCategory();
5051
if ($category->getUrlKey() !== false) {
51-
$category->setUrlKey($this->categoryUrlPathGenerator->getUrlKey($category))
52+
$resultUrlKey = $this->categoryUrlPathGenerator->getUrlKey($category);
53+
if (empty($resultUrlKey)) {
54+
throw new \Magento\Framework\Exception\LocalizedException(__('Invalid URL key'));
55+
}
56+
$category->setUrlKey($resultUrlKey)
5257
->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
5358
if (!$category->isObjectNew()) {
5459
$category->getResource()->saveAttribute($category, 'url_path');

dev/tests/integration/testsuite/Magento/Catalog/Model/Indexer/Category/ProductTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ protected function setUp()
4141
}
4242

4343
/**
44+
* @magentoAppArea adminhtml
4445
* @magentoDataFixture Magento/Catalog/_files/indexer_catalog_category.php
4546
* @magentoDbIsolation enabled
4647
*/
@@ -214,7 +215,7 @@ protected function getCategories($count)
214215
\Magento\Catalog\Model\Category::class
215216
);
216217

217-
$result = $category->getCollection()->getItems();
218+
$result = $category->getCollection()->addAttributeToSelect('name')->getItems();
218219
$result = array_slice($result, 2);
219220

220221
return array_slice($result, 0, $count);

dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGeneratorTest.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ protected function setUp()
2525
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
2626
}
2727

28-
public function tearDown()
29-
{
30-
$category = $this->objectManager->create(\Magento\Catalog\Model\Category::class);
31-
$category->load(3);
32-
$category->delete();
33-
}
34-
3528
/**
3629
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories.php
3730
* @magentoDbIsolation enabled
@@ -96,6 +89,37 @@ public function testGenerateUrlRewritesWithSaveHistory()
9689
$this->assertResults($categoryExpectedResult, $actualResults);
9790
}
9891

92+
/**
93+
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories.php
94+
* @magentoDbIsolation enabled
95+
* @magentoAppIsolation enabled
96+
* @param string $urlKey
97+
* @dataProvider incorrectUrlRewritesDataProvider
98+
*/
99+
public function testGenerateUrlRewritesWithIncorrectUrlKey($urlKey)
100+
{
101+
$this->setExpectedException(
102+
\Magento\Framework\Exception\LocalizedException::class,
103+
'Invalid URL key'
104+
);
105+
/** @var \Magento\Catalog\Api\CategoryRepositoryInterface $repository */
106+
$repository = $this->objectManager->get(\Magento\Catalog\Api\CategoryRepositoryInterface::class);
107+
$category = $repository->get(3);
108+
$category->setUrlKey($urlKey);
109+
$repository->save($category);
110+
}
111+
112+
/**
113+
* @return array
114+
*/
115+
public function incorrectUrlRewritesDataProvider()
116+
{
117+
return [
118+
['#'],
119+
['//']
120+
];
121+
}
122+
99123
/**
100124
* @param array $filter
101125
* @return array

0 commit comments

Comments
 (0)