Skip to content

Commit 7b109ed

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-61798' into 2.0.15-develop-pr8
2 parents c9fcf0b + 39c692f commit 7b109ed

File tree

3 files changed

+64
-8
lines changed
  • app/code/Magento/Catalog
    • Model
    • Test/Unit/Model/ResourceModel/Product/Attribute/Backend

3 files changed

+64
-8
lines changed

app/code/Magento/Catalog/Model/Product/Attribute/Backend/Media.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,10 @@ public function afterSave($object)
194194
if (!empty($image['removed'])) {
195195
if (!empty($image['value_id']) && !isset($picturesInOtherStores[$image['file']])) {
196196
$recordsToDelete[] = $image['value_id'];
197-
$filesToDelete[] = ltrim($image['file'], '/');
197+
// only delete physical files if they are not used by any other products
198+
if ($this->resourceModel->countImageUses($image['file']) <= 1) {
199+
$filesToDelete[] = ltrim($image['file'], '/');
200+
}
198201
}
199202
continue;
200203
}

app/code/Magento/Catalog/Model/ResourceModel/Product/Attribute/Backend/Media.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,4 +398,28 @@ public function loadMediaGalleryEntities($attributeId, $productId)
398398

399399
return $this->getConnection()->fetchAll($select);
400400
}
401+
402+
/**
403+
* Counts uses of the image.
404+
*
405+
* @param string $image
406+
* @return int
407+
* @deprecated 2.1.5
408+
* Added to find out if product image is used for several different products.
409+
* This method is absent in version >=2.1.0 & < 2.1.5.
410+
* In 2.1.5 version it was added in Magento\Catalog\Model\ResourceModel\Product\Gallery.
411+
*/
412+
public function countImageUses($image)
413+
{
414+
$select = $this->getConnection()->select()
415+
->from(
416+
[$this->getMainTableAlias() => $this->getMainTable()],
417+
'count(*)'
418+
)
419+
->where('value = ?', $image);
420+
421+
$countImages = $this->getConnection()->fetchOne($select);
422+
423+
return $countImages;
424+
}
401425
}

app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Attribute/Backend/MediaTest.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,21 @@ class MediaTest extends \PHPUnit_Framework_TestCase
6161
public function setUp()
6262
{
6363
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
64-
$this->connection = $this->getMock('Magento\Framework\DB\Adapter\Pdo\Mysql', [], [], '', false);
65-
$resource = $this->getMock('Magento\Framework\App\ResourceConnection', [], [], '', false);
64+
$this->connection = $this->getMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class, [], [], '', false);
65+
$resource = $this->getMock(\Magento\Framework\App\ResourceConnection::class, [], [], '', false);
6666
$resource->expects($this->any())
6767
->method('getConnection')
6868
->willReturn($this->connection);
6969
$resource->expects($this->any())->method('getTableName')->willReturn('table');
7070
$this->connection->expects($this->any())->method('setCacheAdapter');
7171
$this->resource = $objectManager->getObject(
72-
'Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Media',
72+
\Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Media::class,
7373
['resource' => $resource]
7474
);
75-
$this->product = $this->getMock('Magento\Catalog\Model\Product', [], [], '', false);
76-
$this->model = $this->getMock('Magento\Catalog\Model\Product\Attribute\Backend\Media', [], [], '', false);
77-
$this->select = $this->getMock('Magento\Framework\DB\Select', [], [], '', false);
78-
$this->attribute = $this->getMock('Magento\Eav\Model\Entity\Attribute\AbstractAttribute', [], [], '', false);
75+
$this->product = $this->getMock(\Magento\Catalog\Model\Product::class, [], [], '', false);
76+
$this->model = $this->getMock(\Magento\Catalog\Model\Product\Attribute\Backend\Media::class, [], [], '', false);
77+
$this->select = $this->getMock(\Magento\Framework\DB\Select::class, [], [], '', false);
78+
$this->attribute = $this->getMock(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class, [], [], '', false);
7979
}
8080

8181
public function testLoadDataFromTableByValueId()
@@ -412,4 +412,33 @@ public function testDeleteGalleryValueInStore()
412412

413413
$this->resource->deleteGalleryValueInStore($valueId, $entityId, $storeId);
414414
}
415+
416+
417+
public function testCountImageUses()
418+
{
419+
$results = [
420+
[
421+
'value_id' => '1',
422+
'attribute_id' => 90,
423+
'value' => '/d/o/download_7.jpg',
424+
'media_type' => 'image',
425+
'disabled' => '0',
426+
],
427+
];
428+
429+
$this->connection->expects($this->once())->method('select')->will($this->returnValue($this->select));
430+
$this->select->expects($this->at(0))
431+
->method('from')
432+
->with(['main' => 'table'], 'count(*)' )
433+
->willReturnSelf();
434+
$this->select->expects($this->at(1))
435+
->method('where')
436+
->with('value = ?', 1)
437+
->willReturnSelf();
438+
$this->connection->expects($this->once())
439+
->method('fetchOne')
440+
->with($this->select)
441+
->willReturn(count($results));
442+
$this->assertEquals($this->resource->countImageUses(1), count($results));
443+
}
415444
}

0 commit comments

Comments
 (0)