Skip to content

Commit 8c91c87

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-64643' into BUGS
2 parents 84d382b + 9fe4516 commit 8c91c87

File tree

6 files changed

+79
-35
lines changed

6 files changed

+79
-35
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,12 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
409409
/**
410410
* Column names that holds images files names
411411
*
412+
* Note: the order of array items has a value in order to properly set 'position' value
413+
* of media gallery items.
414+
*
412415
* @var string[]
413416
*/
414-
protected $_imagesArrayKeys = ['_media_image', 'image', 'small_image', 'thumbnail', 'swatch_image'];
417+
protected $_imagesArrayKeys = ['image', 'small_image', 'thumbnail', 'swatch_image', '_media_image'];
415418

416419
/**
417420
* Permanent entity columns.
@@ -1627,8 +1630,14 @@ protected function _saveProducts()
16271630
);
16281631
}
16291632
$rowData[self::COL_MEDIA_IMAGE] = [];
1633+
1634+
/*
1635+
* Note: to avoid problems with undefined sorting, the value of media gallery items positions
1636+
* must be unique in scope of one product.
1637+
*/
1638+
$position = 0;
16301639
foreach ($rowImages as $column => $columnImages) {
1631-
foreach ($columnImages as $position => $columnImage) {
1640+
foreach ($columnImages as $columnImageKey => $columnImage) {
16321641
if (!isset($uploadedImages[$columnImage])) {
16331642
$uploadedFile = $this->uploadMediaFiles($columnImage, true);
16341643
if ($uploadedFile) {
@@ -1652,11 +1661,11 @@ protected function _saveProducts()
16521661

16531662
if ($uploadedFile && !isset($mediaGallery[$rowSku][$uploadedFile])) {
16541663
if (isset($existingImages[$rowSku][$uploadedFile])) {
1655-
if (isset($rowLabels[$column][$position])
1656-
&& $rowLabels[$column][$position] != $existingImages[$rowSku][$uploadedFile]['label']
1664+
if (isset($rowLabels[$column][$columnImageKey])
1665+
&& $rowLabels[$column][$columnImageKey] != $existingImages[$rowSku][$uploadedFile]['label']
16571666
) {
16581667
$labelsForUpdate[] = [
1659-
'label' => $rowLabels[$column][$position],
1668+
'label' => $rowLabels[$column][$columnImageKey],
16601669
'imageData' => $existingImages[$rowSku][$uploadedFile]
16611670
];
16621671
}
@@ -1666,8 +1675,8 @@ protected function _saveProducts()
16661675
}
16671676
$mediaGallery[$rowSku][$uploadedFile] = [
16681677
'attribute_id' => $this->getMediaGalleryAttributeId(),
1669-
'label' => isset($rowLabels[$column][$position]) ? $rowLabels[$column][$position] : '',
1670-
'position' => $position + 1,
1678+
'label' => isset($rowLabels[$column][$columnImageKey]) ? $rowLabels[$column][$columnImageKey] : '',
1679+
'position' => ++$position,
16711680
'disabled' => isset($disabledImages[$columnImage]) ? '1' : '0',
16721681
'value' => $uploadedFile,
16731682
];

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,8 @@ protected function getOptionValues(\Magento\Catalog\Model\Product\Option $option
562562
}
563563

564564
/**
565+
* Test that product import with images works properly
566+
*
565567
* @magentoDataIsolation enabled
566568
* @magentoDataFixture mediaImportImageFixture
567569
* @magentoAppIsolation enabled
@@ -570,40 +572,48 @@ protected function getOptionValues(\Magento\Catalog\Model\Product\Option $option
570572
public function testSaveMediaImage()
571573
{
572574
$this->importDataForMediaTest('import_media.csv');
575+
573576
$product = $this->getProductBySku('simple_new');
574577

578+
$this->assertEquals('/m/a/magento_image.jpg', $product->getData('image'));
579+
$this->assertEquals('/m/a/magento_small_image.jpg', $product->getData('small_image'));
580+
$this->assertEquals('/m/a/magento_thumbnail.jpg', $product->getData('thumbnail'));
575581
$this->assertEquals('/m/a/magento_image.jpg', $product->getData('swatch_image'));
582+
576583
$gallery = $product->getMediaGalleryImages();
577584
$this->assertInstanceOf(\Magento\Framework\Data\Collection::class, $gallery);
578-
$items = $gallery->getItems();
579-
$this->assertCount(1, $items);
580-
$item = array_pop($items);
581-
$this->assertInstanceOf(\Magento\Framework\DataObject::class, $item);
582-
$this->assertEquals('/m/a/magento_image.jpg', $item->getFile());
583-
$this->assertEquals('Image Label', $item->getLabel());
584-
}
585-
586-
/**
587-
* Test that image labels updates after import
588-
*
589-
* @magentoDataFixture mediaImportImageFixture
590-
* @magentoDataFixture Magento/Catalog/_files/product_with_image.php
591-
*/
592-
public function testUpdateImageLabel()
593-
{
594-
$this->importDataForMediaTest('import_media_update_label.csv');
595-
$product = $this->getProductBySku('simple');
596585

597-
$gallery = $product->getMediaGalleryImages();
598586
$items = $gallery->getItems();
599-
$this->assertCount(1, $items);
600-
$item = array_pop($items);
601-
$this->assertInstanceOf(\Magento\Framework\DataObject::class, $item);
602-
$this->assertEquals('Updated Image Label', $item->getLabel());
587+
$this->assertCount(5, $items);
588+
589+
$imageItem = array_shift($items);
590+
$this->assertInstanceOf(\Magento\Framework\DataObject::class, $imageItem);
591+
$this->assertEquals('/m/a/magento_image.jpg', $imageItem->getFile());
592+
$this->assertEquals('Image Label', $imageItem->getLabel());
593+
594+
$smallImageItem = array_shift($items);
595+
$this->assertInstanceOf(\Magento\Framework\DataObject::class, $smallImageItem);
596+
$this->assertEquals('/m/a/magento_small_image.jpg', $smallImageItem->getFile());
597+
$this->assertEquals('Small Image Label', $smallImageItem->getLabel());
598+
599+
$thumbnailItem = array_shift($items);
600+
$this->assertInstanceOf(\Magento\Framework\DataObject::class, $thumbnailItem);
601+
$this->assertEquals('/m/a/magento_thumbnail.jpg', $thumbnailItem->getFile());
602+
$this->assertEquals('Thumbnail Label', $thumbnailItem->getLabel());
603+
604+
$additionalImageOneItem = array_shift($items);
605+
$this->assertInstanceOf(\Magento\Framework\DataObject::class, $additionalImageOneItem);
606+
$this->assertEquals('/m/a/magento_additional_image_one.jpg', $additionalImageOneItem->getFile());
607+
$this->assertEquals('Additional Image Label One', $additionalImageOneItem->getLabel());
608+
609+
$additionalImageTwoItem = array_shift($items);
610+
$this->assertInstanceOf(\Magento\Framework\DataObject::class, $additionalImageTwoItem);
611+
$this->assertEquals('/m/a/magento_additional_image_two.jpg', $additionalImageTwoItem->getFile());
612+
$this->assertEquals('Additional Image Label Two', $additionalImageTwoItem->getLabel());
603613
}
604614

605615
/**
606-
* Copy a fixture image into media import directory
616+
* Copy fixture images into media import directory
607617
*/
608618
public static function mediaImportImageFixture()
609619
{
@@ -613,9 +623,36 @@ public static function mediaImportImageFixture()
613623
)->getDirectoryWrite(
614624
DirectoryList::MEDIA
615625
);
626+
616627
$mediaDirectory->create('import');
617628
$dirPath = $mediaDirectory->getAbsolutePath('import');
618-
copy(__DIR__ . '/../../../../Magento/Catalog/_files/magento_image.jpg', "{$dirPath}/magento_image.jpg");
629+
630+
$items = [
631+
[
632+
'source' => __DIR__ . '/../../../../Magento/Catalog/_files/magento_image.jpg',
633+
'dest' => $dirPath . '/magento_image.jpg',
634+
],
635+
[
636+
'source' => __DIR__ . '/../../../../Magento/Catalog/_files/magento_small_image.jpg',
637+
'dest' => $dirPath . '/magento_small_image.jpg',
638+
],
639+
[
640+
'source' => __DIR__ . '/../../../../Magento/Catalog/_files/magento_thumbnail.jpg',
641+
'dest' => $dirPath . '/magento_thumbnail.jpg',
642+
],
643+
[
644+
'source' => __DIR__ . '/_files/magento_additional_image_one.jpg',
645+
'dest' => $dirPath . '/magento_additional_image_one.jpg',
646+
],
647+
[
648+
'source' => __DIR__ . '/_files/magento_additional_image_two.jpg',
649+
'dest' => $dirPath . '/magento_additional_image_two.jpg',
650+
],
651+
];
652+
653+
foreach ($items as $item) {
654+
copy($item['source'], $item['dest']);
655+
}
619656
}
620657

621658
/**
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
sku,store_view_code,attribute_set_code,product_type,categories,product_websites,name,description,short_description,weight,product_online,tax_class_name,visibility,price,special_price,special_price_from_date,special_price_to_date,url_key,meta_title,meta_keywords,meta_description,base_image,base_image_label,small_image,small_image_label,thumbnail_image,thumbnail_image_label,swatch_image,swatch_image_label1,created_at,updated_at,new_from_date,new_to_date,display_product_options_in,map_price,msrp_price,map_enabled,gift_message_available,custom_design,custom_design_from,custom_design_to,custom_layout_update,page_layout,product_options_container,msrp_display_actual_price_type,country_of_manufacture,additional_attributes,qty,out_of_stock_qty,use_config_min_qty,is_qty_decimal,allow_backorders,use_config_backorders,min_cart_qty,use_config_min_sale_qty,max_cart_qty,use_config_max_sale_qty,is_in_stock,notify_on_stock_below,use_config_notify_stock_qty,manage_stock,use_config_manage_stock,use_config_qty_increments,qty_increments,use_config_enable_qty_inc,enable_qty_increments,is_decimal_divided,website_id,related_skus,crosssell_skus,upsell_skus,additional_images,additional_image_labels,hide_from_product_page,custom_options,bundle_price_type,bundle_sku_type,bundle_price_view,bundle_weight_type,bundle_values,associated_skus
2-
simple_new,,Default,simple,,base,New Product,,,,1,Taxable Goods,"Catalog, Search",10,,,,new-product,New Product,New Product,New Product ,magento_image.jpg,,magento_image.jpg,,magento_image.jpg,,magento_image.jpg,,10/20/15 07:05,10/20/15 07:05,,,Block after Info Column,,,,,,,,,,,,,"has_options=1,quantity_and_stock_status=In Stock,required_options=1",100,0,1,0,0,1,1,1,10000,1,1,1,1,1,0,1,1,0,0,0,1,,,,magento_image.jpg,Image Label,,,,,,,,
2+
simple_new,,Default,simple,,base,New Product,,,,1,Taxable Goods,"Catalog, Search",10,,,,new-product,New Product,New Product,New Product ,magento_image.jpg,Image Label,magento_small_image.jpg,Small Image Label,magento_thumbnail.jpg,Thumbnail Label,magento_image.jpg,Image Label,10/20/15 07:05,10/20/15 07:05,,,Block after Info Column,,,,,,,,,,,,,"has_options=1,quantity_and_stock_status=In Stock,required_options=1",100,0,1,0,0,1,1,1,10000,1,1,1,1,1,0,1,1,0,0,0,1,,,,"magento_additional_image_one.jpg, magento_additional_image_two.jpg","Additional Image Label One,Additional Image Label Two",,,,,,,,

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/_files/import_media_update_label.csv

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)