Skip to content

Commit 083d6db

Browse files
author
Bohdan Korablov
committed
MAGETWO-60908: Image role can't be updated
1 parent 1759088 commit 083d6db

File tree

4 files changed

+75
-6
lines changed

4 files changed

+75
-6
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,17 @@ public function update($sku, ProductAttributeMediaGalleryEntryInterface $entry)
9494
}
9595
$found = false;
9696
foreach ($existingMediaGalleryEntries as $key => $existingEntry) {
97+
$entryTypes = (array)$entry->getTypes();
98+
$existingEntryTypes = (array)$existingMediaGalleryEntries[$key]->getTypes();
99+
$intersectEntryTypes = array_intersect($entryTypes, $existingEntryTypes);
100+
$existingMediaGalleryEntries[$key]->setTypes(array_diff($existingEntryTypes, $intersectEntryTypes));
101+
97102
if ($existingEntry->getId() == $entry->getId()) {
98103
$found = true;
99104
if ($entry->getFile()) {
100105
$entry->setId(null);
101106
}
102107
$existingMediaGalleryEntries[$key] = $entry;
103-
break;
104108
}
105109
}
106110
if (!$found) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,11 @@ public function clearMediaAttribute(\Magento\Catalog\Model\Product $product, $me
298298
if (is_array($mediaAttribute)) {
299299
foreach ($mediaAttribute as $attribute) {
300300
if (in_array($attribute, $mediaAttributeCodes)) {
301-
$product->setData($attribute, null);
301+
$product->setData($attribute, 'no_selection');
302302
}
303303
}
304304
} elseif (in_array($mediaAttribute, $mediaAttributeCodes)) {
305-
$product->setData($mediaAttribute, null);
305+
$product->setData($mediaAttribute, 'no_selection');
306306
}
307307

308308
return $this;

app/code/Magento/Catalog/Test/Unit/Model/Product/Gallery/GalleryManagementTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,33 @@ public function testUpdate()
191191
$productSku = 'testProduct';
192192
$entryMock = $this->getMock(\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class);
193193
$entryId = 42;
194+
$entrySecondId = 43;
194195
$this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)
195196
->willReturn($this->productMock);
196197
$existingEntryMock = $this->getMock(
197198
\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
198199
);
200+
$existingSecondEntryMock = $this->getMock(
201+
\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
202+
);
203+
199204
$existingEntryMock->expects($this->once())->method('getId')->willReturn($entryId);
205+
$existingEntryMock->expects($this->once())->method('getTypes')->willReturn(['small_image']);
206+
$existingEntryMock->expects($this->once())->method('setTypes')->with(['small_image']);
207+
$existingSecondEntryMock->expects($this->once())->method('getId')->willReturn($entrySecondId);
208+
$existingSecondEntryMock->expects($this->once())->method('getTypes')->willReturn(['image']);
209+
$existingSecondEntryMock->expects($this->once())->method('setTypes')->with([]);
200210
$this->productMock->expects($this->once())->method('getMediaGalleryEntries')
201-
->willReturn([$existingEntryMock]);
202-
$entryMock->expects($this->once())->method('getId')->willReturn($entryId);
211+
->willReturn([$existingEntryMock, $existingSecondEntryMock]);
212+
213+
$entryMock->expects($this->exactly(2))->method('getId')->willReturn($entryId);
203214
$entryMock->expects($this->once())->method('getFile')->willReturn("base64");
204215
$entryMock->expects($this->once())->method('setId')->with(null);
216+
$entryMock->expects($this->exactly(2))->method('getTypes')->willReturn(['image']);
205217

206218
$this->productMock->expects($this->once())->method('setMediaGalleryEntries')
207-
->willReturn([$entryMock]);
219+
->with([$entryMock, $existingSecondEntryMock])
220+
->willReturnSelf();
208221
$this->productRepositoryMock->expects($this->once())->method('save')->with($this->productMock);
209222
$this->assertTrue($this->model->update($productSku, $entryMock));
210223
}

app/code/Magento/Catalog/Test/Unit/Model/Product/Gallery/ProcessorTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,56 @@ public function validateDataProvider()
197197
[false]
198198
];
199199
}
200+
201+
/**
202+
* @param int $setDataExpectsCalls
203+
* @param string|null $setDataArgument
204+
* @param array|string $mediaAttribute
205+
* @dataProvider clearMediaAttributeDataProvider
206+
*/
207+
public function testClearMediaAttribute($setDataExpectsCalls, $setDataArgument, $mediaAttribute)
208+
{
209+
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
210+
->disableOriginalConstructor()
211+
->getMock();
212+
213+
$productMock->expects($this->exactly($setDataExpectsCalls))
214+
->method('setData')
215+
->with($setDataArgument, 'no_selection');
216+
217+
$this->mediaConfig->expects($this->once())
218+
->method('getMediaAttributeCodes')
219+
->willReturn(['image', 'small_image']);
220+
221+
$this->assertSame($this->model, $this->model->clearMediaAttribute($productMock, $mediaAttribute));
222+
}
223+
224+
/**
225+
* @return array
226+
*/
227+
public function clearMediaAttributeDataProvider()
228+
{
229+
return [
230+
[
231+
'setDataExpectsCalls' => 1,
232+
'setDataArgument' => 'image',
233+
'mediaAttribute' => 'image',
234+
],
235+
[
236+
'setDataExpectsCalls' => 1,
237+
'setDataArgument' => 'image',
238+
'mediaAttribute' => ['image'],
239+
],
240+
[
241+
'setDataExpectsCalls' => 0,
242+
'setDataArgument' => null,
243+
'mediaAttribute' => 'some_image',
244+
],
245+
[
246+
'setDataExpectsCalls' => 0,
247+
'setDataArgument' => null,
248+
'mediaAttribute' => ['some_image'],
249+
],
250+
];
251+
}
200252
}

0 commit comments

Comments
 (0)