Skip to content

Commit b7167fc

Browse files
committed
Fix integration tests for Page Builder
1 parent 1b05a1b commit b7167fc

File tree

1 file changed

+75
-57
lines changed
  • dev/tests/integration/testsuite/Magento/PageBuilder/Controller/Adminhtml/ContentType/Image

1 file changed

+75
-57
lines changed

dev/tests/integration/testsuite/Magento/PageBuilder/Controller/Adminhtml/ContentType/Image/UploadTest.php

Lines changed: 75 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,48 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
declare(strict_types=1);
87

98
namespace Magento\PageBuilder\Controller\Adminhtml\ContentType\Image;
109

10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\Controller\Result\Json;
12+
use Magento\Framework\Controller\Result\JsonFactory;
1113
use Magento\Framework\File\Mime;
12-
use Magento\PageBuilder\Controller\Adminhtml\ContentType\Image\Upload as Controller;
14+
use Magento\Framework\File\Uploader;
15+
use Magento\Framework\File\UploaderFactory;
16+
use Magento\Framework\ObjectManagerInterface;
17+
use Magento\PageBuilder\Controller\Adminhtml\ContentType\Image\Upload as UploadController;
18+
use PHPUnit\Framework\MockObject\MockObject;
19+
use PHPUnit\Framework\TestCase;
1320

1421
/**
1522
* Class UploadTest
1623
*/
17-
class UploadTest extends \PHPUnit\Framework\TestCase
24+
class UploadTest extends TestCase
1825
{
1926
/**
20-
* @var \Magento\PageBuilder\Controller\Adminhtml\ContentType\Image\Upload
27+
* @var UploadController
2128
*/
2229
private $controller;
2330

2431
/**
25-
* @var \Magento\Framework\ObjectManagerInterface
32+
* @var ObjectManagerInterface
2633
*/
2734
private $objectManager;
2835

2936
/**
30-
* @var \Magento\Framework\File\UploaderFactory|\PHPUnit_Framework_MockObject_MockObject
37+
* @var UploaderFactory|MockObject
3138
*/
3239
private $uploaderFactory;
3340

3441
/**
35-
* @var \Magento\Framework\Controller\Result\Json|\PHPUnit_Framework_MockObject_MockObject
42+
* @var Json|MockObject
3643
*/
3744
private $resultJson;
3845

3946
/**
40-
* @var \Magento\Framework\Controller\Result\JsonFactory|\PHPUnit_Framework_MockObject_MockObject
47+
* @var JsonFactory|MockObject
4148
*/
4249
private $resultJsonFactory;
4350

@@ -46,23 +53,25 @@ class UploadTest extends \PHPUnit\Framework\TestCase
4653
*/
4754
protected function setUp()
4855
{
49-
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
56+
$this->objectManager = ObjectManager::getInstance();
5057

51-
$this->uploaderFactory = $this->createPartialMock(\Magento\Framework\File\UploaderFactory::class, ['create']);
58+
$this->uploaderFactory = $this->createPartialMock(UploaderFactory::class, ['create']);
5259

53-
$this->resultJson = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
60+
$this->resultJson = $this->getMockBuilder(Json::class)
5461
->setMethods(['setData'])
5562
->disableOriginalConstructor()
5663
->getMock();
5764

58-
$this->resultJsonFactory = $this->getMockBuilder(\Magento\Framework\Controller\Result\JsonFactory::class)
65+
$this->resultJsonFactory = $this->getMockBuilder(JsonFactory::class)
5966
->setMethods(['create'])
6067
->disableOriginalConstructor()
6168
->getMock();
6269

63-
$this->resultJsonFactory->expects($this->once())->method('create')->willReturn($this->resultJson);
70+
$this->resultJsonFactory->expects($this->once())
71+
->method('create')
72+
->willReturn($this->resultJson);
6473

65-
$this->controller = $this->objectManager->create(Controller::class, [
74+
$this->controller = $this->objectManager->create(UploadController::class, [
6675
'resultJsonFactory' => $this->resultJsonFactory,
6776
'uploaderFactory' => $this->uploaderFactory
6877
]);
@@ -78,33 +87,41 @@ protected function tearDown()
7887

7988
/**
8089
* Assert that file validation passes when uploaded file has correct extension and valid mime type
90+
* @magentoAppArea adminhtml
8191
*/
8292
public function testFileValidationPassesWhenFileHasCorrectExtensionAndValidMimeType()
8393
{
84-
$valid_file_pathname = realpath(dirname(__FILE__) . '/../../../../_files/uploader/a.png');
94+
$valid_file_pathname = realpath(__DIR__ . '/../../../../_files/uploader/a.png');
8595

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-
];
96+
$this->setFilesGlobalMock($valid_file_pathname);
97+
$this->setUploaderMockForField('background_image');
9598

96-
$uploader = $this->objectManager->create(\Magento\Framework\File\Uploader::class, [
97-
'fileId' => 'background_image',
98-
'fileMime' => $this->objectManager->create(Mime::class),
99-
]);
99+
$this->resultJson->expects($this->once())
100+
->method('setData')
101+
->willReturnCallback(function ($result) {
102+
$this->assertNotEquals([
103+
'error' => 'File validation failed.',
104+
'errorcode' => 0
105+
], $result);
106+
});
107+
108+
$this->controller->execute();
109+
}
110+
111+
/**
112+
* Assert that file validation fails when uploaded file has correct extension but invalid mime type
113+
* @magentoAppArea adminhtml
114+
*/
115+
public function testFileValidationFailsWhenFileHasCorrectExtensionButInvalidMimeType()
116+
{
117+
$invalid_file_pathname = realpath(__DIR__ . '/../../../../_files/uploader/not-a.png');
118+
119+
$this->setFilesGlobalMock($invalid_file_pathname);
120+
$this->setUploaderMockForField('background_image');
100121

101-
$this->uploaderFactory
102-
->expects($this->once())
103-
->method('create')
104-
->will($this->returnValue($uploader));
105122

106123
$this->resultJson->expects($this->once())->method('setData')->willReturnCallback(function ($result) {
107-
$this->assertNotEquals([
124+
$this->assertEquals([
108125
'error' => 'File validation failed.',
109126
'errorcode' => 0
110127
], $result);
@@ -114,39 +131,40 @@ public function testFileValidationPassesWhenFileHasCorrectExtensionAndValidMimeT
114131
}
115132

116133
/**
117-
* Assert that file validation fails when uploaded file has correct extension but invalid mime type
134+
* Initiates Uploader object for `$fieldId` and returns as a result of `UploaderFactory::create()`
135+
*
136+
* @param string $fieldId
137+
* @return Uploader|MockObject
118138
*/
119-
public function testFileValidationFailsWhenFileHasCorrectExtensionButInvalidMimeType()
139+
private function setUploaderMockForField(string $fieldId): Uploader
120140
{
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',
141+
$uploader = $this->objectManager->create(Uploader::class, [
142+
'fileId' => $fieldId,
135143
'fileMime' => $this->objectManager->create(Mime::class),
136144
]);
137145

138146
$this->uploaderFactory
139147
->expects($this->once())
140148
->method('create')
141149
->will($this->returnValue($uploader));
150+
}
142151

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();
152+
/**
153+
* Mock that `$pathname` was uploaded (mock of `$_FILES` array)
154+
*
155+
* @param string $pathname
156+
* @return void
157+
*/
158+
private function setFilesGlobalMock(string $pathname): void
159+
{
160+
$_FILES = [
161+
'background_image' => [
162+
'type' => 'image/png',
163+
'name' => basename($pathname),
164+
'tmp_name' => $pathname,
165+
'size' => filesize($pathname),
166+
'error' => UPLOAD_ERR_OK,
167+
]
168+
];
151169
}
152170
}

0 commit comments

Comments
 (0)