ImagesTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Test\Unit\Helper\Wysiwyg;
  7. use Magento\Cms\Model\Wysiwyg\Config as WysiwygConfig;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class ImagesTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Cms\Helper\Wysiwyg\Images
  15. */
  16. protected $imagesHelper;
  17. /**
  18. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  19. */
  20. protected $objectManager;
  21. /**
  22. * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $filesystemMock;
  25. /**
  26. * @var \Magento\Framework\Filesystem\Directory\Write|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $directoryWriteMock;
  29. /**
  30. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $storeManagerMock;
  33. /**
  34. * @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $storeMock;
  37. /**
  38. * @var \Magento\Framework\App\Helper\Context|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $contextMock;
  41. /**
  42. * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $eventManagerMock;
  45. /**
  46. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. protected $requestMock;
  49. /**
  50. * @var \Magento\Framework\Url\EncoderInterface|\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. protected $urlEncoderMock;
  53. /**
  54. * @var \Magento\Backend\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  55. */
  56. protected $backendDataMock;
  57. /**
  58. * @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject
  59. */
  60. protected $escaperMock;
  61. /**
  62. * @var string
  63. */
  64. protected $path;
  65. protected function setUp()
  66. {
  67. $this->path = 'PATH';
  68. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  69. $this->eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  70. $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  71. $this->urlEncoderMock = $this->createMock(\Magento\Framework\Url\EncoderInterface::class);
  72. $this->backendDataMock = $this->createMock(\Magento\Backend\Helper\Data::class);
  73. $this->contextMock = $this->createMock(\Magento\Framework\App\Helper\Context::class);
  74. $this->contextMock->expects($this->any())
  75. ->method('getEventManager')
  76. ->willReturn($this->eventManagerMock);
  77. $this->contextMock->expects($this->any())
  78. ->method('getRequest')
  79. ->willReturn($this->requestMock);
  80. $this->contextMock->expects($this->any())
  81. ->method('getUrlEncoder')
  82. ->willReturn($this->urlEncoderMock);
  83. $this->directoryWriteMock = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\Write::class)
  84. ->setConstructorArgs(['path' => $this->path])
  85. ->disableOriginalConstructor()
  86. ->getMock();
  87. $this->directoryWriteMock->expects($this->any())
  88. ->method('getAbsolutePath')
  89. ->willReturnMap(
  90. [
  91. [WysiwygConfig::IMAGE_DIRECTORY, null, $this->getAbsolutePath(WysiwygConfig::IMAGE_DIRECTORY)],
  92. [null, null, $this->getAbsolutePath(null)],
  93. ['', null, $this->getAbsolutePath('')],
  94. ]
  95. );
  96. $this->filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
  97. $this->filesystemMock->expects($this->once())
  98. ->method('getDirectoryWrite')
  99. ->willReturn($this->directoryWriteMock);
  100. $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  101. ->setMethods(
  102. [
  103. 'clearWebsiteCache', 'getDefaultStoreView', 'getGroup', 'getGroups',
  104. 'getStore', 'getStores', 'getWebsite', 'getWebsites', 'hasSingleStore',
  105. 'isSingleStoreMode', 'reinitStores', 'setCurrentStore', 'setIsSingleStoreModeAllowed',
  106. ]
  107. )
  108. ->disableOriginalConstructor()
  109. ->getMock();
  110. $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  111. $this->escaperMock = $this->createMock(\Magento\Framework\Escaper::class);
  112. $this->imagesHelper = $this->objectManager->getObject(
  113. \Magento\Cms\Helper\Wysiwyg\Images::class,
  114. [
  115. 'context' => $this->contextMock,
  116. 'filesystem' => $this->filesystemMock,
  117. 'storeManager' => $this->storeManagerMock,
  118. 'backendData' => $this->backendDataMock,
  119. 'escaper' => $this->escaperMock,
  120. ]
  121. );
  122. }
  123. protected function tearDown()
  124. {
  125. $this->objectManager = null;
  126. $this->directoryWriteMock = null;
  127. $this->filesystemMock = null;
  128. $this->storeManagerMock = null;
  129. $this->storeMock = null;
  130. $this->imagesHelper = null;
  131. $this->contextMock = null;
  132. $this->eventManagerMock = null;
  133. $this->requestMock = null;
  134. $this->urlEncoderMock = null;
  135. $this->backendDataMock = null;
  136. $this->escaperMock = null;
  137. }
  138. /**
  139. * @param string $path
  140. * @return string
  141. */
  142. protected function getAbsolutePath($path)
  143. {
  144. return $this->path . $path;
  145. }
  146. public function testSetStoreId()
  147. {
  148. $this->assertEquals($this->imagesHelper, $this->imagesHelper->setStoreId(1));
  149. }
  150. public function testGetStorageRoot()
  151. {
  152. $this->assertEquals(
  153. $this->getAbsolutePath(''),
  154. $this->imagesHelper->getStorageRoot()
  155. );
  156. }
  157. public function testGetBaseUrl()
  158. {
  159. $this->storeManagerMock->expects($this->once())
  160. ->method('getStore')
  161. ->willReturn($this->storeMock);
  162. $this->storeMock->expects($this->once())
  163. ->method('getBaseUrl')
  164. ->with(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
  165. $this->imagesHelper->getBaseUrl();
  166. }
  167. public function testGetTreeNodeName()
  168. {
  169. $this->assertEquals('node', $this->imagesHelper->getTreeNodeName());
  170. }
  171. public function testConvertPathToId()
  172. {
  173. $pathOne = '/test_path';
  174. $pathTwo = $this->getAbsolutePath('') . '/test_path';
  175. $this->assertEquals(
  176. $this->imagesHelper->convertPathToId($pathOne),
  177. $this->imagesHelper->convertPathToId($pathTwo)
  178. );
  179. }
  180. /**
  181. * @param string $path
  182. * @param string $pathId
  183. * @dataProvider providerConvertIdToPath
  184. */
  185. public function testConvertIdToPathAnyId($path, $pathId)
  186. {
  187. $pathOne = $this->imagesHelper->getStorageRoot() . $path;
  188. $pathTwo = $this->imagesHelper->convertIdToPath($pathId);
  189. $this->assertEquals($pathOne, $pathTwo);
  190. }
  191. /**
  192. * @return array
  193. */
  194. public function providerConvertIdToPath()
  195. {
  196. return [
  197. ['', ''],
  198. ['/test_path', 'L3Rlc3RfcGF0aA--'],
  199. ];
  200. }
  201. public function testConvertIdToPathNodeRoot()
  202. {
  203. $pathId = \Magento\Theme\Helper\Storage::NODE_ROOT;
  204. $this->assertEquals($this->imagesHelper->getStorageRoot(), $this->imagesHelper->convertIdToPath($pathId));
  205. }
  206. /**
  207. * @expectedException \InvalidArgumentException
  208. * @expectedExceptionMessage Path is invalid
  209. */
  210. public function testConvertIdToPathInvalid()
  211. {
  212. $this->imagesHelper->convertIdToPath('Ly4uLy4uLy4uLy4uLy4uL3dvcms-');
  213. }
  214. /**
  215. * @param string $fileName
  216. * @param int $maxLength
  217. * @param string $expectedFilename
  218. * @dataProvider providerShortFilename
  219. */
  220. public function testGetShortFilename($fileName, $maxLength, $expectedFilename)
  221. {
  222. $this->assertEquals($expectedFilename, $this->imagesHelper->getShortFilename($fileName, $maxLength));
  223. }
  224. /**
  225. * @return array
  226. */
  227. public function providerShortFilename()
  228. {
  229. return [
  230. ['test', 3, 'tes...'],
  231. ['test', 4, 'test'],
  232. ['test', 20, 'test'],
  233. ];
  234. }
  235. /**
  236. * @param string $fileName
  237. * @param string $expectedFilename
  238. * @dataProvider providerShortFilenameDefaultMaxLength
  239. */
  240. public function testGetShortFilenameDefaultMaxLength($fileName, $expectedFilename)
  241. {
  242. $this->assertEquals($expectedFilename, $this->imagesHelper->getShortFilename($fileName));
  243. }
  244. /**
  245. * @return array
  246. */
  247. public function providerShortFilenameDefaultMaxLength()
  248. {
  249. return [
  250. ['Mini text', 'Mini text'],
  251. ['20 symbols are here', '20 symbols are here'],
  252. ['Some text for this unit test', 'Some text for this u...'],
  253. ];
  254. }
  255. /**
  256. * @param bool $allowedValue
  257. * @dataProvider providerIsUsingStaticUrlsAllowed
  258. */
  259. public function testIsUsingStaticUrlsAllowed($allowedValue)
  260. {
  261. $this->generalSettingsIsUsingStaticUrlsAllowed($allowedValue);
  262. $this->assertEquals($allowedValue, $this->imagesHelper->isUsingStaticUrlsAllowed());
  263. }
  264. /**
  265. * @param bool $allowedValue
  266. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  267. */
  268. protected function generalSettingsIsUsingStaticUrlsAllowed($allowedValue)
  269. {
  270. $storeId = 1;
  271. $this->imagesHelper->setStoreId($storeId);
  272. $checkResult = new \stdClass();
  273. $checkResult->isAllowed = false;
  274. $this->eventManagerMock->expects($this->any())
  275. ->method('dispatch')
  276. ->with('cms_wysiwyg_images_static_urls_allowed', ['result' => $checkResult, 'store_id' => $storeId])
  277. ->willReturnCallback(function ($str, $arr) use ($allowedValue) {
  278. $arr['result']->isAllowed = $allowedValue;
  279. });
  280. }
  281. /**
  282. * @return array
  283. */
  284. public function providerIsUsingStaticUrlsAllowed()
  285. {
  286. return [
  287. [true],
  288. [false],
  289. ];
  290. }
  291. /**
  292. * @param string $pathId
  293. * @param string $expectedPath
  294. * @param bool $isExist
  295. * @dataProvider providerGetCurrentPath
  296. */
  297. public function testGetCurrentPath($pathId, $expectedPath, $isExist)
  298. {
  299. $this->requestMock->expects($this->any())
  300. ->method('getParam')
  301. ->willReturnMap(
  302. [
  303. ['node', null, $pathId],
  304. ]
  305. );
  306. $this->directoryWriteMock->expects($this->any())
  307. ->method('isDirectory')
  308. ->willReturnMap(
  309. [
  310. ['/../test_path', true],
  311. ['/../my.jpg', false],
  312. ['.', true],
  313. ]
  314. );
  315. $this->directoryWriteMock->expects($this->any())
  316. ->method('getRelativePath')
  317. ->willReturnMap(
  318. [
  319. ['PATH/test_path', '/../test_path'],
  320. ['PATH/my.jpg', '/../my.jpg'],
  321. ['PATH', '.'],
  322. ]
  323. );
  324. $this->directoryWriteMock->expects($this->once())
  325. ->method('isExist')
  326. ->willReturn($isExist);
  327. $this->directoryWriteMock->expects($this->any())
  328. ->method('create')
  329. ->with($this->directoryWriteMock->getRelativePath($expectedPath));
  330. $this->assertEquals($expectedPath, $this->imagesHelper->getCurrentPath());
  331. }
  332. public function testGetCurrentPathThrowException()
  333. {
  334. $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
  335. $this->expectExceptionMessage('The directory PATH is not writable by server.');
  336. $this->directoryWriteMock->expects($this->once())
  337. ->method('isExist')
  338. ->willReturn(false);
  339. $this->directoryWriteMock->expects($this->any())
  340. ->method('create')
  341. ->willThrowException(
  342. new \Magento\Framework\Exception\FileSystemException(__('Could not create a directory.'))
  343. );
  344. $this->imagesHelper->getCurrentPath();
  345. $this->fail('An expected exception has not been raised.');
  346. }
  347. /**
  348. * @return array
  349. */
  350. public function providerGetCurrentPath()
  351. {
  352. return [
  353. ['L3Rlc3RfcGF0aA--', 'PATH/test_path', true],
  354. ['L215LmpwZw--', 'PATH', true],
  355. [null, 'PATH', true],
  356. ['L3Rlc3RfcGF0aA--', 'PATH/test_path', false],
  357. ['L215LmpwZw--', 'PATH', false],
  358. [null, 'PATH', false],
  359. ];
  360. }
  361. public function testGetCurrentUrl()
  362. {
  363. $storeId = 1;
  364. $baseUrl = 'http://localhost';
  365. $relativePath = '/../wysiwyg';
  366. $this->imagesHelper->setStoreId($storeId);
  367. $this->storeMock->expects($this->once())
  368. ->method('getBaseUrl')
  369. ->with(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)
  370. ->willReturn($baseUrl);
  371. $this->storeManagerMock->expects($this->once())
  372. ->method('getStore')
  373. ->willReturn($this->storeMock);
  374. $this->directoryWriteMock->expects($this->any())
  375. ->method('getRelativePath')
  376. ->willReturn($relativePath);
  377. $this->assertEquals($baseUrl . $relativePath . '/', $this->imagesHelper->getCurrentUrl());
  378. }
  379. /**
  380. * @param string $baseUrl
  381. * @param string $fileName
  382. * @param bool $isUsingStaticUrls
  383. * @param string|null $escapedValue
  384. * @param string $expectedHtml
  385. * @dataProvider providerGetImageHtmlDeclarationRenderingAsTag
  386. */
  387. public function testGetImageHtmlDeclarationRenderingAsTag(
  388. $baseUrl,
  389. $fileName,
  390. $isUsingStaticUrls,
  391. $escapedValue,
  392. $expectedHtml
  393. ) {
  394. $this->generalSettingsGetImageHtmlDeclaration($baseUrl, $isUsingStaticUrls, $escapedValue);
  395. $this->assertEquals($expectedHtml, $this->imagesHelper->getImageHtmlDeclaration($fileName, true));
  396. }
  397. /**
  398. * @return array
  399. */
  400. public function providerGetImageHtmlDeclarationRenderingAsTag()
  401. {
  402. return [
  403. [
  404. 'http://localhost',
  405. 'test.png',
  406. true,
  407. null,
  408. '<img src="http://localhost/test.png" alt="" />',
  409. ],
  410. [
  411. 'http://localhost',
  412. 'test.png',
  413. false,
  414. '{{media url=&quot;/test.png&quot;}}',
  415. '<img src="{{media url=&quot;/test.png&quot;}}" alt="" />',
  416. ],
  417. ];
  418. }
  419. /**
  420. * @param string $baseUrl
  421. * @param string $fileName
  422. * @param bool $isUsingStaticUrls
  423. * @param string $expectedHtml
  424. * @dataProvider providerGetImageHtmlDeclaration
  425. */
  426. public function testGetImageHtmlDeclaration($baseUrl, $fileName, $isUsingStaticUrls, $expectedHtml)
  427. {
  428. $directive = '{{media url="/' . $fileName . '"}}';
  429. $this->generalSettingsGetImageHtmlDeclaration($baseUrl, $isUsingStaticUrls);
  430. $this->urlEncoderMock->expects($this->any())
  431. ->method('encode')
  432. ->with($directive)
  433. ->willReturn($directive);
  434. $this->backendDataMock->expects($this->any())
  435. ->method('getUrl')
  436. ->with('cms/wysiwyg/directive', ['___directive' => $directive, '_escape_params' => false])
  437. ->willReturn($directive);
  438. $this->assertEquals($expectedHtml, $this->imagesHelper->getImageHtmlDeclaration($fileName));
  439. }
  440. /**
  441. * @return array
  442. */
  443. public function providerGetImageHtmlDeclaration()
  444. {
  445. return [
  446. ['http://localhost', 'test.png', true, 'http://localhost/test.png'],
  447. ['http://localhost', 'test.png', false, '{{media url="/test.png"}}'],
  448. ];
  449. }
  450. /**
  451. * @param string $baseUrl
  452. * @param bool $isUsingStaticUrls
  453. * @param string|null $escapedValue
  454. */
  455. protected function generalSettingsGetImageHtmlDeclaration($baseUrl, $isUsingStaticUrls, $escapedValue = null)
  456. {
  457. $storeId = 1;
  458. $this->imagesHelper->setStoreId($storeId);
  459. $this->storeMock->expects($this->any())
  460. ->method('getBaseUrl')
  461. ->with(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)
  462. ->willReturn($baseUrl);
  463. $this->storeManagerMock->expects($this->any())
  464. ->method('getStore')
  465. ->willReturn($this->storeMock);
  466. if ($escapedValue) {
  467. $this->escaperMock->expects($this->once())->method('escapeHtml')->willReturn($escapedValue);
  468. }
  469. $this->generalSettingsIsUsingStaticUrlsAllowed($isUsingStaticUrls);
  470. }
  471. }