Skip to content

Commit f826a37

Browse files
author
Michail Slabko
committed
MAGETWO-31723: Delete category message is absent after category has been deleted and a page has been loaded
1 parent 8f6bb18 commit f826a37

File tree

3 files changed

+163
-46
lines changed
  • app/code/Magento/Catalog/Controller/Adminhtml/Category
  • dev/tests
    • integration/testsuite/Magento/Catalog/Controller/Adminhtml/Category
    • unit/testsuite/Magento/Catalog/Controller/Adminhtml/Category

3 files changed

+163
-46
lines changed

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@
77

88
class Delete extends \Magento\Catalog\Controller\Adminhtml\Category
99
{
10+
/** @var \Magento\Catalog\Api\CategoryRepositoryInterface */
11+
protected $categoryRepository;
12+
13+
/**
14+
* @param \Magento\Backend\App\Action\Context $context
15+
* @param \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory
16+
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
17+
*/
18+
public function __construct(
19+
\Magento\Backend\App\Action\Context $context,
20+
\Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory,
21+
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
22+
) {
23+
parent::__construct($context, $resultRedirectFactory);
24+
$this->categoryRepository = $categoryRepository;
25+
}
26+
1027
/**
1128
* Delete category action
1229
*
@@ -21,11 +38,11 @@ public function execute()
2138
$parentId = null;
2239
if ($categoryId) {
2340
try {
24-
$category = $this->_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
41+
$category = $this->categoryRepository->get($categoryId);
2542
$parentId = $category->getParentId();
2643
$this->_eventManager->dispatch('catalog_controller_category_delete', ['category' => $category]);
27-
$this->_objectManager->get('Magento\Backend\Model\Auth\Session')->setDeletedPath($category->getPath());
28-
$category->delete();
44+
$this->_auth->getAuthStorage()->setDeletedPath($category->getPath());
45+
$this->categoryRepository->delete($category);
2946
$this->messageManager->addSuccess(__('You deleted the category.'));
3047
} catch (\Magento\Framework\Model\Exception $e) {
3148
$this->messageManager->addError($e->getMessage());

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Category/DeleteTest.php

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
4+
*/
5+
6+
namespace Magento\Catalog\Controller\Adminhtml\Category;
7+
8+
use Magento\TestFramework\Helper\ObjectManager as ObjectManagerHelper;
9+
10+
class DeleteTest extends \PHPUnit_Framework_TestCase
11+
{
12+
/** @var \Magento\Catalog\Controller\Adminhtml\Category\Delete */
13+
protected $unit;
14+
15+
/** @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject */
16+
protected $resultRedirect;
17+
18+
/** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
19+
protected $request;
20+
21+
/** @var \Magento\Catalog\Api\CategoryRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
22+
protected $categoryRepository;
23+
24+
/** @var \Magento\Backend\Model\Auth\StorageInterface|\PHPUnit_Framework_MockObject_MockObject */
25+
protected $authStorage;
26+
27+
protected function setUp()
28+
{
29+
$context = $this->getMock('Magento\Backend\App\Action\Context', [], [], '', false);
30+
$resultRedirectFactory = $this->getMock(
31+
'Magento\Backend\Model\View\Result\RedirectFactory',
32+
['create'],
33+
[],
34+
'',
35+
false
36+
);
37+
$this->request = $this->getMockForAbstractClass(
38+
'Magento\Framework\App\RequestInterface',
39+
[],
40+
'',
41+
false,
42+
true,
43+
true,
44+
['getParam', 'getPost']
45+
);
46+
$auth = $this->getMock(
47+
'Magento\Backend\Model\Auth',
48+
['getAuthStorage'],
49+
[],
50+
'',
51+
false
52+
);
53+
$this->authStorage = $this->getMock(
54+
'Magento\Backend\Model\Auth\StorageInterface'
55+
,
56+
['processLogin', 'processLogout', 'isLoggedIn', 'prolong', 'setDeletedPath'],
57+
[],
58+
'',
59+
false
60+
);
61+
$eventManager = $this->getMockForAbstractClass(
62+
'Magento\Framework\Event\ManagerInterface',
63+
[],
64+
'',
65+
false,
66+
true,
67+
true,
68+
['dispatch']
69+
);
70+
$response = $this->getMockForAbstractClass(
71+
'Magento\Framework\App\ResponseInterface',
72+
[],
73+
'',
74+
false
75+
);
76+
$messageManager = $this->getMockForAbstractClass(
77+
'Magento\Framework\Message\ManagerInterface',
78+
[],
79+
'',
80+
false,
81+
true,
82+
true,
83+
['addSuccess']
84+
);
85+
$this->categoryRepository = $this->getMock('Magento\Catalog\Api\CategoryRepositoryInterface');
86+
$context->expects($this->any())
87+
->method('getRequest')
88+
->will($this->returnValue($this->request));
89+
$context->expects($this->any())
90+
->method('getResponse')
91+
->will($this->returnValue($response));
92+
$context->expects($this->any())
93+
->method('getMessageManager')
94+
->will($this->returnValue($messageManager));
95+
$context->expects($this->any())
96+
->method('getEventManager')
97+
->will($this->returnValue($eventManager));
98+
$context->expects($this->any())
99+
->method('getAuth')
100+
->will($this->returnValue($auth));
101+
$auth->expects($this->any())
102+
->method('getAuthStorage')
103+
->will($this->returnValue($this->authStorage));
104+
105+
$this->resultRedirect = $this->getMock('Magento\Backend\Model\View\Result\Redirect', [], [], '', false);
106+
$resultRedirectFactory->expects($this->any())->method('create')->willReturn($this->resultRedirect);
107+
108+
$this->unit = (new ObjectManagerHelper($this))->getObject(
109+
'Magento\Catalog\Controller\Adminhtml\Category\Delete',
110+
[
111+
'context' => $context,
112+
'resultRedirectFactory' => $resultRedirectFactory,
113+
'categoryRepository' => $this->categoryRepository
114+
]
115+
);
116+
}
117+
118+
public function testDeleteWithoutCategoryId()
119+
{
120+
$this->request->expects($this->any())->method('getParam')->with('id')->willReturn(null);
121+
$this->resultRedirect->expects($this->once())->method('setPath')
122+
->with('catalog/*/', ['_current' => true, 'id' => null]);
123+
$this->categoryRepository->expects($this->never())->method('get');
124+
125+
$this->unit->execute();
126+
}
127+
128+
public function testDelete()
129+
{
130+
$categoryId = 5;
131+
$parentId = 7;
132+
$this->request->expects($this->any())->method('getParam')->with('id')->willReturn($categoryId);
133+
$category = $this->getMock('Magento\Catalog\Model\Category', ['getParentId', 'getPath'], [], '', false);
134+
$category->expects($this->once())->method('getParentId')->willReturn($parentId);
135+
$category->expects($this->once())->method('getPath')->willReturn('category-path');
136+
$this->categoryRepository->expects($this->once())->method('get')->with($categoryId)->willReturn($category);
137+
$this->authStorage->expects($this->once())->method('setDeletedPath')->with('category-path');
138+
$this->resultRedirect->expects($this->once())->method('setPath')
139+
->with('catalog/*/', ['_current' => true, 'id' => $parentId]);
140+
141+
$this->unit->execute();
142+
}
143+
}

0 commit comments

Comments
 (0)