Skip to content

Commit 473182b

Browse files
committed
MC-38951: Images positions are inconsistent across store-views if images were added in a store-view level
- Fix images positions for default scope if image were added in store view level
1 parent f859282 commit 473182b

File tree

3 files changed

+163
-47
lines changed

3 files changed

+163
-47
lines changed

dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Gallery/UpdateHandlerTest.php

Lines changed: 76 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -594,70 +594,99 @@ public function updateImageDataProvider(): array
594594
}
595595

596596
/**
597-
* Tests that images positions are inconsistent across store-views if images were added in a store-view level
597+
* Tests that images are added correctly
598598
*
599599
* @magentoDataFixture Magento/Catalog/_files/product_with_image.php
600600
* @magentoDataFixture Magento/Store/_files/second_store.php
601+
* @dataProvider addImagesDataProvider
602+
* @param string $addFromStore
603+
* @param array $newImages
604+
* @param string $viewFromStore
605+
* @param array $expectedImages
606+
* @param array $select
601607
* @return void
602608
*/
603-
public function testAddImageInStoreView(): void
604-
{
605-
$secondStoreId = (int)$this->storeRepository->get('fixture_second_store')->getId();
606-
$existingImagePath = '/m/a/magento_image.jpg';
607-
$newImagePath = '/m/a/magento_small_image.jpg';
608-
$product = $this->getProduct($secondStoreId);
609+
public function testAddImages(
610+
string $addFromStore,
611+
array $newImages,
612+
string $viewFromStore,
613+
array $expectedImages,
614+
array $select = ['file', 'label', 'position']
615+
): void {
616+
$storeId = (int)$this->storeRepository->get($addFromStore)->getId();
617+
$product = $this->getProduct($storeId);
609618
$images = $product->getData('media_gallery')['images'];
610-
$newImage = [
611-
'file' => $newImagePath,
612-
'position' => 2,
613-
'label' => 'New Image Alt Text',
614-
'disabled' => 0,
615-
'media_type' => 'image'
616-
];
617-
$images[] = $newImage;
619+
$images = array_merge($images, $newImages);
618620
$product->setData('media_gallery', ['images' => $images]);
619621
$this->updateHandler->execute($product);
620-
$product = $this->getProduct(Store::DEFAULT_STORE_ID);
621-
$expectedImages = [
622-
[
623-
'file' => $existingImagePath,
624-
'label' => 'Image Alt Text',
625-
'position' => 1
626-
],
627-
[
628-
'file' => $newImagePath,
629-
'label' => null,
630-
'position' => 2
631-
],
632-
];
622+
$storeId = (int)$this->storeRepository->get($viewFromStore)->getId();
623+
$product = $this->getProduct($storeId);
633624
$actualImages = array_map(
634-
function (\Magento\Framework\DataObject $item) {
635-
return $item->toArray(['file', 'label', 'position']);
625+
function (\Magento\Framework\DataObject $item) use ($select) {
626+
return $item->toArray($select);
636627
},
637628
$product->getMediaGalleryImages()->getItems()
638629
);
639630
$this->assertEquals($expectedImages, array_values($actualImages));
640-
$product->cleanModelCache();
641-
$product = $this->getProduct($secondStoreId);
642-
$expectedImages = [
631+
}
632+
633+
/**
634+
* @return array[]
635+
*/
636+
public function addImagesDataProvider(): array
637+
{
638+
return [
643639
[
644-
'file' => $existingImagePath,
645-
'label' => 'Image Alt Text',
646-
'position' => 1
640+
'fixture_second_store',
641+
[
642+
[
643+
'file' => '/m/a/magento_small_image.jpg',
644+
'position' => 2,
645+
'label' => 'New Image Alt Text',
646+
'disabled' => 0,
647+
'media_type' => 'image'
648+
]
649+
],
650+
'default',
651+
[
652+
[
653+
'file' => '/m/a/magento_image.jpg',
654+
'label' => 'Image Alt Text',
655+
'position' => 1,
656+
],
657+
[
658+
'file' => '/m/a/magento_small_image.jpg',
659+
'label' => null,
660+
'position' => 2,
661+
],
662+
]
647663
],
648664
[
649-
'file' => $newImagePath,
650-
'label' => 'New Image Alt Text',
651-
'position' => 2
652-
],
665+
'fixture_second_store',
666+
[
667+
[
668+
'file' => '/m/a/magento_small_image.jpg',
669+
'position' => 2,
670+
'label' => 'New Image Alt Text',
671+
'disabled' => 0,
672+
'media_type' => 'image'
673+
]
674+
],
675+
'fixture_second_store',
676+
[
677+
[
678+
'file' => '/m/a/magento_image.jpg',
679+
'label' => 'Image Alt Text',
680+
'position' => 1,
681+
],
682+
[
683+
'file' => '/m/a/magento_small_image.jpg',
684+
'label' => 'New Image Alt Text',
685+
'position' => 2,
686+
],
687+
]
688+
]
653689
];
654-
$actualImages = array_map(
655-
function (\Magento\Framework\DataObject $item) {
656-
return $item->toArray(['file', 'label', 'position']);
657-
},
658-
$product->getMediaGalleryImages()->getItems()
659-
);
660-
$this->assertEquals($expectedImages, array_values($actualImages));
661690
}
662691

663692
/**

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3330,4 +3330,89 @@ public function testUpdateImageByNameNotPrefixedWithSlash()
33303330
$imageItems = $product->getMediaGalleryImages()->getItems();
33313331
$this->assertCount(0, $imageItems);
33323332
}
3333+
3334+
/**
3335+
* Tests that images are imported correctly
3336+
*
3337+
* @magentoDataFixture mediaImportImageFixture
3338+
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
3339+
* @magentoDataFixture Magento/Catalog/_files/product_with_image.php
3340+
* @dataProvider importImagesDataProvider
3341+
* @magentoAppIsolation enabled
3342+
* @param string $importFile
3343+
* @param string $productSku
3344+
* @param string $storeCode
3345+
* @param array $expectedImages
3346+
* @param array $select
3347+
*/
3348+
public function testImportImages(
3349+
string $importFile,
3350+
string $productSku,
3351+
string $storeCode,
3352+
array $expectedImages,
3353+
array $select = ['file', 'label', 'position']
3354+
): void {
3355+
$this->importDataForMediaTest($importFile);
3356+
$product = $this->getProductBySku($productSku, $storeCode);
3357+
$actualImages = array_map(
3358+
function (\Magento\Framework\DataObject $item) use ($select) {
3359+
return $item->toArray($select);
3360+
},
3361+
$product->getMediaGalleryImages()->getItems()
3362+
);
3363+
$this->assertEquals($expectedImages, array_values($actualImages));
3364+
}
3365+
3366+
/**
3367+
* @return array[]
3368+
*/
3369+
public function importImagesDataProvider(): array
3370+
{
3371+
return [
3372+
[
3373+
'import_media_additional_images_storeview.csv',
3374+
'simple',
3375+
'default',
3376+
[
3377+
[
3378+
'file' => '/m/a/magento_image.jpg',
3379+
'label' => 'Image Alt Text',
3380+
'position' => 1
3381+
],
3382+
[
3383+
'file' => '/m/a/magento_additional_image_one.jpg',
3384+
'label' => null,
3385+
'position' => 2
3386+
],
3387+
[
3388+
'file' => '/m/a/magento_additional_image_two.jpg',
3389+
'label' => null,
3390+
'position' => 3
3391+
],
3392+
]
3393+
],
3394+
[
3395+
'import_media_additional_images_storeview.csv',
3396+
'simple',
3397+
'fixturestore',
3398+
[
3399+
[
3400+
'file' => '/m/a/magento_image.jpg',
3401+
'label' => 'Image Alt Text',
3402+
'position' => 1
3403+
],
3404+
[
3405+
'file' => '/m/a/magento_additional_image_one.jpg',
3406+
'label' => 'Additional Image Label One',
3407+
'position' => 2
3408+
],
3409+
[
3410+
'file' => '/m/a/magento_additional_image_two.jpg',
3411+
'label' => 'Additional Image Label Two',
3412+
'position' => 3
3413+
],
3414+
]
3415+
]
3416+
];
3417+
}
33333418
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"sku","store_view_code","additional_images","additional_image_labels"
2+
"simple","fixturestore","magento_additional_image_one.jpg, magento_additional_image_two.jpg","Additional Image Label One,Additional Image Label Two"

0 commit comments

Comments
 (0)