Skip to content

Commit 4a66488

Browse files
authored
Merge pull request #8144 from magento-l3/FEB132023_PR_arul
[L3 Kings] Bugfix delivery
2 parents 405bee6 + 9ba473b commit 4a66488

File tree

20 files changed

+853
-59
lines changed

20 files changed

+853
-59
lines changed

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

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +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;
13+
use Magento\Framework\Api\Data\ImageContentInterface;
14+
use Magento\Framework\Api\Data\ImageContentInterfaceFactory;
15+
use Magento\Framework\App\Filesystem\DirectoryList;
1216
use Magento\Framework\App\ObjectManager;
17+
use Magento\Framework\Exception\FileSystemException;
1318
use Magento\Framework\Exception\InputException;
1419
use Magento\Framework\Exception\NoSuchEntityException;
1520
use Magento\Framework\Exception\StateException;
1621
use Magento\Framework\Api\ImageContentValidatorInterface;
22+
use Magento\Framework\Filesystem;
23+
use Magento\Framework\Filesystem\Driver\File\Mime;
24+
use Magento\Framework\Filesystem\Io\File;
1725

1826
/**
1927
* Class GalleryManagement
@@ -44,25 +52,65 @@ class GalleryManagement implements \Magento\Catalog\Api\ProductAttributeMediaGal
4452
*/
4553
private $deleteValidator;
4654

55+
/**
56+
* @var ImageContentInterfaceFactory
57+
*/
58+
private $imageContentInterface;
59+
60+
/**
61+
* Filesystem facade
62+
*
63+
* @var Filesystem
64+
*/
65+
private $filesystem;
66+
67+
/**
68+
* @var Mime
69+
*/
70+
private $mime;
71+
72+
/**
73+
* @var File
74+
*/
75+
private $file;
76+
4777
/**
4878
* @param ProductRepositoryInterface $productRepository
4979
* @param ImageContentValidatorInterface $contentValidator
5080
* @param ProductInterfaceFactory|null $productInterfaceFactory
5181
* @param DeleteValidator|null $deleteValidator
82+
* @param ImageContentInterfaceFactory|null $imageContentInterface
83+
* @param Filesystem|null $filesystem
84+
* @param Mime|null $mime
85+
* @param File|null $file
5286
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
5387
*/
5488
public function __construct(
5589
ProductRepositoryInterface $productRepository,
5690
ImageContentValidatorInterface $contentValidator,
5791
?ProductInterfaceFactory $productInterfaceFactory = null,
58-
?DeleteValidator $deleteValidator = null
92+
?DeleteValidator $deleteValidator = null,
93+
?ImageContentInterfaceFactory $imageContentInterface = null,
94+
?Filesystem $filesystem = null,
95+
?Mime $mime = null,
96+
?File $file = null
5997
) {
6098
$this->productRepository = $productRepository;
6199
$this->contentValidator = $contentValidator;
62100
$this->productInterfaceFactory = $productInterfaceFactory
63101
?? ObjectManager::getInstance()->get(ProductInterfaceFactory::class);
64102
$this->deleteValidator = $deleteValidator
65103
?? ObjectManager::getInstance()->get(DeleteValidator::class);
104+
$this->imageContentInterface = $imageContentInterface
105+
?? ObjectManager::getInstance()->get(ImageContentInterfaceFactory::class);
106+
$this->filesystem = $filesystem
107+
?? ObjectManager::getInstance()->get(Filesystem::class);
108+
$this->mime = $mime
109+
?? ObjectManager::getInstance()->get(Mime::class);
110+
$this->file = $file
111+
?? ObjectManager::getInstance()->get(
112+
File::class
113+
);
66114
}
67115

68116
/**
@@ -195,6 +243,7 @@ public function remove($sku, $entryId)
195243
public function get($sku, $entryId)
196244
{
197245
try {
246+
/** @var Product $product */
198247
$product = $this->productRepository->get($sku);
199248
} catch (\Exception $exception) {
200249
throw new NoSuchEntityException(__("The product doesn't exist. Verify and try again."));
@@ -203,6 +252,7 @@ public function get($sku, $entryId)
203252
$mediaGalleryEntries = $product->getMediaGalleryEntries();
204253
foreach ($mediaGalleryEntries as $entry) {
205254
if ($entry->getId() == $entryId) {
255+
$entry->setContent($this->getImageContent($product, $entry));
206256
return $entry;
207257
}
208258
}
@@ -215,9 +265,32 @@ public function get($sku, $entryId)
215265
*/
216266
public function getList($sku)
217267
{
218-
/** @var \Magento\Catalog\Model\Product $product */
268+
/** @var Product $product */
219269
$product = $this->productRepository->get($sku);
270+
$mediaGalleryEntries = $product->getMediaGalleryEntries();
271+
foreach ($mediaGalleryEntries as $entry) {
272+
$entry->setContent($this->getImageContent($product, $entry));
273+
}
274+
return $mediaGalleryEntries;
275+
}
220276

221-
return $product->getMediaGalleryEntries();
277+
/**
278+
* Get image content
279+
*
280+
* @param Product $product
281+
* @param ProductAttributeMediaGalleryEntryInterface $entry
282+
* @return ImageContentInterface
283+
* @throws FileSystemException
284+
*/
285+
private function getImageContent($product, $entry): ImageContentInterface
286+
{
287+
$mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
288+
$path = $mediaDirectory->getAbsolutePath($product->getMediaConfig()->getMediaPath($entry->getFile()));
289+
$fileName = $this->file->getPathInfo($path)['basename'];
290+
$imageFileContent = $mediaDirectory->getDriver()->fileGetContents($path);
291+
return $this->imageContentInterface->create()
292+
->setName($fileName)
293+
->setBase64EncodedData(base64_encode($imageFileContent))
294+
->setType($this->mime->getMimeType($path));
222295
}
223296
}

app/code/Magento/Catalog/Model/ResourceModel/AbstractResource.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
abstract class AbstractResource extends \Magento\Eav\Model\Entity\AbstractEntity
2525
{
2626
/**
27-
* Store manager
27+
* Store manager to get the store information
2828
*
2929
* @var \Magento\Store\Model\StoreManagerInterface
3030
*/
3131
protected $_storeManager;
3232

3333
/**
34-
* Model factory
34+
* Model factory to create a model object
3535
*
3636
* @var \Magento\Catalog\Model\Factory
3737
*/
@@ -325,7 +325,25 @@ protected function _insertAttribute($object, $attribute, $value)
325325
*/
326326
protected function _updateAttribute($object, $attribute, $valueId, $value)
327327
{
328-
return $this->_saveAttributeValue($object, $attribute, $value);
328+
$entity = $attribute->getEntity();
329+
$row = $this->getAttributeRow($entity, $object, $attribute);
330+
$hasSingleStore = $this->_storeManager->hasSingleStore();
331+
$storeId = $hasSingleStore
332+
? $this->getDefaultStoreId()
333+
: (int) $this->_storeManager->getStore($object->getStoreId())->getId();
334+
if ($valueId > 0 && array_key_exists('store_id', $row) && $storeId === $row['store_id']) {
335+
$table = $attribute->getBackend()->getTable();
336+
$connection = $this->getConnection();
337+
$connection->update(
338+
$table,
339+
['value' => $this->_prepareValueForSave($value, $attribute)],
340+
sprintf('%s=%d', $connection->quoteIdentifier('value_id'), $valueId)
341+
);
342+
343+
return $this;
344+
} else {
345+
return $this->_saveAttributeValue($object, $attribute, $value);
346+
}
329347
}
330348

331349
/**

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

Lines changed: 141 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,17 @@
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\App\Filesystem\DirectoryList;
24+
use Magento\Framework\Filesystem;
25+
use Magento\Framework\Filesystem\Directory\WriteInterface;
26+
use Magento\Framework\Filesystem\Driver\File\Mime;
27+
use Magento\Framework\Filesystem\DriverInterface;
2228
use PHPUnit\Framework\MockObject\MockObject;
2329
use PHPUnit\Framework\TestCase;
30+
use Magento\Framework\Filesystem\Io\File;
31+
use Magento\Catalog\Model\Product\Media\ConfigInterface as MediaConfig;
2432

2533
/**
2634
* Tests for \Magento\Catalog\Model\Product\Gallery\GalleryManagement.
@@ -74,6 +82,26 @@ class GalleryManagementTest extends TestCase
7482
*/
7583
private $newProductMock;
7684

85+
/**
86+
* @var ImageContentInterface|MockObject
87+
*/
88+
private $imageContentInterface;
89+
90+
/**
91+
* @var Filesystem|MockObject
92+
*/
93+
private $filesystem;
94+
95+
/**
96+
* @var Mime|MockObject
97+
*/
98+
private $mime;
99+
100+
/**
101+
* @var File|MockObject
102+
*/
103+
private $file;
104+
77105
/**
78106
* @inheritDoc
79107
*/
@@ -83,6 +111,12 @@ protected function setUp(): void
83111
$this->contentValidatorMock = $this->getMockForAbstractClass(ImageContentValidatorInterface::class);
84112
$this->productInterfaceFactory = $this->createMock(ProductInterfaceFactory::class);
85113
$this->deleteValidator = $this->createMock(DeleteValidator::class);
114+
$this->imageContentInterface = $this->getMockBuilder(ImageContentInterfaceFactory::class)
115+
->disableOriginalConstructor()
116+
->getMock();
117+
$this->filesystem = $this->createMock(Filesystem::class);
118+
$this->mime = $this->createMock(Mime::class);
119+
$this->file = $this->createMock(File::class);
86120
$this->productMock = $this->createPartialMock(
87121
Product::class,
88122
[
@@ -93,7 +127,8 @@ protected function setUp(): void
93127
'getCustomAttribute',
94128
'getMediaGalleryEntries',
95129
'setMediaGalleryEntries',
96-
'getMediaAttributes'
130+
'getMediaAttributes',
131+
'getMediaConfig'
97132
]
98133
);
99134
$this->mediaGalleryEntryMock =
@@ -102,7 +137,11 @@ protected function setUp(): void
102137
$this->productRepositoryMock,
103138
$this->contentValidatorMock,
104139
$this->productInterfaceFactory,
105-
$this->deleteValidator
140+
$this->deleteValidator,
141+
$this->imageContentInterface,
142+
$this->filesystem,
143+
$this->mime,
144+
$this->file,
106145
);
107146
$this->attributeValueMock = $this->getMockBuilder(AttributeValue::class)
108147
->disableOriginalConstructor()
@@ -381,6 +420,55 @@ public function testGet(): void
381420
$existingEntryMock->expects($this->once())->method('getId')->willReturn(42);
382421
$this->productMock->expects($this->once())->method('getMediaGalleryEntries')
383422
->willReturn([$existingEntryMock]);
423+
$mediaConfigMock = $this->getMockBuilder(MediaConfig::class)
424+
->disableOriginalConstructor()
425+
->getMock();
426+
$mediaConfigMock->expects($this->once())
427+
->method('getMediaPath')
428+
->willReturn("base/path/test123.jpg");
429+
$this->productMock->expects($this->once())
430+
->method('getMediaConfig')
431+
->willReturn($mediaConfigMock);
432+
$mediaDirectoryMock = $this->getMockBuilder(WriteInterface::class)
433+
->disableOriginalConstructor()
434+
->getMock();
435+
$this->filesystem->expects($this->once())
436+
->method('getDirectoryWrite')
437+
->with(DirectoryList::MEDIA)
438+
->willReturn($mediaDirectoryMock);
439+
$mediaDirectoryMock->expects($this->once())
440+
->method('getAbsolutePath')
441+
->with('base/path/test123.jpg')
442+
->willReturn('absolute/path/base/path/test123.jpg');
443+
$this->file->expects($this->any())
444+
->method('getPathInfo')
445+
->willReturnCallback(
446+
function ($path) {
447+
return pathinfo($path);
448+
}
449+
);
450+
$driverMock = $this->getMockBuilder(DriverInterface::class)
451+
->disableOriginalConstructor()
452+
->getMock();
453+
$mediaDirectoryMock->expects($this->any())->method('getDriver')->willReturn($driverMock);
454+
$driverMock->expects($this->once())
455+
->method('fileGetContents')
456+
->willReturn('0123456789abcdefghijklmnopqrstuvwxyz');
457+
$ImageContentInterface = $this->getMockBuilder(ImageContentInterface::class)
458+
->disableOriginalConstructor()
459+
->getMock();
460+
$ImageContentInterface->expects($this->once())
461+
->method('setName')
462+
->willReturnSelf();
463+
$ImageContentInterface->expects($this->once())
464+
->method('setBase64EncodedData')
465+
->willReturnSelf();
466+
$ImageContentInterface->expects($this->once())
467+
->method('setType')
468+
->willReturnSelf();
469+
$this->imageContentInterface->expects($this->once())
470+
->method('create')
471+
->willReturn($ImageContentInterface);
384472
$this->assertEquals($existingEntryMock, $this->model->get($productSku, $imageId));
385473
}
386474

@@ -395,6 +483,57 @@ public function testGetList(): void
395483
$entryMock = $this->getMockForAbstractClass(ProductAttributeMediaGalleryEntryInterface::class);
396484
$this->productMock->expects($this->once())->method('getMediaGalleryEntries')
397485
->willReturn([$entryMock]);
486+
$this->productMock->expects($this->once())->method('getMediaGalleryEntries')
487+
->willReturn([$entryMock]);
488+
$mediaConfigMock = $this->getMockBuilder(MediaConfig::class)
489+
->disableOriginalConstructor()
490+
->getMock();
491+
$mediaConfigMock->expects($this->once())
492+
->method('getMediaPath')
493+
->willReturn("base/path/test123.jpg");
494+
$this->productMock->expects($this->once())
495+
->method('getMediaConfig')
496+
->willReturn($mediaConfigMock);
497+
$mediaDirectoryMock = $this->getMockBuilder(WriteInterface::class)
498+
->disableOriginalConstructor()
499+
->getMock();
500+
$this->filesystem->expects($this->once())
501+
->method('getDirectoryWrite')
502+
->with(DirectoryList::MEDIA)
503+
->willReturn($mediaDirectoryMock);
504+
$mediaDirectoryMock->expects($this->once())
505+
->method('getAbsolutePath')
506+
->with('base/path/test123.jpg')
507+
->willReturn('absolute/path/base/path/test123.jpg');
508+
$this->file->expects($this->any())
509+
->method('getPathInfo')
510+
->willReturnCallback(
511+
function ($path) {
512+
return pathinfo($path);
513+
}
514+
);
515+
$driverMock = $this->getMockBuilder(DriverInterface::class)
516+
->disableOriginalConstructor()
517+
->getMock();
518+
$mediaDirectoryMock->expects($this->any())->method('getDriver')->willReturn($driverMock);
519+
$driverMock->expects($this->once())
520+
->method('fileGetContents')
521+
->willReturn('0123456789abcdefghijklmnopqrstuvwxyz');
522+
$ImageContentInterface = $this->getMockBuilder(ImageContentInterface::class)
523+
->disableOriginalConstructor()
524+
->getMock();
525+
$ImageContentInterface->expects($this->once())
526+
->method('setName')
527+
->willReturnSelf();
528+
$ImageContentInterface->expects($this->once())
529+
->method('setBase64EncodedData')
530+
->willReturnSelf();
531+
$ImageContentInterface->expects($this->once())
532+
->method('setType')
533+
->willReturnSelf();
534+
$this->imageContentInterface->expects($this->once())
535+
->method('create')
536+
->willReturn($ImageContentInterface);
398537
$this->assertEquals([$entryMock], $this->model->getList($productSku));
399538
}
400539
}

0 commit comments

Comments
 (0)