Skip to content

Commit 10e64f2

Browse files
committed
Add save product with image integration test scenario
1 parent 71983df commit 10e64f2

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Catalog\Api\Data\ProductExtensionInterfaceFactory;
8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
10+
\Magento\TestFramework\Helper\Bootstrap::getInstance()->reinitialize();
11+
12+
/** @var \Magento\TestFramework\ObjectManager $objectManager */
13+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
14+
15+
/** @var $product \Magento\Catalog\Model\Product */
16+
$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
17+
$product->isObjectNew(true);
18+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
19+
->setId(1)
20+
->setAttributeSetId(4)
21+
->setWebsiteIds([1])
22+
->setName('Simple Product')
23+
->setSku('simple')
24+
->setPrice(10)
25+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
26+
27+
/** @var $mediaConfig \Magento\Catalog\Model\Product\Media\Config */
28+
$mediaConfig = $objectManager->get(\Magento\Catalog\Model\Product\Media\Config::class);
29+
30+
/** @var $mediaDirectory \Magento\Framework\Filesystem\Directory\WriteInterface */
31+
$mediaDirectory = $objectManager->get(\Magento\Framework\Filesystem::class)
32+
->getDirectoryWrite(DirectoryList::MEDIA);
33+
34+
$targetDirPath = $mediaConfig->getBaseMediaPath();
35+
$targetTmpDirPath = $mediaConfig->getBaseTmpMediaPath();
36+
37+
$mediaDirectory->create($targetDirPath);
38+
$mediaDirectory->create($targetTmpDirPath);
39+
40+
$dist = $mediaDirectory->getAbsolutePath($mediaConfig->getBaseMediaPath() . DIRECTORY_SEPARATOR . 'magento_image.jpg');
41+
copy(__DIR__ . '/magento_image.jpg', $dist);
42+
43+
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
44+
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
45+
$productRepository->save($product);

0 commit comments

Comments
 (0)