Skip to content

Commit dc3f34a

Browse files
committed
Merge remote-tracking branch 'patrik/issue/as-889' into 2.4-develop-fast-lane-prs
2 parents 94ad7f7 + 07f0c70 commit dc3f34a

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

app/code/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/DeleteFiles.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
*/
66
namespace Magento\Cms\Controller\Adminhtml\Wysiwyg\Images;
77

8-
use Magento\Framework\App\Filesystem\DirectoryList;
98
use Magento\Framework\App\Action\HttpPostActionInterface;
9+
use Magento\Framework\App\Filesystem\DirectoryList;
1010

1111
/**
1212
* Delete image files.
@@ -60,10 +60,15 @@ public function __construct(
6060
*/
6161
public function execute()
6262
{
63+
$resultJson = $this->resultJsonFactory->create();
64+
65+
if (!$this->getRequest()->isPost()) {
66+
$result = ['error' => true, 'message' => __('Wrong request.')];
67+
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
68+
return $resultJson->setData($result);
69+
}
70+
6371
try {
64-
if (!$this->getRequest()->isPost()) {
65-
throw new \Exception('Wrong request.');
66-
}
6772
$files = $this->getRequest()->getParam('files');
6873

6974
/** @var $helper \Magento\Cms\Helper\Wysiwyg\Images */
@@ -79,17 +84,16 @@ public function execute()
7984
/** @var \Magento\Framework\Filesystem $filesystem */
8085
$filesystem = $this->_objectManager->get(\Magento\Framework\Filesystem::class);
8186
$dir = $filesystem->getDirectoryRead(DirectoryList::MEDIA);
82-
$filePath = $path . '/' . \Magento\Framework\File\Uploader::getCorrectFileName($file);
87+
$filePath = $path . '/' . $file;
8388
if ($dir->isFile($dir->getRelativePath($filePath)) && !preg_match('#.htaccess#', $file)) {
8489
$this->getStorage()->deleteFile($filePath);
8590
}
8691
}
87-
92+
8893
return $this->resultRawFactory->create();
94+
// phpcs:ignore Magento2.Exceptions.ThrowCatch
8995
} catch (\Exception $e) {
9096
$result = ['error' => true, 'message' => $e->getMessage()];
91-
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
92-
$resultJson = $this->resultJsonFactory->create();
9397

9498
return $resultJson->setData($result);
9599
}

dev/tests/integration/testsuite/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/DeleteFilesTest.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,47 @@ protected function setUp()
7575
* Execute method with correct directory path and file name to check that files under WYSIWYG media directory
7676
* can be removed.
7777
*
78+
* @param string $filename
7879
* @return void
80+
* @dataProvider executeDataProvider
7981
*/
80-
public function testExecute()
82+
public function testExecute(string $filename)
8183
{
84+
$filePath = $this->fullDirectoryPath . DIRECTORY_SEPARATOR . $filename;
85+
$fixtureDir = realpath(__DIR__ . '/../../../../../Catalog/_files');
86+
copy($fixtureDir . '/' . $this->fileName, $filePath);
87+
8288
$this->model->getRequest()->setMethod('POST')
83-
->setPostValue('files', [$this->imagesHelper->idEncode($this->fileName)]);
89+
->setPostValue('files', [$this->imagesHelper->idEncode($filename)]);
8490
$this->model->getStorage()->getSession()->setCurrentPath($this->fullDirectoryPath);
8591
$this->model->execute();
8692

8793
$this->assertFalse(
8894
$this->mediaDirectory->isExist(
89-
$this->mediaDirectory->getRelativePath($this->fullDirectoryPath . '/' . $this->fileName)
95+
$this->mediaDirectory->getRelativePath($this->fullDirectoryPath . '/' . $filename)
9096
)
9197
);
9298
}
9399

100+
/**
101+
* DataProvider for testExecute
102+
*
103+
* @return array
104+
*/
105+
public function executeDataProvider(): array
106+
{
107+
return [
108+
['name with spaces.jpg'],
109+
['name with, comma.jpg'],
110+
['name with* asterisk.jpg'],
111+
['name with[ bracket.jpg'],
112+
['magento_small_image.jpg'],
113+
['_.jpg'],
114+
[' - .jpg'],
115+
['-.jpg'],
116+
];
117+
}
118+
94119
/**
95120
* Check that htaccess file couldn't be removed via
96121
* \Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\DeleteFiles::execute method

0 commit comments

Comments
 (0)