Skip to content

Commit b16764a

Browse files
committed
MAGETWO-95551: Adding restriction for controller
1 parent ed27a7a commit b16764a

File tree

2 files changed

+57
-23
lines changed

2 files changed

+57
-23
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)) && !preg_match('#^\.htaccess$#', $file)) {
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: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ protected function setUp()
6060
$this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
6161
$this->model = $this->objectManager->get(\Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\DeleteFiles::class);
6262
$this->fullDirectoryPath = $this->imagesHelper->getStorageRoot() . '/directory1';
63-
$path = $this->fullDirectoryPath . '/.htaccess';
64-
if (!$this->mediaDirectory->isFile($path)) {
65-
$this->mediaDirectory->writeFile($path, "Order deny,allow\nDeny from all");
66-
}
6763
}
6864

6965
/**
@@ -74,20 +70,15 @@ protected function setUp()
7470
*/
7571
public function testExecute()
7672
{
77-
$directoryName = 'directory1';
78-
$fullDirectoryPath = $this->imagesHelper->getStorageRoot() . '/' . $directoryName;
79-
$this->mediaDirectory->create($this->mediaDirectory->getRelativePath($fullDirectoryPath));
80-
$filePath = $fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName;
73+
$this->mediaDirectory->create($this->mediaDirectory->getRelativePath($this->fullDirectoryPath));
74+
$filePath = $this->fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName;
8175
$fixtureDir = realpath(__DIR__ . '/../../../../../Catalog/_files');
8276
copy($fixtureDir . '/' . $this->fileName, $filePath);
8377

84-
$this->model->getRequest()->setMethod('POST')
85-
->setPostValue('files', [$this->imagesHelper->idEncode($this->fileName)]);
86-
$this->model->getStorage()->getSession()->setCurrentPath($fullDirectoryPath);
87-
$this->model->execute();
78+
$this->executeFileDelete($this->fullDirectoryPath, $this->fileName);
8879
$this->assertFalse(
8980
$this->mediaDirectory->isExist(
90-
$this->mediaDirectory->getRelativePath($fullDirectoryPath . '/' . $this->fileName)
81+
$this->mediaDirectory->getRelativePath($this->fullDirectoryPath . '/' . $this->fileName)
9182
)
9283
);
9384
}
@@ -109,10 +100,7 @@ public function testExecuteWithLinkedMedia()
109100
copy($fixtureDir . '/' . $this->fileName, $filePath);
110101

111102
$wysiwygDir = $this->mediaDirectory->getAbsolutePath() . '/wysiwyg';
112-
$this->model->getRequest()->setMethod('POST')
113-
->setPostValue('files', [$this->imagesHelper->idEncode($this->fileName)]);
114-
$this->model->getStorage()->getSession()->setCurrentPath($wysiwygDir);
115-
$this->model->execute();
103+
$this->executeFileDelete($wysiwygDir, $this->fileName);
116104
$this->assertFalse(is_file($fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName));
117105
}
118106

@@ -124,18 +112,64 @@ public function testExecuteWithLinkedMedia()
124112
*/
125113
public function testDeleteHtaccess()
126114
{
127-
$this->model->getRequest()->setMethod('POST')
128-
->setPostValue('files', [$this->imagesHelper->idEncode('.htaccess')]);
129-
$this->model->getStorage()->getSession()->setCurrentPath($this->fullDirectoryPath);
130-
$this->model->execute();
115+
$this->createFile($this->fullDirectoryPath, '.htaccess');
116+
$this->executeFileDelete($this->fullDirectoryPath, '.htaccess');
131117

132118
$this->assertTrue(
133119
$this->mediaDirectory->isExist(
134-
$this->mediaDirectory->getRelativePath($this->fullDirectoryPath . '/' . '.htaccess')
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')
135139
)
136140
);
137141
}
138142

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+
{
167+
$this->model->getRequest()->setMethod('POST')
168+
->setPostValue('files', [$this->imagesHelper->idEncode($fileName)]);
169+
$this->model->getStorage()->getSession()->setCurrentPath($path);
170+
$this->model->execute();
171+
}
172+
139173
/**
140174
* @inheritdoc
141175
*/

0 commit comments

Comments
 (0)