|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + * |
| 6 | + */ |
| 7 | +namespace Magento\Cms\Model\Wysiwyg\Images; |
| 8 | + |
| 9 | +use Magento\Cms\Model\Wysiwyg\Images\Storage\Collection; |
| 10 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 11 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 12 | +use Magento\Framework\DataObject; |
| 13 | +use Magento\Framework\Filesystem; |
| 14 | +use Magento\Framework\Filesystem\Driver\File; |
| 15 | +use Magento\Framework\Filesystem\DriverInterface; |
| 16 | +use Magento\TestFramework\Helper\Bootstrap; |
| 17 | + |
| 18 | +/** |
| 19 | + * Test methods of class Storage |
| 20 | + * |
| 21 | + * @SuppressWarnings(PHPMD.LongVariable) |
| 22 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 23 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 24 | + * @SuppressWarnings(PHPMD.TooManyPublicMethods) |
| 25 | + */ |
| 26 | +class GetThumbnailUrlsTest extends \PHPUnit\Framework\TestCase |
| 27 | +{ |
| 28 | + private const MEDIA_GALLERY_IMAGE_FOLDERS_CONFIG_PATH |
| 29 | + = 'system/media_storage_configuration/allowed_resources/media_gallery_image_folders'; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var \Magento\Framework\ObjectManagerInterface |
| 33 | + */ |
| 34 | + private $objectManager; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var Filesystem |
| 38 | + */ |
| 39 | + private $filesystem; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var Storage |
| 43 | + */ |
| 44 | + private $storage; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var DriverInterface |
| 48 | + */ |
| 49 | + private $driver; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var array |
| 53 | + */ |
| 54 | + private $origConfigValue; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var \Magento\Framework\Filesystem\Directory\WriteInterface |
| 58 | + */ |
| 59 | + private $mediaDirectory; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var string |
| 63 | + */ |
| 64 | + private $fullDirectoryPath; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var \Magento\Cms\Helper\Wysiwyg\Images |
| 68 | + */ |
| 69 | + private $imagesHelper; |
| 70 | + |
| 71 | + /** |
| 72 | + * @inheritdoc |
| 73 | + */ |
| 74 | + protected function setUp(): void |
| 75 | + { |
| 76 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 77 | + $this->filesystem = $this->objectManager->get(Filesystem::class); |
| 78 | + $this->imagesHelper = $this->objectManager->get(\Magento\Cms\Helper\Wysiwyg\Images::class); |
| 79 | + $this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA); |
| 80 | + $this->fullDirectoryPath = rtrim($this->imagesHelper->getStorageRoot(), '/') |
| 81 | + . '/MagentoCmsModelWysiwygImagesStorageTest'; |
| 82 | + $this->mediaDirectory->create($this->mediaDirectory->getRelativePath($this->fullDirectoryPath)); |
| 83 | + $config = $this->objectManager->get(ScopeConfigInterface::class); |
| 84 | + $this->origConfigValue = $config->getValue( |
| 85 | + self::MEDIA_GALLERY_IMAGE_FOLDERS_CONFIG_PATH, |
| 86 | + 'default' |
| 87 | + ); |
| 88 | + $scopeConfig = $this->objectManager->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class); |
| 89 | + $scopeConfig->setValue( |
| 90 | + self::MEDIA_GALLERY_IMAGE_FOLDERS_CONFIG_PATH, |
| 91 | + array_merge($this->origConfigValue, ['MagentoCmsModelWysiwygImagesStorageTest']), |
| 92 | + ); |
| 93 | + $this->storage = $this->objectManager->create(Storage::class); |
| 94 | + $this->driver = $this->mediaDirectory->getDriver(); |
| 95 | + } |
| 96 | + |
| 97 | + protected function tearDown(): void |
| 98 | + { |
| 99 | + $this->mediaDirectory->delete($this->mediaDirectory->getRelativePath($this->fullDirectoryPath)); |
| 100 | + $scopeConfig = $this->objectManager->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class); |
| 101 | + $scopeConfig->setValue( |
| 102 | + self::MEDIA_GALLERY_IMAGE_FOLDERS_CONFIG_PATH, |
| 103 | + $this->origConfigValue |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Test that getThumbnailUrl() returns correct URL for root folder or sub-folders images |
| 109 | + * |
| 110 | + * @param string $directory |
| 111 | + * @param string $filename |
| 112 | + * @param array $expectedUrls |
| 113 | + * @return void |
| 114 | + * @magentoAppIsolation enabled |
| 115 | + * @magentoAppArea adminhtml |
| 116 | + * @dataProvider getThumbnailUrlDataProvider |
| 117 | + */ |
| 118 | + public function testGetThumbnailUrl(string $directory, string $filename, array $expectedUrls): void |
| 119 | + { |
| 120 | + $root = $this->storage->getCmsWysiwygImages()->getStorageRoot(); |
| 121 | + $directory = implode('/', array_filter([rtrim($root, '/'), trim($directory, '/')])); |
| 122 | + $path = $directory . '/' . $filename; |
| 123 | + $this->generateImage($path); |
| 124 | + $this->storage->resizeFile($path); |
| 125 | + $collection = $this->storage->getFilesCollection($directory, 'image'); |
| 126 | + $paths = []; |
| 127 | + foreach ($collection as $item) { |
| 128 | + $paths[] = parse_url($item->getThumbUrl(), PHP_URL_PATH); |
| 129 | + } |
| 130 | + $this->assertEquals($expectedUrls, $paths); |
| 131 | + $this->driver->deleteFile($path); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Provide scenarios for testing getThumbnailUrl() |
| 136 | + * |
| 137 | + * @return array |
| 138 | + */ |
| 139 | + public function getThumbnailUrlDataProvider(): array |
| 140 | + { |
| 141 | + return [ |
| 142 | + [ |
| 143 | + '/', |
| 144 | + 'image1.png', |
| 145 | + [] |
| 146 | + ], |
| 147 | + [ |
| 148 | + '/cms', |
| 149 | + 'image2.png', |
| 150 | + [] |
| 151 | + ], |
| 152 | + [ |
| 153 | + '/cms/pages', |
| 154 | + 'image3.png', |
| 155 | + [] |
| 156 | + ], |
| 157 | + [ |
| 158 | + '/MagentoCmsModelWysiwygImagesStorageTest', |
| 159 | + 'image2.png', |
| 160 | + ['/media/.thumbsMagentoCmsModelWysiwygImagesStorageTest/image2.png'] |
| 161 | + ], |
| 162 | + [ |
| 163 | + '/MagentoCmsModelWysiwygImagesStorageTest/pages', |
| 164 | + 'image3.png', |
| 165 | + ['/media/.thumbsMagentoCmsModelWysiwygImagesStorageTest/pages/image3.png'] |
| 166 | + ] |
| 167 | + ]; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Generate a dummy image of the given width and height. |
| 172 | + * |
| 173 | + * @param string $path |
| 174 | + * @return string |
| 175 | + */ |
| 176 | + private function generateImage(string $path) |
| 177 | + { |
| 178 | + $this->mediaDirectory->create(dirname($this->mediaDirectory->getRelativePath($path))); |
| 179 | + ob_start(); |
| 180 | + $image = imagecreatetruecolor(1024, 768); |
| 181 | + imagepng($image); |
| 182 | + $this->driver->filePutContents($path, ob_get_clean()); |
| 183 | + return $path; |
| 184 | + } |
| 185 | +} |
0 commit comments