@@ -90,4 +90,52 @@ public function skuDataProvider(): array
9090 ['sku ' => 'simple ' ],
9191 ];
9292 }
93+
94+ /**
95+ * Test save product with gallery image
96+ *
97+ * @magentoDataFixture Magento/Catalog/_files/product_simple_with_image.php
98+ *
99+ * @throws \Magento\Framework\Exception\CouldNotSaveException
100+ * @throws \Magento\Framework\Exception\InputException
101+ * @throws \Magento\Framework\Exception\StateException
102+ */
103+ public function testSaveProductWithGalleryImage (): void
104+ {
105+ /** @var $mediaConfig \Magento\Catalog\Model\Product\Media\Config */
106+ $ mediaConfig = Bootstrap::getObjectManager ()
107+ ->get (\Magento \Catalog \Model \Product \Media \Config::class);
108+
109+ /** @var $mediaDirectory \Magento\Framework\Filesystem\Directory\WriteInterface */
110+ $ mediaDirectory = Bootstrap::getObjectManager ()
111+ ->get (\Magento \Framework \Filesystem::class)
112+ ->getDirectoryWrite (\Magento \Framework \App \Filesystem \DirectoryList::MEDIA );
113+
114+ $ product = Bootstrap::getObjectManager ()->create (\Magento \Catalog \Model \Product::class);
115+ $ product ->load (1 );
116+
117+ $ path = $ mediaConfig ->getBaseMediaPath () . '/magento_image.jpg ' ;
118+ $ absolutePath = $ mediaDirectory ->getAbsolutePath () . $ path ;
119+ $ product ->addImageToMediaGallery ($ absolutePath , [
120+ 'image ' ,
121+ 'small_image ' ,
122+ ], false , false );
123+
124+ /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
125+ $ productRepository = Bootstrap::getObjectManager ()
126+ ->create (\Magento \Catalog \Api \ProductRepositoryInterface::class);
127+ $ productRepository ->save ($ product );
128+
129+ $ gallery = $ product ->getData ('media_gallery ' );
130+ $ this ->assertArrayHasKey ('images ' , $ gallery );
131+ $ images = array_values ($ gallery ['images ' ]);
132+
133+ $ this ->assertNotEmpty ($ gallery );
134+ $ this ->assertTrue (isset ($ images [0 ]['file ' ]));
135+ $ this ->assertStringStartsWith ('/m/a/magento_image ' , $ images [0 ]['file ' ]);
136+ $ this ->assertArrayHasKey ('media_type ' , $ images [0 ]);
137+ $ this ->assertEquals ('image ' , $ images [0 ]['media_type ' ]);
138+ $ this ->assertStringStartsWith ('/m/a/magento_image ' , $ product ->getData ('image ' ));
139+ $ this ->assertStringStartsWith ('/m/a/magento_image ' , $ product ->getData ('small_image ' ));
140+ }
93141}
0 commit comments