Skip to content

Commit 525cf22

Browse files
committed
MC-31733: Media gallery breaks in some filesystems
1 parent 5d8f22b commit 525cf22

File tree

4 files changed

+116
-36
lines changed

4 files changed

+116
-36
lines changed

app/code/Magento/Theme/Helper/Storage.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,7 @@ public function getCurrentPath()
255255
if ($path && $path !== self::NODE_ROOT) {
256256
$path = $this->convertIdToPath($path);
257257

258-
$realPath = $this->filesystemDriver->getRealPath(
259-
$this->filesystemDriver->getRealPathSafety($path)
260-
);
258+
$realPath = $this->filesystemDriver->getRealPathSafety($path);
261259

262260
$path = $realPath ?: $path;
263261

app/code/Magento/Theme/Model/Wysiwyg/Storage.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Framework\App\Filesystem\DirectoryList;
1010
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\Filesystem\DriverInterface;
1112

1213
/**
1314
* Theme wysiwyg storage model
@@ -86,6 +87,11 @@ class Storage
8687
*/
8788
private $file;
8889

90+
/**
91+
* @var DriverInterface
92+
*/
93+
private $filesystemDriver;
94+
8995
/**
9096
* Initialize dependencies
9197
*
@@ -96,6 +102,7 @@ class Storage
96102
* @param \Magento\Framework\Url\EncoderInterface $urlEncoder
97103
* @param \Magento\Framework\Url\DecoderInterface $urlDecoder
98104
* @param \Magento\Framework\Filesystem\Io\File|null $file
105+
* @param DriverInterface|null $filesystemDriver
99106
*
100107
* @throws \Magento\Framework\Exception\FileSystemException
101108
*/
@@ -106,7 +113,8 @@ public function __construct(
106113
\Magento\Framework\Image\AdapterFactory $imageFactory,
107114
\Magento\Framework\Url\EncoderInterface $urlEncoder,
108115
\Magento\Framework\Url\DecoderInterface $urlDecoder,
109-
\Magento\Framework\Filesystem\Io\File $file = null
116+
\Magento\Framework\Filesystem\Io\File $file = null,
117+
DriverInterface $filesystemDriver = null
110118
) {
111119
$this->mediaWriteDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
112120
$this->_helper = $helper;
@@ -117,6 +125,8 @@ public function __construct(
117125
$this->file = $file ?: ObjectManager::getInstance()->get(
118126
\Magento\Framework\Filesystem\Io\File::class
119127
);
128+
$this->filesystemDriver = $filesystemDriver ?: ObjectManager::getInstance()
129+
->get(DriverInterface::class);
120130
}
121131

122132
/**
@@ -331,8 +341,9 @@ public function deleteDirectory($path)
331341
{
332342
$rootCmp = rtrim($this->_helper->getStorageRoot(), '/');
333343
$pathCmp = rtrim($path, '/');
344+
$absolutePath = $this->filesystemDriver->getRealPathSafety($this->mediaWriteDirectory->getAbsolutePath($path));
334345

335-
if ($rootCmp == $pathCmp || $rootCmp === $this->mediaWriteDirectory->getAbsolutePath($path)) {
346+
if ($rootCmp == $pathCmp || $rootCmp === $absolutePath) {
336347
throw new \Magento\Framework\Exception\LocalizedException(
337348
__('We can\'t delete root directory %1 right now.', $path)
338349
);

app/code/Magento/Theme/Test/Unit/Helper/StorageTest.php

Lines changed: 83 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
*/
1010
namespace Magento\Theme\Test\Unit\Helper;
1111

12+
use Magento\Framework\Filesystem\DriverInterface;
1213
use Magento\Theme\Helper\Storage;
14+
use PHPUnit\Framework\MockObject\MockObject;
1315

1416
/**
1517
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -37,7 +39,7 @@ class StorageTest extends \PHPUnit\Framework\TestCase
3739
protected $request;
3840

3941
/**
40-
* @var \Magento\Theme\Helper\Storage
42+
* @var Storage
4143
*/
4244
protected $helper;
4345

@@ -78,6 +80,11 @@ class StorageTest extends \PHPUnit\Framework\TestCase
7880

7981
protected $requestParams;
8082

83+
/**
84+
* @var DriverInterface|MockObject
85+
*/
86+
private $filesystemDriver;
87+
8188
protected function setUp()
8289
{
8390
$this->customizationPath = '/' . implode('/', ['var', 'theme']);
@@ -102,25 +109,28 @@ protected function setUp()
102109
$this->contextHelper->expects($this->any())->method('getUrlEncoder')->willReturn($this->urlEncoder);
103110
$this->contextHelper->expects($this->any())->method('getUrlDecoder')->willReturn($this->urlDecoder);
104111
$this->themeFactory->expects($this->any())->method('create')->willReturn($this->theme);
112+
$this->filesystemDriver = $this->createMock(DriverInterface::class);
105113

106114
$this->theme->expects($this->any())
107115
->method('getCustomization')
108116
->will($this->returnValue($this->customization));
109117

110118
$this->request->expects($this->at(0))
111119
->method('getParam')
112-
->with(\Magento\Theme\Helper\Storage::PARAM_THEME_ID)
120+
->with(Storage::PARAM_THEME_ID)
113121
->will($this->returnValue(6));
114122
$this->request->expects($this->at(1))
115123
->method('getParam')
116-
->with(\Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE)
124+
->with(Storage::PARAM_CONTENT_TYPE)
117125
->will($this->returnValue(\Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE));
118126

119-
$this->helper = new \Magento\Theme\Helper\Storage(
127+
$this->helper = new Storage(
120128
$this->contextHelper,
121129
$this->filesystem,
122130
$this->session,
123-
$this->themeFactory
131+
$this->themeFactory,
132+
null,
133+
$this->filesystemDriver
124134
);
125135
}
126136

@@ -196,7 +206,7 @@ public function testGetRequestParams()
196206
)->method(
197207
'getParam'
198208
)->with(
199-
\Magento\Theme\Helper\Storage::PARAM_THEME_ID
209+
Storage::PARAM_THEME_ID
200210
)->will(
201211
$this->returnValue(6)
202212
);
@@ -205,7 +215,7 @@ public function testGetRequestParams()
205215
)->method(
206216
'getParam'
207217
)->with(
208-
\Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE
218+
Storage::PARAM_CONTENT_TYPE
209219
)->will(
210220
$this->returnValue('image')
211221
);
@@ -214,15 +224,15 @@ public function testGetRequestParams()
214224
)->method(
215225
'getParam'
216226
)->with(
217-
\Magento\Theme\Helper\Storage::PARAM_NODE
227+
Storage::PARAM_NODE
218228
)->will(
219229
$this->returnValue('node')
220230
);
221231

222232
$expectedResult = [
223-
\Magento\Theme\Helper\Storage::PARAM_THEME_ID => 6,
224-
\Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE => \Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE,
225-
\Magento\Theme\Helper\Storage::PARAM_NODE => 'node',
233+
Storage::PARAM_THEME_ID => 6,
234+
Storage::PARAM_CONTENT_TYPE => \Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE,
235+
Storage::PARAM_NODE => 'node',
226236
];
227237
$this->assertEquals($expectedResult, $this->helper->getRequestParams());
228238
}
@@ -234,7 +244,7 @@ public function testGetAllowedExtensionsByType()
234244
)->method(
235245
'getParam'
236246
)->with(
237-
\Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE
247+
Storage::PARAM_CONTENT_TYPE
238248
)->will(
239249
$this->returnValue(\Magento\Theme\Model\Wysiwyg\Storage::TYPE_FONT)
240250
);
@@ -244,7 +254,7 @@ public function testGetAllowedExtensionsByType()
244254
)->method(
245255
'getParam'
246256
)->with(
247-
\Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE
257+
Storage::PARAM_CONTENT_TYPE
248258
)->will(
249259
$this->returnValue(\Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE)
250260
);
@@ -273,17 +283,17 @@ public function testGetThumbnailPathNotFound()
273283
->willReturnMap(
274284
[
275285
[
276-
\Magento\Theme\Helper\Storage::PARAM_THEME_ID,
286+
Storage::PARAM_THEME_ID,
277287
null,
278288
6,
279289
],
280290
[
281-
\Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE,
291+
Storage::PARAM_CONTENT_TYPE,
282292
null,
283293
\Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE
284294
],
285295
[
286-
\Magento\Theme\Helper\Storage::PARAM_NODE,
296+
Storage::PARAM_NODE,
287297
null,
288298
$node
289299
],
@@ -349,17 +359,17 @@ public function testGetRelativeUrl()
349359
->willReturnMap(
350360
[
351361
'type' => [
352-
\Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE,
362+
Storage::PARAM_CONTENT_TYPE,
353363
null,
354364
\Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE,
355365
],
356366
'node' => [
357-
\Magento\Theme\Helper\Storage::PARAM_NODE,
367+
Storage::PARAM_NODE,
358368
null,
359369
$notRoot,
360370
],
361371
'filenaem' => [
362-
\Magento\Theme\Helper\Storage::PARAM_FILENAME,
372+
Storage::PARAM_FILENAME,
363373
null,
364374
$filename,
365375
],
@@ -389,8 +399,8 @@ public function testGetRelativeUrl()
389399
public function getStorageTypeForNameDataProvider()
390400
{
391401
return [
392-
'font' => [\Magento\Theme\Model\Wysiwyg\Storage::TYPE_FONT, \Magento\Theme\Helper\Storage::FONTS],
393-
'image' => [\Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE, \Magento\Theme\Helper\Storage::IMAGES],
402+
'font' => [\Magento\Theme\Model\Wysiwyg\Storage::TYPE_FONT, Storage::FONTS],
403+
'image' => [\Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE, Storage::IMAGES],
394404
];
395405
}
396406

@@ -405,7 +415,7 @@ public function testGetStorageTypeName($type, $name)
405415
{
406416
$this->request->expects($this->once())
407417
->method('getParam')
408-
->with(\Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE)
418+
->with(Storage::PARAM_CONTENT_TYPE)
409419
->willReturn($type);
410420

411421
$this->assertEquals($name, $this->helper->getStorageTypeName());
@@ -433,12 +443,62 @@ public function testGetThemeNotFound()
433443
$this->themeFactory->expects($this->once())
434444
->method('create')
435445
->willReturn(null);
436-
$helper = new \Magento\Theme\Helper\Storage(
446+
$helper = new Storage(
437447
$this->contextHelper,
438448
$this->filesystem,
439449
$this->session,
440450
$this->themeFactory
441451
);
442452
$helper->getStorageRoot();
443453
}
454+
455+
/**
456+
* @dataProvider getCurrentPathDataProvider
457+
*/
458+
public function testGetCurrentPath(
459+
string $expectedPath,
460+
string $requestedPath,
461+
?bool $isDirectory = null,
462+
?string $relativePath = null,
463+
?string $resolvedPath = null
464+
) {
465+
$this->directoryWrite->method('isDirectory')
466+
->willReturn($isDirectory);
467+
468+
$this->directoryWrite->method('getRelativePath')
469+
->willReturn($relativePath);
470+
471+
$this->urlDecoder->method('decode')
472+
->willReturnArgument(0);
473+
474+
if ($resolvedPath) {
475+
$this->filesystemDriver->method('getRealpathSafety')
476+
->willReturn($resolvedPath);
477+
} else {
478+
$this->filesystemDriver->method('getRealpathSafety')
479+
->willReturnArgument(0);
480+
}
481+
482+
$this->request->expects($this->once())
483+
->method('getParam')
484+
->with(Storage::PARAM_NODE)
485+
->willReturn($requestedPath);
486+
487+
$actualPath = $this->helper->getCurrentPath();
488+
489+
self::assertSame($expectedPath, $actualPath);
490+
}
491+
492+
public function getCurrentPathDataProvider(): array
493+
{
494+
$rootPath = '/' . \Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE;
495+
496+
return [
497+
[$rootPath, Storage::NODE_ROOT],
498+
[$rootPath, $rootPath . '/foo'],
499+
[$rootPath, $rootPath . '/something', true, null, '/bar'],
500+
['foo/', $rootPath . '/foo', true, 'foo/'],
501+
[$rootPath, $rootPath . '/foo', false],
502+
];
503+
}
444504
}

app/code/Magento/Theme/Test/Unit/Model/Wysiwyg/StorageTest.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
*/
1010
namespace Magento\Theme\Test\Unit\Model\Wysiwyg;
1111

12+
use Magento\Framework\Filesystem\DriverInterface;
13+
use PHPUnit\Framework\MockObject\MockObject;
14+
1215
/**
1316
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1417
*/
@@ -59,6 +62,11 @@ class StorageTest extends \PHPUnit\Framework\TestCase
5962
*/
6063
protected $urlDecoder;
6164

65+
/**
66+
* @var DriverInterface|MockObject
67+
*/
68+
private $filesystemDriver;
69+
6270
protected function setUp()
6371
{
6472
$this->_filesystem = $this->createMock(\Magento\Framework\Filesystem::class);
@@ -99,6 +107,7 @@ function ($path) {
99107
$this->directoryWrite = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
100108
$this->urlEncoder = $this->createPartialMock(\Magento\Framework\Url\EncoderInterface::class, ['encode']);
101109
$this->urlDecoder = $this->createPartialMock(\Magento\Framework\Url\DecoderInterface::class, ['decode']);
110+
$this->filesystemDriver = $this->createMock(DriverInterface::class);
102111

103112
$this->_filesystem->expects(
104113
$this->once()
@@ -114,7 +123,9 @@ function ($path) {
114123
$this->_objectManager,
115124
$this->_imageFactory,
116125
$this->urlEncoder,
117-
$this->urlDecoder
126+
$this->urlDecoder,
127+
null,
128+
$this->filesystemDriver
118129
);
119130

120131
$this->_storageRoot = '/root';
@@ -575,13 +586,13 @@ public function testDeleteRootDirectoryRelative()
575586
->with($fakePath)
576587
->willReturn($directoryPath);
577588

578-
$this->_helperStorage->expects(
579-
$this->atLeastOnce()
580-
)->method(
581-
'getStorageRoot'
582-
)->will(
583-
$this->returnValue($directoryPath)
584-
);
589+
$this->filesystemDriver->method('getRealPathSafety')
590+
->with($directoryPath)
591+
->willReturn($directoryPath);
592+
593+
$this->_helperStorage
594+
->method('getStorageRoot')
595+
->willReturn($directoryPath);
585596

586597
$this->_storageModel->deleteDirectory($fakePath);
587598
}

0 commit comments

Comments
 (0)