Skip to content

Commit 97c367f

Browse files
author
Oleksandr Gorkun
committed
MAGETWO-83426: [Performance] Customer Import check data does not complete
1 parent be6cc2a commit 97c367f

File tree

7 files changed

+31
-89
lines changed

7 files changed

+31
-89
lines changed

app/code/Magento/CustomerImportExport/Model/Import/AbstractCustomer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,12 @@ protected function _getCustomerId($email, $websiteCode)
174174
}
175175

176176
/**
177-
* Pre-loading customers for existing customers checks.
177+
* Pre-loading customers for existing customers checks in order
178+
* to perform mass validation/import efficiently.
179+
*
180+
* @param \Traversable $rows Each row must contain data from columns email
181+
* and website code.
178182
*
179-
* @param \Traversable $rows
180183
* @return void
181184
*/
182185
public function prepareCustomerData(\Traversable $rows)

app/code/Magento/CustomerImportExport/Model/Import/Address.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
1313
use Magento\Store\Model\Store;
1414
use Magento\ImportExport\Model\Import;
15-
use Zend\Stdlib\ArrayObject;
1615

1716
/**
1817
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -457,7 +456,7 @@ protected function _getNextEntityId()
457456
*
458457
* @return $this
459458
* @deprecated
460-
* @see prepareC
459+
* @see prepareCustomerData
461460
*/
462461
protected function _initAddresses()
463462
{
@@ -540,28 +539,16 @@ protected function _initCountryRegions()
540539
*/
541540
protected function _importData()
542541
{
542+
//Preparing data for mass validation/import.
543543
$rows = [];
544-
$customersData = [];
545544
while ($bunch = $this->_dataSourceModel->getNextBunch()) {
546545
$rows = array_merge($rows, $bunch);
547-
$customersData = array_merge(
548-
$customersData,
549-
array_map(
550-
function ($row) {
551-
return [
552-
'email' => $row[self::COLUMN_EMAIL],
553-
'website_id' => $this->getWebsiteId(
554-
self::COLUMN_WEBSITE
555-
)
556-
];
557-
},
558-
$bunch
559-
)
560-
);
561546
}
562-
$this->prepareCustomerData(new ArrayObject($rows));
547+
$this->prepareCustomerData(new \ArrayObject($rows));
563548
unset($bunch, $rows);
564549
$this->_dataSourceModel->getIterator()->rewind();
550+
551+
//Importing
565552
while ($bunch = $this->_dataSourceModel->getNextBunch()) {
566553
$newRows = [];
567554
$updateRows = [];

app/code/Magento/CustomerImportExport/Model/Import/Customer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
use Magento\Customer\Api\Data\CustomerInterface;
99
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
10-
use Zend\Stdlib\ArrayObject;
1110

1211
/**
1312
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -429,7 +428,7 @@ protected function _prepareDataForUpdate(array $rowData)
429428
protected function _importData()
430429
{
431430
while ($bunch = $this->_dataSourceModel->getNextBunch()) {
432-
$this->prepareCustomerData(new ArrayObject($bunch));
431+
$this->prepareCustomerData(new \ArrayObject($bunch));
433432
$entitiesToCreate = [];
434433
$entitiesToUpdate = [];
435434
$entitiesToDelete = [];

app/code/Magento/CustomerImportExport/Model/Import/CustomerComposite.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
*/
66
namespace Magento\CustomerImportExport\Model\Import;
77

8-
use Magento\Framework\DataObject;
98
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
10-
use Zend\Stdlib\ArrayObject;
119

1210
/**
1311
* Import entity customer combined model
@@ -294,6 +292,7 @@ public function getEntityTypeCode()
294292
*/
295293
public function validateData()
296294
{
295+
//Preparing both customer and address imports for mass validation.
297296
$source = $this->getSource();
298297
$this->_customerEntity->prepareCustomerData($source);
299298
$source->rewind();
@@ -305,7 +304,7 @@ public function validateData()
305304
];
306305
}
307306
$source->rewind();
308-
$this->_addressEntity->prepareCustomerData(new ArrayObject($rows));
307+
$this->_addressEntity->prepareCustomerData(new \ArrayObject($rows));
309308

310309
return parent::validateData();
311310
}

app/code/Magento/CustomerImportExport/Model/ResourceModel/Import/Customer/Storage.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,22 @@ public function addCustomer(DataObject $customer)
143143
if (!isset($this->_customerIds[$email])) {
144144
$this->_customerIds[$email] = [];
145145
}
146-
$this->_customerIds[$email][$customer->getWebsiteId()] = $customer->getId();
146+
$this->_customerIds[$email][$customer->getWebsiteId()]
147+
= $customer->getId();
147148

148149
return $this;
149150
}
150151

151152
/**
152-
* Get customer id
153+
* Find customer ID for unique pair of email and website ID.
153154
*
154155
* @param string $email
155156
* @param int $websiteId
156157
* @return bool|int
157158
*/
158159
public function getCustomerId($email, $websiteId)
159160
{
161+
$email = mb_strtolower($email);
160162
//Trying to load the customer.
161163
if (!array_key_exists($email, $this->_customerIds)
162164
|| !array_key_exists($websiteId, $this->_customerIds[$email])
@@ -196,16 +198,19 @@ public function prepareCustomers(array $customersToFind)
196198
$customersData = [];
197199
$filters = [];
198200
foreach ($customersToFind as $customerToFind) {
199-
$email = $customerToFind['email'];
201+
$email = mb_strtolower($customerToFind['email']);
200202
$websiteId = $customerToFind['website_id'];
201203
if (!array_key_exists($email, $this->_customerIds)
202204
|| !array_key_exists($websiteId, $this->_customerIds[$email])
203205
) {
204206
//Only looking for customers we don't already have ID for.
205-
$customersData[] = $customerToFind;
207+
$customersData[] = [
208+
'email' => $email,
209+
'website_id' => $websiteId
210+
];
206211
$filters[] = $this->filterBuilder
207212
->setField('email')
208-
->setValue($customerToFind['email'])
213+
->setValue($email)
209214
->setConditionType('eq')
210215
->create();
211216
}

dev/tests/integration/testsuite/Magento/Customer/_files/import_export/customers_for_address_import.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
* See COPYING.txt for license details.
55
*/
66
//Create customer
7-
$customer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\Customer\Model\Customer');
7+
/** @var Magento\Customer\Model\Customer $customer */
8+
$customer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
9+
->create('Magento\Customer\Model\Customer');
810
$customer->setWebsiteId(
911
0
1012
)->setEntityId(
@@ -69,5 +71,4 @@
6971
$addressSecond->isObjectNew(true);
7072
$customer->addAddress($addressSecond);
7173
$customer->setDefaultShipping($addressSecond->getId());
72-
$customer->isObjectNew(true);
7374
$customer->save();

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

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ public function testConstruct()
136136
$this->_entityAdapter,
137137
'Addresses must be an array.'
138138
);
139-
$this->assertAttributeNotEmpty('_addresses', $this->_entityAdapter, 'Addresses must not be empty');
139+
$this->assertAttributeEmpty(
140+
'_addresses',
141+
$this->_entityAdapter,
142+
'Addresses must not be loaded in constructor'
143+
);
140144

141145
// check country regions and regions
142146
$this->assertAttributeInternalType(
@@ -151,62 +155,6 @@ public function testConstruct()
151155
$this->assertAttributeNotEmpty('_regions', $this->_entityAdapter, 'Regions must not be empty');
152156
}
153157

154-
/**
155-
* Test _initAddresses
156-
*
157-
* @magentoDataFixture Magento/Customer/_files/import_export/customer_with_addresses.php
158-
*/
159-
public function testInitAddresses()
160-
{
161-
/** @var $objectManager \Magento\TestFramework\ObjectManager */
162-
$objectManager = Bootstrap::getObjectManager();
163-
164-
// get addressed from fixture
165-
$customers = $objectManager->get('Magento\Framework\Registry')->registry($this->_fixtureKey);
166-
$correctAddresses = [];
167-
/** @var $customer \Magento\Customer\Model\Customer */
168-
foreach ($customers as $customer) {
169-
$correctAddresses[$customer->getId()] = [];
170-
/** @var $address \Magento\Customer\Model\Address */
171-
foreach ($customer->getAddressesCollection() as $address) {
172-
$correctAddresses[$customer->getId()][] = $address->getId();
173-
}
174-
}
175-
176-
// invoke _initAddresses
177-
$initAddresses = new \ReflectionMethod($this->_testClassName, '_initAddresses');
178-
$initAddresses->setAccessible(true);
179-
$initAddresses->invoke($this->_entityAdapter);
180-
181-
// check addresses
182-
$this->assertAttributeInternalType(
183-
'array',
184-
'_addresses',
185-
$this->_entityAdapter,
186-
'Addresses must be an array.'
187-
);
188-
$this->assertAttributeNotEmpty('_addresses', $this->_entityAdapter, 'Addresses must not be empty');
189-
190-
$addressesReflection = new \ReflectionProperty($this->_testClassName, '_addresses');
191-
$addressesReflection->setAccessible(true);
192-
$testAddresses = $addressesReflection->getValue($this->_entityAdapter);
193-
194-
$correctCustomerIds = array_keys($correctAddresses);
195-
$testCustomerIds = array_keys($testAddresses);
196-
sort($correctCustomerIds);
197-
sort($testCustomerIds);
198-
$this->assertEquals($correctCustomerIds, $testCustomerIds, 'Incorrect customer IDs in addresses array.');
199-
200-
foreach ($correctCustomerIds as $customerId) {
201-
$this->assertInternalType('array', $correctAddresses[$customerId], 'Addresses must be an array.');
202-
$correctAddressIds = $correctAddresses[$customerId];
203-
$testAddressIds = $testAddresses[$customerId];
204-
sort($correctAddressIds);
205-
sort($testAddressIds);
206-
$this->assertEquals($correctAddressIds, $testAddressIds, 'Incorrect addresses IDs.');
207-
}
208-
}
209-
210158
/**
211159
* Test _saveAddressEntity
212160
*

0 commit comments

Comments
 (0)