Skip to content

Commit 9c6c2b3

Browse files
committed
MC-5054: User Can Bypass File Type Validation In PageBuilder's File Upload Spots
Add UploadTest file validation test coverage
1 parent 0b8dc23 commit 9c6c2b3

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\PageBuilder\Test\Unit\Controller\Adminhtml\ContentType\Image;
7+
8+
use Magento\Framework\File\Mime;
9+
use Magento\PageBuilder\Controller\Adminhtml\ContentType\Image\Upload as Controller;
10+
11+
/**
12+
* Class UploadTest
13+
*/
14+
class UploadTest extends \PHPUnit\Framework\TestCase
15+
{
16+
/**
17+
* Subject under test
18+
* @var \Magento\PageBuilder\Controller\Adminhtml\ContentType\Image\Upload
19+
*/
20+
private $controller;
21+
22+
/**
23+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
24+
*/
25+
private $objectManager;
26+
27+
/**
28+
* @var \Magento\Framework\File\UploaderFactory|\PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
private $uploaderFactory;
31+
32+
/**
33+
* @var \Magento\Framework\Controller\Result\Json|\PHPUnit_Framework_MockObject_MockObject
34+
*/
35+
private $resultJson;
36+
37+
/**
38+
* @var \Magento\Framework\Controller\Result\JsonFactory|\PHPUnit_Framework_MockObject_MockObject
39+
*/
40+
private $resultJsonFactory;
41+
42+
protected function setUp()
43+
{
44+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
45+
46+
$this->uploaderFactory = $this->createPartialMock(\Magento\Framework\File\UploaderFactory::class, ['create']);
47+
48+
$this->resultJson = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
49+
->setMethods(['setData'])
50+
->disableOriginalConstructor()
51+
->getMock();
52+
53+
$this->resultJsonFactory = $this->getMockBuilder(\Magento\Framework\Controller\Result\JsonFactory::class)
54+
->setMethods(['create'])
55+
->disableOriginalConstructor()
56+
->getMock();
57+
58+
$this->resultJsonFactory->expects($this->once())->method('create')->willReturn($this->resultJson);
59+
60+
$this->controller = $this->objectManager->getObject(Controller::class, [
61+
'resultJsonFactory' => $this->resultJsonFactory,
62+
'uploaderFactory' => $this->uploaderFactory
63+
]);
64+
}
65+
66+
public function testFileValidationPassesWhenFileHasCorrectExtensionAndValidMimeType()
67+
{
68+
$valid_file_pathname = realpath(dirname(__FILE__) . '/../../../../_files/a.png');
69+
70+
$_FILES = [
71+
'background_image' => [
72+
'type' => 'image/png',
73+
'name' => basename($valid_file_pathname),
74+
'tmp_name' => $valid_file_pathname,
75+
'size' => filesize($valid_file_pathname),
76+
'error' => UPLOAD_ERR_OK,
77+
]
78+
];
79+
80+
$uploader = $this->objectManager->getObject(\Magento\Framework\File\Uploader::class, [
81+
'fileId' => 'background_image',
82+
'fileMime' => $this->objectManager->getObject(Mime::class),
83+
]);
84+
85+
$this->uploaderFactory
86+
->expects($this->once())
87+
->method('create')
88+
->will($this->returnValue($uploader));
89+
90+
$this->resultJson->expects($this->once())->method('setData')->willReturnCallback(function ($result) {
91+
$this->assertNotEquals([
92+
'error' => 'File validation failed.',
93+
'errorcode' => 0
94+
], $result);
95+
});
96+
97+
$this->controller->execute();
98+
}
99+
100+
public function testFileValidationFailsWhenFileHasCorrectExtensionButInvalidMimeType()
101+
{
102+
$invalid_file_pathname = realpath(dirname(__FILE__) . '/../../../../_files/not-a.png');
103+
104+
$_FILES = [
105+
'background_image' => [
106+
'type' => 'image/png',
107+
'name' => basename($invalid_file_pathname),
108+
'tmp_name' => $invalid_file_pathname,
109+
'size' => filesize($invalid_file_pathname),
110+
'error' => UPLOAD_ERR_OK,
111+
]
112+
];
113+
114+
$uploader = $this->objectManager->getObject(\Magento\Framework\File\Uploader::class, [
115+
'fileId' => 'background_image',
116+
'fileMime' => $this->objectManager->getObject(Mime::class),
117+
]);
118+
119+
$this->uploaderFactory
120+
->expects($this->once())
121+
->method('create')
122+
->will($this->returnValue($uploader));
123+
124+
$this->resultJson->expects($this->once())->method('setData')->willReturnCallback(function ($result) {
125+
$this->assertEquals([
126+
'error' => 'File validation failed.',
127+
'errorcode' => 0
128+
], $result);
129+
});
130+
131+
$this->controller->execute();
132+
}
133+
}
Loading
Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)