Skip to content

Commit 9a8d8d5

Browse files
committed
Merge branch 'ACP2E-1353' of https://github.com/magento-l3/magento2ce into PR-L3-2023-01-31
2 parents 896f4a2 + 298582c commit 9a8d8d5

File tree

7 files changed

+148
-10
lines changed

7 files changed

+148
-10
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Image extends AbstractValidator
2727
'jpg' => 'image/jpeg',
2828
'gif' => 'image/gif',
2929
'bmp' => 'image/bmp',
30-
'ico' => 'image/vnd.microsoft.icon',
30+
'ico' => ['image/vnd.microsoft.icon', 'image/x-icon']
3131
];
3232

3333
/**
@@ -70,7 +70,7 @@ public function isValid($filePath): bool
7070
$fileMimeType = $this->fileMime->getMimeType($filePath);
7171
$isValid = true;
7272

73-
if (in_array($fileMimeType, $this->imageMimeTypes)) {
73+
if (stripos(json_encode($this->imageMimeTypes), json_encode($fileMimeType)) !== false) {
7474
try {
7575
$image = $this->imageFactory->create($filePath);
7676
$image->open();
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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\Validator;
9+
10+
use Magento\Framework\File\Mime;
11+
use Magento\Framework\Filesystem\Driver\File;
12+
use Magento\Framework\Image as FrameworkImage;
13+
use Magento\Framework\Image\Factory;
14+
use Magento\MediaStorage\Model\File\Validator\Image;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/** Unit tests for \Magento\MediaStorage\Model\File\Validator\Image class */
19+
class ImageTest extends TestCase
20+
{
21+
/**
22+
* @var Mime|MockObject
23+
*/
24+
private $fileMimeMock;
25+
26+
/**
27+
* @var Factory|MockObject
28+
*/
29+
private $imageFactoryMock;
30+
31+
/**
32+
* @var FrameworkImage|MockObject
33+
*/
34+
private $imageMock;
35+
36+
/**
37+
* @var File|MockObject
38+
*/
39+
private $fileMock;
40+
41+
/**
42+
* @var Image
43+
*/
44+
private $image;
45+
46+
protected function setUp(): void
47+
{
48+
$this->fileMimeMock = $this->createMock(Mime::class);
49+
$this->imageFactoryMock = $this->createMock(Factory::class);
50+
$this->fileMock = $this->createMock(File::class);
51+
$this->imageMock = $this->createMock(FrameworkImage::class);
52+
53+
$this->image = new Image(
54+
$this->fileMimeMock,
55+
$this->imageFactoryMock,
56+
$this->fileMock
57+
);
58+
}
59+
60+
/**
61+
* @dataProvider dataProviderForIsValid
62+
*/
63+
public function testIsValid($filePath, $mimeType, $result): void
64+
{
65+
$this->fileMimeMock->expects($this->once())
66+
->method('getMimeType')
67+
->with($filePath)
68+
->willReturn($mimeType);
69+
$this->imageMock->expects($this->once())
70+
->method('open')
71+
->willReturn(null);
72+
$this->imageFactoryMock->expects($this->once())
73+
->method('create')
74+
->willReturn($this->imageMock);
75+
$this->assertEquals($result, $this->image->isValid($filePath));
76+
}
77+
78+
/**
79+
* @return array[]
80+
*/
81+
public function dataProviderForIsValid()
82+
{
83+
return [
84+
'x-icon' => [dirname(__FILE__) . '/_files/favicon-x-icon.ico',
85+
'image/x-icon', true],
86+
'vnd-microsoft-icon' => [dirname(__FILE__) . '/_files/favicon-vnd-microsoft.ico',
87+
'image/vnd.microsoft.icon', true]
88+
];
89+
}
90+
}

app/code/Magento/Theme/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ Configuration,Configuration
153153
"Other Settings","Other Settings"
154154
"HTML Head","HTML Head"
155155
"Allowed file types: ico, png, gif, jpg, jpeg, apng. Not all browsers support all these formats!","Allowed file types: ico, png, gif, jpg, jpeg, apng. Not all browsers support all these formats!"
156+
"Not all browsers support all these formats! Note: ICO file type is supported by ImageMagik adapter that can be set from Store / Configuration / Developer / Image Processing Settings.","Not all browsers support all these formats! Note: ICO file type is supported by ImageMagik adapter that can be set from Store / Configuration / Developer / Image Processing Settings."
156157
"Favicon Icon","Favicon Icon"
157158
"Default Page Title","Default Page Title"
158159
"Page Title Prefix","Page Title Prefix"

app/code/Magento/Theme/view/adminhtml/ui_component/design_config_form.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
</settings>
5757
<field name="head_shortcut_icon" formElement="imageUploader">
5858
<settings>
59-
<notice translate="true">Not all browsers support all these formats!</notice>
59+
<notice translate="true">Not all browsers support all these formats! Note: ICO file type is supported by ImageMagik adapter that can be set from Store / Configuration / Developer / Image Processing Settings.</notice>
6060
<label translate="true">Favicon Icon</label>
6161
<componentType>imageUploader</componentType>
6262
</settings>

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

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

8-
use Magento\Framework\Filesystem;
8+
use Magento\Framework\App\Config\ScopeConfigInterface;
9+
use Magento\Framework\Exception\FileSystemException;
910
use Magento\Framework\Filesystem\DirectoryList;
1011

1112
/**
1213
* @magentoAppArea adminhtml
1314
*/
1415
class ThemeControllerTest extends \Magento\TestFramework\TestCase\AbstractBackendController
1516
{
17+
/**
18+
* @var ScopeConfigInterface|mixed
19+
*/
20+
private $config;
21+
22+
/**
23+
* @var string
24+
*/
25+
private $imageAdapter;
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
protected function setUp(): void
31+
{
32+
parent::setUp();
33+
34+
$this->config = $this->_objectManager->get(ScopeConfigInterface::class);
35+
$this->imageAdapter = $this->config->getValue('dev/image/default_adapter');
36+
}
37+
1638
public function testUploadJsAction()
1739
{
1840
$name = 'simple-js-file.js';
19-
$this->createUploadFixture($name);
41+
$this->createUploadFixture($name, 'application/x-javascript', 'js_files_uploader');
2042
$theme = $this->_objectManager->create(\Magento\Framework\View\Design\ThemeInterface::class)
2143
->getCollection()
2244
->getFirstItem();
@@ -28,13 +50,38 @@ public function testUploadJsAction()
2850
$this->assertStringContainsString($name, $output);
2951
}
3052

53+
public function testUploadFaviconAction()
54+
{
55+
$names = ['favicon-x-icon.ico', 'favicon-vnd-microsoft.ico'];
56+
foreach ($names as $name) {
57+
$this->createUploadFixture($name, 'image/vnd.microsoft.icon', 'head_shortcut_icon');
58+
$theme = $this->_objectManager->create(\Magento\Framework\View\Design\ThemeInterface::class)
59+
->getCollection()
60+
->getFirstItem();
61+
$this->getRequest()->setPostValue('id', $theme->getId());
62+
$this->dispatch('backend/admin/design_config_fileUploader/save');
63+
$output = $this->getResponse()->getBody();
64+
if (!in_array('imagick', get_loaded_extensions()) || $this->imageAdapter == 'GD2') {
65+
$this->assertStringContainsString(
66+
'{"error":"File validation failed."',
67+
$output
68+
);
69+
} else {
70+
$this->assertStringContainsString('"error":"false"', $output);
71+
$this->assertStringContainsString($name, $output);
72+
}
73+
}
74+
}
75+
3176
/**
3277
* Creates a fixture for testing uploaded file
3378
*
3479
* @param string $name
80+
* @params string $mimeType
3581
* @return void
82+
* @throws FileSystemException
3683
*/
37-
private function createUploadFixture($name)
84+
private function createUploadFixture($name, $mimeType, $model)
3885
{
3986
/** @var \Magento\TestFramework\App\Filesystem $filesystem */
4087
$filesystem = $this->_objectManager->get(\Magento\Framework\Filesystem::class);
@@ -44,11 +91,11 @@ private function createUploadFixture($name)
4491
$target = $tmpDir->getAbsolutePath("{$subDir}/{$name}");
4592
copy(__DIR__ . "/_files/{$name}", $target);
4693
$_FILES = [
47-
'js_files_uploader' => [
48-
'name' => 'simple-js-file.js',
49-
'type' => 'application/x-javascript',
94+
$model => [
95+
'name' => $name,
96+
'type' => $mimeType,
5097
'tmp_name' => $target,
51-
'error' => '0',
98+
'error' => 'false',
5299
'size' => '28',
53100
],
54101
];

0 commit comments

Comments
 (0)