EditTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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\Block;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class EditTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Cms\Controller\Adminhtml\Block\Edit
  14. */
  15. protected $editController;
  16. /**
  17. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  18. */
  19. protected $objectManager;
  20. /**
  21. * @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $contextMock;
  24. /**
  25. * @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $resultRedirectFactoryMock;
  28. /**
  29. * @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $resultRedirectMock;
  32. /**
  33. * @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $messageManagerMock;
  36. /**
  37. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $requestMock;
  40. /**
  41. * @var \Magento\Cms\Model\Block|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $blockMock;
  44. /**
  45. * @var \Magento\Framework\ObjectManager\ObjectManager|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $objectManagerMock;
  48. /**
  49. * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. protected $coreRegistryMock;
  52. /**
  53. * @var \Magento\Framework\View\Result\PageFactory|\PHPUnit_Framework_MockObject_MockObject
  54. */
  55. protected $resultPageFactoryMock;
  56. protected function setUp()
  57. {
  58. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  59. $this->messageManagerMock = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
  60. $this->coreRegistryMock = $this->createMock(\Magento\Framework\Registry::class);
  61. $this->blockMock = $this->getMockBuilder(\Magento\Cms\Model\Block::class)
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManager\ObjectManager::class)
  65. ->setMethods(['create', 'get'])
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->objectManagerMock->expects($this->once())
  69. ->method('create')
  70. ->with(\Magento\Cms\Model\Block::class)
  71. ->willReturn($this->blockMock);
  72. $this->resultRedirectMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $this->resultRedirectFactoryMock = $this->getMockBuilder(
  76. \Magento\Backend\Model\View\Result\RedirectFactory::class
  77. )->disableOriginalConstructor()->getMock();
  78. $this->resultPageFactoryMock = $this->createMock(\Magento\Framework\View\Result\PageFactory::class);
  79. $this->requestMock = $this->getMockForAbstractClass(
  80. \Magento\Framework\App\RequestInterface::class,
  81. [],
  82. '',
  83. false,
  84. true,
  85. true,
  86. []
  87. );
  88. $this->contextMock = $this->createMock(\Magento\Backend\App\Action\Context::class);
  89. $this->contextMock->expects($this->once())->method('getRequest')->willReturn($this->requestMock);
  90. $this->contextMock->expects($this->once())->method('getObjectManager')->willReturn($this->objectManagerMock);
  91. $this->contextMock->expects($this->once())->method('getMessageManager')->willReturn($this->messageManagerMock);
  92. $this->contextMock->expects($this->once())
  93. ->method('getResultRedirectFactory')
  94. ->willReturn($this->resultRedirectFactoryMock);
  95. $this->editController = $this->objectManager->getObject(
  96. \Magento\Cms\Controller\Adminhtml\Block\Edit::class,
  97. [
  98. 'context' => $this->contextMock,
  99. 'coreRegistry' => $this->coreRegistryMock,
  100. 'resultPageFactory' => $this->resultPageFactoryMock
  101. ]
  102. );
  103. }
  104. public function testEditActionBlockNoExists()
  105. {
  106. $blockId = 1;
  107. $this->requestMock->expects($this->once())
  108. ->method('getParam')
  109. ->with('block_id')
  110. ->willReturn($blockId);
  111. $this->blockMock->expects($this->once())
  112. ->method('load')
  113. ->with($blockId);
  114. $this->blockMock->expects($this->once())
  115. ->method('getId')
  116. ->willReturn(null);
  117. $this->messageManagerMock->expects($this->once())
  118. ->method('addErrorMessage')
  119. ->with(__('This block no longer exists.'));
  120. $this->resultRedirectFactoryMock->expects($this->atLeastOnce())
  121. ->method('create')
  122. ->willReturn($this->resultRedirectMock);
  123. $this->resultRedirectMock->expects($this->once())
  124. ->method('setPath')
  125. ->with('*/*/')
  126. ->willReturnSelf();
  127. $this->assertSame($this->resultRedirectMock, $this->editController->execute());
  128. }
  129. /**
  130. * @param int $blockId
  131. * @param string $label
  132. * @param string $title
  133. * @dataProvider editActionData
  134. */
  135. public function testEditAction($blockId, $label, $title)
  136. {
  137. $this->requestMock->expects($this->once())
  138. ->method('getParam')
  139. ->with('block_id')
  140. ->willReturn($blockId);
  141. $this->blockMock->expects($this->any())
  142. ->method('load')
  143. ->with($blockId);
  144. $this->blockMock->expects($this->any())
  145. ->method('getId')
  146. ->willReturn($blockId);
  147. $this->blockMock->expects($this->any())
  148. ->method('getTitle')
  149. ->willReturn('Test title');
  150. $this->coreRegistryMock->expects($this->once())
  151. ->method('register')
  152. ->with('cms_block', $this->blockMock);
  153. $resultPageMock = $this->createMock(\Magento\Backend\Model\View\Result\Page::class);
  154. $this->resultPageFactoryMock->expects($this->once())
  155. ->method('create')
  156. ->willReturn($resultPageMock);
  157. $titleMock = $this->createMock(\Magento\Framework\View\Page\Title::class);
  158. $titleMock->expects($this->at(0))->method('prepend')->with(__('Blocks'));
  159. $titleMock->expects($this->at(1))->method('prepend')->with($this->getTitle());
  160. $pageConfigMock = $this->createMock(\Magento\Framework\View\Page\Config::class);
  161. $pageConfigMock->expects($this->exactly(2))->method('getTitle')->willReturn($titleMock);
  162. $resultPageMock->expects($this->once())
  163. ->method('setActiveMenu')
  164. ->willReturnSelf();
  165. $resultPageMock->expects($this->any())
  166. ->method('addBreadcrumb')
  167. ->willReturnSelf();
  168. $resultPageMock->expects($this->at(3))
  169. ->method('addBreadcrumb')
  170. ->with(__($label), __($title))
  171. ->willReturnSelf();
  172. $resultPageMock->expects($this->exactly(2))
  173. ->method('getConfig')
  174. ->willReturn($pageConfigMock);
  175. $this->assertSame($resultPageMock, $this->editController->execute());
  176. }
  177. /**
  178. * @return \Magento\Framework\Phrase|string
  179. */
  180. protected function getTitle()
  181. {
  182. return $this->blockMock->getId() ? $this->blockMock->getTitle() : __('New Block');
  183. }
  184. /**
  185. * @return array
  186. */
  187. public function editActionData()
  188. {
  189. return [
  190. [null, 'New Block', 'New Block'],
  191. [2, 'Edit Block', 'Edit Block']
  192. ];
  193. }
  194. }