Skip to content

Commit 78edba3

Browse files
author
Oleksii Korshenko
authored
Merge pull request #638 from magento-east/MAGETWO-61039
MAGETWO-61039: Error when saving an existing product with an image
2 parents ed7beea + 72c00e8 commit 78edba3

File tree

2 files changed

+41
-5
lines changed
  • app/code/Magento/Catalog

2 files changed

+41
-5
lines changed

app/code/Magento/Catalog/Test/Unit/Ui/DataProvider/Product/Form/Modifier/ImagesTest.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
*/
66
namespace Magento\Catalog\Test\Unit\Ui\DataProvider\Product\Form\Modifier;
77

8-
use Magento\Catalog\Model\Product\Type;
8+
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Images;
99

1010
/**
11-
* @method \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Images getModel
11+
* @method Images getModel
1212
*/
1313
class ImagesTest extends AbstractModifierTest
1414
{
@@ -17,20 +17,22 @@ class ImagesTest extends AbstractModifierTest
1717
*/
1818
protected function createModel()
1919
{
20-
return $this->objectManager->getObject(\Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Images::class, [
20+
return $this->objectManager->getObject(Images::class, [
2121
'locator' => $this->locatorMock,
2222
]);
2323
}
2424

2525
public function testModifyData()
2626
{
27-
$this->assertSame($this->getSampleData(), $this->getModel()->modifyData($this->getSampleData()));
27+
$this->productMock->expects($this->once())->method('getId')->willReturn(2051);
28+
$actualResult = $this->getModel()->modifyData($this->getSampleData());
29+
$this->assertSame('', $actualResult[2051]['product']['media_gallery']['images'][0]['label']);
2830
}
2931

3032
public function testModifyMeta()
3133
{
3234
$meta = [
33-
\Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Images::CODE_IMAGE_MANAGEMENT_GROUP => [
35+
Images::CODE_IMAGE_MANAGEMENT_GROUP => [
3436
'children' => [],
3537
'label' => __('Images'),
3638
'sortOrder' => '20',
@@ -40,4 +42,24 @@ public function testModifyMeta()
4042

4143
$this->assertSame([], $this->getModel()->modifyMeta($meta));
4244
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
protected function getSampleData()
50+
{
51+
return [
52+
2051 => [
53+
'product' => [
54+
'media_gallery' => [
55+
'images' => [
56+
[
57+
'label' => null
58+
]
59+
]
60+
]
61+
]
62+
]
63+
];
64+
}
4365
}

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Images.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ public function modifyMeta(array $meta)
5151
*/
5252
public function modifyData(array $data)
5353
{
54+
/** @var \Magento\Catalog\Api\Data\ProductInterface $product */
55+
$product = $this->locator->getProduct();
56+
$modelId = $product->getId();
57+
if (
58+
isset($data[$modelId][self::DATA_SOURCE_DEFAULT]['media_gallery']['images'])
59+
&& is_array($data[$modelId][self::DATA_SOURCE_DEFAULT]['media_gallery']['images'])
60+
) {
61+
foreach ($data[$modelId][self::DATA_SOURCE_DEFAULT]['media_gallery']['images'] as $index => $image) {
62+
if (!isset($image['label'])) {
63+
$data[$modelId][self::DATA_SOURCE_DEFAULT]['media_gallery']['images'][$index]['label'] = '';
64+
}
65+
}
66+
}
67+
5468
return $data;
5569
}
5670
}

0 commit comments

Comments
 (0)