diff --git a/app/code/Magento/Cms/Model/Wysiwyg/Images/Storage.php b/app/code/Magento/Cms/Model/Wysiwyg/Images/Storage.php index ae88b24bd2682..0cc108e5bed8b 100644 --- a/app/code/Magento/Cms/Model/Wysiwyg/Images/Storage.php +++ b/app/code/Magento/Cms/Model/Wysiwyg/Images/Storage.php @@ -22,6 +22,7 @@ * @SuppressWarnings(PHPMD.TooManyFields) * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.CookieAndSessionMisuse) + * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) * * @api * @since 100.0.2 @@ -152,6 +153,11 @@ class Storage extends \Magento\Framework\DataObject */ private $ioFile; + /** + * @var \Magento\Framework\File\Mime|null + */ + private $mime; + /** * Construct * @@ -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) @@ -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; @@ -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); } @@ -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); @@ -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); @@ -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; @@ -679,7 +688,7 @@ private function getResizedParams(string $source): array } return [$configWidth, $configHeight]; } - + /** * Resize images on the fly in controller action * diff --git a/dev/tests/integration/testsuite/Magento/Cms/Model/Wysiwyg/Images/StorageTest.php b/dev/tests/integration/testsuite/Magento/Cms/Model/Wysiwyg/Images/StorageTest.php index a68a546c20bc6..cb96ca2a14cac 100644 --- a/dev/tests/integration/testsuite/Magento/Cms/Model/Wysiwyg/Images/StorageTest.php +++ b/dev/tests/integration/testsuite/Magento/Cms/Model/Wysiwyg/Images/StorageTest.php @@ -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 @@ -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)) { @@ -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 ); @@ -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); } /** @@ -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; } @@ -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); } /** @@ -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'); @@ -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'); @@ -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'); @@ -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"); diff --git a/lib/internal/Magento/Framework/File/Mime.php b/lib/internal/Magento/Framework/File/Mime.php index 148f43d47cfd4..e0b22e4c944d9 100644 --- a/lib/internal/Magento/Framework/File/Mime.php +++ b/lib/internal/Magento/Framework/File/Mime.php @@ -108,6 +108,9 @@ public function getMimeType($file) if (function_exists('mime_content_type')) { $result = $this->getNativeMimeType($file); + } else { + $imageInfo = getimagesize($file); + $result = $imageInfo['mime']; } if (null === $result && isset($this->mimeTypes[$extension])) { diff --git a/lib/internal/Magento/Framework/File/Test/Unit/MimeTest.php b/lib/internal/Magento/Framework/File/Test/Unit/MimeTest.php index 3c571452d7a3e..7a54a7966b500 100644 --- a/lib/internal/Magento/Framework/File/Test/Unit/MimeTest.php +++ b/lib/internal/Magento/Framework/File/Test/Unit/MimeTest.php @@ -58,6 +58,7 @@ public function getMimeTypeDataProvider(): array 'weird extension' => [__DIR__ . '/_files/file.weird', 'application/octet-stream'], 'weird uppercase extension' => [__DIR__ . '/_files/UPPERCASE.WEIRD', 'application/octet-stream'], 'generic mime type' => [__DIR__ . '/_files/blank.html', 'text/html'], + 'tmp file mime type' => [__DIR__ . '/_files/magento', 'image/jpeg'], ]; } } diff --git a/lib/internal/Magento/Framework/File/Test/Unit/_files/magento b/lib/internal/Magento/Framework/File/Test/Unit/_files/magento new file mode 100644 index 0000000000000..c377daf8fb0b3 Binary files /dev/null and b/lib/internal/Magento/Framework/File/Test/Unit/_files/magento differ