33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6-
76declare (strict_types=1 );
87
98namespace 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 ;
1113use 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
1822{
1923 /**
20- * @var \Magento\PageBuilder\Controller\Adminhtml\ContentType\Image\Upload
24+ * @var UploadController
2125 */
2226 private $ controller ;
2327
2428 /**
25- * @var \Magento\Framework\ ObjectManagerInterface
29+ * @var ObjectManagerInterface
2630 */
2731 private $ objectManager ;
2832
2933 /**
30- * @var \Magento\Framework\File\ UploaderFactory|\PHPUnit_Framework_MockObject_MockObject
34+ * @var UploaderFactory|MockObject
3135 */
3236 private $ uploaderFactory ;
3337
3438 /**
35- * @var \Magento\Framework\Controller\Result\ Json|\PHPUnit_Framework_MockObject_MockObject
39+ * @var Json|MockObject
3640 */
3741 private $ resultJson ;
3842
3943 /**
40- * @var \Magento\Framework\Controller\Result\ JsonFactory|\PHPUnit_Framework_MockObject_MockObject
44+ * @var JsonFactory|MockObject
4145 */
4246 private $ resultJsonFactory ;
4347
@@ -46,23 +50,25 @@ class UploadTest extends \PHPUnit\Framework\TestCase
4650 */
4751 protected function setUp ()
4852 {
49- $ this ->objectManager = \ Magento \ TestFramework \ Helper \Bootstrap:: getObjectManager ();
53+ $ this ->objectManager = ObjectManager:: getInstance ();
5054
51- $ this ->uploaderFactory = $ this ->createPartialMock (\ Magento \ Framework \ File \ UploaderFactory::class, ['create ' ]);
55+ $ this ->uploaderFactory = $ this ->createPartialMock (UploaderFactory::class, ['create ' ]);
5256
53- $ this ->resultJson = $ this ->getMockBuilder (\ Magento \ Framework \ Controller \ Result \ Json::class)
57+ $ this ->resultJson = $ this ->getMockBuilder (Json::class)
5458 ->setMethods (['setData ' ])
5559 ->disableOriginalConstructor ()
5660 ->getMock ();
5761
58- $ this ->resultJsonFactory = $ this ->getMockBuilder (\ Magento \ Framework \ Controller \ Result \ JsonFactory::class)
62+ $ this ->resultJsonFactory = $ this ->getMockBuilder (JsonFactory::class)
5963 ->setMethods (['create ' ])
6064 ->disableOriginalConstructor ()
6165 ->getMock ();
6266
63- $ this ->resultJsonFactory ->expects ($ this ->once ())->method ('create ' )->willReturn ($ this ->resultJson );
67+ $ this ->resultJsonFactory ->expects ($ this ->once ())
68+ ->method ('create ' )
69+ ->willReturn ($ this ->resultJson );
6470
65- $ this ->controller = $ this ->objectManager ->create (Controller ::class, [
71+ $ this ->controller = $ this ->objectManager ->create (UploadController ::class, [
6672 'resultJsonFactory ' => $ this ->resultJsonFactory ,
6773 'uploaderFactory ' => $ this ->uploaderFactory
6874 ]);
@@ -78,33 +84,40 @@ protected function tearDown()
7884
7985 /**
8086 * Assert that file validation passes when uploaded file has correct extension and valid mime type
87+ * @magentoAppArea adminhtml
8188 */
8289 public function testFileValidationPassesWhenFileHasCorrectExtensionAndValidMimeType ()
8390 {
84- $ valid_file_pathname = realpath (dirname ( __FILE__ ) . '/../../../../_files/uploader/a.png ' );
91+ $ valid_file_pathname = realpath (__DIR__ . '/../../../../_files/uploader/a.png ' );
8592
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 ' );
9595
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+ });
100104
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 ' );
105118
106119 $ this ->resultJson ->expects ($ this ->once ())->method ('setData ' )->willReturnCallback (function ($ result ) {
107- $ this ->assertNotEquals ([
120+ $ this ->assertEquals ([
108121 'error ' => 'File validation failed. ' ,
109122 'errorcode ' => 0
110123 ], $ result );
@@ -114,39 +127,40 @@ public function testFileValidationPassesWhenFileHasCorrectExtensionAndValidMimeT
114127 }
115128
116129 /**
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
118134 */
119- public function testFileValidationFailsWhenFileHasCorrectExtensionButInvalidMimeType ()
135+ private function setUploaderMockForField ( string $ fieldId ): void
120136 {
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 ,
135139 'fileMime ' => $ this ->objectManager ->create (Mime::class),
136140 ]);
137141
138142 $ this ->uploaderFactory
139143 ->expects ($ this ->once ())
140144 ->method ('create ' )
141145 ->will ($ this ->returnValue ($ uploader ));
146+ }
142147
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+ ];
151165 }
152166}
0 commit comments