Skip to content

Commit fc272ef

Browse files
author
Stanislav Idolov
authored
ENGCOM-1283: #13296: Category name with special characters brakes … #13397
2 parents eff3168 + 11532f0 commit fc272ef

File tree

6 files changed

+123
-36
lines changed

6 files changed

+123
-36
lines changed

app/code/Magento/Catalog/view/adminhtml/web/js/category-tree.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
define([
77
'jquery',
8+
'mageUtils',
89
'jquery/ui',
910
'jquery/jstree/jquery.jstree'
10-
], function ($) {
11+
], function ($, utils) {
1112
'use strict';
1213

1314
$.widget('mage.categoryTree', {
@@ -88,9 +89,10 @@ define([
8889
if (!node) {
8990
return result;
9091
}
92+
9193
result = {
9294
data: {
93-
title: node.name + ' (' + node['product_count'] + ')'
95+
title: utils.unescape(node.name) + ' (' + node['product_count'] + ')'
9496
},
9597
attr: {
9698
'class': node.cls + (!!node.disabled ? ' disabled' : '') //eslint-disable-line no-extra-boolean-cast

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\UrlRewrite\Block\Catalog\Category;
78

9+
use Magento\Backend\Block\Widget\Context;
10+
use Magento\Backend\Helper\Data;
811
use Magento\Catalog\Api\CategoryRepositoryInterface;
12+
use Magento\Catalog\Block\Adminhtml\Category\AbstractCategory;
913
use Magento\Catalog\Model\Category;
14+
use Magento\Catalog\Model\CategoryFactory;
15+
use Magento\Catalog\Model\ProductFactory;
1016
use Magento\Framework\Exception\NoSuchEntityException;
17+
use Magento\Framework\Json\EncoderInterface;
18+
use Magento\Framework\Registry;
1119

1220
/**
1321
* Categories tree block for URL rewrites editing process
1422
*
1523
* @author Magento Core Team <core@magentocommerce.com>
1624
*/
17-
class Tree extends \Magento\Catalog\Block\Adminhtml\Category\AbstractCategory
25+
class Tree extends AbstractCategory
1826
{
1927
/**
2028
* List of allowed category ids
@@ -31,22 +39,22 @@ class Tree extends \Magento\Catalog\Block\Adminhtml\Category\AbstractCategory
3139
/**
3240
* Adminhtml data
3341
*
34-
* @var \Magento\Backend\Helper\Data
42+
* @var Data
3543
*/
3644
protected $_adminhtmlData = null;
3745

3846
/**
39-
* @var \Magento\Catalog\Model\CategoryFactory
47+
* @var CategoryFactory
4048
*/
4149
protected $_categoryFactory;
4250

4351
/**
44-
* @var \Magento\Catalog\Model\ProductFactory
52+
* @var ProductFactory
4553
*/
4654
protected $_productFactory;
4755

4856
/**
49-
* @var \Magento\Framework\Json\EncoderInterface
57+
* @var EncoderInterface
5058
*/
5159
protected $_jsonEncoder;
5260

@@ -56,24 +64,24 @@ class Tree extends \Magento\Catalog\Block\Adminhtml\Category\AbstractCategory
5664
protected $categoryRepository;
5765

5866
/**
59-
* @param \Magento\Backend\Block\Widget\Context $context
67+
* @param Context $context
6068
* @param \Magento\Catalog\Model\ResourceModel\Category\Tree $categoryTree
61-
* @param \Magento\Framework\Registry $registry
62-
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
63-
* @param \Magento\Catalog\Model\ProductFactory $productFactory
64-
* @param \Magento\Catalog\Model\CategoryFactory $categoryFactory
65-
* @param \Magento\Backend\Helper\Data $adminhtmlData
69+
* @param Registry $registry
70+
* @param EncoderInterface $jsonEncoder
71+
* @param ProductFactory $productFactory
72+
* @param CategoryFactory $categoryFactory
73+
* @param Data $adminhtmlData
6674
* @param CategoryRepositoryInterface $categoryRepository
6775
* @param array $data
6876
*/
6977
public function __construct(
70-
\Magento\Backend\Block\Widget\Context $context,
78+
Context $context,
7179
\Magento\Catalog\Model\ResourceModel\Category\Tree $categoryTree,
72-
\Magento\Framework\Registry $registry,
73-
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
74-
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
75-
\Magento\Catalog\Model\ProductFactory $productFactory,
76-
\Magento\Backend\Helper\Data $adminhtmlData,
80+
Registry $registry,
81+
CategoryFactory $categoryFactory,
82+
EncoderInterface $jsonEncoder,
83+
ProductFactory $productFactory,
84+
Data $adminhtmlData,
7785
CategoryRepositoryInterface $categoryRepository,
7886
array $data = []
7987
) {

app/code/Magento/UrlRewrite/view/adminhtml/templates/categories.phtml

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,26 @@
88

99
/** @var \Magento\UrlRewrite\Block\Catalog\Category\Tree $block */
1010
?>
11-
<fieldset class="admin__fieldset" data-ui-id="category-selector">
12-
<legend class="admin__legend"><span><?= $block->escapeHtml(__('Select Category')) ?></span></legend>
13-
<div class="content content-category-tree">
14-
<input type="hidden" name="categories" id="product_categories" value="" />
15-
<?php if ($block->getRoot()): ?>
16-
<div data-mage-init='<?php /* @noEscape */ echo $this->helper('Magento\Framework\Json\Helper\Data')->jsonEncode(
17-
[
18-
'categoryTree' => [
19-
'data' => $block->getTreeArray(),
20-
'url' => $block->escapeUrl($block->getLoadTreeUrl()),
21-
],
22-
]
23-
); ?>' class="jstree-default"></div>
24-
<?php endif; ?>
25-
</div>
26-
</fieldset>
11+
<fieldset class="admin__fieldset" data-ui-id="category-selector">
12+
<legend class="admin__legend"><span><?= $block->escapeHtml(__('Select Category')) ?></span></legend>
13+
<div class="content content-category-tree">
14+
<input type="hidden" name="categories" id="product_categories" value=""/>
15+
<?php if ($block->getRoot()): ?>
16+
<div class="jstree-default"></div>
17+
<?php endif; ?>
18+
</div>
19+
</fieldset>
20+
<?php if ($block->getRoot()): ?>
21+
<script type="text/x-magento-init">
22+
{
23+
".jstree-default": {
24+
"categoryTree": {
25+
"data": <?php /* @noEscape */
26+
echo $this->helper('Magento\Framework\Json\Helper\Data')->jsonEncode($block->getTreeArray()); ?>,
27+
"url": "<?php /* @noEscape */
28+
echo $block->escapeUrl($block->getLoadTreeUrl()); ?>"
29+
}
30+
}
31+
}
32+
</script>
33+
<?php endif; ?>
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();

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\UrlRewrite\Block\Catalog\Category;
78

89
/**
@@ -56,7 +57,27 @@ public function testGetTreeArrayApostropheReplaced()
5657
$tree = $this->_treeBlock->getTreeArray();
5758

5859
$this->assertNotContains('\'', $tree['children'][0]['children'][0]['children'][0]['name']);
59-
$this->assertEquals('&#039;Category 6&#039;', $tree['children'][0]['children'][0]['children'][0]['name']);
60+
$this->assertEquals(
61+
'&#039;Category 6&#039;',
62+
$tree['children'][0]['children'][0]['children'][0]['name']
63+
);
64+
}
65+
66+
/**
67+
* Test that the getTreeArray() method scrubs single quotes and apostrophes from names
68+
*
69+
* @magentoAppIsolation enabled
70+
* @magentoDataFixture Magento/Catalog/_files/catalog_category_with_doublequotes.php
71+
*/
72+
public function testGetTreeArrayDoubleQuotesReplaced()
73+
{
74+
$tree = $this->_treeBlock->getTreeArray();
75+
76+
$this->assertNotContains('\"', $tree['children'][0]['children'][0]['children'][0]['name']);
77+
$this->assertEquals(
78+
'&quot;Category 6&quot;',
79+
$tree['children'][0]['children'][0]['children'][0]['name']
80+
);
6081
}
6182

6283
/**

lib/web/mage/utils/misc.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,26 @@ define([
242242
return data;
243243
},
244244

245+
/**
246+
* Replaces symbol codes with their unescaped counterparts.
247+
*
248+
* @param {String} data
249+
*
250+
* @returns {String}
251+
*/
252+
unescape: function (data) {
253+
var unescaped = _.unescape(data),
254+
mapCharacter = {
255+
'&#039;': '\''
256+
};
257+
258+
_.each(mapCharacter, function (value, key) {
259+
unescaped = unescaped.replace(key, value);
260+
});
261+
262+
return unescaped;
263+
},
264+
245265
/**
246266
* Converts PHP IntlFormatter format to moment format.
247267
*

0 commit comments

Comments
 (0)