|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\PageBuilder\Controller\Adminhtml\ContentType\Image; |
| 10 | + |
| 11 | +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 |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var \Magento\PageBuilder\Controller\Adminhtml\ContentType\Image\Upload |
| 21 | + */ |
| 22 | + private $controller; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var \Magento\Framework\ObjectManagerInterface |
| 26 | + */ |
| 27 | + private $objectManager; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var \Magento\Framework\File\UploaderFactory|\PHPUnit_Framework_MockObject_MockObject |
| 31 | + */ |
| 32 | + private $uploaderFactory; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var \Magento\Framework\Controller\Result\Json|\PHPUnit_Framework_MockObject_MockObject |
| 36 | + */ |
| 37 | + private $resultJson; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var \Magento\Framework\Controller\Result\JsonFactory|\PHPUnit_Framework_MockObject_MockObject |
| 41 | + */ |
| 42 | + private $resultJsonFactory; |
| 43 | + |
| 44 | + /** |
| 45 | + * @inheritdoc |
| 46 | + */ |
| 47 | + protected function setUp() |
| 48 | + { |
| 49 | + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 50 | + |
| 51 | + $this->uploaderFactory = $this->createPartialMock(\Magento\Framework\File\UploaderFactory::class, ['create']); |
| 52 | + |
| 53 | + $this->resultJson = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class) |
| 54 | + ->setMethods(['setData']) |
| 55 | + ->disableOriginalConstructor() |
| 56 | + ->getMock(); |
| 57 | + |
| 58 | + $this->resultJsonFactory = $this->getMockBuilder(\Magento\Framework\Controller\Result\JsonFactory::class) |
| 59 | + ->setMethods(['create']) |
| 60 | + ->disableOriginalConstructor() |
| 61 | + ->getMock(); |
| 62 | + |
| 63 | + $this->resultJsonFactory->expects($this->once())->method('create')->willReturn($this->resultJson); |
| 64 | + |
| 65 | + $this->controller = $this->objectManager->create(Controller::class, [ |
| 66 | + 'resultJsonFactory' => $this->resultJsonFactory, |
| 67 | + 'uploaderFactory' => $this->uploaderFactory |
| 68 | + ]); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @inheritdoc |
| 73 | + */ |
| 74 | + protected function tearDown() |
| 75 | + { |
| 76 | + $_FILES = []; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Assert that file validation passes when uploaded file has correct extension and valid mime type |
| 81 | + */ |
| 82 | + public function testFileValidationPassesWhenFileHasCorrectExtensionAndValidMimeType() |
| 83 | + { |
| 84 | + $valid_file_pathname = realpath(dirname(__FILE__) . '/../../../../_files/uploader/a.png'); |
| 85 | + |
| 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 | + ]; |
| 95 | + |
| 96 | + $uploader = $this->objectManager->create(\Magento\Framework\File\Uploader::class, [ |
| 97 | + 'fileId' => 'background_image', |
| 98 | + 'fileMime' => $this->objectManager->create(Mime::class), |
| 99 | + ]); |
| 100 | + |
| 101 | + $this->uploaderFactory |
| 102 | + ->expects($this->once()) |
| 103 | + ->method('create') |
| 104 | + ->will($this->returnValue($uploader)); |
| 105 | + |
| 106 | + $this->resultJson->expects($this->once())->method('setData')->willReturnCallback(function ($result) { |
| 107 | + $this->assertNotEquals([ |
| 108 | + 'error' => 'File validation failed.', |
| 109 | + 'errorcode' => 0 |
| 110 | + ], $result); |
| 111 | + }); |
| 112 | + |
| 113 | + $this->controller->execute(); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Assert that file validation fails when uploaded file has correct extension but invalid mime type |
| 118 | + */ |
| 119 | + public function testFileValidationFailsWhenFileHasCorrectExtensionButInvalidMimeType() |
| 120 | + { |
| 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', |
| 135 | + 'fileMime' => $this->objectManager->create(Mime::class), |
| 136 | + ]); |
| 137 | + |
| 138 | + $this->uploaderFactory |
| 139 | + ->expects($this->once()) |
| 140 | + ->method('create') |
| 141 | + ->will($this->returnValue($uploader)); |
| 142 | + |
| 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(); |
| 151 | + } |
| 152 | +} |
0 commit comments