Skip to content

Commit ef48e5a

Browse files
committed
Merge remote-tracking branch 'goinc/MAGETWO-31647-MAGETWO-31723' into MAGETWO-32346
2 parents e25ad0d + f826a37 commit ef48e5a

File tree

5 files changed

+178
-27
lines changed

5 files changed

+178
-27
lines changed

app/code/Magento/Catalog/Block/Adminhtml/Category/Edit/Form.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ protected function _prepareLayout()
9393
'onclick' => "categoryDelete('" . $this->getUrl(
9494
'catalog/*/delete',
9595
['_current' => true]
96-
) . "', true, {$categoryId})",
96+
) . "')",
9797
'class' => 'delete'
9898
]
9999
);

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

Lines changed: 23 additions & 6 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
*
@@ -18,14 +35,14 @@ public function execute()
1835
$resultRedirect = $this->resultRedirectFactory->create();
1936

2037
$categoryId = (int)$this->getRequest()->getParam('id');
38+
$parentId = null;
2139
if ($categoryId) {
2240
try {
23-
$category = $this->_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
41+
$category = $this->categoryRepository->get($categoryId);
42+
$parentId = $category->getParentId();
2443
$this->_eventManager->dispatch('catalog_controller_category_delete', ['category' => $category]);
25-
26-
$this->_objectManager->get('Magento\Backend\Model\Auth\Session')->setDeletedPath($category->getPath());
27-
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());
@@ -35,6 +52,6 @@ public function execute()
3552
return $resultRedirect->setPath('catalog/*/edit', ['_current' => true]);
3653
}
3754
}
38-
return $resultRedirect->setPath('catalog/*/', ['_current' => true, 'id' => null]);
55+
return $resultRedirect->setPath('catalog/*/', ['_current' => true, 'id' => $parentId]);
3956
}
4057
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public function execute()
9797
. $resultPage->getLayout()->getBlock('category.tree')
9898
->getBreadcrumbsJavascript($breadcrumbsPath, 'editingCategoryBreadcrumbs'),
9999
'messages' => $resultPage->getLayout()->getMessagesBlock()->getGroupedHtml(),
100+
'toolbar' => $resultPage->getLayout()->getBlock('page.actions.toolbar')->toHtml()
100101
]);
101102
$this->_eventManager->dispatch(
102103
'category_prepare_ajax_response',

app/code/Magento/Catalog/view/adminhtml/templates/catalog/category/edit.phtml

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,16 @@ require([
3434
* This routine get categoryId explicitly, so even if currently selected tree node is out of sync
3535
* with this form, we surely delete same category in the tree and at backend
3636
*/
37-
function categoryDelete(url, useAjax, categoryId) {
37+
function categoryDelete(url) {
3838
if (confirm('<?php echo __('Are you sure you want to delete this category?') ?>')){
39-
if (useAjax){
40-
tree.nodeForDelete = categoryId;
41-
updateContent(url, {}, true, true);
42-
} else {
43-
location.href = url;
44-
}
39+
location.href = url;
4540
}
4641
}
4742

4843
/**
4944
* Update category content area
5045
*/
51-
function updateContent(url, params, refreshTree, deleteAction) {
46+
function updateContent(url, params, refreshTree) {
5247
var node = tree.getNodeById(tree.currentNodeId),
5348
parentNode = node && node.parentNode,
5449
parentId,
@@ -64,23 +59,15 @@ require([
6459
}
6560

6661
(function($){
67-
var $categoryContainer = $('#category-edit-container');
62+
var $categoryContainer = $('#category-edit-container'),
63+
messagesContainer = $('.messages');
64+
messagesContainer.html('');
6865
$.ajax({
6966
url: url + (url.match(new RegExp('\\?')) ? '&isAjax=true' : '?isAjax=true' ),
7067
data: params,
7168
context: $('body'),
7269
showLoader: true
7370
}).done(function(data){
74-
if (deleteAction && parentNode) {
75-
parentId = parentNode.id;
76-
77-
redirectUrl = !parentNode.isRoot ?
78-
tree.buildUrl(parentId) :
79-
tree.getBaseUrl();
80-
81-
location.href = redirectUrl;
82-
}
83-
8471
if (data.content) {
8572
$('.page-actions').floatingHeader('destroy');
8673
try {
@@ -111,7 +98,10 @@ require([
11198
}
11299

113100
if (data.messages && data.messages.length > 0) {
114-
$('.messages').html(data.messages);
101+
messagesContainer.html(data.messages);
102+
}
103+
if (data.toolbar) {
104+
$('[data-ui-id="page-actions-toolbar-content-header"]').replaceWith(data.toolbar)
115105
}
116106
});
117107
})(jQuery);
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)