Skip to content

Commit 2130851

Browse files
committed
Merge branch 'ACP2E-122' of https://github.com/magento-l3/magento2ce into PR-2021-20-08
2 parents 5ace3df + 1603cf1 commit 2130851

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ private function processMediaAttribute(
550550
* @param array $clearImages
551551
* @param array $newImages
552552
* @param array $existImages
553+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
553554
*/
554555
private function processMediaAttributeLabel(
555556
Product $product,
@@ -571,6 +572,9 @@ private function processMediaAttributeLabel(
571572

572573
if (in_array($attrData, array_keys($existImages)) && isset($existImages[$attrData]['label'])) {
573574
$product->setData($mediaAttrCode . '_label', $existImages[$attrData]['label']);
575+
if ($existImages[$attrData]['label'] == null) {
576+
$resetLabel = true;
577+
}
574578
}
575579

576580
if ($attrData === 'no_selection' && !empty($product->getData($mediaAttrCode . '_label'))) {

dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Gallery/UpdateHandlerTest.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
1717
use Magento\Catalog\Model\ResourceModel\Product\Gallery;
1818
use Magento\Framework\App\Filesystem\DirectoryList;
19+
use Magento\Framework\Exception\ConfigurationMismatchException;
20+
use Magento\Framework\Exception\CouldNotSaveException;
21+
use Magento\Framework\Exception\FileSystemException;
22+
use Magento\Framework\Exception\InputException;
23+
use Magento\Framework\Exception\LocalizedException;
24+
use Magento\Framework\Exception\NoSuchEntityException;
25+
use Magento\Framework\Exception\StateException;
1926
use Magento\Framework\Exception\ValidatorException;
2027
use Magento\Framework\EntityManager\MetadataPool;
2128
use Magento\Framework\Filesystem;
@@ -25,13 +32,14 @@
2532
use Magento\Store\Model\Store;
2633
use Magento\Store\Model\StoreManagerInterface;
2734
use Magento\TestFramework\Helper\Bootstrap;
35+
use PHPUnit\Framework\TestCase;
2836

2937
/**
3038
* Provides tests for media gallery images update during product save.
3139
*
3240
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3341
*/
34-
class UpdateHandlerTest extends \PHPUnit\Framework\TestCase
42+
class UpdateHandlerTest extends TestCase
3543
{
3644
/**
3745
* @var ObjectManagerInterface
@@ -133,6 +141,8 @@ protected function setUp(): void
133141
* @magentoDataFixture Magento/Catalog/_files/product_image.php
134142
*
135143
* @return void
144+
* @throws LocalizedException
145+
* @throws NoSuchEntityException
136146
*/
137147
public function testExecuteWithIllegalFilename(): void
138148
{
@@ -161,6 +171,8 @@ public function testExecuteWithIllegalFilename(): void
161171
* @magentoDataFixture Magento/Catalog/_files/product_with_image.php
162172
* @magentoDbIsolation enabled
163173
* @return void
174+
* @throws LocalizedException
175+
* @throws NoSuchEntityException
164176
*/
165177
public function testExecuteWithOneImage(): void
166178
{
@@ -176,6 +188,38 @@ public function testExecuteWithOneImage(): void
176188
$this->assertEquals('1', $updatedImage['disabled_default']);
177189
}
178190

191+
/**
192+
* Tests updating image label and label default during product save.
193+
*
194+
* @magentoDataFixture Magento/Catalog/_files/product_with_image.php
195+
* @magentoDbIsolation enabled
196+
* @return void
197+
* @throws LocalizedException
198+
*/
199+
public function testExecuteImageWithUpdatedAttributeLabel(): void
200+
{
201+
$product = $this->getProduct();
202+
$productImages = $this->galleryResource->loadProductGalleryByAttributeId($product, $this->mediaAttributeId);
203+
$updatedImage = reset($productImages);
204+
$this->assertIsArray($updatedImage);
205+
$this->assertEquals('Image Alt Text', $updatedImage['label']);
206+
$this->assertEquals('Image Alt Text', $updatedImage['label_default']);
207+
$this->updateProductGalleryImages($product, ['label' => 'New image']);
208+
$this->updateHandler->execute($product);
209+
$productImages = $this->galleryResource->loadProductGalleryByAttributeId($product, $this->mediaAttributeId);
210+
$updatedImage = reset($productImages);
211+
$this->assertIsArray($updatedImage);
212+
$this->assertEquals('New image', $updatedImage['label']);
213+
$this->assertEquals('New image', $updatedImage['label_default']);
214+
$this->updateProductGalleryImages($product, ['label' => '']);
215+
$this->updateHandler->execute($product);
216+
$productImages = $this->galleryResource->loadProductGalleryByAttributeId($product, $this->mediaAttributeId);
217+
$updatedImage = reset($productImages);
218+
$this->assertIsArray($updatedImage);
219+
$this->assertEquals('', $updatedImage['label']);
220+
$this->assertEquals('', $updatedImage['label_default']);
221+
}
222+
179223
/**
180224
* Tests updating image roles during product save.
181225
*
@@ -184,6 +228,7 @@ public function testExecuteWithOneImage(): void
184228
* @magentoDbIsolation enabled
185229
* @param array $roles
186230
* @return void
231+
* @throws LocalizedException
187232
*/
188233
public function testExecuteWithTwoImagesAndDifferentRoles(array $roles): void
189234
{
@@ -208,6 +253,9 @@ public function testExecuteWithTwoImagesAndDifferentRoles(array $roles): void
208253
* @magentoDbIsolation enabled
209254
* @param array $roles
210255
* @return void
256+
* @throws LocalizedException
257+
* @throws ConfigurationMismatchException
258+
* @throws NoSuchEntityException
211259
*/
212260
public function testExecuteWithTwoImagesAndDifferentRolesOnStoreView(array $roles): void
213261
{
@@ -275,6 +323,7 @@ public function executeWithTwoImagesAndRolesDataProvider(): array
275323
* @magentoDataFixture Magento/Catalog/_files/product_with_multiple_images.php
276324
* @magentoDbIsolation enabled
277325
* @return void
326+
* @throws LocalizedException
278327
*/
279328
public function testExecuteWithTwoImagesAndChangedPosition(): void
280329
{
@@ -303,6 +352,7 @@ public function testExecuteWithTwoImagesAndChangedPosition(): void
303352
* @magentoDataFixture Magento/Catalog/_files/product_with_image.php
304353
* @magentoDbIsolation enabled
305354
* @return void
355+
* @throws LocalizedException
306356
*/
307357
public function testExecuteWithImageToDelete(): void
308358
{
@@ -332,6 +382,8 @@ public function testExecuteWithImageToDelete(): void
332382
* @magentoDataFixture Magento/Store/_files/second_store.php
333383
* @magentoDbIsolation enabled
334384
* @return void
385+
* @throws LocalizedException
386+
* @throws NoSuchEntityException
335387
*/
336388
public function testExecuteWithTwoImagesOnStoreView(): void
337389
{
@@ -382,6 +434,7 @@ public function testExecuteWithTwoImagesOnStoreView(): void
382434
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
383435
*
384436
* @return void
437+
* @throws LocalizedException
385438
*/
386439
public function testDeleteSharedImage(): void
387440
{
@@ -416,6 +469,7 @@ protected function tearDown(): void
416469
* @param int|null $storeId
417470
* @param string|null $sku
418471
* @return ProductInterface|Product
472+
* @throws NoSuchEntityException
419473
*/
420474
private function getProduct(?int $storeId = null, ?string $sku = null): ProductInterface
421475
{
@@ -441,6 +495,11 @@ private function updateProductGalleryImages(ProductInterface $product, array $im
441495
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
442496
* @magentoDbIsolation disabled
443497
* @return void
498+
* @throws LocalizedException
499+
* @throws NoSuchEntityException
500+
* @throws CouldNotSaveException
501+
* @throws InputException
502+
* @throws StateException
444503
*/
445504
public function testDeleteWithMultiWebsites(): void
446505
{
@@ -518,6 +577,9 @@ public function testDeleteWithMultiWebsites(): void
518577
* @param string $expectedFile
519578
* @param bool $exist
520579
* @return void
580+
* @throws LocalizedException
581+
* @throws NoSuchEntityException
582+
* @throws FileSystemException
521583
*/
522584
public function testUpdateImage(string $newFile, string $expectedFile, bool $exist): void
523585
{
@@ -607,6 +669,8 @@ public function updateImageDataProvider(): array
607669
* @param array $expectedImages
608670
* @param array $select
609671
* @return void
672+
* @throws LocalizedException
673+
* @throws NoSuchEntityException
610674
*/
611675
public function testAddImages(
612676
string $addFromStore,
@@ -697,6 +761,7 @@ public function addImagesDataProvider(): array
697761
* @param ProductInterface $product
698762
* @param string $imagePath
699763
* @return void
764+
* @throws FileSystemException
700765
*/
701766
private function checkProductImageExist(ProductInterface $product, string $imagePath): void
702767
{
@@ -735,6 +800,8 @@ private function prepareRemoveImage(ProductInterface $product): ProductInterface
735800
* @param string $imagePath
736801
* @param string $productSku
737802
* @return void
803+
* @throws LocalizedException
804+
* @throws NoSuchEntityException
738805
*/
739806
private function duplicateMediaGalleryForProduct(string $imagePath, string $productSku): void
740807
{

0 commit comments

Comments
 (0)