Skip to content

Commit c72589d

Browse files
committed
MC-37070: Create automated test for "Import products with shared images"
1 parent 853ec11 commit c72589d

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
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\EntityManager\MetadataPool;
1920
use Magento\Framework\Filesystem;
2021
use Magento\Framework\Filesystem\Directory\WriteInterface;
2122
use Magento\Framework\ObjectManagerInterface;
@@ -90,6 +91,9 @@ class UpdateHandlerTest extends \PHPUnit\Framework\TestCase
9091
*/
9192
private $currentStoreId;
9293

94+
/** @var MetadataPool */
95+
private $metadataPool;
96+
9397
/**
9498
* @inheritdoc
9599
*/
@@ -109,6 +113,7 @@ protected function setUp(): void
109113
$this->mediaDirectory = $this->objectManager->get(Filesystem::class)
110114
->getDirectoryWrite(DirectoryList::MEDIA);
111115
$this->mediaDirectory->writeFile($this->fileName, 'Test');
116+
$this->metadataPool = $this->objectManager->get(MetadataPool::class);
112117
}
113118

114119
/**
@@ -534,28 +539,30 @@ private function duplicateMediaGalleryForProduct(string $imagePath, string $prod
534539
$product = $this->getProduct(null, $productSku);
535540
$connect = $this->galleryResource->getConnection();
536541
$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']);
540545
$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'],
545550
];
546551
$connect->insert($this->galleryResource->getMainTable(), $rows);
547552
$select = $connect->select()
548553
->from($this->galleryResource->getTable(Gallery::GALLERY_VALUE_TABLE))
549554
->where('value_id = ?', $value_id);
550-
$res = $connect->fetchRow($select);
555+
$result = $connect->fetchRow($select);
551556
$newValueId = (int)$value_id + 1;
557+
$metadata = $this->metadataPool->getMetadata(ProductInterface::class);
558+
$linkField = $metadata->getLinkField();
552559
$rows = [
553560
'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),
559566
];
560567
$connect->insert($this->galleryResource->getTable(Gallery::GALLERY_VALUE_TABLE), $rows);
561568
$rows = ['value_id' => $newValueId, 'row_id' => $product->getRowId()];

0 commit comments

Comments
 (0)