Skip to content

Commit 2e51795

Browse files
committed
MC-15448: One can't download or delete export csv file from export index page grid
1 parent 5c887ea commit 2e51795

File tree

5 files changed

+181
-37
lines changed

5 files changed

+181
-37
lines changed

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Magento\ImportExport\Controller\Adminhtml\Export\File;
99

1010
use Magento\Backend\App\Action;
11-
use Magento\Framework\App\Action\HttpGetActionInterface;
11+
use Magento\Framework\App\Action\HttpPostActionInterface;
1212
use Magento\Framework\Controller\ResultFactory;
1313
use Magento\Framework\Exception\FileSystemException;
1414
use Magento\Framework\Exception\LocalizedException;
@@ -20,12 +20,12 @@
2020
/**
2121
* Controller that delete file by name.
2222
*/
23-
class Delete extends ExportController implements HttpGetActionInterface
23+
class Delete extends ExportController implements HttpPostActionInterface
2424
{
2525
/**
26-
* url to this controller
26+
* Url to this controller
2727
*/
28-
const URL = 'admin/export_file/delete';
28+
const URL = 'adminhtml/export_file/delete';
2929

3030
/**
3131
* @var Filesystem
@@ -37,11 +37,6 @@ class Delete extends ExportController implements HttpGetActionInterface
3737
*/
3838
private $file;
3939

40-
/**
41-
* @inheritdoc
42-
*/
43-
protected $_publicActions = ['delete'];
44-
4540
/**
4641
* Delete constructor.
4742
* @param Action\Context $context

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
class Download extends ExportController implements HttpGetActionInterface
2222
{
2323
/**
24-
* url to this controller
24+
* Url to this controller
2525
*/
26-
const URL = 'admin/export_file/download/';
26+
const URL = 'adminhtml/export_file/download/';
2727

2828
/**
2929
* @var FileFactory
@@ -35,11 +35,6 @@ class Download extends ExportController implements HttpGetActionInterface
3535
*/
3636
private $filesystem;
3737

38-
/**
39-
* @inheritdoc
40-
*/
41-
protected $_publicActions = ['download'];
42-
4338
/**
4439
* DownloadFile constructor.
4540
* @param Action\Context $context

app/code/Magento/ImportExport/Ui/Component/Columns/ExportGridActions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public function prepareDataSource(array $dataSource)
6565
'confirm' => [
6666
'title' => __('Delete'),
6767
'message' => __('Are you sure you wan\'t to delete a file?')
68-
]
68+
],
69+
'post' => true,
6970
];
7071
}
7172
}

dev/tests/integration/testsuite/Magento/ImportExport/Controller/Adminhtml/Export/File/DeleteTest.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Magento\ImportExport\Controller\Adminhtml\Export\File;
99

1010
use Magento\Framework\App\Filesystem\DirectoryList;
11-
use Magento\Framework\Data\Form\FormKey;
11+
use Magento\Framework\App\Request\Http;
1212
use Magento\Framework\Filesystem;
1313
use Magento\Framework\Filesystem\Directory\WriteInterface;
1414
use Magento\TestFramework\Helper\Bootstrap;
@@ -47,47 +47,34 @@ protected function setUp()
4747
parent::setUp();
4848

4949
$this->filesystem = $this->_objectManager->get(Filesystem::class);
50+
$baseDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::ROOT);
5051
$this->varDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
5152
$this->varDirectory->create($this->varDirectory->getRelativePath('export'));
5253
$this->fullDirectoryPath = $this->varDirectory->getAbsolutePath('export');
5354
$filePath = $this->fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName;
5455
$fixtureDir = realpath(__DIR__ . '/../../Import/_files');
55-
copy($fixtureDir . '/' . $this->fileName, $filePath);
56+
$baseDirectory->copyFile($fixtureDir . '/' . $this->fileName, $filePath);
5657
}
5758

5859
/**
5960
* Check that file can be removed under var/export directory.
6061
*
6162
* @return void
63+
* @magentoConfigFixture default_store admin/security/use_form_key 1
6264
*/
6365
public function testExecute(): void
6466
{
6567
$uri = 'backend/admin/export_file/delete/filename/' . $this->fileName;
66-
$this->prepareRequest($uri);
68+
$request = $this->getRequest();
69+
$request->setMethod(Http::METHOD_POST);
70+
$request->setRequestUri($uri);
6771
$this->dispatch($uri);
6872

6973
$this->assertFalse(
7074
$this->varDirectory->isExist($this->varDirectory->getRelativePath('export/' . $this->fileName))
7175
);
7276
}
7377

74-
/**
75-
* Prepares GET request for file deletion.
76-
*
77-
* @param string $uri
78-
* @return void
79-
*/
80-
private function prepareRequest(string $uri): void
81-
{
82-
/** @var FormKey $formKey */
83-
$formKey = $this->_objectManager->get(FormKey::class);
84-
$request = $this->getRequest();
85-
$request->setMethod('GET');
86-
$request->setParam('form_key', $formKey->getFormKey());
87-
$request->setRequestUri($uri);
88-
$request->setParams(['filename' => 'catalog_product.csv']);
89-
}
90-
9178
/**
9279
* @inheritdoc
9380
*/
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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\Controller\Adminhtml\Export\File;
9+
10+
use Magento\Backend\Model\Auth\Session;
11+
use Magento\Framework\App\Filesystem\DirectoryList;
12+
use Magento\Framework\App\Request\Http;
13+
use Magento\Framework\Filesystem;
14+
use Magento\Framework\Filesystem\Directory\WriteInterface;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use Magento\TestFramework\TestCase\AbstractBackendController;
17+
use Magento\Backend\Model\UrlInterface as BackendUrl;
18+
use Magento\Backend\Model\Auth;
19+
use Magento\TestFramework\Bootstrap as TestBootstrap;
20+
21+
/**
22+
* Test for \Magento\ImportExport\Controller\Adminhtml\Export\File\Download class.
23+
*/
24+
class DownloadTest extends AbstractBackendController
25+
{
26+
/**
27+
* @var WriteInterface
28+
*/
29+
private $varDirectory;
30+
31+
/**
32+
* @var string
33+
*/
34+
private $fullDirectoryPath;
35+
36+
/**
37+
* @var string
38+
*/
39+
private $fileName = 'catalog_product.csv';
40+
41+
/**
42+
* @var string
43+
*/
44+
private $filesize;
45+
46+
/**
47+
* @var Filesystem
48+
*/
49+
private $filesystem;
50+
51+
/**
52+
* @var Auth
53+
*/
54+
private $auth;
55+
56+
/**
57+
* @var BackendUrl
58+
*/
59+
private $backendUrl;
60+
61+
/**
62+
* @inheritdoc
63+
*/
64+
protected function setUp()
65+
{
66+
parent::setUp();
67+
68+
$this->filesystem = $this->_objectManager->get(Filesystem::class);
69+
$this->auth = $this->_objectManager->get(Auth::class);
70+
$this->backendUrl = $this->_objectManager->get(BackendUrl::class);
71+
$baseDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::ROOT);
72+
$this->varDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
73+
$this->varDirectory->create($this->varDirectory->getRelativePath('export'));
74+
$this->fullDirectoryPath = $this->varDirectory->getAbsolutePath('export');
75+
$filePath = $this->fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName;
76+
$fixtureDir = realpath(__DIR__ . '/../../Import/_files');
77+
$baseDirectory->copyFile($fixtureDir . '/' . $this->fileName, $filePath);
78+
$this->filesize = filesize($filePath);
79+
}
80+
81+
/**
82+
* Check that file can be downloaded.
83+
*
84+
* @return void
85+
* @magentoConfigFixture default_store admin/security/use_form_key 1
86+
* @magentoAppArea adminhtml
87+
*/
88+
public function testExecute(): void
89+
{
90+
$uri = 'backend/admin/export_file/download/filename/' . $this->fileName;
91+
$this->prepareRequest($uri);
92+
93+
$this->dispatch($uri);
94+
95+
$contentType = $this->getResponse()->getHeader('content-type');
96+
$contentLength = $this->getResponse()->getHeader('content-length');
97+
$contentDisposition = $this->getResponse()->getHeader('content-disposition');
98+
99+
$this->assertEquals(200, $this->getResponse()->getStatusCode(), 'Incorrect response status code');
100+
$this->assertEquals(
101+
'application/octet-stream',
102+
$contentType->getFieldValue(),
103+
'Incorrect response header "content-type"'
104+
);
105+
$this->assertEquals(
106+
'attachment; filename="export/' . $this->fileName . '"',
107+
$contentDisposition->getFieldValue(),
108+
'Incorrect response header "content-disposition"'
109+
);
110+
$this->assertEquals(
111+
$this->filesize,
112+
$contentLength->getFieldValue(),
113+
'Incorrect response header "content-length"'
114+
);
115+
}
116+
117+
/**
118+
* Prepares GET request to download file.
119+
*
120+
* @param string $uri
121+
* @return void
122+
*/
123+
private function prepareRequest(string $uri): void
124+
{
125+
$authSession = $this->_objectManager->create(Session::class);
126+
$authSession->setIsFirstPageAfterLogin(false);
127+
$this->auth->login(
128+
TestBootstrap::ADMIN_NAME,
129+
TestBootstrap::ADMIN_PASSWORD
130+
);
131+
$this->auth->setAuthStorage($authSession);
132+
133+
list($routeName, $controllerName, $actionName) = explode('/', Download::URL);
134+
$request = $this->getRequest();
135+
$request->setMethod(Http::METHOD_GET)
136+
->setRouteName($routeName)
137+
->setControllerName($controllerName)
138+
->setActionName($actionName)
139+
->setParam(BackendUrl::SECRET_KEY_PARAM_NAME, $this->backendUrl->getSecretKey())
140+
->setRequestUri($uri);
141+
$this->backendUrl->turnOnSecretKey();
142+
}
143+
144+
/**
145+
* @inheritdoc
146+
*/
147+
protected function tearDown()
148+
{
149+
$this->auth = null;
150+
151+
parent::tearDown();
152+
}
153+
154+
/**
155+
* @inheritdoc
156+
*/
157+
public static function tearDownAfterClass()
158+
{
159+
$filesystem = Bootstrap::getObjectManager()->get(Filesystem::class);
160+
/** @var WriteInterface $directory */
161+
$directory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
162+
if ($directory->isExist('export')) {
163+
$directory->delete('export');
164+
}
165+
}
166+
}

0 commit comments

Comments
 (0)