Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:magento/magento2ce into MAGETWO-…
Browse files Browse the repository at this point in the history
…61132
  • Loading branch information
rganin committed Nov 25, 2016
2 parents ec2f926 + b241a65 commit 84f93ad
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,16 @@ public function update($sku, ProductAttributeMediaGalleryEntryInterface $entry)
}
$found = false;
foreach ($existingMediaGalleryEntries as $key => $existingEntry) {
$entryTypes = (array)$entry->getTypes();
$existingEntryTypes = (array)$existingMediaGalleryEntries[$key]->getTypes();
$existingMediaGalleryEntries[$key]->setTypes(array_diff($existingEntryTypes, $entryTypes));

if ($existingEntry->getId() == $entry->getId()) {
$found = true;
if ($entry->getFile()) {
$entry->setId(null);
}
$existingMediaGalleryEntries[$key] = $entry;
break;
}
}
if (!$found) {
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/Catalog/Model/Product/Gallery/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,11 @@ public function clearMediaAttribute(\Magento\Catalog\Model\Product $product, $me
if (is_array($mediaAttribute)) {
foreach ($mediaAttribute as $attribute) {
if (in_array($attribute, $mediaAttributeCodes)) {
$product->setData($attribute, null);
$product->setData($attribute, 'no_selection');
}
}
} elseif (in_array($mediaAttribute, $mediaAttributeCodes)) {
$product->setData($mediaAttribute, null);
$product->setData($mediaAttribute, 'no_selection');
}

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,20 +191,33 @@ public function testUpdate()
$productSku = 'testProduct';
$entryMock = $this->getMock(\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class);
$entryId = 42;
$entrySecondId = 43;
$this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)
->willReturn($this->productMock);
$existingEntryMock = $this->getMock(
\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
);
$existingSecondEntryMock = $this->getMock(
\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
);

$existingEntryMock->expects($this->once())->method('getId')->willReturn($entryId);
$existingEntryMock->expects($this->once())->method('getTypes')->willReturn(['small_image']);
$existingEntryMock->expects($this->once())->method('setTypes')->with(['small_image']);
$existingSecondEntryMock->expects($this->once())->method('getId')->willReturn($entrySecondId);
$existingSecondEntryMock->expects($this->once())->method('getTypes')->willReturn(['image']);
$existingSecondEntryMock->expects($this->once())->method('setTypes')->with([]);
$this->productMock->expects($this->once())->method('getMediaGalleryEntries')
->willReturn([$existingEntryMock]);
$entryMock->expects($this->once())->method('getId')->willReturn($entryId);
->willReturn([$existingEntryMock, $existingSecondEntryMock]);

$entryMock->expects($this->exactly(2))->method('getId')->willReturn($entryId);
$entryMock->expects($this->once())->method('getFile')->willReturn("base64");
$entryMock->expects($this->once())->method('setId')->with(null);
$entryMock->expects($this->exactly(2))->method('getTypes')->willReturn(['image']);

$this->productMock->expects($this->once())->method('setMediaGalleryEntries')
->willReturn([$entryMock]);
->with([$entryMock, $existingSecondEntryMock])
->willReturnSelf();
$this->productRepositoryMock->expects($this->once())->method('save')->with($this->productMock);
$this->assertTrue($this->model->update($productSku, $entryMock));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,56 @@ public function validateDataProvider()
[false]
];
}

/**
* @param int $setDataExpectsCalls
* @param string|null $setDataArgument
* @param array|string $mediaAttribute
* @dataProvider clearMediaAttributeDataProvider
*/
public function testClearMediaAttribute($setDataExpectsCalls, $setDataArgument, $mediaAttribute)
{
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
->disableOriginalConstructor()
->getMock();

$productMock->expects($this->exactly($setDataExpectsCalls))
->method('setData')
->with($setDataArgument, 'no_selection');

$this->mediaConfig->expects($this->once())
->method('getMediaAttributeCodes')
->willReturn(['image', 'small_image']);

$this->assertSame($this->model, $this->model->clearMediaAttribute($productMock, $mediaAttribute));
}

/**
* @return array
*/
public function clearMediaAttributeDataProvider()
{
return [
[
'setDataExpectsCalls' => 1,
'setDataArgument' => 'image',
'mediaAttribute' => 'image',
],
[
'setDataExpectsCalls' => 1,
'setDataArgument' => 'image',
'mediaAttribute' => ['image'],
],
[
'setDataExpectsCalls' => 0,
'setDataArgument' => null,
'mediaAttribute' => 'some_image',
],
[
'setDataExpectsCalls' => 0,
'setDataArgument' => null,
'mediaAttribute' => ['some_image'],
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ public function testUpdate()

$targetProduct = $this->getTargetSimpleProduct();
$this->assertEquals('/m/a/magento_image.jpg', $targetProduct->getData('thumbnail'));
$this->assertNull($targetProduct->getData('image'));
$this->assertNull($targetProduct->getData('small_image'));
$this->assertEquals('no_selection', $targetProduct->getData('image'));
$this->assertEquals('no_selection', $targetProduct->getData('small_image'));
$mediaGallery = $targetProduct->getData('media_gallery');
$this->assertCount(1, $mediaGallery['images']);
$updatedImage = array_shift($mediaGallery['images']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,14 +522,16 @@ public function waitLoader()
}

/**
* Check id media gallery is visible for the product.
* Check if media gallery is visible for the product.
*
* @return bool
*/
public function isGalleryVisible()
{
$this->waitForElementNotVisible($this->galleryLoader);
return $this->_rootElement->find($this->mediaGallery)->isVisible();
$this->waitForElementVisible($this->mediaGallery);

return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ public function testClearMediaAttribute()
);
$product->setData(['image' => 'test1', 'small_image' => 'test2', 'thumbnail' => 'test3']);

$this->assertNotEmpty($product->getData('image'));
$this->assertNotEquals('no_selection', $product->getData('image'));
$this->_model->clearMediaAttribute($product, 'image');
$this->assertNull($product->getData('image'));
$this->assertEquals('no_selection', $product->getData('image'));

$this->assertNotEmpty($product->getData('small_image'));
$this->assertNotEmpty($product->getData('thumbnail'));
$this->assertNotEquals('no_selection', $product->getData('small_image'));
$this->assertNotEquals('no_selection', $product->getData('thumbnail'));
$this->_model->clearMediaAttribute($product, ['small_image', 'thumbnail']);
$this->assertNull($product->getData('small_image'));
$this->assertNull($product->getData('thumbnail'));
$this->assertEquals('no_selection', $product->getData('small_image'));
$this->assertEquals('no_selection', $product->getData('thumbnail'));
}

public function testSetMediaAttribute()
Expand Down

0 comments on commit 84f93ad

Please sign in to comment.