Skip to content

Commit cc728fa

Browse files
MC-20660: Admin: Assign/delete image(s) from simple product in single/multiple store views mode
1 parent 9f55a6f commit cc728fa

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

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

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class UpdateHandlerTest extends \PHPUnit\Framework\TestCase
7373
* @var string
7474
*/
7575
private $fileName;
76+
private $mediaAttributeId;
7677

7778
/**
7879
* @inheritdoc
@@ -86,6 +87,7 @@ protected function setUp()
8687
$this->storeRepository = $this->objectManager->create(StoreRepositoryInterface::class);
8788
$this->galleryResource = $this->objectManager->create(Gallery::class);
8889
$this->productResource = $this->objectManager->create(ProductResource::class);
90+
$this->mediaAttributeId = $this->productResource->getAttribute('media_gallery')->getAttributeId();
8991
$this->config = $this->objectManager->get(Config::class);
9092
$this->mediaDirectory = $this->objectManager->get(Filesystem::class)
9193
->getDirectoryWrite(DirectoryList::MEDIA);
@@ -109,7 +111,7 @@ public function testExecuteWithIllegalFilename(): void
109111
'images' => [
110112
'image' => [
111113
'value_id' => '100',
112-
'file' => str_repeat('/..', 2) . DIRECTORY_SEPARATOR . $this->fileName,
114+
'file' => '/../..' . DIRECTORY_SEPARATOR . $this->fileName,
113115
'label' => 'New image',
114116
'removed' => 1,
115117
],
@@ -130,13 +132,11 @@ public function testExecuteWithIllegalFilename(): void
130132
public function testExecuteWithOneImage(): void
131133
{
132134
$product = $this->getProduct();
133-
$this->prepareProductWithOneImage($product, ['label' => 'New image', 'disabled' => '1']);
135+
$this->updateProductGalleryImages($product, ['label' => 'New image', 'disabled' => '1']);
134136
$this->updateHandler->execute($product);
135-
$productImages = $this->galleryResource->loadProductGalleryByAttributeId(
136-
$product,
137-
$this->productResource->getAttribute('media_gallery')->getAttributeId()
138-
);
137+
$productImages = $this->galleryResource->loadProductGalleryByAttributeId($product, $this->mediaAttributeId);
139138
$updatedImage = reset($productImages);
139+
$this->assertTrue(is_array($updatedImage));
140140
$this->assertEquals('New image', $updatedImage['label']);
141141
$this->assertEquals('New image', $updatedImage['label_default']);
142142
$this->assertEquals('1', $updatedImage['disabled']);
@@ -248,8 +248,7 @@ public function testExecuteWithTwoImagesAndChangedPosition(): void
248248
$product->setData('store_id', Store::DEFAULT_STORE_ID);
249249
$product->setData('media_gallery', ['images' => $images]);
250250
$this->updateHandler->execute($product);
251-
$galleryAttributeId = $this->productResource->getAttribute('media_gallery')->getAttributeId();
252-
$productImages = $this->galleryResource->loadProductGalleryByAttributeId($product, $galleryAttributeId);
251+
$productImages = $this->galleryResource->loadProductGalleryByAttributeId($product, $this->mediaAttributeId);
253252
foreach ($productImages as $updatedImage) {
254253
$this->assertEquals($positionMap[$updatedImage['file']], $updatedImage['position']);
255254
$this->assertEquals($positionMap[$updatedImage['file']], $updatedImage['position_default']);
@@ -266,12 +265,9 @@ public function testExecuteWithTwoImagesAndChangedPosition(): void
266265
public function testExecuteWithImageToDelete(): void
267266
{
268267
$product = $this->getProduct();
269-
$this->prepareProductWithOneImage($product, ['removed' => '1']);
268+
$this->updateProductGalleryImages($product, ['removed' => '1']);
270269
$this->updateHandler->execute($product);
271-
$productImages = $this->galleryResource->loadProductGalleryByAttributeId(
272-
$product,
273-
$this->productResource->getAttribute('media_gallery')->getAttributeId()
274-
);
270+
$productImages = $this->galleryResource->loadProductGalleryByAttributeId($product, $this->mediaAttributeId);
275271
$this->assertCount(0, $productImages);
276272
$this->assertFileNotExists(
277273
$this->mediaDirectory->getAbsolutePath($this->config->getBaseMediaPath() . '/m/a/magento_image.jpg')
@@ -324,8 +320,7 @@ public function testExecuteWithTwoImagesOnStoreView(): void
324320
}
325321
$product->setData('media_gallery', ['images' => $images]);
326322
$this->updateHandler->execute($product);
327-
$galleryAttributeId = $this->productResource->getAttribute('media_gallery')->getAttributeId();
328-
$productImages = $this->galleryResource->loadProductGalleryByAttributeId($product, $galleryAttributeId);
323+
$productImages = $this->galleryResource->loadProductGalleryByAttributeId($product, $this->mediaAttributeId);
329324
foreach ($productImages as $image) {
330325
$imageToAssert = [
331326
'label' => $image['label'],
@@ -363,10 +358,10 @@ private function getProduct(?int $storeId = null): ProductInterface
363358
* @param array $imageData
364359
* @return void
365360
*/
366-
private function prepareProductWithOneImage(ProductInterface $product, array $imageData): void
361+
private function updateProductGalleryImages(ProductInterface $product, array $imageData): void
367362
{
368363
$images = $product->getData('media_gallery')['images'];
369-
$image = reset($images);
364+
$image = reset($images) ?: [];
370365
$product->setData('store_id', Store::DEFAULT_STORE_ID);
371366
$product->setData('media_gallery', ['images' => ['image' => array_merge($image, $imageData)]]);
372367
}

0 commit comments

Comments
 (0)