Skip to content

Commit c4d4654

Browse files
committed
MAGETWO-58053: [GITHUB] Product image issue with multiple store views Magento 2.1.0 #6259
1 parent 7800e37 commit c4d4654

File tree

2 files changed

+86
-14
lines changed

2 files changed

+86
-14
lines changed

app/code/Magento/Catalog/Model/Product/Gallery/CreateHandler.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,13 @@ public function execute($product, $arguments = [])
160160
if (in_array($attrData, array_keys($existImages))) {
161161
$product->setData($mediaAttrCode . '_label', $existImages[$attrData]['label']);
162162
}
163-
164-
$product->addAttributeUpdate(
165-
$mediaAttrCode,
166-
$product->getData($mediaAttrCode),
167-
$product->getStoreId()
168-
);
163+
if (!empty($product->getData($mediaAttrCode))) {
164+
$product->addAttributeUpdate(
165+
$mediaAttrCode,
166+
$product->getData($mediaAttrCode),
167+
$product->getStoreId()
168+
);
169+
}
169170
}
170171

171172
$product->setData($attrCode, $value);

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

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class CreateHandlerTest extends \PHPUnit_Framework_TestCase
1818
*/
1919
protected $createHandler;
2020

21+
private $fileName = '/m/a/magento_image.jpg';
22+
23+
private $fileLabel = 'Magento image';
24+
2125
protected function setUp()
2226
{
2327
$this->createHandler = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
@@ -28,31 +32,98 @@ protected function setUp()
2832
/**
2933
* @covers \Magento\Catalog\Model\Product\Gallery\CreateHandler::execute
3034
*/
31-
public function testExecute()
35+
public function testExecuteWithImageDuplicate()
3236
{
33-
$fileName = '/m/a/magento_image.jpg';
34-
$fileLabel = 'Magento image';
3537
/** @var $product \Magento\Catalog\Model\Product */
3638
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
3739
\Magento\Catalog\Model\Product::class
3840
);
3941
$product->load(1);
4042
$product->setData(
4143
'media_gallery',
42-
['images' => ['image' => ['file' => $fileName, 'label' => $fileLabel]]]
44+
['images' => ['image' => ['file' => $this->fileName, 'label' => $this->fileLabel]]]
4345
);
44-
$product->setData('image', $fileName);
46+
$product->setData('image', $this->fileName);
4547
$this->createHandler->execute($product);
4648
$this->assertStringStartsWith('/m/a/magento_image', $product->getData('media_gallery/images/image/new_file'));
47-
$this->assertEquals($fileLabel, $product->getData('image_label'));
49+
$this->assertEquals($this->fileLabel, $product->getData('image_label'));
4850

4951
$product->setIsDuplicate(true);
5052
$product->setData(
5153
'media_gallery',
52-
['images' => ['image' => ['value_id' => '100', 'file' => $fileName, 'label' => $fileLabel]]]
54+
['images' => ['image' => ['value_id' => '100', 'file' => $this->fileName, 'label' => $this->fileLabel]]]
5355
);
5456
$this->createHandler->execute($product);
5557
$this->assertStringStartsWith('/m/a/magento_image', $product->getData('media_gallery/duplicate/100'));
56-
$this->assertEquals($fileLabel, $product->getData('image_label'));
58+
$this->assertEquals($this->fileLabel, $product->getData('image_label'));
59+
}
60+
61+
/**
62+
* @covers \Magento\Catalog\Model\Product\Gallery\CreateHandler::execute
63+
* @dataProvider executeDataProvider
64+
* @param array $expectedValues
65+
*/
66+
public function testExecuteWithImageRoles($expectedValues)
67+
{
68+
/** @var $product \Magento\Catalog\Model\Product */
69+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
70+
\Magento\Catalog\Model\Product::class
71+
);
72+
$product->load(1);
73+
$product->setData(
74+
'media_gallery',
75+
['images' => ['image' => ['file' => $this->fileName, 'label' => '']]]
76+
);
77+
foreach ($expectedValues as $mediaAttribute => $value) {
78+
if ($value) {
79+
$product->setData($mediaAttribute, $value);
80+
}
81+
}
82+
$this->createHandler->execute($product);
83+
$this->assertStringStartsWith('/m/a/magento_image', $product->getData('media_gallery/images/image/new_file'));
84+
$resource = $product->getResource();
85+
$attributeValues = [];
86+
foreach (array_keys($expectedValues) as $mediaAttribute) {
87+
$attributeValues[$mediaAttribute] = $resource->getAttributeRawValue(
88+
$product->getId(),
89+
$resource->getAttribute($mediaAttribute),
90+
$product->getStoreId()
91+
);
92+
}
93+
$this->assertStringStartsWith('/m/a/magento_image', $product->getData('media_gallery/images/image/new_file'));
94+
$this->assertEquals($expectedValues, $attributeValues);
95+
}
96+
97+
/**
98+
* @return array
99+
*/
100+
public function executeDataProvider()
101+
{
102+
return [
103+
[
104+
[
105+
'image' => $this->fileName,
106+
'small_image' => $this->fileName,
107+
'swatch_image' => $this->fileName,
108+
'thumbnail' => $this->fileName
109+
]
110+
],
111+
[
112+
[
113+
'image' => 'no_selection',
114+
'small_image' => 'no_selection',
115+
'swatch_image' => 'no_selection',
116+
'thumbnail' => 'no_selection'
117+
]
118+
],
119+
[
120+
[
121+
'image' => null,
122+
'small_image' => null,
123+
'swatch_image' => null,
124+
'thumbnail' => null
125+
]
126+
]
127+
];
57128
}
58129
}

0 commit comments

Comments
 (0)