Skip to content

Commit cc04d2d

Browse files
ENGCOM-1256: #13929: Images can't be uploaded using WYSIWYG if media directory is a symlink #14353
- Merge Pull Request #14353 from mikewhitby/magento2:fix_13929_2.3 - Merged commits: 1. a866bb5
2 parents b18ce4a + a866bb5 commit cc04d2d

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

dev/tests/integration/testsuite/Magento/Framework/App/Filesystem/DirectoryResolverTest.php

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class DirectoryResolverTest extends \PHPUnit\Framework\TestCase
2424
private $directoryResolver;
2525

2626
/**
27-
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
27+
* @var \Magento\Framework\Filesystem
2828
*/
29-
private $directory;
29+
private $filesystem;
3030

3131
/**
3232
* @inheritdoc
@@ -36,22 +36,23 @@ protected function setUp()
3636
$this->objectManager = Bootstrap::getObjectManager();
3737
$this->directoryResolver = $this->objectManager
3838
->create(\Magento\Framework\App\Filesystem\DirectoryResolver::class);
39-
/** @var \Magento\Framework\Filesystem $filesystem */
40-
$filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
41-
$this->directory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
39+
$this->filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
4240
}
4341

4442
/**
4543
* @param string $path
4644
* @param string $directoryConfig
4745
* @param bool $expectation
4846
* @dataProvider validatePathDataProvider
47+
* @throws \Magento\Framework\Exception\FileSystemException
4948
* @magentoAppIsolation enabled
5049
* @return void
5150
*/
5251
public function testValidatePath($path, $directoryConfig, $expectation)
5352
{
54-
$path = $this->directory->getAbsolutePath($path);
53+
$directory = $this->filesystem
54+
->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
55+
$path = $directory->getAbsolutePath($path);
5556
$this->assertEquals($expectation, $this->directoryResolver->validatePath($path, $directoryConfig));
5657
}
5758

@@ -62,10 +63,45 @@ public function testValidatePath($path, $directoryConfig, $expectation)
6263
*/
6364
public function testValidatePathWithException()
6465
{
65-
$path = $this->directory->getAbsolutePath();
66+
$directory = $this->filesystem
67+
->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
68+
$path = $directory->getAbsolutePath();
6669
$this->directoryResolver->validatePath($path, 'wrong_dir');
6770
}
6871

72+
/**
73+
* @param string $path
74+
* @param string $directoryConfig
75+
* @param bool $expectation
76+
* @dataProvider validatePathDataProvider
77+
* @throws \Magento\Framework\Exception\FileSystemException
78+
* @magentoAppIsolation enabled
79+
* @return void
80+
*/
81+
public function testValidatePathWithSymlink($path, $directoryConfig, $expectation)
82+
{
83+
$directory = $this->filesystem
84+
->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::PUB);
85+
$driver = $directory->getDriver();
86+
87+
$mediaPath = $directory->getAbsolutePath('media');
88+
$mediaMovedPath = $directory->getAbsolutePath('moved-media');
89+
90+
try {
91+
$driver->rename($mediaPath, $mediaMovedPath);
92+
$driver->symlink($mediaMovedPath, $mediaPath);
93+
$this->testValidatePath($path, $directoryConfig, $expectation);
94+
} finally {
95+
// be defensive in case some operations failed
96+
if ($driver->isExists($mediaPath) && $driver->isExists($mediaMovedPath)) {
97+
$driver->deleteFile($mediaPath);
98+
$driver->rename($mediaMovedPath, $mediaPath);
99+
} elseif ($driver->isExists($mediaMovedPath)) {
100+
$driver->rename($mediaMovedPath, $mediaPath);
101+
}
102+
}
103+
}
104+
69105
/**
70106
* @return array
71107
*/

lib/internal/Magento/Framework/App/Filesystem/DirectoryResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public function __construct(DirectoryList $directoryList)
3939
public function validatePath($path, $directoryConfig = DirectoryList::MEDIA)
4040
{
4141
$realPath = realpath($path);
42-
$root = $this->directoryList->getPath($directoryConfig);
43-
42+
$root = realpath($this->directoryList->getPath($directoryConfig));
43+
4444
return strpos($realPath, $root) === 0;
4545
}
4646
}

0 commit comments

Comments
 (0)