Skip to content

Commit a9c6cdc

Browse files
committed
MAGETWO-34180: Active item in Nivagation menu is not highlighted
- Merge remote-tracking branch 'south/MAGETWO-34180' into MAGETWO-34180 - Conflicts: - app/code/Magento/Catalog/Model/Observer.php - app/code/Magento/Catalog/Test/Unit/Model/Resource/Category/TreeTest.php - app/code/Magento/Theme/Test/Unit/Block/Html/TopmenuTest.php
2 parents 242e210 + 0e7d399 commit a9c6cdc

File tree

4 files changed

+93
-51
lines changed

4 files changed

+93
-51
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +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),
145-
'is_current_item' => $category->getIsCurrentItem()
144+
'has_active' => $this->_isActiveMenuCategory($category),
145+
'is_active' => $category->getIsCurrentItem()
146146
];
147147
$categoryNode = new \Magento\Framework\Data\Tree\Node($categoryData, 'id', $tree, $parentCategoryNode);
148148
$parentCategoryNode->addChild($categoryNode);

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

Lines changed: 82 additions & 36 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,44 +165,101 @@ 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

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(
207+
'Magento\Catalog\Model\Resource\Category\Tree',
208+
[
209+
'storeManager' => $this->getConfiguredStoreManager(),
210+
'resource' => $this->getConfiguredResource(),
211+
'eventManager' => $eventManager,
212+
'attributeConfig' => $this->getConfiguredAttributeConfig(),
213+
'collectionFactory' => $this->getConfiguredCollectionFactory()
214+
]
215+
);
216+
217+
$nodeMock = $this->getMock('\Magento\Framework\Data\Tree\Node', ['getId', 'getPath'], [], '', false);
218+
$nodeMock->expects($this->any())->method('getId')->will($this->returnValue(1));
219+
$nodeMock->expects($this->once())->method('getPath')->will($this->returnValue([]));
220+
221+
$model->addNode($nodeMock);
222+
223+
$this->assertSame($model, $model->addCollectionData(null, false, [], false, true));
224+
}
225+
226+
public function testAddCollectionDataWhenThereIsCurrentCategoryInRegistry()
227+
{
228+
$eventManager = $this->getMock('Magento\Framework\Event\ManagerInterface', [], [], '', false);
229+
185230
$category = $this->getMock('Magento\Catalog\Model\Category', [], [], '', false);
186231
$category
187-
->expects($this->exactly(2))
232+
->expects($this->atLeastOnce())
188233
->method('getId')
189234
->willReturn(12);
190-
$category
191-
->expects($this->once())
192-
->method('__call')
193-
->with('setIsCurrentCategory', [true]);
235+
194236
$registry = $this->getMock('Magento\Framework\Registry', [], [], '', false);
195237
$registry
196238
->expects($this->once())
197239
->method('registry')
198240
->with('current_category')
199241
->willReturn($category);
200242

201-
$model = $objectHelper->getObject(
243+
$model = $this->_objectHelper->getObject(
202244
'Magento\Catalog\Model\Resource\Category\Tree',
203245
[
204-
'storeManager' => $storeManager,
205-
'resource' => $resource,
246+
'storeManager' => $this->getConfiguredStoreManager(),
247+
'resource' => $this->getConfiguredResource(),
206248
'eventManager' => $eventManager,
207-
'attributeConfig' => $attributeConfig,
208-
'collectionFactory' => $collectionFactory,
249+
'attributeConfig' => $this->getConfiguredAttributeConfig(),
250+
'collectionFactory' => $this->getConfiguredCollectionFactory(),
209251
'registry' => $registry
210252
]
211253
);
212254

213-
$nodeMock = $this->getMock('\Magento\Framework\Data\Tree\Node', ['getId', 'getPath'], [], '', false);
214-
$nodeMock->expects($this->any())->method('getId')->will($this->returnValue(1));
255+
$nodeMock = $this->getMock('\Magento\Framework\Data\Tree\Node', ['getId', 'getPath', '__call'], [], '', false);
256+
$nodeMock->expects($this->any())->method('getId')->will($this->returnValue(12));
215257
$nodeMock->expects($this->once())->method('getPath')->will($this->returnValue([]));
216-
$nodeMock->setData('id', 12);
258+
$nodeMock
259+
->expects($this->once())
260+
->method('__call')
261+
->with('setIsCurrentItem', [true])
262+
->willReturn(true);
217263

218264
$model->addNode($nodeMock);
219265

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,10 @@ protected function _getMenuItemClasses(\Magento\Framework\Data\Tree\Node $item)
292292
$classes[] = 'first';
293293
}
294294

295-
if ($item->getIsActive() && !$item->getIsCurrentItem()) {
296-
$classes[] = 'has-active';
297-
}
298-
299-
if ($item->getIsCurrentItem()) {
295+
if ($item->getIsActive()) {
300296
$classes[] = 'active';
297+
} elseif ($item->getHasActive()) {
298+
$classes[] = 'has-active';
301299
}
302300

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

app/code/Magento/Theme/Test/Unit/Block/Html/TopmenuTest.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ class TopmenuTest extends \PHPUnit_Framework_TestCase
5353

5454
// @codingStandardsIgnoreEnd
5555

56-
public function prepare($isCurrentCategory = false)
56+
public function setUp()
5757
{
58+
$isCurrentItem = $this->getName() == 'testGetHtmlWithSelectedCategory' ? true : false;
5859
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
5960

6061
$this->context = $objectManager->getObject('Magento\Framework\View\Element\Template\Context');
@@ -88,8 +89,8 @@ public function prepare($isCurrentCategory = false)
8889
'name' => "Category $i",
8990
'id' => $id,
9091
'url' => "http://magento2/category-$i.html",
91-
'is_active' => $i == 0 ? $isCurrentCategory : false,
92-
'is_current_category' => $i == 0 ? $isCurrentCategory : false,
92+
'is_active' => $i == 0 ? $isCurrentItem : false,
93+
'is_current_item' => $i == 0 ? $isCurrentItem : false,
9394

9495
]
9596
);
@@ -123,21 +124,18 @@ public function prepare($isCurrentCategory = false)
123124
->willReturn($tree);
124125
}
125126

126-
public function getTopmenu()
127+
protected function getTopmenu()
127128
{
128129
return new Topmenu($this->context, $this->nodeFactory, $this->treeFactory);
129130
}
130131

131132
public function testGetHtmlWithoutSelectedCategory()
132133
{
133-
$this->prepare(false);
134134
$this->assertEquals($this->htmlWithoutCategory, $this->getTopmenu()->getHtml());
135135
}
136136

137137
public function testGetHtmlWithSelectedCategory()
138138
{
139-
$this->prepare(true);
140-
$result = $this->getTopmenu()->getHtml();
141-
$this->assertEquals($this->htmlWithCategory, $result);
139+
$this->assertEquals($this->htmlWithCategory, $this->getTopmenu()->getHtml());
142140
}
143141
}

0 commit comments

Comments
 (0)