Skip to content

Commit 5b1b70d

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-95551' into 2.2.7-develop-pr53
2 parents 3432f2b + b16764a commit 5b1b70d

File tree

2 files changed

+77
-13
lines changed

2 files changed

+77
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function execute()
7979
$filesystem = $this->_objectManager->get(\Magento\Framework\Filesystem::class);
8080
$dir = $filesystem->getDirectoryRead(DirectoryList::MEDIA);
8181
$filePath = $path . '/' . \Magento\Framework\File\Uploader::getCorrectFileName($file);
82-
if ($dir->isFile($dir->getRelativePath($filePath))) {
82+
if ($dir->isFile($dir->getRelativePath($filePath)) && !preg_match('/^\.htaccess$/', $file)) {
8383
$this->getStorage()->deleteFile($filePath);
8484
}
8585
}

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

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ class DeleteFilesTest extends \PHPUnit\Framework\TestCase
4343
*/
4444
private $objectManager;
4545

46+
/**
47+
* @var string
48+
*/
49+
private $fullDirectoryPath;
50+
4651
/**
4752
* @inheritdoc
4853
*/
@@ -54,6 +59,7 @@ protected function setUp()
5459
$this->imagesHelper = $this->objectManager->get(\Magento\Cms\Helper\Wysiwyg\Images::class);
5560
$this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
5661
$this->model = $this->objectManager->get(\Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\DeleteFiles::class);
62+
$this->fullDirectoryPath = $this->imagesHelper->getStorageRoot() . '/directory1';
5763
}
5864

5965
/**
@@ -64,20 +70,15 @@ protected function setUp()
6470
*/
6571
public function testExecute()
6672
{
67-
$directoryName = 'directory1';
68-
$fullDirectoryPath = $this->imagesHelper->getStorageRoot() . '/' . $directoryName;
69-
$this->mediaDirectory->create($this->mediaDirectory->getRelativePath($fullDirectoryPath));
70-
$filePath = $fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName;
73+
$this->mediaDirectory->create($this->mediaDirectory->getRelativePath($this->fullDirectoryPath));
74+
$filePath = $this->fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName;
7175
$fixtureDir = realpath(__DIR__ . '/../../../../../Catalog/_files');
7276
copy($fixtureDir . '/' . $this->fileName, $filePath);
7377

74-
$this->model->getRequest()->setMethod('POST')
75-
->setPostValue('files', [$this->imagesHelper->idEncode($this->fileName)]);
76-
$this->model->getStorage()->getSession()->setCurrentPath($fullDirectoryPath);
77-
$this->model->execute();
78+
$this->executeFileDelete($this->fullDirectoryPath, $this->fileName);
7879
$this->assertFalse(
7980
$this->mediaDirectory->isExist(
80-
$this->mediaDirectory->getRelativePath($fullDirectoryPath . '/' . $this->fileName)
81+
$this->mediaDirectory->getRelativePath($this->fullDirectoryPath . '/' . $this->fileName)
8182
)
8283
);
8384
}
@@ -99,11 +100,74 @@ public function testExecuteWithLinkedMedia()
99100
copy($fixtureDir . '/' . $this->fileName, $filePath);
100101

101102
$wysiwygDir = $this->mediaDirectory->getAbsolutePath() . '/wysiwyg';
103+
$this->executeFileDelete($wysiwygDir, $this->fileName);
104+
$this->assertFalse(is_file($fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName));
105+
}
106+
107+
/**
108+
* Check that htaccess file couldn't be removed via
109+
* \Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\DeleteFiles::execute method
110+
*
111+
* @return void
112+
*/
113+
public function testDeleteHtaccess()
114+
{
115+
$this->createFile($this->fullDirectoryPath, '.htaccess');
116+
$this->executeFileDelete($this->fullDirectoryPath, '.htaccess');
117+
118+
$this->assertTrue(
119+
$this->mediaDirectory->isExist(
120+
$this->mediaDirectory->getRelativePath($this->fullDirectoryPath . '/.htaccess')
121+
)
122+
);
123+
}
124+
125+
/**
126+
* Check that random file could be removed via
127+
* \Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\DeleteFiles::execute method
128+
*
129+
* @return void
130+
*/
131+
public function testDeleteAnyFile()
132+
{
133+
$this->createFile($this->fullDirectoryPath, 'ahtaccess');
134+
$this->executeFileDelete($this->fullDirectoryPath, 'ahtaccess');
135+
136+
$this->assertFalse(
137+
$this->mediaDirectory->isExist(
138+
$this->mediaDirectory->getRelativePath($this->fullDirectoryPath . '/ahtaccess')
139+
)
140+
);
141+
}
142+
143+
/**
144+
* Create file.
145+
*
146+
* @param string $path
147+
* @param string $fileName
148+
* @return void
149+
*/
150+
private function createFile(string $path, string $fileName)
151+
{
152+
$file = $path . '/' . $fileName;
153+
if (!$this->mediaDirectory->isFile($file)) {
154+
$this->mediaDirectory->writeFile($file, 'Content');
155+
}
156+
}
157+
158+
/**
159+
* Execute file delete operation.
160+
*
161+
* @param string $path
162+
* @param string $fileName
163+
* @return void
164+
*/
165+
private function executeFileDelete(string $path, string $fileName)
166+
{
102167
$this->model->getRequest()->setMethod('POST')
103-
->setPostValue('files', [$this->imagesHelper->idEncode($this->fileName)]);
104-
$this->model->getStorage()->getSession()->setCurrentPath($wysiwygDir);
168+
->setPostValue('files', [$this->imagesHelper->idEncode($fileName)]);
169+
$this->model->getStorage()->getSession()->setCurrentPath($path);
105170
$this->model->execute();
106-
$this->assertFalse(is_file($fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName));
107171
}
108172

109173
/**

0 commit comments

Comments
 (0)