Skip to content

Commit

Permalink
Merge pull request #5438 from magento-borg/2.4.0-bugfixes-030929
Browse files Browse the repository at this point in the history
Fixed Issues:
- MC-31362: Media folder update
- MC-30944: Incorrect flow of saving Newsletter templates
- MC-31357: Customer file uploader update
  • Loading branch information
dvoskoboinikov authored Apr 28, 2020
2 parents dbc71bc + 09bc685 commit 58844c0
Show file tree
Hide file tree
Showing 14 changed files with 845 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Cms\Controller\Adminhtml\Wysiwyg\Images;

use Magento\Framework\App\Action\HttpPostActionInterface;
Expand Down Expand Up @@ -60,13 +63,8 @@ public function execute()
{
try {
$path = $this->getStorage()->getCmsWysiwygImages()->getCurrentPath();
if (!$this->directoryResolver->validatePath($path, DirectoryList::MEDIA)) {
throw new \Magento\Framework\Exception\LocalizedException(
__('Directory %1 is not under storage root path.', $path)
);
}
$this->getStorage()->deleteDirectory($path);

return $this->resultRawFactory->create();
} catch (\Exception $e) {
$result = ['error' => true, 'message' => $e->getMessage()];
Expand Down
41 changes: 36 additions & 5 deletions app/code/Magento/Cms/Model/Wysiwyg/Images/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ public function __construct(
*
* @param string $path
* @return void
* @throws \Magento\Framework\Exception\FileSystemException
* @throws \Magento\Framework\Exception\ValidatorException
*/
protected function createSubDirectories($path)
{
Expand Down Expand Up @@ -295,6 +297,7 @@ protected function removeItemFromCollection($collection, $conditions)
*
* @param string $path Parent directory path
* @return \Magento\Framework\Data\Collection\Filesystem
* @throws \Exception
*/
public function getDirsCollection($path)
{
Expand Down Expand Up @@ -393,6 +396,7 @@ public function getFilesCollection($path, $type = null)
*
* @param string $path Path to the directory
* @return \Magento\Cms\Model\Wysiwyg\Images\Storage\Collection
* @throws \Exception
*/
public function getCollection($path = null)
{
Expand Down Expand Up @@ -485,6 +489,9 @@ public function deleteDirectory($path)
*
* @param string $path
* @return void
* @throws \Magento\Framework\Exception\FileSystemException
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\ValidatorException
*/
protected function _deleteByPath($path)
{
Expand All @@ -500,6 +507,8 @@ protected function _deleteByPath($path)
*
* @param string $target File path to be deleted
* @return $this
* @throws \Magento\Framework\Exception\FileSystemException
* @throws \Magento\Framework\Exception\ValidatorException
*/
public function deleteFile($target)
{
Expand Down Expand Up @@ -561,9 +570,11 @@ public function uploadFile($targetPath, $type = null)
/**
* Thumbnail path getter
*
* @param string $filePath original file path
* @param bool $checkFile OPTIONAL is it necessary to check file availability
* @param string $filePath original file path
* @param bool $checkFile OPTIONAL is it necessary to check file availability
* @return string|false
* @throws \Magento\Framework\Exception\FileSystemException
* @throws \Magento\Framework\Exception\ValidatorException
*/
public function getThumbnailPath($filePath, $checkFile = false)
{
Expand All @@ -587,9 +598,11 @@ public function getThumbnailPath($filePath, $checkFile = false)
/**
* Thumbnail URL getter
*
* @param string $filePath original file path
* @param bool $checkFile OPTIONAL is it necessary to check file availability
* @param string $filePath original file path
* @param bool $checkFile OPTIONAL is it necessary to check file availability
* @return string|false
* @throws \Magento\Framework\Exception\FileSystemException
* @throws \Magento\Framework\Exception\ValidatorException
*/
public function getThumbnailUrl($filePath, $checkFile = false)
{
Expand All @@ -610,6 +623,8 @@ public function getThumbnailUrl($filePath, $checkFile = false)
* @param string $source Image path to be resized
* @param bool $keepRatio Keep aspect ratio or not
* @return bool|string Resized filepath or false if errors were occurred
* @throws \Magento\Framework\Exception\FileSystemException
* @throws \Magento\Framework\Exception\ValidatorException
*/
public function resizeFile($source, $keepRatio = true)
{
Expand Down Expand Up @@ -643,6 +658,9 @@ public function resizeFile($source, $keepRatio = true)
*
* @param string $filename File basename
* @return bool|string Thumbnail path or false for errors
* @throws \Magento\Framework\Exception\FileSystemException
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\ValidatorException
*/
public function resizeOnTheFly($filename)
{
Expand All @@ -658,6 +676,8 @@ public function resizeOnTheFly($filename)
*
* @param bool|string $filePath Path to the file
* @return string
* @throws \Magento\Framework\Exception\FileSystemException
* @throws \Magento\Framework\Exception\ValidatorException
*/
public function getThumbsPath($filePath = false)
{
Expand Down Expand Up @@ -782,17 +802,28 @@ protected function _validatePath($path)
*
* @param string $path
* @return string
* @throws \Magento\Framework\Exception\ValidatorException
*/
protected function _sanitizePath($path)
{
return rtrim(preg_replace('~[/\\\]+~', '/', $this->_directory->getDriver()->getRealPathSafety($path)), '/');
return rtrim(
preg_replace(
'~[/\\\]+~',
'/',
$this->_directory->getDriver()->getRealPathSafety(
$this->_directory->getAbsolutePath($path)
)
),
'/'
);
}

/**
* Get path in root storage dir
*
* @param string $path
* @return string|bool
* @throws \Magento\Framework\Exception\ValidatorException
*/
protected function _getRelativePathToRoot($path)
{
Expand Down
40 changes: 21 additions & 19 deletions app/code/Magento/Cms/Test/Unit/Model/Wysiwyg/Images/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ class StorageTest extends \PHPUnit\Framework\TestCase
protected $imagesStorage;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var \PHPUnit\Framework\MockObject\MockObject
*/
protected $filesystemMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var \PHPUnit\Framework\MockObject\MockObject
*/
protected $adapterFactoryMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var \PHPUnit\Framework\MockObject\MockObject
*/
protected $imageHelperMock;

Expand All @@ -49,67 +49,67 @@ class StorageTest extends \PHPUnit\Framework\TestCase
protected $resizeParameters;

/**
* @var \Magento\Cms\Model\Wysiwyg\Images\Storage\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
* @var \Magento\Cms\Model\Wysiwyg\Images\Storage\CollectionFactory|\PHPUnit\Framework\MockObject\MockObject
*/
protected $storageCollectionFactoryMock;

/**
* @var \Magento\MediaStorage\Model\File\Storage\FileFactory|\PHPUnit_Framework_MockObject_MockObject
* @var \Magento\MediaStorage\Model\File\Storage\FileFactory|\PHPUnit\Framework\MockObject\MockObject
*/
protected $storageFileFactoryMock;

/**
* @var \Magento\MediaStorage\Model\File\Storage\DatabaseFactory|\PHPUnit_Framework_MockObject_MockObject
* @var \Magento\MediaStorage\Model\File\Storage\DatabaseFactory|\PHPUnit\Framework\MockObject\MockObject
*/
protected $storageDatabaseFactoryMock;

/**
* @var \Magento\MediaStorage\Model\File\Storage\Directory\DatabaseFactory|\PHPUnit_Framework_MockObject_MockObject
* @var \Magento\MediaStorage\Model\File\Storage\Directory\DatabaseFactory|\PHPUnit\Framework\MockObject\MockObject
*/
protected $directoryDatabaseFactoryMock;

/**
* @var \Magento\MediaStorage\Model\File\Storage\Directory\Database|\PHPUnit_Framework_MockObject_MockObject
* @var \Magento\MediaStorage\Model\File\Storage\Directory\Database|\PHPUnit\Framework\MockObject\MockObject
*/
protected $directoryCollectionMock;

/**
* @var \Magento\MediaStorage\Model\File\UploaderFactory|\PHPUnit_Framework_MockObject_MockObject
* @var \Magento\MediaStorage\Model\File\UploaderFactory|\PHPUnit\Framework\MockObject\MockObject
*/
protected $uploaderFactoryMock;

/**
* @var \Magento\Backend\Model\Session|\PHPUnit_Framework_MockObject_MockObject
* @var \Magento\Backend\Model\Session|\PHPUnit\Framework\MockObject\MockObject
*/
protected $sessionMock;

/**
* @var \Magento\Backend\Model\Url|\PHPUnit_Framework_MockObject_MockObject
* @var \Magento\Backend\Model\Url|\PHPUnit\Framework\MockObject\MockObject
*/
protected $backendUrlMock;

/**
* @var \Magento\Framework\Filesystem\Directory\Write|\PHPUnit_Framework_MockObject_MockObject
* @var \Magento\Framework\Filesystem\Directory\Write|\PHPUnit\Framework\MockObject\MockObject
*/
protected $directoryMock;

/**
* @var \Magento\Framework\Filesystem\DriverInterface|\PHPUnit_Framework_MockObject_MockObject
* @var \Magento\Framework\Filesystem\DriverInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $driverMock;

/**
* @var \Magento\MediaStorage\Helper\File\Storage\Database|\PHPUnit_Framework_MockObject_MockObject
* @var \Magento\MediaStorage\Helper\File\Storage\Database|\PHPUnit\Framework\MockObject\MockObject
*/
protected $coreFileStorageMock;

/**
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager|\PHPUnit_Framework_MockObject_MockObject
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager|\PHPUnit\Framework\MockObject\MockObject
*/
protected $objectManagerHelper;

/**
* @var \Magento\Framework\Filesystem\Io\File|\PHPUnit_Framework_MockObject_MockObject
* @var \Magento\Framework\Filesystem\Io\File|\PHPUnit\Framework\MockObject\MockObject
*/
protected $ioFileMock;

Expand Down Expand Up @@ -139,7 +139,7 @@ protected function setUp()

$this->directoryMock = $this->createPartialMock(
\Magento\Framework\Filesystem\Directory\Write::class,
['delete', 'getDriver', 'create', 'getRelativePath', 'isExist', 'isFile']
['delete', 'getDriver', 'create', 'getRelativePath', 'getAbsolutePath', 'isExist', 'isFile']
);
$this->directoryMock->expects(
$this->any()
Expand Down Expand Up @@ -283,6 +283,7 @@ public function testGetResizeHeight()
public function testDeleteDirectoryOverRoot()
{
$this->driverMock->expects($this->atLeastOnce())->method('getRealPathSafety')->will($this->returnArgument(0));
$this->directoryMock->expects($this->atLeastOnce())->method('getAbsolutePath')->will($this->returnArgument(0));
$this->imagesStorage->deleteDirectory(self::INVALID_DIRECTORY_OVER_ROOT);
}

Expand All @@ -294,6 +295,7 @@ public function testDeleteDirectoryOverRoot()
public function testDeleteRootDirectory()
{
$this->driverMock->expects($this->atLeastOnce())->method('getRealPathSafety')->will($this->returnArgument(0));
$this->directoryMock->expects($this->atLeastOnce())->method('getAbsolutePath')->will($this->returnArgument(0));
$this->imagesStorage->deleteDirectory(self::STORAGE_ROOT_DIR);
}

Expand Down Expand Up @@ -355,7 +357,7 @@ public function testGetDirsCollection($exclude, $include, $fileNames, $expectedR

$collection = [];
foreach ($fileNames as $filename) {
/** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject $objectMock */
/** @var \Magento\Framework\DataObject|\PHPUnit\Framework\MockObject\MockObject $objectMock */
$objectMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getFilename']);
$objectMock->expects($this->any())
->method('getFilename')
Expand Down Expand Up @@ -428,7 +430,7 @@ public function dirsCollectionDataProvider()
*/
protected function generalTestGetDirsCollection($path, $collectionArray = [], $expectedRemoveKeys = [])
{
/** @var StorageCollection|\PHPUnit_Framework_MockObject_MockObject $storageCollectionMock */
/** @var StorageCollection|\PHPUnit\Framework\MockObject\MockObject $storageCollectionMock */
$storageCollectionMock = $this->getMockBuilder(\Magento\Cms\Model\Wysiwyg\Images\Storage\Collection::class)
->disableOriginalConstructor()
->getMock();
Expand Down
Loading

0 comments on commit 58844c0

Please sign in to comment.