ServiceTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Test for uploader service
  8. */
  9. namespace Magento\Theme\Test\Unit\Model\Uploader;
  10. use Magento\Framework\Convert\DataSize;
  11. class ServiceTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Theme\Model\Uploader\Service
  15. */
  16. protected $_service;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\MediaStorage\Model\File\Uploader
  19. */
  20. protected $_uploader;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\MediaStorage\Model\File\UploaderFactory
  23. */
  24. protected $_uploaderFactory;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\File\Size
  27. */
  28. protected $_fileSizeMock;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Convert\DataSize
  31. */
  32. protected $dataSize;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Filesystem
  35. */
  36. protected $_filesystemMock;
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Filesystem\Directory\Read
  39. */
  40. protected $_directoryMock;
  41. /**
  42. * @var int
  43. */
  44. const MB_MULTIPLIER = 1048576;
  45. protected function setUp()
  46. {
  47. $this->_uploader = $this->createMock(\Magento\MediaStorage\Model\File\Uploader::class);
  48. $this->dataSize = new DataSize();
  49. $this->_uploaderFactory = $this->createPartialMock(
  50. \Magento\MediaStorage\Model\File\UploaderFactory::class,
  51. ['create']
  52. );
  53. $this->_uploaderFactory->expects($this->any())->method('create')->will($this->returnValue($this->_uploader));
  54. $this->_directoryMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Read::class);
  55. $this->_filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
  56. $this->_filesystemMock->expects(
  57. $this->any()
  58. )->method(
  59. 'getDirectoryRead'
  60. )->will(
  61. $this->returnValue($this->_directoryMock)
  62. );
  63. /** @var $service \Magento\Theme\Model\Uploader\Service */
  64. $this->_fileSizeMock = $this->getMockBuilder(
  65. \Magento\Framework\File\Size::class
  66. )->setMethods(
  67. ['getMaxFileSize']
  68. )->disableOriginalConstructor()->getMock();
  69. $this->_fileSizeMock->expects(
  70. $this->any()
  71. )->method(
  72. 'getMaxFileSize'
  73. )->will(
  74. $this->returnValue(600 * self::MB_MULTIPLIER)
  75. );
  76. }
  77. protected function tearDown()
  78. {
  79. $this->_service = null;
  80. $this->_uploader = null;
  81. $this->_fileSizeMock = null;
  82. $this->_filesystemMock = null;
  83. $this->_uploaderFactory = null;
  84. }
  85. public function testUploadLimitNotConfigured()
  86. {
  87. $this->_service = new \Magento\Theme\Model\Uploader\Service(
  88. $this->_filesystemMock,
  89. $this->_fileSizeMock,
  90. $this->dataSize,
  91. $this->_uploaderFactory
  92. );
  93. $this->assertEquals(600 * self::MB_MULTIPLIER, $this->_service->getJsUploadMaxSize());
  94. $this->assertEquals(600 * self::MB_MULTIPLIER, $this->_service->getCssUploadMaxSize());
  95. }
  96. public function testGetCssUploadMaxSize()
  97. {
  98. $this->_service = new \Magento\Theme\Model\Uploader\Service(
  99. $this->_filesystemMock,
  100. $this->_fileSizeMock,
  101. $this->dataSize,
  102. $this->_uploaderFactory,
  103. ['css' => '5M']
  104. );
  105. $this->assertEquals(5 * self::MB_MULTIPLIER, $this->_service->getCssUploadMaxSize());
  106. }
  107. public function testGetJsUploadMaxSize()
  108. {
  109. $this->_service = new \Magento\Theme\Model\Uploader\Service(
  110. $this->_filesystemMock,
  111. $this->_fileSizeMock,
  112. $this->dataSize,
  113. $this->_uploaderFactory,
  114. ['js' => '3M']
  115. );
  116. $this->assertEquals(3 * self::MB_MULTIPLIER, $this->_service->getJsUploadMaxSize());
  117. }
  118. public function testGetFileContent()
  119. {
  120. $fileName = 'file.name';
  121. $this->_directoryMock->expects(
  122. $this->once()
  123. )->method(
  124. 'getRelativePath'
  125. )->with(
  126. $fileName
  127. )->will(
  128. $this->returnValue($fileName)
  129. );
  130. $this->_directoryMock->expects(
  131. $this->once()
  132. )->method(
  133. 'readFile'
  134. )->with(
  135. $fileName
  136. )->will(
  137. $this->returnValue('content from my file')
  138. );
  139. $this->_service = new \Magento\Theme\Model\Uploader\Service(
  140. $this->_filesystemMock,
  141. $this->_fileSizeMock,
  142. $this->dataSize,
  143. $this->_uploaderFactory,
  144. ['js' => '3M']
  145. );
  146. $this->assertEquals('content from my file', $this->_service->getFileContent($fileName));
  147. }
  148. public function testUploadCssFile()
  149. {
  150. $fileName = 'file.name';
  151. $this->_service = new \Magento\Theme\Model\Uploader\Service(
  152. $this->_filesystemMock,
  153. $this->_fileSizeMock,
  154. $this->dataSize,
  155. $this->_uploaderFactory,
  156. ['css' => '3M']
  157. );
  158. $this->_directoryMock->expects(
  159. $this->once()
  160. )->method(
  161. 'getRelativePath'
  162. )->with(
  163. $fileName
  164. )->will(
  165. $this->returnValue($fileName)
  166. );
  167. $this->_directoryMock->expects(
  168. $this->once()
  169. )->method(
  170. 'readFile'
  171. )->with(
  172. $fileName
  173. )->will(
  174. $this->returnValue('content')
  175. );
  176. $this->_uploader->expects(
  177. $this->once()
  178. )->method(
  179. 'validateFile'
  180. )->will(
  181. $this->returnValue(['name' => $fileName, 'tmp_name' => $fileName])
  182. );
  183. $this->assertEquals(
  184. ['content' => 'content', 'filename' => $fileName],
  185. $this->_service->uploadCssFile($fileName)
  186. );
  187. }
  188. /**
  189. * @expectedException \Magento\Framework\Exception\LocalizedException
  190. */
  191. public function testUploadInvalidCssFile()
  192. {
  193. $fileName = 'file.name';
  194. $this->_uploader->expects(
  195. $this->once()
  196. )->method(
  197. 'getFileSize'
  198. )->will(
  199. $this->returnValue(30 * self::MB_MULTIPLIER)
  200. );
  201. $this->_service = new \Magento\Theme\Model\Uploader\Service(
  202. $this->_filesystemMock,
  203. $this->_fileSizeMock,
  204. $this->dataSize,
  205. $this->_uploaderFactory,
  206. ['css' => '10M']
  207. );
  208. $this->_service->uploadCssFile($fileName);
  209. }
  210. public function testUploadJsFile()
  211. {
  212. $fileName = 'file.name';
  213. $this->_fileSizeMock->expects(
  214. $this->once()
  215. )->method(
  216. 'getMaxFileSize'
  217. )->will(
  218. $this->returnValue(600 * self::MB_MULTIPLIER)
  219. );
  220. $this->_service = new \Magento\Theme\Model\Uploader\Service(
  221. $this->_filesystemMock,
  222. $this->_fileSizeMock,
  223. $this->dataSize,
  224. $this->_uploaderFactory,
  225. ['js' => '500M']
  226. );
  227. $this->_directoryMock->expects(
  228. $this->once()
  229. )->method(
  230. 'getRelativePath'
  231. )->with(
  232. $fileName
  233. )->will(
  234. $this->returnValue($fileName)
  235. );
  236. $this->_directoryMock->expects(
  237. $this->once()
  238. )->method(
  239. 'readFile'
  240. )->with(
  241. $fileName
  242. )->will(
  243. $this->returnValue('content')
  244. );
  245. $this->_uploader->expects(
  246. $this->once()
  247. )->method(
  248. 'validateFile'
  249. )->will(
  250. $this->returnValue(['name' => $fileName, 'tmp_name' => $fileName])
  251. );
  252. $this->_uploader->expects($this->once())->method('getFileSize')->will($this->returnValue('499'));
  253. $this->assertEquals(
  254. ['content' => 'content', 'filename' => $fileName],
  255. $this->_service->uploadJsFile($fileName)
  256. );
  257. }
  258. /**
  259. * @expectedException \Magento\Framework\Exception\LocalizedException
  260. */
  261. public function testUploadInvalidJsFile()
  262. {
  263. $fileName = 'file.name';
  264. $this->_service = new \Magento\Theme\Model\Uploader\Service(
  265. $this->_filesystemMock,
  266. $this->_fileSizeMock,
  267. $this->dataSize,
  268. $this->_uploaderFactory,
  269. ['js' => '100M']
  270. );
  271. $this->_uploader->expects(
  272. $this->once()
  273. )->method(
  274. 'getFileSize'
  275. )->will(
  276. $this->returnValue(499 * self::MB_MULTIPLIER)
  277. );
  278. $this->_service->uploadJsFile($fileName);
  279. }
  280. }