InlineEditTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. use Magento\Cms\Controller\Adminhtml\Page\InlineEdit;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class InlineEditTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  14. protected $request;
  15. /** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  16. protected $messageManager;
  17. /** @var \Magento\Framework\Message\MessageInterface|\PHPUnit_Framework_MockObject_MockObject */
  18. protected $message;
  19. /** @var \Magento\Framework\Message\Collection|\PHPUnit_Framework_MockObject_MockObject */
  20. protected $messageCollection;
  21. /** @var \Magento\Cms\Model\Page|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $cmsPage;
  23. /** @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject */
  24. protected $context;
  25. /** @var \Magento\Cms\Controller\Adminhtml\Page\PostDataProcessor|\PHPUnit_Framework_MockObject_MockObject */
  26. protected $dataProcessor;
  27. /** @var \Magento\Cms\Api\PageRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
  28. protected $pageRepository;
  29. /** @var \Magento\Framework\Controller\Result\JsonFactory|\PHPUnit_Framework_MockObject_MockObject */
  30. protected $jsonFactory;
  31. /** @var \Magento\Framework\Controller\Result\Json|\PHPUnit_Framework_MockObject_MockObject */
  32. protected $resultJson;
  33. /** @var InlineEdit */
  34. protected $controller;
  35. protected function setUp()
  36. {
  37. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  38. $this->request = $this->getMockForAbstractClass(\Magento\Framework\App\RequestInterface::class);
  39. $this->messageManager = $this->getMockForAbstractClass(\Magento\Framework\Message\ManagerInterface::class);
  40. $this->messageCollection = $this->createMock(\Magento\Framework\Message\Collection::class);
  41. $this->message = $this->getMockForAbstractClass(\Magento\Framework\Message\MessageInterface::class);
  42. $this->cmsPage = $this->createMock(\Magento\Cms\Model\Page::class);
  43. $this->context = $helper->getObject(
  44. \Magento\Backend\App\Action\Context::class,
  45. [
  46. 'request' => $this->request,
  47. 'messageManager' => $this->messageManager
  48. ]
  49. );
  50. $this->dataProcessor = $this->createMock(\Magento\Cms\Controller\Adminhtml\Page\PostDataProcessor::class);
  51. $this->pageRepository = $this->getMockForAbstractClass(\Magento\Cms\Api\PageRepositoryInterface::class);
  52. $this->resultJson = $this->createMock(\Magento\Framework\Controller\Result\Json::class);
  53. $this->jsonFactory = $this->createPartialMock(
  54. \Magento\Framework\Controller\Result\JsonFactory::class,
  55. ['create']
  56. );
  57. $this->controller = new InlineEdit(
  58. $this->context,
  59. $this->dataProcessor,
  60. $this->pageRepository,
  61. $this->jsonFactory
  62. );
  63. }
  64. public function prepareMocksForTestExecute()
  65. {
  66. $postData = [
  67. 1 => [
  68. 'title' => '404 Not Found',
  69. 'identifier' => 'no-route',
  70. 'custom_theme' => '1',
  71. 'custom_root_template' => '2'
  72. ]
  73. ];
  74. $this->request->expects($this->any())
  75. ->method('getParam')
  76. ->willReturnMap(
  77. [
  78. ['isAjax', null, true],
  79. ['items', [], $postData]
  80. ]
  81. );
  82. $this->pageRepository->expects($this->once())
  83. ->method('getById')
  84. ->with(1)
  85. ->willReturn($this->cmsPage);
  86. $this->dataProcessor->expects($this->once())
  87. ->method('filter')
  88. ->with($postData[1])
  89. ->willReturnArgument(0);
  90. $this->dataProcessor->expects($this->once())
  91. ->method('validate')
  92. ->with($postData[1])
  93. ->willReturn(false);
  94. $this->messageManager->expects($this->once())
  95. ->method('getMessages')
  96. ->with(true)
  97. ->willReturn($this->messageCollection);
  98. $this->messageCollection
  99. ->expects($this->once())
  100. ->method('getItems')
  101. ->willReturn([$this->message]);
  102. $this->message->expects($this->once())
  103. ->method('getText')
  104. ->willReturn('Error message');
  105. $this->cmsPage->expects($this->atLeastOnce())
  106. ->method('getId')
  107. ->willReturn('1');
  108. $this->cmsPage->expects($this->atLeastOnce())
  109. ->method('getData')
  110. ->willReturn([
  111. 'layout' => '1column',
  112. 'identifier' => 'test-identifier'
  113. ]);
  114. $this->cmsPage->expects($this->once())
  115. ->method('setData')
  116. ->with([
  117. 'layout' => '1column',
  118. 'title' => '404 Not Found',
  119. 'identifier' => 'no-route',
  120. 'custom_theme' => '1',
  121. 'custom_root_template' => '2'
  122. ]);
  123. $this->jsonFactory->expects($this->once())
  124. ->method('create')
  125. ->willReturn($this->resultJson);
  126. }
  127. public function testExecuteWithLocalizedException()
  128. {
  129. $this->prepareMocksForTestExecute();
  130. $this->pageRepository->expects($this->once())
  131. ->method('save')
  132. ->with($this->cmsPage)
  133. ->willThrowException(new \Magento\Framework\Exception\LocalizedException(__('LocalizedException')));
  134. $this->resultJson->expects($this->once())
  135. ->method('setData')
  136. ->with([
  137. 'messages' => [
  138. '[Page ID: 1] Error message',
  139. '[Page ID: 1] LocalizedException'
  140. ],
  141. 'error' => true
  142. ])
  143. ->willReturnSelf();
  144. $this->assertSame($this->resultJson, $this->controller->execute());
  145. }
  146. public function testExecuteWithRuntimeException()
  147. {
  148. $this->prepareMocksForTestExecute();
  149. $this->pageRepository->expects($this->once())
  150. ->method('save')
  151. ->with($this->cmsPage)
  152. ->willThrowException(new \RuntimeException(__('RuntimeException')));
  153. $this->resultJson->expects($this->once())
  154. ->method('setData')
  155. ->with([
  156. 'messages' => [
  157. '[Page ID: 1] Error message',
  158. '[Page ID: 1] RuntimeException'
  159. ],
  160. 'error' => true
  161. ])
  162. ->willReturnSelf();
  163. $this->assertSame($this->resultJson, $this->controller->execute());
  164. }
  165. public function testExecuteWithException()
  166. {
  167. $this->prepareMocksForTestExecute();
  168. $this->pageRepository->expects($this->once())
  169. ->method('save')
  170. ->with($this->cmsPage)
  171. ->willThrowException(new \Exception(__('Exception')));
  172. $this->resultJson->expects($this->once())
  173. ->method('setData')
  174. ->with([
  175. 'messages' => [
  176. '[Page ID: 1] Error message',
  177. '[Page ID: 1] Something went wrong while saving the page.'
  178. ],
  179. 'error' => true
  180. ])
  181. ->willReturnSelf();
  182. $this->assertSame($this->resultJson, $this->controller->execute());
  183. }
  184. public function testExecuteWithoutData()
  185. {
  186. $this->jsonFactory->expects($this->once())
  187. ->method('create')
  188. ->willReturn($this->resultJson);
  189. $this->request->expects($this->any())
  190. ->method('getParam')
  191. ->willReturnMap(
  192. [
  193. ['items', [], []],
  194. ['isAjax', null, true]
  195. ]
  196. );
  197. $this->resultJson->expects($this->once())
  198. ->method('setData')
  199. ->with([
  200. 'messages' => [
  201. 'Please correct the data sent.'
  202. ],
  203. 'error' => true
  204. ])
  205. ->willReturnSelf();
  206. $this->assertSame($this->resultJson, $this->controller->execute());
  207. }
  208. public function testSetCmsPageData()
  209. {
  210. $extendedPageData = [
  211. 'page_id' => '2',
  212. 'title' => 'Home Page',
  213. 'page_layout' => '1column',
  214. 'identifier' => 'home',
  215. 'content_heading' => 'Home Page',
  216. 'content' => 'CMS homepage content goes here.',
  217. 'is_active' => '1',
  218. 'sort_order' => '1',
  219. 'custom_theme' => '3',
  220. 'store_id' => ['0']
  221. ];
  222. $pageData = [
  223. 'page_id' => '2',
  224. 'title' => 'Home Page',
  225. 'page_layout' => '1column',
  226. 'identifier' => 'home',
  227. 'is_active' => '1',
  228. 'custom_theme' => '3',
  229. ];
  230. $getData = [
  231. 'page_id' => '2',
  232. 'title' => 'Home Page',
  233. 'page_layout' => '1column',
  234. 'identifier' => 'home',
  235. 'content_heading' => 'Home Page',
  236. 'content' => 'CMS homepage content goes here.',
  237. 'is_active' => '1',
  238. 'sort_order' => '1',
  239. 'custom_theme' => '3',
  240. 'custom_root_template' => '1column',
  241. 'store_id' => ['0']
  242. ];
  243. $mergedData = [
  244. 'page_id' => '2',
  245. 'title' => 'Home Page',
  246. 'page_layout' => '1column',
  247. 'identifier' => 'home',
  248. 'content_heading' => 'Home Page',
  249. 'content' => 'CMS homepage content goes here.',
  250. 'is_active' => '1',
  251. 'sort_order' => '1',
  252. 'custom_theme' => '3',
  253. 'custom_root_template' => '1column',
  254. 'store_id' => ['0']
  255. ];
  256. $this->cmsPage->expects($this->once())->method('getData')->willReturn($getData);
  257. $this->cmsPage->expects($this->once())->method('setData')->with($mergedData)->willReturnSelf();
  258. $this->assertSame(
  259. $this->controller,
  260. $this->controller->setCmsPageData($this->cmsPage, $extendedPageData, $pageData)
  261. );
  262. }
  263. }