Skip to content

Commit 55e7b3c

Browse files
committed
ACP2E-1353: allow image/x-icon favicon
1 parent 8bda3ec commit 55e7b3c

File tree

5 files changed

+50
-19
lines changed

5 files changed

+50
-19
lines changed

app/code/Magento/MediaStorage/Model/File/Validator/Image.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,17 @@ public function __construct(
8787
public function isValid($filePath): bool
8888
{
8989
$fileMimeType = $this->fileMime->getMimeType($filePath);
90-
$isValid = false;
90+
$isValid = true;
9191

9292
if (stripos(json_encode($this->imageMimeTypes), json_encode($fileMimeType)) !== false) {
93-
$defaultAdapter = $this->config->getAdapterAlias();
9493
try {
95-
$image = $this->imageFactory->create($filePath, $defaultAdapter);
94+
$image = $this->imageFactory->create($filePath);
9695
$image->open();
97-
$isValid = true;
9896
} catch (\InvalidArgumentException $e) {
99-
$adapters = $this->config->getAdapters();
100-
unset($adapters[$defaultAdapter]);
101-
$image = $this->imageFactory->create($filePath, array_key_first($adapters) ?? null);
102-
$image->open();
103-
$isValid = true;
97+
if (stripos($fileMimeType, 'icon') !== false) {
98+
$image = $this->imageFactory->create($filePath, $this->getNonDefaultAdapter());
99+
$image->open();
100+
}
104101
} catch (\Exception $e) {
105102
$isValid = false;
106103
$this->logger->critical($e, ['exception' => $e]);
@@ -109,4 +106,17 @@ public function isValid($filePath): bool
109106

110107
return $isValid;
111108
}
109+
110+
/**
111+
* Get non default image adapter
112+
*
113+
* @return string|null
114+
*/
115+
private function getNonDefaultAdapter(): ?string
116+
{
117+
$defaultAdapter = $this->config->getAdapterAlias();
118+
$adapters = $this->config->getAdapters();
119+
unset($adapters[$defaultAdapter]);
120+
return array_key_first($adapters) ?? null;
121+
}
112122
}

app/code/Magento/MediaStorage/Test/Unit/Model/File/Validator/ImageTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,12 @@ public function testIsValid($filePath, $mimeType, $result): void
8383
public function dataProviderForIsValid()
8484
{
8585
return [
86-
'x-icon' => [dirname(__FILE__) . '/_files/favicon-x-icon.ico', 'image/x-icon', true],
87-
'vnd-microsoft-icon' => [dirname(__FILE__) . '/_files/favicon-vnd-microsoft.ico', 'image/vnd.microsoft.icon', true],
88-
'not-valid-ico' => [dirname(__FILE__) . '/_files/not-valid-file.ico', 'text/plain', false]
86+
'x-icon' => [dirname(__FILE__) . '/_files/favicon-x-icon.ico',
87+
'image/x-icon', true],
88+
'vnd-microsoft-icon' => [dirname(__FILE__) . '/_files/favicon-vnd-microsoft.ico',
89+
'image/vnd.microsoft.icon', true],
90+
'not-valid-ico' => [dirname(__FILE__) . '/_files/not-valid-file.ico',
91+
'text/plain', false]
8992
];
9093
}
9194
}

dev/tests/integration/testsuite/Magento/Theme/Controller/Adminhtml/System/Design/ThemeControllerTest.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
namespace Magento\Theme\Controller\Adminhtml\System\Design;
77

8-
use Magento\Framework\Filesystem;
8+
use Magento\Framework\Exception\FileSystemException;
99
use Magento\Framework\Filesystem\DirectoryList;
1010

1111
/**
@@ -16,7 +16,7 @@ class ThemeControllerTest extends \Magento\TestFramework\TestCase\AbstractBacken
1616
public function testUploadJsAction()
1717
{
1818
$name = 'simple-js-file.js';
19-
$this->createUploadFixture($name);
19+
$this->createUploadFixture($name, 'application/x-javascript', 'js_files_uploader');
2020
$theme = $this->_objectManager->create(\Magento\Framework\View\Design\ThemeInterface::class)
2121
->getCollection()
2222
->getFirstItem();
@@ -28,13 +28,31 @@ public function testUploadJsAction()
2828
$this->assertStringContainsString($name, $output);
2929
}
3030

31+
public function testUploadFaviconAction()
32+
{
33+
$names = ['favicon-x-icon.ico', 'favicon-vnd-microsoft.ico'];
34+
foreach ($names as $name) {
35+
$this->createUploadFixture($name, 'image/vnd.microsoft.icon', 'head_shortcut_icon');
36+
$theme = $this->_objectManager->create(\Magento\Framework\View\Design\ThemeInterface::class)
37+
->getCollection()
38+
->getFirstItem();
39+
$this->getRequest()->setPostValue('id', $theme->getId());
40+
$this->dispatch('backend/admin/design_config_fileUploader/save');
41+
$output = $this->getResponse()->getBody();
42+
$this->assertStringContainsString('"error":"false"', $output);
43+
$this->assertStringContainsString($name, $output);
44+
}
45+
}
46+
3147
/**
3248
* Creates a fixture for testing uploaded file
3349
*
3450
* @param string $name
51+
* @params string $mimeType
3552
* @return void
53+
* @throws FileSystemException
3654
*/
37-
private function createUploadFixture($name)
55+
private function createUploadFixture($name, $mimeType, $model)
3856
{
3957
/** @var \Magento\TestFramework\App\Filesystem $filesystem */
4058
$filesystem = $this->_objectManager->get(\Magento\Framework\Filesystem::class);
@@ -44,11 +62,11 @@ private function createUploadFixture($name)
4462
$target = $tmpDir->getAbsolutePath("{$subDir}/{$name}");
4563
copy(__DIR__ . "/_files/{$name}", $target);
4664
$_FILES = [
47-
'js_files_uploader' => [
48-
'name' => 'simple-js-file.js',
49-
'type' => 'application/x-javascript',
65+
$model => [
66+
'name' => $name,
67+
'type' => $mimeType,
5068
'tmp_name' => $target,
51-
'error' => '0',
69+
'error' => 'false',
5270
'size' => '28',
5371
],
5472
];

0 commit comments

Comments
 (0)