diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Category/Delete.php b/app/code/Magento/Catalog/Controller/Adminhtml/Category/Delete.php index e50c9d38b9c070fee9a5d81b6d7f432e80d850b0..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 * @@ -21,11 +38,11 @@ class Delete extends \Magento\Catalog\Controller\Adminhtml\Category $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()); diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Category/DeleteTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Category/DeleteTest.php deleted file mode 100644 index 34b69eeaddef32fcd690ee77896c266fe734ff1c..0000000000000000000000000000000000000000 --- a/dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Category/DeleteTest.php +++ /dev/null @@ -1,43 +0,0 @@ -<?php -/** - * @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com) - */ -namespace Magento\Catalog\Controller\Adminhtml\Category; - -use Magento\Framework\Message\MessageInterface; - -class DeleteTest extends \Magento\Backend\Utility\Controller -{ - /** - * @magentoDataFixture Magento/Catalog/_files/categories.php - */ - public function testDeleteById() - { - $categoryId = 4; - $parentId = 3; - $this->getRequest()->setParam('id', $categoryId); - - $this->dispatch('backend/catalog/category/delete/'); - - $this->assertNull($this->getCategoryById($categoryId)); - $this->assertSessionMessages( - $this->equalTo(['You deleted the category.']), - MessageInterface::TYPE_SUCCESS - ); - $this->assertRedirect($this->stringContains('catalog/category/index/id/' . $parentId)); - } - - /** - * Retrieve attribute set based on given name. - * - * @param int $categoryId - * @return \Magento\Catalog\Model\Category|null - */ - protected function getCategoryById($categoryId) - { - $category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( - 'Magento\Catalog\Model\Category' - )->load($categoryId); - return $category->getId() === null ? null : $category; - } -} 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(); + } +}