Skip to content

Commit ec31a59

Browse files
committed
Merge remote-tracking branch 'origin/MC-21630' into 2.3.4-develop-pr83
2 parents 8b9a586 + b9d39ca commit ec31a59

File tree

2 files changed

+165
-2
lines changed

2 files changed

+165
-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) !== 0) {
64+
throw new LocalizedException(__('Please provide valid export file name'));
6465
}
6566
try {
6667
$path = 'export/' . $fileName;
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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 testExecute(): void
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)
106+
->willReturn($response);
107+
108+
$this->controller->execute();
109+
}
110+
111+
/**
112+
* Check behavior with incorrect filename.
113+
*
114+
* @expectedException \Magento\Framework\Exception\LocalizedException
115+
* @expectedExceptionMessage Please provide valid export file name
116+
* @return void
117+
*/
118+
public function testExecuteWithEmptyFileName(): void
119+
{
120+
$this->requestMock->expects($this->once())->method('getParam')->with('filename')->willReturn('');
121+
122+
$this->controller->execute();
123+
}
124+
125+
/**
126+
* Check behavior when method throw exception.
127+
*
128+
* @expectedException \Magento\Framework\Exception\LocalizedException
129+
* @expectedExceptionMessage There are no export file with such name customer.csv
130+
* @return void
131+
*/
132+
public function testExecuteWithNonExistanceFile(): void
133+
{
134+
$fileName = 'customer.csv';
135+
$path = 'export/' . $fileName;
136+
137+
$this->processDownloadAction($fileName, $path);
138+
$this->readMock->expects($this->once())
139+
->method('readFile')
140+
->with($path)
141+
->willThrowException(new \Exception('Message'));
142+
143+
$this->controller->execute();
144+
}
145+
146+
/**
147+
* Check that parameter valid and file exist.
148+
*
149+
* @param string $fileName
150+
* @param string $path
151+
* @return void
152+
*/
153+
private function processDownloadAction(string $fileName, string $path): void
154+
{
155+
$this->requestMock->expects($this->once())->method('getParam')->with('filename')->willReturn($fileName);
156+
$this->fileSystemMock->expects($this->once())
157+
->method('getDirectoryRead')
158+
->with(DirectoryList::VAR_DIR)
159+
->willReturn($this->readMock);
160+
$this->readMock->expects($this->once())->method('isFile')->with($path)->willReturn(true);
161+
}
162+
}

0 commit comments

Comments
 (0)