Skip to content

Commit 8d40cd8

Browse files
author
Sergey Shvets
committed
MAGETWO-61039: Error when saving an existing product with an image
1 parent 7186af8 commit 8d40cd8

File tree

2 files changed

+40
-6
lines changed
  • app/code/Magento/Catalog

2 files changed

+40
-6
lines changed

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

Lines changed: 26 additions & 6 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,9 +17,9 @@ class ImagesTest extends AbstractModifierTest
1717
*/
1818
protected function createModel()
1919
{
20-
return $this->objectManager->getObject(\Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Images::class, [
21-
'locator' => $this->locatorMock,
22-
]);
20+
$this->productMock->expects($this->once())->method('getId')->willReturn(2051);
21+
$actualResult = $this->getModel()->modifyData($this->getSampleData());
22+
$this->assertSame('', $actualResult[2051]['product']['media_gallery']['images'][0]['label']);
2323
}
2424

2525
public function testModifyData()
@@ -30,7 +30,7 @@ public function testModifyData()
3030
public function testModifyMeta()
3131
{
3232
$meta = [
33-
\Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Images::CODE_IMAGE_MANAGEMENT_GROUP => [
33+
Images::CODE_IMAGE_MANAGEMENT_GROUP => [
3434
'children' => [],
3535
'label' => __('Images'),
3636
'sortOrder' => '20',
@@ -40,4 +40,24 @@ public function testModifyMeta()
4040

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

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)