Skip to content

Commit c7e436f

Browse files
committed
ACP2E-3797: New Files Created Instead of Replacing Existing Images
1 parent a04b0ef commit c7e436f

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

app/code/Magento/Catalog/Model/Product/Gallery/CreateHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ public function execute($product, $arguments = [])
221221
$newFile = $this->moveImageFromTmp($image['file'] ?? '');
222222
$image['new_file'] = $newFile;
223223
$newImages[$image['file']] = $image;
224+
if (!empty($image['recreate'])) {
225+
//delete old image
226+
$this->mediaDirectory->delete($this->mediaConfig->getMediaPath($image['file']));
227+
}
224228
$image['file'] = $newFile;
225229
} else {
226230
$existImages[$image['file']] = $image;

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductAttributeMediaGalleryManagementInterfaceTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
1212
use Magento\Framework\Api\Data\ImageContentInterface;
13+
use Magento\Framework\App\Filesystem\DirectoryList;
14+
use Magento\Framework\Filesystem;
15+
use Magento\Framework\Filesystem\Directory\WriteInterface;
1316
use Magento\Store\Test\Fixture\Store as StoreFixture;
1417
use Magento\TestFramework\Fixture\DataFixture;
1518
use Magento\TestFramework\Fixture\DataFixtureStorage;
@@ -69,6 +72,11 @@ class ProductAttributeMediaGalleryManagementInterfaceTest extends WebapiAbstract
6972
*/
7073
private $fixtures;
7174

75+
/**
76+
* @var WriteInterface
77+
*/
78+
private $mediaDirectory;
79+
7280
/**
7381
* @inheritDoc
7482
*/
@@ -113,6 +121,8 @@ protected function setUp(): void
113121

114122
$this->testImagePath = __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'test_image.jpg';
115123
$this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage();
124+
$this->mediaDirectory = $this->objectManager->get(Filesystem::class)
125+
->getDirectoryWrite(DirectoryList::MEDIA);
116126
}
117127

118128
/**
@@ -895,4 +905,44 @@ public function testContentAttributeInGet(): void
895905
$response = $this->_webApiCall($serviceInfo, $requestData);
896906
$this->assertArrayHasKey('content', $response);
897907
}
908+
909+
/**
910+
* Test update() method when existing image gets overwritten
911+
*
912+
* @magentoApiDataFixture Magento/Catalog/_files/product_with_image.php
913+
*/
914+
public function testUpdateExistingImage()
915+
{
916+
$productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
917+
/** @var \Magento\Catalog\Api\Data\ProductInterface $product */
918+
$product = $productRepository->get('simple');
919+
$imageId = (int)$product->getMediaGalleryImages()->getFirstItem()->getValueId();
920+
$originalImagePath = $product->getMediaGalleryImages()->getFirstItem()->getFile();
921+
$requestData = [
922+
'sku' => 'simple',
923+
'entry' => [
924+
'id' => $this->getTargetGalleryEntryId(),
925+
'label' => 'Updated Image Text',
926+
'position' => 10,
927+
'types' => ['thumbnail'],
928+
'disabled' => true,
929+
'media_type' => 'image',
930+
'content' => [
931+
'base64_encoded_data' => base64_encode(file_get_contents($this->testImagePath)),
932+
'type' => 'image/jpeg',
933+
'name' => 'test_image.jpg',
934+
]
935+
]
936+
];
937+
938+
$this->updateServiceInfo['rest']['resourcePath'] = $this->updateServiceInfo['rest']['resourcePath']
939+
. '/' . $this->getTargetGalleryEntryId();
940+
941+
$this->assertTrue($this->_webApiCall($this->updateServiceInfo, $requestData, null, 'all'));
942+
$updatedImage = $this->assertMediaGalleryData($imageId, '/t/e/test_image.jpg', 'Updated Image Text');
943+
$this->assertEquals(10, $updatedImage['position_default']);
944+
$this->assertEquals(1, $updatedImage['disabled_default']);
945+
$this->assertStringStartsWith('/t/e/test_image.jpg', $updatedImage['file']);
946+
$this->assertFalse($this->mediaDirectory->isExist($originalImagePath));
947+
}
898948
}

0 commit comments

Comments
 (0)