Skip to content

Commit e7f5340

Browse files
Merge pull request #764 from magento-okapis/MAGETWO-61104-Deleting-image-in-admin-panel
MAGETWO-61104: [Magento Cloud] - Deleting image in the admin panel deletes it in the server and causes error for other products
2 parents af5ee20 + e93b4b2 commit e7f5340

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ protected function processDeletedImages($product, array &$images)
2929
if (!empty($image['removed'])) {
3030
if (!empty($image['value_id']) && !isset($picturesInOtherStores[$image['file']])) {
3131
$recordsToDelete[] = $image['value_id'];
32-
$filesToDelete[] = ltrim($image['file'], '/');
32+
// only delete physical files if they are not used by any other products
33+
if (!$this->resourceModel->countImageUses($image['file']) > 1) {
34+
$filesToDelete[] = ltrim($image['file'], '/');
35+
}
3336
}
3437
}
3538
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,4 +449,21 @@ public function getProductImages($product, $storeIds)
449449

450450
return $this->getConnection()->fetchAll($select);
451451
}
452+
453+
/**
454+
* Counts uses of this image.
455+
*
456+
* @param string $image
457+
* @return int
458+
*/
459+
public function countImageUses($image)
460+
{
461+
$select = $this->getConnection()->select()
462+
->from([$this->getMainTableAlias() => $this->getMainTable()])
463+
->where(
464+
'value = ?',
465+
$image
466+
);
467+
return count($this->getConnection()->fetchAll($select));
468+
}
452469
}

app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/GalleryTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,33 @@ public function testDeleteGalleryValueInStore()
443443

444444
$this->resource->deleteGalleryValueInStore($valueId, $entityId, $storeId);
445445
}
446+
447+
public function testCountImageUses()
448+
{
449+
$results = [
450+
[
451+
'value_id' => '1',
452+
'attribute_id' => 90,
453+
'value' => '/d/o/download_7.jpg',
454+
'media_type' => 'image',
455+
'disabled' => '0',
456+
],
457+
];
458+
459+
$this->connection->expects($this->once())->method('select')->will($this->returnValue($this->select));
460+
$this->select->expects($this->at(0))->method('from')->with(
461+
[
462+
'main' => 'table',
463+
],
464+
'*'
465+
)->willReturnSelf();
466+
$this->select->expects($this->at(1))->method('where')->with(
467+
'value = ?',
468+
1
469+
)->willReturnSelf();
470+
$this->connection->expects($this->once())->method('fetchAll')
471+
->with($this->select)
472+
->willReturn($results);
473+
$this->assertEquals($this->resource->countImageUses(1), count($results));
474+
}
446475
}

0 commit comments

Comments
 (0)