Skip to content

Commit 7216b82

Browse files
committed
MAGETWO-58053: [GITHUB] Product image issue with multiple store views Magento 2.1.0 #6259
2 parents b7ea348 + 1048d63 commit 7216b82

File tree

2 files changed

+138
-14
lines changed

2 files changed

+138
-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: 131 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,150 @@ 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+
* @dataProvider executeDataProvider
63+
* @param $image
64+
* @param $smallImage
65+
* @param $swatchImage
66+
* @param $thumbnail
67+
*/
68+
public function testExecuteWithImageRoles($image, $smallImage, $swatchImage, $thumbnail)
69+
{
70+
/** @var $product \Magento\Catalog\Model\Product */
71+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
72+
\Magento\Catalog\Model\Product::class
73+
);
74+
$product->load(1);
75+
$product->setData(
76+
'media_gallery',
77+
['images' => ['image' => ['file' => $this->fileName, 'label' => '']]]
78+
);
79+
$product->setData('image', $image);
80+
$product->setData('small_image', $smallImage);
81+
$product->setData('swatch_image', $swatchImage);
82+
$product->setData('thumbnail', $thumbnail);
83+
$this->createHandler->execute($product);
84+
85+
$resource = $product->getResource();
86+
$id = $product->getId();
87+
$storeId = $product->getStoreId();
88+
89+
$this->assertStringStartsWith('/m/a/magento_image', $product->getData('media_gallery/images/image/new_file'));
90+
$this->assertEquals(
91+
$image,
92+
$resource->getAttributeRawValue($id, $resource->getAttribute('image'), $storeId)
93+
);
94+
$this->assertEquals(
95+
$smallImage,
96+
$resource->getAttributeRawValue($id, $resource->getAttribute('small_image'), $storeId)
97+
);
98+
$this->assertEquals(
99+
$swatchImage,
100+
$resource->getAttributeRawValue($id, $resource->getAttribute('swatch_image'), $storeId)
101+
);
102+
$this->assertEquals(
103+
$thumbnail,
104+
$resource->getAttributeRawValue($id, $resource->getAttribute('thumbnail'), $storeId)
105+
);
106+
}
107+
108+
/**
109+
* @dataProvider executeDataProvider
110+
* @param $image
111+
* @param $smallImage
112+
* @param $swatchImage
113+
* @param $thumbnail
114+
*/
115+
public function testExecuteWithoutImages($image, $smallImage, $swatchImage, $thumbnail)
116+
{
117+
/** @var $product \Magento\Catalog\Model\Product */
118+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
119+
\Magento\Catalog\Model\Product::class
120+
);
121+
$product->load(1);
122+
$product->setData(
123+
'media_gallery',
124+
['images' => ['image' => ['file' => $this->fileName, 'label' => '']]]
125+
);
126+
$product->setData('image', $image);
127+
$product->setData('small_image', $smallImage);
128+
$product->setData('swatch_image', $swatchImage);
129+
$product->setData('thumbnail', $thumbnail);
130+
$this->createHandler->execute($product);
131+
132+
$product->unsetData('image');
133+
$product->unsetData('small_image');
134+
$product->unsetData('swatch_image');
135+
$product->unsetData('thumbnail');
136+
$this->createHandler->execute($product);
137+
138+
$resource = $product->getResource();
139+
$id = $product->getId();
140+
$storeId = $product->getStoreId();
141+
142+
$this->assertStringStartsWith('/m/a/magento_image', $product->getData('media_gallery/images/image/new_file'));
143+
$this->assertEquals(
144+
$image,
145+
$resource->getAttributeRawValue($id, $resource->getAttribute('image'), $storeId)
146+
);
147+
$this->assertEquals(
148+
$smallImage,
149+
$resource->getAttributeRawValue($id, $resource->getAttribute('small_image'), $storeId)
150+
);
151+
$this->assertEquals(
152+
$swatchImage,
153+
$resource->getAttributeRawValue($id, $resource->getAttribute('swatch_image'), $storeId)
154+
);
155+
$this->assertEquals(
156+
$thumbnail,
157+
$resource->getAttributeRawValue($id, $resource->getAttribute('thumbnail'), $storeId)
158+
);
159+
}
160+
161+
/**
162+
* @return array
163+
*/
164+
public function executeDataProvider()
165+
{
166+
return [
167+
[
168+
'image' => $this->fileName,
169+
'small_image' => $this->fileName,
170+
'swatch_image' => $this->fileName,
171+
'thumbnail' => $this->fileName
172+
],
173+
[
174+
'image' => 'no_selection',
175+
'small_image' => 'no_selection',
176+
'swatch_image' => 'no_selection',
177+
'thumbnail' => 'no_selection'
178+
]
179+
];
57180
}
58181
}

0 commit comments

Comments
 (0)