Skip to content

Commit 0e7d399

Browse files
author
okarpenko
committed
MAGETWO-34180: Active item in Nivagation menu is not highlighted
1 parent 779e8d2 commit 0e7d399

File tree

5 files changed

+267
-51
lines changed

5 files changed

+267
-51
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ protected function _addCategoriesToMenu($categories, $parentCategoryNode, $block
141141
'name' => $category->getName(),
142142
'id' => $nodeId,
143143
'url' => $this->_catalogCategory->getCategoryUrl($category),
144-
'is_active' => $this->_isActiveMenuCategory($category),
144+
'has_active' => $this->_isActiveMenuCategory($category),
145+
'is_active' => $category->getIsCurrentItem()
145146
];
146147
$categoryNode = new \Magento\Framework\Data\Tree\Node($categoryData, 'id', $tree, $parentCategoryNode);
147148
$parentCategoryNode->addChild($categoryNode);

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,26 @@ class Tree extends \Magento\Framework\Data\Tree\Dbp
8787
*/
8888
protected $_catalogCategory;
8989

90+
/**
91+
* @var Registry
92+
*/
93+
protected $registry;
94+
9095
/**
9196
* Construct
9297
*
98+
* @param \Magento\Framework\Registry $registry
9399
* @param \Magento\Catalog\Model\Resource\Category $catalogCategory
94100
* @param \Magento\Framework\App\CacheInterface $cache
95101
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
96102
* @param \Magento\Framework\App\Resource $resource
97103
* @param \Magento\Framework\Event\ManagerInterface $eventManager
98104
* @param \Magento\Catalog\Model\Attribute\Config $attributeConfig
99-
* @param \Magento\Catalog\Model\Resource\Category\Collection\Factory $collectionFactory
105+
* @param Collection\Factory $collectionFactory
106+
* @throws \Exception
100107
*/
101108
public function __construct(
109+
\Magento\Framework\Registry $registry,
102110
\Magento\Catalog\Model\Resource\Category $catalogCategory,
103111
\Magento\Framework\App\CacheInterface $cache,
104112
\Magento\Store\Model\StoreManagerInterface $storeManager,
@@ -107,6 +115,7 @@ public function __construct(
107115
\Magento\Catalog\Model\Attribute\Config $attributeConfig,
108116
\Magento\Catalog\Model\Resource\Category\Collection\Factory $collectionFactory
109117
) {
118+
$this->registry = $registry;
110119
$this->_catalogCategory = $catalogCategory;
111120
$this->_cache = $cache;
112121
$this->_storeManager = $storeManager;
@@ -217,6 +226,11 @@ public function addCollectionData(
217226
}
218227
}
219228

229+
$category = $this->registry->registry('current_category');
230+
if ($category && $category->getId()) {
231+
$this->getNodeById($category->getId())->setIsCurrentItem(true);
232+
}
233+
220234
return $this;
221235
}
222236

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

Lines changed: 92 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,14 @@ class TreeTest extends \PHPUnit_Framework_TestCase
3030
*/
3131
protected $_collectionFactory;
3232

33+
/**
34+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager|\PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
protected $_objectHelper;
37+
3338
protected function setUp()
3439
{
35-
$objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
40+
$this->_objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
3641
$select = $this->getMock('Zend_Db_Select', [], [], '', false);
3742
$select->expects($this->once())->method('from')->with('catalog_category_entity');
3843
$connection = $this->getMock('Magento\Framework\DB\Adapter\AdapterInterface');
@@ -71,7 +76,7 @@ protected function setUp()
7176
'',
7277
false
7378
);
74-
$this->_model = $objectHelper->getObject(
79+
$this->_model = $this->_objectHelper->getObject(
7580
'Magento\Catalog\Model\Resource\Category\Tree',
7681
[
7782
'resource' => $this->_resource,
@@ -128,9 +133,8 @@ public function testCallCleaningDuringSetCollection()
128133
$this->assertEquals($model, $model->setCollection($this->getCollectionMock()));
129134
}
130135

131-
public function testAddCollectionData()
136+
protected function getConfiguredResource()
132137
{
133-
$objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
134138
$select = $this->getMock('Zend_Db_Select', [], [], '', false);
135139
$select->expects($this->any())->method('from')->will($this->returnSelf());
136140
$select->expects($this->any())->method('join')->will($this->returnSelf());
@@ -145,26 +149,11 @@ public function testAddCollectionData()
145149
$resource->expects($this->any())->method('getConnection')->will($this->returnValue($connection));
146150
$resource->expects($this->any())->method('getTableName')->will($this->returnArgument(0));
147151

148-
$eventManager = $this->getMock('Magento\Framework\Event\ManagerInterface', [], [], '', false);
149-
$attributeConfig = $this->getMock(
150-
'Magento\Catalog\Model\Attribute\Config',
151-
[],
152-
[],
153-
'',
154-
false
155-
);
156-
157-
$attributes = ['attribute_one', 'attribute_two'];
158-
$attributeConfig->expects(
159-
$this->once()
160-
)->method(
161-
'getAttributeNames'
162-
)->with(
163-
'catalog_category'
164-
)->will(
165-
$this->returnValue($attributes)
166-
);
152+
return $resource;
153+
}
167154

155+
protected function getConfiguredCollectionFactory()
156+
{
168157
$collection = $this->getMock('Magento\Catalog\Model\Resource\Category\Collection', [], [], '', false);
169158
$collection->expects($this->never())->method('getAllIds')->will($this->returnValue([]));
170159
$collectionFactory = $this->getMock(
@@ -176,20 +165,52 @@ public function testAddCollectionData()
176165
);
177166
$collectionFactory->expects($this->once())->method('create')->will($this->returnValue($collection));
178167

168+
return $collectionFactory;
169+
}
170+
171+
protected function getConfiguredStoreManager()
172+
{
179173
$store = $this->getMock('Magento\Store\Model\Store', [], [], '', false);
180174
$store->expects($this->any())->method('getId')->will($this->returnValue(1));
181175

182176
$storeManager = $this->getMockForAbstractClass('Magento\Store\Model\StoreManagerInterface');
183177
$storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store));
184178

185-
$model = $objectHelper->getObject(
179+
return $storeManager;
180+
}
181+
182+
protected function getConfiguredAttributeConfig()
183+
{
184+
$attributeConfig = $this->getMock(
185+
'Magento\Catalog\Model\Attribute\Config',
186+
[],
187+
[],
188+
'',
189+
false
190+
);
191+
192+
$attributes = ['attribute_one', 'attribute_two'];
193+
$attributeConfig
194+
->expects($this->once())
195+
->method('getAttributeNames')
196+
->with('catalog_category')
197+
->willReturn($attributes);
198+
199+
return $attributeConfig;
200+
}
201+
202+
public function testAddCollectionData()
203+
{
204+
$eventManager = $this->getMock('Magento\Framework\Event\ManagerInterface', [], [], '', false);
205+
206+
$model = $this->_objectHelper->getObject(
186207
'Magento\Catalog\Model\Resource\Category\Tree',
187208
[
188-
'storeManager' => $storeManager,
189-
'resource' => $resource,
209+
'storeManager' => $this->getConfiguredStoreManager(),
210+
'resource' => $this->getConfiguredResource(),
190211
'eventManager' => $eventManager,
191-
'attributeConfig' => $attributeConfig,
192-
'collectionFactory' => $collectionFactory
212+
'attributeConfig' => $this->getConfiguredAttributeConfig(),
213+
'collectionFactory' => $this->getConfiguredCollectionFactory()
193214
]
194215
);
195216

@@ -201,4 +222,47 @@ public function testAddCollectionData()
201222

202223
$this->assertSame($model, $model->addCollectionData(null, false, [], false, true));
203224
}
225+
226+
public function testAddCollectionDataWhenThereIsCurrentCategoryInRegistry()
227+
{
228+
$eventManager = $this->getMock('Magento\Framework\Event\ManagerInterface', [], [], '', false);
229+
230+
$category = $this->getMock('Magento\Catalog\Model\Category', [], [], '', false);
231+
$category
232+
->expects($this->atLeastOnce())
233+
->method('getId')
234+
->willReturn(12);
235+
236+
$registry = $this->getMock('Magento\Framework\Registry', [], [], '', false);
237+
$registry
238+
->expects($this->once())
239+
->method('registry')
240+
->with('current_category')
241+
->willReturn($category);
242+
243+
$model = $this->_objectHelper->getObject(
244+
'Magento\Catalog\Model\Resource\Category\Tree',
245+
[
246+
'storeManager' => $this->getConfiguredStoreManager(),
247+
'resource' => $this->getConfiguredResource(),
248+
'eventManager' => $eventManager,
249+
'attributeConfig' => $this->getConfiguredAttributeConfig(),
250+
'collectionFactory' => $this->getConfiguredCollectionFactory(),
251+
'registry' => $registry
252+
]
253+
);
254+
255+
$nodeMock = $this->getMock('\Magento\Framework\Data\Tree\Node', ['getId', 'getPath', '__call'], [], '', false);
256+
$nodeMock->expects($this->any())->method('getId')->will($this->returnValue(12));
257+
$nodeMock->expects($this->once())->method('getPath')->will($this->returnValue([]));
258+
$nodeMock
259+
->expects($this->once())
260+
->method('__call')
261+
->with('setIsCurrentItem', [true])
262+
->willReturn(true);
263+
264+
$model->addNode($nodeMock);
265+
266+
$this->assertSame($model, $model->addCollectionData(null, false, [], false, true));
267+
}
204268
}

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

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
use Magento\Framework\View\Block\IdentityInterface;
99
use Magento\Framework\View\Element\Template;
10-
use Magento\Framework\Registry;
10+
use Magento\Framework\Data\TreeFactory;
11+
use Magento\Framework\Data\Tree\Node;
12+
use Magento\Framework\Data\Tree\NodeFactory;
1113

1214
/**
1315
* Html page top menu block
@@ -36,27 +38,25 @@ class Topmenu extends Template implements IdentityInterface
3638
protected $registry;
3739

3840
/**
39-
* @param Registry $registry
4041
* @param Template\Context $context
42+
* @param NodeFactory $nodeFactory
43+
* @param TreeFactory $treeFactory
4144
* @param array $data
4245
*/
4346
public function __construct(
44-
Registry $registry,
4547
Template\Context $context,
48+
NodeFactory $nodeFactory,
49+
TreeFactory $treeFactory,
4650
array $data = []
4751
) {
48-
$this->registry = $registry;
4952
parent::__construct($context, $data);
50-
}
51-
52-
/**
53-
* Init top menu tree structure
54-
*
55-
* @return void
56-
*/
57-
public function _construct()
58-
{
59-
$this->_menu = new \Magento\Framework\Data\Tree\Node([], 'root', new \Magento\Framework\Data\Tree());
53+
$this->_menu = $nodeFactory->create(
54+
[
55+
'data' => [],
56+
'idField' => 'root',
57+
'tree' => $treeFactory->create()
58+
]
59+
);
6060
}
6161

6262
/**
@@ -288,18 +288,14 @@ protected function _getMenuItemClasses(\Magento\Framework\Data\Tree\Node $item)
288288
$classes[] = 'level' . $item->getLevel();
289289
$classes[] = $item->getPositionClass();
290290

291-
$currentCategoryName = $this->registry->registry('current_category')->getName();
292-
293291
if ($item->getIsFirst()) {
294292
$classes[] = 'first';
295293
}
296294

297-
if ($item->getIsActive() && $currentCategoryName != $item->getName()) {
298-
$classes[] = 'has-active';
299-
}
300-
301-
if ($currentCategoryName == $item->getName()) {
295+
if ($item->getIsActive()) {
302296
$classes[] = 'active';
297+
} elseif ($item->getHasActive()) {
298+
$classes[] = 'has-active';
303299
}
304300

305301
if ($item->getIsLast()) {

0 commit comments

Comments
 (0)