Skip to content

Commit ce30b12

Browse files
committed
MC-21630: Controller behavior minor changes
1 parent 9e5c194 commit ce30b12

File tree

2 files changed

+179
-2
lines changed

2 files changed

+179
-2
lines changed

app/code/Magento/ImportExport/Controller/Adminhtml/Export/File/Download.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ public function __construct(
5959
*/
6060
public function execute()
6161
{
62-
if (empty($fileName = $this->getRequest()->getParam('filename'))) {
63-
throw new LocalizedException(__('Please provide export file name'));
62+
$fileName = $this->getRequest()->getParam('filename');
63+
if (empty($fileName) || preg_match('/\.\.(\\\|\/)/', $fileName)) {
64+
throw new LocalizedException(__('Please provide valid export file name'));
6465
}
6566
try {
6667
$path = 'export/' . $fileName;
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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\Framework\Filesystem;
11+
use Magento\Backend\App\Action\Context;
12+
use Magento\Framework\App\Request\Http;
13+
use Magento\Framework\App\ResponseInterface;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use Magento\Framework\Filesystem\Directory\Read;
16+
use Magento\Framework\App\Filesystem\DirectoryList;
17+
use Magento\Framework\App\Response\Http\FileFactory;
18+
use Magento\ImportExport\Controller\Adminhtml\Export\File\Download;
19+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
20+
21+
/**
22+
* Unit tests for \Magento\ImportExport\Controller\Adminhtml\Export\File\Download.
23+
*/
24+
class DownloadTest extends \PHPUnit\Framework\TestCase
25+
{
26+
/**
27+
* @var Download
28+
*/
29+
private $controller;
30+
31+
/**
32+
* @var ObjectManagerHelper
33+
*/
34+
private $objectManager;
35+
36+
/**
37+
* @var Context|MockObject
38+
*/
39+
private $contextMock;
40+
41+
/**
42+
* @var FileFactory|MockObject
43+
*/
44+
private $fileFactoryMock;
45+
46+
/**
47+
* @var Filesystem|MockObject
48+
*/
49+
private $fileSystemMock;
50+
51+
/**
52+
* @var Http|MockObject
53+
*/
54+
private $requestMock;
55+
56+
/**
57+
* @var Read|MockObject
58+
*/
59+
private $readMock;
60+
61+
/**
62+
* @inheritdoc
63+
*/
64+
protected function setUp()
65+
{
66+
$this->objectManager = new ObjectManagerHelper($this);
67+
68+
$this->contextMock = $this->createPartialMock(
69+
Context::class,
70+
['getRequest', 'getObjectManager', 'getResultRedirectFactory']
71+
);
72+
$this->fileFactoryMock = $this->createPartialMock(FileFactory::class, ['create']);
73+
$this->fileSystemMock = $this->createPartialMock(Filesystem::class, ['getDirectoryRead']);
74+
$this->requestMock = $this->createPartialMock(Http::class, ['getParam']);
75+
$this->readMock = $this->createPartialMock(Read::class, ['isFile', 'readFile']);
76+
77+
$this->contextMock->expects($this->once())->method('getRequest')->willReturn($this->requestMock);
78+
79+
$this->controller = $this->objectManager->getObject(
80+
Download::class,
81+
[
82+
'context' => $this->contextMock,
83+
'fileFactory' => $this->fileFactoryMock,
84+
'filesystem' => $this->fileSystemMock,
85+
]
86+
);
87+
}
88+
89+
/**
90+
* Check download controller behavior.
91+
*
92+
* @return void
93+
*/
94+
public function testDownload()
95+
{
96+
$fileName = 'customer.csv';
97+
$path = 'export/' . $fileName;
98+
$fileContent = 'content';
99+
100+
$this->processDownloadAction($fileName, $path);
101+
$this->readMock->expects($this->once())->method('readFile')->with($path)->willReturn($fileContent);
102+
$response = $this->createMock(ResponseInterface::class);
103+
$this->fileFactoryMock->expects($this->once())
104+
->method('create')
105+
->with($path, $fileContent, DirectoryList::VAR_DIR)->willReturn($response);
106+
107+
$this->controller->execute();
108+
}
109+
110+
/**
111+
* Check behavior with incorrect filename.
112+
*
113+
* @param string $fileName
114+
* @dataProvider fileNameDataProvider
115+
* @expectedException \Magento\Framework\Exception\LocalizedException
116+
* @expectedExceptionMessage Please provide valid export file name
117+
* @return void
118+
*/
119+
public function testDownloadWithIncorrectFilename(string $fileName)
120+
{
121+
$this->requestMock->expects($this->once())->method('getParam')->with('filename')->willReturn($fileName);
122+
123+
$this->controller->execute();
124+
}
125+
126+
/**
127+
* Data provider for testDownloadWithIncorrectFilename.
128+
*
129+
* @return array
130+
*/
131+
public function fileNameDataProvider(): array
132+
{
133+
return [
134+
'Empty parameter' => [''],
135+
'Incorrect Name' => ['../customer.csv'],
136+
];
137+
}
138+
139+
/**
140+
* Check behavior when method throw exception.
141+
*
142+
* @expectedException \Magento\Framework\Exception\LocalizedException
143+
* @expectedExceptionMessage There are no export file with such name
144+
* @return void
145+
*/
146+
public function testDownloadWithException()
147+
{
148+
$fileName = 'customer.csv';
149+
$path = 'export/' . $fileName;
150+
151+
$this->processDownloadAction($fileName, $path);
152+
$this->readMock->expects($this->once())
153+
->method('readFile')
154+
->with($path)
155+
->willThrowException(new \Exception('Message'));
156+
157+
$this->controller->execute();
158+
}
159+
160+
/**
161+
* Check that parameter valid and file exist.
162+
*
163+
* @param string $fileName
164+
* @param string $path
165+
* @return void
166+
*/
167+
private function processDownloadAction(string $fileName, string $path)
168+
{
169+
$this->requestMock->expects($this->once())->method('getParam')->with('filename')->willReturn($fileName);
170+
$this->fileSystemMock->expects($this->once())
171+
->method('getDirectoryRead')
172+
->with(DirectoryList::VAR_DIR)
173+
->willReturn($this->readMock);
174+
$this->readMock->expects($this->once())->method('isFile')->with($path)->willReturn(true);
175+
}
176+
}

0 commit comments

Comments
 (0)