SaveTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class SaveTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. private $requestMock;
  16. /**
  17. * @var \Magento\Cms\Controller\Adminhtml\Page\PostDataProcessor|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $dataProcessorMock;
  20. /**
  21. * @var \Magento\Framework\App\Request\DataPersistorInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $dataPersistorMock;
  24. /**
  25. * @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $resultRedirectFactory;
  28. /**
  29. * @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $resultRedirect;
  32. /**
  33. * @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $messageManagerMock;
  36. /**
  37. * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $eventManagerMock;
  40. /**
  41. * @var \Magento\Cms\Model\PageFactory|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $pageFactory;
  44. /**
  45. * @var \Magento\Cms\Api\PageRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. private $pageRepository;
  48. /**
  49. * @var \Magento\Cms\Controller\Adminhtml\Page\Save
  50. */
  51. private $saveController;
  52. /**
  53. * @var int
  54. */
  55. private $pageId = 1;
  56. protected function setUp()
  57. {
  58. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  59. $this->resultRedirectFactory = $this->getMockBuilder(\Magento\Backend\Model\View\Result\RedirectFactory::class)
  60. ->disableOriginalConstructor()
  61. ->setMethods(['create'])
  62. ->getMock();
  63. $this->resultRedirect = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $this->resultRedirectFactory->expects($this->atLeastOnce())
  67. ->method('create')
  68. ->willReturn($this->resultRedirect);
  69. $this->dataProcessorMock = $this->getMockBuilder(
  70. \Magento\Cms\Controller\Adminhtml\Page\PostDataProcessor::class
  71. )->setMethods(['filter'])->disableOriginalConstructor()->getMock();
  72. $this->dataPersistorMock = $this->getMockBuilder(\Magento\Framework\App\Request\DataPersistorInterface::class)
  73. ->getMock();
  74. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  75. ->setMethods(['getParam', 'getPostValue'])
  76. ->getMockForAbstractClass();
  77. $this->messageManagerMock = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
  78. ->getMockForAbstractClass();
  79. $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
  80. ->setMethods(['dispatch'])
  81. ->getMockForAbstractClass();
  82. $this->pageFactory = $this->getMockBuilder(\Magento\Cms\Model\PageFactory::class)
  83. ->disableOriginalConstructor()
  84. ->setMethods(['create'])
  85. ->getMock();
  86. $this->pageRepository = $this->getMockBuilder(\Magento\Cms\Api\PageRepositoryInterface::class)
  87. ->disableOriginalConstructor()
  88. ->getMockForAbstractClass();
  89. $this->saveController = $objectManager->getObject(
  90. \Magento\Cms\Controller\Adminhtml\Page\Save::class,
  91. [
  92. 'request' => $this->requestMock,
  93. 'messageManager' => $this->messageManagerMock,
  94. 'eventManager' => $this->eventManagerMock,
  95. 'resultRedirectFactory' => $this->resultRedirectFactory,
  96. 'dataProcessor' => $this->dataProcessorMock,
  97. 'dataPersistor' => $this->dataPersistorMock,
  98. 'pageFactory' => $this->pageFactory,
  99. 'pageRepository' => $this->pageRepository
  100. ]
  101. );
  102. }
  103. public function testSaveAction()
  104. {
  105. $postData = [
  106. 'title' => '"><img src=y onerror=prompt(document.domain)>;',
  107. 'identifier' => 'unique_title_123',
  108. 'stores' => ['0'],
  109. 'is_active' => true,
  110. 'content' => '"><script>alert("cookie: "+document.cookie)</script>',
  111. 'back' => 'close'
  112. ];
  113. $filteredPostData = [
  114. 'title' => '&quot;&gt;&lt;img src=y onerror=prompt(document.domain)&gt;;',
  115. 'identifier' => 'unique_title_123',
  116. 'stores' => ['0'],
  117. 'is_active' => true,
  118. 'content' => '&quot;&gt;&lt;script&gt;alert(&quot;cookie: &quot;+document.cookie)&lt;/script&gt;',
  119. 'back' => 'close'
  120. ];
  121. $this->dataProcessorMock->expects($this->any())
  122. ->method('filter')
  123. ->with($postData)
  124. ->willReturn($filteredPostData);
  125. $this->requestMock->expects($this->any())->method('getPostValue')->willReturn($postData);
  126. $this->requestMock->expects($this->atLeastOnce())
  127. ->method('getParam')
  128. ->willReturnMap(
  129. [
  130. ['page_id', null, $this->pageId],
  131. ['back', null, false],
  132. ]
  133. );
  134. $page = $this->getMockBuilder(\Magento\Cms\Model\Page::class)
  135. ->disableOriginalConstructor()
  136. ->getMock();
  137. $this->pageFactory->expects($this->atLeastOnce())
  138. ->method('create')
  139. ->willReturn($page);
  140. $this->pageRepository->expects($this->once())->method('getById')->with($this->pageId)->willReturn($page);
  141. $page->expects($this->once())->method('setData');
  142. $this->pageRepository->expects($this->once())->method('save')->with($page);
  143. $this->dataPersistorMock->expects($this->any())
  144. ->method('clear')
  145. ->with('cms_page');
  146. $this->messageManagerMock->expects($this->once())
  147. ->method('addSuccessMessage')
  148. ->with(__('You saved the page.'));
  149. $this->resultRedirect->expects($this->atLeastOnce())->method('setPath')->with('*/*/') ->willReturnSelf();
  150. $this->assertSame($this->resultRedirect, $this->saveController->execute());
  151. }
  152. public function testSaveActionWithoutData()
  153. {
  154. $this->requestMock->expects($this->any())->method('getPostValue')->willReturn(false);
  155. $this->resultRedirect->expects($this->atLeastOnce())->method('setPath')->with('*/*/') ->willReturnSelf();
  156. $this->assertSame($this->resultRedirect, $this->saveController->execute());
  157. }
  158. public function testSaveActionNoId()
  159. {
  160. $this->requestMock->expects($this->any())->method('getPostValue')->willReturn(['page_id' => 1]);
  161. $this->requestMock->expects($this->atLeastOnce())
  162. ->method('getParam')
  163. ->willReturnMap(
  164. [
  165. ['page_id', null, 1],
  166. ['back', null, 'close'],
  167. ]
  168. );
  169. $page = $this->getMockBuilder(\Magento\Cms\Model\Page::class)
  170. ->disableOriginalConstructor()
  171. ->getMock();
  172. $this->pageFactory->expects($this->atLeastOnce())
  173. ->method('create')
  174. ->willReturn($page);
  175. $this->pageRepository->expects($this->once())
  176. ->method('getById')
  177. ->with($this->pageId)
  178. ->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException(__('Error message')));
  179. $this->messageManagerMock->expects($this->once())
  180. ->method('addErrorMessage')
  181. ->with(__('This page no longer exists.'));
  182. $this->resultRedirect->expects($this->atLeastOnce())->method('setPath')->with('*/*/') ->willReturnSelf();
  183. $this->assertSame($this->resultRedirect, $this->saveController->execute());
  184. }
  185. public function testSaveAndContinue()
  186. {
  187. $postData = [
  188. 'title' => '"><img src=y onerror=prompt(document.domain)>;',
  189. 'identifier' => 'unique_title_123',
  190. 'stores' => ['0'],
  191. 'is_active' => true,
  192. 'content' => '"><script>alert("cookie: "+document.cookie)</script>',
  193. 'back' => 'continue'
  194. ];
  195. $this->requestMock->expects($this->any())->method('getPostValue')->willReturn($postData);
  196. $this->requestMock->expects($this->atLeastOnce())
  197. ->method('getParam')
  198. ->willReturnMap(
  199. [
  200. ['page_id', null, $this->pageId],
  201. ['back', null, 'continue'],
  202. ]
  203. );
  204. $this->dataProcessorMock->expects($this->any())
  205. ->method('filter')
  206. ->willReturnArgument(0);
  207. $page = $this->getMockBuilder(\Magento\Cms\Model\Page::class)
  208. ->disableOriginalConstructor()
  209. ->getMock();
  210. $this->pageFactory->expects($this->atLeastOnce())
  211. ->method('create')
  212. ->willReturn($page);
  213. $this->pageRepository->expects($this->once())->method('getById')->with($this->pageId)->willReturn($page);
  214. $page->expects($this->once())->method('setData');
  215. $this->pageRepository->expects($this->once())->method('save')->with($page);
  216. $this->messageManagerMock->expects($this->once())
  217. ->method('addSuccessMessage')
  218. ->with(__('You saved the page.'));
  219. $this->dataPersistorMock->expects($this->any())
  220. ->method('clear')
  221. ->with('cms_page');
  222. $this->resultRedirect->expects($this->atLeastOnce())
  223. ->method('setPath')
  224. ->with('*/*/edit', ['page_id' => $this->pageId])
  225. ->willReturnSelf();
  226. $this->assertSame($this->resultRedirect, $this->saveController->execute());
  227. }
  228. public function testSaveActionThrowsException()
  229. {
  230. $this->requestMock->expects($this->any())->method('getPostValue')->willReturn(['page_id' => $this->pageId]);
  231. $this->requestMock->expects($this->atLeastOnce())
  232. ->method('getParam')
  233. ->willReturnMap(
  234. [
  235. ['page_id', null, $this->pageId],
  236. ['back', null, true],
  237. ]
  238. );
  239. $this->dataProcessorMock->expects($this->any())
  240. ->method('filter')
  241. ->willReturnArgument(0);
  242. $page = $this->getMockBuilder(\Magento\Cms\Model\Page::class)
  243. ->disableOriginalConstructor()
  244. ->getMock();
  245. $this->pageFactory->expects($this->atLeastOnce())
  246. ->method('create')
  247. ->willReturn($page);
  248. $this->pageRepository->expects($this->once())->method('getById')->with($this->pageId)->willReturn($page);
  249. $page->expects($this->once())->method('setData');
  250. $this->pageRepository->expects($this->once())->method('save')->with($page)
  251. ->willThrowException(new \Exception('Error message.'));
  252. $this->messageManagerMock->expects($this->never())
  253. ->method('addSuccessMessage');
  254. $this->messageManagerMock->expects($this->once())
  255. ->method('addExceptionMessage');
  256. $this->dataPersistorMock->expects($this->any())
  257. ->method('set')
  258. ->with('cms_page', ['page_id' => $this->pageId]);
  259. $this->resultRedirect->expects($this->atLeastOnce())
  260. ->method('setPath')
  261. ->with('*/*/edit', ['page_id' => $this->pageId])
  262. ->willReturnSelf();
  263. $this->assertSame($this->resultRedirect, $this->saveController->execute());
  264. }
  265. }