@@ -107,6 +107,13 @@ class StorageTest extends \PHPUnit\Framework\TestCase
107
107
*/
108
108
protected $ objectManagerHelper ;
109
109
110
+ private $ allowedImageExtensions = [
111
+ 'jpg ' => 'image/jpg ' ,
112
+ 'jpeg ' => 'image/jpeg ' ,
113
+ 'png ' => 'image/png ' ,
114
+ 'gif ' => 'image/png ' ,
115
+ ];
116
+
110
117
/**
111
118
* @return void
112
119
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -120,7 +127,7 @@ protected function setUp()
120
127
121
128
$ this ->directoryMock = $ this ->createPartialMock (
122
129
\Magento \Framework \Filesystem \Directory \Write::class,
123
- ['delete ' , 'getDriver ' , 'create ' , 'getRelativePath ' , 'isExist ' ]
130
+ ['delete ' , 'getDriver ' , 'create ' , 'getRelativePath ' , 'isExist ' , ' isFile ' ]
124
131
);
125
132
$ this ->directoryMock ->expects (
126
133
$ this ->any ()
@@ -176,14 +183,27 @@ protected function setUp()
176
183
->disableOriginalConstructor ()
177
184
->getMock ();
178
185
$ this ->sessionMock = $ this ->getMockBuilder (\Magento \Backend \Model \Session::class)
179
- ->setMethods (['getCurrentPath ' ])
186
+ ->setMethods (
187
+ [
188
+ 'getCurrentPath ' ,
189
+ 'getName ' ,
190
+ 'getSessionId ' ,
191
+ 'getCookieLifetime ' ,
192
+ 'getCookiePath ' ,
193
+ 'getCookieDomain ' ,
194
+ ]
195
+ )
180
196
->disableOriginalConstructor ()
181
197
->getMock ();
182
198
$ this ->backendUrlMock = $ this ->createMock (\Magento \Backend \Model \Url::class);
183
199
184
200
$ this ->coreFileStorageMock = $ this ->getMockBuilder (\Magento \MediaStorage \Helper \File \Storage \Database::class)
185
201
->disableOriginalConstructor ()
186
202
->getMock ();
203
+ $ allowedExtensions = [
204
+ 'allowed ' => $ this ->allowedImageExtensions ,
205
+ 'image_allowed ' => $ this ->allowedImageExtensions ,
206
+ ];
187
207
188
208
$ this ->objectManagerHelper = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
189
209
@@ -205,8 +225,9 @@ protected function setUp()
205
225
'resizeParameters ' => $ this ->resizeParameters ,
206
226
'dirs ' => [
207
227
'exclude ' => [],
208
- 'include ' => []
209
- ]
228
+ 'include ' => [],
229
+ ],
230
+ 'extensions ' => $ allowedExtensions ,
210
231
]
211
232
);
212
233
}
@@ -232,8 +253,8 @@ public function testGetResizeHeight()
232
253
*/
233
254
public function testDeleteDirectoryOverRoot ()
234
255
{
235
- $ this ->expectException (\ Magento \ Framework \ Exception \LocalizedException::class);
236
- $ this -> expectExceptionMessage (
256
+ $ this ->expectException (
257
+ \ Magento \ Framework \ Exception \LocalizedException::class,
237
258
sprintf ('Directory %s is not under storage root path. ' , self ::INVALID_DIRECTORY_OVER_ROOT )
238
259
);
239
260
$ this ->driverMock ->expects ($ this ->atLeastOnce ())->method ('getRealPathSafety ' )->will ($ this ->returnArgument (0 ));
@@ -245,8 +266,10 @@ public function testDeleteDirectoryOverRoot()
245
266
*/
246
267
public function testDeleteRootDirectory ()
247
268
{
248
- $ this ->expectException (\Magento \Framework \Exception \LocalizedException::class);
249
- $ this ->expectExceptionMessage (sprintf ('We can \'t delete root directory %s right now. ' , self ::STORAGE_ROOT_DIR ));
269
+ $ this ->expectException (
270
+ \Magento \Framework \Exception \LocalizedException::class,
271
+ sprintf ('We can \'t delete root directory %s right now. ' , self ::STORAGE_ROOT_DIR )
272
+ );
250
273
$ this ->driverMock ->expects ($ this ->atLeastOnce ())->method ('getRealPathSafety ' )->will ($ this ->returnArgument (0 ));
251
274
$ this ->imagesStorage ->deleteDirectory (self ::STORAGE_ROOT_DIR );
252
275
}
@@ -302,8 +325,8 @@ public function testGetDirsCollection($exclude, $include, $fileNames, $expectedR
302
325
'resizeParameters ' => $ this ->resizeParameters ,
303
326
'dirs ' => [
304
327
'exclude ' => $ exclude ,
305
- 'include ' => $ include
306
- ]
328
+ 'include ' => $ include,
329
+ ],
307
330
]
308
331
);
309
332
@@ -328,48 +351,48 @@ public function dirsCollectionDataProvider()
328
351
return [
329
352
[
330
353
'exclude ' => [
331
- ['name ' => 'dress ' ]
354
+ ['name ' => 'dress ' ],
332
355
],
333
356
'include ' => [],
334
357
'filenames ' => [],
335
- 'expectRemoveKeys ' => []
358
+ 'expectRemoveKeys ' => [],
336
359
],
337
360
[
338
361
'exclude ' => [],
339
362
'include ' => [],
340
363
'filenames ' => [
341
364
'/dress ' ,
342
365
],
343
- 'expectRemoveKeys ' => []
366
+ 'expectRemoveKeys ' => [],
344
367
],
345
368
[
346
369
'exclude ' => [
347
- ['name ' => 'dress ' ]
370
+ ['name ' => 'dress ' ],
348
371
],
349
372
'include ' => [],
350
373
'filenames ' => [
351
374
'/collection ' ,
352
375
],
353
- 'expectRemoveKeys ' => []
376
+ 'expectRemoveKeys ' => [],
354
377
],
355
378
[
356
379
'exclude ' => [
357
380
['name ' => 'gear ' , 'regexp ' => 1 ],
358
381
['name ' => 'home ' , 'regexp ' => 1 ],
359
382
['name ' => 'collection ' ],
360
- ['name ' => 'dress ' ]
383
+ ['name ' => 'dress ' ],
361
384
],
362
385
'include ' => [
363
386
['name ' => 'home ' , 'regexp ' => 1 ],
364
- ['name ' => 'collection ' ]
387
+ ['name ' => 'collection ' ],
365
388
],
366
389
'filenames ' => [
367
390
'/dress ' ,
368
391
'/collection ' ,
369
- '/gear '
392
+ '/gear ' ,
370
393
],
371
- 'expectRemoveKeys ' => [[0 ], [2 ]]
372
- ]
394
+ 'expectRemoveKeys ' => [[0 ], [2 ]],
395
+ ],
373
396
];
374
397
}
375
398
@@ -411,4 +434,94 @@ protected function generalTestGetDirsCollection($path, $collectionArray = [], $e
411
434
412
435
$ this ->imagesStorage ->getDirsCollection ($ path );
413
436
}
437
+
438
+ public function testUploadFile ()
439
+ {
440
+ $ targetPath = '/target/path ' ;
441
+ $ fileName = 'image.gif ' ;
442
+ $ realPath = $ targetPath . '/ ' . $ fileName ;
443
+ $ thumbnailTargetPath = self ::STORAGE_ROOT_DIR . '/.thumbs ' ;
444
+ $ thumbnailDestination = $ thumbnailTargetPath . '/ ' . $ fileName ;
445
+ $ type = 'image ' ;
446
+ $ result = [
447
+ 'result ' ,
448
+ 'cookie ' => [
449
+ 'name ' => 'session_name ' ,
450
+ 'value ' => '1 ' ,
451
+ 'lifetime ' => '50 ' ,
452
+ 'path ' => 'cookie/path ' ,
453
+ 'domain ' => 'cookie_domain ' ,
454
+ ],
455
+ ];
456
+ $ uploader = $ this ->getMockBuilder (\Magento \MediaStorage \Model \File \Uploader::class)
457
+ ->disableOriginalConstructor ()
458
+ ->setMethods (
459
+ [
460
+ 'setAllowedExtensions ' ,
461
+ 'setAllowRenameFiles ' ,
462
+ 'setFilesDispersion ' ,
463
+ 'checkMimeType ' ,
464
+ 'save ' ,
465
+ 'getUploadedFileName ' ,
466
+ ]
467
+ )
468
+ ->getMock ();
469
+ $ this ->uploaderFactoryMock ->expects ($ this ->atLeastOnce ())->method ('create ' )->with (['fileId ' => 'image ' ])
470
+ ->willReturn ($ uploader );
471
+ $ uploader ->expects ($ this ->atLeastOnce ())->method ('setAllowedExtensions ' )
472
+ ->with (array_keys ($ this ->allowedImageExtensions ))->willReturnSelf ();
473
+ $ uploader ->expects ($ this ->atLeastOnce ())->method ('setAllowRenameFiles ' )->with (true )->willReturnSelf ();
474
+ $ uploader ->expects ($ this ->atLeastOnce ())->method ('setFilesDispersion ' )->with (false )
475
+ ->willReturnSelf ();
476
+ $ uploader ->expects ($ this ->atLeastOnce ())->method ('checkMimeType ' )
477
+ ->with (array_values ($ this ->allowedImageExtensions ))->willReturnSelf ();
478
+ $ uploader ->expects ($ this ->atLeastOnce ())->method ('save ' )->with ($ targetPath )->willReturn ($ result );
479
+ $ uploader ->expects ($ this ->atLeastOnce ())->method ('getUploadedFileName ' )->willReturn ($ fileName );
480
+
481
+ $ this ->directoryMock ->expects ($ this ->atLeastOnce ())->method ('getRelativePath ' )->willReturnMap (
482
+ [
483
+ [$ realPath , $ realPath ],
484
+ [$ thumbnailTargetPath , $ thumbnailTargetPath ],
485
+ [$ thumbnailDestination , $ thumbnailDestination ],
486
+ ]
487
+ );
488
+ $ this ->directoryMock ->expects ($ this ->atLeastOnce ())->method ('isFile ' )
489
+ ->willReturnMap (
490
+ [
491
+ [$ realPath , true ],
492
+ [$ thumbnailDestination , true ],
493
+ ]
494
+ );
495
+ $ this ->directoryMock ->expects ($ this ->atLeastOnce ())->method ('isExist ' )
496
+ ->willReturnMap (
497
+ [
498
+ [$ realPath , true ],
499
+ [$ thumbnailTargetPath , true ],
500
+ ]
501
+ );
502
+
503
+ $ image = $ this ->getMockBuilder (\Magento \Catalog \Model \Product \Image::class)
504
+ ->disableOriginalConstructor ()
505
+ ->setMethods (['open ' , 'keepAspectRatio ' , 'resize ' , 'save ' ])
506
+ ->getMock ();
507
+ $ image ->expects ($ this ->atLeastOnce ())->method ('open ' )->with ($ realPath );
508
+ $ image ->expects ($ this ->atLeastOnce ())->method ('keepAspectRatio ' )->with (true );
509
+ $ image ->expects ($ this ->atLeastOnce ())->method ('resize ' )->with (100 , 50 );
510
+ $ image ->expects ($ this ->atLeastOnce ())->method ('save ' )->with ($ thumbnailDestination );
511
+
512
+ $ this ->adapterFactoryMock ->expects ($ this ->atLeastOnce ())->method ('create ' )->willReturn ($ image );
513
+
514
+ $ this ->sessionMock ->expects ($ this ->atLeastOnce ())->method ('getName ' )
515
+ ->willReturn ($ result ['cookie ' ]['name ' ]);
516
+ $ this ->sessionMock ->expects ($ this ->atLeastOnce ())->method ('getSessionId ' )
517
+ ->willReturn ($ result ['cookie ' ]['value ' ]);
518
+ $ this ->sessionMock ->expects ($ this ->atLeastOnce ())->method ('getCookieLifetime ' )
519
+ ->willReturn ($ result ['cookie ' ]['lifetime ' ]);
520
+ $ this ->sessionMock ->expects ($ this ->atLeastOnce ())->method ('getCookiePath ' )
521
+ ->willReturn ($ result ['cookie ' ]['path ' ]);
522
+ $ this ->sessionMock ->expects ($ this ->atLeastOnce ())->method ('getCookieDomain ' )
523
+ ->willReturn ($ result ['cookie ' ]['domain ' ]);
524
+
525
+ $ this ->assertEquals ($ result , $ this ->imagesStorage ->uploadFile ($ targetPath , $ type ));
526
+ }
414
527
}
0 commit comments