Skip to content

Commit 4841a21

Browse files
author
Eric Bohanon
committed
MAGETWO-70779: Add URL Rewrite not retrieving categories
1 parent 54998a2 commit 4841a21

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

app/code/Magento/UrlRewrite/Block/Catalog/Category/Tree.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ public function getTreeArray($parentId = null, $asJson = false, $recursionLevel
119119
$result = $this->_getNodesArray($this->getRoot(null, $recursionLevel));
120120
}
121121

122+
$result = $this->convertNameSingleQuote($result);
123+
122124
if ($asJson) {
123125
return $this->_jsonEncoder->encode($result);
124126
}
@@ -182,6 +184,27 @@ protected function _getNodesArray($node)
182184
return $result;
183185
}
184186

187+
/**
188+
* Scrubs category names of single quotes, replacing them with HTML Entity equivalent
189+
*
190+
* @param array $node
191+
* @return array
192+
*/
193+
private function convertNameSingleQuote($node)
194+
{
195+
if (!empty($node['name']) && strpos($node['name'], '\'') !== false) {
196+
$node['name'] = str_replace('\'', ''', $node['name']);
197+
}
198+
199+
// Recurse through all children
200+
$childrenCount = !empty($node['children']) ? count($node['children']) : 0;
201+
for ($i = 0; $i < $childrenCount; $i++) {
202+
$node['children'][$i] = $this->convertNameSingleQuote($node['children'][$i]);
203+
}
204+
205+
return $node;
206+
}
207+
185208
/**
186209
* Get URL for categories tree ajax loader
187210
*
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/** @var $category \Magento\Catalog\Model\Category */
8+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
9+
10+
$categoryFirst = $objectManager->create(\Magento\Catalog\Model\Category::class);
11+
$categoryFirst->setName('Category 1')
12+
->setPath('1/2')
13+
->setLevel(2)
14+
->setAvailableSortBy('name')
15+
->setDefaultSortBy('name')
16+
->setIsActive(true)
17+
->setPosition(1)
18+
->save();
19+
20+
// products from this fixture were moved to indexer_catalog_products.php
21+
$categorySecond = $objectManager->create(\Magento\Catalog\Model\Category::class);
22+
$categorySecond->setName('\'Category 6\'')
23+
->setPath($categoryFirst->getPath())
24+
->setLevel(3)
25+
->setAvailableSortBy('name')
26+
->setDefaultSortBy('name')
27+
->setIsActive(true)
28+
->setPosition(1)
29+
->save();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/** @var \Magento\Framework\ObjectManagerInterface $objectManager */
8+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
9+
10+
/** @var \Magento\Framework\Registry $registry */
11+
$registry = $objectManager->get(\Magento\Framework\Registry::class);
12+
13+
$registry->unregister('isSecureArea');
14+
$registry->register('isSecureArea', true);
15+
16+
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
17+
$collection = $objectManager->create(\Magento\Catalog\Model\ResourceModel\Category\Collection::class);
18+
$collection
19+
->addAttributeToFilter('level', 2)
20+
->load()
21+
->delete();
22+
23+
$registry->unregister('isSecureArea');
24+
$registry->register('isSecureArea', false);

dev/tests/integration/testsuite/Magento/UrlRewrite/Block/Catalog/Category/TreeTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ public function testGetTreeArray()
4545
$this->assertCount(1, $tree['children']);
4646
}
4747

48+
/**
49+
* Test that the getTree() method scrubs single quotes and apostrophes from names
50+
*
51+
* @magentoAppIsolation enabled
52+
* @magentoDataFixture Magento/Catalog/_files/indexer_catalog_category_with_apostrophe.php
53+
* @magentoDataFixture Magento/Catalog/_files/indexer_catalog_products.php
54+
*/
55+
public function testGetTreeArrayApostropheReplaced()
56+
{
57+
$tree = $this->_treeBlock->getTreeArray();
58+
59+
$this->assertContains('&apos;', $tree['children'][0]['children'][0]['children'][0]['name']);
60+
}
61+
4862
/**
4963
* Test prepare grid
5064
*/

0 commit comments

Comments
 (0)