Skip to content

Commit aa01632

Browse files
author
Hwashiang Yu
committed
MC-31362: Media folder update
- Updated storage path handling logic - Updated file driver path check logic - Updated unit test for path check logic - Updated test for image class
1 parent d0065c1 commit aa01632

File tree

5 files changed

+221
-62
lines changed

5 files changed

+221
-62
lines changed

app/code/Magento/Cms/Model/Wysiwyg/Images/Storage.php

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ public function __construct(
225225
*
226226
* @param string $path
227227
* @return void
228+
* @throws \Magento\Framework\Exception\FileSystemException
229+
* @throws \Magento\Framework\Exception\ValidatorException
228230
*/
229231
protected function createSubDirectories($path)
230232
{
@@ -295,6 +297,7 @@ protected function removeItemFromCollection($collection, $conditions)
295297
*
296298
* @param string $path Parent directory path
297299
* @return \Magento\Framework\Data\Collection\Filesystem
300+
* @throws \Exception
298301
*/
299302
public function getDirsCollection($path)
300303
{
@@ -393,6 +396,7 @@ public function getFilesCollection($path, $type = null)
393396
*
394397
* @param string $path Path to the directory
395398
* @return \Magento\Cms\Model\Wysiwyg\Images\Storage\Collection
399+
* @throws \Exception
396400
*/
397401
public function getCollection($path = null)
398402
{
@@ -485,6 +489,9 @@ public function deleteDirectory($path)
485489
*
486490
* @param string $path
487491
* @return void
492+
* @throws \Magento\Framework\Exception\FileSystemException
493+
* @throws \Magento\Framework\Exception\LocalizedException
494+
* @throws \Magento\Framework\Exception\ValidatorException
488495
*/
489496
protected function _deleteByPath($path)
490497
{
@@ -500,6 +507,8 @@ protected function _deleteByPath($path)
500507
*
501508
* @param string $target File path to be deleted
502509
* @return $this
510+
* @throws \Magento\Framework\Exception\FileSystemException
511+
* @throws \Magento\Framework\Exception\ValidatorException
503512
*/
504513
public function deleteFile($target)
505514
{
@@ -561,9 +570,11 @@ public function uploadFile($targetPath, $type = null)
561570
/**
562571
* Thumbnail path getter
563572
*
564-
* @param string $filePath original file path
565-
* @param bool $checkFile OPTIONAL is it necessary to check file availability
573+
* @param string $filePath original file path
574+
* @param bool $checkFile OPTIONAL is it necessary to check file availability
566575
* @return string|false
576+
* @throws \Magento\Framework\Exception\FileSystemException
577+
* @throws \Magento\Framework\Exception\ValidatorException
567578
*/
568579
public function getThumbnailPath($filePath, $checkFile = false)
569580
{
@@ -587,9 +598,11 @@ public function getThumbnailPath($filePath, $checkFile = false)
587598
/**
588599
* Thumbnail URL getter
589600
*
590-
* @param string $filePath original file path
591-
* @param bool $checkFile OPTIONAL is it necessary to check file availability
601+
* @param string $filePath original file path
602+
* @param bool $checkFile OPTIONAL is it necessary to check file availability
592603
* @return string|false
604+
* @throws \Magento\Framework\Exception\FileSystemException
605+
* @throws \Magento\Framework\Exception\ValidatorException
593606
*/
594607
public function getThumbnailUrl($filePath, $checkFile = false)
595608
{
@@ -610,6 +623,8 @@ public function getThumbnailUrl($filePath, $checkFile = false)
610623
* @param string $source Image path to be resized
611624
* @param bool $keepRatio Keep aspect ratio or not
612625
* @return bool|string Resized filepath or false if errors were occurred
626+
* @throws \Magento\Framework\Exception\FileSystemException
627+
* @throws \Magento\Framework\Exception\ValidatorException
613628
*/
614629
public function resizeFile($source, $keepRatio = true)
615630
{
@@ -643,6 +658,9 @@ public function resizeFile($source, $keepRatio = true)
643658
*
644659
* @param string $filename File basename
645660
* @return bool|string Thumbnail path or false for errors
661+
* @throws \Magento\Framework\Exception\FileSystemException
662+
* @throws \Magento\Framework\Exception\LocalizedException
663+
* @throws \Magento\Framework\Exception\ValidatorException
646664
*/
647665
public function resizeOnTheFly($filename)
648666
{
@@ -658,6 +676,8 @@ public function resizeOnTheFly($filename)
658676
*
659677
* @param bool|string $filePath Path to the file
660678
* @return string
679+
* @throws \Magento\Framework\Exception\FileSystemException
680+
* @throws \Magento\Framework\Exception\ValidatorException
661681
*/
662682
public function getThumbsPath($filePath = false)
663683
{
@@ -765,13 +785,12 @@ public function getCmsWysiwygImages()
765785
protected function _validatePath($path)
766786
{
767787
$root = $this->_sanitizePath($this->_cmsWysiwygImages->getStorageRoot());
768-
$realPath = $this->_sanitizePath($path);
769-
if ($root == $realPath) {
788+
if ($root == $path) {
770789
throw new \Magento\Framework\Exception\LocalizedException(
771790
__('We can\'t delete root directory %1 right now.', $path)
772791
);
773792
}
774-
if (strpos($realPath, (string) $root) !== 0) {
793+
if (strpos($path, (string) $root) !== 0) {
775794
throw new \Magento\Framework\Exception\LocalizedException(
776795
__('Directory %1 is not under storage root path.', $path)
777796
);
@@ -783,17 +802,28 @@ protected function _validatePath($path)
783802
*
784803
* @param string $path
785804
* @return string
805+
* @throws \Magento\Framework\Exception\ValidatorException
786806
*/
787807
protected function _sanitizePath($path)
788808
{
789-
return rtrim(preg_replace('~[/\\\]+~', '/', $this->_directory->getDriver()->getRealPathSafety($path)), '/');
809+
return rtrim(
810+
preg_replace(
811+
'~[/\\\]+~',
812+
'/',
813+
$this->_directory->getDriver()->getRealPathSafety(
814+
$this->_directory->getAbsolutePath($path)
815+
)
816+
),
817+
'/'
818+
);
790819
}
791820

792821
/**
793822
* Get path in root storage dir
794823
*
795824
* @param string $path
796825
* @return string|bool
826+
* @throws \Magento\Framework\Exception\ValidatorException
797827
*/
798828
protected function _getRelativePathToRoot($path)
799829
{

app/code/Magento/Theme/Test/Unit/Model/Design/Backend/FileTest.php

Lines changed: 62 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
*/
66
namespace Magento\Theme\Test\Unit\Model\Design\Backend;
77

8-
use Magento\Framework\UrlInterface;
9-
use Magento\Theme\Model\Design\Backend\File;
108
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\Exception\LocalizedException;
1110
use Magento\Framework\Filesystem\Io\File as IoFileSystem;
11+
use Magento\Framework\UrlInterface;
12+
use Magento\Theme\Model\Design\Backend\File;
1213

1314
/**
1415
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -24,7 +25,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
2425
/** @var File */
2526
protected $fileBackend;
2627

27-
/** @var IoFileSystem|\PHPUnit_Framework_MockObject_MockObject */
28+
/** @var IoFileSystem */
2829
private $ioFileSystem;
2930

3031
/**
@@ -41,43 +42,48 @@ public function setUp()
4142
{
4243
$context = $this->getMockObject(\Magento\Framework\Model\Context::class);
4344
$registry = $this->getMockObject(\Magento\Framework\Registry::class);
44-
$config = $this->getMockObjectForAbstractClass(\Magento\Framework\App\Config\ScopeConfigInterface::class);
45-
$cacheTypeList = $this->getMockObjectForAbstractClass(\Magento\Framework\App\Cache\TypeListInterface::class);
46-
$uploaderFactory = $this->getMockObject(\Magento\MediaStorage\Model\File\UploaderFactory::class, ['create']);
45+
$config = $this->getMockObjectForAbstractClass(
46+
\Magento\Framework\App\Config\ScopeConfigInterface::class
47+
);
48+
$cacheTypeList = $this->getMockObjectForAbstractClass(
49+
\Magento\Framework\App\Cache\TypeListInterface::class
50+
);
51+
$uploaderFactory = $this->getMockObject(
52+
\Magento\MediaStorage\Model\File\UploaderFactory::class,
53+
['create']
54+
);
4755
$requestData = $this->getMockObjectForAbstractClass(
4856
\Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface::class
4957
);
5058
$filesystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
5159
->disableOriginalConstructor()
5260
->getMock();
53-
$this->mediaDirectory = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\WriteInterface::class)
61+
$this->mediaDirectory = $this->getMockBuilder(
62+
\Magento\Framework\Filesystem\Directory\WriteInterface::class
63+
)
5464
->getMockForAbstractClass();
55-
5665
$filesystem->expects($this->once())
5766
->method('getDirectoryWrite')
5867
->with(DirectoryList::MEDIA)
5968
->willReturn($this->mediaDirectory);
6069
$this->urlBuilder = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
6170
->getMockForAbstractClass();
62-
63-
$this->ioFileSystem = $this->getMockBuilder(\Magento\Framework\Filesystem\Io\File::class)
64-
->getMockForAbstractClass();
65-
6671
$this->mime = $this->getMockBuilder(\Magento\Framework\File\Mime::class)
6772
->disableOriginalConstructor()
6873
->getMock();
69-
70-
$this->databaseHelper = $this->getMockBuilder(\Magento\MediaStorage\Helper\File\Storage\Database::class)
74+
$this->databaseHelper = $this->getMockBuilder(
75+
\Magento\MediaStorage\Helper\File\Storage\Database::class
76+
)
7177
->disableOriginalConstructor()
7278
->getMock();
73-
74-
$abstractResource = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\AbstractResource::class)
79+
$abstractResource = $this->getMockBuilder(
80+
\Magento\Framework\Model\ResourceModel\AbstractResource::class
81+
)
7582
->getMockForAbstractClass();
76-
7783
$abstractDb = $this->getMockBuilder(\Magento\Framework\Data\Collection\AbstractDb::class)
7884
->disableOriginalConstructor()
7985
->getMockForAbstractClass();
80-
86+
$this->ioFileSystem = new IoFileSystem();
8187
$this->fileBackend = new File(
8288
$context,
8389
$registry,
@@ -93,7 +99,6 @@ public function setUp()
9399
$this->databaseHelper,
94100
$this->ioFileSystem
95101
);
96-
97102
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
98103
$objectManager->setBackwardCompatibleProperty(
99104
$this->fileBackend,
@@ -139,16 +144,18 @@ public function testAfterLoad()
139144

140145
$absoluteFilePath = '/absolute_path/' . $value;
141146

142-
$this->fileBackend->setValue($value);
143-
$this->fileBackend->setFieldConfig(
147+
$this->fileBackend->setData(
144148
[
145-
'upload_dir' => [
146-
'value' => 'value',
147-
'config' => 'system/filesystem/media',
148-
],
149-
'base_url' => [
150-
'type' => 'media',
151-
'value' => 'design/file'
149+
'value' => $value,
150+
'field_config' => [
151+
'upload_dir' => [
152+
'value' => 'value',
153+
'config' => 'system/filesystem/media',
154+
],
155+
'base_url' => [
156+
'type' => 'media',
157+
'value' => 'design/file'
158+
],
152159
],
153160
]
154161
);
@@ -161,7 +168,6 @@ public function testAfterLoad()
161168
->method('getAbsolutePath')
162169
->with('value/' . $value)
163170
->willReturn($absoluteFilePath);
164-
165171
$this->urlBuilder->expects($this->once())
166172
->method('getBaseUrl')
167173
->with(['_type' => UrlInterface::URL_TYPE_MEDIA])
@@ -174,12 +180,10 @@ public function testAfterLoad()
174180
->method('stat')
175181
->with('value/' . $value)
176182
->willReturn(['size' => 234234]);
177-
178183
$this->mime->expects($this->once())
179184
->method('getMimeType')
180185
->with($absoluteFilePath)
181186
->willReturn($mime);
182-
183187
$this->fileBackend->afterLoad();
184188
$this->assertEquals(
185189
[
@@ -199,27 +203,28 @@ public function testAfterLoad()
199203
/**
200204
* @dataProvider beforeSaveDataProvider
201205
* @param string $fileName
206+
* @throws LocalizedException
202207
*/
203208
public function testBeforeSave($fileName)
204209
{
205210
$expectedFileName = basename($fileName);
206211
$expectedTmpMediaPath = 'tmp/design/file/' . $expectedFileName;
207-
$this->fileBackend->setScope('store');
208-
$this->fileBackend->setScopeId(1);
209-
$this->fileBackend->setValue(
210-
[
211-
[
212-
'url' => 'http://magento2.com/pub/media/tmp/image/' . $fileName,
213-
'file' => $fileName,
214-
'size' => 234234,
215-
]
216-
]
217-
);
218-
$this->fileBackend->setFieldConfig(
212+
$this->fileBackend->setData(
219213
[
220-
'upload_dir' => [
221-
'value' => 'value',
222-
'config' => 'system/filesystem/media',
214+
'scope' => 'store',
215+
'scope_id' => 1,
216+
'value' => [
217+
[
218+
'url' => 'http://magento2.com/pub/media/tmp/image/' . $fileName,
219+
'file' => $fileName,
220+
'size' => 234234,
221+
]
222+
],
223+
'field_config' => [
224+
'upload_dir' => [
225+
'value' => 'value',
226+
'config' => 'system/filesystem/media',
227+
],
223228
],
224229
]
225230
);
@@ -274,16 +279,19 @@ public function testBeforeSaveWithoutFile()
274279
public function testBeforeSaveWithExistingFile()
275280
{
276281
$value = 'filename.jpg';
277-
$this->fileBackend->setValue(
282+
$this->fileBackend->setData(
278283
[
279-
[
280-
'url' => 'http://magento2.com/pub/media/tmp/image/' . $value,
281-
'file' => $value,
282-
'size' => 234234,
283-
'exists' => true
284-
]
284+
'value' => [
285+
[
286+
'url' => 'http://magento2.com/pub/media/tmp/image/' . $value,
287+
'file' => $value,
288+
'size' => 234234,
289+
'exists' => true
290+
]
291+
],
285292
]
286293
);
294+
287295
$this->fileBackend->beforeSave();
288296
$this->assertEquals(
289297
$value,
@@ -297,6 +305,7 @@ public function testBeforeSaveWithExistingFile()
297305
* @param string $path
298306
* @param string $filename
299307
* @dataProvider getRelativeMediaPathDataProvider
308+
* @throws \ReflectionException
300309
*/
301310
public function testGetRelativeMediaPath(string $path, string $filename)
302311
{

0 commit comments

Comments
 (0)