Skip to content

Commit 13de5a7

Browse files
🔃 [EngCom] Public Pull Requests - 2.2-develop
Accepted Public Pull Requests: - #18960: local themes should be added to git repo (by @torhoehn) - #18642: [Backport] Fix issue with unexpected changing of subscription status after customer saving (by @alexeya-ven) - #18951: Magento 2.2 Fix Product::addImageToMediaGallery throws Exception (by @progreg) Fixed GitHub Issues: - #18949: dev/tools/grunt/configs/themes.js gets replaced after update magento (reported by @iget-master) has been fixed in #18960 by @torhoehn in 2.2-develop branch Related commits: 1. bc485de - #6803: Product::addImageToMediaGallery throws Exception (reported by @R4c00n) has been fixed in #18951 by @progreg in 2.2-develop branch Related commits: 1. 4df596b 2. fedacf9
2 parents 32a7c4b + 5218dd9 commit 13de5a7

File tree

8 files changed

+201
-19
lines changed

8 files changed

+201
-19
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ atlassian*
3333
/.php_cs
3434
/.php_cs.cache
3535
/grunt-config.json
36-
/dev/tools/grunt/configs/local-themes.js
3736

3837
/pub/media/*.*
3938
!/pub/media/.htaccess

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

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
*/
66
namespace Magento\Catalog\Model\Product\Gallery;
77

8+
use Magento\Framework\Api\Data\ImageContentInterface;
89
use Magento\Framework\App\Filesystem\DirectoryList;
910
use Magento\Framework\Exception\LocalizedException;
10-
use Magento\Framework\Filesystem\DriverInterface;
11+
use Magento\Framework\App\ObjectManager;
1112

1213
/**
1314
* Catalog product Media Gallery attribute processor.
@@ -55,28 +56,39 @@ class Processor
5556
*/
5657
protected $resourceModel;
5758

59+
/**
60+
* @var \Magento\Framework\File\Mime
61+
*/
62+
private $mime;
63+
5864
/**
5965
* @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository
6066
* @param \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDb
6167
* @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
6268
* @param \Magento\Framework\Filesystem $filesystem
6369
* @param \Magento\Catalog\Model\ResourceModel\Product\Gallery $resourceModel
70+
* @param \Magento\Framework\File\Mime|null $mime
71+
* @throws \Magento\Framework\Exception\FileSystemException
6472
*/
6573
public function __construct(
6674
\Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository,
6775
\Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDb,
6876
\Magento\Catalog\Model\Product\Media\Config $mediaConfig,
6977
\Magento\Framework\Filesystem $filesystem,
70-
\Magento\Catalog\Model\ResourceModel\Product\Gallery $resourceModel
78+
\Magento\Catalog\Model\ResourceModel\Product\Gallery $resourceModel,
79+
\Magento\Framework\File\Mime $mime = null
7180
) {
7281
$this->attributeRepository = $attributeRepository;
7382
$this->fileStorageDb = $fileStorageDb;
7483
$this->mediaConfig = $mediaConfig;
7584
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
7685
$this->resourceModel = $resourceModel;
86+
$this->mime = $mime ?: ObjectManager::getInstance()->get(\Magento\Framework\File\Mime::class);
7787
}
7888

7989
/**
90+
* Return media_gallery attribute
91+
*
8092
* @return \Magento\Catalog\Api\Data\ProductAttributeInterface
8193
* @since 101.0.0
8294
*/
@@ -178,6 +190,13 @@ public function addImage(
178190
$attrCode = $this->getAttribute()->getAttributeCode();
179191
$mediaGalleryData = $product->getData($attrCode);
180192
$position = 0;
193+
194+
$absoluteFilePath = $this->mediaDirectory->getAbsolutePath($file);
195+
$imageMimeType = $this->mime->getMimeType($absoluteFilePath);
196+
$imageContent = $this->mediaDirectory->readFile($absoluteFilePath);
197+
$imageBase64 = base64_encode($imageContent);
198+
$imageName = $pathinfo['filename'];
199+
181200
if (!is_array($mediaGalleryData)) {
182201
$mediaGalleryData = ['images' => []];
183202
}
@@ -192,9 +211,17 @@ public function addImage(
192211
$mediaGalleryData['images'][] = [
193212
'file' => $fileName,
194213
'position' => $position,
195-
'media_type' => 'image',
196214
'label' => '',
197215
'disabled' => (int)$exclude,
216+
'media_type' => 'image',
217+
'types' => $mediaAttribute,
218+
'content' => [
219+
'data' => [
220+
ImageContentInterface::NAME => $imageName,
221+
ImageContentInterface::BASE64_ENCODED_DATA => $imageBase64,
222+
ImageContentInterface::TYPE => $imageMimeType,
223+
]
224+
]
198225
];
199226

200227
$product->setData($attrCode, $mediaGalleryData);
@@ -353,7 +380,8 @@ public function setMediaAttribute(\Magento\Catalog\Model\Product $product, $medi
353380
}
354381

355382
/**
356-
* get media attribute codes
383+
* Get media attribute codes
384+
*
357385
* @return array
358386
* @since 101.0.0
359387
*/
@@ -363,6 +391,8 @@ public function getMediaAttributeCodes()
363391
}
364392

365393
/**
394+
* Trim .tmp ending from filename
395+
*
366396
* @param string $file
367397
* @return string
368398
* @since 101.0.0
@@ -484,7 +514,6 @@ public function getAffectedFields($object)
484514
/**
485515
* Attribute value is not to be saved in a conventional way, separate table is used to store the complex value
486516
*
487-
* {@inheritdoc}
488517
* @since 101.0.0
489518
*/
490519
public function isScalar()

app/code/Magento/Newsletter/Model/ResourceModel/Subscriber.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Newsletter\Model\ResourceModel;
77

8+
use Magento\Store\Model\StoreManagerInterface;
9+
use Magento\Framework\App\ObjectManager;
10+
811
/**
912
* Newsletter subscriber resource model
1013
*
@@ -48,22 +51,33 @@ class Subscriber extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
4851
*/
4952
protected $mathRandom;
5053

54+
/**
55+
* Store manager
56+
*
57+
* @var StoreManagerInterface
58+
*/
59+
private $storeManager;
60+
5161
/**
5262
* Construct
5363
*
5464
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
5565
* @param \Magento\Framework\Stdlib\DateTime\DateTime $date
5666
* @param \Magento\Framework\Math\Random $mathRandom
5767
* @param string $connectionName
68+
* @param StoreManagerInterface $storeManager
5869
*/
5970
public function __construct(
6071
\Magento\Framework\Model\ResourceModel\Db\Context $context,
6172
\Magento\Framework\Stdlib\DateTime\DateTime $date,
6273
\Magento\Framework\Math\Random $mathRandom,
63-
$connectionName = null
74+
$connectionName = null,
75+
StoreManagerInterface $storeManager = null
6476
) {
6577
$this->_date = $date;
6678
$this->mathRandom = $mathRandom;
79+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
80+
->get(StoreManagerInterface::class);
6781
parent::__construct($context, $connectionName);
6882
}
6983

@@ -118,6 +132,9 @@ public function loadByEmail($subscriberEmail)
118132
*/
119133
public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer)
120134
{
135+
$storeId = (int)$customer->getStoreId() ?: $this->storeManager
136+
->getWebsite($customer->getWebsiteId())->getDefaultStore()->getId();
137+
121138
$select = $this->connection
122139
->select()
123140
->from($this->getMainTable())
@@ -128,7 +145,7 @@ public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface
128145
$select,
129146
[
130147
'customer_id' => $customer->getId(),
131-
'store_id' => $customer->getStoreId()
148+
'store_id' => $storeId
132149
]
133150
);
134151

@@ -146,7 +163,7 @@ public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface
146163
$select,
147164
[
148165
'subscriber_email' => $customer->getEmail(),
149-
'store_id' => $customer->getStoreId()
166+
'store_id' => $storeId
150167
]
151168
);
152169

app/code/Magento/Newsletter/Model/Subscriber.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -612,16 +612,17 @@ protected function _updateCustomerSubscription($customerId, $subscribe)
612612

613613
$this->setStatus($status);
614614

615+
$storeId = $customerData->getStoreId();
616+
if ((int)$customerData->getStoreId() === 0) {
617+
$storeId = $this->_storeManager->getWebsite($customerData->getWebsiteId())->getDefaultStore()->getId();
618+
}
619+
615620
if (!$this->getId()) {
616-
$storeId = $customerData->getStoreId();
617-
if ($customerData->getStoreId() == 0) {
618-
$storeId = $this->_storeManager->getWebsite($customerData->getWebsiteId())->getDefaultStore()->getId();
619-
}
620621
$this->setStoreId($storeId)
621622
->setCustomerId($customerData->getId())
622623
->setEmail($customerData->getEmail());
623624
} else {
624-
$this->setStoreId($customerData->getStoreId())
625+
$this->setStoreId($storeId)
625626
->setEmail($customerData->getEmail());
626627
}
627628

app/code/Magento/Newsletter/Test/Unit/Model/SubscriberTest.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ public function testSubscribeNotLoggedIn()
211211

212212
public function testUpdateSubscription()
213213
{
214+
$storeId = 2;
214215
$customerId = 1;
215216
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
216217
->getMock();
@@ -232,7 +233,7 @@ public function testUpdateSubscription()
232233
->method('getConfirmationStatus')
233234
->with($customerId)
234235
->willReturn('account_confirmation_required');
235-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
236+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
236237
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
237238

238239
$storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
@@ -246,6 +247,7 @@ public function testUpdateSubscription()
246247

247248
public function testUnsubscribeCustomerById()
248249
{
250+
$storeId = 2;
249251
$customerId = 1;
250252
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
251253
->getMock();
@@ -263,7 +265,7 @@ public function testUnsubscribeCustomerById()
263265
);
264266
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
265267
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
266-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
268+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
267269
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
268270
$this->sendEmailCheck();
269271

@@ -272,6 +274,7 @@ public function testUnsubscribeCustomerById()
272274

273275
public function testSubscribeCustomerById()
274276
{
277+
$storeId = 2;
275278
$customerId = 1;
276279
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
277280
->getMock();
@@ -289,7 +292,7 @@ public function testSubscribeCustomerById()
289292
);
290293
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
291294
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
292-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
295+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
293296
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
294297
$this->sendEmailCheck();
295298

@@ -298,6 +301,7 @@ public function testSubscribeCustomerById()
298301

299302
public function testSubscribeCustomerById1()
300303
{
304+
$storeId = 2;
301305
$customerId = 1;
302306
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
303307
->getMock();
@@ -315,7 +319,7 @@ public function testSubscribeCustomerById1()
315319
);
316320
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
317321
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
318-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
322+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
319323
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
320324
$this->sendEmailCheck();
321325
$this->customerAccountManagement->expects($this->once())
@@ -329,6 +333,7 @@ public function testSubscribeCustomerById1()
329333

330334
public function testSubscribeCustomerByIdAfterConfirmation()
331335
{
336+
$storeId = 2;
332337
$customerId = 1;
333338
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
334339
->getMock();
@@ -346,7 +351,7 @@ public function testSubscribeCustomerByIdAfterConfirmation()
346351
);
347352
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
348353
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
349-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
354+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
350355
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
351356
$this->sendEmailCheck();
352357
$this->customerAccountManagement->expects($this->never())->method('getConfirmationStatus');

dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,52 @@ public function testFilterByStoreId()
198198

199199
$this->assertGreaterThanOrEqual(1, $count);
200200
}
201+
202+
/**
203+
* Test save product with gallery image
204+
*
205+
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_image.php
206+
*
207+
* @throws \Magento\Framework\Exception\CouldNotSaveException
208+
* @throws \Magento\Framework\Exception\InputException
209+
* @throws \Magento\Framework\Exception\StateException
210+
*/
211+
public function testSaveProductWithGalleryImage()
212+
{
213+
/** @var $mediaConfig \Magento\Catalog\Model\Product\Media\Config */
214+
$mediaConfig = Bootstrap::getObjectManager()
215+
->get(\Magento\Catalog\Model\Product\Media\Config::class);
216+
217+
/** @var $mediaDirectory \Magento\Framework\Filesystem\Directory\WriteInterface */
218+
$mediaDirectory = Bootstrap::getObjectManager()
219+
->get(\Magento\Framework\Filesystem::class)
220+
->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
221+
222+
$product = Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
223+
$product->load(1);
224+
225+
$path = $mediaConfig->getBaseMediaPath() . '/magento_image.jpg';
226+
$absolutePath = $mediaDirectory->getAbsolutePath() . $path;
227+
$product->addImageToMediaGallery($absolutePath, [
228+
'image',
229+
'small_image',
230+
], false, false);
231+
232+
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
233+
$productRepository = Bootstrap::getObjectManager()
234+
->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
235+
$productRepository->save($product);
236+
237+
$gallery = $product->getData('media_gallery');
238+
$this->assertArrayHasKey('images', $gallery);
239+
$images = array_values($gallery['images']);
240+
241+
$this->assertNotEmpty($gallery);
242+
$this->assertTrue(isset($images[0]['file']));
243+
$this->assertStringStartsWith('/m/a/magento_image', $images[0]['file']);
244+
$this->assertArrayHasKey('media_type', $images[0]);
245+
$this->assertEquals('image', $images[0]['media_type']);
246+
$this->assertStringStartsWith('/m/a/magento_image', $product->getData('image'));
247+
$this->assertStringStartsWith('/m/a/magento_image', $product->getData('small_image'));
248+
}
201249
}

0 commit comments

Comments
 (0)