3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
-
7
6
declare (strict_types=1 );
8
7
9
8
namespace Magento \PageBuilder \Controller \Adminhtml \ContentType \Image ;
10
9
10
+ use Magento \Framework \App \ObjectManager ;
11
+ use Magento \Framework \Controller \Result \Json ;
12
+ use Magento \Framework \Controller \Result \JsonFactory ;
11
13
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
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 ;
20
+
21
+ class UploadTest extends TestCase
18
22
{
19
23
/**
20
- * @var \Magento\PageBuilder\Controller\Adminhtml\ContentType\Image\Upload
24
+ * @var UploadController
21
25
*/
22
26
private $ controller ;
23
27
24
28
/**
25
- * @var \Magento\Framework\ ObjectManagerInterface
29
+ * @var ObjectManagerInterface
26
30
*/
27
31
private $ objectManager ;
28
32
29
33
/**
30
- * @var \Magento\Framework\File\ UploaderFactory|\PHPUnit_Framework_MockObject_MockObject
34
+ * @var UploaderFactory|MockObject
31
35
*/
32
36
private $ uploaderFactory ;
33
37
34
38
/**
35
- * @var \Magento\Framework\Controller\Result\ Json|\PHPUnit_Framework_MockObject_MockObject
39
+ * @var Json|MockObject
36
40
*/
37
41
private $ resultJson ;
38
42
39
43
/**
40
- * @var \Magento\Framework\Controller\Result\ JsonFactory|\PHPUnit_Framework_MockObject_MockObject
44
+ * @var JsonFactory|MockObject
41
45
*/
42
46
private $ resultJsonFactory ;
43
47
@@ -46,23 +50,25 @@ class UploadTest extends \PHPUnit\Framework\TestCase
46
50
*/
47
51
protected function setUp ()
48
52
{
49
- $ this ->objectManager = \ Magento \ TestFramework \ Helper \Bootstrap:: getObjectManager ();
53
+ $ this ->objectManager = ObjectManager:: getInstance ();
50
54
51
- $ this ->uploaderFactory = $ this ->createPartialMock (\ Magento \ Framework \ File \ UploaderFactory::class, ['create ' ]);
55
+ $ this ->uploaderFactory = $ this ->createPartialMock (UploaderFactory::class, ['create ' ]);
52
56
53
- $ this ->resultJson = $ this ->getMockBuilder (\ Magento \ Framework \ Controller \ Result \ Json::class)
57
+ $ this ->resultJson = $ this ->getMockBuilder (Json::class)
54
58
->setMethods (['setData ' ])
55
59
->disableOriginalConstructor ()
56
60
->getMock ();
57
61
58
- $ this ->resultJsonFactory = $ this ->getMockBuilder (\ Magento \ Framework \ Controller \ Result \ JsonFactory::class)
62
+ $ this ->resultJsonFactory = $ this ->getMockBuilder (JsonFactory::class)
59
63
->setMethods (['create ' ])
60
64
->disableOriginalConstructor ()
61
65
->getMock ();
62
66
63
- $ this ->resultJsonFactory ->expects ($ this ->once ())->method ('create ' )->willReturn ($ this ->resultJson );
67
+ $ this ->resultJsonFactory ->expects ($ this ->once ())
68
+ ->method ('create ' )
69
+ ->willReturn ($ this ->resultJson );
64
70
65
- $ this ->controller = $ this ->objectManager ->create (Controller ::class, [
71
+ $ this ->controller = $ this ->objectManager ->create (UploadController ::class, [
66
72
'resultJsonFactory ' => $ this ->resultJsonFactory ,
67
73
'uploaderFactory ' => $ this ->uploaderFactory
68
74
]);
@@ -78,33 +84,40 @@ protected function tearDown()
78
84
79
85
/**
80
86
* Assert that file validation passes when uploaded file has correct extension and valid mime type
87
+ * @magentoAppArea adminhtml
81
88
*/
82
89
public function testFileValidationPassesWhenFileHasCorrectExtensionAndValidMimeType ()
83
90
{
84
- $ valid_file_pathname = realpath (dirname ( __FILE__ ) . '/../../../../_files/uploader/a.png ' );
91
+ $ valid_file_pathname = realpath (__DIR__ . '/../../../../_files/uploader/a.png ' );
85
92
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
- ];
93
+ $ this ->setFilesGlobalMock ($ valid_file_pathname );
94
+ $ this ->setUploaderMockForField ('background_image ' );
95
95
96
- $ uploader = $ this ->objectManager ->create (\Magento \Framework \File \Uploader::class, [
97
- 'fileId ' => 'background_image ' ,
98
- 'fileMime ' => $ this ->objectManager ->create (Mime::class),
99
- ]);
96
+ $ this ->resultJson ->expects ($ this ->once ())
97
+ ->method ('setData ' )
98
+ ->willReturnCallback (function ($ result ) {
99
+ $ this ->assertNotEquals ([
100
+ 'error ' => 'File validation failed. ' ,
101
+ 'errorcode ' => 0
102
+ ], $ result );
103
+ });
100
104
101
- $ this ->uploaderFactory
102
- ->expects ($ this ->once ())
103
- ->method ('create ' )
104
- ->will ($ this ->returnValue ($ uploader ));
105
+ $ this ->controller ->execute ();
106
+ }
107
+
108
+ /**
109
+ * Assert that file validation fails when uploaded file has correct extension but invalid mime type
110
+ * @magentoAppArea adminhtml
111
+ */
112
+ public function testFileValidationFailsWhenFileHasCorrectExtensionButInvalidMimeType ()
113
+ {
114
+ $ invalid_file_pathname = realpath (__DIR__ . '/../../../../_files/uploader/not-a.png ' );
115
+
116
+ $ this ->setFilesGlobalMock ($ invalid_file_pathname );
117
+ $ this ->setUploaderMockForField ('background_image ' );
105
118
106
119
$ this ->resultJson ->expects ($ this ->once ())->method ('setData ' )->willReturnCallback (function ($ result ) {
107
- $ this ->assertNotEquals ([
120
+ $ this ->assertEquals ([
108
121
'error ' => 'File validation failed. ' ,
109
122
'errorcode ' => 0
110
123
], $ result );
@@ -114,39 +127,40 @@ public function testFileValidationPassesWhenFileHasCorrectExtensionAndValidMimeT
114
127
}
115
128
116
129
/**
117
- * Assert that file validation fails when uploaded file has correct extension but invalid mime type
130
+ * Initiates Uploader object for `$fieldId` and returns as a result of `UploaderFactory::create()`
131
+ *
132
+ * @param string $fieldId
133
+ * @return void
118
134
*/
119
- public function testFileValidationFailsWhenFileHasCorrectExtensionButInvalidMimeType ()
135
+ private function setUploaderMockForField ( string $ fieldId ): void
120
136
{
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 ' ,
137
+ $ uploader = $ this ->objectManager ->create (Uploader::class, [
138
+ 'fileId ' => $ fieldId ,
135
139
'fileMime ' => $ this ->objectManager ->create (Mime::class),
136
140
]);
137
141
138
142
$ this ->uploaderFactory
139
143
->expects ($ this ->once ())
140
144
->method ('create ' )
141
145
->will ($ this ->returnValue ($ uploader ));
146
+ }
142
147
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 ();
148
+ /**
149
+ * Mock that `$pathname` was uploaded (mock of `$_FILES` array)
150
+ *
151
+ * @param string $pathname
152
+ * @return void
153
+ */
154
+ private function setFilesGlobalMock (string $ pathname ): void
155
+ {
156
+ $ _FILES = [
157
+ 'background_image ' => [
158
+ 'type ' => 'image/png ' ,
159
+ 'name ' => basename ($ pathname ),
160
+ 'tmp_name ' => $ pathname ,
161
+ 'size ' => filesize ($ pathname ),
162
+ 'error ' => UPLOAD_ERR_OK ,
163
+ ]
164
+ ];
151
165
}
152
166
}
0 commit comments