Skip to content

Commit 30ee4ce

Browse files
committed
ACP2E-1353: allow image/x-icon favicon
1 parent 2283572 commit 30ee4ce

File tree

5 files changed

+95
-3
lines changed

5 files changed

+95
-3
lines changed

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

Lines changed: 4 additions & 3 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', 'image/x-icon']
30+
'ico' => [ 'image/vnd.microsoft.icon', 'image/x-icon']
3131
];
3232

3333
/**
@@ -68,12 +68,13 @@ public function __construct(
6868
public function isValid($filePath): bool
6969
{
7070
$fileMimeType = $this->fileMime->getMimeType($filePath);
71-
$isValid = true;
71+
$isValid = false;
7272

73-
if (in_array($fileMimeType, $this->imageMimeTypes)) {
73+
if (stripos(serialize($this->imageMimeTypes), $fileMimeType) !== false) {
7474
try {
7575
$image = $this->imageFactory->create($filePath);
7676
$image->open();
77+
$isValid = true;
7778
} catch (\Exception $e) {
7879
$isValid = false;
7980
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
if ($result) {
70+
$this->imageMock->expects($this->once())
71+
->method('open')
72+
->willReturn(null);
73+
$this->imageFactoryMock->expects($this->once())
74+
->method('create')
75+
->willReturn($this->imageMock);
76+
}
77+
$this->assertEquals($result, $this->image->isValid($filePath));
78+
}
79+
80+
/**
81+
* @return array[]
82+
*/
83+
public function dataProviderForIsValid()
84+
{
85+
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]
89+
];
90+
}
91+
}
Binary file not shown.

app/code/Magento/MediaStorage/Test/Unit/Model/File/Validator/_files/not-valid-file.ico

Whitespace-only changes.

0 commit comments

Comments
 (0)