Skip to content

Commit db5fead

Browse files
committed
Merge branch 'MC-5054' of github.com:magento-obsessive-owls/magento2-page-builder into cms-team-1-delivery
2 parents 75a27de + 3d768e0 commit db5fead

File tree

7 files changed

+187
-2
lines changed

7 files changed

+187
-2
lines changed

app/code/Magento/PageBuilder/Controller/Adminhtml/ContentType/Image/Upload.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
*/
66
namespace Magento\PageBuilder\Controller\Adminhtml\ContentType\Image;
77

8-
use Magento\Framework\Controller\ResultFactory;
8+
use Magento\Framework\App\Action\HttpPostActionInterface;
99

1010
/**
1111
* Class Upload
1212
*/
13-
class Upload extends \Magento\Backend\App\Action
13+
class Upload extends \Magento\Backend\App\Action implements HttpPostActionInterface
1414
{
1515
const UPLOAD_DIR = 'wysiwyg';
1616

@@ -94,7 +94,12 @@ public function execute()
9494
$fileUploader->setAllowRenameFiles(true);
9595
$fileUploader->setAllowedExtensions(['jpeg','jpg','png','gif']);
9696
$fileUploader->setAllowCreateFolders(true);
97+
9798
try {
99+
if (!$fileUploader->checkMimeType(['image/png', 'image/jpeg', 'image/gif'])) {
100+
throw new \Magento\Framework\Exception\LocalizedException(__('File validation failed.'));
101+
}
102+
98103
$result = $fileUploader->save($this->getUploadDir());
99104
$baseUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
100105
$result['id'] = $this->cmsWysiwygImages->idEncode($result['file']);

app/code/Magento/PageBuilder/Test/Mftf/Data/BackgroundFormData.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@
218218
<data key="extension">sql</data>
219219
<data key="errorMessage">We don't recognize or support this file extension type.</data>
220220
</entity>
221+
<entity name="PageBuilderBackgroundImage_InvalidMimeType" type="pagebuilder_background_image_property">
222+
<data key="section">background</data>
223+
<data key="fieldName">background_image</data>
224+
<data key="value">not-a.png</data>
225+
<data key="errorMessage">File validation failed.</data>
226+
</entity>
221227
<!-- Background Mobile Image -->
222228
<entity name="PageBuilderBackgroundMobileImage_Default" type="pagebuilder_background_mobile_image_property">
223229
<data key="name">Background Image</data>

app/code/Magento/PageBuilder/Test/Mftf/Test/AdminPageBuilderBackgroundFormAttributeTests.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,26 @@
11481148
<argument name="backgroundImage" value="PageBuilderBackgroundImage_JPG"/>
11491149
</actionGroup>
11501150
</test>
1151+
<test name="BackgroundImageInvalidMimeType" extends="BackgroundImageInvalidFileSize">
1152+
<annotations>
1153+
<features value="PageBuilder"/>
1154+
<stories value="Background Form"/>
1155+
<title value="Background Image - Invalid Mime Type"/>
1156+
<description value="Tests that the Background Image attribute functions as expected for files with correct extension but invalid mime type"/>
1157+
<severity value="CRITICAL"/>
1158+
<useCaseId value="MC-5054"/>
1159+
<testCaseId value="MC-5071"/>
1160+
<group value="pagebuilder"/>
1161+
<group value="pagebuilder-banner"/>
1162+
<group value="pagebuilder-backgroundForm"/>
1163+
</annotations>
1164+
<actionGroup ref="attachInvalidFileOnSlideOut" stepKey="attachInvalidFileOnSlideOut">
1165+
<argument name="property" value="PageBuilderBackgroundImage_InvalidMimeType"/>
1166+
</actionGroup>
1167+
<actionGroup ref="attachInvalidFileOnSlideOut" stepKey="attachInvalidFileOnSlideOut2">
1168+
<argument name="property" value="PageBuilderBackgroundImage_InvalidMimeType"/>
1169+
</actionGroup>
1170+
</test>
11511171
<test name="BackgroundMobileImageJPG">
11521172
<annotations>
11531173
<features value="PageBuilder"/>
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\PageBuilder\Controller\Adminhtml\ContentType\Image;
10+
11+
use Magento\Framework\File\Mime;
12+
use Magento\PageBuilder\Controller\Adminhtml\ContentType\Image\Upload as Controller;
13+
14+
/**
15+
* Class UploadTest
16+
*/
17+
class UploadTest extends \PHPUnit\Framework\TestCase
18+
{
19+
/**
20+
* @var \Magento\PageBuilder\Controller\Adminhtml\ContentType\Image\Upload
21+
*/
22+
private $controller;
23+
24+
/**
25+
* @var \Magento\Framework\ObjectManagerInterface
26+
*/
27+
private $objectManager;
28+
29+
/**
30+
* @var \Magento\Framework\File\UploaderFactory|\PHPUnit_Framework_MockObject_MockObject
31+
*/
32+
private $uploaderFactory;
33+
34+
/**
35+
* @var \Magento\Framework\Controller\Result\Json|\PHPUnit_Framework_MockObject_MockObject
36+
*/
37+
private $resultJson;
38+
39+
/**
40+
* @var \Magento\Framework\Controller\Result\JsonFactory|\PHPUnit_Framework_MockObject_MockObject
41+
*/
42+
private $resultJsonFactory;
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
protected function setUp()
48+
{
49+
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
50+
51+
$this->uploaderFactory = $this->createPartialMock(\Magento\Framework\File\UploaderFactory::class, ['create']);
52+
53+
$this->resultJson = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
54+
->setMethods(['setData'])
55+
->disableOriginalConstructor()
56+
->getMock();
57+
58+
$this->resultJsonFactory = $this->getMockBuilder(\Magento\Framework\Controller\Result\JsonFactory::class)
59+
->setMethods(['create'])
60+
->disableOriginalConstructor()
61+
->getMock();
62+
63+
$this->resultJsonFactory->expects($this->once())->method('create')->willReturn($this->resultJson);
64+
65+
$this->controller = $this->objectManager->create(Controller::class, [
66+
'resultJsonFactory' => $this->resultJsonFactory,
67+
'uploaderFactory' => $this->uploaderFactory
68+
]);
69+
}
70+
71+
/**
72+
* @inheritdoc
73+
*/
74+
protected function tearDown()
75+
{
76+
$_FILES = [];
77+
}
78+
79+
/**
80+
* Assert that file validation passes when uploaded file has correct extension and valid mime type
81+
*/
82+
public function testFileValidationPassesWhenFileHasCorrectExtensionAndValidMimeType()
83+
{
84+
$valid_file_pathname = realpath(dirname(__FILE__) . '/../../../../_files/uploader/a.png');
85+
86+
$_FILES = [
87+
'background_image' => [
88+
'type' => 'image/png',
89+
'name' => basename($valid_file_pathname),
90+
'tmp_name' => $valid_file_pathname,
91+
'size' => filesize($valid_file_pathname),
92+
'error' => UPLOAD_ERR_OK,
93+
]
94+
];
95+
96+
$uploader = $this->objectManager->create(\Magento\Framework\File\Uploader::class, [
97+
'fileId' => 'background_image',
98+
'fileMime' => $this->objectManager->create(Mime::class),
99+
]);
100+
101+
$this->uploaderFactory
102+
->expects($this->once())
103+
->method('create')
104+
->will($this->returnValue($uploader));
105+
106+
$this->resultJson->expects($this->once())->method('setData')->willReturnCallback(function ($result) {
107+
$this->assertNotEquals([
108+
'error' => 'File validation failed.',
109+
'errorcode' => 0
110+
], $result);
111+
});
112+
113+
$this->controller->execute();
114+
}
115+
116+
/**
117+
* Assert that file validation fails when uploaded file has correct extension but invalid mime type
118+
*/
119+
public function testFileValidationFailsWhenFileHasCorrectExtensionButInvalidMimeType()
120+
{
121+
$invalid_file_pathname = realpath(dirname(__FILE__) . '/../../../../_files/uploader/not-a.png');
122+
123+
$_FILES = [
124+
'background_image' => [
125+
'type' => 'image/png',
126+
'name' => basename($invalid_file_pathname),
127+
'tmp_name' => $invalid_file_pathname,
128+
'size' => filesize($invalid_file_pathname),
129+
'error' => UPLOAD_ERR_OK,
130+
]
131+
];
132+
133+
$uploader = $this->objectManager->create(\Magento\Framework\File\Uploader::class, [
134+
'fileId' => 'background_image',
135+
'fileMime' => $this->objectManager->create(Mime::class),
136+
]);
137+
138+
$this->uploaderFactory
139+
->expects($this->once())
140+
->method('create')
141+
->will($this->returnValue($uploader));
142+
143+
$this->resultJson->expects($this->once())->method('setData')->willReturnCallback(function ($result) {
144+
$this->assertEquals([
145+
'error' => 'File validation failed.',
146+
'errorcode' => 0
147+
], $result);
148+
});
149+
150+
$this->controller->execute();
151+
}
152+
}
Loading
Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)