|
7 | 7 |
|
8 | 8 | namespace Magento\PageBuilder\Controller\Adminhtml\ContentType\Image;
|
9 | 9 |
|
10 |
| -use Magento\Framework\App\ObjectManager; |
11 |
| -use Magento\Framework\Controller\Result\Json; |
12 |
| -use Magento\Framework\Controller\Result\JsonFactory; |
13 |
| -use Magento\Framework\File\Mime; |
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; |
| 10 | +use Magento\Framework\App\Request\Http as HttpRequest; |
| 11 | +use Magento\Framework\Exception\FileSystemException; |
| 12 | +use Magento\Framework\Filesystem; |
| 13 | +use Magento\Framework\Filesystem\DirectoryList; |
| 14 | +use Magento\TestFramework\TestCase\AbstractBackendController; |
20 | 15 |
|
21 |
| -class UploadTest extends TestCase |
| 16 | +/** |
| 17 | + * @magentoAppArea adminhtml |
| 18 | + */ |
| 19 | +class UploadTest extends AbstractBackendController |
22 | 20 | {
|
23 |
| - /** |
24 |
| - * @var UploadController |
25 |
| - */ |
26 |
| - private $controller; |
27 |
| - |
28 |
| - /** |
29 |
| - * @var ObjectManagerInterface |
30 |
| - */ |
31 |
| - private $objectManager; |
32 |
| - |
33 |
| - /** |
34 |
| - * @var UploaderFactory|MockObject |
35 |
| - */ |
36 |
| - private $uploaderFactory; |
37 |
| - |
38 |
| - /** |
39 |
| - * @var Json|MockObject |
40 |
| - */ |
41 |
| - private $resultJson; |
42 |
| - |
43 |
| - /** |
44 |
| - * @var JsonFactory|MockObject |
45 |
| - */ |
46 |
| - private $resultJsonFactory; |
47 |
| - |
48 |
| - /** |
49 |
| - * @inheritdoc |
50 |
| - */ |
51 |
| - protected function setUp(): void |
52 |
| - { |
53 |
| - $this->objectManager = ObjectManager::getInstance(); |
54 |
| - |
55 |
| - $this->uploaderFactory = $this->createPartialMock(UploaderFactory::class, ['create']); |
56 |
| - |
57 |
| - $this->resultJson = $this->getMockBuilder(Json::class) |
58 |
| - ->setMethods(['setData']) |
59 |
| - ->disableOriginalConstructor() |
60 |
| - ->getMock(); |
61 |
| - |
62 |
| - $this->resultJsonFactory = $this->getMockBuilder(JsonFactory::class) |
63 |
| - ->setMethods(['create']) |
64 |
| - ->disableOriginalConstructor() |
65 |
| - ->getMock(); |
66 |
| - |
67 |
| - $this->resultJsonFactory->expects($this->once()) |
68 |
| - ->method('create') |
69 |
| - ->willReturn($this->resultJson); |
70 |
| - |
71 |
| - $this->controller = $this->objectManager->create(UploadController::class, [ |
72 |
| - 'resultJsonFactory' => $this->resultJsonFactory, |
73 |
| - 'uploaderFactory' => $this->uploaderFactory |
74 |
| - ]); |
75 |
| - } |
| 21 | + private const URI = 'backend/pagebuilder/contentType/image_upload'; |
76 | 22 |
|
77 | 23 | /**
|
78 | 24 | * @inheritdoc
|
79 | 25 | */
|
80 | 26 | protected function tearDown(): void
|
81 | 27 | {
|
82 | 28 | $_FILES = [];
|
| 29 | + parent::tearDown(); |
83 | 30 | }
|
84 | 31 |
|
85 | 32 | /**
|
86 | 33 | * Assert that file validation passes when uploaded file has correct extension and valid mime type
|
87 |
| - * @magentoAppArea adminhtml |
88 | 34 | */
|
89 | 35 | public function testFileValidationPassesWhenFileHasCorrectExtensionAndValidMimeType()
|
90 | 36 | {
|
91 |
| - $valid_file_pathname = realpath(__DIR__ . '/../../../../_files/uploader/a.png'); |
92 |
| - |
93 |
| - $this->setFilesGlobalMock($valid_file_pathname); |
94 |
| - $this->setUploaderMockForField('background_image'); |
95 |
| - |
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 |
| - }); |
104 |
| - |
105 |
| - $this->controller->execute(); |
| 37 | + $filePath = realpath(__DIR__ . '/../../../../_files/uploader/a.png'); |
| 38 | + |
| 39 | + $this->createUploadFixture($filePath); |
| 40 | + $this->makeRequest(); |
| 41 | + $this->assertEquals(200, $this->getResponse()->getStatusCode()); |
| 42 | + $result = json_decode($this->getResponse()->getBody(), true); |
| 43 | + $this->assertEquals(0, $result['error']); |
| 44 | + $this->assertNotEmpty($result['url']); |
106 | 45 | }
|
107 | 46 |
|
108 | 47 | /**
|
109 | 48 | * Assert that file validation fails when uploaded file has correct extension but invalid mime type
|
110 |
| - * @magentoAppArea adminhtml |
111 | 49 | */
|
112 | 50 | public function testFileValidationFailsWhenFileHasCorrectExtensionButInvalidMimeType()
|
113 | 51 | {
|
114 |
| - $invalid_file_pathname = realpath(__DIR__ . '/../../../../_files/uploader/not-a.png'); |
115 |
| - |
116 |
| - $this->setFilesGlobalMock($invalid_file_pathname); |
117 |
| - $this->setUploaderMockForField('background_image'); |
118 |
| - |
119 |
| - $this->resultJson->expects($this->once())->method('setData')->willReturnCallback(function ($result) { |
120 |
| - $this->assertEquals([ |
| 52 | + $filePath = realpath(__DIR__ . '/../../../../_files/uploader/not-a.png'); |
| 53 | + |
| 54 | + $this->createUploadFixture($filePath); |
| 55 | + $this->makeRequest(); |
| 56 | + $this->assertEquals(200, $this->getResponse()->getStatusCode()); |
| 57 | + $result = json_decode($this->getResponse()->getBody(), true); |
| 58 | + $this->assertEquals( |
| 59 | + [ |
121 | 60 | 'error' => 'File validation failed.',
|
122 | 61 | 'errorcode' => 0
|
123 |
| - ], $result); |
124 |
| - }); |
125 |
| - |
126 |
| - $this->controller->execute(); |
| 62 | + ], |
| 63 | + $result |
| 64 | + ); |
127 | 65 | }
|
128 | 66 |
|
129 | 67 | /**
|
130 |
| - * Initiates Uploader object for `$fieldId` and returns as a result of `UploaderFactory::create()` |
| 68 | + * Assert that file url should be based on backend base url |
131 | 69 | *
|
132 |
| - * @param string $fieldId |
133 |
| - * @return void |
| 70 | + * @magentoConfigFixture default_store web/unsecure/base_url http://storefront.magento.test/ |
| 71 | + * @magentoConfigFixture default_store web/secure/base_url https://storefront.magento.test/ |
134 | 72 | */
|
135 |
| - private function setUploaderMockForField(string $fieldId): void |
| 73 | + public function testFileUrlShouldBeBaseOnBackendBaseUrl() |
136 | 74 | {
|
137 |
| - $uploader = $this->objectManager->create(Uploader::class, [ |
138 |
| - 'fileId' => $fieldId, |
139 |
| - 'fileMime' => $this->objectManager->create(Mime::class), |
140 |
| - ]); |
| 75 | + $filePath = realpath(__DIR__ . '/../../../../_files/uploader/a.png'); |
141 | 76 |
|
142 |
| - $this->uploaderFactory |
143 |
| - ->expects($this->once()) |
144 |
| - ->method('create') |
145 |
| - ->will($this->returnValue($uploader)); |
| 77 | + $this->createUploadFixture($filePath); |
| 78 | + $this->makeRequest(); |
| 79 | + $this->assertEquals(200, $this->getResponse()->getStatusCode()); |
| 80 | + $result = json_decode($this->getResponse()->getBody(), true); |
| 81 | + $this->assertStringStartsWith('http://localhost/media/', $result['url']); |
146 | 82 | }
|
147 | 83 |
|
148 | 84 | /**
|
149 |
| - * Mock that `$pathname` was uploaded (mock of `$_FILES` array) |
| 85 | + * Initiates request |
150 | 86 | *
|
151 |
| - * @param string $pathname |
152 |
| - * @return void |
| 87 | + * @param string $fileParam |
153 | 88 | */
|
154 |
| - private function setFilesGlobalMock(string $pathname): void |
| 89 | + private function makeRequest(string $fileParam = 'image'): void |
155 | 90 | {
|
| 91 | + $this->getRequest() |
| 92 | + ->setParams(['param_name' => $fileParam]) |
| 93 | + ->setMethod(HttpRequest::METHOD_POST); |
| 94 | + $this->dispatch(self::URI); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Creates a fixture for testing uploaded file |
| 99 | + * |
| 100 | + * @param string $filePath |
| 101 | + * @param string $fileType |
| 102 | + * @param string $fileParam |
| 103 | + * @return void |
| 104 | + * @throws FileSystemException |
| 105 | + */ |
| 106 | + private function createUploadFixture( |
| 107 | + string $filePath, |
| 108 | + string $fileType = 'image/png', |
| 109 | + string $fileParam = 'image' |
| 110 | + ): void { |
| 111 | + $filename = basename($filePath); |
| 112 | + $filesize = filesize($filePath); |
| 113 | + /** @var \Magento\TestFramework\App\Filesystem $filesystem */ |
| 114 | + $filesystem = $this->_objectManager->get(Filesystem::class); |
| 115 | + $tmpDir = $filesystem->getDirectoryWrite(DirectoryList::SYS_TMP); |
| 116 | + // phpcs:ignore |
| 117 | + $subDir = md5(get_class($this)); |
| 118 | + $tmpDir->create($subDir); |
| 119 | + $tmpPath = $tmpDir->getAbsolutePath("{$subDir}/{$filename}"); |
| 120 | + copy($filePath, $tmpPath); |
156 | 121 | $_FILES = [
|
157 |
| - 'background_image' => [ |
158 |
| - 'type' => 'image/png', |
159 |
| - 'name' => basename($pathname), |
160 |
| - 'tmp_name' => $pathname, |
161 |
| - 'size' => filesize($pathname), |
| 122 | + $fileParam => [ |
| 123 | + 'type' => $fileType, |
| 124 | + 'name' => $filename, |
| 125 | + 'tmp_name' => $tmpPath, |
| 126 | + 'size' => $filesize, |
162 | 127 | 'error' => UPLOAD_ERR_OK,
|
163 |
| - ] |
| 128 | + ], |
164 | 129 | ];
|
165 | 130 | }
|
166 | 131 | }
|
0 commit comments