Skip to content

Commit 847ec86

Browse files
authored
ENGCOM-9144: Fix a PHP error being thrown on a Magento error being thrown #32814
2 parents 8300ada + 15aa0e3 commit 847ec86

File tree

2 files changed

+108
-5
lines changed

2 files changed

+108
-5
lines changed

app/code/Magento/MediaStorage/Model/File/Storage/File.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,16 +296,15 @@ public function saveFile($file, $overwrite = true)
296296
&& isset($file['content'])
297297
&& !empty($file['content'])
298298
) {
299-
try {
300-
$filename = isset(
301-
$file['directory']
302-
) && !empty($file['directory']) ? $file['directory'] . '/' . $file['filename'] : $file['filename'];
299+
$directory = !empty($file['directory'] ?? '') ? $file['directory'] . '/' : '';
300+
$filename = $directory . $file['filename'];
303301

302+
try {
304303
return $this->_fileUtility->saveFile($filename, $file['content'], $overwrite);
305304
} catch (\Exception $e) {
306305
$this->_logger->critical($e);
307306
throw new \Magento\Framework\Exception\LocalizedException(
308-
__('Unable to save file "%1" at "%2"', $file['filename'], $file['directory'])
307+
__('Unable to save file "%1" at "%2"', $file['filename'], $filename)
309308
);
310309
}
311310
} else {
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaStorage\Test\Unit\Model\File\Storage;
9+
10+
use Exception;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\Stdlib\DateTime\DateTime;
13+
use Magento\MediaStorage\Helper\File\Media;
14+
use Magento\MediaStorage\Helper\File\Storage\Database;
15+
use Magento\MediaStorage\Model\File\Storage\File;
16+
use PHPUnit\Framework\TestCase;
17+
use Psr\Log\LoggerInterface;
18+
19+
/** Unit tests for \Magento\MediaStorage\Model\File\Storage\File class */
20+
class FileTest extends TestCase
21+
{
22+
/**
23+
* @var File
24+
*/
25+
private $file;
26+
27+
/**
28+
* @var Media
29+
*/
30+
private $loggerMock;
31+
32+
/**
33+
* @var Database
34+
*/
35+
private $storageHelperMock;
36+
37+
/**
38+
* @var DateTime
39+
*/
40+
private $mediaHelperMock;
41+
42+
/**
43+
* @var \Magento\MediaStorage\Model\ResourceModel\File\Storage\File
44+
*/
45+
private $fileUtilityMock;
46+
47+
protected function setUp(): void
48+
{
49+
$this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
50+
$this->storageHelperMock = $this->createMock(Database::class);
51+
$this->mediaHelperMock = $this->createMock(Media::class);
52+
$this->fileUtilityMock = $this->createMock(\Magento\MediaStorage\Model\ResourceModel\File\Storage\File::class);
53+
54+
$this->file = new File(
55+
$this->loggerMock,
56+
$this->storageHelperMock,
57+
$this->mediaHelperMock,
58+
$this->fileUtilityMock
59+
);
60+
}
61+
62+
protected function tearDown(): void
63+
{
64+
unset($this->file);
65+
}
66+
67+
public function testSaveFileWithWrongFileFormat(): void
68+
{
69+
$this->expectException(LocalizedException::class);
70+
$this->expectExceptionMessage('Wrong file info format');
71+
$this->file->saveFile([]);
72+
}
73+
74+
public function testSaveFileUnsuccessfullyWithMissingDirectory(): void
75+
{
76+
$this->fileUtilityMock
77+
->expects($this->once())
78+
->method('saveFile')
79+
->willThrowException(new Exception());
80+
81+
$this->expectException(LocalizedException::class);
82+
$this->expectExceptionMessage('Unable to save file "filename.ext" at "filename.ext"');
83+
$this->file->saveFile([
84+
'filename' => 'filename.ext',
85+
'content' => 'content',
86+
]);
87+
}
88+
89+
public function testSaveFileUnsuccessfullyWithoutMissingDirectory(): void
90+
{
91+
$this->fileUtilityMock
92+
->expects($this->once())
93+
->method('saveFile')
94+
->willThrowException(new Exception());
95+
96+
$this->expectException(LocalizedException::class);
97+
$this->expectExceptionMessage('Unable to save file "filename.ext" at "directory/filename.ext"');
98+
$this->file->saveFile([
99+
'directory' => 'directory',
100+
'filename' => 'filename.ext',
101+
'content' => 'content',
102+
]);
103+
}
104+
}

0 commit comments

Comments
 (0)