Skip to content

Commit 06d623e

Browse files
committed
MAGETWO-83204: "Hide from Product Page" option does not work for child of configurable product
1 parent 279522a commit 06d623e

File tree

1 file changed

+98
-57
lines changed

1 file changed

+98
-57
lines changed

app/code/Magento/Swatches/Test/Unit/Helper/DataTest.php

Lines changed: 98 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public function dataForAssembleEavAttribute()
234234
public function testLoadFirstVariationWithSwatchImage($imageTypes, $expected, $requiredAttributes)
235235
{
236236
$this->getSwatchAttributes($this->productMock);
237-
$this->getUsedProducts($imageTypes + $requiredAttributes);
237+
$this->getUsedProducts($imageTypes + $requiredAttributes, $imageTypes);
238238

239239
$result = $this->swatchHelperObject->loadFirstVariationWithSwatchImage($this->productMock, $requiredAttributes);
240240

@@ -293,7 +293,7 @@ public function testLoadVariationByFallback($product)
293293
public function testLoadFirstVariationWithImage($imageTypes, $expected, $requiredAttributes)
294294
{
295295
$this->getSwatchAttributes($this->productMock);
296-
$this->getUsedProducts($imageTypes + $requiredAttributes);
296+
$this->getUsedProducts($imageTypes + $requiredAttributes, $imageTypes);
297297

298298
$result = $this->swatchHelperObject->loadFirstVariationWithImage($this->productMock, $requiredAttributes);
299299

@@ -346,53 +346,68 @@ public function testLoadVariationByFallbackWithoutProduct()
346346
*/
347347
public function testGetProductMediaGallery($mediaGallery, $image)
348348
{
349-
$this->productMock->expects($this->once())->method('getMediaAttributeValues')->willReturn($mediaGallery);
350-
$this->productMock->expects($this->any())->method('getId')->willReturn(95);
351-
352-
$this->imageHelperMock->expects($this->any())
353-
->method('init')
354-
->willReturnMap([
355-
[$this->productMock, 'product_page_image_large', [], $this->imageHelperMock],
356-
[$this->productMock, 'product_page_image_medium', [], $this->imageHelperMock],
357-
[$this->productMock, 'product_page_image_small', [], $this->imageHelperMock],
358-
]);
359-
360-
$this->imageHelperMock->expects($this->any())
361-
->method('setImageFile')
362-
->with($image)
363-
->willReturnSelf();
364-
$this->imageHelperMock->expects($this->any())
365-
->method('constrainOnly')
366-
->willReturnSelf();
367-
$this->imageHelperMock->expects($this->any())
368-
->method('keepAspectRatio')
369-
->willReturnSelf();
370-
$this->imageHelperMock->expects($this->any())
371-
->method('keepFrame')
372-
->willReturnSelf();
373-
$this->imageHelperMock->expects($this->any())
374-
->method('getUrl')
375-
->willReturn('http://full_path_to_image/magento1.png');
376-
377-
$this->productRepoMock->expects($this->any())
378-
->method('getById')
379-
->with(95)
380-
->willReturn($this->productMock);
381-
382-
$mediaObject = $this->getMock(\Magento\Framework\DataObject::class, [], [], '', false);
383-
$iterator = new \ArrayIterator([$mediaObject]);
384-
$mediaCollectionMock = $this->getMock(\Magento\Framework\Data\Collection::class, [], [], '', false);
385-
$mediaCollectionMock->expects($this->any())->method('getIterator')->willReturn($iterator);
386-
$mediaObject->method('getData')->withConsecutive(
387-
['value_id'],
388-
['file']
389-
)->willReturnOnConsecutiveCalls(
390-
0,
391-
$image
392-
);
393-
$this->productMock->method('getMediaGalleryImages')->willReturn($mediaCollectionMock);
349+
$mediaGalleryEntries = [];
350+
$id = 0;
351+
$mediaUrls = [];
352+
foreach ($mediaGallery as $mediaType => $mediaFile) {
353+
$mediaGalleryEntryMock = $this->getMockBuilder(
354+
\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
355+
)->getMock();
356+
$mediaGalleryEntryMock->expects($this->atLeastOnce())
357+
->method('isDisabled')
358+
->willReturn(false);
359+
$mediaGalleryEntryMock->expects($this->atLeastOnce())
360+
->method('getTypes')
361+
->willReturn([$mediaType]);
362+
$mediaGalleryEntryMock->expects($this->atLeastOnce())
363+
->method('getFile')
364+
->willReturn($mediaFile);
365+
$mediaGalleryEntryMock->expects($this->atLeastOnce())
366+
->method('getId')
367+
->willReturn(++$id);
368+
369+
$mediaGalleryEntries[] = $mediaGalleryEntryMock;
370+
$mediaUrls[] = ['http://full_path_to_image' . $mediaFile]; //large
371+
$mediaUrls[] = ['http://full_path_to_image' . $mediaFile]; //medium
372+
$mediaUrls[] = ['http://full_path_to_image' . $mediaFile]; //small
373+
}
374+
$this->productMock->expects($this->once())
375+
->method('getMediaGalleryEntries')
376+
->willReturn($mediaGalleryEntries);
377+
378+
if ($mediaGallery) {
379+
$this->imageHelperMock->expects($this->atLeastOnce())
380+
->method('init')
381+
->willReturnMap([
382+
[$this->productMock, 'product_page_image_large', [], $this->imageHelperMock],
383+
[$this->productMock, 'product_page_image_medium', [], $this->imageHelperMock],
384+
[$this->productMock, 'product_page_image_small', [], $this->imageHelperMock],
385+
]);
386+
$this->imageHelperMock->expects($this->atLeastOnce())
387+
->method('setImageFile')
388+
->willReturnSelf();
389+
$this->imageHelperMock->expects($this->atLeastOnce())
390+
->method('constrainOnly')
391+
->willReturnSelf();
392+
$this->imageHelperMock->expects($this->atLeastOnce())
393+
->method('keepAspectRatio')
394+
->willReturnSelf();
395+
$this->imageHelperMock->expects($this->atLeastOnce())
396+
->method('keepFrame')
397+
->willReturnSelf();
398+
$this->imageHelperMock->expects($this->atLeastOnce())
399+
->method('getUrl')
400+
->willReturnMap($mediaUrls);
401+
}
394402

395-
$this->swatchHelperObject->getProductMediaGallery($this->productMock);
403+
$productMediaGallery = $this->swatchHelperObject->getProductMediaGallery($this->productMock);
404+
if ($mediaGallery) {
405+
$this->assertContains($image, $productMediaGallery['large']);
406+
$this->assertContains($image, $productMediaGallery['medium']);
407+
$this->assertContains($image, $productMediaGallery['small']);
408+
} else {
409+
$this->assertEmpty($productMediaGallery);
410+
}
396411
}
397412

398413
public function dataForMediaGallery()
@@ -432,22 +447,48 @@ protected function getSwatchAttributes()
432447
->willReturn($returnFromProvideMethod);
433448
}
434449

435-
protected function getUsedProducts(array $attributes)
450+
protected function getUsedProducts(array $attributes, array $imageTypes)
436451
{
437452
$this->productMock
438453
->expects($this->atLeastOnce())
439454
->method('getTypeInstance')
440455
->willReturn($this->configurableMock);
441456

442-
$product1 = $this->getMock(\Magento\Catalog\Model\Product::class, ['hasData'], [], '', false);
443-
$product1->setData($attributes);
444-
445-
$product2 = $this->getMock(\Magento\Catalog\Model\Product::class, ['hasData'], [], '', false);
446-
$product2->setData($attributes);
447-
448-
$simpleProducts = [$product2, $product1];
457+
$simpleProducts = [];
458+
for ($i = 0; $i < 2; $i++) {
459+
$simpleProduct = $this->getMock(
460+
\Magento\Catalog\Model\Product::class,
461+
['hasData', 'getMediaGalleryEntries'],
462+
[],
463+
'',
464+
false
465+
);
466+
$simpleProduct->setData($attributes);
467+
468+
$mediaGalleryEntries = [];
469+
foreach ($imageTypes as $mediaType => $mediaFile) {
470+
$mediaGalleryEntryMock = $this->getMockBuilder(
471+
\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface::class
472+
)->getMock();
473+
$mediaGalleryEntryMock->expects($this->any())
474+
->method('isDisabled')
475+
->willReturn(false);
476+
$mediaGalleryEntryMock->expects($this->any())
477+
->method('getTypes')
478+
->willReturn([$mediaType]);
479+
480+
$mediaGalleryEntries[] = $mediaGalleryEntryMock;
481+
}
482+
$simpleProduct->expects($this->any())
483+
->method('getMediaGalleryEntries')
484+
->willReturn($mediaGalleryEntries);
485+
486+
$simpleProducts[] = $simpleProduct;
487+
}
449488

450-
$this->configurableMock->expects($this->once())->method('getUsedProducts')->with($this->productMock)
489+
$this->configurableMock->expects($this->once())
490+
->method('getUsedProducts')
491+
->with($this->productMock)
451492
->willReturn($simpleProducts);
452493

453494
}

0 commit comments

Comments
 (0)