Skip to content

Commit 37c4b9d

Browse files
author
Hayder Sharhan
committed
MAGETWO-56868: CLONE - Cannot save a product with images for the second time
- Added unit test.
1 parent 8fb56e1 commit 37c4b9d

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,16 @@ class ProductTest extends \PHPUnit_Framework_TestCase
185185
*/
186186
private $extensionAttributesFactory;
187187

188+
/**
189+
* @var \Magento\Framework\Filesystem
190+
*/
191+
private $filesystemMock;
192+
193+
/**
194+
* @var \Magento\Framework\Data\CollectionFactory
195+
*/
196+
private $collectionFactoryMock;
197+
188198
/**
189199
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
190200
*/
@@ -374,6 +384,12 @@ protected function setUp()
374384
$this->extensionAttributesFactory = $this->getMockBuilder(ExtensionAttributesFactory::class)
375385
->disableOriginalConstructor()
376386
->getMock();
387+
$this->filesystemMock = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
388+
->disableOriginalConstructor()
389+
->getMock();
390+
$this->collectionFactoryMock = $this->getMockBuilder(\Magento\Framework\Data\CollectionFactory::class)
391+
->disableOriginalConstructor()
392+
->getMock();
377393
$this->mediaConfig = $this->getMock(\Magento\Catalog\Model\Product\Media\Config::class, [], [], '', false);
378394
$this->objectManagerHelper = new ObjectManagerHelper($this);
379395

@@ -402,6 +418,8 @@ protected function setUp()
402418
'mediaGalleryEntryConverterPool' => $this->mediaGalleryEntryConverterPoolMock,
403419
'linkRepository' => $this->productLinkRepositoryMock,
404420
'catalogProductMediaConfig' => $this->mediaConfig,
421+
'_filesystem' => $this->filesystemMock,
422+
'_collectionFactory' => $this->collectionFactoryMock,
405423
'data' => ['id' => 1]
406424
]
407425
);
@@ -1230,6 +1248,71 @@ public function testSetMediaGalleryEntries()
12301248
$this->assertEquals($expectedResult, $this->model->getMediaGallery());
12311249
}
12321250

1251+
public function testGetMediaGalleryImagesMerging()
1252+
{
1253+
$mediaEntries = [
1254+
'images' => [
1255+
[
1256+
'value_id' => 1,
1257+
'file' => 'imageFile.jpg',
1258+
'media_type' => 'image',
1259+
],
1260+
[
1261+
'value_id' => 1,
1262+
'file' => 'imageFile.jpg',
1263+
],
1264+
[
1265+
'value_id' => 2,
1266+
'file' => 'smallImageFile.jpg',
1267+
'media_type' => 'image',
1268+
],
1269+
]
1270+
];
1271+
$expectedImageDataObject = new \Magento\Framework\DataObject([
1272+
'value_id' => 1,
1273+
'file' => 'imageFile.jpg',
1274+
'media_type' => 'image',
1275+
'url' => 'http://magento.dev/pub/imageFile.jpg',
1276+
'id' => 1,
1277+
'path' => '/var/www/html/pub/imageFile.jpg',
1278+
]);
1279+
$expectedSmallImageDataObject = new \Magento\Framework\DataObject([
1280+
'value_id' => 2,
1281+
'file' => 'smallImageFile.jpg',
1282+
'media_type' => 'image',
1283+
'url' => 'http://magento.dev/pub/smallImageFile.jpg',
1284+
'id' => 2,
1285+
'path' => '/var/www/html/pub/smallImageFile.jpg',
1286+
]);
1287+
1288+
$directoryMock = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\ReadInterface::class)
1289+
->disableOriginalConstructor()
1290+
->getMock();
1291+
$this->filesystemMock->expects($this->once())->method('getDirectoryRead')->willReturn($directoryMock);
1292+
$this->model->setData('media_gallery', $mediaEntries);
1293+
$imagesCollectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection::class)
1294+
->disableOriginalConstructor()
1295+
->getMock();
1296+
$this->collectionFactoryMock->expects($this->once())->method('create')->willReturn($imagesCollectionMock);
1297+
$imagesCollectionMock->expects($this->at(2))->method('getItemById')->with(1)->willReturn($expectedImageDataObject);
1298+
$this->mediaConfig->expects($this->at(0))
1299+
->method('getMediaUrl')
1300+
->willReturn('http://magento.dev/pub/imageFile.jpg');
1301+
$directoryMock->expects($this->at(0))
1302+
->method('getAbsolutePath')
1303+
->willReturn('/var/www/html/pub/imageFile.jpg');
1304+
$this->mediaConfig->expects($this->at(2))
1305+
->method('getMediaUrl')
1306+
->willReturn('http://magento.dev/pub/smallImageFile.jpg');
1307+
$directoryMock->expects($this->at(1))
1308+
->method('getAbsolutePath')
1309+
->willReturn('/var/www/html/pub/smallImageFile.jpg');
1310+
$imagesCollectionMock->expects($this->at(1))->method('addItem')->with($expectedImageDataObject);
1311+
$imagesCollectionMock->expects($this->at(4))->method('addItem')->with($expectedSmallImageDataObject);
1312+
1313+
$this->model->getMediaGalleryImages();
1314+
}
1315+
12331316
public function testGetCustomAttributes()
12341317
{
12351318
$priceCode = 'price';

0 commit comments

Comments
 (0)