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 ;
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 ;
13
20
14
21
/**
15
22
* Class UploadTest
16
23
*/
17
- class UploadTest extends \ PHPUnit \ Framework \ TestCase
24
+ class UploadTest extends TestCase
18
25
{
19
26
/**
20
- * @var \Magento\PageBuilder\Controller\Adminhtml\ContentType\Image\Upload
27
+ * @var UploadController
21
28
*/
22
29
private $ controller ;
23
30
24
31
/**
25
- * @var \Magento\Framework\ ObjectManagerInterface
32
+ * @var ObjectManagerInterface
26
33
*/
27
34
private $ objectManager ;
28
35
29
36
/**
30
- * @var \Magento\Framework\File\ UploaderFactory|\PHPUnit_Framework_MockObject_MockObject
37
+ * @var UploaderFactory|MockObject
31
38
*/
32
39
private $ uploaderFactory ;
33
40
34
41
/**
35
- * @var \Magento\Framework\Controller\Result\ Json|\PHPUnit_Framework_MockObject_MockObject
42
+ * @var Json|MockObject
36
43
*/
37
44
private $ resultJson ;
38
45
39
46
/**
40
- * @var \Magento\Framework\Controller\Result\ JsonFactory|\PHPUnit_Framework_MockObject_MockObject
47
+ * @var JsonFactory|MockObject
41
48
*/
42
49
private $ resultJsonFactory ;
43
50
@@ -46,23 +53,25 @@ class UploadTest extends \PHPUnit\Framework\TestCase
46
53
*/
47
54
protected function setUp ()
48
55
{
49
- $ this ->objectManager = \ Magento \ TestFramework \ Helper \Bootstrap:: getObjectManager ();
56
+ $ this ->objectManager = ObjectManager:: getInstance ();
50
57
51
- $ this ->uploaderFactory = $ this ->createPartialMock (\ Magento \ Framework \ File \ UploaderFactory::class, ['create ' ]);
58
+ $ this ->uploaderFactory = $ this ->createPartialMock (UploaderFactory::class, ['create ' ]);
52
59
53
- $ this ->resultJson = $ this ->getMockBuilder (\ Magento \ Framework \ Controller \ Result \ Json::class)
60
+ $ this ->resultJson = $ this ->getMockBuilder (Json::class)
54
61
->setMethods (['setData ' ])
55
62
->disableOriginalConstructor ()
56
63
->getMock ();
57
64
58
- $ this ->resultJsonFactory = $ this ->getMockBuilder (\ Magento \ Framework \ Controller \ Result \ JsonFactory::class)
65
+ $ this ->resultJsonFactory = $ this ->getMockBuilder (JsonFactory::class)
59
66
->setMethods (['create ' ])
60
67
->disableOriginalConstructor ()
61
68
->getMock ();
62
69
63
- $ this ->resultJsonFactory ->expects ($ this ->once ())->method ('create ' )->willReturn ($ this ->resultJson );
70
+ $ this ->resultJsonFactory ->expects ($ this ->once ())
71
+ ->method ('create ' )
72
+ ->willReturn ($ this ->resultJson );
64
73
65
- $ this ->controller = $ this ->objectManager ->create (Controller ::class, [
74
+ $ this ->controller = $ this ->objectManager ->create (UploadController ::class, [
66
75
'resultJsonFactory ' => $ this ->resultJsonFactory ,
67
76
'uploaderFactory ' => $ this ->uploaderFactory
68
77
]);
@@ -78,33 +87,41 @@ protected function tearDown()
78
87
79
88
/**
80
89
* Assert that file validation passes when uploaded file has correct extension and valid mime type
90
+ * @magentoAppArea adminhtml
81
91
*/
82
92
public function testFileValidationPassesWhenFileHasCorrectExtensionAndValidMimeType ()
83
93
{
84
- $ valid_file_pathname = realpath (dirname ( __FILE__ ) . '/../../../../_files/uploader/a.png ' );
94
+ $ valid_file_pathname = realpath (__DIR__ . '/../../../../_files/uploader/a.png ' );
85
95
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 ' );
95
98
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 ' );
100
121
101
- $ this ->uploaderFactory
102
- ->expects ($ this ->once ())
103
- ->method ('create ' )
104
- ->will ($ this ->returnValue ($ uploader ));
105
122
106
123
$ this ->resultJson ->expects ($ this ->once ())->method ('setData ' )->willReturnCallback (function ($ result ) {
107
- $ this ->assertNotEquals ([
124
+ $ this ->assertEquals ([
108
125
'error ' => 'File validation failed. ' ,
109
126
'errorcode ' => 0
110
127
], $ result );
@@ -114,39 +131,40 @@ public function testFileValidationPassesWhenFileHasCorrectExtensionAndValidMimeT
114
131
}
115
132
116
133
/**
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
118
138
*/
119
- public function testFileValidationFailsWhenFileHasCorrectExtensionButInvalidMimeType ()
139
+ private function setUploaderMockForField ( string $ fieldId ): Uploader
120
140
{
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 ,
135
143
'fileMime ' => $ this ->objectManager ->create (Mime::class),
136
144
]);
137
145
138
146
$ this ->uploaderFactory
139
147
->expects ($ this ->once ())
140
148
->method ('create ' )
141
149
->will ($ this ->returnValue ($ uploader ));
150
+ }
142
151
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
+ ];
151
169
}
152
170
}
0 commit comments