ViewfileTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Controller\Adminhtml\Index;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class ViewfileTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\Controller\Result\RawFactory|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $resultRawFactoryMock;
  16. /**
  17. * @var \Magento\Framework\Controller\Result\Raw|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $resultRawMock;
  20. /**
  21. * @var \Magento\Framework\Url\DecoderInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $urlDecoderMock;
  24. /**
  25. * @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $contextMock;
  28. /**
  29. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  30. */
  31. protected $objectManager;
  32. /**
  33. * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $objectManagerMock;
  36. /**
  37. * @var \Magento\MediaStorage\Helper\File\Storage|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $storage;
  40. /**
  41. * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $fileSystemMock;
  44. /**
  45. * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $responseMock;
  48. /**
  49. * @var \Magento\Framework\Filesystem\Directory\ReadInterface|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. protected $directoryMock;
  52. /**
  53. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  54. */
  55. protected $requestMock;
  56. protected function setUp()
  57. {
  58. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  59. $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  60. $this->responseMock = $this->createMock(\Magento\Framework\App\ResponseInterface::class);
  61. $this->directoryMock = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
  62. $this->fileSystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
  63. $this->storage = $this->createMock(\Magento\MediaStorage\Helper\File\Storage::class);
  64. $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  65. $this->contextMock = $this->createMock(\Magento\Backend\App\Action\Context::class);
  66. $this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock);
  67. $this->contextMock->expects($this->any())->method('getResponse')->willReturn($this->responseMock);
  68. $this->contextMock->expects($this->any())->method('getObjectManager')->willReturn($this->objectManagerMock);
  69. $this->urlDecoderMock = $this->createMock(\Magento\Framework\Url\DecoderInterface::class);
  70. $this->resultRawMock = $this->createMock(\Magento\Framework\Controller\Result\Raw::class);
  71. $this->resultRawFactoryMock = $this->createPartialMock(
  72. \Magento\Framework\Controller\Result\RawFactory::class,
  73. ['create']
  74. );
  75. }
  76. /**
  77. * @throws \Magento\Framework\Exception\NotFoundException
  78. * @expectedException \Magento\Framework\Exception\NotFoundException
  79. */
  80. public function testExecuteNoParamsShouldThrowException()
  81. {
  82. /** @var \Magento\Customer\Controller\Adminhtml\Index\Viewfile $controller */
  83. $controller = $this->objectManager->getObject(\Magento\Customer\Controller\Adminhtml\Index\Viewfile::class);
  84. $controller->execute();
  85. }
  86. public function testExecuteParamFile()
  87. {
  88. $decodedFile = 'decoded_file';
  89. $file = 'file';
  90. $fileName = 'customer/' . $file;
  91. $path = 'path';
  92. $this->requestMock->expects($this->atLeastOnce())->method('getParam')->with('file')->willReturn($decodedFile);
  93. $this->directoryMock->expects($this->once())->method('getAbsolutePath')->with($fileName)->willReturn($path);
  94. $this->fileSystemMock->expects($this->once())->method('getDirectoryRead')
  95. ->with(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)
  96. ->willReturn($this->directoryMock);
  97. $this->storage->expects($this->once())->method('processStorageFile')->with($path)->willReturn(true);
  98. $this->objectManagerMock->expects($this->any())->method('get')
  99. ->willReturnMap(
  100. [
  101. [\Magento\Framework\Filesystem::class, $this->fileSystemMock],
  102. [\Magento\MediaStorage\Helper\File\Storage::class, $this->storage]
  103. ]
  104. );
  105. $this->urlDecoderMock->expects($this->once())->method('decode')->with($decodedFile)->willReturn($file);
  106. $fileResponse = $this->createMock(\Magento\Framework\App\ResponseInterface::class);
  107. $fileFactoryMock = $this->createMock(\Magento\Framework\App\Response\Http\FileFactory::class);
  108. $fileFactoryMock->expects($this->once())->method('create')->with(
  109. $path,
  110. ['type' => 'filename', 'value' => $fileName],
  111. \Magento\Framework\App\Filesystem\DirectoryList::MEDIA
  112. )->willReturn($fileResponse);
  113. /** @var \Magento\Customer\Controller\Adminhtml\Index\Viewfile $controller */
  114. $controller = $this->objectManager->getObject(
  115. \Magento\Customer\Controller\Adminhtml\Index\Viewfile::class,
  116. [
  117. 'context' => $this->contextMock,
  118. 'urlDecoder' => $this->urlDecoderMock,
  119. 'fileFactory' => $fileFactoryMock
  120. ]
  121. );
  122. $controller->execute();
  123. }
  124. public function testExecuteGetParamImage()
  125. {
  126. $decodedFile = 'decoded_file';
  127. $file = 'file';
  128. $fileName = 'customer/' . $file;
  129. $path = 'path';
  130. $stat = ['size' => 10, 'mtime' => 10];
  131. $this->requestMock->expects($this->any())->method('getParam')
  132. ->willReturnMap([['file', null, null], ['image', null, $decodedFile]]);
  133. $this->directoryMock->expects($this->once())->method('getAbsolutePath')->with($fileName)->willReturn($path);
  134. $this->directoryMock->expects($this->once())->method('stat')->with($fileName)->willReturn($stat);
  135. $this->fileSystemMock->expects($this->once())->method('getDirectoryRead')
  136. ->with(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)
  137. ->willReturn($this->directoryMock);
  138. $this->storage->expects($this->once())->method('processStorageFile')->with($path)->willReturn(true);
  139. $this->objectManagerMock->expects($this->any())->method('get')
  140. ->willReturnMap(
  141. [
  142. [\Magento\Framework\Filesystem::class, $this->fileSystemMock],
  143. [\Magento\MediaStorage\Helper\File\Storage::class, $this->storage]
  144. ]
  145. );
  146. $this->urlDecoderMock->expects($this->once())->method('decode')->with($decodedFile)->willReturn($file);
  147. $this->resultRawMock->expects($this->once())->method('setHttpResponseCode')->with(200)->willReturnSelf();
  148. $this->resultRawMock->expects($this->any())->method('setHeader')
  149. ->willReturnMap(
  150. [
  151. ['Pragma', 'public', true, $this->resultRawMock],
  152. ['Content-type', 'application/octet-stream', true, $this->resultRawMock],
  153. ['Content-Length', $stat['size'], false, $this->resultRawMock],
  154. ['Pragma', 'public', true, $this->resultRawMock],
  155. ]
  156. );
  157. $this->resultRawFactoryMock = $this->createPartialMock(
  158. \Magento\Framework\Controller\Result\RawFactory::class,
  159. ['create']
  160. );
  161. $this->resultRawFactoryMock->expects($this->once())->method('create')->willReturn($this->resultRawMock);
  162. /** @var \Magento\Customer\Controller\Adminhtml\Index\Viewfile $controller */
  163. $controller = $this->objectManager->getObject(
  164. \Magento\Customer\Controller\Adminhtml\Index\Viewfile::class,
  165. [
  166. 'context' => $this->contextMock,
  167. 'urlDecoder' => $this->urlDecoderMock,
  168. 'resultRawFactory' => $this->resultRawFactoryMock
  169. ]
  170. );
  171. $this->assertSame($this->resultRawMock, $controller->execute());
  172. }
  173. /**
  174. * @expectedException \Magento\Framework\Exception\NotFoundException
  175. * @expectedExceptionMessage Page not found.
  176. */
  177. public function testExecuteInvalidFile()
  178. {
  179. $file = '../../../app/etc/env.php';
  180. $decodedFile = base64_encode($file);
  181. $fileName = 'customer/' . $file;
  182. $path = 'path';
  183. $this->requestMock->expects($this->atLeastOnce())->method('getParam')->with('file')->willReturn($decodedFile);
  184. $this->directoryMock->expects($this->once())->method('getAbsolutePath')->with($fileName)->willReturn($path);
  185. $this->fileSystemMock->expects($this->once())->method('getDirectoryRead')
  186. ->with(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)
  187. ->willReturn($this->directoryMock);
  188. $this->storage->expects($this->once())->method('processStorageFile')->with($path)->willReturn(false);
  189. $this->objectManagerMock->expects($this->any())->method('get')
  190. ->willReturnMap(
  191. [
  192. [\Magento\Framework\Filesystem::class, $this->fileSystemMock],
  193. [\Magento\MediaStorage\Helper\File\Storage::class, $this->storage],
  194. ]
  195. );
  196. $this->urlDecoderMock->expects($this->once())->method('decode')->with($decodedFile)->willReturn($file);
  197. $fileFactoryMock = $this->createMock(
  198. \Magento\Framework\App\Response\Http\FileFactory::class,
  199. [],
  200. [],
  201. '',
  202. false
  203. );
  204. $controller = $this->objectManager->getObject(
  205. \Magento\Customer\Controller\Adminhtml\Index\Viewfile::class,
  206. [
  207. 'context' => $this->contextMock,
  208. 'urlDecoder' => $this->urlDecoderMock,
  209. 'fileFactory' => $fileFactoryMock,
  210. ]
  211. );
  212. $controller->execute();
  213. }
  214. }