16
16
use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
17
17
use Magento \Catalog \Model \ResourceModel \Product \Gallery ;
18
18
use Magento \Framework \App \Filesystem \DirectoryList ;
19
+ use Magento \Framework \EntityManager \MetadataPool ;
19
20
use Magento \Framework \Filesystem ;
20
21
use Magento \Framework \Filesystem \Directory \WriteInterface ;
21
22
use Magento \Framework \ObjectManagerInterface ;
@@ -90,6 +91,9 @@ class UpdateHandlerTest extends \PHPUnit\Framework\TestCase
90
91
*/
91
92
private $ currentStoreId ;
92
93
94
+ /** @var MetadataPool */
95
+ private $ metadataPool ;
96
+
93
97
/**
94
98
* @inheritdoc
95
99
*/
@@ -109,6 +113,7 @@ protected function setUp(): void
109
113
$ this ->mediaDirectory = $ this ->objectManager ->get (Filesystem::class)
110
114
->getDirectoryWrite (DirectoryList::MEDIA );
111
115
$ this ->mediaDirectory ->writeFile ($ this ->fileName , 'Test ' );
116
+ $ this ->metadataPool = $ this ->objectManager ->get (MetadataPool::class);
112
117
}
113
118
114
119
/**
@@ -534,28 +539,30 @@ private function duplicateMediaGalleryForProduct(string $imagePath, string $prod
534
539
$ product = $ this ->getProduct (null , $ productSku );
535
540
$ connect = $ this ->galleryResource ->getConnection ();
536
541
$ select = $ connect ->select ()->from ($ this ->galleryResource ->getMainTable ())->where ('value = ? ' , $ imagePath );
537
- $ res = $ connect ->fetchRow ($ select );
538
- $ value_id = $ res ['value_id ' ];
539
- unset($ res ['value_id ' ]);
542
+ $ result = $ connect ->fetchRow ($ select );
543
+ $ value_id = $ result ['value_id ' ];
544
+ unset($ result ['value_id ' ]);
540
545
$ rows = [
541
- 'attribute_id ' => $ res ['attribute_id ' ],
542
- 'value ' => $ res ['value ' ],
543
- ProductAttributeMediaGalleryEntryInterface::MEDIA_TYPE => $ res ['media_type ' ],
544
- ProductAttributeMediaGalleryEntryInterface::DISABLED => $ res ['disabled ' ],
546
+ 'attribute_id ' => $ result ['attribute_id ' ],
547
+ 'value ' => $ result ['value ' ],
548
+ ProductAttributeMediaGalleryEntryInterface::MEDIA_TYPE => $ result ['media_type ' ],
549
+ ProductAttributeMediaGalleryEntryInterface::DISABLED => $ result ['disabled ' ],
545
550
];
546
551
$ connect ->insert ($ this ->galleryResource ->getMainTable (), $ rows );
547
552
$ select = $ connect ->select ()
548
553
->from ($ this ->galleryResource ->getTable (Gallery::GALLERY_VALUE_TABLE ))
549
554
->where ('value_id = ? ' , $ value_id );
550
- $ res = $ connect ->fetchRow ($ select );
555
+ $ result = $ connect ->fetchRow ($ select );
551
556
$ newValueId = (int )$ value_id + 1 ;
557
+ $ metadata = $ this ->metadataPool ->getMetadata (ProductInterface::class);
558
+ $ linkField = $ metadata ->getLinkField ();
552
559
$ rows = [
553
560
'value_id ' => $ newValueId ,
554
- 'store_id ' => $ res ['store_id ' ],
555
- ProductAttributeMediaGalleryEntryInterface::LABEL => $ res ['label ' ],
556
- ProductAttributeMediaGalleryEntryInterface::POSITION => $ res ['position ' ],
557
- ProductAttributeMediaGalleryEntryInterface::DISABLED => $ res ['disabled ' ],
558
- ' row_id ' => $ product ->getRowId ( ),
561
+ 'store_id ' => $ result ['store_id ' ],
562
+ ProductAttributeMediaGalleryEntryInterface::LABEL => $ result ['label ' ],
563
+ ProductAttributeMediaGalleryEntryInterface::POSITION => $ result ['position ' ],
564
+ ProductAttributeMediaGalleryEntryInterface::DISABLED => $ result ['disabled ' ],
565
+ $ linkField => $ product ->getData ( $ linkField ),
559
566
];
560
567
$ connect ->insert ($ this ->galleryResource ->getTable (Gallery::GALLERY_VALUE_TABLE ), $ rows );
561
568
$ rows = ['value_id ' => $ newValueId , 'row_id ' => $ product ->getRowId ()];
0 commit comments