3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
7
+ declare (strict_types=1 );
8
+
6
9
namespace Magento \Theme \Test \Unit \Model \Design \Backend ;
7
10
8
11
use Magento \Framework \App \Filesystem \DirectoryList ;
9
12
use Magento \Framework \Exception \LocalizedException ;
13
+ use Magento \Framework \File \Mime ;
14
+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
10
15
use Magento \Framework \Filesystem \Io \File as IoFileSystem ;
11
16
use Magento \Framework \UrlInterface ;
17
+ use Magento \MediaStorage \Helper \File \Storage \Database ;
12
18
use Magento \Theme \Model \Design \Backend \File ;
19
+ use PHPUnit_Framework_MockObject_MockObject ;
13
20
14
21
/**
15
22
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
16
23
*/
17
24
class FileTest extends \PHPUnit \Framework \TestCase
18
25
{
19
- /** @var \Magento\Framework\Filesystem\Directory\ WriteInterface|\ PHPUnit_Framework_MockObject_MockObject */
20
- protected $ mediaDirectory ;
26
+ /** @var WriteInterface|PHPUnit_Framework_MockObject_MockObject */
27
+ private $ mediaDirectory ;
21
28
22
- /** @var UrlInterface|\ PHPUnit_Framework_MockObject_MockObject */
23
- protected $ urlBuilder ;
29
+ /** @var UrlInterface|PHPUnit_Framework_MockObject_MockObject */
30
+ private $ urlBuilder ;
24
31
25
32
/** @var File */
26
- protected $ fileBackend ;
33
+ private $ fileBackend ;
27
34
28
- /** @var IoFileSystem */
35
+ /** @var IoFileSystem|PHPUnit_Framework_MockObject_MockObject */
29
36
private $ ioFileSystem ;
30
37
31
38
/**
32
- * @var \Magento\Framework\File\ Mime|\ PHPUnit_Framework_MockObject_MockObject
39
+ * @var Mime|PHPUnit_Framework_MockObject_MockObject
33
40
*/
34
41
private $ mime ;
35
42
36
43
/**
37
- * @var \Magento\MediaStorage\Helper\File\Storage\ Database|\ PHPUnit_Framework_MockObject_MockObject
44
+ * @var Database|PHPUnit_Framework_MockObject_MockObject
38
45
*/
39
46
private $ databaseHelper ;
40
47
48
+ /**
49
+ * @inheritdoc
50
+ */
41
51
public function setUp ()
42
52
{
43
53
$ context = $ this ->getMockObject (\Magento \Framework \Model \Context::class);
@@ -59,7 +69,7 @@ public function setUp()
59
69
->disableOriginalConstructor ()
60
70
->getMock ();
61
71
$ this ->mediaDirectory = $ this ->getMockBuilder (
62
- \ Magento \ Framework \ Filesystem \ Directory \ WriteInterface::class
72
+ WriteInterface::class
63
73
)
64
74
->getMockForAbstractClass ();
65
75
$ filesystem ->expects ($ this ->once ())
@@ -68,11 +78,13 @@ public function setUp()
68
78
->willReturn ($ this ->mediaDirectory );
69
79
$ this ->urlBuilder = $ this ->getMockBuilder (\Magento \Framework \UrlInterface::class)
70
80
->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)
72
84
->disableOriginalConstructor ()
73
85
->getMock ();
74
86
$ this ->databaseHelper = $ this ->getMockBuilder (
75
- \ Magento \ MediaStorage \ Helper \ File \ Storage \ Database::class
87
+ Database::class
76
88
)
77
89
->disableOriginalConstructor ()
78
90
->getMock ();
@@ -83,7 +95,6 @@ public function setUp()
83
95
$ abstractDb = $ this ->getMockBuilder (\Magento \Framework \Data \Collection \AbstractDb::class)
84
96
->disableOriginalConstructor ()
85
97
->getMockForAbstractClass ();
86
- $ this ->ioFileSystem = new IoFileSystem ();
87
98
$ this ->fileBackend = new File (
88
99
$ context ,
89
100
$ registry ,
@@ -107,17 +118,22 @@ public function setUp()
107
118
);
108
119
}
109
120
121
+ /**
122
+ * @inheritdoc
123
+ */
110
124
public function tearDown ()
111
125
{
112
126
unset($ this ->fileBackend );
113
127
}
114
128
115
129
/**
130
+ * Gets the mock object.
131
+ *
116
132
* @param string $class
117
133
* @param array $methods
118
- * @return \ PHPUnit_Framework_MockObject_MockObject
134
+ * @return PHPUnit_Framework_MockObject_MockObject
119
135
*/
120
- protected function getMockObject ($ class , $ methods = [])
136
+ private function getMockObject (string $ class , array $ methods = []): PHPUnit_Framework_MockObject_MockObject
121
137
{
122
138
$ builder = $ this ->getMockBuilder ($ class )
123
139
->disableOriginalConstructor ();
@@ -128,15 +144,20 @@ protected function getMockObject($class, $methods = [])
128
144
}
129
145
130
146
/**
147
+ * Gets mock objects for abstract class.
148
+ *
131
149
* @param string $class
132
- * @return \ PHPUnit_Framework_MockObject_MockObject
150
+ * @return PHPUnit_Framework_MockObject_MockObject
133
151
*/
134
- protected function getMockObjectForAbstractClass ($ class )
152
+ private function getMockObjectForAbstractClass (string $ class ): PHPUnit_Framework_MockObject_MockObject
135
153
{
136
154
return $ this ->getMockBuilder ($ class )
137
155
->getMockForAbstractClass ();
138
156
}
139
157
158
+ /**
159
+ * Test for afterLoad method.
160
+ */
140
161
public function testAfterLoad ()
141
162
{
142
163
$ value = 'filename.jpg ' ;
@@ -201,11 +222,13 @@ public function testAfterLoad()
201
222
}
202
223
203
224
/**
225
+ * Test for beforeSave method.
226
+ *
204
227
* @dataProvider beforeSaveDataProvider
205
228
* @param string $fileName
206
229
* @throws LocalizedException
207
230
*/
208
- public function testBeforeSave ($ fileName )
231
+ public function testBeforeSave (string $ fileName )
209
232
{
210
233
$ expectedFileName = basename ($ fileName );
211
234
$ expectedTmpMediaPath = 'tmp/design/file/ ' . $ expectedFileName ;
@@ -247,17 +270,21 @@ public function testBeforeSave($fileName)
247
270
}
248
271
249
272
/**
273
+ * Data provider for testBeforeSave.
274
+ *
250
275
* @return array
251
276
*/
252
- public function beforeSaveDataProvider ()
277
+ public function beforeSaveDataProvider (): array
253
278
{
254
279
return [
255
280
'Normal file name ' => ['filename.jpg ' ],
256
- 'Vulnerable file name ' => ['../../../../../../../../etc/passwd ' ],
281
+ 'Vulnerable file name ' => ['../../../../../../../../etc/pass ' ],
257
282
];
258
283
}
259
284
260
285
/**
286
+ * Test for beforeSave method without file.
287
+ *
261
288
* @expectedException \Magento\Framework\Exception\LocalizedException
262
289
* @expectedExceptionMessage header_logo_src does not contain field 'file'
263
290
*/
@@ -276,6 +303,11 @@ public function testBeforeSaveWithoutFile()
276
303
$ this ->fileBackend ->beforeSave ();
277
304
}
278
305
306
+ /**
307
+ * Test for beforeSave method with existing file.
308
+ *
309
+ * @throws LocalizedException
310
+ */
279
311
public function testBeforeSaveWithExistingFile ()
280
312
{
281
313
$ value = 'filename.jpg ' ;
@@ -327,7 +359,7 @@ public function getRelativeMediaPathDataProvider(): array
327
359
{
328
360
return [
329
361
'Normal path ' => ['pub/media/ ' , 'filename.jpg ' ],
330
- 'Complex path ' => ['somepath /pub/media/ ' , 'filename.jpg ' ],
362
+ 'Complex path ' => ['some_path /pub/media/ ' , 'filename.jpg ' ],
331
363
];
332
364
}
333
365
}
0 commit comments