ImagesTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Helper\Wysiwyg;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\TestFramework\ObjectManager;
  9. class ImagesTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var ObjectManager
  13. */
  14. private $objectManager;
  15. protected function setUp()
  16. {
  17. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  18. }
  19. public function testGetStorageRoot()
  20. {
  21. /** @var \Magento\Framework\Filesystem $filesystem */
  22. $filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  23. \Magento\Framework\Filesystem::class
  24. );
  25. $mediaPath = $filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath();
  26. /** @var \Magento\Cms\Helper\Wysiwyg\Images $helper */
  27. $helper = $this->objectManager->create(
  28. \Magento\Cms\Helper\Wysiwyg\Images::class
  29. );
  30. $this->assertStringStartsWith($mediaPath, $helper->getStorageRoot());
  31. }
  32. /**
  33. * @magentoConfigFixture current_store web/unsecure/base_url http://example.com/
  34. */
  35. public function testGetCurrentUrl()
  36. {
  37. /** @var \Magento\Cms\Helper\Wysiwyg\Images $helper */
  38. $helper = $this->objectManager->create(
  39. \Magento\Cms\Helper\Wysiwyg\Images::class
  40. );
  41. $this->assertStringStartsWith('http://example.com/', $helper->getCurrentUrl());
  42. }
  43. /**
  44. * @param bool $isStaticUrlsAllowed
  45. * @param string $filename
  46. * @param bool $renderAsTag
  47. * @param string|callable $expectedResult - string or callable to make unique assertions on $expectedResult
  48. * @magentoConfigFixture current_store web/unsecure/base_url http://example.com/
  49. * @dataProvider providerGetImageHtmlDeclaration
  50. */
  51. public function testGetImageHtmlDeclaration(
  52. $isStaticUrlsAllowed,
  53. $filename,
  54. $renderAsTag,
  55. $expectedResult
  56. ) {
  57. $helper = $this->generateHelper($isStaticUrlsAllowed);
  58. $actualResult = $helper->getImageHtmlDeclaration($filename, $renderAsTag);
  59. if (is_callable($expectedResult)) {
  60. $expectedResult($actualResult);
  61. } else {
  62. $this->assertEquals(
  63. $expectedResult,
  64. $actualResult
  65. );
  66. }
  67. }
  68. /**
  69. * Data provider for testGetImageHtmlDeclaration
  70. *
  71. * @return array
  72. */
  73. public function providerGetImageHtmlDeclaration()
  74. {
  75. return [
  76. [true, 'wysiwyg/hello.png', true, '<img src="http://example.com/pub/media/wysiwyg/hello.png" alt="" />'],
  77. [
  78. false,
  79. 'wysiwyg/hello.png',
  80. false,
  81. function ($actualResult) {
  82. $expectedResult = (
  83. '/backend/cms/wysiwyg/directive/___directive/' .
  84. 'e3ttZWRpYSB1cmw9Ind5c2l3eWcvaGVsbG8ucG5nIn19/'
  85. );
  86. $this->assertContains($expectedResult, parse_url($actualResult, PHP_URL_PATH));
  87. }
  88. ],
  89. [true, 'wysiwyg/hello.png', false, 'http://example.com/pub/media/wysiwyg/hello.png'],
  90. [false, 'wysiwyg/hello.png', true, '<img src="{{media url=&quot;wysiwyg/hello.png&quot;}}" alt="" />'],
  91. ];
  92. }
  93. /**
  94. * Generate instance of Images Helper
  95. *
  96. * @param bool $isStaticUrlsAllowed - mock is created to override value of isUsingStaticUrlsAllowed method in class
  97. * @return \Magento\Cms\Helper\Wysiwyg\Images
  98. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  99. */
  100. private function generateHelper($isStaticUrlsAllowed = false)
  101. {
  102. $storeId = 1;
  103. $eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  104. $contextMock = $this->objectManager->create(\Magento\Framework\App\Helper\Context::class, [
  105. 'eventManager' => $eventManagerMock,
  106. ]);
  107. $helper = $this->objectManager->create(\Magento\Cms\Helper\Wysiwyg\Images::class, [
  108. 'context' => $contextMock
  109. ]);
  110. $checkResult = new \stdClass();
  111. $checkResult->isAllowed = false;
  112. $eventManagerMock->expects($this->any())
  113. ->method('dispatch')
  114. ->with('cms_wysiwyg_images_static_urls_allowed', ['result' => $checkResult, 'store_id' => $storeId])
  115. ->willReturnCallback(function ($_, $arr) use ($isStaticUrlsAllowed) {
  116. $arr['result']->isAllowed = $isStaticUrlsAllowed;
  117. });
  118. $helper->setStoreId($storeId);
  119. return $helper;
  120. }
  121. }