Skip to content

Commit b88784c

Browse files
committed
Merge remote-tracking branch 'south/MAGETWO-34180' into MAGETWO-34180
2 parents ac4dc25 + d914e6b commit b88784c

File tree

3 files changed

+52
-114
lines changed

3 files changed

+52
-114
lines changed

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

Lines changed: 23 additions & 7 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,12 +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-
'has_active' => $this->_isActiveMenuCategory($category),
145-
'is_active' => $category->getIsCurrentItem()
160+
'has_active' => $this->hasActive($category),
161+
'is_active' => $isActiveCategory
146162
];
147163
$categoryNode = new \Magento\Framework\Data\Tree\Node($categoryData, 'id', $tree, $parentCategoryNode);
148164
$parentCategoryNode->addChild($categoryNode);
@@ -163,7 +179,7 @@ protected function _addCategoriesToMenu($categories, $parentCategoryNode, $block
163179
* @param \Magento\Framework\Data\Tree\Node $category
164180
* @return bool
165181
*/
166-
protected function _isActiveMenuCategory($category)
182+
protected function hasActive($category)
167183
{
168184
if (!$this->_catalogLayer) {
169185
return false;

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

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

90-
/**
91-
* @var Registry
92-
*/
93-
protected $registry;
94-
9590
/**
9691
* Construct
9792
*
98-
* @param \Magento\Framework\Registry $registry
9993
* @param \Magento\Catalog\Model\Resource\Category $catalogCategory
10094
* @param \Magento\Framework\App\CacheInterface $cache
10195
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
10296
* @param \Magento\Framework\App\Resource $resource
10397
* @param \Magento\Framework\Event\ManagerInterface $eventManager
10498
* @param \Magento\Catalog\Model\Attribute\Config $attributeConfig
105-
* @param Collection\Factory $collectionFactory
106-
* @throws \Exception
99+
* @param \Magento\Catalog\Model\Resource\Category\Collection\Factory $collectionFactory
107100
*/
108101
public function __construct(
109-
\Magento\Framework\Registry $registry,
110102
\Magento\Catalog\Model\Resource\Category $catalogCategory,
111103
\Magento\Framework\App\CacheInterface $cache,
112104
\Magento\Store\Model\StoreManagerInterface $storeManager,
@@ -115,7 +107,6 @@ public function __construct(
115107
\Magento\Catalog\Model\Attribute\Config $attributeConfig,
116108
\Magento\Catalog\Model\Resource\Category\Collection\Factory $collectionFactory
117109
) {
118-
$this->registry = $registry;
119110
$this->_catalogCategory = $catalogCategory;
120111
$this->_cache = $cache;
121112
$this->_storeManager = $storeManager;
@@ -226,11 +217,6 @@ public function addCollectionData(
226217
}
227218
}
228219

229-
$category = $this->registry->registry('current_category');
230-
if ($category && $category->getId()) {
231-
$this->getNodeById($category->getId())->setIsCurrentItem(true);
232-
}
233-
234220
return $this;
235221
}
236222

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

Lines changed: 28 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,9 @@ 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-
3833
protected function setUp()
3934
{
40-
$this->_objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
35+
$objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
4136
$select = $this->getMock('Zend_Db_Select', [], [], '', false);
4237
$select->expects($this->once())->method('from')->with('catalog_category_entity');
4338
$connection = $this->getMock('Magento\Framework\DB\Adapter\AdapterInterface');
@@ -76,7 +71,7 @@ protected function setUp()
7671
'',
7772
false
7873
);
79-
$this->_model = $this->_objectHelper->getObject(
74+
$this->_model = $objectHelper->getObject(
8075
'Magento\Catalog\Model\Resource\Category\Tree',
8176
[
8277
'resource' => $this->_resource,
@@ -133,8 +128,9 @@ public function testCallCleaningDuringSetCollection()
133128
$this->assertEquals($model, $model->setCollection($this->getCollectionMock()));
134129
}
135130

136-
protected function getConfiguredResource()
131+
public function testAddCollectionData()
137132
{
133+
$objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
138134
$select = $this->getMock('Zend_Db_Select', [], [], '', false);
139135
$select->expects($this->any())->method('from')->will($this->returnSelf());
140136
$select->expects($this->any())->method('join')->will($this->returnSelf());
@@ -149,11 +145,26 @@ protected function getConfiguredResource()
149145
$resource->expects($this->any())->method('getConnection')->will($this->returnValue($connection));
150146
$resource->expects($this->any())->method('getTableName')->will($this->returnArgument(0));
151147

152-
return $resource;
153-
}
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+
);
154167

155-
protected function getConfiguredCollectionFactory()
156-
{
157168
$collection = $this->getMock('Magento\Catalog\Model\Resource\Category\Collection', [], [], '', false);
158169
$collection->expects($this->never())->method('getAllIds')->will($this->returnValue([]));
159170
$collectionFactory = $this->getMock(
@@ -165,52 +176,20 @@ protected function getConfiguredCollectionFactory()
165176
);
166177
$collectionFactory->expects($this->once())->method('create')->will($this->returnValue($collection));
167178

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

176182
$storeManager = $this->getMockForAbstractClass('Magento\Store\Model\StoreManagerInterface');
177183
$storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store));
178184

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(
185+
$model = $objectHelper->getObject(
207186
'Magento\Catalog\Model\Resource\Category\Tree',
208187
[
209-
'storeManager' => $this->getConfiguredStoreManager(),
210-
'resource' => $this->getConfiguredResource(),
188+
'storeManager' => $storeManager,
189+
'resource' => $resource,
211190
'eventManager' => $eventManager,
212-
'attributeConfig' => $this->getConfiguredAttributeConfig(),
213-
'collectionFactory' => $this->getConfiguredCollectionFactory()
191+
'attributeConfig' => $attributeConfig,
192+
'collectionFactory' => $collectionFactory
214193
]
215194
);
216195

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

223202
$this->assertSame($model, $model->addCollectionData(null, false, [], false, true));
224203
}
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-
}
268204
}

0 commit comments

Comments
 (0)