Skip to content

Commit

Permalink
MAGETWO-89281: Files and folders symlinked in pub/media cannot be del…
Browse files Browse the repository at this point in the history
…eted from media gallery browser
  • Loading branch information
le0n4eg committed Apr 3, 2018
1 parent 3b0783f commit a6afb59
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 64 deletions.
2 changes: 1 addition & 1 deletion app/code/Magento/Cms/Model/Wysiwyg/Images/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ protected function _validatePath($path)
*/
protected function _sanitizePath($path)
{
return rtrim(preg_replace('~[/\\\]+~', '/', $this->_directory->getDriver()->getRealPath($path)), '/');
return rtrim(preg_replace('~[/\\\]+~', '/', $this->_directory->getDriver()->getRealPathSafety($path)), '/');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,11 @@ class StorageTest extends \PHPUnit\Framework\TestCase
protected function setUp()
{
$this->filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
$this->driverMock = $this->getMockForAbstractClass(
\Magento\Framework\Filesystem\DriverInterface::class,
[],
'',
false,
false,
true,
['getRealPath']
);
$this->driverMock = $this->getMockBuilder(\Magento\Framework\Filesystem\DriverInterface::class)
->setMethods(['getRealPath', 'getRealPathSafety'])
->getMockForAbstractClass();
$this->driverMock->expects($this->any())->method('getRealPath')->will($this->returnArgument(0));
$this->driverMock->expects($this->any())->method('getRealPathSafety')->will($this->returnArgument(0));

$this->directoryMock = $this->createPartialMock(
\Magento\Framework\Filesystem\Directory\Write::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,29 @@ class DeleteFilesTest extends \PHPUnit\Framework\TestCase
/**
* @var string
*/
private $fullDirectoryPath;
private $fileName = 'magento_small_image.jpg';

/**
* @var string
* @var \Magento\Framework\Filesystem
*/
private $fileName = 'magento_small_image.jpg';
private $filesystem;

/**
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManager;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$directoryName = 'directory1';
$filesystem = $objectManager->get(\Magento\Framework\Filesystem::class);
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->filesystem = $this->objectManager->get(\Magento\Framework\Filesystem::class);
/** @var \Magento\Cms\Helper\Wysiwyg\Images $imagesHelper */
$this->imagesHelper = $objectManager->get(\Magento\Cms\Helper\Wysiwyg\Images::class);
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$this->fullDirectoryPath = $this->imagesHelper->getStorageRoot() . '/' . $directoryName;
$this->mediaDirectory->create($this->mediaDirectory->getRelativePath($this->fullDirectoryPath));
$filePath = $this->fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName;
$fixtureDir = realpath(__DIR__ . '/../../../../../Catalog/_files');
copy($fixtureDir . '/' . $this->fileName, $filePath);
$this->model = $objectManager->get(\Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\DeleteFiles::class);
$this->imagesHelper = $this->objectManager->get(\Magento\Cms\Helper\Wysiwyg\Images::class);
$this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$this->model = $this->objectManager->get(\Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\DeleteFiles::class);
}

/**
Expand All @@ -65,17 +64,48 @@ protected function setUp()
*/
public function testExecute()
{
$directoryName = 'directory1';
$fullDirectoryPath = $this->imagesHelper->getStorageRoot() . '/' . $directoryName;
$this->mediaDirectory->create($this->mediaDirectory->getRelativePath($fullDirectoryPath));
$filePath = $fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName;
$fixtureDir = realpath(__DIR__ . '/../../../../../Catalog/_files');
copy($fixtureDir . '/' . $this->fileName, $filePath);

$this->model->getRequest()->setMethod('POST')
->setPostValue('files', [$this->imagesHelper->idEncode($this->fileName)]);
$this->model->getStorage()->getSession()->setCurrentPath($this->fullDirectoryPath);
$this->model->getStorage()->getSession()->setCurrentPath($fullDirectoryPath);
$this->model->execute();
$this->assertFalse(
$this->mediaDirectory->isExist(
$this->mediaDirectory->getRelativePath($this->fullDirectoryPath . '/' . $this->fileName)
$this->mediaDirectory->getRelativePath($fullDirectoryPath . '/' . $this->fileName)
)
);
}

/**
* Execute method with correct directory path and file name to check that files under linked media directory
* can be removed.
*
* @return void
* @magentoDataFixture Magento/Cms/_files/linked_media.php
*/
public function testExecuteWithLinkedMedia()
{
$directoryName = 'linked_media';
$fullDirectoryPath = $this->filesystem->getDirectoryRead(DirectoryList::PUB)
->getAbsolutePath() . DIRECTORY_SEPARATOR . $directoryName;
$filePath = $fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName;
$fixtureDir = realpath(__DIR__ . '/../../../../../Catalog/_files');
copy($fixtureDir . '/' . $this->fileName, $filePath);

$wysiwygDir = $this->mediaDirectory->getAbsolutePath() . '/wysiwyg';
$this->model->getRequest()->setMethod('POST')
->setPostValue('files', [$this->imagesHelper->idEncode($this->fileName)]);
$this->model->getStorage()->getSession()->setCurrentPath($wysiwygDir);
$this->model->execute();
$this->assertFalse(is_file($fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName));
}

/**
* @inheritdoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,66 @@ class DeleteFolderTest extends \PHPUnit\Framework\TestCase
private $mediaDirectory;

/**
* @var string
* @var \Magento\Framework\Filesystem
*/
private $fullDirectoryPath;
private $filesystem;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$filesystem = $objectManager->get(\Magento\Framework\Filesystem::class);
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$this->filesystem = $objectManager->get(\Magento\Framework\Filesystem::class);
$this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
/** @var \Magento\Cms\Helper\Wysiwyg\Images $imagesHelper */
$this->imagesHelper = $objectManager->get(\Magento\Cms\Helper\Wysiwyg\Images::class);
$this->fullDirectoryPath = $this->imagesHelper->getStorageRoot();
$this->model = $objectManager->get(\Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\DeleteFolder::class);
}

/**
* Execute method with correct directory path to check that directories under WYSIWYG media directory
* can be removed.
*
* @return void
*/
public function testExecute()
{
$fullDirectoryPath = $this->imagesHelper->getStorageRoot();
$directoryName = DIRECTORY_SEPARATOR . 'NewDirectory';
$this->mediaDirectory->create(
$this->mediaDirectory->getRelativePath($this->fullDirectoryPath . $directoryName)
$this->mediaDirectory->getRelativePath($fullDirectoryPath . $directoryName)
);
$this->model->getRequest()->setParams(['node' => $this->imagesHelper->idEncode($directoryName)]);
$this->model->execute();
$this->assertFalse(
$this->mediaDirectory->isExist(
$this->mediaDirectory->getRelativePath(
$this->fullDirectoryPath . $directoryName
$fullDirectoryPath . $directoryName
)
)
);
}

/**
* Execute method with correct directory path to check that directories under linked media directory
* can be removed.
*
* @magentoDataFixture Magento/Cms/_files/linked_media.php
*/
public function testExecuteWithLinkedMedia()
{
$linkedDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::PUB);
$linkedDirectoryPath = $this->filesystem->getDirectoryRead(DirectoryList::PUB)
->getAbsolutePath() . 'linked_media';
$directoryName = 'NewDirectory';

$linkedDirectory->create(
$linkedDirectory->getRelativePath($linkedDirectoryPath . DIRECTORY_SEPARATOR . $directoryName)
);
$this->model->getRequest()->setParams(['node' => $this->imagesHelper->idEncode($directoryName)]);
$this->model->execute();
$this->assertFalse(is_dir($linkedDirectoryPath . DIRECTORY_SEPARATOR . $directoryName));
}

/**
* @inheritdoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,47 +26,67 @@ class NewFolderTest extends \PHPUnit\Framework\TestCase
/**
* @var string
*/
private $fullDirectoryPath;
private $dirName= 'NewDirectory';

/**
* @var string
* @var \Magento\Framework\Filesystem
*/
private $dirName= 'NewDirectory';
private $filesystem;

/**
* @var \Magento\Cms\Helper\Wysiwyg\Images
*/
private $imagesHelper;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$filesystem = $objectManager->get(\Magento\Framework\Filesystem::class);
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$this->filesystem = $objectManager->get(\Magento\Framework\Filesystem::class);
/** @var \Magento\Cms\Helper\Wysiwyg\Images $imagesHelper */
$imagesHelper = $objectManager->get(\Magento\Cms\Helper\Wysiwyg\Images::class);
$this->fullDirectoryPath = $imagesHelper->getStorageRoot();
$this->imagesHelper = $objectManager->get(\Magento\Cms\Helper\Wysiwyg\Images::class);
$this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$this->model = $objectManager->get(\Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\NewFolder::class);
}

/**
* Execute method with correct directory path to check that new folder can be created under WYSIWYG media directory.
*
* @return void
* Execute method with correct directory path to check that new folder can be created under linked media directory.
*/
public function testExecute()
{
$fullDirectoryPath = $this->imagesHelper->getStorageRoot();
$this->model->getRequest()->setMethod('POST')
->setPostValue('name', $this->dirName);
$this->model->getStorage()->getSession()->setCurrentPath($this->fullDirectoryPath);
$this->model->getStorage()->getSession()->setCurrentPath($fullDirectoryPath);
$this->model->execute();
$this->assertTrue(
$this->mediaDirectory->isExist(
$this->mediaDirectory->getRelativePath(
$this->fullDirectoryPath . DIRECTORY_SEPARATOR . $this->dirName
$fullDirectoryPath . DIRECTORY_SEPARATOR . $this->dirName
)
)
);
}

/**
* Execute method with correct directory path to check that new folder can be created under WYSIWYG media directory.
*
* @magentoDataFixture Magento/Cms/_files/linked_media.php
*/
public function testExecuteWithLinkedMedia()
{
$linkedDirectoryPath = $this->filesystem->getDirectoryRead(DirectoryList::PUB)
->getAbsolutePath() . DIRECTORY_SEPARATOR . 'linked_media';
$fullDirectoryPath = $this->imagesHelper->getStorageRoot();
$this->model->getRequest()->setMethod('POST')
->setPostValue('name', $this->dirName);
$this->model->getStorage()->getSession()->setCurrentPath($fullDirectoryPath);
$this->model->execute();
$this->assertTrue(is_dir($linkedDirectoryPath . DIRECTORY_SEPARATOR . $this->dirName));
}

/**
* @inheritdoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,29 @@ class UploadTest extends \PHPUnit\Framework\TestCase
/**
* @var string
*/
private $fullDirectoryPath;
private $fileName = 'magento_small_image.jpg';

/**
* @var string
* @var \Magento\Framework\Filesystem
*/
private $fileName = 'magento_small_image.jpg';
private $filesystem;

/**
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManager;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$directoryName = 'directory1';
$filesystem = $objectManager->get(\Magento\Framework\Filesystem::class);
/** @var \Magento\Cms\Helper\Wysiwyg\Images $imagesHelper */
$imagesHelper = $objectManager->get(\Magento\Cms\Helper\Wysiwyg\Images::class);
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$this->fullDirectoryPath = $imagesHelper->getStorageRoot() . DIRECTORY_SEPARATOR . $directoryName;
$this->mediaDirectory->create($this->mediaDirectory->getRelativePath($this->fullDirectoryPath));
$this->model = $objectManager->get(\Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\Upload::class);
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->filesystem = $this->objectManager->get(\Magento\Framework\Filesystem::class);
$this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$this->model = $this->objectManager->get(\Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\Upload::class);
$fixtureDir = realpath(__DIR__ . '/../../../../../Catalog/_files');
$tmpFile = __DIR__ . DIRECTORY_SEPARATOR . $this->fileName;
$tmpFile = $this->filesystem->getDirectoryRead(DirectoryList::PUB)->getAbsolutePath() . $this->fileName;
copy($fixtureDir . DIRECTORY_SEPARATOR . $this->fileName, $tmpFile);
$_FILES = [
'image' => [
Expand All @@ -66,21 +66,47 @@ protected function setUp()
* located under WYSIWYG media.
*
* @return void
* @magentoAppIsolation enabled
*/
public function testExecute()
{
$directoryName = 'directory1';
/** @var \Magento\Cms\Helper\Wysiwyg\Images $imagesHelper */
$imagesHelper = $this->objectManager->get(\Magento\Cms\Helper\Wysiwyg\Images::class);
$fullDirectoryPath = $imagesHelper->getStorageRoot() . DIRECTORY_SEPARATOR . $directoryName;
$this->mediaDirectory->create($this->mediaDirectory->getRelativePath($fullDirectoryPath));

$this->model->getRequest()->setParams(['type' => 'image/png']);
$this->model->getStorage()->getSession()->setCurrentPath($this->fullDirectoryPath);
$this->model->getStorage()->getSession()->setCurrentPath($fullDirectoryPath);
$this->model->execute();
$this->assertTrue(
$this->mediaDirectory->isExist(
$this->mediaDirectory->getRelativePath(
$this->fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName
$fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName
)
)
);
}

/**
* Execute method with correct directory path and file name to check that file can be uploaded to the directory
* located under linked folder.
*
* @return void
* @magentoDataFixture Magento/Cms/_files/linked_media.php
*/
public function testExecuteWithLinkedMedia()
{
$directoryName = 'linked_media';
$fullDirectoryPath = $this->filesystem->getDirectoryRead(DirectoryList::PUB)
->getAbsolutePath() . DIRECTORY_SEPARATOR . $directoryName;
$wysiwygDir = $this->mediaDirectory->getAbsolutePath() . '/wysiwyg';
$this->model->getRequest()->setParams(['type' => 'image/png']);
$this->model->getStorage()->getSession()->setCurrentPath($wysiwygDir);
$this->model->execute();
$this->assertTrue(is_file($fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName));
}

/**
* @inheritdoc
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$directoryName = 'linked_media';
/** @var \Magento\Framework\Filesystem $filesystem */
$filesystem = $objectManager->get(\Magento\Framework\Filesystem::class);
$fullDirectoryPath = $filesystem->getDirectoryRead(Magento\Framework\App\Filesystem\DirectoryList::PUB)
->getAbsolutePath() . $directoryName;
$mediaDirectory = $filesystem->getDirectoryWrite(Magento\Framework\App\Filesystem\DirectoryList::MEDIA);

$wysiwygDir = $mediaDirectory->getAbsolutePath() . 'wysiwyg';
$mediaDirectory->delete($wysiwygDir);
if (!is_dir($fullDirectoryPath)) {
mkdir($fullDirectoryPath);
}
if (is_dir($fullDirectoryPath) && !is_dir($wysiwygDir)) {
symlink($fullDirectoryPath, $wysiwygDir);
}
Loading

0 comments on commit a6afb59

Please sign in to comment.