Skip to content

Commit

Permalink
magento#13929: Images can't be uploaded using WYSIWYG if media direct…
Browse files Browse the repository at this point in the history
…ory is a symlink

- Added realpath() to root dir to resolve symlinks
- Added tests
  • Loading branch information
mikewhitby committed Mar 24, 2018
1 parent 068a97e commit a866bb5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class DirectoryResolverTest extends \PHPUnit\Framework\TestCase
private $directoryResolver;

/**
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
* @var \Magento\Framework\Filesystem
*/
private $directory;
private $filesystem;

/**
* @inheritdoc
Expand All @@ -36,22 +36,23 @@ protected function setUp()
$this->objectManager = Bootstrap::getObjectManager();
$this->directoryResolver = $this->objectManager
->create(\Magento\Framework\App\Filesystem\DirectoryResolver::class);
/** @var \Magento\Framework\Filesystem $filesystem */
$filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
$this->directory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
$this->filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
}

/**
* @param string $path
* @param string $directoryConfig
* @param bool $expectation
* @dataProvider validatePathDataProvider
* @throws \Magento\Framework\Exception\FileSystemException
* @magentoAppIsolation enabled
* @return void
*/
public function testValidatePath($path, $directoryConfig, $expectation)
{
$path = $this->directory->getAbsolutePath($path);
$directory = $this->filesystem
->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
$path = $directory->getAbsolutePath($path);
$this->assertEquals($expectation, $this->directoryResolver->validatePath($path, $directoryConfig));
}

Expand All @@ -62,10 +63,45 @@ public function testValidatePath($path, $directoryConfig, $expectation)
*/
public function testValidatePathWithException()
{
$path = $this->directory->getAbsolutePath();
$directory = $this->filesystem
->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
$path = $directory->getAbsolutePath();
$this->directoryResolver->validatePath($path, 'wrong_dir');
}

/**
* @param string $path
* @param string $directoryConfig
* @param bool $expectation
* @dataProvider validatePathDataProvider
* @throws \Magento\Framework\Exception\FileSystemException
* @magentoAppIsolation enabled
* @return void
*/
public function testValidatePathWithSymlink($path, $directoryConfig, $expectation)
{
$directory = $this->filesystem
->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::PUB);
$driver = $directory->getDriver();

$mediaPath = $directory->getAbsolutePath('media');
$mediaMovedPath = $directory->getAbsolutePath('moved-media');

try {
$driver->rename($mediaPath, $mediaMovedPath);
$driver->symlink($mediaMovedPath, $mediaPath);
$this->testValidatePath($path, $directoryConfig, $expectation);
} finally {
// be defensive in case some operations failed
if ($driver->isExists($mediaPath) && $driver->isExists($mediaMovedPath)) {
$driver->deleteFile($mediaPath);
$driver->rename($mediaMovedPath, $mediaPath);
} elseif ($driver->isExists($mediaMovedPath)) {
$driver->rename($mediaMovedPath, $mediaPath);
}
}
}

/**
* @return array
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public function __construct(DirectoryList $directoryList)
public function validatePath($path, $directoryConfig = DirectoryList::MEDIA)
{
$realPath = realpath($path);
$root = $this->directoryList->getPath($directoryConfig);
$root = realpath($this->directoryList->getPath($directoryConfig));

return strpos($realPath, $root) === 0;
}
}

0 comments on commit a866bb5

Please sign in to comment.