Skip to content

Commit 20aa5d6

Browse files
author
Natalia Momotenko
committed
Merge remote-tracking branch 'origin/MAGETWO-34180' into UI
2 parents e8afb50 + 62b462f commit 20aa5d6

File tree

7 files changed

+431
-55
lines changed

7 files changed

+431
-55
lines changed

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,23 @@ class Observer
6262
protected $_productResourceFactory;
6363

6464
/**
65-
* @param \Magento\Catalog\Model\Resource\Category $categoryResource
66-
* @param \Magento\Catalog\Model\Resource\Product $catalogProduct
65+
* @var \Magento\Framework\Registry
66+
*/
67+
protected $_registry;
68+
69+
/**
70+
* @param \Magento\Framework\Registry $registry
71+
* @param Resource\Category $categoryResource
72+
* @param Resource\Product $catalogProduct
6773
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
68-
* @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
74+
* @param Layer\Resolver $layerResolver
6975
* @param \Magento\Catalog\Helper\Category $catalogCategory
7076
* @param \Magento\Catalog\Helper\Data $catalogData
7177
* @param Indexer\Category\Flat\State $categoryFlatState
72-
* @param \Magento\Catalog\Model\Resource\ProductFactory $productResourceFactory
78+
* @param Resource\ProductFactory $productResourceFactory
7379
*/
7480
public function __construct(
81+
\Magento\Framework\Registry $registry,
7582
\Magento\Catalog\Model\Resource\Category $categoryResource,
7683
\Magento\Catalog\Model\Resource\Product $catalogProduct,
7784
\Magento\Store\Model\StoreManagerInterface $storeManager,
@@ -81,6 +88,7 @@ public function __construct(
8188
\Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState,
8289
\Magento\Catalog\Model\Resource\ProductFactory $productResourceFactory
8390
) {
91+
$this->_registry = $registry;
8492
$this->_categoryResource = $categoryResource;
8593
$this->_catalogProduct = $catalogProduct;
8694
$this->_storeManager = $storeManager;
@@ -137,11 +145,20 @@ protected function _addCategoriesToMenu($categories, $parentCategoryNode, $block
137145
$block->addIdentity(\Magento\Catalog\Model\Category::CACHE_TAG . '_' . $category->getId());
138146

139147
$tree = $parentCategoryNode->getTree();
148+
149+
$isActiveCategory = false;
150+
/** @var \Magento\Catalog\Model\Category $currentCategory */
151+
$currentCategory = $this->_registry->registry('current_category');
152+
if ($currentCategory && $currentCategory->getId() == $category->getId()) {
153+
$isActiveCategory = true;
154+
}
155+
140156
$categoryData = [
141157
'name' => $category->getName(),
142158
'id' => $nodeId,
143159
'url' => $this->_catalogCategory->getCategoryUrl($category),
144-
'is_active' => $this->_isActiveMenuCategory($category),
160+
'has_active' => $this->hasActive($category),
161+
'is_active' => $isActiveCategory
145162
];
146163
$categoryNode = new \Magento\Framework\Data\Tree\Node($categoryData, 'id', $tree, $parentCategoryNode);
147164
$parentCategoryNode->addChild($categoryNode);
@@ -162,7 +179,7 @@ protected function _addCategoriesToMenu($categories, $parentCategoryNode, $block
162179
* @param \Magento\Framework\Data\Tree\Node $category
163180
* @return bool
164181
*/
165-
protected function _isActiveMenuCategory($category)
182+
protected function hasActive($category)
166183
{
167184
if (!$this->_catalogLayer) {
168185
return false;

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

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

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

1114
/**
1215
* Html page top menu block
@@ -28,13 +31,32 @@ class Topmenu extends Template implements IdentityInterface
2831
protected $_menu;
2932

3033
/**
31-
* Init top menu tree structure
34+
* Core registry
3235
*
33-
* @return void
36+
* @var Registry
3437
*/
35-
public function _construct()
36-
{
37-
$this->_menu = new \Magento\Framework\Data\Tree\Node([], 'root', new \Magento\Framework\Data\Tree());
38+
protected $registry;
39+
40+
/**
41+
* @param Template\Context $context
42+
* @param NodeFactory $nodeFactory
43+
* @param TreeFactory $treeFactory
44+
* @param array $data
45+
*/
46+
public function __construct(
47+
Template\Context $context,
48+
NodeFactory $nodeFactory,
49+
TreeFactory $treeFactory,
50+
array $data = []
51+
) {
52+
parent::__construct($context, $data);
53+
$this->_menu = $nodeFactory->create(
54+
[
55+
'data' => [],
56+
'idField' => 'root',
57+
'tree' => $treeFactory->create()
58+
]
59+
);
3860
}
3961

4062
/**
@@ -207,13 +229,13 @@ protected function _getHtml(
207229

208230
$html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
209231
$html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>' . $this->escapeHtml(
210-
$child->getName()
211-
) . '</span></a>' . $this->_addSubMenu(
212-
$child,
213-
$childLevel,
214-
$childrenWrapClass,
215-
$limit
216-
) . '</li>';
232+
$child->getName()
233+
) . '</span></a>' . $this->_addSubMenu(
234+
$child,
235+
$childLevel,
236+
$childrenWrapClass,
237+
$limit
238+
) . '</li>';
217239
$itemPosition++;
218240
$counter++;
219241
}
@@ -272,6 +294,8 @@ protected function _getMenuItemClasses(\Magento\Framework\Data\Tree\Node $item)
272294

273295
if ($item->getIsActive()) {
274296
$classes[] = 'active';
297+
} elseif ($item->getHasActive()) {
298+
$classes[] = 'has-active';
275299
}
276300

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

0 commit comments

Comments
 (0)