@@ -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
}
@@ -229,24 +250,22 @@ public function testGetResizeHeight()
229
250
230
251
/**
231
252
* @covers \Magento\Cms\Model\Wysiwyg\Images\Storage::deleteDirectory
253
+ * @expectedException \Magento\Framework\Exception\LocalizedException
254
+ * @expectedExceptionMessage Directory /storage/some/another/dir is not under storage root path.
232
255
*/
233
256
public function testDeleteDirectoryOverRoot ()
234
257
{
235
- $ this ->expectException (\Magento \Framework \Exception \LocalizedException::class);
236
- $ this ->expectExceptionMessage (
237
- sprintf ('Directory %s is not under storage root path. ' , self ::INVALID_DIRECTORY_OVER_ROOT )
238
- );
239
258
$ this ->driverMock ->expects ($ this ->atLeastOnce ())->method ('getRealPathSafety ' )->will ($ this ->returnArgument (0 ));
240
259
$ this ->imagesStorage ->deleteDirectory (self ::INVALID_DIRECTORY_OVER_ROOT );
241
260
}
242
261
243
262
/**
244
263
* @covers \Magento\Cms\Model\Wysiwyg\Images\Storage::deleteDirectory
264
+ * @expectedException \Magento\Framework\Exception\LocalizedException
265
+ * @expectedExceptionMessage We can't delete root directory /storage/root/dir right now.
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 ));
250
269
$ this ->driverMock ->expects ($ this ->atLeastOnce ())->method ('getRealPathSafety ' )->will ($ this ->returnArgument (0 ));
251
270
$ this ->imagesStorage ->deleteDirectory (self ::STORAGE_ROOT_DIR );
252
271
}
@@ -302,8 +321,8 @@ public function testGetDirsCollection($exclude, $include, $fileNames, $expectedR
302
321
'resizeParameters ' => $ this ->resizeParameters ,
303
322
'dirs ' => [
304
323
'exclude ' => $ exclude ,
305
- 'include ' => $ include
306
- ]
324
+ 'include ' => $ include,
325
+ ],
307
326
]
308
327
);
309
328
@@ -328,48 +347,48 @@ public function dirsCollectionDataProvider()
328
347
return [
329
348
[
330
349
'exclude ' => [
331
- ['name ' => 'dress ' ]
350
+ ['name ' => 'dress ' ],
332
351
],
333
352
'include ' => [],
334
353
'filenames ' => [],
335
- 'expectRemoveKeys ' => []
354
+ 'expectRemoveKeys ' => [],
336
355
],
337
356
[
338
357
'exclude ' => [],
339
358
'include ' => [],
340
359
'filenames ' => [
341
360
'/dress ' ,
342
361
],
343
- 'expectRemoveKeys ' => []
362
+ 'expectRemoveKeys ' => [],
344
363
],
345
364
[
346
365
'exclude ' => [
347
- ['name ' => 'dress ' ]
366
+ ['name ' => 'dress ' ],
348
367
],
349
368
'include ' => [],
350
369
'filenames ' => [
351
370
'/collection ' ,
352
371
],
353
- 'expectRemoveKeys ' => []
372
+ 'expectRemoveKeys ' => [],
354
373
],
355
374
[
356
375
'exclude ' => [
357
376
['name ' => 'gear ' , 'regexp ' => 1 ],
358
377
['name ' => 'home ' , 'regexp ' => 1 ],
359
378
['name ' => 'collection ' ],
360
- ['name ' => 'dress ' ]
379
+ ['name ' => 'dress ' ],
361
380
],
362
381
'include ' => [
363
382
['name ' => 'home ' , 'regexp ' => 1 ],
364
- ['name ' => 'collection ' ]
383
+ ['name ' => 'collection ' ],
365
384
],
366
385
'filenames ' => [
367
386
'/dress ' ,
368
387
'/collection ' ,
369
- '/gear '
388
+ '/gear ' ,
370
389
],
371
- 'expectRemoveKeys ' => [[0 ], [2 ]]
372
- ]
390
+ 'expectRemoveKeys ' => [[0 ], [2 ]],
391
+ ],
373
392
];
374
393
}
375
394
@@ -411,4 +430,94 @@ protected function generalTestGetDirsCollection($path, $collectionArray = [], $e
411
430
412
431
$ this ->imagesStorage ->getDirsCollection ($ path );
413
432
}
433
+
434
+ public function testUploadFile ()
435
+ {
436
+ $ targetPath = '/target/path ' ;
437
+ $ fileName = 'image.gif ' ;
438
+ $ realPath = $ targetPath . '/ ' . $ fileName ;
439
+ $ thumbnailTargetPath = self ::STORAGE_ROOT_DIR . '/.thumbs ' ;
440
+ $ thumbnailDestination = $ thumbnailTargetPath . '/ ' . $ fileName ;
441
+ $ type = 'image ' ;
442
+ $ result = [
443
+ 'result ' ,
444
+ 'cookie ' => [
445
+ 'name ' => 'session_name ' ,
446
+ 'value ' => '1 ' ,
447
+ 'lifetime ' => '50 ' ,
448
+ 'path ' => 'cookie/path ' ,
449
+ 'domain ' => 'cookie_domain ' ,
450
+ ],
451
+ ];
452
+ $ uploader = $ this ->getMockBuilder (\Magento \MediaStorage \Model \File \Uploader::class)
453
+ ->disableOriginalConstructor ()
454
+ ->setMethods (
455
+ [
456
+ 'setAllowedExtensions ' ,
457
+ 'setAllowRenameFiles ' ,
458
+ 'setFilesDispersion ' ,
459
+ 'checkMimeType ' ,
460
+ 'save ' ,
461
+ 'getUploadedFileName ' ,
462
+ ]
463
+ )
464
+ ->getMock ();
465
+ $ this ->uploaderFactoryMock ->expects ($ this ->atLeastOnce ())->method ('create ' )->with (['fileId ' => 'image ' ])
466
+ ->willReturn ($ uploader );
467
+ $ uploader ->expects ($ this ->atLeastOnce ())->method ('setAllowedExtensions ' )
468
+ ->with (array_keys ($ this ->allowedImageExtensions ))->willReturnSelf ();
469
+ $ uploader ->expects ($ this ->atLeastOnce ())->method ('setAllowRenameFiles ' )->with (true )->willReturnSelf ();
470
+ $ uploader ->expects ($ this ->atLeastOnce ())->method ('setFilesDispersion ' )->with (false )
471
+ ->willReturnSelf ();
472
+ $ uploader ->expects ($ this ->atLeastOnce ())->method ('checkMimeType ' )
473
+ ->with (array_values ($ this ->allowedImageExtensions ))->willReturnSelf ();
474
+ $ uploader ->expects ($ this ->atLeastOnce ())->method ('save ' )->with ($ targetPath )->willReturn ($ result );
475
+ $ uploader ->expects ($ this ->atLeastOnce ())->method ('getUploadedFileName ' )->willReturn ($ fileName );
476
+
477
+ $ this ->directoryMock ->expects ($ this ->atLeastOnce ())->method ('getRelativePath ' )->willReturnMap (
478
+ [
479
+ [$ realPath , $ realPath ],
480
+ [$ thumbnailTargetPath , $ thumbnailTargetPath ],
481
+ [$ thumbnailDestination , $ thumbnailDestination ],
482
+ ]
483
+ );
484
+ $ this ->directoryMock ->expects ($ this ->atLeastOnce ())->method ('isFile ' )
485
+ ->willReturnMap (
486
+ [
487
+ [$ realPath , true ],
488
+ [$ thumbnailDestination , true ],
489
+ ]
490
+ );
491
+ $ this ->directoryMock ->expects ($ this ->atLeastOnce ())->method ('isExist ' )
492
+ ->willReturnMap (
493
+ [
494
+ [$ realPath , true ],
495
+ [$ thumbnailTargetPath , true ],
496
+ ]
497
+ );
498
+
499
+ $ image = $ this ->getMockBuilder (\Magento \Catalog \Model \Product \Image::class)
500
+ ->disableOriginalConstructor ()
501
+ ->setMethods (['open ' , 'keepAspectRatio ' , 'resize ' , 'save ' ])
502
+ ->getMock ();
503
+ $ image ->expects ($ this ->atLeastOnce ())->method ('open ' )->with ($ realPath );
504
+ $ image ->expects ($ this ->atLeastOnce ())->method ('keepAspectRatio ' )->with (true );
505
+ $ image ->expects ($ this ->atLeastOnce ())->method ('resize ' )->with (100 , 50 );
506
+ $ image ->expects ($ this ->atLeastOnce ())->method ('save ' )->with ($ thumbnailDestination );
507
+
508
+ $ this ->adapterFactoryMock ->expects ($ this ->atLeastOnce ())->method ('create ' )->willReturn ($ image );
509
+
510
+ $ this ->sessionMock ->expects ($ this ->atLeastOnce ())->method ('getName ' )
511
+ ->willReturn ($ result ['cookie ' ]['name ' ]);
512
+ $ this ->sessionMock ->expects ($ this ->atLeastOnce ())->method ('getSessionId ' )
513
+ ->willReturn ($ result ['cookie ' ]['value ' ]);
514
+ $ this ->sessionMock ->expects ($ this ->atLeastOnce ())->method ('getCookieLifetime ' )
515
+ ->willReturn ($ result ['cookie ' ]['lifetime ' ]);
516
+ $ this ->sessionMock ->expects ($ this ->atLeastOnce ())->method ('getCookiePath ' )
517
+ ->willReturn ($ result ['cookie ' ]['path ' ]);
518
+ $ this ->sessionMock ->expects ($ this ->atLeastOnce ())->method ('getCookieDomain ' )
519
+ ->willReturn ($ result ['cookie ' ]['domain ' ]);
520
+
521
+ $ this ->assertEquals ($ result , $ this ->imagesStorage ->uploadFile ($ targetPath , $ type ));
522
+ }
414
523
}
0 commit comments