diff --git a/app/code/Magento/Catalog/Block/Adminhtml/Category/Edit/Form.php b/app/code/Magento/Catalog/Block/Adminhtml/Category/Edit/Form.php index 9cd66815ecf3e0b9307fe5732a92c968d894ad54..6ff14589e82ebfbe5435bbeef8a50cb03e45a0ae 100644 --- a/app/code/Magento/Catalog/Block/Adminhtml/Category/Edit/Form.php +++ b/app/code/Magento/Catalog/Block/Adminhtml/Category/Edit/Form.php @@ -93,7 +93,7 @@ class Form extends \Magento\Catalog\Block\Adminhtml\Category\AbstractCategory 'onclick' => "categoryDelete('" . $this->getUrl( 'catalog/*/delete', ['_current' => true] - ) . "', true, {$categoryId})", + ) . "')", 'class' => 'delete' ] ); diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Category/Delete.php b/app/code/Magento/Catalog/Controller/Adminhtml/Category/Delete.php index f5b053679733a438d0da357db58fc75858db8c7a..3c4eded51ce978738835e9d41cc01e32629f3a46 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Category/Delete.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Category/Delete.php @@ -7,6 +7,23 @@ namespace Magento\Catalog\Controller\Adminhtml\Category; class Delete extends \Magento\Catalog\Controller\Adminhtml\Category { + /** @var \Magento\Catalog\Api\CategoryRepositoryInterface */ + protected $categoryRepository; + + /** + * @param \Magento\Backend\App\Action\Context $context + * @param \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory + * @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository + */ + public function __construct( + \Magento\Backend\App\Action\Context $context, + \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory, + \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository + ) { + parent::__construct($context, $resultRedirectFactory); + $this->categoryRepository = $categoryRepository; + } + /** * Delete category action * @@ -18,14 +35,14 @@ class Delete extends \Magento\Catalog\Controller\Adminhtml\Category $resultRedirect = $this->resultRedirectFactory->create(); $categoryId = (int)$this->getRequest()->getParam('id'); + $parentId = null; if ($categoryId) { try { - $category = $this->_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId); + $category = $this->categoryRepository->get($categoryId); + $parentId = $category->getParentId(); $this->_eventManager->dispatch('catalog_controller_category_delete', ['category' => $category]); - - $this->_objectManager->get('Magento\Backend\Model\Auth\Session')->setDeletedPath($category->getPath()); - - $category->delete(); + $this->_auth->getAuthStorage()->setDeletedPath($category->getPath()); + $this->categoryRepository->delete($category); $this->messageManager->addSuccess(__('You deleted the category.')); } catch (\Magento\Framework\Model\Exception $e) { $this->messageManager->addError($e->getMessage()); @@ -35,6 +52,6 @@ class Delete extends \Magento\Catalog\Controller\Adminhtml\Category return $resultRedirect->setPath('catalog/*/edit', ['_current' => true]); } } - return $resultRedirect->setPath('catalog/*/', ['_current' => true, 'id' => null]); + return $resultRedirect->setPath('catalog/*/', ['_current' => true, 'id' => $parentId]); } } diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Category/Edit.php b/app/code/Magento/Catalog/Controller/Adminhtml/Category/Edit.php index f17bfc3abd5c1630e0e93082724eb30a8f6e1b1a..7f6dde5cb3ee101c0d2ed9f1dc3b2c1c19cf213a 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Category/Edit.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Category/Edit.php @@ -97,6 +97,7 @@ class Edit extends \Magento\Catalog\Controller\Adminhtml\Category . $resultPage->getLayout()->getBlock('category.tree') ->getBreadcrumbsJavascript($breadcrumbsPath, 'editingCategoryBreadcrumbs'), 'messages' => $resultPage->getLayout()->getMessagesBlock()->getGroupedHtml(), + 'toolbar' => $resultPage->getLayout()->getBlock('page.actions.toolbar')->toHtml() ]); $this->_eventManager->dispatch( 'category_prepare_ajax_response', diff --git a/app/code/Magento/Catalog/view/adminhtml/templates/catalog/category/edit.phtml b/app/code/Magento/Catalog/view/adminhtml/templates/catalog/category/edit.phtml index 75b59da8d09aa3b5da4483c7fe22493056038a6d..d1d3ec2b11497fd593f8e7346144b79f4e321a0b 100644 --- a/app/code/Magento/Catalog/view/adminhtml/templates/catalog/category/edit.phtml +++ b/app/code/Magento/Catalog/view/adminhtml/templates/catalog/category/edit.phtml @@ -34,21 +34,16 @@ require([ * This routine get categoryId explicitly, so even if currently selected tree node is out of sync * with this form, we surely delete same category in the tree and at backend */ - function categoryDelete(url, useAjax, categoryId) { + function categoryDelete(url) { if (confirm('<?php echo __('Are you sure you want to delete this category?') ?>')){ - if (useAjax){ - tree.nodeForDelete = categoryId; - updateContent(url, {}, true, true); - } else { - location.href = url; - } + location.href = url; } } /** * Update category content area */ - function updateContent(url, params, refreshTree, deleteAction) { + function updateContent(url, params, refreshTree) { var node = tree.getNodeById(tree.currentNodeId), parentNode = node && node.parentNode, parentId, @@ -64,23 +59,15 @@ require([ } (function($){ - var $categoryContainer = $('#category-edit-container'); + var $categoryContainer = $('#category-edit-container'), + messagesContainer = $('.messages'); + messagesContainer.html(''); $.ajax({ url: url + (url.match(new RegExp('\\?')) ? '&isAjax=true' : '?isAjax=true' ), data: params, context: $('body'), showLoader: true }).done(function(data){ - if (deleteAction && parentNode) { - parentId = parentNode.id; - - redirectUrl = !parentNode.isRoot ? - tree.buildUrl(parentId) : - tree.getBaseUrl(); - - location.href = redirectUrl; - } - if (data.content) { $('.page-actions').floatingHeader('destroy'); try { @@ -111,7 +98,10 @@ require([ } if (data.messages && data.messages.length > 0) { - $('.messages').html(data.messages); + messagesContainer.html(data.messages); + } + if (data.toolbar) { + $('[data-ui-id="page-actions-toolbar-content-header"]').replaceWith(data.toolbar) } }); })(jQuery); diff --git a/dev/tests/unit/testsuite/Magento/Catalog/Controller/Adminhtml/Category/DeleteTest.php b/dev/tests/unit/testsuite/Magento/Catalog/Controller/Adminhtml/Category/DeleteTest.php new file mode 100644 index 0000000000000000000000000000000000000000..0a1ec1af089b9b4a0261d5248ecc3c3f66b39bf2 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Catalog/Controller/Adminhtml/Category/DeleteTest.php @@ -0,0 +1,143 @@ +<?php +/** + * @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com) + */ + +namespace Magento\Catalog\Controller\Adminhtml\Category; + +use Magento\TestFramework\Helper\ObjectManager as ObjectManagerHelper; + +class DeleteTest extends \PHPUnit_Framework_TestCase +{ + /** @var \Magento\Catalog\Controller\Adminhtml\Category\Delete */ + protected $unit; + + /** @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject */ + protected $resultRedirect; + + /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ + protected $request; + + /** @var \Magento\Catalog\Api\CategoryRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */ + protected $categoryRepository; + + /** @var \Magento\Backend\Model\Auth\StorageInterface|\PHPUnit_Framework_MockObject_MockObject */ + protected $authStorage; + + protected function setUp() + { + $context = $this->getMock('Magento\Backend\App\Action\Context', [], [], '', false); + $resultRedirectFactory = $this->getMock( + 'Magento\Backend\Model\View\Result\RedirectFactory', + ['create'], + [], + '', + false + ); + $this->request = $this->getMockForAbstractClass( + 'Magento\Framework\App\RequestInterface', + [], + '', + false, + true, + true, + ['getParam', 'getPost'] + ); + $auth = $this->getMock( + 'Magento\Backend\Model\Auth', + ['getAuthStorage'], + [], + '', + false + ); + $this->authStorage = $this->getMock( + 'Magento\Backend\Model\Auth\StorageInterface' + , + ['processLogin', 'processLogout', 'isLoggedIn', 'prolong', 'setDeletedPath'], + [], + '', + false + ); + $eventManager = $this->getMockForAbstractClass( + 'Magento\Framework\Event\ManagerInterface', + [], + '', + false, + true, + true, + ['dispatch'] + ); + $response = $this->getMockForAbstractClass( + 'Magento\Framework\App\ResponseInterface', + [], + '', + false + ); + $messageManager = $this->getMockForAbstractClass( + 'Magento\Framework\Message\ManagerInterface', + [], + '', + false, + true, + true, + ['addSuccess'] + ); + $this->categoryRepository = $this->getMock('Magento\Catalog\Api\CategoryRepositoryInterface'); + $context->expects($this->any()) + ->method('getRequest') + ->will($this->returnValue($this->request)); + $context->expects($this->any()) + ->method('getResponse') + ->will($this->returnValue($response)); + $context->expects($this->any()) + ->method('getMessageManager') + ->will($this->returnValue($messageManager)); + $context->expects($this->any()) + ->method('getEventManager') + ->will($this->returnValue($eventManager)); + $context->expects($this->any()) + ->method('getAuth') + ->will($this->returnValue($auth)); + $auth->expects($this->any()) + ->method('getAuthStorage') + ->will($this->returnValue($this->authStorage)); + + $this->resultRedirect = $this->getMock('Magento\Backend\Model\View\Result\Redirect', [], [], '', false); + $resultRedirectFactory->expects($this->any())->method('create')->willReturn($this->resultRedirect); + + $this->unit = (new ObjectManagerHelper($this))->getObject( + 'Magento\Catalog\Controller\Adminhtml\Category\Delete', + [ + 'context' => $context, + 'resultRedirectFactory' => $resultRedirectFactory, + 'categoryRepository' => $this->categoryRepository + ] + ); + } + + public function testDeleteWithoutCategoryId() + { + $this->request->expects($this->any())->method('getParam')->with('id')->willReturn(null); + $this->resultRedirect->expects($this->once())->method('setPath') + ->with('catalog/*/', ['_current' => true, 'id' => null]); + $this->categoryRepository->expects($this->never())->method('get'); + + $this->unit->execute(); + } + + public function testDelete() + { + $categoryId = 5; + $parentId = 7; + $this->request->expects($this->any())->method('getParam')->with('id')->willReturn($categoryId); + $category = $this->getMock('Magento\Catalog\Model\Category', ['getParentId', 'getPath'], [], '', false); + $category->expects($this->once())->method('getParentId')->willReturn($parentId); + $category->expects($this->once())->method('getPath')->willReturn('category-path'); + $this->categoryRepository->expects($this->once())->method('get')->with($categoryId)->willReturn($category); + $this->authStorage->expects($this->once())->method('setDeletedPath')->with('category-path'); + $this->resultRedirect->expects($this->once())->method('setPath') + ->with('catalog/*/', ['_current' => true, 'id' => $parentId]); + + $this->unit->execute(); + } +}