|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\ImportExport\Test\Unit\Controller\Adminhtml\Export\File; |
| 9 | + |
| 10 | +use Magento\Backend\App\Action\Context; |
| 11 | +use Magento\Backend\Model\View\Result\Redirect; |
| 12 | +use Magento\Framework\App\Request\Http; |
| 13 | +use Magento\Framework\Controller\Result\Raw; |
| 14 | +use Magento\Framework\Controller\Result\RedirectFactory; |
| 15 | +use Magento\Framework\Filesystem; |
| 16 | +use Magento\Framework\Filesystem\Directory\ReadInterface; |
| 17 | +use Magento\Framework\Filesystem\DriverInterface; |
| 18 | +use Magento\Framework\Message\ManagerInterface; |
| 19 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 20 | +use Magento\ImportExport\Controller\Adminhtml\Export\File\Delete; |
| 21 | +use PHPUnit\Framework\MockObject\MockObject; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | + |
| 24 | +/** |
| 25 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 26 | + */ |
| 27 | +class DeleteTest extends TestCase |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @var Context|MockObject |
| 31 | + */ |
| 32 | + private $contextMock; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var ObjectManagerHelper |
| 36 | + */ |
| 37 | + private $objectManagerHelper; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var Http|MockObject |
| 41 | + */ |
| 42 | + private $requestMock; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var Raw|MockObject |
| 46 | + */ |
| 47 | + private $redirectMock; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var RedirectFactory|MockObject |
| 51 | + */ |
| 52 | + private $resultRedirectFactoryMock; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var Filesystem|MockObject |
| 56 | + */ |
| 57 | + private $fileSystemMock; |
| 58 | + |
| 59 | + /** |
| 60 | + * @var DriverInterface|MockObject |
| 61 | + */ |
| 62 | + private $fileMock; |
| 63 | + |
| 64 | + /** |
| 65 | + * @var Delete|MockObject |
| 66 | + */ |
| 67 | + private $deleteControllerMock; |
| 68 | + |
| 69 | + /** |
| 70 | + * @var ManagerInterface|MockObject |
| 71 | + */ |
| 72 | + private $messageManagerMock; |
| 73 | + |
| 74 | + /** |
| 75 | + * @var ReadInterface|MockObject |
| 76 | + */ |
| 77 | + private $directoryMock; |
| 78 | + |
| 79 | + /** |
| 80 | + * Set up |
| 81 | + */ |
| 82 | + protected function setUp() |
| 83 | + { |
| 84 | + $this->requestMock = $this->getMockBuilder(Http::class) |
| 85 | + ->disableOriginalConstructor() |
| 86 | + ->getMock(); |
| 87 | + |
| 88 | + $this->fileSystemMock = $this->getMockBuilder(Filesystem::class) |
| 89 | + ->disableOriginalConstructor() |
| 90 | + ->getMock(); |
| 91 | + |
| 92 | + $this->directoryMock = $this->getMockBuilder(ReadInterface::class) |
| 93 | + ->disableOriginalConstructor() |
| 94 | + ->getMock(); |
| 95 | + |
| 96 | + $this->fileMock = $this->getMockBuilder(DriverInterface::class) |
| 97 | + ->disableOriginalConstructor() |
| 98 | + ->getMock(); |
| 99 | + |
| 100 | + $this->messageManagerMock = $this->getMockBuilder(ManagerInterface::class) |
| 101 | + ->disableOriginalConstructor() |
| 102 | + ->getMock(); |
| 103 | + |
| 104 | + $this->contextMock = $this->createPartialMock( |
| 105 | + Context::class, |
| 106 | + ['getRequest', 'getResultRedirectFactory', 'getMessageManager'] |
| 107 | + ); |
| 108 | + |
| 109 | + $this->redirectMock = $this->createPartialMock(Redirect::class, ['setPath']); |
| 110 | + |
| 111 | + $this->resultRedirectFactoryMock = $this->createPartialMock( |
| 112 | + RedirectFactory::class, |
| 113 | + ['create'] |
| 114 | + ); |
| 115 | + $this->resultRedirectFactoryMock->expects($this->any())->method('create')->willReturn($this->redirectMock); |
| 116 | + $this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock); |
| 117 | + $this->contextMock->expects($this->any()) |
| 118 | + ->method('getResultRedirectFactory') |
| 119 | + ->willReturn($this->resultRedirectFactoryMock); |
| 120 | + |
| 121 | + $this->contextMock->expects($this->any()) |
| 122 | + ->method('getMessageManager') |
| 123 | + ->willReturn($this->messageManagerMock); |
| 124 | + |
| 125 | + $this->objectManagerHelper = new ObjectManagerHelper($this); |
| 126 | + $this->deleteControllerMock = $this->objectManagerHelper->getObject( |
| 127 | + Delete::class, |
| 128 | + [ |
| 129 | + 'context' => $this->contextMock, |
| 130 | + 'filesystem' => $this->fileSystemMock, |
| 131 | + 'file' => $this->fileMock |
| 132 | + ] |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Tests download controller with different file names in request. |
| 138 | + */ |
| 139 | + public function testExecuteSuccess() |
| 140 | + { |
| 141 | + $this->requestMock->method('getParam') |
| 142 | + ->with('filename') |
| 143 | + ->willReturn('sampleFile'); |
| 144 | + |
| 145 | + $this->fileSystemMock->expects($this->once()) |
| 146 | + ->method('getDirectoryRead') |
| 147 | + ->will($this->returnValue($this->directoryMock)); |
| 148 | + $this->directoryMock->expects($this->once())->method('isFile')->willReturn(true); |
| 149 | + $this->fileMock->expects($this->once())->method('deleteFile')->willReturn(true); |
| 150 | + $this->messageManagerMock->expects($this->once())->method('addSuccessMessage'); |
| 151 | + |
| 152 | + $this->deleteControllerMock->execute(); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Tests download controller with different file names in request. |
| 157 | + */ |
| 158 | + public function testExecuteFileDoesntExists() |
| 159 | + { |
| 160 | + $this->requestMock->method('getParam') |
| 161 | + ->with('filename') |
| 162 | + ->willReturn('sampleFile'); |
| 163 | + |
| 164 | + $this->fileSystemMock->expects($this->once()) |
| 165 | + ->method('getDirectoryRead') |
| 166 | + ->will($this->returnValue($this->directoryMock)); |
| 167 | + $this->directoryMock->expects($this->once())->method('isFile')->willReturn(false); |
| 168 | + $this->messageManagerMock->expects($this->once())->method('addErrorMessage'); |
| 169 | + |
| 170 | + $this->deleteControllerMock->execute(); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Test execute() with invalid file name |
| 175 | + * @param string $requestFilename |
| 176 | + * @dataProvider invalidFileDataProvider |
| 177 | + */ |
| 178 | + public function testExecuteInvalidFileName($requestFilename) |
| 179 | + { |
| 180 | + $this->requestMock->method('getParam')->with('filename')->willReturn($requestFilename); |
| 181 | + $this->messageManagerMock->expects($this->once())->method('addErrorMessage'); |
| 182 | + |
| 183 | + $this->deleteControllerMock->execute(); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Data provider to test possible invalid filenames |
| 188 | + * @return array |
| 189 | + */ |
| 190 | + public function invalidFileDataProvider() |
| 191 | + { |
| 192 | + return [ |
| 193 | + 'Relative file name' => ['../.htaccess'], |
| 194 | + 'Empty file name' => [''], |
| 195 | + 'Null file name' => [null], |
| 196 | + ]; |
| 197 | + } |
| 198 | +} |
0 commit comments