@@ -626,6 +626,7 @@ function ($input) {
626626 explode (', ' , $ optionData )
627627 )
628628 );
629+ // phpcs:ignore Magento2.Performance.ForeachArrayMerge.ForeachArrayMerge
629630 $ option = array_merge (...$ option );
630631
631632 if (!empty ($ option ['type ' ]) && !empty ($ option ['name ' ])) {
@@ -692,12 +693,14 @@ protected function mergeWithExistingData(
692693 }
693694 } else {
694695 $ existingOptionId = array_search ($ optionKey , $ expectedOptions );
696+ // phpcs:ignore Magento2.Performance.ForeachArrayMerge.ForeachArrayMerge
695697 $ expectedData [$ existingOptionId ] = array_merge (
696698 $ this ->getOptionData ($ option ),
697699 $ expectedData [$ existingOptionId ]
698700 );
699701 if ($ optionValues ) {
700702 foreach ($ optionValues as $ optionKey => $ optionValue ) {
703+ // phpcs:ignore Magento2.Performance.ForeachArrayMerge.ForeachArrayMerge
701704 $ expectedValues [$ existingOptionId ][$ optionKey ] = array_merge (
702705 $ optionValue ,
703706 $ expectedValues [$ existingOptionId ][$ optionKey ]
@@ -842,6 +845,58 @@ public function testSaveMediaImage()
842845 $ this ->assertEquals ('Additional Image Label Two ' , $ additionalImageTwoItem ->getLabel ());
843846 }
844847
848+ /**
849+ * Test that new images should be added after the existing ones.
850+ *
851+ * @magentoDataFixture mediaImportImageFixture
852+ * @magentoAppIsolation enabled
853+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
854+ */
855+ public function testNewImagesShouldBeAddedAfterExistingOnes ()
856+ {
857+ $ this ->importDataForMediaTest ('import_media.csv ' );
858+
859+ $ product = $ this ->getProductBySku ('simple_new ' );
860+
861+ $ items = array_values ($ product ->getMediaGalleryImages ()->getItems ());
862+
863+ $ images = [
864+ ['file ' => '/m/a/magento_image.jpg ' , 'label ' => 'Image Label ' ],
865+ ['file ' => '/m/a/magento_small_image.jpg ' , 'label ' => 'Small Image Label ' ],
866+ ['file ' => '/m/a/magento_thumbnail.jpg ' , 'label ' => 'Thumbnail Label ' ],
867+ ['file ' => '/m/a/magento_additional_image_one.jpg ' , 'label ' => 'Additional Image Label One ' ],
868+ ['file ' => '/m/a/magento_additional_image_two.jpg ' , 'label ' => 'Additional Image Label Two ' ],
869+ ];
870+
871+ $ this ->assertCount (5 , $ items );
872+ $ this ->assertEquals (
873+ $ images ,
874+ array_map (
875+ function (\Magento \Framework \DataObject $ item ) {
876+ return $ item ->toArray (['file ' , 'label ' ]);
877+ },
878+ $ items
879+ )
880+ );
881+
882+ $ this ->importDataForMediaTest ('import_media_additional_images.csv ' );
883+ $ product ->cleanModelCache ();
884+ $ product = $ this ->getProductBySku ('simple_new ' );
885+ $ items = array_values ($ product ->getMediaGalleryImages ()->getItems ());
886+ $ images [] = ['file ' => '/m/a/magento_additional_image_three.jpg ' , 'label ' => '' ];
887+ $ images [] = ['file ' => '/m/a/magento_additional_image_four.jpg ' , 'label ' => '' ];
888+ $ this ->assertCount (7 , $ items );
889+ $ this ->assertEquals (
890+ $ images ,
891+ array_map (
892+ function (\Magento \Framework \DataObject $ item ) {
893+ return $ item ->toArray (['file ' , 'label ' ]);
894+ },
895+ $ items
896+ )
897+ );
898+ }
899+
845900 /**
846901 * Test that errors occurred during importing images are logged.
847902 *
@@ -892,6 +947,14 @@ public static function mediaImportImageFixture()
892947 'source ' => __DIR__ . '/_files/magento_additional_image_two.jpg ' ,
893948 'dest ' => $ dirPath . '/magento_additional_image_two.jpg ' ,
894949 ],
950+ [
951+ 'source ' => __DIR__ . '/_files/magento_additional_image_three.jpg ' ,
952+ 'dest ' => $ dirPath . '/magento_additional_image_three.jpg ' ,
953+ ],
954+ [
955+ 'source ' => __DIR__ . '/_files/magento_additional_image_four.jpg ' ,
956+ 'dest ' => $ dirPath . '/magento_additional_image_four.jpg ' ,
957+ ],
895958 ];
896959
897960 foreach ($ items as $ item ) {
@@ -2023,7 +2086,17 @@ private function importDataForMediaTest(string $fileName, int $expectedErrors =
20232086 $ this ->assertTrue ($ errors ->getErrorsCount () == 0 );
20242087
20252088 $ this ->_model ->importData ();
2026- $ this ->assertTrue ($ this ->_model ->getErrorAggregator ()->getErrorsCount () == $ expectedErrors );
2089+ $ this ->assertEquals (
2090+ $ expectedErrors ,
2091+ $ this ->_model ->getErrorAggregator ()->getErrorsCount (),
2092+ array_reduce (
2093+ $ this ->_model ->getErrorAggregator ()->getAllErrors (),
2094+ function ($ output , $ error ) {
2095+ return "$ output \n{$ error ->getErrorMessage ()}" ;
2096+ },
2097+ ''
2098+ )
2099+ );
20272100 }
20282101
20292102 /**
0 commit comments