8
8
9
9
namespace Magento \Catalog \Model \Product \Gallery ;
10
10
11
+ use Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterface ;
11
12
use Magento \Catalog \Api \Data \ProductInterface ;
12
13
use Magento \Catalog \Api \ProductRepositoryInterface ;
13
14
use Magento \Catalog \Model \Product ;
14
15
use Magento \Catalog \Model \Product \Media \Config ;
15
16
use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
16
17
use Magento \Catalog \Model \ResourceModel \Product \Gallery ;
17
18
use Magento \Framework \App \Filesystem \DirectoryList ;
19
+ use Magento \Framework \EntityManager \MetadataPool ;
18
20
use Magento \Framework \Filesystem ;
19
21
use Magento \Framework \Filesystem \Directory \WriteInterface ;
20
22
use Magento \Framework \ObjectManagerInterface ;
@@ -84,11 +86,17 @@ class UpdateHandlerTest extends \PHPUnit\Framework\TestCase
84
86
* @var StoreManagerInterface
85
87
*/
86
88
private $ storeManager ;
89
+
87
90
/**
88
91
* @var int
89
92
*/
90
93
private $ currentStoreId ;
91
94
95
+ /**
96
+ * @var MetadataPool
97
+ */
98
+ private $ metadataPool ;
99
+
92
100
/**
93
101
* @inheritdoc
94
102
*/
@@ -108,6 +116,7 @@ protected function setUp(): void
108
116
$ this ->mediaDirectory = $ this ->objectManager ->get (Filesystem::class)
109
117
->getDirectoryWrite (DirectoryList::MEDIA );
110
118
$ this ->mediaDirectory ->writeFile ($ this ->fileName , 'Test ' );
119
+ $ this ->metadataPool = $ this ->objectManager ->get (MetadataPool::class);
111
120
}
112
121
113
122
/**
@@ -351,6 +360,23 @@ public function testExecuteWithTwoImagesOnStoreView(): void
351
360
}
352
361
}
353
362
363
+ /**
364
+ * @magentoDataFixture Magento/Catalog/_files/product_with_image.php
365
+ * @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
366
+ *
367
+ * @return void
368
+ */
369
+ public function testDeleteSharedImage (): void
370
+ {
371
+ $ product = $ this ->getProduct (null , 'simple ' );
372
+ $ this ->duplicateMediaGalleryForProduct ('/m/a/magento_image.jpg ' , 'simple2 ' );
373
+ $ secondProduct = $ this ->getProduct (null , 'simple2 ' );
374
+ $ this ->updateHandler ->execute ($ this ->prepareRemoveImage ($ product ), []);
375
+ $ product = $ this ->getProduct (null , 'simple ' );
376
+ $ this ->assertEmpty ($ product ->getMediaGalleryImages ()->getItems ());
377
+ $ this ->checkProductImageExist ($ secondProduct , '/m/a/magento_image.jpg ' );
378
+ }
379
+
354
380
/**
355
381
* @inheritdoc
356
382
*/
@@ -371,11 +397,13 @@ protected function tearDown(): void
371
397
* Returns current product.
372
398
*
373
399
* @param int|null $storeId
400
+ * @param string|null $sku
374
401
* @return ProductInterface|Product
375
402
*/
376
- private function getProduct (?int $ storeId = null ): ProductInterface
403
+ private function getProduct (?int $ storeId = null , ? string $ sku = null ): ProductInterface
377
404
{
378
- return $ this ->productRepository ->get ('simple ' , false , $ storeId , true );
405
+ $ sku = $ sku ?: 'simple ' ;
406
+ return $ this ->productRepository ->get ($ sku , false , $ storeId , true );
379
407
}
380
408
381
409
/**
@@ -464,6 +492,86 @@ public function testDeleteWithMultiWebsites(): void
464
492
$ this ->assertArrayNotHasKey ($ secondStoreId , $ imageRolesPerStore );
465
493
}
466
494
495
+ /**
496
+ * Check product image link and product image exist
497
+ *
498
+ * @param ProductInterface $product
499
+ * @param string $imagePath
500
+ * @return void
501
+ */
502
+ private function checkProductImageExist (ProductInterface $ product , string $ imagePath ): void
503
+ {
504
+ $ productImageItem = $ product ->getMediaGalleryImages ()->getFirstItem ();
505
+ $ this ->assertEquals ($ imagePath , $ productImageItem ->getFile ());
506
+ $ productImageFile = $ productImageItem ->getPath ();
507
+ $ this ->assertNotEmpty ($ productImageFile );
508
+ $ this ->assertTrue ($ this ->mediaDirectory ->getDriver ()->isExists ($ productImageFile ));
509
+ $ this ->fileName = $ productImageFile ;
510
+ }
511
+
512
+ /**
513
+ * Prepare the product to remove image
514
+ *
515
+ * @param ProductInterface $product
516
+ * @return ProductInterface
517
+ */
518
+ private function prepareRemoveImage (ProductInterface $ product ): ProductInterface
519
+ {
520
+ $ item = $ product ->getMediaGalleryImages ()->getFirstItem ();
521
+ $ item ->setRemoved ('1 ' );
522
+ $ galleryData = [
523
+ 'images ' => [
524
+ (int )$ item ->getValueId () => $ item ->getData (),
525
+ ]
526
+ ];
527
+ $ product ->setData (ProductInterface::MEDIA_GALLERY , $ galleryData );
528
+ $ product ->setStoreId (0 );
529
+
530
+ return $ product ;
531
+ }
532
+
533
+ /**
534
+ * Duplicate media gallery entries for a product
535
+ *
536
+ * @param string $imagePath
537
+ * @param string $productSku
538
+ * @return void
539
+ */
540
+ private function duplicateMediaGalleryForProduct (string $ imagePath , string $ productSku ): void
541
+ {
542
+ $ product = $ this ->getProduct (null , $ productSku );
543
+ $ connect = $ this ->galleryResource ->getConnection ();
544
+ $ select = $ connect ->select ()->from ($ this ->galleryResource ->getMainTable ())->where ('value = ? ' , $ imagePath );
545
+ $ result = $ connect ->fetchRow ($ select );
546
+ $ value_id = $ result ['value_id ' ];
547
+ unset($ result ['value_id ' ]);
548
+ $ rows = [
549
+ 'attribute_id ' => $ result ['attribute_id ' ],
550
+ 'value ' => $ result ['value ' ],
551
+ ProductAttributeMediaGalleryEntryInterface::MEDIA_TYPE => $ result ['media_type ' ],
552
+ ProductAttributeMediaGalleryEntryInterface::DISABLED => $ result ['disabled ' ],
553
+ ];
554
+ $ connect ->insert ($ this ->galleryResource ->getMainTable (), $ rows );
555
+ $ select = $ connect ->select ()
556
+ ->from ($ this ->galleryResource ->getTable (Gallery::GALLERY_VALUE_TABLE ))
557
+ ->where ('value_id = ? ' , $ value_id );
558
+ $ result = $ connect ->fetchRow ($ select );
559
+ $ newValueId = (int )$ value_id + 1 ;
560
+ $ metadata = $ this ->metadataPool ->getMetadata (ProductInterface::class);
561
+ $ linkField = $ metadata ->getLinkField ();
562
+ $ rows = [
563
+ 'value_id ' => $ newValueId ,
564
+ 'store_id ' => $ result ['store_id ' ],
565
+ ProductAttributeMediaGalleryEntryInterface::LABEL => $ result ['label ' ],
566
+ ProductAttributeMediaGalleryEntryInterface::POSITION => $ result ['position ' ],
567
+ ProductAttributeMediaGalleryEntryInterface::DISABLED => $ result ['disabled ' ],
568
+ $ linkField => $ product ->getData ($ linkField ),
569
+ ];
570
+ $ connect ->insert ($ this ->galleryResource ->getTable (Gallery::GALLERY_VALUE_TABLE ), $ rows );
571
+ $ rows = ['value_id ' => $ newValueId , $ linkField => $ product ->getData ($ linkField )];
572
+ $ connect ->insert ($ this ->galleryResource ->getTable (Gallery::GALLERY_VALUE_TO_ENTITY_TABLE ), $ rows );
573
+ }
574
+
467
575
/**
468
576
* @param Product $product
469
577
* @param array $roles
0 commit comments