Skip to content
19 changes: 14 additions & 5 deletions app/code/Magento/Cms/Model/Wysiwyg/Images/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*
* @api
* @since 100.0.2
Expand Down Expand Up @@ -152,6 +153,11 @@ class Storage extends \Magento\Framework\DataObject
*/
private $ioFile;

/**
* @var \Magento\Framework\File\Mime|null
*/
private $mime;

/**
* Construct
*
Expand All @@ -174,6 +180,7 @@ class Storage extends \Magento\Framework\DataObject
* @param \Magento\Framework\Filesystem\DriverInterface $file
* @param \Magento\Framework\Filesystem\Io\File|null $ioFile
* @param \Psr\Log\LoggerInterface|null $logger
* @param \Magento\Framework\File\Mime $mime
*
* @throws \Magento\Framework\Exception\FileSystemException
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
Expand All @@ -197,7 +204,8 @@ public function __construct(
array $data = [],
\Magento\Framework\Filesystem\DriverInterface $file = null,
\Magento\Framework\Filesystem\Io\File $ioFile = null,
\Psr\Log\LoggerInterface $logger = null
\Psr\Log\LoggerInterface $logger = null,
\Magento\Framework\File\Mime $mime = null
) {
$this->_session = $session;
$this->_backendUrl = $backendUrl;
Expand All @@ -217,6 +225,7 @@ public function __construct(
$this->_dirs = $dirs;
$this->file = $file ?: ObjectManager::getInstance()->get(\Magento\Framework\Filesystem\Driver\File::class);
$this->ioFile = $ioFile ?: ObjectManager::getInstance()->get(\Magento\Framework\Filesystem\Io\File::class);
$this->mime = $mime ?: ObjectManager::getInstance()->get(\Magento\Framework\File\Mime::class);
parent::__construct($data);
}

Expand Down Expand Up @@ -362,7 +371,7 @@ public function getFilesCollection($path, $type = null)
$item->setUrl($this->_cmsWysiwygImages->getCurrentUrl() . $item->getBasename());
$itemStats = $this->file->stat($item->getFilename());
$item->setSize($itemStats['size']);
$item->setMimeType(\mime_content_type($item->getFilename()));
$item->setMimeType($this->mime->getMimeType($item->getFilename()));

if ($this->isImage($item->getBasename())) {
$thumbUrl = $this->getThumbnailUrl($item->getFilename(), true);
Expand Down Expand Up @@ -647,7 +656,7 @@ public function resizeFile($source, $keepRatio = true)
$image->keepAspectRatio($keepRatio);

list($imageWidth, $imageHeight) = $this->getResizedParams($source);

$image->resize($imageWidth, $imageHeight);
$dest = $targetDir . '/' . $this->ioFile->getPathInfo($source)['basename'];
$image->save($dest);
Expand All @@ -670,7 +679,7 @@ private function getResizedParams(string $source): array

//phpcs:ignore Generic.PHP.NoSilencedErrors
list($imageWidth, $imageHeight) = @getimagesize($source);

if ($imageWidth && $imageHeight) {
$imageWidth = $configWidth > $imageWidth ? $imageWidth : $configWidth;
$imageHeight = $configHeight > $imageHeight ? $imageHeight : $configHeight;
Expand All @@ -679,7 +688,7 @@ private function getResizedParams(string $source): array
}
return [$configWidth, $configHeight];
}

/**
* Resize images on the fly in controller action
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
*/
namespace Magento\Cms\Model\Wysiwyg\Images;

use Magento\Cms\Model\Wysiwyg\Images\Storage\Collection;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\DataObject;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Filesystem\DriverInterface;
use Magento\TestFramework\Helper\Bootstrap;

/**
* Test methods of class Storage
Expand All @@ -29,22 +35,27 @@ class StorageTest extends \PHPUnit\Framework\TestCase
private $objectManager;

/**
* @var \Magento\Framework\Filesystem
* @var Filesystem
*/
private $filesystem;

/**
* @var \Magento\Cms\Model\Wysiwyg\Images\Storage
* @var Storage
*/
private $storage;

/**
* @var DriverInterface
*/
private $driver;

/**
* @inheritdoc
*/
// phpcs:disable
public static function setUpBeforeClass(): void
{
self::$_baseDir = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
self::$_baseDir = Bootstrap::getObjectManager()->get(
\Magento\Cms\Helper\Wysiwyg\Images::class
)->getCurrentPath() . 'MagentoCmsModelWysiwygImagesStorageTest';
if (!file_exists(self::$_baseDir)) {
Expand All @@ -60,8 +71,8 @@ public static function setUpBeforeClass(): void
// phpcs:ignore
public static function tearDownAfterClass(): void
{
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Framework\Filesystem\Driver\File::class
Bootstrap::getObjectManager()->create(
File::class
)->deleteDirectory(
self::$_baseDir
);
Expand All @@ -72,9 +83,10 @@ public static function tearDownAfterClass(): void
*/
protected function setUp(): void
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->filesystem = $this->objectManager->get(\Magento\Framework\Filesystem::class);
$this->storage = $this->objectManager->create(\Magento\Cms\Model\Wysiwyg\Images\Storage::class);
$this->objectManager = Bootstrap::getObjectManager();
$this->filesystem = $this->objectManager->get(Filesystem::class);
$this->storage = $this->objectManager->create(Storage::class);
$this->driver = Bootstrap::getObjectManager()->get(DriverInterface::class);
}

/**
Expand All @@ -83,16 +95,31 @@ protected function setUp(): void
*/
public function testGetFilesCollection(): void
{
\Magento\TestFramework\Helper\Bootstrap::getInstance()
Bootstrap::getInstance()
->loadArea(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE);
$collection = $this->storage->getFilesCollection(self::$_baseDir, 'media');
$this->assertInstanceOf(\Magento\Cms\Model\Wysiwyg\Images\Storage\Collection::class, $collection);
$fileName = 'magento_image.jpg';
$imagePath = realpath(__DIR__ . '/../../../../Catalog/_files/' . $fileName);
$mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$modifiableFilePath = $mediaDirectory->getAbsolutePath('MagentoCmsModelWysiwygImagesStorageTest/' . $fileName);
$this->driver->copy(
$imagePath,
$modifiableFilePath
);
$this->storage->resizeFile($modifiableFilePath);
$collection = $this->storage->getFilesCollection(self::$_baseDir, 'image');
$this->assertInstanceOf(Collection::class, $collection);
foreach ($collection as $item) {
$this->assertInstanceOf(\Magento\Framework\DataObject::class, $item);
$this->assertStringEndsWith('/1.swf', $item->getUrl());
$this->assertStringMatchesFormat(
'http://%s/static/%s/adminhtml/%s/%s/Magento_Cms/images/placeholder_thumbnail.jpg',
$item->getThumbUrl()
$this->assertInstanceOf(DataObject::class, $item);
$this->assertStringEndsWith('/' . $fileName, $item->getUrl());
$this->assertEquals(
'/pub/media/.thumbsMagentoCmsModelWysiwygImagesStorageTest/magento_image.jpg',
parse_url($item->getThumbUrl(), PHP_URL_PATH),
"Check if Thumbnail URL is equal to the generated URL"
);
$this->assertEquals(
'image/jpeg',
$item->getMimeType(),
"Check if Mime Type is equal to the image in the file system"
);
return;
}
Expand Down Expand Up @@ -121,7 +148,7 @@ public function testDeleteDirectory(): void
$this->storage->createDirectory($dir, $path);
$this->assertFileExists($fullPath);
$this->storage->deleteDirectory($fullPath);
$this->assertFileNotExists($fullPath);
$this->assertFileDoesNotExist($fullPath);
}

/**
Expand All @@ -142,7 +169,7 @@ public function testDeleteDirectoryWithExcludedDirPath(): void
public function testUploadFile(): void
{
$fileName = 'magento_small_image.jpg';
$tmpDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
$tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
$filePath = $tmpDirectory->getAbsolutePath($fileName);
// phpcs:disable
$fixtureDir = realpath(__DIR__ . '/../../../../Catalog/_files');
Expand Down Expand Up @@ -172,7 +199,7 @@ public function testUploadFileWithExcludedDirPath(): void
);

$fileName = 'magento_small_image.jpg';
$tmpDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
$tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
$filePath = $tmpDirectory->getAbsolutePath($fileName);
// phpcs:disable
$fixtureDir = realpath(__DIR__ . '/../../../../Catalog/_files');
Expand Down Expand Up @@ -204,7 +231,7 @@ public function testUploadFileWithWrongExtension(string $fileName, string $fileT
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
$this->expectExceptionMessage('File validation failed.');

$tmpDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
$tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
$filePath = $tmpDirectory->getAbsolutePath($fileName);
// phpcs:disable
$fixtureDir = realpath(__DIR__ . '/../../../_files');
Expand Down Expand Up @@ -251,7 +278,7 @@ public function testUploadFileWithWrongFile(): void
$this->expectExceptionMessage('File validation failed.');

$fileName = 'file.gif';
$tmpDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
$tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
$filePath = $tmpDirectory->getAbsolutePath($fileName);
// phpcs:disable
$file = fopen($filePath, "wb");
Expand Down