DeleteTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Test\Unit\Controller\Adminhtml\Page;
  7. class DeleteTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /** @var \Magento\Cms\Controller\Adminhtml\Page\Delete */
  10. protected $deleteController;
  11. /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
  12. protected $objectManager;
  13. /** @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject */
  14. protected $contextMock;
  15. /** @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject */
  16. protected $resultRedirectFactoryMock;
  17. /** @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject */
  18. protected $resultRedirectMock;
  19. /** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  20. protected $messageManagerMock;
  21. /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $requestMock;
  23. /** @var \Magento\Framework\ObjectManager\ObjectManager|\PHPUnit_Framework_MockObject_MockObject */
  24. protected $objectManagerMock;
  25. /** @var \Magento\Cms\Model\Page|\PHPUnit_Framework_MockObject_MockObject $pageMock */
  26. protected $pageMock;
  27. /** @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  28. protected $eventManagerMock;
  29. /** @var string */
  30. protected $title = 'This is the title of the page.';
  31. /** @var int */
  32. protected $pageId = 1;
  33. protected function setUp()
  34. {
  35. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  36. $this->messageManagerMock = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
  37. $this->requestMock = $this->getMockForAbstractClass(
  38. \Magento\Framework\App\RequestInterface::class,
  39. [],
  40. '',
  41. false,
  42. true,
  43. true,
  44. ['getParam']
  45. );
  46. $this->pageMock = $this->getMockBuilder(\Magento\Cms\Model\Page::class)
  47. ->disableOriginalConstructor()
  48. ->setMethods(['load', 'delete', 'getTitle'])
  49. ->getMock();
  50. $this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManager\ObjectManager::class)
  51. ->disableOriginalConstructor()
  52. ->setMethods(['create'])
  53. ->getMock();
  54. $this->resultRedirectMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
  55. ->setMethods(['setPath'])
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->resultRedirectFactoryMock = $this->getMockBuilder(
  59. \Magento\Backend\Model\View\Result\RedirectFactory::class
  60. )->disableOriginalConstructor()
  61. ->setMethods(['create'])
  62. ->getMock();
  63. $this->resultRedirectFactoryMock->expects($this->atLeastOnce())
  64. ->method('create')
  65. ->willReturn($this->resultRedirectMock);
  66. $this->eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  67. $this->contextMock = $this->createMock(\Magento\Backend\App\Action\Context::class);
  68. $this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock);
  69. $this->contextMock->expects($this->any())->method('getMessageManager')->willReturn($this->messageManagerMock);
  70. $this->contextMock->expects($this->any())->method('getObjectManager')->willReturn($this->objectManagerMock);
  71. $this->contextMock->expects($this->any())->method('getEventManager')->willReturn($this->eventManagerMock);
  72. $this->contextMock->expects($this->any())
  73. ->method('getResultRedirectFactory')
  74. ->willReturn($this->resultRedirectFactoryMock);
  75. $this->deleteController = $this->objectManager->getObject(
  76. \Magento\Cms\Controller\Adminhtml\Page\Delete::class,
  77. [
  78. 'context' => $this->contextMock,
  79. ]
  80. );
  81. }
  82. public function testDeleteAction()
  83. {
  84. $this->requestMock->expects($this->once())
  85. ->method('getParam')
  86. ->willReturn($this->pageId);
  87. $this->objectManagerMock->expects($this->once())
  88. ->method('create')
  89. ->with(\Magento\Cms\Model\Page::class)
  90. ->willReturn($this->pageMock);
  91. $this->pageMock->expects($this->once())
  92. ->method('load')
  93. ->with($this->pageId);
  94. $this->pageMock->expects($this->once())
  95. ->method('getTitle')
  96. ->willReturn($this->title);
  97. $this->pageMock->expects($this->once())
  98. ->method('delete');
  99. $this->messageManagerMock->expects($this->once())
  100. ->method('addSuccessMessage')
  101. ->with(__('The page has been deleted.'));
  102. $this->messageManagerMock->expects($this->never())
  103. ->method('addErrorMessage');
  104. $this->eventManagerMock->expects($this->once())
  105. ->method('dispatch')
  106. ->with(
  107. 'adminhtml_cmspage_on_delete',
  108. ['title' => $this->title, 'status' => 'success']
  109. );
  110. $this->resultRedirectMock->expects($this->once())
  111. ->method('setPath')
  112. ->with('*/*/')
  113. ->willReturnSelf();
  114. $this->assertSame($this->resultRedirectMock, $this->deleteController->execute());
  115. }
  116. public function testDeleteActionNoId()
  117. {
  118. $this->requestMock->expects($this->once())
  119. ->method('getParam')
  120. ->willReturn(null);
  121. $this->messageManagerMock->expects($this->once())
  122. ->method('addErrorMessage')
  123. ->with(__('We can\'t find a page to delete.'));
  124. $this->messageManagerMock->expects($this->never())
  125. ->method('addSuccessMessage');
  126. $this->resultRedirectMock->expects($this->once())
  127. ->method('setPath')
  128. ->with('*/*/')
  129. ->willReturnSelf();
  130. $this->assertSame($this->resultRedirectMock, $this->deleteController->execute());
  131. }
  132. public function testDeleteActionThrowsException()
  133. {
  134. $errorMsg = 'Can\'t delete the page';
  135. $this->requestMock->expects($this->once())
  136. ->method('getParam')
  137. ->willReturn($this->pageId);
  138. $this->objectManagerMock->expects($this->once())
  139. ->method('create')
  140. ->with(\Magento\Cms\Model\Page::class)
  141. ->willReturn($this->pageMock);
  142. $this->pageMock->expects($this->once())
  143. ->method('load')
  144. ->with($this->pageId);
  145. $this->pageMock->expects($this->once())
  146. ->method('getTitle')
  147. ->willReturn($this->title);
  148. $this->pageMock->expects($this->once())
  149. ->method('delete')
  150. ->willThrowException(new \Exception(__($errorMsg)));
  151. $this->eventManagerMock->expects($this->once())
  152. ->method('dispatch')
  153. ->with(
  154. 'adminhtml_cmspage_on_delete',
  155. ['title' => $this->title, 'status' => 'fail']
  156. );
  157. $this->messageManagerMock->expects($this->once())
  158. ->method('addErrorMessage')
  159. ->with($errorMsg);
  160. $this->messageManagerMock->expects($this->never())
  161. ->method('addSuccessMessage');
  162. $this->resultRedirectMock->expects($this->once())
  163. ->method('setPath')
  164. ->with('*/*/edit', ['page_id' => $this->pageId])
  165. ->willReturnSelf();
  166. $this->assertSame($this->resultRedirectMock, $this->deleteController->execute());
  167. }
  168. }