Skip to content

Commit 471c8cf

Browse files
author
lakshmana
committed
ACP2E-1526 : Fixed existing unit test and added fix for single media entry API
1 parent 155427d commit 471c8cf

File tree

3 files changed

+188
-2
lines changed

3 files changed

+188
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ public function remove($sku, $entryId)
243243
public function get($sku, $entryId)
244244
{
245245
try {
246+
/** @var Product $product */
246247
$product = $this->productRepository->get($sku);
247248
} catch (\Exception $exception) {
248249
throw new NoSuchEntityException(__("The product doesn't exist. Verify and try again."));
@@ -251,6 +252,7 @@ public function get($sku, $entryId)
251252
$mediaGalleryEntries = $product->getMediaGalleryEntries();
252253
foreach ($mediaGalleryEntries as $entry) {
253254
if ($entry->getId() == $entryId) {
255+
$entry->setContent($this->getImageContent($product, $entry));
254256
return $entry;
255257
}
256258
}

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

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@
2020
use Magento\Framework\Api\Data\ImageContentInterface;
2121
use Magento\Framework\Api\Data\ImageContentInterfaceFactory;
2222
use Magento\Framework\Api\ImageContentValidatorInterface;
23+
use Magento\Framework\App\Filesystem\DirectoryList;
2324
use Magento\Framework\Filesystem;
25+
use Magento\Framework\Filesystem\Directory\WriteInterface;
2426
use Magento\Framework\Filesystem\Driver\File\Mime;
27+
use Magento\Framework\Filesystem\DriverInterface;
2528
use PHPUnit\Framework\MockObject\MockObject;
2629
use PHPUnit\Framework\TestCase;
2730
use Magento\Framework\Filesystem\Io\File;
31+
use Magento\Catalog\Model\Product\Media\ConfigInterface as MediaConfig;
2832

2933
/**
3034
* Tests for \Magento\Catalog\Model\Product\Gallery\GalleryManagement.
@@ -107,7 +111,9 @@ protected function setUp(): void
107111
$this->contentValidatorMock = $this->getMockForAbstractClass(ImageContentValidatorInterface::class);
108112
$this->productInterfaceFactory = $this->createMock(ProductInterfaceFactory::class);
109113
$this->deleteValidator = $this->createMock(DeleteValidator::class);
110-
$this->imageContentInterface = $this->createMock(ImageContentInterfaceFactory::class);
114+
$this->imageContentInterface = $this->getMockBuilder(ImageContentInterfaceFactory::class)
115+
->disableOriginalConstructor()
116+
->getMock();
111117
$this->filesystem = $this->createMock(Filesystem::class);
112118
$this->mime = $this->createMock(Mime::class);
113119
$this->file = $this->createMock(File::class);
@@ -121,7 +127,8 @@ protected function setUp(): void
121127
'getCustomAttribute',
122128
'getMediaGalleryEntries',
123129
'setMediaGalleryEntries',
124-
'getMediaAttributes'
130+
'getMediaAttributes',
131+
'getMediaConfig'
125132
]
126133
);
127134
$this->mediaGalleryEntryMock =
@@ -146,6 +153,7 @@ protected function setUp(): void
146153
->willReturn($this->newProductMock);
147154
}
148155

156+
149157
/**
150158
* @return void
151159
*/
@@ -413,6 +421,55 @@ public function testGet(): void
413421
$existingEntryMock->expects($this->once())->method('getId')->willReturn(42);
414422
$this->productMock->expects($this->once())->method('getMediaGalleryEntries')
415423
->willReturn([$existingEntryMock]);
424+
$mediaConfigMock = $this->getMockBuilder(MediaConfig::class)
425+
->disableOriginalConstructor()
426+
->getMock();
427+
$mediaConfigMock->expects($this->once())
428+
->method('getMediaPath')
429+
->willReturn("base/path/test123.jpg");
430+
$this->productMock->expects($this->once())
431+
->method('getMediaConfig')
432+
->willReturn($mediaConfigMock);
433+
$mediaDirectoryMock = $this->getMockBuilder(WriteInterface::class)
434+
->disableOriginalConstructor()
435+
->getMock();
436+
$this->filesystem->expects($this->once())
437+
->method('getDirectoryWrite')
438+
->with(DirectoryList::MEDIA)
439+
->willReturn($mediaDirectoryMock);
440+
$mediaDirectoryMock->expects($this->once())
441+
->method('getAbsolutePath')
442+
->with('base/path/test123.jpg')
443+
->willReturn('absolute/path/base/path/test123.jpg');
444+
$this->file->expects($this->any())
445+
->method('getPathInfo')
446+
->willReturnCallback(
447+
function ($path) {
448+
return pathinfo($path);
449+
}
450+
);
451+
$driverMock = $this->getMockBuilder(DriverInterface::class)
452+
->disableOriginalConstructor()
453+
->getMock();
454+
$mediaDirectoryMock->expects($this->any())->method('getDriver')->willReturn($driverMock);
455+
$driverMock->expects($this->once())
456+
->method('fileGetContents')
457+
->willReturn('0123456789abcdefghijklmnopqrstuvwxyz');
458+
$ImageContentInterface = $this->getMockBuilder(ImageContentInterface::class)
459+
->disableOriginalConstructor()
460+
->getMock();
461+
$ImageContentInterface->expects($this->once())
462+
->method('setName')
463+
->willReturnSelf();
464+
$ImageContentInterface->expects($this->once())
465+
->method('setBase64EncodedData')
466+
->willReturnSelf();
467+
$ImageContentInterface->expects($this->once())
468+
->method('setType')
469+
->willReturnSelf();
470+
$this->imageContentInterface->expects($this->once())
471+
->method('create')
472+
->willReturn($ImageContentInterface);
416473
$this->assertEquals($existingEntryMock, $this->model->get($productSku, $imageId));
417474
}
418475

@@ -427,6 +484,57 @@ public function testGetList(): void
427484
$entryMock = $this->getMockForAbstractClass(ProductAttributeMediaGalleryEntryInterface::class);
428485
$this->productMock->expects($this->once())->method('getMediaGalleryEntries')
429486
->willReturn([$entryMock]);
487+
$this->productMock->expects($this->once())->method('getMediaGalleryEntries')
488+
->willReturn([$entryMock]);
489+
$mediaConfigMock = $this->getMockBuilder(MediaConfig::class)
490+
->disableOriginalConstructor()
491+
->getMock();
492+
$mediaConfigMock->expects($this->once())
493+
->method('getMediaPath')
494+
->willReturn("base/path/test123.jpg");
495+
$this->productMock->expects($this->once())
496+
->method('getMediaConfig')
497+
->willReturn($mediaConfigMock);
498+
$mediaDirectoryMock = $this->getMockBuilder(WriteInterface::class)
499+
->disableOriginalConstructor()
500+
->getMock();
501+
$this->filesystem->expects($this->once())
502+
->method('getDirectoryWrite')
503+
->with(DirectoryList::MEDIA)
504+
->willReturn($mediaDirectoryMock);
505+
$mediaDirectoryMock->expects($this->once())
506+
->method('getAbsolutePath')
507+
->with('base/path/test123.jpg')
508+
->willReturn('absolute/path/base/path/test123.jpg');
509+
$this->file->expects($this->any())
510+
->method('getPathInfo')
511+
->willReturnCallback(
512+
function ($path) {
513+
return pathinfo($path);
514+
}
515+
);
516+
$driverMock = $this->getMockBuilder(DriverInterface::class)
517+
->disableOriginalConstructor()
518+
->getMock();
519+
$mediaDirectoryMock->expects($this->any())->method('getDriver')->willReturn($driverMock);
520+
$driverMock->expects($this->once())
521+
->method('fileGetContents')
522+
->willReturn('0123456789abcdefghijklmnopqrstuvwxyz');
523+
$ImageContentInterface = $this->getMockBuilder(ImageContentInterface::class)
524+
->disableOriginalConstructor()
525+
->getMock();
526+
$ImageContentInterface->expects($this->once())
527+
->method('setName')
528+
->willReturnSelf();
529+
$ImageContentInterface->expects($this->once())
530+
->method('setBase64EncodedData')
531+
->willReturnSelf();
532+
$ImageContentInterface->expects($this->once())
533+
->method('setType')
534+
->willReturnSelf();
535+
$this->imageContentInterface->expects($this->once())
536+
->method('create')
537+
->willReturn($ImageContentInterface);
430538
$this->assertEquals([$entryMock], $this->model->getList($productSku));
431539
}
432540
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Api;
9+
10+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
11+
use Magento\Framework\Webapi\Rest\Request;
12+
use Magento\TestFramework\Fixture\DataFixture;
13+
use Magento\TestFramework\Fixture\DataFixtureStorage;
14+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use Magento\TestFramework\TestCase\WebapiAbstract;
17+
18+
class GalleryManagementTest extends WebapiAbstract
19+
{
20+
public const RESOURCE_PATH = '/V1/products/';
21+
22+
/**
23+
* @var DataFixtureStorage
24+
*/
25+
private $fixtures;
26+
27+
protected function setUp(): void
28+
{
29+
$this->objectManager = Bootstrap::getObjectManager();
30+
$this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage();
31+
}
32+
33+
/**
34+
* Check content attribute in getList method
35+
*
36+
* @return void
37+
*/
38+
#[
39+
DataFixture(ProductFixture::class, ['media_gallery_entries' => [['label' => 'image1']]], as: 'product'),
40+
]
41+
public function testContentAttributeInGetList(): void
42+
{
43+
$productSku = $this->fixtures->get('product')->getSku();
44+
$serviceInfo = [
45+
'rest' => [
46+
'resourcePath' => self::RESOURCE_PATH.$productSku."/media",
47+
'httpMethod' => Request::HTTP_METHOD_GET,
48+
],
49+
];
50+
$response = $this->_webApiCall($serviceInfo, []);
51+
$this->assertArrayHasKey('content', $response[0]);
52+
}
53+
54+
/**
55+
* Check content attribute in getList method
56+
*
57+
* @return void
58+
*/
59+
#[
60+
DataFixture(ProductFixture::class, ['media_gallery_entries' => [['label' => 'image1']]], as: 'product'),
61+
]
62+
public function testContentAttributeInGet(): void
63+
{
64+
$product = $this->fixtures->get('product');
65+
$productSku = $product->getSku();
66+
$entryId = $product-> getMediaGalleryEntries()[0]->getId();
67+
$serviceInfo = [
68+
'rest' => [
69+
'resourcePath' => self::RESOURCE_PATH.$productSku."/media/".$entryId,
70+
'httpMethod' => Request::HTTP_METHOD_GET,
71+
],
72+
];
73+
$response = $this->_webApiCall($serviceInfo, []);
74+
$this->assertArrayHasKey('content', $response);
75+
}
76+
}

0 commit comments

Comments
 (0)