Skip to content

Commit 48ca677

Browse files
committed
Merge remote-tracking branch 'mpi/MC-35080' into PR-2020-07-22
2 parents aff7fcb + a6c9775 commit 48ca677

File tree

2 files changed

+177
-17
lines changed
  • app/code/Magento/CustomerImportExport/Model/Import
  • dev/tests/integration/testsuite/Magento/CustomerImportExport/Model/Import

2 files changed

+177
-17
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -661,8 +661,6 @@ protected function _prepareDataForUpdate(array $rowData):array
661661
$defaults[$table][$customerId][$attributeCode] = $addressId;
662662
}
663663
}
664-
// let's try to find region ID
665-
$entityRow[self::COLUMN_REGION_ID] = null;
666664

667665
if (!empty($entityRow[self::COLUMN_REGION]) && !empty($entityRow[self::COLUMN_COUNTRY_ID])) {
668666
$entityRow[self::COLUMN_REGION_ID] = $this->getCountryRegionId(
@@ -673,6 +671,8 @@ protected function _prepareDataForUpdate(array $rowData):array
673671
$entityRow[self::COLUMN_REGION] = $entityRow[self::COLUMN_REGION_ID] !== null
674672
? $this->_regions[$entityRow[self::COLUMN_REGION_ID]]
675673
: $entityRow[self::COLUMN_REGION];
674+
} elseif ($newAddress) {
675+
$entityRow[self::COLUMN_REGION_ID] = null;
676676
}
677677
if ($newAddress) {
678678
$entityRowNew = $entityRow;
@@ -902,7 +902,7 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
902902
}
903903
}
904904

905-
if (isset($rowData[self::COLUMN_COUNTRY_ID])) {
905+
if (!empty($rowData[self::COLUMN_COUNTRY_ID])) {
906906
if (isset($rowData[self::COLUMN_POSTCODE])
907907
&& !$this->postcodeValidator->isValid(
908908
$rowData[self::COLUMN_COUNTRY_ID],
@@ -912,8 +912,7 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
912912
$this->addRowError(self::ERROR_VALUE_IS_REQUIRED, $rowNumber, self::COLUMN_POSTCODE);
913913
}
914914

915-
if (isset($rowData[self::COLUMN_REGION])
916-
&& !empty($rowData[self::COLUMN_REGION])
915+
if (!empty($rowData[self::COLUMN_REGION])
917916
&& count($this->getCountryRegions($rowData[self::COLUMN_COUNTRY_ID])) > 0
918917
&& null === $this->getCountryRegionId(
919918
$rowData[self::COLUMN_COUNTRY_ID],

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

Lines changed: 173 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
use Magento\Customer\Api\AddressRepositoryInterface;
1313
use Magento\Customer\Api\CustomerRepositoryInterface;
1414
use Magento\Customer\Api\Data\AddressInterface;
15+
use Magento\Customer\Api\Data\CustomerInterface;
16+
use Magento\Customer\Model\AddressRegistry;
1517
use Magento\Framework\Api\SearchCriteriaBuilder;
1618
use Magento\Framework\App\Filesystem\DirectoryList;
1719
use Magento\Framework\Filesystem;
1820
use Magento\ImportExport\Model\Import as ImportModel;
1921
use Magento\ImportExport\Model\Import\Adapter as ImportAdapter;
22+
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
2023
use Magento\ImportExport\Model\Import\Source\Csv;
2124
use Magento\TestFramework\Helper\Bootstrap;
2225
use Magento\Framework\Indexer\StateInterface;
@@ -538,23 +541,15 @@ public function testCustomerIndexer(): void
538541
/**
539542
* Test import address with region for a country that does not have regions defined
540543
*
544+
* @magentoAppIsolation enabled
541545
* @magentoDataFixture Magento/Customer/_files/import_export/customer_with_addresses.php
542546
*/
543547
public function testImportAddressWithOptionalRegion()
544548
{
545-
$objectManager = Bootstrap::getObjectManager();
546-
$customerRepository = $objectManager->get(CustomerRepositoryInterface::class);
547-
$customer = $customerRepository->get('BetsyParker@example.com');
549+
$customer = $this->getCustomer('BetsyParker@example.com');
548550
$file = __DIR__ . '/_files/import_uk_address.csv';
549-
$filesystem = Bootstrap::getObjectManager()->create(Filesystem::class);
550-
$directoryWrite = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
551-
$source = new Csv($file, $directoryWrite);
552-
$errors = $this->_entityAdapter
553-
->setParameters(['behavior' => ImportModel::BEHAVIOR_ADD_UPDATE])
554-
->setSource($source)
555-
->validateData();
556-
$this->assertEmpty($errors->getAllErrors(), 'Import validation failed');
557-
$this->_entityAdapter->importData();
551+
$errors = $this->doImport($file);
552+
$this->assertImportValidationPassed($errors);
558553
$address = $this->getAddresses(
559554
[
560555
'parent_id' => $customer->getId(),
@@ -566,6 +561,55 @@ public function testImportAddressWithOptionalRegion()
566561
$this->assertEquals('Liverpool', $address[0]->getRegion()->getRegion());
567562
}
568563

564+
/**
565+
* Test update first name and last name
566+
*
567+
* @magentoAppIsolation enabled
568+
* @magentoDataFixture Magento/Customer/_files/import_export/customer_with_addresses.php
569+
*/
570+
public function testUpdateFirstAndLastName()
571+
{
572+
$customer = $this->getCustomer('BetsyParker@example.com');
573+
$addresses = $this->getAddresses(
574+
[
575+
'parent_id' => $customer->getId(),
576+
]
577+
);
578+
$this->assertCount(1, $addresses);
579+
$address = $addresses[0];
580+
$row = [
581+
'_website' => 'base',
582+
'_email' => $customer->getEmail(),
583+
'_entity_id' => $address->getId(),
584+
'firstname' => 'Mark',
585+
'lastname' => 'Antony',
586+
];
587+
$file = $this->generateImportFile([$row]);
588+
$errors = $this->doImport($file);
589+
$this->assertImportValidationPassed($errors);
590+
$objectManager = Bootstrap::getObjectManager();
591+
//clear cache
592+
$objectManager->get(AddressRegistry::class)->remove($address->getId());
593+
$addresses = $this->getAddresses(
594+
[
595+
'parent_id' => $customer->getId(),
596+
'entity_id' => $address->getId(),
597+
]
598+
);
599+
$this->assertCount(1, $addresses);
600+
$updatedAddress = $addresses[0];
601+
//assert that firstname and lastname were updated
602+
$this->assertEquals($row['firstname'], $updatedAddress->getFirstname());
603+
$this->assertEquals($row['lastname'], $updatedAddress->getLastname());
604+
//assert other values have not changed
605+
$this->assertEquals($address->getStreet(), $updatedAddress->getStreet());
606+
$this->assertEquals($address->getCity(), $updatedAddress->getCity());
607+
$this->assertEquals($address->getCountryId(), $updatedAddress->getCountryId());
608+
$this->assertEquals($address->getPostcode(), $updatedAddress->getPostcode());
609+
$this->assertEquals($address->getTelephone(), $updatedAddress->getTelephone());
610+
$this->assertEquals($address->getRegionId(), $updatedAddress->getRegionId());
611+
}
612+
569613
/**
570614
* Get Addresses by filter
571615
*
@@ -584,4 +628,121 @@ private function getAddresses(array $filter): array
584628
}
585629
return $repository->getList($searchCriteriaBuilder->create())->getItems();
586630
}
631+
632+
/**
633+
* @param string $email
634+
* @return CustomerInterface
635+
*/
636+
private function getCustomer(string $email): CustomerInterface
637+
{
638+
$objectManager = Bootstrap::getObjectManager();
639+
$customerRepository = $objectManager->get(CustomerRepositoryInterface::class);
640+
return $customerRepository->get($email);
641+
}
642+
643+
/**
644+
* @param string $file
645+
* @param string $behavior
646+
* @param bool $validateOnly
647+
* @return ProcessingErrorAggregatorInterface
648+
*/
649+
private function doImport(
650+
string $file,
651+
string $behavior = ImportModel::BEHAVIOR_ADD_UPDATE,
652+
bool $validateOnly = false
653+
): ProcessingErrorAggregatorInterface {
654+
$objectManager = Bootstrap::getObjectManager();
655+
/** @var Filesystem $filesystem */
656+
$filesystem = $objectManager->create(Filesystem::class);
657+
$directoryWrite = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
658+
$source = ImportAdapter::findAdapterFor($file, $directoryWrite);
659+
$errors = $this->_entityAdapter
660+
->setParameters(['behavior' => $behavior])
661+
->setSource($source)
662+
->validateData();
663+
if (!$validateOnly && !$errors->getAllErrors()) {
664+
$this->_entityAdapter->importData();
665+
}
666+
return $errors;
667+
}
668+
669+
/**
670+
* @param array $data
671+
* @return string
672+
*/
673+
private function generateImportFile(array $data): string
674+
{
675+
$objectManager = Bootstrap::getObjectManager();
676+
/** @var Filesystem $filesystem */
677+
$filesystem = $objectManager->get(Filesystem::class);
678+
$tmpDir = $filesystem->getDirectoryWrite(DirectoryList::TMP);
679+
$tmpFilename = uniqid('test_import_address_') . '.csv';
680+
$stream = $tmpDir->openFile($tmpFilename, 'w+');
681+
$stream->lock();
682+
$stream->writeCsv($this->getFields());
683+
$emptyRow = array_fill_keys($this->getFields(), '');
684+
foreach ($data as $row) {
685+
$row = array_replace($emptyRow, $row);
686+
$stream->writeCsv($row);
687+
}
688+
$stream->unlock();
689+
$stream->close();
690+
return $tmpDir->getAbsolutePath($tmpFilename);
691+
}
692+
693+
/**
694+
* @param ProcessingErrorAggregatorInterface $errors
695+
*/
696+
private function assertImportValidationPassed(ProcessingErrorAggregatorInterface $errors): void
697+
{
698+
if ($errors->getAllErrors()) {
699+
$messages = [];
700+
$messages[] = 'Import validation failed';
701+
$messages[] = '';
702+
foreach ($errors->getAllErrors() as $error) {
703+
$messages[] = sprintf(
704+
'%s: #%d [%s] %s: %s',
705+
strtoupper($error->getErrorLevel()),
706+
$error->getRowNumber(),
707+
$error->getErrorCode(),
708+
$error->getErrorMessage(),
709+
$error->getErrorDescription()
710+
);
711+
}
712+
$this->fail(implode("\n", $messages));
713+
}
714+
}
715+
716+
/**
717+
* @return array
718+
*/
719+
private function getFields(): array
720+
{
721+
return [
722+
'_website',
723+
'_email',
724+
'_entity_id',
725+
'city',
726+
'company',
727+
'country_id',
728+
'fax',
729+
'firstname',
730+
'lastname',
731+
'middlename',
732+
'postcode',
733+
'prefix',
734+
'region',
735+
'region_id',
736+
'street',
737+
'suffix',
738+
'telephone',
739+
'vat_id',
740+
'vat_is_valid',
741+
'vat_request_date',
742+
'vat_request_id',
743+
'vat_request_success',
744+
'_address_default_billing_',
745+
'_address_default_shipping_',
746+
];
747+
}
587748
}

0 commit comments

Comments
 (0)