12
12
use Magento \Customer \Api \AddressRepositoryInterface ;
13
13
use Magento \Customer \Api \CustomerRepositoryInterface ;
14
14
use Magento \Customer \Api \Data \AddressInterface ;
15
+ use Magento \Customer \Api \Data \CustomerInterface ;
16
+ use Magento \Customer \Model \AddressRegistry ;
15
17
use Magento \Framework \Api \SearchCriteriaBuilder ;
16
18
use Magento \Framework \App \Filesystem \DirectoryList ;
17
19
use Magento \Framework \Filesystem ;
18
20
use Magento \ImportExport \Model \Import as ImportModel ;
19
21
use Magento \ImportExport \Model \Import \Adapter as ImportAdapter ;
22
+ use Magento \ImportExport \Model \Import \ErrorProcessing \ProcessingErrorAggregatorInterface ;
20
23
use Magento \ImportExport \Model \Import \Source \Csv ;
21
24
use Magento \TestFramework \Helper \Bootstrap ;
22
25
use Magento \Framework \Indexer \StateInterface ;
@@ -538,23 +541,15 @@ public function testCustomerIndexer(): void
538
541
/**
539
542
* Test import address with region for a country that does not have regions defined
540
543
*
544
+ * @magentoAppIsolation enabled
541
545
* @magentoDataFixture Magento/Customer/_files/import_export/customer_with_addresses.php
542
546
*/
543
547
public function testImportAddressWithOptionalRegion ()
544
548
{
545
- $ objectManager = Bootstrap::getObjectManager ();
546
- $ customerRepository = $ objectManager ->get (CustomerRepositoryInterface::class);
547
- $ customer = $ customerRepository ->get ('BetsyParker@example.com ' );
549
+ $ customer = $ this ->getCustomer ('BetsyParker@example.com ' );
548
550
$ 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 );
558
553
$ address = $ this ->getAddresses (
559
554
[
560
555
'parent_id ' => $ customer ->getId (),
@@ -566,6 +561,55 @@ public function testImportAddressWithOptionalRegion()
566
561
$ this ->assertEquals ('Liverpool ' , $ address [0 ]->getRegion ()->getRegion ());
567
562
}
568
563
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
+
569
613
/**
570
614
* Get Addresses by filter
571
615
*
@@ -584,4 +628,121 @@ private function getAddresses(array $filter): array
584
628
}
585
629
return $ repository ->getList ($ searchCriteriaBuilder ->create ())->getItems ();
586
630
}
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
+ }
587
748
}
0 commit comments