16
16
use Magento \Cms \Model \Wysiwyg \Images \Storage \CollectionFactory ;
17
17
use Magento \Framework \App \Config \ScopeConfigInterface ;
18
18
use Magento \Framework \App \Filesystem \DirectoryList ;
19
+ use Magento \Framework \DataObject ;
20
+ use Magento \Framework \Exception \FileSystemException ;
19
21
use Magento \Framework \Exception \LocalizedException ;
20
22
use Magento \Framework \Filesystem ;
21
23
use Magento \Framework \Filesystem \Directory \Write ;
31
33
use Magento \MediaStorage \Model \File \UploaderFactory ;
32
34
use PHPUnit \Framework \MockObject \MockObject ;
33
35
use PHPUnit \Framework \TestCase ;
36
+ use Psr \Log \LoggerInterface ;
34
37
35
38
/**
36
39
* @SuppressWarnings(PHPMD.LongVariable)
@@ -141,6 +144,16 @@ class StorageTest extends TestCase
141
144
*/
142
145
private $ fileMock ;
143
146
147
+ /**
148
+ * @var LoggerInterface|MockObject
149
+ */
150
+ private $ loggerMock ;
151
+
152
+ /**
153
+ * @var Repository|MockObject
154
+ */
155
+ private $ assetRepo ;
156
+
144
157
/**
145
158
* @var array
146
159
*/
@@ -206,7 +219,7 @@ function ($path) {
206
219
$ this ->adapterFactoryMock = $ this ->createMock (AdapterFactory::class);
207
220
$ this ->imageHelperMock = $ this ->createPartialMock (
208
221
Images::class,
209
- ['getStorageRoot ' , 'getCurrentPath ' ]
222
+ ['getStorageRoot ' , 'getCurrentPath ' , ' getCurrentUrl ' ]
210
223
);
211
224
$ this ->imageHelperMock ->expects (
212
225
$ this ->any ()
@@ -234,6 +247,12 @@ function ($path) {
234
247
Database::class
235
248
);
236
249
250
+ $ this ->loggerMock = $ this ->getMockBuilder (LoggerInterface::class)
251
+ ->disableOriginalConstructor ()
252
+ ->getMock ();
253
+
254
+ $ this ->assetRepo = $ this ->createMock (Repository::class);
255
+
237
256
$ this ->uploaderFactoryMock = $ this ->getMockBuilder (UploaderFactory::class)
238
257
->disableOriginalConstructor ()
239
258
->getMock ();
@@ -284,7 +303,7 @@ function ($path) {
284
303
'coreFileStorageDb ' => $ this ->coreFileStorageMock ,
285
304
'filesystem ' => $ this ->filesystemMock ,
286
305
'imageFactory ' => $ this ->adapterFactoryMock ,
287
- 'assetRepo ' => $ this ->createMock (Repository::class) ,
306
+ 'assetRepo ' => $ this ->assetRepo ,
288
307
'storageCollectionFactory ' => $ this ->storageCollectionFactoryMock ,
289
308
'storageFileFactory ' => $ this ->storageFileFactoryMock ,
290
309
'storageDatabaseFactory ' => $ this ->storageDatabaseFactoryMock ,
@@ -299,7 +318,8 @@ function ($path) {
299
318
'data ' => [],
300
319
'file ' => $ this ->fileMock ,
301
320
'ioFile ' => $ this ->ioFileMock ,
302
- 'coreConfig ' => $ this ->coreConfigMock
321
+ 'coreConfig ' => $ this ->coreConfigMock ,
322
+ 'logger ' => $ this ->loggerMock
303
323
]
304
324
);
305
325
}
@@ -368,6 +388,130 @@ public function testGetDirsCollectionCreateSubDirectories()
368
388
);
369
389
}
370
390
391
+ /**
392
+ * Test getFilesCollection() with the set of valid and invalid files
393
+ *
394
+ * @return void
395
+ * @throws LocalizedException
396
+ * @throws FileSystemException
397
+ * @dataProvider fileItemsDataProvider
398
+ */
399
+ public function testGetFilesCollection (
400
+ int $ timesWarningTriggered ,
401
+ string $ thumbnailPath ,
402
+ DataObject $ imageItem
403
+ ) {
404
+ /** @var StorageCollection|MockObject $storageCollectionMock */
405
+ $ storageCollectionMock = $ this ->getMockBuilder (StorageCollection::class)
406
+ ->disableOriginalConstructor ()
407
+ ->getMock ();
408
+ $ storageCollectionMock ->expects ($ this ->once ())
409
+ ->method ('setCollectDirs ' )
410
+ ->willReturnSelf ();
411
+ $ storageCollectionMock ->expects ($ this ->once ())
412
+ ->method ('setCollectFiles ' )
413
+ ->willReturnSelf ();
414
+ $ storageCollectionMock ->expects ($ this ->once ())
415
+ ->method ('setCollectRecursively ' )
416
+ ->willReturnSelf ();
417
+ $ storageCollectionMock ->expects ($ this ->once ())
418
+ ->method ('setOrder ' )
419
+ ->willReturnSelf ();
420
+ $ storageCollectionMock ->method ('getIterator ' )
421
+ ->willReturn (new \ArrayIterator ([$ imageItem ]));
422
+
423
+ $ this ->storageCollectionFactoryMock ->expects ($ this ->once ())
424
+ ->method ('create ' )
425
+ ->willReturn ($ storageCollectionMock );
426
+
427
+ $ this ->driverMock ->expects (self ::once ())
428
+ ->method ('stat ' )
429
+ ->willReturn ($ imageItem ->toArray ());
430
+
431
+ $ this ->assetRepo ->expects ($ this ->exactly ($ timesWarningTriggered ))
432
+ ->method ('getUrl ' )
433
+ ->willReturn ($ thumbnailPath );
434
+
435
+ $ this ->loggerMock ->expects ($ this ->exactly ($ timesWarningTriggered ))
436
+ ->method ('warning ' )
437
+ ->with (
438
+ sprintf (
439
+ "The image %s is invalid and cannot be displayed in the gallery. " ,
440
+ $ imageItem ->getBasename ()
441
+ )
442
+ );
443
+
444
+ $ this ->imagesStorage ->getFilesCollection ('/webroot/pub/media/ ' , 'image ' );
445
+ }
446
+
447
+ /**
448
+ * Returns a set of valid and invalid image files
449
+ *
450
+ * @return array[]
451
+ */
452
+ public function fileItemsDataProvider ()
453
+ {
454
+ return [
455
+ // Images files with the size of 0 bytes should generate proper warnings
456
+ [
457
+ 'timesWarningTriggered ' => 1 ,
458
+ 'thumbnailPath ' => Storage::THUMB_PLACEHOLDER_PATH_SUFFIX ,
459
+ 'imageItem ' =>
460
+ new DataObject (
461
+ [
462
+ 'mtime ' => 0 ,
463
+ 'size ' => 0 ,
464
+ 'filename ' => '/webroot/pub/media/wysiwyg/zero-bytes.jpg ' ,
465
+ 'basename ' => 'zero-bytes.jpg ' ,
466
+ 'id ' => 1 ,
467
+ 'name ' => 'zero-bytes.jpg ' ,
468
+ 'short_name ' => 'zero-bytes.jpg ' ,
469
+ 'url ' => 'https://magento.local/pub/media/wysiwyg/zero-bytes.jpg ' ,
470
+ 'mime_type ' => 'image/jpeg '
471
+ ]
472
+ )
473
+ ],
474
+ // Images files with incorrect not allowed extensions should generate proper warnings
475
+ [
476
+ 'timesWarningTriggered ' => 1 ,
477
+ 'thumbnailPath ' => Storage::THUMB_PLACEHOLDER_PATH_SUFFIX ,
478
+ 'imageItem ' =>
479
+ new DataObject (
480
+ [
481
+ 'mtime ' => 0 ,
482
+ 'size ' => 1024 ,
483
+ 'filename ' => '/webroot/pub/media/wysiwyg/wrong-image.exe ' ,
484
+ 'basename ' => 'wrong-image.exe ' ,
485
+ 'id ' => 1 ,
486
+ 'name ' => 'wrong-image.exe ' ,
487
+ 'short_name ' => 'wrong-image.exe ' ,
488
+ 'url ' => 'https://magento.local/pub/media/wysiwyg/wrong-image.exe ' ,
489
+ 'mime_type ' => 'image/jpeg '
490
+ ]
491
+ )
492
+ ],
493
+ // Images with non-zero size and allowed extension should not generate warnings
494
+ [
495
+ 'timesWarningTriggered ' => 0 ,
496
+ 'thumbnailPath ' => '' ,
497
+ 'imageItem ' =>
498
+ new DataObject (
499
+ [
500
+ 'mtime ' => 0 ,
501
+ 'size ' => 1024 ,
502
+ 'filename ' => '/webroot/pub/media/wysiwyg/image.jpg ' ,
503
+ 'basename ' => 'image.jpg ' ,
504
+ 'id ' => 1 ,
505
+ 'name ' => 'image.jpg ' ,
506
+ 'short_name ' => 'image.jpg ' ,
507
+ 'url ' => 'https://magento.local/pub/media/wysiwyg/image.jpg ' ,
508
+ 'mime_type ' => 'image/jpeg '
509
+ ]
510
+ )
511
+ ],
512
+ ];
513
+ }
514
+
371
515
/**
372
516
* @param $path
373
517
* @param $callNum
@@ -432,7 +576,7 @@ public static function dirsCollectionDataProvider()
432
576
protected function generalTestGetDirsCollection (string $ path , int $ callNum , string $ dirsFilter )
433
577
{
434
578
/** @var StorageCollection|MockObject $storageCollectionMock */
435
- $ storageCollectionMock = $ this ->getMockBuilder (\ Magento \ Cms \ Model \ Wysiwyg \ Images \ Storage \Collection ::class)
579
+ $ storageCollectionMock = $ this ->getMockBuilder (StorageCollection ::class)
436
580
->disableOriginalConstructor ()
437
581
->getMock ();
438
582
$ storageCollectionMock ->expects ($ this ->once ())
0 commit comments