Skip to content

Commit 20ef52d

Browse files
author
lakshmana
committed
ACP2E-1526 : Fixed build failures
1 parent 142e224 commit 20ef52d

File tree

2 files changed

+63
-9
lines changed

2 files changed

+63
-9
lines changed

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@
99
use Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface;
1010
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
1111
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Catalog\Model\Product;
1213
use Magento\Framework\Api\Data\ImageContentInterface;
1314
use Magento\Framework\Api\Data\ImageContentInterfaceFactory;
1415
use Magento\Framework\App\Filesystem\DirectoryList;
1516
use Magento\Framework\App\ObjectManager;
17+
use Magento\Framework\Exception\FileSystemException;
1618
use Magento\Framework\Exception\InputException;
1719
use Magento\Framework\Exception\NoSuchEntityException;
1820
use Magento\Framework\Exception\StateException;
1921
use Magento\Framework\Api\ImageContentValidatorInterface;
2022
use Magento\Framework\Filesystem;
2123
use Magento\Framework\Filesystem\Driver\File\Mime;
24+
use Magento\Framework\Filesystem\Io\File;
2225

2326
/**
2427
* Class GalleryManagement
@@ -64,7 +67,12 @@ class GalleryManagement implements \Magento\Catalog\Api\ProductAttributeMediaGal
6467
/**
6568
* @var Mime
6669
*/
67-
protected $imageMime;
70+
protected $mime;
71+
72+
/**
73+
* @var File
74+
*/
75+
protected $file;
6876

6977
/**
7078
* @param ProductRepositoryInterface $productRepository
@@ -73,7 +81,8 @@ class GalleryManagement implements \Magento\Catalog\Api\ProductAttributeMediaGal
7381
* @param DeleteValidator|null $deleteValidator
7482
* @param ImageContentInterfaceFactory|null $imageContentInterface
7583
* @param Filesystem|null $filesystem
76-
* @param Mime|null $imageMime
84+
* @param Mime|null $mime
85+
* @param File|null $file
7786
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
7887
*/
7988
public function __construct(
@@ -83,7 +92,8 @@ public function __construct(
8392
?DeleteValidator $deleteValidator = null,
8493
?ImageContentInterfaceFactory $imageContentInterface = null,
8594
?Filesystem $filesystem = null,
86-
?Mime $imageMime = null
95+
?Mime $mime = null,
96+
?File $file = null
8797
) {
8898
$this->productRepository = $productRepository;
8999
$this->contentValidator = $contentValidator;
@@ -95,8 +105,12 @@ public function __construct(
95105
?? ObjectManager::getInstance()->get(ImageContentInterfaceFactory::class);
96106
$this->filesystem = $filesystem
97107
?? ObjectManager::getInstance()->get(Filesystem::class);
98-
$this->imageMime = $imageMime
108+
$this->mime = $mime
99109
?? ObjectManager::getInstance()->get(Mime::class);
110+
$this->file = $file
111+
?? ObjectManager::getInstance()->get(
112+
File::class
113+
);
100114
}
101115

102116
/**
@@ -249,23 +263,31 @@ public function get($sku, $entryId)
249263
*/
250264
public function getList($sku)
251265
{
252-
/** @var \Magento\Catalog\Model\Product $product */
266+
/** @var Product $product */
253267
$product = $this->productRepository->get($sku);
254268
$mediaGalleryEntries = $product->getMediaGalleryEntries();
255269
foreach ($mediaGalleryEntries as $entry) {
256-
$entry->setContent($this->getImageContent($product, $entry));
270+
$entry->setContent($this->getImageContent($product, $entry));
257271
}
258272
return $mediaGalleryEntries;
259273
}
260274

275+
/**
276+
* Get image content
277+
*
278+
* @param Product $product
279+
* @param ProductAttributeMediaGalleryEntryInterface $entry
280+
* @throws FileSystemException
281+
*/
261282
private function getImageContent($product, $entry): ImageContentInterface
262283
{
263284
$mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
264285
$path = $mediaDirectory->getAbsolutePath($product->getMediaConfig()->getMediaPath($entry->getFile()));
286+
$fileName = $this->file->getPathInfo($path)['basename'];
265287
$imageFileContent = $mediaDirectory->getDriver()->fileGetContents($path);
266288
return $this->imageContentInterface->create()
267-
->setName(basename($entry->getFile()))
289+
->setName($fileName)
268290
->setBase64EncodedData(base64_encode($imageFileContent))
269-
->setType($this->imageMime->getMimeType($path));
291+
->setType($this->mime->getMimeType($path));
270292
}
271293
}

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
1919
use Magento\Framework\Api\AttributeValue;
2020
use Magento\Framework\Api\Data\ImageContentInterface;
21+
use Magento\Framework\Api\Data\ImageContentInterfaceFactory;
2122
use Magento\Framework\Api\ImageContentValidatorInterface;
23+
use Magento\Framework\Filesystem;
24+
use Magento\Framework\Filesystem\Driver\File\Mime;
2225
use PHPUnit\Framework\MockObject\MockObject;
2326
use PHPUnit\Framework\TestCase;
27+
use Magento\Framework\Filesystem\Io\File;
2428

2529
/**
2630
* Tests for \Magento\Catalog\Model\Product\Gallery\GalleryManagement.
@@ -74,6 +78,26 @@ class GalleryManagementTest extends TestCase
7478
*/
7579
private $newProductMock;
7680

81+
/**
82+
* @var ImageContentInterface|MockObject
83+
*/
84+
private $imageContentInterface;
85+
86+
/**
87+
* @var Filesystem|MockObject
88+
*/
89+
private $filesystem;
90+
91+
/**
92+
* @var Mime|MockObject
93+
*/
94+
private $mime;
95+
96+
/**
97+
* @var File|MockObject
98+
*/
99+
private $file;
100+
77101
/**
78102
* @inheritDoc
79103
*/
@@ -83,6 +107,10 @@ protected function setUp(): void
83107
$this->contentValidatorMock = $this->getMockForAbstractClass(ImageContentValidatorInterface::class);
84108
$this->productInterfaceFactory = $this->createMock(ProductInterfaceFactory::class);
85109
$this->deleteValidator = $this->createMock(DeleteValidator::class);
110+
$this->imageContentInterface = $this->createMock(ImageContentInterfaceFactory::class);
111+
$this->filesystem = $this->createMock(Filesystem::class);
112+
$this->mime = $this->createMock(Mime::class);
113+
$this->file = $this->createMock(File::class);
86114
$this->productMock = $this->createPartialMock(
87115
Product::class,
88116
[
@@ -102,7 +130,11 @@ protected function setUp(): void
102130
$this->productRepositoryMock,
103131
$this->contentValidatorMock,
104132
$this->productInterfaceFactory,
105-
$this->deleteValidator
133+
$this->deleteValidator,
134+
$this->imageContentInterface,
135+
$this->filesystem,
136+
$this->mime,
137+
$this->file,
106138
);
107139
$this->attributeValueMock = $this->getMockBuilder(AttributeValue::class)
108140
->disableOriginalConstructor()

0 commit comments

Comments
 (0)