Skip to content

Commit 05b300a

Browse files
committed
Merge branch 'MAGETWO-60055' into 2.0-develop-pr2
2 parents fc55693 + 546d4c5 commit 05b300a

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\Event\Observer;
1212
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
1313
use Magento\Framework\Event\ObserverInterface;
14+
use Magento\Framework\Exception\LocalizedException;
1415
use Magento\Store\Model\Store;
1516

1617
class CategoryUrlPathAutogeneratorObserver implements ObserverInterface
@@ -42,13 +43,22 @@ public function __construct(
4243
/**
4344
* @param \Magento\Framework\Event\Observer $observer
4445
* @return void
46+
* @throws LocalizedException
4547
*/
4648
public function execute(\Magento\Framework\Event\Observer $observer)
4749
{
4850
/** @var Category $category */
4951
$category = $observer->getEvent()->getCategory();
5052
if ($category->getUrlKey() !== false) {
51-
$category->setUrlKey($this->categoryUrlPathGenerator->getUrlKey($category))
53+
54+
/** @var string $resultUrlKey */
55+
$resultUrlKey = $this->categoryUrlPathGenerator->getUrlKey($category);
56+
57+
if (empty($resultUrlKey)) {
58+
throw new LocalizedException(__('Invalid URL key'));
59+
}
60+
61+
$category->setUrlKey($resultUrlKey)
5262
->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
5363
if (!$category->isObjectNew()) {
5464
$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'
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: 34 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');
31-
$category->load(3);
32-
$category->delete();
33-
}
34-
3528
/**
3629
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories.php
3730
* @magentoDbIsolation enabled
@@ -96,6 +89,40 @@ 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+
106+
/** @var \Magento\Catalog\Api\CategoryRepositoryInterface $repository */
107+
$repository = $this->objectManager->get(\Magento\Catalog\Api\CategoryRepositoryInterface::class);
108+
109+
$category = $repository->get(3);
110+
$category->setUrlKey($urlKey);
111+
112+
$repository->save($category);
113+
}
114+
115+
/**
116+
* @return array
117+
*/
118+
public function incorrectUrlRewritesDataProvider()
119+
{
120+
return [
121+
['#'],
122+
['//']
123+
];
124+
}
125+
99126
/**
100127
* @param array $filter
101128
* @return array

0 commit comments

Comments
 (0)