StorageTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Storage helper test
  8. */
  9. namespace Magento\Theme\Test\Unit\Helper;
  10. use Magento\Theme\Helper\Storage;
  11. /**
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class StorageTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $filesystem;
  20. /**
  21. * @var \Magento\Backend\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $session;
  24. /**
  25. * @var \Magento\Framework\View\Design\Theme\FlyweightFactory|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $themeFactory;
  28. /**
  29. * @var \PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $request;
  32. /**
  33. * @var \Magento\Theme\Helper\Storage
  34. */
  35. protected $helper;
  36. /**
  37. * @var string
  38. */
  39. protected $customizationPath;
  40. /**
  41. * @var \Magento\Framework\Filesystem\Directory\Write|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $directoryWrite;
  44. /**
  45. * @var \Magento\Framework\App\Helper\Context|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $contextHelper;
  48. /**
  49. * @var \Magento\Theme\Model\Theme|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. protected $theme;
  52. /**
  53. * @var \Magento\Framework\View\Design\Theme\Customization|\PHPUnit_Framework_MockObject_MockObject
  54. */
  55. protected $customization;
  56. /**
  57. * @var \Magento\Framework\Url\EncoderInterface|\PHPUnit_Framework_MockObject_MockObject
  58. */
  59. protected $urlEncoder;
  60. /**
  61. * @var \Magento\Framework\Url\DecoderInterface|\PHPUnit_Framework_MockObject_MockObject
  62. */
  63. protected $urlDecoder;
  64. protected $requestParams;
  65. protected function setUp()
  66. {
  67. $this->customizationPath = '/' . implode('/', ['var', 'theme']);
  68. $this->request = $this->createMock(\Magento\Framework\App\Request\Http::class);
  69. $this->filesystem = $this->createMock(\Magento\Framework\Filesystem::class);
  70. $this->session = $this->createMock(\Magento\Backend\Model\Session::class);
  71. $this->contextHelper = $this->createMock(\Magento\Framework\App\Helper\Context::class);
  72. $this->directoryWrite = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
  73. $this->themeFactory = $this->createMock(\Magento\Framework\View\Design\Theme\FlyweightFactory::class);
  74. $this->theme = $this->createMock(\Magento\Theme\Model\Theme::class);
  75. $this->customization = $this->createMock(\Magento\Framework\View\Design\Theme\Customization::class);
  76. $this->filesystem->expects($this->any())
  77. ->method('getDirectoryWrite')
  78. ->will($this->returnValue($this->directoryWrite));
  79. $this->urlEncoder = $this->getMockBuilder(\Magento\Framework\Url\EncoderInterface::class)->getMock();
  80. $this->urlDecoder = $this->getMockBuilder(\Magento\Framework\Url\DecoderInterface::class)->getMock();
  81. $this->directoryWrite->expects($this->any())->method('create')->willReturn(true);
  82. $this->contextHelper->expects($this->any())->method('getRequest')->willReturn($this->request);
  83. $this->contextHelper->expects($this->any())->method('getUrlEncoder')->willReturn($this->urlEncoder);
  84. $this->contextHelper->expects($this->any())->method('getUrlDecoder')->willReturn($this->urlDecoder);
  85. $this->themeFactory->expects($this->any())->method('create')->willReturn($this->theme);
  86. $this->theme->expects($this->any())
  87. ->method('getCustomization')
  88. ->will($this->returnValue($this->customization));
  89. $this->request->expects($this->at(0))
  90. ->method('getParam')
  91. ->with(\Magento\Theme\Helper\Storage::PARAM_THEME_ID)
  92. ->will($this->returnValue(6));
  93. $this->request->expects($this->at(1))
  94. ->method('getParam')
  95. ->with(\Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE)
  96. ->will($this->returnValue(\Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE));
  97. $this->helper = new \Magento\Theme\Helper\Storage(
  98. $this->contextHelper,
  99. $this->filesystem,
  100. $this->session,
  101. $this->themeFactory
  102. );
  103. }
  104. protected function tearDown()
  105. {
  106. $this->request = null;
  107. $this->filesystem = null;
  108. $this->session = null;
  109. $this->contextHelper = null;
  110. $this->directoryWrite = null;
  111. $this->themeFactory = null;
  112. $this->theme = null;
  113. $this->customization = null;
  114. }
  115. /**
  116. * @covers \Magento\Theme\Helper\Storage::getShortFilename
  117. * @covers \Magento\Theme\Helper\Storage::__construct
  118. */
  119. public function testGetShortFilename()
  120. {
  121. $longFileName = 'veryLongFileNameMoreThanTwenty';
  122. $expectedFileName = 'veryLongFileNameMore...';
  123. $this->assertEquals($expectedFileName, $this->helper->getShortFilename($longFileName, 20));
  124. }
  125. public function testGetStorageRoot()
  126. {
  127. $expectedStorageRoot = '/' . \Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE;
  128. $this->assertEquals($expectedStorageRoot, $this->helper->getStorageRoot());
  129. }
  130. public function testGetThumbnailDirectory()
  131. {
  132. $imagePath = implode('/', ['root', 'image', 'image_name.jpg']);
  133. $thumbnailDir = implode(
  134. '/',
  135. ['root', 'image', \Magento\Theme\Model\Wysiwyg\Storage::THUMBNAIL_DIRECTORY]
  136. );
  137. $this->assertEquals($thumbnailDir, $this->helper->getThumbnailDirectory($imagePath));
  138. }
  139. public function testGetThumbnailPath()
  140. {
  141. $image = 'image_name.jpg';
  142. $thumbnailPath = '/' . implode(
  143. '/',
  144. [
  145. \Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE,
  146. \Magento\Theme\Model\Wysiwyg\Storage::THUMBNAIL_DIRECTORY,
  147. $image
  148. ]
  149. );
  150. $this->customization->expects(
  151. $this->any()
  152. )->method(
  153. 'getCustomizationPath'
  154. )->will(
  155. $this->returnValue($this->customizationPath)
  156. );
  157. $this->directoryWrite->expects($this->any())->method('isExist')->will($this->returnValue(true));
  158. $this->assertEquals($thumbnailPath, $this->helper->getThumbnailPath($image));
  159. }
  160. public function testGetRequestParams()
  161. {
  162. $this->request->expects(
  163. $this->at(0)
  164. )->method(
  165. 'getParam'
  166. )->with(
  167. \Magento\Theme\Helper\Storage::PARAM_THEME_ID
  168. )->will(
  169. $this->returnValue(6)
  170. );
  171. $this->request->expects(
  172. $this->at(1)
  173. )->method(
  174. 'getParam'
  175. )->with(
  176. \Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE
  177. )->will(
  178. $this->returnValue('image')
  179. );
  180. $this->request->expects(
  181. $this->at(2)
  182. )->method(
  183. 'getParam'
  184. )->with(
  185. \Magento\Theme\Helper\Storage::PARAM_NODE
  186. )->will(
  187. $this->returnValue('node')
  188. );
  189. $expectedResult = [
  190. \Magento\Theme\Helper\Storage::PARAM_THEME_ID => 6,
  191. \Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE => \Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE,
  192. \Magento\Theme\Helper\Storage::PARAM_NODE => 'node',
  193. ];
  194. $this->assertEquals($expectedResult, $this->helper->getRequestParams());
  195. }
  196. public function testGetAllowedExtensionsByType()
  197. {
  198. $this->request->expects(
  199. $this->at(0)
  200. )->method(
  201. 'getParam'
  202. )->with(
  203. \Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE
  204. )->will(
  205. $this->returnValue(\Magento\Theme\Model\Wysiwyg\Storage::TYPE_FONT)
  206. );
  207. $this->request->expects(
  208. $this->at(1)
  209. )->method(
  210. 'getParam'
  211. )->with(
  212. \Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE
  213. )->will(
  214. $this->returnValue(\Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE)
  215. );
  216. $fontTypes = $this->helper->getAllowedExtensionsByType();
  217. $this->assertEquals(['ttf', 'otf', 'eot', 'svg', 'woff'], $fontTypes);
  218. $imagesTypes = $this->helper->getAllowedExtensionsByType();
  219. $this->assertEquals(['jpg', 'jpeg', 'gif', 'png', 'xbm', 'wbmp'], $imagesTypes);
  220. }
  221. /**
  222. * @test
  223. * @return void
  224. * @expectedException \InvalidArgumentException
  225. * @expectedExceptionMessage The image not found
  226. */
  227. public function testGetThumbnailPathNotFound()
  228. {
  229. $image = 'notFoundImage.png';
  230. $root = '/image';
  231. $sourceNode = '/not/a/root';
  232. $node = base64_encode($sourceNode);
  233. $this->request->expects($this->at(0))
  234. ->method('getParam')
  235. ->willReturnMap(
  236. [
  237. [
  238. \Magento\Theme\Helper\Storage::PARAM_THEME_ID,
  239. null,
  240. 6,
  241. ],
  242. [
  243. \Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE,
  244. null,
  245. \Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE
  246. ],
  247. [
  248. \Magento\Theme\Helper\Storage::PARAM_NODE,
  249. null,
  250. $node
  251. ],
  252. ]
  253. );
  254. $this->urlDecoder->expects($this->once())
  255. ->method('decode')
  256. ->with($node)
  257. ->willReturnCallback(function ($path) {
  258. return base64_decode($path);
  259. });
  260. $this->directoryWrite->expects($this->once())
  261. ->method('isDirectory')
  262. ->with($root . $sourceNode)
  263. ->willReturn(true);
  264. $this->directoryWrite->expects($this->once())
  265. ->method('getRelativePath')
  266. ->with($root . $sourceNode)
  267. ->willReturn($sourceNode);
  268. $this->directoryWrite->expects($this->once())
  269. ->method('isExist')
  270. ->with($sourceNode . '/' . $image);
  271. $this->helper->getThumbnailPath($image);
  272. }
  273. /**
  274. * @covers \Magento\Theme\Helper\Storage::convertPathToId
  275. * @covers \Magento\Theme\Helper\Storage::convertIdToPath
  276. */
  277. public function testConvertPathToIdAndIdToPath()
  278. {
  279. $path = '/image/path/to';
  280. $this->urlEncoder->expects($this->once())
  281. ->method('encode')
  282. ->with('/path/to')
  283. ->willReturnCallback(function ($path) {
  284. return base64_encode($path);
  285. });
  286. $this->urlDecoder->expects($this->once())
  287. ->method('decode')
  288. ->with(base64_encode('/path/to'))
  289. ->willReturnCallback(function ($path) {
  290. return base64_decode($path);
  291. });
  292. $value = $this->helper->convertPathToId($path);
  293. $this->assertEquals(base64_encode('/path/to'), $value);
  294. $this->assertEquals($path, $this->helper->convertIdToPath($value));
  295. }
  296. public function testGetSession()
  297. {
  298. $this->assertInstanceOf(\Magento\Backend\Model\Session::class, $this->helper->getSession());
  299. }
  300. public function testGetRelativeUrl()
  301. {
  302. $filename = base64_encode('filename.ext');
  303. $notRoot = base64_encode('not/a/root');
  304. $this->request->expects($this->any())
  305. ->method('getParam')
  306. ->willReturnMap(
  307. [
  308. 'type' => [
  309. \Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE,
  310. null,
  311. \Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE,
  312. ],
  313. 'node' => [
  314. \Magento\Theme\Helper\Storage::PARAM_NODE,
  315. null,
  316. $notRoot,
  317. ],
  318. 'filenaem' => [
  319. \Magento\Theme\Helper\Storage::PARAM_FILENAME,
  320. null,
  321. $filename,
  322. ],
  323. ]
  324. );
  325. $decode = function ($value) {
  326. return base64_decode($value);
  327. };
  328. $this->urlDecoder->expects($this->at(0))
  329. ->method('decode')
  330. ->with($notRoot)
  331. ->willReturnCallback($decode);
  332. $this->urlDecoder->expects($this->at(1))
  333. ->method('decode')
  334. ->with($filename)
  335. ->willReturnCallback($decode);
  336. $this->assertEquals(
  337. '../image/not/a/root/filename.ext',
  338. $this->helper->getRelativeUrl()
  339. );
  340. }
  341. /**
  342. * @return array
  343. */
  344. public function getStorageTypeForNameDataProvider()
  345. {
  346. return [
  347. 'font' => [\Magento\Theme\Model\Wysiwyg\Storage::TYPE_FONT, \Magento\Theme\Helper\Storage::FONTS],
  348. 'image' => [\Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE, \Magento\Theme\Helper\Storage::IMAGES],
  349. ];
  350. }
  351. /**
  352. * @test
  353. * @param string $type
  354. * @param string $name
  355. * @return void
  356. * @dataProvider getStorageTypeForNameDataProvider
  357. */
  358. public function testGetStorageTypeName($type, $name)
  359. {
  360. $this->request->expects($this->once())
  361. ->method('getParam')
  362. ->with(\Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE)
  363. ->willReturn($type);
  364. $this->assertEquals($name, $this->helper->getStorageTypeName());
  365. }
  366. /**
  367. * @test
  368. * @return void
  369. * @expectedException \Magento\Framework\Exception\LocalizedException
  370. * @expectedExceptionMessage Invalid type
  371. */
  372. public function testGetStorageTypeNameInvalid()
  373. {
  374. $this->helper->getStorageTypeName();
  375. }
  376. /**
  377. * @test
  378. * @return void
  379. * @expectedException \InvalidArgumentException
  380. * @expectedExceptionMessage Theme was not found
  381. */
  382. public function testGetThemeNotFound()
  383. {
  384. $this->themeFactory->expects($this->once())
  385. ->method('create')
  386. ->willReturn(null);
  387. $helper = new \Magento\Theme\Helper\Storage(
  388. $this->contextHelper,
  389. $this->filesystem,
  390. $this->session,
  391. $this->themeFactory
  392. );
  393. $helper->getStorageRoot();
  394. }
  395. }