Skip to content

Commit b2ac86e

Browse files
MC-29960: [Magento Cloud] Unable to delete CSV files from Export Grid
1 parent dbb159a commit b2ac86e

File tree

4 files changed

+135
-34
lines changed

4 files changed

+135
-34
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,17 @@ public function prepareDataSource(array $dataSource)
5656
$name = $this->getData('name');
5757
if (isset($item['file_name'])) {
5858
$item[$name]['view'] = [
59-
'href' => $this->urlBuilder->getUrl(Download::URL, ['filename' => $item['file_name']]),
59+
'href' => $this->urlBuilder->getUrl(
60+
Download::URL,
61+
['_query' => ['filename' => $item['file_name']]]
62+
),
6063
'label' => __('Download')
6164
];
6265
$item[$name]['delete'] = [
63-
'href' => $this->urlBuilder->getUrl(Delete::URL, ['filename' => $item['file_name']]),
66+
'href' => $this->urlBuilder->getUrl(
67+
Delete::URL,
68+
['_query' => ['filename' => $item['file_name']]]
69+
),
6470
'label' => __('Delete'),
6571
'confirm' => [
6672
'title' => __('Delete'),

app/code/Magento/ImportExport/Ui/DataProvider/ExportFileDataProvider.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function getData()
100100
}
101101
$result = [];
102102
foreach ($files as $file) {
103-
$result['items'][]['file_name'] = $this->fileIO->getPathInfo($file)['basename'];
103+
$result['items'][]['file_name'] = $this->getPathToExportFile($this->fileIO->getPathInfo($file));
104104
}
105105

106106
$pageSize = (int) $this->request->getParam('paging')['pageSize'];
@@ -112,6 +112,31 @@ public function getData()
112112
return $result;
113113
}
114114

115+
/**
116+
* Return relative export file path after "var/export"
117+
*
118+
* @param mixed $file
119+
* @return string
120+
*/
121+
private function getPathToExportFile($file): string
122+
{
123+
$directory = $this->fileSystem->getDirectoryRead(DirectoryList::VAR_DIR);
124+
$delimiter = '/';
125+
$cutPath = explode(
126+
$delimiter,
127+
$directory->getAbsolutePath() . 'export'
128+
);
129+
$filePath = explode(
130+
$delimiter,
131+
$file['dirname']
132+
);
133+
134+
return ltrim(
135+
implode($delimiter, array_diff($filePath, $cutPath)) . $delimiter . $file['basename'],
136+
$delimiter
137+
);
138+
}
139+
115140
/**
116141
* Get files from directory path, sort them by date modified and return sorted array of full path to files
117142
*
@@ -127,8 +152,10 @@ private function getExportFiles(string $directoryPath): array
127152
return [];
128153
}
129154
foreach ($files as $filePath) {
130-
//phpcs:ignore Magento2.Functions.DiscouragedFunction
131-
$sortedFiles[filemtime($filePath)] = $filePath;
155+
if ($this->file->isFile($filePath)) {
156+
//phpcs:ignore Magento2.Functions.DiscouragedFunction
157+
$sortedFiles[filemtime($filePath)] = $filePath;
158+
}
132159
}
133160
//sort array elements using key value
134161
krsort($sortedFiles);

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

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,42 +29,78 @@ class DeleteTest extends AbstractBackendController
2929
*/
3030
private $fileName = 'catalog_product.csv';
3131

32+
/**
33+
* @var Filesystem
34+
*/
35+
private $fileSystem;
36+
37+
/**
38+
* @var string
39+
*/
40+
private $sourceFilePath;
41+
3242
/**
3343
* @inheritdoc
3444
*/
3545
protected function setUp()
3646
{
3747
parent::setUp();
3848

39-
$filesystem = $this->_objectManager->get(Filesystem::class);
40-
$sourceFilePath = __DIR__ . '/../../Import/_files' . DIRECTORY_SEPARATOR . $this->fileName;
41-
$destinationFilePath = 'export' . DIRECTORY_SEPARATOR . $this->fileName;
49+
$this->fileSystem = $this->_objectManager->get(Filesystem::class);
50+
$this->sourceFilePath = __DIR__ . '/../../Import/_files' . DIRECTORY_SEPARATOR . $this->fileName;
4251
//Refers to tests 'var' directory
43-
$this->varDirectory = $filesystem->getDirectoryRead(DirectoryList::VAR_DIR);
44-
//Refers to application root directory
45-
$rootDirectory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
46-
$rootDirectory->copyFile($sourceFilePath, $this->varDirectory->getAbsolutePath($destinationFilePath));
52+
$this->varDirectory = $this->fileSystem->getDirectoryRead(DirectoryList::VAR_DIR);
4753
}
4854

4955
/**
5056
* Check that file can be removed under var/export directory.
5157
*
58+
* @param string $file
59+
* @dataProvider testExecuteProvider
5260
* @return void
5361
* @magentoConfigFixture default_store admin/security/use_form_key 1
5462
*/
55-
public function testExecute(): void
63+
public function testExecute($file): void
5664
{
65+
$fullPath = 'export/' . $file;
66+
$this->copyFile($fullPath);
5767
$request = $this->getRequest();
58-
$request->setParam('filename', $this->fileName);
68+
$request->setParam('filename', $file);
5969
$request->setMethod(Http::METHOD_POST);
6070

61-
if ($this->varDirectory->isExist('export/' . $this->fileName)) {
71+
if ($this->varDirectory->isExist($fullPath)) {
6272
$this->dispatch('backend/admin/export_file/delete');
6373
} else {
6474
throw new \AssertionError('Export product file supposed to exist');
6575
}
6676

67-
$this->assertFalse($this->varDirectory->isExist('export/' . $this->fileName));
77+
$this->assertFalse($this->varDirectory->isExist($fullPath));
78+
}
79+
80+
/**
81+
* Copy csv file from sourceFilePath to destinationFilePath
82+
*
83+
* @param $destinationFilePath
84+
* @return void
85+
*/
86+
private function copyFile($destinationFilePath): void
87+
{
88+
//Refers to application root directory
89+
$rootDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::ROOT);
90+
$rootDirectory->copyFile($this->sourceFilePath, $this->varDirectory->getAbsolutePath($destinationFilePath));
91+
}
92+
93+
/**
94+
* Csv file path for copying from sourceFilePath and for future deleting
95+
*
96+
* @return array
97+
*/
98+
public static function testExecuteProvider(): array
99+
{
100+
return [
101+
['catalog_product.csv'],
102+
['test/catalog_product.csv']
103+
];
68104
}
69105

70106
/**

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

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Magento\ImportExport\Controller\Adminhtml\Export\File;
99

10-
use Magento\Backend\Model\Auth\Session;
1110
use Magento\Framework\App\Filesystem\DirectoryList;
1211
use Magento\Framework\App\Request\Http;
1312
use Magento\Framework\Filesystem;
@@ -16,7 +15,6 @@
1615
use Magento\TestFramework\TestCase\AbstractBackendController;
1716
use Magento\Backend\Model\UrlInterface as BackendUrl;
1817
use Magento\Backend\Model\Auth;
19-
use Magento\TestFramework\Bootstrap as TestBootstrap;
2018

2119
/**
2220
* Test for \Magento\ImportExport\Controller\Adminhtml\Export\File\Download class.
@@ -28,11 +26,6 @@ class DownloadTest extends AbstractBackendController
2826
*/
2927
private $fileName = 'catalog_product.csv';
3028

31-
/**
32-
* @var string
33-
*/
34-
private $filesize;
35-
3629
/**
3730
* @var Auth
3831
*/
@@ -43,38 +36,51 @@ class DownloadTest extends AbstractBackendController
4336
*/
4437
private $backendUrl;
4538

39+
/**
40+
* @var WriteInterface
41+
*/
42+
private $varDirectory;
43+
44+
/**
45+
* @var Filesystem
46+
*/
47+
private $fileSystem;
48+
49+
/**
50+
* @var string
51+
*/
52+
private $sourceFilePath;
53+
4654
/**
4755
* @inheritdoc
4856
*/
4957
protected function setUp()
5058
{
5159
parent::setUp();
5260

53-
$filesystem = $this->_objectManager->get(Filesystem::class);
61+
$this->fileSystem = $this->_objectManager->get(Filesystem::class);
5462
$auth = $this->_objectManager->get(Auth::class);
5563
$auth->getAuthStorage()->setIsFirstPageAfterLogin(false);
5664
$this->backendUrl = $this->_objectManager->get(BackendUrl::class);
5765
$this->backendUrl->turnOnSecretKey();
58-
59-
$sourceFilePath = __DIR__ . '/../../Import/_files' . DIRECTORY_SEPARATOR . $this->fileName;
60-
$destinationFilePath = 'export' . DIRECTORY_SEPARATOR . $this->fileName;
66+
$this->sourceFilePath = __DIR__ . '/../../Import/_files' . DIRECTORY_SEPARATOR . $this->fileName;
6167
//Refers to tests 'var' directory
62-
$varDirectory = $filesystem->getDirectoryRead(DirectoryList::VAR_DIR);
63-
//Refers to application root directory
64-
$rootDirectory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
65-
$rootDirectory->copyFile($sourceFilePath, $varDirectory->getAbsolutePath($destinationFilePath));
66-
$this->filesize = $varDirectory->stat($destinationFilePath)['size'];
68+
$this->varDirectory = $this->fileSystem->getDirectoryRead(DirectoryList::VAR_DIR);
6769
}
6870

6971
/**
7072
* Check that file can be downloaded.
7173
*
74+
* @param string $file
75+
* @dataProvider testExecuteProvider
7276
* @return void
7377
* @magentoConfigFixture default_store admin/security/use_form_key 1
7478
* @magentoAppArea adminhtml
7579
*/
76-
public function testExecute(): void
80+
public function testExecute($file): void
7781
{
82+
$this->copyFile('export/' . $file);
83+
$fileSize = $this->varDirectory->stat('export/' . $file)['size'];
7884
$request = $this->getRequest();
7985
list($routeName, $controllerName, $actionName) = explode('/', Download::URL);
8086
$request->setMethod(Http::METHOD_GET)
@@ -104,12 +110,38 @@ public function testExecute(): void
104110
'Incorrect response header "content-disposition"'
105111
);
106112
$this->assertEquals(
107-
$this->filesize,
113+
$fileSize,
108114
$contentLength->getFieldValue(),
109115
'Incorrect response header "content-length"'
110116
);
111117
}
112118

119+
/**
120+
* Copy csv file from sourceFilePath to destinationFilePath
121+
*
122+
* @param $destinationFilePath
123+
* @return void
124+
*/
125+
private function copyFile($destinationFilePath): void
126+
{
127+
//Refers to application root directory
128+
$rootDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::ROOT);
129+
$rootDirectory->copyFile($this->sourceFilePath, $this->varDirectory->getAbsolutePath($destinationFilePath));
130+
}
131+
132+
/**
133+
* Csv file path for copying from sourceFilePath and for future deleting
134+
*
135+
* @return array
136+
*/
137+
public static function testExecuteProvider(): array
138+
{
139+
return [
140+
['catalog_product.csv'],
141+
['test/catalog_product.csv']
142+
];
143+
}
144+
113145
/**
114146
* @inheritdoc
115147
*/

0 commit comments

Comments
 (0)