@@ -56,6 +56,9 @@ class UploaderTest extends \PHPUnit\Framework\TestCase
56
56
*/
57
57
protected $ uploader ;
58
58
59
+ /**
60
+ * @inheritdoc
61
+ */
59
62
protected function setUp ()
60
63
{
61
64
$ this ->coreFileStorageDb = $ this ->getMockBuilder (\Magento \MediaStorage \Helper \File \Storage \Database::class)
@@ -79,7 +82,7 @@ protected function setUp()
79
82
->setMethods (['create ' ])
80
83
->getMock ();
81
84
82
- $ this ->directoryMock = $ this ->getMockBuilder (\Magento \Framework \Filesystem \Directory \Writer ::class)
85
+ $ this ->directoryMock = $ this ->getMockBuilder (\Magento \Framework \Filesystem \Directory \Write ::class)
83
86
->setMethods (['writeFile ' , 'getRelativePath ' , 'isWritable ' , 'isReadable ' , 'getAbsolutePath ' ])
84
87
->disableOriginalConstructor ()
85
88
->getMock ();
@@ -109,41 +112,53 @@ protected function setUp()
109
112
null ,
110
113
$ this ->directoryResolver
111
114
])
112
- ->setMethods (['_setUploadFile ' , 'save ' , 'getTmpDir ' ])
115
+ ->setMethods (['_setUploadFile ' , 'save ' , 'getTmpDir ' , ' checkAllowedExtension ' ])
113
116
->getMock ();
114
117
}
115
118
116
119
/**
117
120
* @dataProvider moveFileUrlDataProvider
121
+ * @param string $fileUrl
122
+ * @param string $expectedHost
123
+ * @param string $expectedFileName
124
+ * @param int $checkAllowedExtension
125
+ * @return void
118
126
*/
119
- public function testMoveFileUrl ($ fileUrl , $ expectedHost , $ expectedFileName )
120
- {
127
+ public function testMoveFileUrl (
128
+ string $ fileUrl ,
129
+ string $ expectedHost ,
130
+ string $ expectedFileName ,
131
+ int $ checkAllowedExtension
132
+ ) {
121
133
$ destDir = 'var/dest/dir ' ;
122
134
$ expectedRelativeFilePath = $ expectedFileName ;
123
135
$ this ->directoryMock ->expects ($ this ->once ())->method ('isWritable ' )->with ($ destDir )->willReturn (true );
124
136
$ this ->directoryMock ->expects ($ this ->any ())->method ('getRelativePath ' )->with ($ expectedRelativeFilePath );
125
137
$ this ->directoryMock ->expects ($ this ->once ())->method ('getAbsolutePath ' )->with ($ destDir )
126
138
->willReturn ($ destDir . '/ ' . $ expectedFileName );
127
139
// Check writeFile() method invoking.
128
- $ this ->directoryMock ->expects ($ this ->any ())->method ('writeFile ' )->will ( $ this -> returnValue ( $ expectedFileName) );
140
+ $ this ->directoryMock ->expects ($ this ->any ())->method ('writeFile ' )->willReturn ( $ expectedFileName );
129
141
130
142
// Create adjusted reader which does not validate path.
131
143
$ readMock = $ this ->getMockBuilder (\Magento \Framework \Filesystem \File \Read::class)
132
144
->disableOriginalConstructor ()
133
145
->setMethods (['readAll ' ])
134
146
->getMock ();
135
147
// Check readAll() method invoking.
136
- $ readMock ->expects ($ this ->once ())->method ('readAll ' )->will ( $ this -> returnValue ( null ) );
148
+ $ readMock ->expects ($ this ->once ())->method ('readAll ' )->willReturn ( null );
137
149
138
150
// Check create() method invoking with expected argument.
139
151
$ this ->readFactory ->expects ($ this ->once ())
140
152
->method ('create ' )
141
153
->will ($ this ->returnValue ($ readMock ))->with ($ expectedHost );
142
154
//Check invoking of getTmpDir(), _setUploadFile(), save() methods.
143
- $ this ->uploader ->expects ($ this ->any ())->method ('getTmpDir ' )->will ( $ this -> returnValue ( '' ) );
144
- $ this ->uploader ->expects ($ this ->once ())->method ('_setUploadFile ' )->will ( $ this -> returnSelf () );
155
+ $ this ->uploader ->expects ($ this ->any ())->method ('getTmpDir ' )->willReturn ( '' );
156
+ $ this ->uploader ->expects ($ this ->once ())->method ('_setUploadFile ' )->willReturnSelf ( );
145
157
$ this ->uploader ->expects ($ this ->once ())->method ('save ' )->with ($ destDir . '/ ' . $ expectedFileName )
146
158
->willReturn (['name ' => $ expectedFileName , 'path ' => 'absPath ' ]);
159
+ $ this ->uploader ->expects ($ this ->exactly ($ checkAllowedExtension ))
160
+ ->method ('checkAllowedExtension ' )
161
+ ->willReturn (true );
147
162
148
163
$ this ->uploader ->setDestDir ($ destDir );
149
164
$ result = $ this ->uploader ->move ($ fileUrl );
@@ -161,8 +176,8 @@ public function testMoveFileName()
161
176
$ this ->directoryMock ->expects ($ this ->once ())->method ('getAbsolutePath ' )->with ($ destDir )
162
177
->willReturn ($ destDir . '/ ' . $ fileName );
163
178
//Check invoking of getTmpDir(), _setUploadFile(), save() methods.
164
- $ this ->uploader ->expects ($ this ->once ())->method ('getTmpDir ' )->will ( $ this -> returnValue ( '' ) );
165
- $ this ->uploader ->expects ($ this ->once ())->method ('_setUploadFile ' )->will ( $ this -> returnSelf () );
179
+ $ this ->uploader ->expects ($ this ->once ())->method ('getTmpDir ' )->willReturn ( '' );
180
+ $ this ->uploader ->expects ($ this ->once ())->method ('_setUploadFile ' )->willReturnSelf ( );
166
181
$ this ->uploader ->expects ($ this ->once ())->method ('save ' )->with ($ destDir . '/ ' . $ fileName )
167
182
->willReturn (['name ' => $ fileName ]);
168
183
@@ -239,12 +254,38 @@ public function moveFileUrlDataProvider()
239
254
[
240
255
'$fileUrl ' => 'http://test_uploader_file ' ,
241
256
'$expectedHost ' => 'test_uploader_file ' ,
242
- '$expectedFileName ' => 'httptest_uploader_file ' ,
257
+ '$expectedFileName ' => 'test_uploader_file ' ,
258
+ '$checkAllowedExtension ' => 0 ,
243
259
],
244
260
[
245
261
'$fileUrl ' => 'https://!:^&`;file ' ,
246
262
'$expectedHost ' => '!:^&`;file ' ,
247
- '$expectedFileName ' => 'httpsfile ' ,
263
+ '$expectedFileName ' => 'file ' ,
264
+ '$checkAllowedExtension ' => 0 ,
265
+ ],
266
+ [
267
+ '$fileUrl ' => 'https://www.google.com/image.jpg ' ,
268
+ '$expectedHost ' => 'www.google.com/image.jpg ' ,
269
+ '$expectedFileName ' => 'image.jpg ' ,
270
+ '$checkAllowedExtension ' => 1 ,
271
+ ],
272
+ [
273
+ '$fileUrl ' => 'https://www.google.com/image.jpg?param=1 ' ,
274
+ '$expectedHost ' => 'www.google.com/image.jpg?param=1 ' ,
275
+ '$expectedFileName ' => 'image.jpg ' ,
276
+ '$checkAllowedExtension ' => 1 ,
277
+ ],
278
+ [
279
+ '$fileUrl ' => 'https://www.google.com/image.jpg?param=1¶m=2 ' ,
280
+ '$expectedHost ' => 'www.google.com/image.jpg?param=1¶m=2 ' ,
281
+ '$expectedFileName ' => 'image.jpg ' ,
282
+ '$checkAllowedExtension ' => 1 ,
283
+ ],
284
+ [
285
+ '$fileUrl ' => 'http://www.google.com/image.jpg?param=1¶m=2 ' ,
286
+ '$expectedHost ' => 'www.google.com/image.jpg?param=1¶m=2 ' ,
287
+ '$expectedFileName ' => 'image.jpg ' ,
288
+ '$checkAllowedExtension ' => 1 ,
248
289
],
249
290
];
250
291
}
@@ -253,8 +294,9 @@ public function moveFileUrlDataProvider()
253
294
* @dataProvider validatePathDataProvider
254
295
*
255
296
* @param bool $pathIsValid
297
+ * @return void
256
298
*/
257
- public function testSetTmpDir ($ pathIsValid )
299
+ public function testSetTmpDir (bool $ pathIsValid )
258
300
{
259
301
$ path = 'path ' ;
260
302
$ absolutePath = 'absolute_path ' ;
@@ -272,11 +314,11 @@ public function testSetTmpDir($pathIsValid)
272
314
*
273
315
* @return array
274
316
*/
275
- public function validatePathDataProvider ()
317
+ public function validatePathDataProvider (): array
276
318
{
277
319
return [
278
320
[true ],
279
- [false ]
321
+ [false ],
280
322
];
281
323
}
282
324
}
0 commit comments