SaveTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 SaveTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $requestMock;
  16. /**
  17. * @var \Magento\Framework\App\Request\DataPersistorInterface|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $dataPersistorMock;
  20. /**
  21. * @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $resultRedirectFactory;
  24. /**
  25. * @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $resultRedirect;
  28. /**
  29. * @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $contextMock;
  32. /**
  33. * @var \Magento\Framework\ObjectManager\ObjectManager|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $objectManagerMock;
  36. /**
  37. * @var \Magento\Cms\Model\Block|\PHPUnit_Framework_MockObject_MockObject $blockMock
  38. */
  39. protected $blockMock;
  40. /**
  41. * @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $messageManagerMock;
  44. /**
  45. * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $eventManagerMock;
  48. /**
  49. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  50. */
  51. protected $objectManager;
  52. /**
  53. * @var \Magento\Cms\Controller\Adminhtml\Block\Save
  54. */
  55. protected $saveController;
  56. /**
  57. * @var \Magento\Cms\Model\BlockFactory|\PHPUnit_Framework_MockObject_MockObject
  58. */
  59. private $blockFactory;
  60. /**
  61. * @var \Magento\Cms\Api\BlockRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  62. */
  63. private $blockRepository;
  64. /**
  65. * @var int
  66. */
  67. protected $blockId = 1;
  68. protected function setUp()
  69. {
  70. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  71. $this->contextMock = $this->createMock(\Magento\Backend\App\Action\Context::class);
  72. $this->resultRedirectFactory = $this->getMockBuilder(\Magento\Backend\Model\View\Result\RedirectFactory::class)
  73. ->disableOriginalConstructor()
  74. ->setMethods(['create'])
  75. ->getMock();
  76. $this->resultRedirect = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. $this->resultRedirectFactory->expects($this->atLeastOnce())
  80. ->method('create')
  81. ->willReturn($this->resultRedirect);
  82. $this->dataPersistorMock = $this->getMockBuilder(\Magento\Framework\App\Request\DataPersistorInterface::class)
  83. ->getMock();
  84. $this->requestMock = $this->getMockForAbstractClass(
  85. \Magento\Framework\App\RequestInterface::class,
  86. [],
  87. '',
  88. false,
  89. true,
  90. true,
  91. ['getParam', 'getPostValue']
  92. );
  93. $this->blockMock = $this->getMockBuilder(
  94. \Magento\Cms\Model\Block::class
  95. )->disableOriginalConstructor()->getMock();
  96. $this->messageManagerMock = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
  97. $this->eventManagerMock = $this->getMockForAbstractClass(
  98. \Magento\Framework\Event\ManagerInterface::class,
  99. [],
  100. '',
  101. false,
  102. true,
  103. true,
  104. ['dispatch']
  105. );
  106. $this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManager\ObjectManager::class)
  107. ->disableOriginalConstructor()
  108. ->setMethods(['get', 'create'])
  109. ->getMock();
  110. $this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock);
  111. $this->contextMock->expects($this->any())->method('getObjectManager')->willReturn($this->objectManagerMock);
  112. $this->contextMock->expects($this->any())->method('getMessageManager')->willReturn($this->messageManagerMock);
  113. $this->contextMock->expects($this->any())->method('getEventManager')->willReturn($this->eventManagerMock);
  114. $this->contextMock->expects($this->any())
  115. ->method('getResultRedirectFactory')
  116. ->willReturn($this->resultRedirectFactory);
  117. $this->blockFactory = $this->getMockBuilder(\Magento\Cms\Model\BlockFactory::class)
  118. ->disableOriginalConstructor()
  119. ->setMethods(['create'])
  120. ->getMock();
  121. $this->blockRepository = $this->getMockBuilder(\Magento\Cms\Api\BlockRepositoryInterface::class)
  122. ->disableOriginalConstructor()
  123. ->getMockForAbstractClass();
  124. $this->saveController = $this->objectManager->getObject(
  125. \Magento\Cms\Controller\Adminhtml\Block\Save::class,
  126. [
  127. 'context' => $this->contextMock,
  128. 'dataPersistor' => $this->dataPersistorMock,
  129. 'blockFactory' => $this->blockFactory,
  130. 'blockRepository' => $this->blockRepository,
  131. ]
  132. );
  133. }
  134. public function testSaveAction()
  135. {
  136. $postData = [
  137. 'title' => '"><img src=y onerror=prompt(document.domain)>;',
  138. 'identifier' => 'unique_title_123',
  139. 'stores' => ['0'],
  140. 'is_active' => true,
  141. 'content' => '"><script>alert("cookie: "+document.cookie)</script>',
  142. 'back' => 'continue'
  143. ];
  144. $this->requestMock->expects($this->any())->method('getPostValue')->willReturn($postData);
  145. $this->requestMock->expects($this->atLeastOnce())
  146. ->method('getParam')
  147. ->willReturnMap(
  148. [
  149. ['block_id', null, 1],
  150. ['back', null, 'continue'],
  151. ]
  152. );
  153. $this->blockFactory->expects($this->atLeastOnce())
  154. ->method('create')
  155. ->willReturn($this->blockMock);
  156. $this->blockRepository->expects($this->once())
  157. ->method('getById')
  158. ->with($this->blockId)
  159. ->willReturn($this->blockMock);
  160. $this->blockMock->expects($this->once())->method('setData');
  161. $this->blockRepository->expects($this->once())->method('save')->with($this->blockMock);
  162. $this->dataPersistorMock->expects($this->any())
  163. ->method('clear')
  164. ->with('cms_block');
  165. $this->messageManagerMock->expects($this->once())
  166. ->method('addSuccessMessage')
  167. ->with(__('You saved the block.'));
  168. $this->resultRedirect->expects($this->atLeastOnce())->method('setPath')->with('*/*/edit') ->willReturnSelf();
  169. $this->assertSame($this->resultRedirect, $this->saveController->execute());
  170. }
  171. public function testSaveActionWithoutData()
  172. {
  173. $this->requestMock->expects($this->any())->method('getPostValue')->willReturn(false);
  174. $this->resultRedirect->expects($this->atLeastOnce())->method('setPath')->with('*/*/') ->willReturnSelf();
  175. $this->assertSame($this->resultRedirect, $this->saveController->execute());
  176. }
  177. public function testSaveActionNoId()
  178. {
  179. $postData = [
  180. 'block_id' => 1,
  181. 'back' => 'continue'
  182. ];
  183. $this->requestMock->expects($this->any())->method('getPostValue')->willReturn($postData);
  184. $this->requestMock->expects($this->atLeastOnce())
  185. ->method('getParam')
  186. ->willReturnMap(
  187. [
  188. ['block_id', null, 1],
  189. ['back', null, false],
  190. ]
  191. );
  192. $this->blockFactory->expects($this->atLeastOnce())
  193. ->method('create')
  194. ->willReturn($this->blockMock);
  195. $this->blockRepository->expects($this->once())
  196. ->method('getById')
  197. ->with($this->blockId)
  198. ->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException(__('Error message')));
  199. $this->messageManagerMock->expects($this->once())
  200. ->method('addErrorMessage')
  201. ->with(__('This block no longer exists.'));
  202. $this->resultRedirect->expects($this->atLeastOnce())->method('setPath')->with('*/*/') ->willReturnSelf();
  203. $this->assertSame($this->resultRedirect, $this->saveController->execute());
  204. }
  205. public function testSaveAndDuplicate()
  206. {
  207. $postData = [
  208. 'title' => 'unique_title_123',
  209. 'identifier' => 'unique_title_123',
  210. 'stores' => ['0'],
  211. 'is_active' => true,
  212. 'content' => '"><script>alert("cookie: "+document.cookie)</script>',
  213. 'back' => 'duplicate'
  214. ];
  215. $this->requestMock->expects($this->any())->method('getPostValue')->willReturn($postData);
  216. $this->requestMock->expects($this->atLeastOnce())
  217. ->method('getParam')
  218. ->willReturnMap(
  219. [
  220. ['block_id', null, 1],
  221. ['back', null, true],
  222. ]
  223. );
  224. $this->blockFactory->expects($this->at(0))
  225. ->method('create')
  226. ->willReturn($this->blockMock);
  227. $duplicateBlockMock = $this->getMockBuilder(
  228. \Magento\Cms\Model\Block::class
  229. )->disableOriginalConstructor()->getMock();
  230. $this->blockFactory->expects($this->at(1))
  231. ->method('create')
  232. ->willReturn($duplicateBlockMock);
  233. $duplicateBlockMock->expects($this->atLeastOnce())
  234. ->method('setId')
  235. ->with(null)
  236. ->willReturnSelf();
  237. $duplicateBlockMock->expects($this->atLeastOnce())
  238. ->method('setIdentifier')
  239. ->willReturnSelf();
  240. $duplicateBlockMock->expects($this->atLeastOnce())
  241. ->method('setIsActive')
  242. ->with(0)
  243. ->willReturnSelf();
  244. $duplicateBlockMock->expects($this->atLeastOnce())
  245. ->method('getId')
  246. ->willReturn(1);
  247. $this->blockRepository->expects($this->once())
  248. ->method('getById')
  249. ->with($this->blockId)
  250. ->willReturn($this->blockMock);
  251. $this->blockMock->expects($this->any())->method('setData');
  252. $this->blockRepository->expects($this->at(1))->method('save')->with($this->blockMock);
  253. $this->blockRepository->expects($this->at(2))->method('save')->with($duplicateBlockMock);
  254. $this->messageManagerMock->expects($this->at(0))
  255. ->method('addSuccessMessage')
  256. ->with(__('You saved the block.'));
  257. $this->messageManagerMock->expects($this->at(1))
  258. ->method('addSuccessMessage')
  259. ->with(__('You duplicated the block.'));
  260. $this->dataPersistorMock->expects($this->any())
  261. ->method('clear')
  262. ->with('cms_block');
  263. $this->resultRedirect->expects($this->atLeastOnce())
  264. ->method('setPath')
  265. ->with('*/*/edit', ['block_id' => $this->blockId])
  266. ->willReturnSelf();
  267. $this->assertSame($this->resultRedirect, $this->saveController->execute());
  268. }
  269. public function testSaveAndClose()
  270. {
  271. $postData = [
  272. 'title' => '"><img src=y onerror=prompt(document.domain)>;',
  273. 'identifier' => 'unique_title_123',
  274. 'stores' => ['0'],
  275. 'is_active' => true,
  276. 'content' => '"><script>alert("cookie: "+document.cookie)</script>',
  277. 'back' => 'close'
  278. ];
  279. $this->requestMock->expects($this->any())->method('getPostValue')->willReturn($postData);
  280. $this->requestMock->expects($this->atLeastOnce())
  281. ->method('getParam')
  282. ->willReturnMap(
  283. [
  284. ['block_id', null, 1],
  285. ['back', null, 'close'],
  286. ]
  287. );
  288. $this->blockFactory->expects($this->atLeastOnce())
  289. ->method('create')
  290. ->willReturn($this->blockMock);
  291. $this->blockRepository->expects($this->atLeastOnce())
  292. ->method('getById')
  293. ->with($this->blockId)
  294. ->willReturn($this->blockMock);
  295. $this->blockMock->expects($this->atLeastOnce())->method('setData');
  296. $this->blockRepository->expects($this->once())->method('save')->with($this->blockMock);
  297. $this->dataPersistorMock->expects($this->any())
  298. ->method('clear')
  299. ->with('cms_block');
  300. $this->messageManagerMock->expects($this->atLeastOnce())
  301. ->method('addSuccessMessage')
  302. ->with(__('You saved the block.'));
  303. $this->resultRedirect->expects($this->atLeastOnce())->method('setPath')->with('*/*/')->willReturnSelf();
  304. $this->assertSame($this->resultRedirect, $this->saveController->execute());
  305. }
  306. public function testSaveActionThrowsException()
  307. {
  308. $postData = [
  309. 'title' => '"><img src=y onerror=prompt(document.domain)>;',
  310. 'identifier' => 'unique_title_123',
  311. 'stores' => ['0'],
  312. 'is_active' => true,
  313. 'content' => '"><script>alert("cookie: "+document.cookie)</script>',
  314. 'back' => 'continue'
  315. ];
  316. $this->requestMock->expects($this->any())->method('getPostValue')->willReturn($postData);
  317. $this->requestMock->expects($this->atLeastOnce())
  318. ->method('getParam')
  319. ->willReturnMap(
  320. [
  321. ['block_id', null, 1],
  322. ['back', null, true],
  323. ]
  324. );
  325. $this->blockFactory->expects($this->atLeastOnce())
  326. ->method('create')
  327. ->willReturn($this->blockMock);
  328. $this->blockRepository->expects($this->once())
  329. ->method('getById')
  330. ->with($this->blockId)
  331. ->willReturn($this->blockMock);
  332. $this->blockMock->expects($this->once())->method('setData');
  333. $this->blockRepository->expects($this->once())->method('save')
  334. ->with($this->blockMock)
  335. ->willThrowException(new \Exception('Error message.'));
  336. $this->messageManagerMock->expects($this->never())
  337. ->method('addSuccessMessage');
  338. $this->messageManagerMock->expects($this->once())
  339. ->method('addExceptionMessage');
  340. $this->dataPersistorMock->expects($this->any())
  341. ->method('set')
  342. ->with('cms_block', array_merge($postData, ['block_id' => null]));
  343. $this->resultRedirect->expects($this->atLeastOnce())
  344. ->method('setPath')
  345. ->with('*/*/edit', ['block_id' => $this->blockId])
  346. ->willReturnSelf();
  347. $this->assertSame($this->resultRedirect, $this->saveController->execute());
  348. }
  349. }