DirectiveTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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\Wysiwyg;
  7. /**
  8. * @covers \Magento\Cms\Controller\Adminhtml\Wysiwyg\Directive
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class DirectiveTest extends \PHPUnit\Framework\TestCase
  12. {
  13. const IMAGE_PATH = 'pub/media/wysiwyg/image.jpg';
  14. /**
  15. * @var \Magento\Cms\Controller\Adminhtml\Wysiwyg\Directive
  16. */
  17. protected $wysiwygDirective;
  18. /**
  19. * @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $actionContextMock;
  22. /**
  23. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $requestMock;
  26. /**
  27. * @var \Magento\Framework\Url\DecoderInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $urlDecoderMock;
  30. /**
  31. * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $objectManagerMock;
  34. /**
  35. * @var \Magento\Cms\Model\Template\Filter|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $templateFilterMock;
  38. /**
  39. * @var \Magento\Framework\Image\AdapterFactory|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $imageAdapterFactoryMock;
  42. /**
  43. * @var \Magento\Framework\Image\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $imageAdapterMock;
  46. /**
  47. * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $responseMock;
  50. /**
  51. * @var \Magento\Cms\Model\Wysiwyg\Config|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $wysiwygConfigMock;
  54. /**
  55. * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. protected $loggerMock;
  58. /**
  59. * @var \Magento\Framework\Controller\Result\RawFactory|\PHPUnit_Framework_MockObject_MockObject
  60. */
  61. protected $rawFactoryMock;
  62. /**
  63. * @var \Magento\Framework\Controller\Result\Raw|\PHPUnit_Framework_MockObject_MockObject
  64. */
  65. protected $rawMock;
  66. protected function setUp()
  67. {
  68. $this->actionContextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $this->urlDecoderMock = $this->getMockBuilder(\Magento\Framework\Url\DecoderInterface::class)
  75. ->disableOriginalConstructor()
  76. ->getMock();
  77. $this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $this->templateFilterMock = $this->getMockBuilder(\Magento\Cms\Model\Template\Filter::class)
  81. ->disableOriginalConstructor()
  82. ->getMock();
  83. $this->imageAdapterFactoryMock = $this->getMockBuilder(\Magento\Framework\Image\AdapterFactory::class)
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $this->imageAdapterMock = $this->getMockBuilder(\Magento\Framework\Image\Adapter\AdapterInterface::class)
  87. ->disableOriginalConstructor()
  88. ->setMethods(
  89. [
  90. 'getMimeType',
  91. 'getColorAt',
  92. 'getImage',
  93. 'watermark',
  94. 'refreshImageDimensions',
  95. 'checkDependencies',
  96. 'createPngFromString',
  97. 'open',
  98. 'resize',
  99. 'crop',
  100. 'save',
  101. 'rotate'
  102. ]
  103. )
  104. ->getMock();
  105. $this->responseMock = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
  106. ->disableOriginalConstructor()
  107. ->setMethods(['setHeader', 'setBody', 'sendResponse'])
  108. ->getMock();
  109. $this->wysiwygConfigMock = $this->getMockBuilder(\Magento\Cms\Model\Wysiwyg\Config::class)
  110. ->disableOriginalConstructor()
  111. ->getMock();
  112. $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
  113. ->disableOriginalConstructor()
  114. ->getMock();
  115. $this->rawFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\RawFactory::class)
  116. ->setMethods(['create'])
  117. ->disableOriginalConstructor()
  118. ->getMock();
  119. $this->rawMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Raw::class)
  120. ->disableOriginalConstructor()
  121. ->getMock();
  122. $this->actionContextMock->expects($this->any())
  123. ->method('getRequest')
  124. ->willReturn($this->requestMock);
  125. $this->actionContextMock->expects($this->any())
  126. ->method('getResponse')
  127. ->willReturn($this->responseMock);
  128. $this->actionContextMock->expects($this->any())
  129. ->method('getObjectManager')
  130. ->willReturn($this->objectManagerMock);
  131. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  132. $this->wysiwygDirective = $objectManager->getObject(
  133. \Magento\Cms\Controller\Adminhtml\Wysiwyg\Directive::class,
  134. [
  135. 'context' => $this->actionContextMock,
  136. 'urlDecoder' => $this->urlDecoderMock,
  137. 'resultRawFactory' => $this->rawFactoryMock
  138. ]
  139. );
  140. }
  141. /**
  142. * @covers \Magento\Cms\Controller\Adminhtml\Wysiwyg\Directive::execute
  143. */
  144. public function testExecute()
  145. {
  146. $mimeType = 'image/jpeg';
  147. $imageBody = 'abcdefghijklmnopqrstuvwxyz0123456789';
  148. $this->prepareExecuteTest();
  149. $this->imageAdapterMock->expects($this->once())
  150. ->method('open')
  151. ->with(self::IMAGE_PATH);
  152. $this->imageAdapterMock->expects($this->once())
  153. ->method('getMimeType')
  154. ->willReturn($mimeType);
  155. $this->rawMock->expects($this->once())
  156. ->method('setHeader')
  157. ->with('Content-Type', $mimeType)
  158. ->willReturnSelf();
  159. $this->rawMock->expects($this->once())
  160. ->method('setContents')
  161. ->with($imageBody)
  162. ->willReturnSelf();
  163. $this->imageAdapterMock->expects($this->once())
  164. ->method('getImage')
  165. ->willReturn($imageBody);
  166. $this->rawFactoryMock->expects($this->any())
  167. ->method('create')
  168. ->willReturn($this->rawMock);
  169. $this->assertSame(
  170. $this->rawMock,
  171. $this->wysiwygDirective->execute()
  172. );
  173. }
  174. /**
  175. * @covers \Magento\Cms\Controller\Adminhtml\Wysiwyg\Directive::execute
  176. */
  177. public function testExecuteException()
  178. {
  179. $exception = new \Exception('epic fail');
  180. $placeholderPath = 'pub/static/adminhtml/Magento/backend/en_US/Magento_Cms/images/wysiwyg_skin_image.png';
  181. $mimeType = 'image/png';
  182. $imageBody = '0123456789abcdefghijklmnopqrstuvwxyz';
  183. $this->prepareExecuteTest();
  184. $this->imageAdapterMock->expects($this->at(0))
  185. ->method('open')
  186. ->with(self::IMAGE_PATH)
  187. ->willThrowException($exception);
  188. $this->wysiwygConfigMock->expects($this->once())
  189. ->method('getSkinImagePlaceholderPath')
  190. ->willReturn($placeholderPath);
  191. $this->imageAdapterMock->expects($this->at(1))
  192. ->method('open')
  193. ->with($placeholderPath);
  194. $this->imageAdapterMock->expects($this->once())
  195. ->method('getMimeType')
  196. ->willReturn($mimeType);
  197. $this->rawMock->expects($this->once())
  198. ->method('setHeader')
  199. ->with('Content-Type', $mimeType)
  200. ->willReturnSelf();
  201. $this->rawMock->expects($this->once())
  202. ->method('setContents')
  203. ->with($imageBody)
  204. ->willReturnSelf();
  205. $this->imageAdapterMock->expects($this->once())
  206. ->method('getImage')
  207. ->willReturn($imageBody);
  208. $this->loggerMock->expects($this->once())
  209. ->method('critical')
  210. ->with($exception);
  211. $this->rawFactoryMock->expects($this->any())
  212. ->method('create')
  213. ->willReturn($this->rawMock);
  214. $this->assertSame(
  215. $this->rawMock,
  216. $this->wysiwygDirective->execute()
  217. );
  218. }
  219. protected function prepareExecuteTest()
  220. {
  221. $directiveParam = 'e3ttZWRpYSB1cmw9Ind5c2l3eWcvYnVubnkuanBnIn19';
  222. $directive = '{{media url="wysiwyg/image.jpg"}}';
  223. $this->requestMock->expects($this->once())
  224. ->method('getParam')
  225. ->with('___directive')
  226. ->willReturn($directiveParam);
  227. $this->urlDecoderMock->expects($this->once())
  228. ->method('decode')
  229. ->with($directiveParam)
  230. ->willReturn($directive);
  231. $this->objectManagerMock->expects($this->once())
  232. ->method('create')
  233. ->with(\Magento\Cms\Model\Template\Filter::class)
  234. ->willReturn($this->templateFilterMock);
  235. $this->templateFilterMock->expects($this->once())
  236. ->method('filter')
  237. ->with($directive)
  238. ->willReturn(self::IMAGE_PATH);
  239. $this->objectManagerMock->expects($this->any())
  240. ->method('get')
  241. ->willReturnMap(
  242. [
  243. [\Magento\Framework\Image\AdapterFactory::class, $this->imageAdapterFactoryMock],
  244. [\Magento\Cms\Model\Wysiwyg\Config::class, $this->wysiwygConfigMock],
  245. [\Psr\Log\LoggerInterface::class, $this->loggerMock]
  246. ]
  247. );
  248. $this->imageAdapterFactoryMock->expects($this->once())
  249. ->method('create')
  250. ->willReturn($this->imageAdapterMock);
  251. }
  252. }