StorageTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Storage model test
  8. */
  9. namespace Magento\Theme\Test\Unit\Model\Wysiwyg;
  10. class StorageTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var string
  14. */
  15. protected $_storageRoot;
  16. /**
  17. * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $_filesystem;
  20. /**
  21. * @var \Magento\Theme\Helper\Storage|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $_helperStorage;
  24. /**
  25. * @var \Magento\Framework\ObjectManagerInterface
  26. */
  27. protected $_objectManager;
  28. /**
  29. * @var null|\Magento\Theme\Model\Wysiwyg\Storage
  30. */
  31. protected $_storageModel;
  32. /**
  33. * @var \Magento\Framework\Image\AdapterFactory|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $_imageFactory;
  36. /**
  37. * @var \Magento\Framework\Filesystem\Directory\Write|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $directoryWrite;
  40. /**
  41. * @var \Magento\Framework\Url\EncoderInterface|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $urlEncoder;
  44. /**
  45. * @var \Magento\Framework\Url\DecoderInterface|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $urlDecoder;
  48. protected function setUp()
  49. {
  50. $this->_filesystem = $this->createMock(\Magento\Framework\Filesystem::class);
  51. $this->_helperStorage = $this->createPartialMock(
  52. \Magento\Theme\Helper\Storage::class,
  53. [
  54. 'urlEncode',
  55. 'getStorageType',
  56. 'getCurrentPath',
  57. 'getStorageRoot',
  58. 'getShortFilename',
  59. 'getSession',
  60. 'convertPathToId',
  61. 'getRequestParams'
  62. ]
  63. );
  64. $this->_objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  65. $this->_imageFactory = $this->createMock(\Magento\Framework\Image\AdapterFactory::class);
  66. $this->directoryWrite = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
  67. $this->urlEncoder = $this->createPartialMock(\Magento\Framework\Url\EncoderInterface::class, ['encode']);
  68. $this->urlDecoder = $this->createPartialMock(\Magento\Framework\Url\DecoderInterface::class, ['decode']);
  69. $this->_filesystem->expects(
  70. $this->once()
  71. )->method(
  72. 'getDirectoryWrite'
  73. )->will(
  74. $this->returnValue($this->directoryWrite)
  75. );
  76. $this->_storageModel = new \Magento\Theme\Model\Wysiwyg\Storage(
  77. $this->_filesystem,
  78. $this->_helperStorage,
  79. $this->_objectManager,
  80. $this->_imageFactory,
  81. $this->urlEncoder,
  82. $this->urlDecoder
  83. );
  84. $this->_storageRoot = '/root';
  85. }
  86. protected function tearDown()
  87. {
  88. $this->_filesystem = null;
  89. $this->_helperStorage = null;
  90. $this->_objectManager = null;
  91. $this->_storageModel = null;
  92. $this->_storageRoot = null;
  93. }
  94. /**
  95. * cover \Magento\Theme\Model\Wysiwyg\Storage::_createThumbnail
  96. * cover \Magento\Theme\Model\Wysiwyg\Storage::uploadFile
  97. */
  98. public function testUploadFile()
  99. {
  100. $uploader = $this->_prepareUploader();
  101. $uploader->expects($this->once())->method('save')->will($this->returnValue(['not_empty', 'path' => 'absPath']));
  102. $this->_helperStorage->expects(
  103. $this->any()
  104. )->method(
  105. 'getStorageType'
  106. )->will(
  107. $this->returnValue(\Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE)
  108. );
  109. /** Prepare filesystem */
  110. $this->directoryWrite->expects($this->any())->method('isFile')->will($this->returnValue(true));
  111. $this->directoryWrite->expects($this->once())->method('isReadable')->will($this->returnValue(true));
  112. /** Prepare image */
  113. $image = $this->createMock(\Magento\Framework\Image\Adapter\Gd2::class);
  114. $image->expects($this->once())->method('open')->will($this->returnValue(true));
  115. $image->expects($this->once())->method('keepAspectRatio')->will($this->returnValue(true));
  116. $image->expects($this->once())->method('resize')->will($this->returnValue(true));
  117. $image->expects($this->once())->method('save')->will($this->returnValue(true));
  118. $this->_imageFactory->expects($this->at(0))->method('create')->will($this->returnValue($image));
  119. /** Prepare session */
  120. $session = $this->createMock(\Magento\Backend\Model\Session::class);
  121. $this->_helperStorage->expects($this->any())->method('getSession')->will($this->returnValue($session));
  122. $expectedResult = [
  123. 'not_empty'
  124. ];
  125. $this->assertEquals($expectedResult, $this->_storageModel->uploadFile($this->_storageRoot));
  126. }
  127. /**
  128. * cover \Magento\Theme\Model\Wysiwyg\Storage::uploadFile
  129. * @expectedException \Magento\Framework\Exception\LocalizedException
  130. */
  131. public function testUploadInvalidFile()
  132. {
  133. $uploader = $this->_prepareUploader();
  134. $uploader->expects($this->once())->method('save')->will($this->returnValue(null));
  135. $this->_storageModel->uploadFile($this->_storageRoot);
  136. }
  137. /**
  138. * @return \PHPUnit_Framework_MockObject_MockObject
  139. */
  140. protected function _prepareUploader()
  141. {
  142. $uploader = $this->createMock(\Magento\MediaStorage\Model\File\Uploader::class);
  143. $this->_objectManager->expects($this->once())->method('create')->will($this->returnValue($uploader));
  144. $uploader->expects($this->once())->method('setAllowedExtensions')->will($this->returnValue($uploader));
  145. $uploader->expects($this->once())->method('setAllowRenameFiles')->will($this->returnValue($uploader));
  146. $uploader->expects($this->once())->method('setFilesDispersion')->will($this->returnValue($uploader));
  147. return $uploader;
  148. }
  149. /**
  150. * @dataProvider booleanCasesDataProvider
  151. * cover \Magento\Theme\Model\Wysiwyg\Storage::createFolder
  152. */
  153. public function testCreateFolder($isWritable)
  154. {
  155. $newDirectoryName = 'dir1';
  156. $fullNewPath = $this->_storageRoot . '/' . $newDirectoryName;
  157. $this->directoryWrite->expects(
  158. $this->any()
  159. )->method(
  160. 'isWritable'
  161. )->with(
  162. $this->_storageRoot
  163. )->will(
  164. $this->returnValue($isWritable)
  165. );
  166. $this->directoryWrite->expects(
  167. $this->once()
  168. )->method(
  169. 'isExist'
  170. )->with(
  171. $fullNewPath
  172. )->will(
  173. $this->returnValue(false)
  174. );
  175. $this->_helperStorage->expects(
  176. $this->once()
  177. )->method(
  178. 'getShortFilename'
  179. )->with(
  180. $newDirectoryName
  181. )->will(
  182. $this->returnValue($newDirectoryName)
  183. );
  184. $this->_helperStorage->expects(
  185. $this->once()
  186. )->method(
  187. 'convertPathToId'
  188. )->with(
  189. $fullNewPath
  190. )->will(
  191. $this->returnValue($newDirectoryName)
  192. );
  193. $this->_helperStorage->expects(
  194. $this->any()
  195. )->method(
  196. 'getStorageRoot'
  197. )->will(
  198. $this->returnValue($this->_storageRoot)
  199. );
  200. $expectedResult = [
  201. 'name' => $newDirectoryName,
  202. 'short_name' => $newDirectoryName,
  203. 'path' => '/' . $newDirectoryName,
  204. 'id' => $newDirectoryName,
  205. ];
  206. $this->assertEquals(
  207. $expectedResult,
  208. $this->_storageModel->createFolder($newDirectoryName, $this->_storageRoot)
  209. );
  210. }
  211. /**
  212. * cover \Magento\Theme\Model\Wysiwyg\Storage::createFolder
  213. * @expectedException \Magento\Framework\Exception\LocalizedException
  214. */
  215. public function testCreateFolderWithInvalidName()
  216. {
  217. $newDirectoryName = 'dir2!#$%^&';
  218. $this->_storageModel->createFolder($newDirectoryName, $this->_storageRoot);
  219. }
  220. /**
  221. * cover \Magento\Theme\Model\Wysiwyg\Storage::createFolder
  222. * @expectedException \Magento\Framework\Exception\LocalizedException
  223. */
  224. public function testCreateFolderDirectoryAlreadyExist()
  225. {
  226. $newDirectoryName = 'mew';
  227. $fullNewPath = $this->_storageRoot . '/' . $newDirectoryName;
  228. $this->directoryWrite->expects(
  229. $this->any()
  230. )->method(
  231. 'isWritable'
  232. )->with(
  233. $this->_storageRoot
  234. )->will(
  235. $this->returnValue(true)
  236. );
  237. $this->directoryWrite->expects(
  238. $this->once()
  239. )->method(
  240. 'isExist'
  241. )->with(
  242. $fullNewPath
  243. )->will(
  244. $this->returnValue(true)
  245. );
  246. $this->_storageModel->createFolder($newDirectoryName, $this->_storageRoot);
  247. }
  248. /**
  249. * cover \Magento\Theme\Model\Wysiwyg\Storage::getDirsCollection
  250. */
  251. public function testGetDirsCollection()
  252. {
  253. $dirs = [$this->_storageRoot . '/dir1', $this->_storageRoot . '/dir2'];
  254. $this->directoryWrite->expects(
  255. $this->any()
  256. )->method(
  257. 'isExist'
  258. )->with(
  259. $this->_storageRoot
  260. )->will(
  261. $this->returnValue(true)
  262. );
  263. $this->directoryWrite->expects($this->once())->method('search')->will($this->returnValue($dirs));
  264. $this->directoryWrite->expects($this->any())->method('isDirectory')->will($this->returnValue(true));
  265. $this->assertEquals($dirs, $this->_storageModel->getDirsCollection($this->_storageRoot));
  266. }
  267. /**
  268. * cover \Magento\Theme\Model\Wysiwyg\Storage::getDirsCollection
  269. * @expectedException \Magento\Framework\Exception\LocalizedException
  270. */
  271. public function testGetDirsCollectionWrongDirName()
  272. {
  273. $this->directoryWrite->expects(
  274. $this->once()
  275. )->method(
  276. 'isExist'
  277. )->with(
  278. $this->_storageRoot
  279. )->will(
  280. $this->returnValue(false)
  281. );
  282. $this->_storageModel->getDirsCollection($this->_storageRoot);
  283. }
  284. /**
  285. * cover \Magento\Theme\Model\Wysiwyg\Storage::getFilesCollection
  286. */
  287. public function testGetFilesCollection()
  288. {
  289. $this->_helperStorage->expects(
  290. $this->once()
  291. )->method(
  292. 'getCurrentPath'
  293. )->will(
  294. $this->returnValue($this->_storageRoot)
  295. );
  296. $this->_helperStorage->expects(
  297. $this->once()
  298. )->method(
  299. 'getStorageType'
  300. )->will(
  301. $this->returnValue(\Magento\Theme\Model\Wysiwyg\Storage::TYPE_FONT)
  302. );
  303. $this->_helperStorage->expects($this->any())->method('urlEncode')->will($this->returnArgument(0));
  304. $paths = [$this->_storageRoot . '/' . 'font1.ttf', $this->_storageRoot . '/' . 'font2.ttf'];
  305. $this->directoryWrite->expects($this->once())->method('search')->will($this->returnValue($paths));
  306. $this->directoryWrite->expects($this->any())->method('isFile')->will($this->returnValue(true));
  307. $result = $this->_storageModel->getFilesCollection();
  308. $this->assertCount(2, $result);
  309. $this->assertEquals('font1.ttf', $result[0]['text']);
  310. $this->assertEquals('font2.ttf', $result[1]['text']);
  311. }
  312. /**
  313. * cover \Magento\Theme\Model\Wysiwyg\Storage::getFilesCollection
  314. */
  315. public function testGetFilesCollectionImageType()
  316. {
  317. $this->_helperStorage->expects(
  318. $this->once()
  319. )->method(
  320. 'getCurrentPath'
  321. )->will(
  322. $this->returnValue($this->_storageRoot)
  323. );
  324. $this->_helperStorage->expects(
  325. $this->once()
  326. )->method(
  327. 'getStorageType'
  328. )->will(
  329. $this->returnValue(\Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE)
  330. );
  331. $this->_helperStorage->expects($this->any())->method('urlEncode')->will($this->returnArgument(0));
  332. $paths = [$this->_storageRoot . '/picture1.jpg'];
  333. $this->directoryWrite->expects($this->once())->method('search')->will($this->returnValue($paths));
  334. $this->directoryWrite->expects(
  335. $this->once()
  336. )->method(
  337. 'isFile'
  338. )->with(
  339. $this->_storageRoot . '/picture1.jpg'
  340. )->will(
  341. $this->returnValue(true)
  342. );
  343. $result = $this->_storageModel->getFilesCollection();
  344. $this->assertCount(1, $result);
  345. $this->assertEquals('picture1.jpg', $result[0]['text']);
  346. $this->assertEquals('picture1.jpg', $result[0]['thumbnailParams']['file']);
  347. }
  348. /**
  349. * cover \Magento\Theme\Model\Wysiwyg\Storage::getTreeArray
  350. */
  351. public function testTreeArray()
  352. {
  353. $currentPath = $this->_storageRoot . '/dir';
  354. $dirs = [$currentPath . '/dir_one', $currentPath . '/dir_two'];
  355. $expectedResult = [
  356. ['text' => pathinfo($dirs[0], PATHINFO_BASENAME), 'id' => $dirs[0], 'cls' => 'folder'],
  357. ['text' => pathinfo($dirs[1], PATHINFO_BASENAME), 'id' => $dirs[1], 'cls' => 'folder'],
  358. ];
  359. $this->directoryWrite->expects(
  360. $this->once()
  361. )->method(
  362. 'isExist'
  363. )->with(
  364. $currentPath
  365. )->will(
  366. $this->returnValue(true)
  367. );
  368. $this->directoryWrite->expects($this->once())->method('search')->will($this->returnValue($dirs));
  369. $this->directoryWrite->expects($this->any())->method('isDirectory')->will($this->returnValue(true));
  370. $this->_helperStorage->expects(
  371. $this->once()
  372. )->method(
  373. 'getCurrentPath'
  374. )->will(
  375. $this->returnValue($currentPath)
  376. );
  377. $this->_helperStorage->expects($this->any())->method('getShortFilename')->will($this->returnArgument(0));
  378. $this->_helperStorage->expects($this->any())->method('convertPathToId')->will($this->returnArgument(0));
  379. $result = $this->_storageModel->getTreeArray();
  380. $this->assertEquals($expectedResult, $result);
  381. }
  382. /**
  383. * @cover \Magento\Theme\Model\Wysiwyg\Storage::deleteFile
  384. */
  385. public function testDeleteFile()
  386. {
  387. $image = 'image.jpg';
  388. $this->_helperStorage->expects($this->once())
  389. ->method('getCurrentPath')
  390. ->will($this->returnValue($this->_storageRoot));
  391. $this->urlDecoder->expects($this->any())
  392. ->method('decode')
  393. ->with($image)
  394. ->willReturnArgument(0);
  395. $this->directoryWrite->expects($this->at(0))
  396. ->method('getRelativePath')
  397. ->with($this->_storageRoot)
  398. ->willReturn($this->_storageRoot);
  399. $this->directoryWrite->expects($this->at(1))
  400. ->method('getRelativePath')
  401. ->with($this->_storageRoot . '/' . $image)
  402. ->willReturn($this->_storageRoot . '/' . $image);
  403. $this->_helperStorage->expects($this->once())
  404. ->method('getStorageRoot')
  405. ->willReturn('/');
  406. $this->directoryWrite->expects($this->any())->method('delete');
  407. $this->assertInstanceOf(\Magento\Theme\Model\Wysiwyg\Storage::class, $this->_storageModel->deleteFile($image));
  408. }
  409. /**
  410. * cover \Magento\Theme\Model\Wysiwyg\Storage::deleteDirectory
  411. */
  412. public function testDeleteDirectory()
  413. {
  414. $directoryPath = $this->_storageRoot . '/../root';
  415. $this->_helperStorage->expects(
  416. $this->atLeastOnce()
  417. )->method(
  418. 'getStorageRoot'
  419. )->will(
  420. $this->returnValue($this->_storageRoot)
  421. );
  422. $this->directoryWrite->expects($this->once())->method('delete')->with($directoryPath);
  423. $this->_storageModel->deleteDirectory($directoryPath);
  424. }
  425. /**
  426. * cover \Magento\Theme\Model\Wysiwyg\Storage::deleteDirectory
  427. * @expectedException \Magento\Framework\Exception\LocalizedException
  428. */
  429. public function testDeleteRootDirectory()
  430. {
  431. $directoryPath = $this->_storageRoot;
  432. $this->_helperStorage->expects(
  433. $this->atLeastOnce()
  434. )->method(
  435. 'getStorageRoot'
  436. )->will(
  437. $this->returnValue($this->_storageRoot)
  438. );
  439. $this->_storageModel->deleteDirectory($directoryPath);
  440. }
  441. /**
  442. * @return array
  443. */
  444. public function booleanCasesDataProvider()
  445. {
  446. return [[true], [false]];
  447. }
  448. }