Skip to content

Commit ea10b80

Browse files
committed
ACP2E-2970: Old media gallery fails to render images when a 0-byte image is placed in the directory
1 parent 1904a1f commit ea10b80

File tree

1 file changed

+148
-4
lines changed

1 file changed

+148
-4
lines changed

app/code/Magento/Cms/Test/Unit/Model/Wysiwyg/Images/StorageTest.php

Lines changed: 148 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Magento\Cms\Model\Wysiwyg\Images\Storage\CollectionFactory;
1717
use Magento\Framework\App\Config\ScopeConfigInterface;
1818
use Magento\Framework\App\Filesystem\DirectoryList;
19+
use Magento\Framework\DataObject;
20+
use Magento\Framework\Exception\FileSystemException;
1921
use Magento\Framework\Exception\LocalizedException;
2022
use Magento\Framework\Filesystem;
2123
use Magento\Framework\Filesystem\Directory\Write;
@@ -31,6 +33,7 @@
3133
use Magento\MediaStorage\Model\File\UploaderFactory;
3234
use PHPUnit\Framework\MockObject\MockObject;
3335
use PHPUnit\Framework\TestCase;
36+
use Psr\Log\LoggerInterface;
3437

3538
/**
3639
* @SuppressWarnings(PHPMD.LongVariable)
@@ -141,6 +144,16 @@ class StorageTest extends TestCase
141144
*/
142145
private $fileMock;
143146

147+
/**
148+
* @var LoggerInterface|MockObject
149+
*/
150+
private $loggerMock;
151+
152+
/**
153+
* @var Repository|MockObject
154+
*/
155+
private $assetRepo;
156+
144157
/**
145158
* @var array
146159
*/
@@ -206,7 +219,7 @@ function ($path) {
206219
$this->adapterFactoryMock = $this->createMock(AdapterFactory::class);
207220
$this->imageHelperMock = $this->createPartialMock(
208221
Images::class,
209-
['getStorageRoot', 'getCurrentPath']
222+
['getStorageRoot', 'getCurrentPath', 'getCurrentUrl']
210223
);
211224
$this->imageHelperMock->expects(
212225
$this->any()
@@ -234,6 +247,12 @@ function ($path) {
234247
Database::class
235248
);
236249

250+
$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
251+
->disableOriginalConstructor()
252+
->getMock();
253+
254+
$this->assetRepo = $this->createMock(Repository::class);
255+
237256
$this->uploaderFactoryMock = $this->getMockBuilder(UploaderFactory::class)
238257
->disableOriginalConstructor()
239258
->getMock();
@@ -284,7 +303,7 @@ function ($path) {
284303
'coreFileStorageDb' => $this->coreFileStorageMock,
285304
'filesystem' => $this->filesystemMock,
286305
'imageFactory' => $this->adapterFactoryMock,
287-
'assetRepo' => $this->createMock(Repository::class),
306+
'assetRepo' => $this->assetRepo,
288307
'storageCollectionFactory' => $this->storageCollectionFactoryMock,
289308
'storageFileFactory' => $this->storageFileFactoryMock,
290309
'storageDatabaseFactory' => $this->storageDatabaseFactoryMock,
@@ -299,7 +318,8 @@ function ($path) {
299318
'data' => [],
300319
'file' => $this->fileMock,
301320
'ioFile' => $this->ioFileMock,
302-
'coreConfig' => $this->coreConfigMock
321+
'coreConfig' => $this->coreConfigMock,
322+
'logger' => $this->loggerMock
303323
]
304324
);
305325
}
@@ -368,6 +388,130 @@ public function testGetDirsCollectionCreateSubDirectories()
368388
);
369389
}
370390

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+
371515
/**
372516
* @param $path
373517
* @param $callNum
@@ -432,7 +576,7 @@ public static function dirsCollectionDataProvider()
432576
protected function generalTestGetDirsCollection(string $path, int $callNum, string $dirsFilter)
433577
{
434578
/** @var StorageCollection|MockObject $storageCollectionMock */
435-
$storageCollectionMock = $this->getMockBuilder(\Magento\Cms\Model\Wysiwyg\Images\Storage\Collection::class)
579+
$storageCollectionMock = $this->getMockBuilder(StorageCollection::class)
436580
->disableOriginalConstructor()
437581
->getMock();
438582
$storageCollectionMock->expects($this->once())

0 commit comments

Comments
 (0)