Skip to content

Commit

Permalink
Merge pull request #764 from magento-okapis/MAGETWO-61104-Deleting-im…
Browse files Browse the repository at this point in the history
…age-in-admin-panel

MAGETWO-61104: [Magento Cloud] - Deleting image in the admin panel deletes it in the server and causes error for other products
  • Loading branch information
heyitsroberthe authored Jan 30, 2017
2 parents af5ee20 + e93b4b2 commit e7f5340
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ protected function processDeletedImages($product, array &$images)
if (!empty($image['removed'])) {
if (!empty($image['value_id']) && !isset($picturesInOtherStores[$image['file']])) {
$recordsToDelete[] = $image['value_id'];
$filesToDelete[] = ltrim($image['file'], '/');
// only delete physical files if they are not used by any other products
if (!$this->resourceModel->countImageUses($image['file']) > 1) {
$filesToDelete[] = ltrim($image['file'], '/');
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions app/code/Magento/Catalog/Model/ResourceModel/Product/Gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,21 @@ public function getProductImages($product, $storeIds)

return $this->getConnection()->fetchAll($select);
}

/**
* Counts uses of this image.
*
* @param string $image
* @return int
*/
public function countImageUses($image)
{
$select = $this->getConnection()->select()
->from([$this->getMainTableAlias() => $this->getMainTable()])
->where(
'value = ?',
$image
);
return count($this->getConnection()->fetchAll($select));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,33 @@ public function testDeleteGalleryValueInStore()

$this->resource->deleteGalleryValueInStore($valueId, $entityId, $storeId);
}

public function testCountImageUses()
{
$results = [
[
'value_id' => '1',
'attribute_id' => 90,
'value' => '/d/o/download_7.jpg',
'media_type' => 'image',
'disabled' => '0',
],
];

$this->connection->expects($this->once())->method('select')->will($this->returnValue($this->select));
$this->select->expects($this->at(0))->method('from')->with(
[
'main' => 'table',
],
'*'
)->willReturnSelf();
$this->select->expects($this->at(1))->method('where')->with(
'value = ?',
1
)->willReturnSelf();
$this->connection->expects($this->once())->method('fetchAll')
->with($this->select)
->willReturn($results);
$this->assertEquals($this->resource->countImageUses(1), count($results));
}
}

0 comments on commit e7f5340

Please sign in to comment.