Skip to content

Commit c2c5ec4

Browse files
authored
Merge pull request #7165 from magento-arcticfoxes/2.4-develop-pr
[arcticfoxes] PRs
2 parents e9c16d7 + ef4ecf2 commit c2c5ec4

File tree

13 files changed

+359
-169
lines changed

13 files changed

+359
-169
lines changed

app/code/Magento/CustomerImportExport/Test/Unit/Model/Import/CustomerCompositeTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,10 @@ public function testPrepareRowForDb()
588588
$directoryMock->expects($this->any())
589589
->method('openFile')
590590
->willReturn(new Read($pathToCsvFile, new File()));
591+
$directoryMock->expects($this->any())
592+
->method('getRelativePath')
593+
->with($pathToCsvFile)
594+
->willReturn($pathToCsvFile);
591595
$source = new Csv($pathToCsvFile, $directoryMock);
592596
$modelUnderTest->setSource($source);
593597
$modelUnderTest->validateData();

app/code/Magento/ImportExport/Model/Import/Source/Csv.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\ImportExport\Model\Import\Source;
77

8+
use Magento\Framework\Filesystem\Directory\Read;
9+
810
/**
911
* CSV import adapter
1012
*/
@@ -27,26 +29,40 @@ class Csv extends \Magento\ImportExport\Model\Import\AbstractSource
2729
*/
2830
protected $_enclosure = '';
2931

32+
/**
33+
* @var string
34+
*/
35+
private string $filePath;
36+
37+
/**
38+
* @var array
39+
*/
40+
private static array $openFiles;
41+
3042
/**
3143
* Open file and detect column names
3244
*
3345
* There must be column names in the first line
3446
*
3547
* @param string $file
36-
* @param \Magento\Framework\Filesystem\Directory\Read $directory
48+
* @param Read $directory
3749
* @param string $delimiter
3850
* @param string $enclosure
3951
* @throws \LogicException
4052
*/
4153
public function __construct(
4254
$file,
43-
\Magento\Framework\Filesystem\Directory\Read $directory,
55+
Read $directory,
4456
$delimiter = ',',
4557
$enclosure = '"'
4658
) {
59+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
4760
register_shutdown_function([$this, 'destruct']);
4861
try {
49-
$this->_file = $directory->openFile($directory->getRelativePath($file), 'r');
62+
$this->filePath = $directory->getRelativePath($file);
63+
$this->_file = $directory->openFile($this->filePath, 'r');
64+
$this->_file->seek(0);
65+
self::$openFiles[$this->filePath] = true;
5066
} catch (\Magento\Framework\Exception\FileSystemException $e) {
5167
throw new \LogicException("Unable to open file: '{$file}'");
5268
}
@@ -64,8 +80,9 @@ public function __construct(
6480
*/
6581
public function destruct()
6682
{
67-
if (is_object($this->_file)) {
83+
if (is_object($this->_file) && !empty(self::$openFiles[$this->filePath])) {
6884
$this->_file->close();
85+
unset(self::$openFiles[$this->filePath]);
6986
}
7087
}
7188

app/code/Magento/ImportExport/Test/Unit/Model/Import/Source/CsvTest.php

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,16 @@ protected function setUp(): void
4040

4141
public function testConstructException()
4242
{
43+
$filePath = __DIR__ . '/invalid_file';
4344
$this->expectException(\LogicException::class);
44-
$this->_directoryMock->expects($this->any())
45+
$this->_directoryMock->expects($this->once())
46+
->method('getRelativePath')
47+
->with($filePath)
48+
->willReturn($filePath);
49+
$this->_directoryMock->expects($this->once())
4550
->method('openFile')
4651
->willThrowException(new FileSystemException(__('Error message')));
47-
new Csv(__DIR__ . '/invalid_file', $this->_directoryMock);
52+
new Csv($filePath, $this->_directoryMock);
4853
}
4954

5055
public function testConstructStream()
@@ -56,9 +61,7 @@ public function testConstructStream()
5661
)->method(
5762
'openFile'
5863
)->willReturn(
59-
60-
new Read($stream, new Http())
61-
64+
new Read($stream, new Http())
6265
);
6366
$this->_filesystem->expects(
6467
$this->any()
@@ -82,18 +85,16 @@ public function testConstructStream()
8285
*/
8386
public function testOptionalArgs($delimiter, $enclosure, $expectedColumns)
8487
{
85-
$this->_directoryMock->expects(
86-
$this->any()
87-
)->method(
88-
'openFile'
89-
)->willReturn(
90-
new Read(
91-
__DIR__ . '/_files/test.csv',
92-
new File()
93-
)
94-
);
88+
$filePath = __DIR__ . '/_files/test.csv';
89+
$this->_directoryMock->expects($this->once())
90+
->method('getRelativePath')
91+
->with($filePath)
92+
->willReturn($filePath);
93+
$this->_directoryMock->expects($this->any())
94+
->method('openFile')
95+
->willReturn(new Read($filePath, new File()));
9596
$model = new Csv(
96-
__DIR__ . '/_files/test.csv',
97+
$filePath,
9798
$this->_directoryMock,
9899
$delimiter,
99100
$enclosure
@@ -117,20 +118,15 @@ public function testRewind()
117118
{
118119
$this->expectException(\InvalidArgumentException::class);
119120
$this->expectExceptionMessage('wrongColumnsNumber');
120-
$this->_directoryMock->expects(
121-
$this->any()
122-
)->method(
123-
'openFile'
124-
)->willReturn(
125-
new Read(
126-
__DIR__ . '/_files/test.csv',
127-
new File()
128-
)
129-
);
130-
$model = new Csv(
131-
__DIR__ . '/_files/test.csv',
132-
$this->_directoryMock
133-
);
121+
$filePath = __DIR__ . '/_files/test.csv';
122+
$this->_directoryMock->expects($this->once())
123+
->method('getRelativePath')
124+
->with($filePath)
125+
->willReturn($filePath);
126+
$this->_directoryMock->expects($this->any())
127+
->method('openFile')
128+
->willReturn(new Read($filePath, new File()));
129+
$model = new Csv($filePath, $this->_directoryMock);
134130
$this->assertSame(-1, $model->key());
135131
$model->next();
136132
$this->assertSame(0, $model->key());

app/code/Magento/RemoteStorage/Plugin/Image.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function __construct(
8787
*/
8888
public function beforeOpen(AbstractAdapter $subject, $filename): array
8989
{
90-
if ($this->isEnabled) {
90+
if ($this->isEnabled && !empty($filename)) {
9191
$filename = $this->copyFileToTmp($filename);
9292
}
9393
return [$filename];
@@ -167,8 +167,9 @@ public function aroundSave(
167167
public function __destruct()
168168
{
169169
try {
170-
foreach ($this->tmpFiles as $tmpFile) {
170+
foreach ($this->tmpFiles as $key => $tmpFile) {
171171
$this->tmpDirectoryWrite->delete($tmpFile);
172+
unset($this->tmpFiles[$key]);
172173
}
173174
} catch (\Exception $e) {
174175
$this->logger->error($e->getMessage());
@@ -192,7 +193,7 @@ private function copyFileToTmp(string $filePath): string
192193
$this->tmpDirectoryWrite->create();
193194
$tmpPath = $this->storeTmpName($filePath);
194195
$content = $this->remoteDirectoryWrite->getDriver()->fileGetContents($filePath);
195-
$filePath = $this->tmpDirectoryWrite->getDriver()->filePutContents($tmpPath, $content)
196+
$filePath = $this->tmpDirectoryWrite->getDriver()->filePutContents($tmpPath, $content) >= 0
196197
? $tmpPath
197198
: $filePath;
198199
}

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

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\App\Bootstrap;
1010
use Magento\Framework\App\Filesystem\DirectoryList;
1111
use Magento\ImportExport\Model\Export\Adapter\AbstractAdapter;
12+
use Magento\Framework\Filesystem\Driver\File;
1213
use Magento\Store\Model\Store;
1314
use Magento\TestFramework\Annotation\DataFixture;
1415
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
@@ -41,7 +42,7 @@ abstract class AbstractProductExportImportTestCase extends \PHPUnit\Framework\Te
4142
protected $fixtures;
4243

4344
/**
44-
* skipped attributes
45+
* List of attributes which will be skipped
4546
*
4647
* @var array
4748
*/
@@ -66,11 +67,6 @@ abstract class AbstractProductExportImportTestCase extends \PHPUnit\Framework\Te
6667
'tax_class_id',
6768
];
6869

69-
/**
70-
* @var AbstractAdapter
71-
*/
72-
private $writer;
73-
7470
/**
7571
* @var string
7672
*/
@@ -97,7 +93,7 @@ protected function tearDown(): void
9793
$this->executeFixtures($this->fixtures, true);
9894

9995
if ($this->csvFile !== null) {
100-
$directoryWrite = $this->fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR);
96+
$directoryWrite = $this->fileSystem->getDirectoryWrite(DirectoryList::VAR_IMPORT_EXPORT);
10197
$directoryWrite->delete($this->csvFile);
10298
}
10399
}
@@ -416,12 +412,16 @@ private function exportProducts(Product $exportProduct = null)
416412
$exportProduct = $exportProduct ?: $this->objectManager->create(
417413
Product::class
418414
);
419-
$this->writer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
415+
$writer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
420416
\Magento\ImportExport\Model\Export\Adapter\Csv::class,
421-
['fileSystem' => $this->fileSystem, 'destination' => $csvfile]
417+
['fileSystem' => $this->fileSystem]
422418
);
423-
$exportProduct->setWriter($this->writer);
424-
$this->assertNotEmpty($exportProduct->export());
419+
$exportProduct->setWriter($writer);
420+
$content = $exportProduct->export();
421+
$this->assertNotEmpty($content);
422+
423+
$directory = $this->fileSystem->getDirectoryWrite(DirectoryList::VAR_IMPORT_EXPORT);
424+
$directory->getDriver()->filePutContents($directory->getAbsolutePath($csvfile), $content);
425425

426426
return $csvfile;
427427
}
@@ -439,7 +439,7 @@ private function importProducts(string $csvfile, string $behavior): void
439439
$importModel = $this->objectManager->create(
440440
\Magento\CatalogImportExport\Model\Import\Product::class
441441
);
442-
$directory = $this->fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR);
442+
$directory = $this->fileSystem->getDirectoryWrite(DirectoryList::VAR_IMPORT_EXPORT);
443443
$source = $this->objectManager->create(
444444
\Magento\ImportExport\Model\Import\Source\Csv::class,
445445
[
@@ -451,19 +451,21 @@ private function importProducts(string $csvfile, string $behavior): void
451451
$appParams = \Magento\TestFramework\Helper\Bootstrap::getInstance()->getBootstrap()
452452
->getApplication()
453453
->getInitParams()[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS];
454-
$uploader = $importModel->getUploader();
455-
$rootDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::ROOT);
456-
$destDir = $rootDirectory->getRelativePath(
457-
$appParams[DirectoryList::MEDIA][DirectoryList::PATH] . '/catalog/product'
458-
);
459-
$tmpDir = $rootDirectory->getRelativePath(
460-
$appParams[DirectoryList::MEDIA][DirectoryList::PATH] . '/import'
461-
);
454+
$mediaDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
455+
456+
$mediaDir = $mediaDirectory->getDriver() instanceof File ?
457+
$appParams[DirectoryList::MEDIA][DirectoryList::PATH] : 'media';
462458

463-
$rootDirectory->create($destDir);
464-
$rootDirectory->create($tmpDir);
465-
$this->assertTrue($uploader->setDestDir($destDir));
466-
$this->assertTrue($uploader->setTmpDir($tmpDir));
459+
$mediaDirectory->create('catalog/product');
460+
$mediaDirectory->create('import');
461+
$importModel->setParameters(
462+
[
463+
\Magento\ImportExport\Model\Import::FIELD_NAME_IMG_FILE_DIR => $mediaDir . '/import'
464+
]
465+
);
466+
$uploader = $importModel->getUploader();
467+
$this->assertTrue($uploader->setDestDir($mediaDir . '/catalog/product'));
468+
$this->assertTrue($uploader->setTmpDir($mediaDir . '/import'));
467469

468470
$errors = $importModel->setParameters(
469471
[

0 commit comments

Comments
 (0)