Skip to content

Commit

Permalink
ENGCOM-9144: Fix a PHP error being thrown on a Magento error being th…
Browse files Browse the repository at this point in the history
…rown #32814
  • Loading branch information
sidolov committed Sep 16, 2021
2 parents 8300ada + 15aa0e3 commit 847ec86
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 5 deletions.
9 changes: 4 additions & 5 deletions app/code/Magento/MediaStorage/Model/File/Storage/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,15 @@ public function saveFile($file, $overwrite = true)
&& isset($file['content'])
&& !empty($file['content'])
) {
try {
$filename = isset(
$file['directory']
) && !empty($file['directory']) ? $file['directory'] . '/' . $file['filename'] : $file['filename'];
$directory = !empty($file['directory'] ?? '') ? $file['directory'] . '/' : '';
$filename = $directory . $file['filename'];

try {
return $this->_fileUtility->saveFile($filename, $file['content'], $overwrite);
} catch (\Exception $e) {
$this->_logger->critical($e);
throw new \Magento\Framework\Exception\LocalizedException(
__('Unable to save file "%1" at "%2"', $file['filename'], $file['directory'])
__('Unable to save file "%1" at "%2"', $file['filename'], $filename)
);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaStorage\Test\Unit\Model\File\Storage;

use Exception;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\MediaStorage\Helper\File\Media;
use Magento\MediaStorage\Helper\File\Storage\Database;
use Magento\MediaStorage\Model\File\Storage\File;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

/** Unit tests for \Magento\MediaStorage\Model\File\Storage\File class */
class FileTest extends TestCase
{
/**
* @var File
*/
private $file;

/**
* @var Media
*/
private $loggerMock;

/**
* @var Database
*/
private $storageHelperMock;

/**
* @var DateTime
*/
private $mediaHelperMock;

/**
* @var \Magento\MediaStorage\Model\ResourceModel\File\Storage\File
*/
private $fileUtilityMock;

protected function setUp(): void
{
$this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
$this->storageHelperMock = $this->createMock(Database::class);
$this->mediaHelperMock = $this->createMock(Media::class);
$this->fileUtilityMock = $this->createMock(\Magento\MediaStorage\Model\ResourceModel\File\Storage\File::class);

$this->file = new File(
$this->loggerMock,
$this->storageHelperMock,
$this->mediaHelperMock,
$this->fileUtilityMock
);
}

protected function tearDown(): void
{
unset($this->file);
}

public function testSaveFileWithWrongFileFormat(): void
{
$this->expectException(LocalizedException::class);
$this->expectExceptionMessage('Wrong file info format');
$this->file->saveFile([]);
}

public function testSaveFileUnsuccessfullyWithMissingDirectory(): void
{
$this->fileUtilityMock
->expects($this->once())
->method('saveFile')
->willThrowException(new Exception());

$this->expectException(LocalizedException::class);
$this->expectExceptionMessage('Unable to save file "filename.ext" at "filename.ext"');
$this->file->saveFile([
'filename' => 'filename.ext',
'content' => 'content',
]);
}

public function testSaveFileUnsuccessfullyWithoutMissingDirectory(): void
{
$this->fileUtilityMock
->expects($this->once())
->method('saveFile')
->willThrowException(new Exception());

$this->expectException(LocalizedException::class);
$this->expectExceptionMessage('Unable to save file "filename.ext" at "directory/filename.ext"');
$this->file->saveFile([
'directory' => 'directory',
'filename' => 'filename.ext',
'content' => 'content',
]);
}
}

0 comments on commit 847ec86

Please sign in to comment.