Skip to content

Commit bc06501

Browse files
committed
MC-30148: Temporary files are not deleted after exporting products
1 parent 06a0dd0 commit bc06501

File tree

3 files changed

+92
-14
lines changed

3 files changed

+92
-14
lines changed

app/code/Magento/ImportExport/Model/Export/Adapter/Csv.php

Lines changed: 11 additions & 8 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -5,13 +5,16 @@
5
*/
5
*/
6
namespace Magento\ImportExport\Model\Export\Adapter;
6
namespace Magento\ImportExport\Model\Export\Adapter;
7

7

8+
use Magento\Framework\Exception\LocalizedException;
9+
use Magento\Framework\Filesystem\File\Write;
10+
8
/**
11
/**
9
* Export adapter csv.
12
* Export adapter csv.
10
*
13
*
11
* @api
14
* @api
12
* @since 100.0.2
15
* @since 100.0.2
13
*/
16
*/
14-
class Csv extends \Magento\ImportExport\Model\Export\Adapter\AbstractAdapter
17+
class Csv extends AbstractAdapter
15
{
18
{
16
/**
19
/**
17
* Field delimiter.
20
* Field delimiter.
@@ -30,28 +33,28 @@ class Csv extends \Magento\ImportExport\Model\Export\Adapter\AbstractAdapter
30
/**
33
/**
31
* Source file handler.
34
* Source file handler.
32
*
35
*
33-
* @var \Magento\Framework\Filesystem\File\Write
36+
* @var Write
34
*/
37
*/
35
protected $_fileHandler;
38
protected $_fileHandler;
36

39

37
/**
40
/**
38-
* {@inheritdoc }
41+
* Object destructor
39
*/
42
*/
40-
public function __construct(\Magento\Framework\Filesystem $filesystem, $destination = null)
43+
public function __destruct()
41
{
44
{
42-
register_shutdown_function([$this, 'destruct']);
45+
$this->destruct();
43-
parent::__construct($filesystem, $destination);
44
}
46
}
45

47

46
/**
48
/**
47-
* Object destructor.
49+
* Clean cached values
48
*
50
*
49
* @return void
51
* @return void
50
*/
52
*/
51
public function destruct()
53
public function destruct()
52
{
54
{
53
if (is_object($this->_fileHandler)) {
55
if (is_object($this->_fileHandler)) {
54
$this->_fileHandler->close();
56
$this->_fileHandler->close();
57+
$this->_directoryHandle->delete($this->_destination);
55
}
58
}
56
}
59
}
57

60

@@ -96,7 +99,7 @@ public function getFileExtension()
96
public function setHeaderCols(array $headerColumns)
99
public function setHeaderCols(array $headerColumns)
97
{
100
{
98
if (null !== $this->_headerCols) {
101
if (null !== $this->_headerCols) {
99-
throw new \Magento\Framework\Exception\LocalizedException(__('The header column names are already set.'));
102+
throw new LocalizedException(__('The header column names are already set.'));
100
}
103
}
101
if ($headerColumns) {
104
if ($headerColumns) {
102
foreach ($headerColumns as $columnName) {
105
foreach ($headerColumns as $columnName) {

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/AbstractProductExportImportTestCase.php

Lines changed: 11 additions & 6 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -7,6 +7,7 @@
7

7

8
use Magento\Framework\App\Bootstrap;
8
use Magento\Framework\App\Bootstrap;
9
use Magento\Framework\App\Filesystem\DirectoryList;
9
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\ImportExport\Model\Export\Adapter\AbstractAdapter;
10
use Magento\Store\Model\Store;
11
use Magento\Store\Model\Store;
11

12

12
/**
13
/**
@@ -62,6 +63,11 @@ abstract class AbstractProductExportImportTestCase extends \PHPUnit\Framework\Te
62
'tax_class_id',
63
'tax_class_id',
63
];
64
];
64

65

66+
/**
67+
* @var AbstractAdapter
68+
*/
69+
private $writer;
70+
65
/**
71
/**
66
* @inheritdoc
72
* @inheritdoc
67
*/
73
*/
@@ -367,7 +373,7 @@ protected function executeImportReplaceTest(
367
* Export products in the system.
373
* Export products in the system.
368
*
374
*
369
* @param \Magento\CatalogImportExport\Model\Export\Product|null $exportProduct
375
* @param \Magento\CatalogImportExport\Model\Export\Product|null $exportProduct
370-
* @return string Return exported file name
376+
* @return string Return exported file
371
*/
377
*/
372
private function exportProducts(\Magento\CatalogImportExport\Model\Export\Product $exportProduct = null)
378
private function exportProducts(\Magento\CatalogImportExport\Model\Export\Product $exportProduct = null)
373
{
379
{
@@ -376,12 +382,11 @@ private function exportProducts(\Magento\CatalogImportExport\Model\Export\Produc
376
$exportProduct = $exportProduct ?: $this->objectManager->create(
382
$exportProduct = $exportProduct ?: $this->objectManager->create(
377
\Magento\CatalogImportExport\Model\Export\Product::class
383
\Magento\CatalogImportExport\Model\Export\Product::class
378
);
384
);
379-
$exportProduct->setWriter(
385+
$this->writer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
380-
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
386+
\Magento\ImportExport\Model\Export\Adapter\Csv::class,
381-
\Magento\ImportExport\Model\Export\Adapter\Csv::class,
387+
['fileSystem' => $this->fileSystem, 'destination' => $csvfile]
382-
['fileSystem' => $this->fileSystem, 'destination' => $csvfile]
383-
)
384
);
388
);
389+
$exportProduct->setWriter($this->writer);
385
$this->assertNotEmpty($exportProduct->export());
390
$this->assertNotEmpty($exportProduct->export());
386

391

387
return $csvfile;
392
return $csvfile;
Lines changed: 70 additions & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -0,0 +1,70 @@
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\Model\Export\Adapter;
9+
10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\Filesystem;
12+
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* Test for Export adapter csv
18+
*/
19+
class CsvTest extends TestCase
20+
{
21+
/**
22+
* @var string Destination file name
23+
*/
24+
private $destination = 'destinationFile';
25+
26+
/**
27+
* @var ObjectManagerInterface
28+
*/
29+
private $objectManager;
30+
31+
/**
32+
* @var Csv
33+
*/
34+
private $csv;
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
protected function setUp()
40+
{
41+
parent::setUp();
42+
43+
$this->objectManager = Bootstrap::getObjectManager();
44+
$this->csv = $this->objectManager->create(
45+
Csv::class,
46+
['destination' => $this->destination]
47+
);
48+
}
49+
50+
/**
51+
* Test to destruct export adapter
52+
*/
53+
public function testDestruct(): void
54+
{
55+
/** @var Filesystem $fileSystem */
56+
$fileSystem = $this->objectManager->get(Filesystem::class);
57+
$directoryHandle = $fileSystem->getDirectoryRead(DirectoryList::VAR_DIR);
58+
/** Assert that the destination file is present after construct */
59+
$this->assertFileExists(
60+
$directoryHandle->getAbsolutePath($this->destination),
61+
'The destination file was\'t created after construct'
62+
);
63+
/** Assert that the destination file was removed after destruct */
64+
$this->csv = null;
65+
$this->assertFileNotExists(
66+
$directoryHandle->getAbsolutePath($this->destination),
67+
'The destination file was\'t removed after destruct'
68+
);
69+
}
70+
}

0 commit comments

Comments
 (0)