Skip to content

Commit 1079153

Browse files
committed
MAGETWO-61628: [Backport] - Match products by rule in admin not working - for 2.0
1 parent d7ebdef commit 1079153

File tree

2 files changed

+52
-12
lines changed
  • app/code/Magento/Catalog

2 files changed

+52
-12
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Category/Save.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ public function __construct(
4949
$this->resultRawFactory = $resultRawFactory;
5050
$this->resultJsonFactory = $resultJsonFactory;
5151
$this->layoutFactory = $layoutFactory;
52-
53-
if ($this->storeManager == null){
54-
$this->storeManager = $this->_objectManager->get(StoreManagerInterface::class);
55-
}
5652
}
5753

5854
/**
@@ -92,8 +88,9 @@ public function execute()
9288
}
9389

9490
$storeId = $this->getRequest()->getParam('store');
95-
$store = $this->storeManager->getStore($storeId);
96-
$this->storeManager->setCurrentStore($store->getCode());
91+
$store = $this->getStoreManager()->getStore($storeId);
92+
$this->getStoreManager()->setCurrentStore($store->getCode());
93+
9794
$refreshTree = false;
9895
$data = $this->getRequest()->getPostValue();
9996
if ($data) {
@@ -102,9 +99,7 @@ public function execute()
10299
$parentId = $this->getRequest()->getParam('parent');
103100
if (!$parentId) {
104101
if ($storeId) {
105-
$parentId = $this->_objectManager->get(
106-
'Magento\Store\Model\StoreManagerInterface'
107-
)->getStore(
102+
$parentId = $this->getStoreManager()->getStore(
108103
$storeId
109104
)->getRootCategoryId();
110105
} else {
@@ -223,4 +218,18 @@ public function execute()
223218
$redirectParams
224219
);
225220
}
221+
222+
/**
223+
* Get StoreManager object
224+
*
225+
* @return StoreManagerInterface
226+
*/
227+
private function getStoreManager()
228+
{
229+
if ($this->storeManager == null) {
230+
$this->storeManager = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface');
231+
}
232+
233+
return $this->storeManager;
234+
}
226235
}

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Category/SaveTest.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ public function testExecute($categoryId, $storeId, $activeTabId, $parentId)
326326
false,
327327
true,
328328
true,
329-
['getStore', 'getRootCategoryId']
329+
['getStore', 'getRootCategoryId', 'setCurrentStore']
330330
);
331331
/**
332332
* @var \Magento\Framework\View\Layout
@@ -364,6 +364,20 @@ public function testExecute($categoryId, $storeId, $activeTabId, $parentId)
364364
false
365365
);
366366

367+
/**
368+
* @var \Magento\Store\Model\Store
369+
* |\PHPUnit_Framework_MockObject_MockObject $storeMock
370+
*/
371+
$storeMock = $this->getMock(
372+
\Magento\Store\Model\Store::class,
373+
[
374+
'getCode',
375+
],
376+
[],
377+
'',
378+
false
379+
);
380+
367381
$this->resultRedirectFactoryMock->expects($this->once())
368382
->method('create')
369383
->will($this->returnValue($resultRedirectMock));
@@ -379,6 +393,15 @@ public function testExecute($categoryId, $storeId, $activeTabId, $parentId)
379393
]
380394
)
381395
);
396+
397+
$storeMock->expects($this->once())
398+
->method('getCode')
399+
->will($this->returnValue('admin'));
400+
401+
$storeManagerMock->expects($this->once())
402+
->method('setCurrentStore')
403+
->with('admin');
404+
382405
$this->objectManagerMock->expects($this->atLeastOnce())
383406
->method('create')
384407
->will($this->returnValue($categoryMock));
@@ -441,15 +464,23 @@ public function testExecute($categoryId, $storeId, $activeTabId, $parentId)
441464

442465
if (!$parentId) {
443466
if ($storeId) {
444-
$storeManagerMock->expects($this->once())
467+
$storeManagerMock->expects($this->exactly(2))
445468
->method('getStore')
446469
->with($storeId)
447-
->will($this->returnSelf());
470+
->willReturnOnConsecutiveCalls(
471+
$storeMock,
472+
$this->returnSelf()
473+
);
448474
$storeManagerMock->expects($this->once())
449475
->method('getRootCategoryId')
450476
->will($this->returnValue($rootCategoryId));
451477
$parentId = $rootCategoryId;
452478
}
479+
} else {
480+
$storeManagerMock->expects($this->once())
481+
->method('getStore')
482+
->with($storeId)
483+
->will($this->returnValue($storeMock));
453484
}
454485
$categoryMock->expects($this->any())
455486
->method('load')

0 commit comments

Comments
 (0)