Skip to content

Commit 4ed0792

Browse files
committed
MAGETWO-90313: Import existing customer with only three columns will override customer group_id and store_id
1 parent 07c2590 commit 4ed0792

File tree

3 files changed

+88
-9
lines changed

3 files changed

+88
-9
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -378,19 +378,11 @@ protected function _prepareDataForUpdate(array $rowData)
378378
$this->_newCustomers[$emailInLowercase][$rowData[self::COLUMN_WEBSITE]] = $entityId;
379379
}
380380

381-
$entityRow = [
382-
'group_id' => empty($rowData['group_id']) ? self::DEFAULT_GROUP_ID : $rowData['group_id'],
383-
'store_id' => empty($rowData[self::COLUMN_STORE]) ? 0 : $this->_storeCodeToId[$rowData[self::COLUMN_STORE]],
384-
'created_at' => $createdAt->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT),
385-
'updated_at' => $now->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT),
386-
'entity_id' => $entityId,
387-
];
388-
389381
// password change/set
390382
if (isset($rowData['password']) && strlen($rowData['password'])) {
391383
$rowData['password_hash'] = $this->_customerModel->hashPassword($rowData['password']);
392384
}
393-
385+
$entityRow = ['entity_id' => $entityId];
394386
// attribute values
395387
foreach (array_intersect_key($rowData, $this->_attributes) as $attributeCode => $value) {
396388
$attributeParameters = $this->_attributes[$attributeCode];
@@ -429,12 +421,21 @@ protected function _prepareDataForUpdate(array $rowData)
429421

430422
if ($newCustomer) {
431423
// create
424+
$entityRow['group_id'] = empty($rowData['group_id']) ? self::DEFAULT_GROUP_ID : $rowData['group_id'];
425+
$entityRow['store_id'] = empty($rowData[self::COLUMN_STORE])
426+
? 0 : $this->_storeCodeToId[$rowData[self::COLUMN_STORE]];
427+
$entityRow['created_at'] = $createdAt->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
428+
$entityRow['updated_at'] = $now->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
432429
$entityRow['website_id'] = $this->_websiteCodeToId[$rowData[self::COLUMN_WEBSITE]];
433430
$entityRow['email'] = $emailInLowercase;
434431
$entityRow['is_active'] = 1;
435432
$entitiesToCreate[] = $entityRow;
436433
} else {
437434
// edit
435+
$entityRow['updated_at'] = $now->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
436+
if (!empty($rowData[self::COLUMN_STORE])) {
437+
$entityRow['store_id'] = $this->_storeCodeToId[$rowData[self::COLUMN_STORE]];
438+
}
438439
$entitiesToUpdate[] = $entityRow;
439440
}
440441

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,82 @@ public function testImportData()
135135
);
136136
}
137137

138+
/**
139+
* Tests importData() method.
140+
*
141+
* @magentoDataFixture Magento/Customer/_files/import_export/customer.php
142+
*
143+
* @return void
144+
*/
145+
public function testImportDataWithOneAdditionalColumn(): void
146+
{
147+
$source = new \Magento\ImportExport\Model\Import\Source\Csv(
148+
__DIR__ . '/_files/customer_to_import_with_one_additional_column.csv',
149+
$this->directoryWrite
150+
);
151+
152+
/** @var $customersCollection \Magento\Customer\Model\ResourceModel\Customer\Collection */
153+
$customersCollection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
154+
\Magento\Customer\Model\ResourceModel\Customer\Collection::class
155+
);
156+
$customersCollection->resetData();
157+
$customersCollection->clear();
158+
159+
$this->_model
160+
->setParameters(['behavior' => Import::BEHAVIOR_ADD_UPDATE])
161+
->setSource($source)
162+
->validateData()
163+
->hasToBeTerminated();
164+
sleep(1);
165+
$this->_model->importData();
166+
167+
$customers = $customersCollection->getItems();
168+
169+
/** @var $objectManager \Magento\TestFramework\ObjectManager */
170+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
171+
172+
$existingCustomer = $objectManager->get(\Magento\Framework\Registry::class)
173+
->registry('_fixture/Magento_ImportExport_Customer');
174+
175+
$updatedCustomer = $customers[$existingCustomer->getId()];
176+
177+
$this->assertNotEquals(
178+
$existingCustomer->getFirstname(),
179+
$updatedCustomer->getFirstname(),
180+
'Firstname must be changed'
181+
);
182+
183+
$this->assertNotEquals(
184+
$existingCustomer->getUpdatedAt(),
185+
$updatedCustomer->getUpdatedAt(),
186+
'Updated at date must be changed'
187+
);
188+
189+
$this->assertEquals(
190+
$existingCustomer->getLastname(),
191+
$updatedCustomer->getLastname(),
192+
'Lastname must not be changed'
193+
);
194+
195+
$this->assertEquals(
196+
$existingCustomer->getStoreId(),
197+
$updatedCustomer->getStoreId(),
198+
'Store Id must not be changed'
199+
);
200+
201+
$this->assertEquals(
202+
$existingCustomer->getCreatedAt(),
203+
$updatedCustomer->getCreatedAt(),
204+
'Creation date must not be changed'
205+
);
206+
207+
$this->assertEquals(
208+
$existingCustomer->getCustomerGroupId(),
209+
$updatedCustomer->getCustomerGroupId(),
210+
'Customer group must not be changed'
211+
);
212+
}
213+
138214
/**
139215
* Test importData() method (delete behavior)
140216
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
email,_website,firstname
2+
CharlesTAlston@teleworm.us,base,Jhon

0 commit comments

Comments
 (0)