Skip to content

Commit

Permalink
ACP2E-3127: imagecreatetruecolor(): Argument #2 () must be greater th…
Browse files Browse the repository at this point in the history
…an 0. Can't upload specific image
  • Loading branch information
Chhandak.Barua authored and Chhandak.Barua committed Jul 25, 2024
1 parent 53080dc commit fd31d92
Showing 1 changed file with 93 additions and 27 deletions.
120 changes: 93 additions & 27 deletions app/code/Magento/Cms/Test/Unit/Model/Wysiwyg/Images/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,6 @@ function ($path) {
self::STORAGE_ROOT_DIR
);

$this->resizeParameters = ['width' => 100, 'height' => 50];

$this->storageCollectionFactoryMock = $this->createPartialMock(
CollectionFactory::class,
['create']
Expand Down Expand Up @@ -296,31 +294,12 @@ function ($path) {

$this->imagesStorage = $this->objectManagerHelper->getObject(
Storage::class,
[
'session' => $this->sessionMock,
'backendUrl' => $this->backendUrlMock,
'cmsWysiwygImages' => $this->imageHelperMock,
'coreFileStorageDb' => $this->coreFileStorageMock,
'filesystem' => $this->filesystemMock,
'imageFactory' => $this->adapterFactoryMock,
'assetRepo' => $this->assetRepo,
'storageCollectionFactory' => $this->storageCollectionFactoryMock,
'storageFileFactory' => $this->storageFileFactoryMock,
'storageDatabaseFactory' => $this->storageDatabaseFactoryMock,
'directoryDatabaseFactory' => $this->directoryDatabaseFactoryMock,
'uploaderFactory' => $this->uploaderFactoryMock,
'resizeParameters' => $this->resizeParameters,
'extensions' => $allowedExtensions,
'dirs' => [
'exclude' => [],
'include' => [],
],
'data' => [],
'file' => $this->fileMock,
'ioFile' => $this->ioFileMock,
'coreConfig' => $this->coreConfigMock,
'logger' => $this->loggerMock
]
$this->getStorageClass(100, 50, $allowedExtensions)
);

$this->imagesStorageWithZeroHeight = $this->objectManagerHelper->getObject(
Storage::class,
$this->getStorageClass(100, 0, $allowedExtensions)
);
}

Expand Down Expand Up @@ -696,4 +675,91 @@ public function testCreateDirectoryWithInvalidName()
);
$this->imagesStorage->createDirectory($name, $path);
}

public function testResizeFileWithZeroHeight()
{
$path = 'target/path';
$source = self::STORAGE_ROOT_DIR . $path;
$fileName = 'image.jpg';
$realPath = $source . '/' . $fileName;
$thumbnailTargetPath = self::STORAGE_ROOT_DIR . '.thumbs' . $path;
$thumbnailDestination = $thumbnailTargetPath . '/' . $fileName;
$result = false;
$exceptionMessage = 'Exception message';
$this->directoryMock->expects($this->atLeastOnce())->method('getRelativePath')->willReturnMap(
[
[$realPath, $realPath],
[$thumbnailTargetPath, $thumbnailTargetPath],
[$thumbnailDestination, $thumbnailDestination],
]
);
$this->directoryMock->expects($this->atLeastOnce())->method('isFile')
->willReturnMap(
[
[$realPath, true],
[$thumbnailDestination, true],
]
);
$this->directoryMock->expects($this->atLeastOnce())->method('isExist')
->willReturnMap(
[
[$realPath, true],
[$thumbnailTargetPath, true],
]
);
$this->driverMock->expects(self::once())
->method('fileGetContents')
->willReturn('some content');


$image = $this->getMockBuilder(image::class)
->disableOriginalConstructor()
->addMethods(['open', 'keepAspectRatio'])
->onlyMethods(['resize', 'save'])
->getMock();
$image->expects($this->any())->method('open')->with($realPath);
$image->expects($this->any())->method('keepAspectRatio')->with(true);
$image->expects($this->once())->method('resize')->with(100, 0)
->willThrowException(new \LogicException($exceptionMessage));


$this->adapterFactoryMock->expects($this->atLeastOnce())->method('create')->willReturn($image);

// Logger should not log any critical errors in this scenario
$this->loggerMock->expects($this->once())
->method('critical');

$this->assertEquals($result, $this->imagesStorageWithZeroHeight->resizeFile($realPath));
}

public function getStorageClass($width, $height, $allowedExtensions)
{
$this->resizeParameters = ['width' => $width, 'height' => $height];
return
[
'session' => $this->sessionMock,
'backendUrl' => $this->backendUrlMock,
'cmsWysiwygImages' => $this->imageHelperMock,
'coreFileStorageDb' => $this->coreFileStorageMock,
'filesystem' => $this->filesystemMock,
'imageFactory' => $this->adapterFactoryMock,
'assetRepo' => $this->assetRepo,
'storageCollectionFactory' => $this->storageCollectionFactoryMock,
'storageFileFactory' => $this->storageFileFactoryMock,
'storageDatabaseFactory' => $this->storageDatabaseFactoryMock,
'directoryDatabaseFactory' => $this->directoryDatabaseFactoryMock,
'uploaderFactory' => $this->uploaderFactoryMock,
'resizeParameters' => $this->resizeParameters,
'extensions' => $allowedExtensions,
'dirs' => [
'exclude' => [],
'include' => [],
],
'data' => [],
'file' => $this->fileMock,
'ioFile' => $this->ioFileMock,
'coreConfig' => $this->coreConfigMock,
'logger' => $this->loggerMock
];
}
}

0 comments on commit fd31d92

Please sign in to comment.