Skip to content

Commit 242e210

Browse files
committed
MAGETWO-34180: Active item in Nivagation menu is not highlighted
1 parent 092594b commit 242e210

File tree

7 files changed

+199
-55
lines changed

7 files changed

+199
-55
lines changed

app/code/Magento/Catalog/Model/Observer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ protected function _addCategoriesToMenu($categories, $parentCategoryNode, $block
142142
'id' => $nodeId,
143143
'url' => $this->_catalogCategory->getCategoryUrl($category),
144144
'is_active' => $this->_isActiveMenuCategory($category),
145-
'is_current_category' => $category->getIsCurrentCategory()
145+
'is_current_item' => $category->getIsCurrentItem()
146146
];
147147
$categoryNode = new \Magento\Framework\Data\Tree\Node($categoryData, 'id', $tree, $parentCategoryNode);
148148
$parentCategoryNode->addChild($categoryNode);

app/code/Magento/Catalog/Model/Resource/Category/Tree.php

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@
55
*/
66
namespace Magento\Catalog\Model\Resource\Category;
77

8-
use Magento\Framework\Registry;
9-
use Magento\Catalog\Model\Resource\Category;
10-
use Magento\Framework\App\CacheInterface;
11-
use Magento\Store\Model\StoreManagerInterface;
12-
use Magento\Framework\App\Resource;
13-
use Magento\Framework\Event\ManagerInterface;
14-
use Magento\Catalog\Model\Attribute\Config;
15-
use Magento\Catalog\Model\Resource\Category\Collection\Factory;
16-
178
/**
189
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1910
*/
@@ -104,25 +95,25 @@ class Tree extends \Magento\Framework\Data\Tree\Dbp
10495
/**
10596
* Construct
10697
*
107-
* @param Registry $registry
108-
* @param Category $catalogCategory
109-
* @param CacheInterface $cache
110-
* @param StoreManagerInterface $storeManager
111-
* @param Resource $resource
112-
* @param ManagerInterface $eventManager
113-
* @param Config $attributeConfig
114-
* @param Factory $collectionFactory
98+
* @param \Magento\Framework\Registry $registry
99+
* @param \Magento\Catalog\Model\Resource\Category $catalogCategory
100+
* @param \Magento\Framework\App\CacheInterface $cache
101+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
102+
* @param \Magento\Framework\App\Resource $resource
103+
* @param \Magento\Framework\Event\ManagerInterface $eventManager
104+
* @param \Magento\Catalog\Model\Attribute\Config $attributeConfig
105+
* @param Collection\Factory $collectionFactory
115106
* @throws \Exception
116107
*/
117108
public function __construct(
118-
Registry $registry,
119-
Category $catalogCategory,
120-
CacheInterface $cache,
121-
StoreManagerInterface $storeManager,
122-
Resource $resource,
123-
ManagerInterface $eventManager,
124-
Config $attributeConfig,
125-
Factory $collectionFactory
109+
\Magento\Framework\Registry $registry,
110+
\Magento\Catalog\Model\Resource\Category $catalogCategory,
111+
\Magento\Framework\App\CacheInterface $cache,
112+
\Magento\Store\Model\StoreManagerInterface $storeManager,
113+
\Magento\Framework\App\Resource $resource,
114+
\Magento\Framework\Event\ManagerInterface $eventManager,
115+
\Magento\Catalog\Model\Attribute\Config $attributeConfig,
116+
\Magento\Catalog\Model\Resource\Category\Collection\Factory $collectionFactory
126117
) {
127118
$this->registry = $registry;
128119
$this->_catalogCategory = $catalogCategory;
@@ -235,20 +226,12 @@ public function addCollectionData(
235226
}
236227
}
237228

238-
$this->markNodeAsCurrentCategory();
239-
240-
return $this;
241-
}
242-
243-
/**
244-
* @return void
245-
*/
246-
protected function markNodeAsCurrentCategory()
247-
{
248229
$category = $this->registry->registry('current_category');
249230
if ($category && $category->getId()) {
250-
$this->getNodeById($category->getId())->setIsCurrentCategory(true);
231+
$this->getNodeById($category->getId())->setIsCurrentItem(true);
251232
}
233+
234+
return $this;
252235
}
253236

254237
/**

app/code/Magento/Catalog/Test/Unit/Model/Resource/Category/TreeTest.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,12 @@ public function testAddCollectionData()
158158
$attributeConfig->expects(
159159
$this->once()
160160
)->method(
161-
'getAttributeNames'
162-
)->with(
163-
'catalog_category'
164-
)->will(
165-
$this->returnValue($attributes)
166-
);
161+
'getAttributeNames'
162+
)->with(
163+
'catalog_category'
164+
)->will(
165+
$this->returnValue($attributes)
166+
);
167167

168168
$collection = $this->getMock('Magento\Catalog\Model\Resource\Category\Collection', [], [], '', false);
169169
$collection->expects($this->never())->method('getAllIds')->will($this->returnValue([]));
@@ -182,20 +182,38 @@ public function testAddCollectionData()
182182
$storeManager = $this->getMockForAbstractClass('Magento\Store\Model\StoreManagerInterface');
183183
$storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store));
184184

185+
$category = $this->getMock('Magento\Catalog\Model\Category', [], [], '', false);
186+
$category
187+
->expects($this->exactly(2))
188+
->method('getId')
189+
->willReturn(12);
190+
$category
191+
->expects($this->once())
192+
->method('__call')
193+
->with('setIsCurrentCategory', [true]);
194+
$registry = $this->getMock('Magento\Framework\Registry', [], [], '', false);
195+
$registry
196+
->expects($this->once())
197+
->method('registry')
198+
->with('current_category')
199+
->willReturn($category);
200+
185201
$model = $objectHelper->getObject(
186202
'Magento\Catalog\Model\Resource\Category\Tree',
187203
[
188204
'storeManager' => $storeManager,
189205
'resource' => $resource,
190206
'eventManager' => $eventManager,
191207
'attributeConfig' => $attributeConfig,
192-
'collectionFactory' => $collectionFactory
208+
'collectionFactory' => $collectionFactory,
209+
'registry' => $registry
193210
]
194211
);
195212

196213
$nodeMock = $this->getMock('\Magento\Framework\Data\Tree\Node', ['getId', 'getPath'], [], '', false);
197214
$nodeMock->expects($this->any())->method('getId')->will($this->returnValue(1));
198215
$nodeMock->expects($this->once())->method('getPath')->will($this->returnValue([]));
216+
$nodeMock->setData('id', 12);
199217

200218
$model->addNode($nodeMock);
201219

app/code/Magento/Theme/Block/Html/Topmenu.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Topmenu extends Template implements IdentityInterface
2828
*
2929
* @var \Magento\Framework\Data\Tree\Node
3030
*/
31-
protected $menu;
31+
protected $_menu;
3232

3333
/**
3434
* Core registry
@@ -50,7 +50,7 @@ public function __construct(
5050
array $data = []
5151
) {
5252
parent::__construct($context, $data);
53-
$this->menu = $nodeFactory->create(
53+
$this->_menu = $nodeFactory->create(
5454
[
5555
'data' => [],
5656
'idField' => 'root',
@@ -71,18 +71,18 @@ public function getHtml($outermostClass = '', $childrenWrapClass = '', $limit =
7171
{
7272
$this->_eventManager->dispatch(
7373
'page_block_html_topmenu_gethtml_before',
74-
['menu' => $this->menu, 'block' => $this]
74+
['menu' => $this->_menu, 'block' => $this]
7575
);
7676

77-
$this->menu->setOutermostClass($outermostClass);
78-
$this->menu->setChildrenWrapClass($childrenWrapClass);
77+
$this->_menu->setOutermostClass($outermostClass);
78+
$this->_menu->setChildrenWrapClass($childrenWrapClass);
7979

80-
$html = $this->_getHtml($this->menu, $childrenWrapClass, $limit);
80+
$html = $this->_getHtml($this->_menu, $childrenWrapClass, $limit);
8181

8282
$transportObject = new \Magento\Framework\Object(['html' => $html]);
8383
$this->_eventManager->dispatch(
8484
'page_block_html_topmenu_gethtml_after',
85-
['menu' => $this->menu, 'transportObject' => $transportObject]
85+
['menu' => $this->_menu, 'transportObject' => $transportObject]
8686
);
8787
$html = $transportObject->getHtml();
8888

@@ -292,11 +292,11 @@ protected function _getMenuItemClasses(\Magento\Framework\Data\Tree\Node $item)
292292
$classes[] = 'first';
293293
}
294294

295-
if ($item->getIsActive() && !$item->getIsCurrentCategory()) {
295+
if ($item->getIsActive() && !$item->getIsCurrentItem()) {
296296
$classes[] = 'has-active';
297297
}
298298

299-
if ($item->getIsCurrentCategory()) {
299+
if ($item->getIsCurrentItem()) {
300300
$classes[] = 'active';
301301
}
302302

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Theme\Test\Unit\Block\Html;
8+
9+
use Magento\Theme\Block\Html\Topmenu;
10+
use Magento\Framework\Registry;
11+
use Magento\Framework\View\Element\Template\Context;
12+
use Magento\Framework\Data\TreeFactory;
13+
use Magento\Framework\Data\Tree\NodeFactory;
14+
15+
class TopmenuTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* @var Registry|\PHPUnit_Framework_MockObject_MockObject
19+
*/
20+
protected $registry;
21+
22+
/**
23+
* @var Context|\PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
protected $context;
26+
27+
/**
28+
* @var NodeFactory|\PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
protected $nodeFactory;
31+
32+
/**
33+
* @var TreeFactory|\PHPUnit_Framework_MockObject_MockObject
34+
*/
35+
protected $treeFactory;
36+
37+
/**
38+
* @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject
39+
*/
40+
protected $category;
41+
42+
// @codingStandardsIgnoreStart
43+
44+
/** @var string */
45+
protected $htmlWithoutCategory = <<<HTML
46+
<li class="level0 nav-1 first"><a href="http://magento2/category-0.html" ><span></span></a></li><li class="level0 nav-2"><a href="http://magento2/category-1.html" ><span></span></a></li><li class="level0 nav-3"><a href="http://magento2/category-2.html" ><span></span></a></li><li class="level0 nav-4"><a href="http://magento2/category-3.html" ><span></span></a></li><li class="level0 nav-5"><a href="http://magento2/category-4.html" ><span></span></a></li><li class="level0 nav-6"><a href="http://magento2/category-5.html" ><span></span></a></li><li class="level0 nav-7"><a href="http://magento2/category-6.html" ><span></span></a></li><li class="level0 nav-8"><a href="http://magento2/category-7.html" ><span></span></a></li><li class="level0 nav-9"><a href="http://magento2/category-8.html" ><span></span></a></li><li class="level0 nav-10 last"><a href="http://magento2/category-9.html" ><span></span></a></li>
47+
HTML;
48+
49+
/** @var string */
50+
protected $htmlWithCategory = <<<HTML
51+
<li class="level0 nav-1 first active"><a href="http://magento2/category-0.html" ><span></span></a></li><li class="level0 nav-2"><a href="http://magento2/category-1.html" ><span></span></a></li><li class="level0 nav-3"><a href="http://magento2/category-2.html" ><span></span></a></li><li class="level0 nav-4"><a href="http://magento2/category-3.html" ><span></span></a></li><li class="level0 nav-5"><a href="http://magento2/category-4.html" ><span></span></a></li><li class="level0 nav-6"><a href="http://magento2/category-5.html" ><span></span></a></li><li class="level0 nav-7"><a href="http://magento2/category-6.html" ><span></span></a></li><li class="level0 nav-8"><a href="http://magento2/category-7.html" ><span></span></a></li><li class="level0 nav-9"><a href="http://magento2/category-8.html" ><span></span></a></li><li class="level0 nav-10 last"><a href="http://magento2/category-9.html" ><span></span></a></li>
52+
HTML;
53+
54+
// @codingStandardsIgnoreEnd
55+
56+
public function prepare($isCurrentCategory = false)
57+
{
58+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
59+
60+
$this->context = $objectManager->getObject('Magento\Framework\View\Element\Template\Context');
61+
62+
$this->nodeFactory = $this->getMock('Magento\Framework\Data\Tree\NodeFactory', [], [], '', false);
63+
$this->treeFactory = $this->getMock('Magento\Framework\Data\TreeFactory', [], [], '', false);
64+
65+
$tree = $this->getMock('Magento\Framework\Data\Tree', [], [], '', false);
66+
67+
$container = $this->getMock('Magento\Catalog\Model\Resource\Category\Tree', [], [], '', false);
68+
69+
$children = $this->getMock(
70+
'Magento\Framework\Data\Tree\Node\Collection',
71+
['count'],
72+
['container' => $container]
73+
);
74+
75+
for ($i = 0; $i < 10; $i++) {
76+
$id = "category-node-$i";
77+
$categoryNode = $this->getMock('Magento\Framework\Data\Tree\Node', ['getId', 'hasChildren'], [], '', false);
78+
$categoryNode
79+
->expects($this->once())
80+
->method('getId')
81+
->willReturn($id);
82+
$categoryNode
83+
->expects($this->atLeastOnce())
84+
->method('hasChildren')
85+
->willReturn(false);
86+
$categoryNode->setData(
87+
[
88+
'name' => "Category $i",
89+
'id' => $id,
90+
'url' => "http://magento2/category-$i.html",
91+
'is_active' => $i == 0 ? $isCurrentCategory : false,
92+
'is_current_category' => $i == 0 ? $isCurrentCategory : false,
93+
94+
]
95+
);
96+
$children->add($categoryNode);
97+
}
98+
99+
$children
100+
->expects($this->once())
101+
->method('count')
102+
->willReturn(10);
103+
104+
$node = $this->getMock('Magento\Framework\Data\Tree\Node', ['getChildren'], [], '', false);
105+
$node
106+
->expects($this->once())
107+
->method('getChildren')
108+
->willReturn($children);
109+
$node
110+
->expects($this->any())
111+
->method('__call')
112+
->with('getLevel', [])
113+
->willReturn(null);
114+
115+
$this->nodeFactory
116+
->expects($this->once())
117+
->method('create')
118+
->willReturn($node);
119+
120+
$this->treeFactory
121+
->expects($this->once())
122+
->method('create')
123+
->willReturn($tree);
124+
}
125+
126+
public function getTopmenu()
127+
{
128+
return new Topmenu($this->context, $this->nodeFactory, $this->treeFactory);
129+
}
130+
131+
public function testGetHtmlWithoutSelectedCategory()
132+
{
133+
$this->prepare(false);
134+
$this->assertEquals($this->htmlWithoutCategory, $this->getTopmenu()->getHtml());
135+
}
136+
137+
public function testGetHtmlWithSelectedCategory()
138+
{
139+
$this->prepare(true);
140+
$result = $this->getTopmenu()->getHtml();
141+
$this->assertEquals($this->htmlWithCategory, $result);
142+
}
143+
}

lib/internal/Magento/Framework/Data/Tree/NodeFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class NodeFactory
3535
*/
3636
public function __construct(
3737
ObjectManagerInterface $objectManager,
38-
$instanceName = '\\Magento\\Framework\\Data\\Tree\\Node'
38+
$instanceName = '\Magento\Framework\Data\Tree\Node'
3939
) {
4040
$this->objectManager = $objectManager;
4141
$this->instanceName = $instanceName;

lib/internal/Magento/Framework/Data/TreeFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class TreeFactory
3535
*/
3636
public function __construct(
3737
ObjectManagerInterface $objectManager,
38-
$instanceName = '\\Magento\\Framework\\Data\\Tree'
38+
$instanceName = '\Magento\Framework\Data\Tree'
3939
) {
4040
$this->objectManager = $objectManager;
4141
$this->instanceName = $instanceName;

0 commit comments

Comments
 (0)