Skip to content

Commit 4dc80f0

Browse files
author
Hwashiang Yu
committed
MC-31362: Media folder update
- Added strict typing - Added docblocks and type hints to tests - Updated unit tests
1 parent 9cb72ff commit 4dc80f0

File tree

5 files changed

+105
-40
lines changed

5 files changed

+105
-40
lines changed

app/code/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/DeleteFolder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
8+
declare(strict_types=1);
9+
710
namespace Magento\Cms\Controller\Adminhtml\Wysiwyg\Images;
811

912
use Magento\Framework\App\Action\HttpPostActionInterface;

app/code/Magento/Theme/Model/Design/Backend/File.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
declare(strict_types=1);
8+
79
namespace Magento\Theme\Model\Design\Backend;
810

911
use Magento\Config\Model\Config\Backend\File as BackendFile;

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

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,51 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Theme\Test\Unit\Model\Design\Backend;
710

811
use Magento\Framework\App\Filesystem\DirectoryList;
912
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\File\Mime;
14+
use Magento\Framework\Filesystem\Directory\WriteInterface;
1015
use Magento\Framework\Filesystem\Io\File as IoFileSystem;
1116
use Magento\Framework\UrlInterface;
17+
use Magento\MediaStorage\Helper\File\Storage\Database;
1218
use Magento\Theme\Model\Design\Backend\File;
19+
use PHPUnit_Framework_MockObject_MockObject;
1320

1421
/**
1522
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1623
*/
1724
class FileTest extends \PHPUnit\Framework\TestCase
1825
{
19-
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface|\PHPUnit_Framework_MockObject_MockObject */
20-
protected $mediaDirectory;
26+
/** @var WriteInterface|PHPUnit_Framework_MockObject_MockObject */
27+
private $mediaDirectory;
2128

22-
/** @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject */
23-
protected $urlBuilder;
29+
/** @var UrlInterface|PHPUnit_Framework_MockObject_MockObject */
30+
private $urlBuilder;
2431

2532
/** @var File */
26-
protected $fileBackend;
33+
private $fileBackend;
2734

28-
/** @var IoFileSystem */
35+
/** @var IoFileSystem|PHPUnit_Framework_MockObject_MockObject */
2936
private $ioFileSystem;
3037

3138
/**
32-
* @var \Magento\Framework\File\Mime|\PHPUnit_Framework_MockObject_MockObject
39+
* @var Mime|PHPUnit_Framework_MockObject_MockObject
3340
*/
3441
private $mime;
3542

3643
/**
37-
* @var \Magento\MediaStorage\Helper\File\Storage\Database|\PHPUnit_Framework_MockObject_MockObject
44+
* @var Database|PHPUnit_Framework_MockObject_MockObject
3845
*/
3946
private $databaseHelper;
4047

48+
/**
49+
* @inheritdoc
50+
*/
4151
public function setUp()
4252
{
4353
$context = $this->getMockObject(\Magento\Framework\Model\Context::class);
@@ -59,7 +69,7 @@ public function setUp()
5969
->disableOriginalConstructor()
6070
->getMock();
6171
$this->mediaDirectory = $this->getMockBuilder(
62-
\Magento\Framework\Filesystem\Directory\WriteInterface::class
72+
WriteInterface::class
6373
)
6474
->getMockForAbstractClass();
6575
$filesystem->expects($this->once())
@@ -68,11 +78,13 @@ public function setUp()
6878
->willReturn($this->mediaDirectory);
6979
$this->urlBuilder = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
7080
->getMockForAbstractClass();
71-
$this->mime = $this->getMockBuilder(\Magento\Framework\File\Mime::class)
81+
$this->ioFileSystem = $this->getMockBuilder(\Magento\Framework\Filesystem\Io\File::class)
82+
->getMockForAbstractClass();
83+
$this->mime = $this->getMockBuilder(Mime::class)
7284
->disableOriginalConstructor()
7385
->getMock();
7486
$this->databaseHelper = $this->getMockBuilder(
75-
\Magento\MediaStorage\Helper\File\Storage\Database::class
87+
Database::class
7688
)
7789
->disableOriginalConstructor()
7890
->getMock();
@@ -83,7 +95,6 @@ public function setUp()
8395
$abstractDb = $this->getMockBuilder(\Magento\Framework\Data\Collection\AbstractDb::class)
8496
->disableOriginalConstructor()
8597
->getMockForAbstractClass();
86-
$this->ioFileSystem = new IoFileSystem();
8798
$this->fileBackend = new File(
8899
$context,
89100
$registry,
@@ -107,17 +118,22 @@ public function setUp()
107118
);
108119
}
109120

121+
/**
122+
* @inheritdoc
123+
*/
110124
public function tearDown()
111125
{
112126
unset($this->fileBackend);
113127
}
114128

115129
/**
130+
* Gets the mock object.
131+
*
116132
* @param string $class
117133
* @param array $methods
118-
* @return \PHPUnit_Framework_MockObject_MockObject
134+
* @return PHPUnit_Framework_MockObject_MockObject
119135
*/
120-
protected function getMockObject($class, $methods = [])
136+
private function getMockObject(string $class, array $methods = []): PHPUnit_Framework_MockObject_MockObject
121137
{
122138
$builder = $this->getMockBuilder($class)
123139
->disableOriginalConstructor();
@@ -128,15 +144,20 @@ protected function getMockObject($class, $methods = [])
128144
}
129145

130146
/**
147+
* Gets mock objects for abstract class.
148+
*
131149
* @param string $class
132-
* @return \PHPUnit_Framework_MockObject_MockObject
150+
* @return PHPUnit_Framework_MockObject_MockObject
133151
*/
134-
protected function getMockObjectForAbstractClass($class)
152+
private function getMockObjectForAbstractClass(string $class): PHPUnit_Framework_MockObject_MockObject
135153
{
136154
return $this->getMockBuilder($class)
137155
->getMockForAbstractClass();
138156
}
139157

158+
/**
159+
* Test for afterLoad method.
160+
*/
140161
public function testAfterLoad()
141162
{
142163
$value = 'filename.jpg';
@@ -201,11 +222,13 @@ public function testAfterLoad()
201222
}
202223

203224
/**
225+
* Test for beforeSave method.
226+
*
204227
* @dataProvider beforeSaveDataProvider
205228
* @param string $fileName
206229
* @throws LocalizedException
207230
*/
208-
public function testBeforeSave($fileName)
231+
public function testBeforeSave(string $fileName)
209232
{
210233
$expectedFileName = basename($fileName);
211234
$expectedTmpMediaPath = 'tmp/design/file/' . $expectedFileName;
@@ -247,17 +270,21 @@ public function testBeforeSave($fileName)
247270
}
248271

249272
/**
273+
* Data provider for testBeforeSave.
274+
*
250275
* @return array
251276
*/
252-
public function beforeSaveDataProvider()
277+
public function beforeSaveDataProvider(): array
253278
{
254279
return [
255280
'Normal file name' => ['filename.jpg'],
256-
'Vulnerable file name' => ['../../../../../../../../etc/passwd'],
281+
'Vulnerable file name' => ['../../../../../../../../etc/pass'],
257282
];
258283
}
259284

260285
/**
286+
* Test for beforeSave method without file.
287+
*
261288
* @expectedException \Magento\Framework\Exception\LocalizedException
262289
* @expectedExceptionMessage header_logo_src does not contain field 'file'
263290
*/
@@ -276,6 +303,11 @@ public function testBeforeSaveWithoutFile()
276303
$this->fileBackend->beforeSave();
277304
}
278305

306+
/**
307+
* Test for beforeSave method with existing file.
308+
*
309+
* @throws LocalizedException
310+
*/
279311
public function testBeforeSaveWithExistingFile()
280312
{
281313
$value = 'filename.jpg';
@@ -327,7 +359,7 @@ public function getRelativeMediaPathDataProvider(): array
327359
{
328360
return [
329361
'Normal path' => ['pub/media/', 'filename.jpg'],
330-
'Complex path' => ['somepath/pub/media/', 'filename.jpg'],
362+
'Complex path' => ['some_path/pub/media/', 'filename.jpg'],
331363
];
332364
}
333365
}

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,25 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Theme\Test\Unit\Model\Design\Backend;
710

8-
use Magento\Framework\Filesystem\Io\File as IoFileSystem;
911
use Magento\Theme\Model\Design\Backend\Image;
12+
use PHPUnit_Framework_MockObject_MockObject;
1013

1114
/**
1215
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1316
*/
1417
class ImageTest extends \PHPUnit\Framework\TestCase
1518
{
1619
/** @var Image */
17-
protected $imageBackend;
20+
private $imageBackend;
1821

22+
/**
23+
* @inheritdoc
24+
*/
1925
public function setUp()
2026
{
2127
$context = $this->getMockObject(\Magento\Framework\Model\Context::class);
@@ -31,7 +37,7 @@ public function setUp()
3137
$databaseHelper = $this->getMockObject(\Magento\MediaStorage\Helper\File\Storage\Database::class);
3238
$abstractResource = $this->getMockObject(\Magento\Framework\Model\ResourceModel\AbstractResource::class);
3339
$abstractDb = $this->getMockObject(\Magento\Framework\Data\Collection\AbstractDb::class);
34-
$ioFileSystem = new IoFileSystem();
40+
$ioFileSystem = $this->getMockObject(\Magento\Framework\Filesystem\Io\File::class);
3541
$this->imageBackend = new Image(
3642
$context,
3743
$registry,
@@ -49,6 +55,9 @@ public function setUp()
4955
);
5056
}
5157

58+
/**
59+
* @inheritdoc
60+
*/
5261
public function tearDown()
5362
{
5463
unset($this->imageBackend);
@@ -57,9 +66,9 @@ public function tearDown()
5766
/**
5867
* @param string $class
5968
* @param array $methods
60-
* @return \PHPUnit_Framework_MockObject_MockObject
69+
* @return PHPUnit_Framework_MockObject_MockObject
6170
*/
62-
protected function getMockObject($class, $methods = [])
71+
private function getMockObject(string $class, array $methods = []): PHPUnit_Framework_MockObject_MockObject
6372
{
6473
$builder = $this->getMockBuilder($class)
6574
->disableOriginalConstructor();
@@ -70,6 +79,8 @@ protected function getMockObject($class, $methods = [])
7079
}
7180

7281
/**
82+
* Test for beforeSave method with invalid file extension.
83+
*
7384
* @expectedException \Magento\Framework\Exception\LocalizedException
7485
* @expectedExceptionMessage Something is wrong with the file upload settings.
7586
*/

lib/internal/Magento/Framework/Filesystem/Test/Unit/Driver/FileTest.php

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
declare(strict_types=1);
8+
79
namespace Magento\Framework\Filesystem\Test\Unit\Driver;
810

911
use Magento\Framework\Filesystem\Driver\File;
@@ -16,28 +18,35 @@ class FileTest extends \PHPUnit\Framework\TestCase
1618
/** @var bool Result of file_put_contents() function */
1719
public static $filePutContents;
1820

21+
/**
22+
* @inheritdoc
23+
*/
1924
public function setUp()
2025
{
2126
self::$fileGetContents = '';
2227
self::$filePutContents = true;
2328
}
2429

2530
/**
31+
* Test for getAbsolutePath method.
32+
*
2633
* @dataProvider dataProviderForTestGetAbsolutePath
27-
* @param $basePath
28-
* @param $path
29-
* @param $expected
34+
* @param string $basePath
35+
* @param string $path
36+
* @param string $expected
3037
*/
31-
public function testGetAbsolutePath($basePath, $path, $expected)
38+
public function testGetAbsolutePath(string $basePath, string $path, string $expected)
3239
{
3340
$file = new File();
3441
$this->assertEquals($expected, $file->getAbsolutePath($basePath, $path));
3542
}
3643

3744
/**
45+
* Data provider for testGetAbsolutePath.
46+
*
3847
* @return array
3948
*/
40-
public function dataProviderForTestGetAbsolutePath()
49+
public function dataProviderForTestGetAbsolutePath(): array
4150
{
4251
return [
4352
['/root/path/', 'sub', '/root/path/sub'],
@@ -48,21 +57,25 @@ public function dataProviderForTestGetAbsolutePath()
4857
}
4958

5059
/**
60+
* Test for getRelativePath method.
61+
*
5162
* @dataProvider dataProviderForTestGetRelativePath
52-
* @param $basePath
53-
* @param $path
54-
* @param $expected
63+
* @param string $basePath
64+
* @param string $path
65+
* @param string $expected
5566
*/
56-
public function testGetRelativePath($basePath, $path, $expected)
67+
public function testGetRelativePath(string $basePath, string $path, string $expected)
5768
{
5869
$file = new File();
5970
$this->assertEquals($expected, $file->getRelativePath($basePath, $path));
6071
}
6172

6273
/**
74+
* Data provider for testGetRelativePath.
75+
*
6376
* @return array
6477
*/
65-
public function dataProviderForTestGetRelativePath()
78+
public function dataProviderForTestGetRelativePath(): array
6679
{
6780
return [
6881
['/root/path/', 'sub', 'sub'],
@@ -73,20 +86,24 @@ public function dataProviderForTestGetRelativePath()
7386
}
7487

7588
/**
76-
* @dataProvider dataProviderForTestRealPathSafety
77-
* @param $path
78-
* @param $expected
89+
* Test for getRealPathSafety method.
90+
*
91+
* @dataProvider dataProviderForTestGetRealPathSafety
92+
* @param string $path
93+
* @param string $expected
7994
*/
80-
public function testGetRealPathSafety($path, $expected)
95+
public function testGetRealPathSafety(string $path, string $expected)
8196
{
8297
$file = new File();
8398
$this->assertEquals($expected, $file->getRealPathSafety($path));
8499
}
85100

86101
/**
102+
* Data provider for testGetRealPathSafety;
103+
*
87104
* @return array
88105
*/
89-
public function dataProviderForTestRealPathSafety()
106+
public function dataProviderForTestGetRealPathSafety(): array
90107
{
91108
return [
92109
['/1/2/3', '/1/2/3'],

0 commit comments

Comments
 (0)