Skip to content

Commit f5fe742

Browse files
ACPT-820
Added new integration test to cover new functionality and test for bugs that we saw when testing Customer imports with large CSV.
1 parent b7d5b17 commit f5fe742

File tree

2 files changed

+121
-19
lines changed

2 files changed

+121
-19
lines changed

dev/tests/integration/testsuite/Magento/CustomerImportExport/Model/Import/CustomerTest.php

Lines changed: 121 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@
88

99
use Magento\Customer\Api\CustomerRepositoryInterface;
1010
use Magento\Customer\Api\Data\CustomerInterface;
11+
use Magento\Customer\Model\Indexer\Processor;
1112
use Magento\Framework\App\Filesystem\DirectoryList;
1213
use Magento\Framework\Exception\NoSuchEntityException;
13-
use Magento\ImportExport\Model\Import;
14+
use Magento\Framework\Filesystem\Directory\Write as DirectoryWrite;
15+
use Magento\Framework\Filesystem\File\WriteFactory;
1416
use Magento\Framework\Indexer\StateInterface;
17+
use Magento\Framework\ObjectManagerInterface;
18+
use Magento\ImportExport\Model\Import;
19+
use Magento\ImportExport\Model\Import\Source\CsvFactory;
20+
use Magento\TestFramework\Helper\Bootstrap;
1521

1622
/**
17-
* Test for class \Magento\CustomerImportExport\Model\Import\Customer which covers validation logic
23+
* Test for class Customer which covers validation logic
1824
*
1925
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2026
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
@@ -24,44 +30,54 @@ class CustomerTest extends \PHPUnit\Framework\TestCase
2430
/**
2531
* Model object which used for tests
2632
*
27-
* @var Customer|\PHPUnit\Framework\MockObject\MockObject
33+
* @var ObjectManagerInterface
2834
*/
29-
protected $_model;
35+
private $objectManager;
3036

3137
/**
32-
* Customer data
38+
* Model object which used for tests
3339
*
40+
* @var Customer&\PHPUnit\Framework\MockObject\MockObject
41+
*/
42+
protected $_model;
43+
44+
/**
3445
* @var array
3546
*/
3647
protected $_customerData;
3748

3849
/**
39-
* @var \Magento\Framework\Filesystem\Directory\Write
50+
* @var DirectoryWrite
4051
*/
4152
protected $directoryWrite;
4253

4354
/**
44-
* @var \Magento\Customer\Model\Indexer\Processor
55+
* @var Processor
4556
*/
4657
private $indexerProcessor;
4758

59+
/**
60+
* @var CsvFactory
61+
*/
62+
private $csvFactory;
63+
64+
/**
65+
* @var WriteFactory
66+
*/
67+
private $writeFactory;
4868
/**
4969
* Create all necessary data for tests
5070
*/
5171
protected function setUp(): void
5272
{
5373
parent::setUp();
54-
55-
$this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
56-
->create(\Magento\CustomerImportExport\Model\Import\Customer::class);
74+
$this->objectManager = Bootstrap::getObjectManager();
75+
$this->_model = $this->objectManager->create(Customer::class);
5776
$this->_model->setParameters(['behavior' => Import::BEHAVIOR_ADD_UPDATE]);
58-
$this->indexerProcessor = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
59-
->create(\Magento\Customer\Model\Indexer\Processor::class);
60-
77+
$this->indexerProcessor = $this->objectManager->create(\Magento\Customer\Model\Indexer\Processor::class);
6178
$propertyAccessor = new \ReflectionProperty($this->_model, 'errorMessageTemplates');
6279
$propertyAccessor->setAccessible(true);
6380
$propertyAccessor->setValue($this->_model, []);
64-
6581
$this->_customerData = [
6682
'firstname' => 'Firstname',
6783
'lastname' => 'Lastname',
@@ -73,11 +89,10 @@ protected function setUp(): void
7389
'website_id' => 1,
7490
'password' => 'password',
7591
];
76-
77-
$filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
78-
->create(\Magento\Framework\Filesystem::class);
79-
$this->directoryWrite = $filesystem
80-
->getDirectoryWrite(DirectoryList::ROOT);
92+
$filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
93+
$this->directoryWrite = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
94+
$this->writeFactory = $this->objectManager->get(WriteFactory::class);
95+
$this->csvFactory = $this->objectManager->get(CsvFactory::class);
8196
}
8297

8398
/**
@@ -149,6 +164,93 @@ public function testImportData()
149164
);
150165
}
151166

167+
/**
168+
* Decompresses if gz compressed, stores in memory or temp file, and loads CSV adapter
169+
*
170+
* @param string $importData
171+
* @return Import\AbstractSource
172+
*/
173+
private function createImportAdapter(string $importData)
174+
{
175+
if (0 === strncmp("\x1f\x8b", $importData, 2)) { // gz's magic string
176+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
177+
$importData = gzdecode($importData);
178+
}
179+
$openedFile = $this->writeFactory->create('php://temp', '', 'w');
180+
$openedFile->write($importData);
181+
unset($importData);
182+
$directory = $this->directoryWrite;
183+
$adapter = $this->csvFactory->create(['directory' => $directory, 'file' => $openedFile]);
184+
return $adapter;
185+
}
186+
187+
/**
188+
* Test validateSource() and importData() and using same $ids between them
189+
*
190+
* @magentoDataFixture Magento/Customer/_files/import_export/customer.php
191+
*/
192+
public function testValidateSourceAndImportSource()
193+
{
194+
/** @var Import $import */
195+
$import = $this->objectManager->create(Import::class);
196+
$importData = \file_get_contents(__DIR__ . '/_files/2k_customers.csv.gz');
197+
$source = $this->createImportAdapter($importData);
198+
unset($importData);
199+
$import->setData([
200+
'form_key' => 'Ded3z8XBEaMWt3sH',
201+
'entity' => 'customer',
202+
'behavior' => 'add_update',
203+
'validation_strategy' => 'validation-stop-on-errors',
204+
'allowed_error_count' => '10',
205+
'_import_field_separator' => ',',
206+
'_import_multiple_value_separator' => ',',
207+
'_import_empty_attribute_value_constant' => '__EMPTY__VALUE__',
208+
'import_images_file_dir' => '',
209+
'_import_ids' => '',
210+
]);
211+
$import->validateSource($source);
212+
$ids = $import->getValidatedIds();
213+
$errorAggregator = $import->getErrorAggregator();
214+
$errorStrings = [];
215+
foreach ($errorAggregator->getAllErrors() as $error) {
216+
$errorStrings[] = sprintf(
217+
"Error:\nRowNumber: %s\nColumnName: %s\nCode: %s\nDescription: %s\nLevel: %s\nMessage: %s\n",
218+
(string)$error->getRowNumber(),
219+
$error->getColumnName(),
220+
$error->getErrorCode(),
221+
$error->getErrorDescription(),
222+
$error->getErrorLevel(),
223+
$error->getErrorMessage(),
224+
);
225+
}
226+
if (!empty($errorStrings)) {
227+
$exceptionString = sprintf(
228+
"Errors:\n%s\n",
229+
implode("\n", $errorStrings)
230+
);
231+
throw new \Exception($exceptionString);
232+
}
233+
$this->assertCount(20, $ids);
234+
/** @var Import $import2 */
235+
$import2 = $this->objectManager->create(Import::class);
236+
$import2->setData([
237+
'form_key' => 'DedGz8CNEaMWt3sH',
238+
'entity' => 'customer',
239+
'behavior' => 'add_update',
240+
'validation_strategy' => 'validation-stop-on-errors',
241+
'allowed_error_count' => '10',
242+
'_import_field_separator' => ',',
243+
'_import_multiple_value_separator' => ',',
244+
'_import_empty_attribute_value_constant' => '__EMPTY__VALUE__',
245+
'import_images_file_dir' => '',
246+
'_import_ids' => implode(',', $ids),
247+
]);
248+
$this->assertEmpty($import2->getValidatedIds());
249+
$import2->importSource();
250+
$createdItemsCount = $import2->getCreatedItemsCount();
251+
$this->assertEquals(2000, $createdItemsCount);
252+
}
253+
152254
/**
153255
* Tests importData() method.
154256
*

0 commit comments

Comments
 (0)