@@ -48,6 +48,11 @@ class UploaderTest extends \PHPUnit_Framework_TestCase
48
48
*/
49
49
private $ directoryResolver ;
50
50
51
+ /**
52
+ * @var \Magento\Framework\Math\Random|\PHPUnit_Framework_MockObject_MockObject
53
+ */
54
+ private $ random ;
55
+
51
56
/**
52
57
* @var \Magento\CatalogImportExport\Model\Import\Uploader|\PHPUnit_Framework_MockObject_MockObject
53
58
*/
@@ -94,6 +99,11 @@ protected function setUp()
94
99
->setMethods (['validatePath ' ])
95
100
->getMock ();
96
101
102
+ $ this ->random = $ this ->getMockBuilder (\Magento \Framework \Math \Random::class)
103
+ ->disableOriginalConstructor ()
104
+ ->setMethods (['getRandomString ' ])
105
+ ->getMock ();
106
+
97
107
$ this ->uploader = $ this ->getMockBuilder (\Magento \CatalogImportExport \Model \Import \Uploader::class)
98
108
->setConstructorArgs ([
99
109
$ this ->coreFileStorageDb ,
@@ -104,49 +114,75 @@ protected function setUp()
104
114
$ this ->readFactory ,
105
115
null ,
106
116
$ this ->directoryResolver ,
117
+ $ this ->random
107
118
])
108
119
->setMethods (['_setUploadFile ' , 'save ' , 'getTmpDir ' , 'checkAllowedExtension ' ])
109
120
->getMock ();
110
121
}
111
122
112
123
/**
113
124
* @dataProvider moveFileUrlDataProvider
125
+ * @param $fileUrl
126
+ * @param $expectedHost
127
+ * @param $expectedFileName
128
+ * @param $checkAllowedExtension
129
+ * @throws \Magento\Framework\Exception\LocalizedException
114
130
*/
115
131
public function testMoveFileUrl ($ fileUrl , $ expectedHost , $ expectedFileName , $ checkAllowedExtension )
116
132
{
133
+ $ tmpDir = 'var/tmp ' ;
117
134
$ destDir = 'var/dest/dir ' ;
118
- $ expectedRelativeFilePath = $ expectedFileName ;
119
- $ this ->directoryMock ->expects ($ this ->once ())->method ('isWritable ' )->with ($ destDir )->willReturn (true );
120
- $ this ->directoryMock ->expects ($ this ->any ())->method ('getRelativePath ' )->with ($ expectedRelativeFilePath );
121
- $ this ->directoryMock ->expects ($ this ->once ())->method ('getAbsolutePath ' )->with ($ destDir )
122
- ->willReturn ($ destDir . '/ ' . $ expectedFileName );
123
- // Check writeFile() method invoking.
124
- $ this ->directoryMock ->expects ($ this ->any ())->method ('writeFile ' )->will ($ this ->returnValue ($ expectedFileName ));
135
+
136
+ // Expected invocation to validate file extension
137
+ $ this ->uploader ->expects ($ this ->exactly ($ checkAllowedExtension ))->method ('checkAllowedExtension ' )
138
+ ->willReturn (true );
139
+
140
+ // Expected invocation to generate random string for file name postfix
141
+ $ this ->random ->expects ($ this ->once ())->method ('getRandomString ' )
142
+ ->with (16 )
143
+ ->willReturn ('38GcEmPFKXXR8NMj ' );
144
+
145
+ // Expected invocation to build the temp file path with the correct directory and filename
146
+ $ this ->directoryMock ->expects ($ this ->any ())->method ('getRelativePath ' )
147
+ ->with ($ tmpDir . '/ ' . $ expectedFileName );
125
148
126
149
// Create adjusted reader which does not validate path.
127
150
$ readMock = $ this ->getMockBuilder (\Magento \Framework \Filesystem \File \Read::class)
128
151
->disableOriginalConstructor ()
129
152
->setMethods (['readAll ' ])
130
153
->getMock ();
131
- // Check readAll() method invoking.
132
- $ readMock ->expects ($ this ->once ())->method ('readAll ' )->will ($ this ->returnValue (null ));
133
154
134
- // Check create() method invoking with expected argument.
135
- $ this ->readFactory ->expects ($ this ->once ())
136
- ->method ('create ' )
137
- ->will ($ this ->returnValue ($ readMock ))->with ($ expectedHost );
138
- //Check invoking of getTmpDir(), _setUploadFile(), save() methods.
139
- $ this ->uploader ->expects ($ this ->any ())->method ('getTmpDir ' )->will ($ this ->returnValue ('' ));
140
- $ this ->uploader ->expects ($ this ->once ())->method ('_setUploadFile ' )->will ($ this ->returnSelf ());
141
- $ this ->uploader ->expects ($ this ->once ())->method ('save ' )->with ($ destDir . '/ ' . $ expectedFileName )
142
- ->willReturn (['name ' => $ expectedFileName , 'path ' => 'absPath ' ]);
143
- $ this ->uploader ->expects ($ this ->exactly ($ checkAllowedExtension ))
144
- ->method ('checkAllowedExtension ' )
155
+ // Expected invocations to create reader and read contents from url
156
+ $ this ->readFactory ->expects ($ this ->once ())->method ('create ' )
157
+ ->with ($ expectedHost )
158
+ ->will ($ this ->returnValue ($ readMock ));
159
+ $ readMock ->expects ($ this ->once ())->method ('readAll ' )
160
+ ->will ($ this ->returnValue (null ));
161
+
162
+ // Expected invocation to write the temp file
163
+ $ this ->directoryMock ->expects ($ this ->any ())->method ('writeFile ' )
164
+ ->will ($ this ->returnValue ($ expectedFileName ));
165
+
166
+ // Expected invocations to move the temp file to the destination directory
167
+ $ this ->directoryMock ->expects ($ this ->once ())->method ('isWritable ' )
168
+ ->with ($ destDir )
145
169
->willReturn (true );
170
+ $ this ->directoryMock ->expects ($ this ->once ())->method ('getAbsolutePath ' )
171
+ ->with ($ destDir )
172
+ ->willReturn ($ destDir . '/ ' . $ expectedFileName );
173
+ $ this ->uploader ->expects ($ this ->once ())->method ('_setUploadFile ' )
174
+ ->willReturnSelf ();
175
+ $ this ->uploader ->expects ($ this ->once ())->method ('save ' )
176
+ ->with ($ destDir . '/ ' . $ expectedFileName )
177
+ ->willReturn (['name ' => $ expectedFileName , 'path ' => 'absPath ' ]);
178
+
179
+ // Do not use configured temp directory
180
+ $ this ->uploader ->expects ($ this ->never ())->method ('getTmpDir ' );
146
181
147
182
$ this ->uploader ->setDestDir ($ destDir );
148
183
$ result = $ this ->uploader ->move ($ fileUrl );
149
184
$ this ->assertEquals (['name ' => $ expectedFileName ], $ result );
185
+
150
186
$ this ->assertArrayNotHasKey ('path ' , $ result );
151
187
}
152
188
@@ -175,18 +211,66 @@ public function testMoveFileName()
175
211
public function moveFileUrlDataProvider ()
176
212
{
177
213
return [
178
- [
179
- '$fileUrl ' => 'http ://test_uploader_file ' ,
214
+ ' https_no_file_ext ' => [
215
+ '$fileUrl ' => 'https ://test_uploader_file ' ,
180
216
'$expectedHost ' => 'test_uploader_file ' ,
181
- '$expectedFileName ' => 'httptest_uploader_file ' ,
182
- '$checkAllowedExtension ' => 0 ,
217
+ '$expectedFileName ' => 'test_uploader_file_38GcEmPFKXXR8NMj ' ,
218
+ '$checkAllowedExtension ' => 0
219
+ ],
220
+ 'https_invalid_chars ' => [
221
+ '$fileUrl ' => 'https://www.google.com/!:^&`;image.jpg ' ,
222
+ '$expectedHost ' => 'www.google.com/!:^&`;image.jpg ' ,
223
+ '$expectedFileName ' => 'image_38GcEmPFKXXR8NMj.jpg ' ,
224
+ '$checkAllowedExtension ' => 1
225
+ ],
226
+ 'https_invalid_chars_no_file_ext ' => [
227
+ '$fileUrl ' => 'https://!:^&`;image ' ,
228
+ '$expectedHost ' => '!:^&`;image ' ,
229
+ '$expectedFileName ' => 'image_38GcEmPFKXXR8NMj ' ,
230
+ '$checkAllowedExtension ' => 0
231
+ ],
232
+ 'http_jpg ' => [
233
+ '$fileUrl ' => 'http://www.google.com/image.jpg ' ,
234
+ '$expectedHost ' => 'www.google.com/image.jpg ' ,
235
+ '$expectedFileName ' => 'image_38GcEmPFKXXR8NMj.jpg ' ,
236
+ '$checkAllowedExtension ' => 1
237
+ ],
238
+ 'https_jpg ' => [
239
+ '$fileUrl ' => 'https://www.google.com/image.jpg ' ,
240
+ '$expectedHost ' => 'www.google.com/image.jpg ' ,
241
+ '$expectedFileName ' => 'image_38GcEmPFKXXR8NMj.jpg ' ,
242
+ '$checkAllowedExtension ' => 1
243
+ ],
244
+ 'https_jpeg ' => [
245
+ '$fileUrl ' => 'https://www.google.com/image.jpeg ' ,
246
+ '$expectedHost ' => 'www.google.com/image.jpeg ' ,
247
+ '$expectedFileName ' => 'image_38GcEmPFKXXR8NMj.jpeg ' ,
248
+ '$checkAllowedExtension ' => 1
249
+ ],
250
+ 'https_png ' => [
251
+ '$fileUrl ' => 'https://www.google.com/image.png ' ,
252
+ '$expectedHost ' => 'www.google.com/image.png ' ,
253
+ '$expectedFileName ' => 'image_38GcEmPFKXXR8NMj.png ' ,
254
+ '$checkAllowedExtension ' => 1
255
+ ],
256
+ 'https_gif ' => [
257
+ '$fileUrl ' => 'https://www.google.com/image.gif ' ,
258
+ '$expectedHost ' => 'www.google.com/image.gif ' ,
259
+ '$expectedFileName ' => 'image_38GcEmPFKXXR8NMj.gif ' ,
260
+ '$checkAllowedExtension ' => 1
183
261
],
184
- [
185
- '$fileUrl ' => 'https://!:^&`;file ' ,
186
- '$expectedHost ' => '!:^&`;file ' ,
187
- '$expectedFileName ' => 'httpsfile ' ,
188
- '$checkAllowedExtension ' => 0 ,
262
+ ' https_one_query_param ' => [
263
+ '$fileUrl ' => 'https://www.google.com/image.jpg?param=1 ' ,
264
+ '$expectedHost ' => 'www.google.com/image.jpg?param=1 ' ,
265
+ '$expectedFileName ' => 'image_38GcEmPFKXXR8NMj.jpg ' ,
266
+ '$checkAllowedExtension ' => 1
189
267
],
268
+ 'https_two_query_params ' => [
269
+ '$fileUrl ' => 'https://www.google.com/image.jpg?param=1¶m=2 ' ,
270
+ '$expectedHost ' => 'www.google.com/image.jpg?param=1¶m=2 ' ,
271
+ '$expectedFileName ' => 'image_38GcEmPFKXXR8NMj.jpg ' ,
272
+ '$checkAllowedExtension ' => 1
273
+ ]
190
274
];
191
275
}
192
276
0 commit comments