Skip to content

Commit

Permalink
Fix EZP-30857: Referenced image file is not deleted from storage when…
Browse files Browse the repository at this point in the history
… removing draft, archived or published version. (#179)

* Fix EZP-30857: Referenced image file is not deleted from storage when removing draft, archived or published version.

* [Travis] Fix AliasCleanerTest
  • Loading branch information
blankse authored Nov 16, 2020
1 parent a89e0e0 commit ba8025d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions bundle/Resources/config/fieldtype_services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ services:
arguments:
- "@ezpublish.image_alias.imagine.alias_cleaner"
- "@ezpublish.core.io.image_fieldtype.legacy_url_redecorator"
- "@ezpublish.fieldType.ezimage.io_service"
lazy: true
public: false
16 changes: 12 additions & 4 deletions mvc/Image/AliasCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace eZ\Publish\Core\MVC\Legacy\Image;

use eZ\Publish\Core\FieldType\Image\AliasCleanerInterface;
use eZ\Publish\Core\IO\IOServiceInterface;
use eZ\Publish\Core\IO\UrlRedecoratorInterface;

class AliasCleaner implements AliasCleanerInterface
Expand All @@ -21,18 +22,25 @@ class AliasCleaner implements AliasCleanerInterface
*/
private $urlRedecorator;

/**
* @var IOServiceInterface
*/
private $IOService;

public function __construct(
AliasCleanerInterface $innerAliasCleaner,
UrlRedecoratorInterface $urlRedecorator
UrlRedecoratorInterface $urlRedecorator,
IOServiceInterface $IOService
) {
$this->innerAliasCleaner = $innerAliasCleaner;
$this->urlRedecorator = $urlRedecorator;
$this->IOService = $IOService;
}

public function removeAliases($originalPath)
{
$this->innerAliasCleaner->removeAliases(
$this->urlRedecorator->redecorateFromTarget($originalPath)
);
$uri = $this->urlRedecorator->redecorateFromTarget($originalPath);
$binaryFile = $this->IOService->loadBinaryFileByUri($uri);
$this->innerAliasCleaner->removeAliases($binaryFile->id);
}
}
22 changes: 19 additions & 3 deletions mvc/Tests/Image/AliasCleanerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
namespace eZ\Publish\Core\MVC\Legacy\Tests\Image;

use eZ\Publish\Core\IO\IOServiceInterface;
use eZ\Publish\Core\IO\Values\BinaryFile;
use eZ\Publish\Core\MVC\Legacy\Image\AliasCleaner;
use eZ\Publish\Core\FieldType\Image\AliasCleanerInterface;
use eZ\Publish\Core\IO\UrlRedecoratorInterface;
Expand All @@ -28,22 +30,36 @@ class AliasCleanerTest extends TestCase
*/
private $urlRedecorator;

/**
* @var \eZ\Publish\Core\IO\IOServiceInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $IOService;

protected function setUp()
{
parent::setUp();
$this->innerAliasCleaner = $this->createMock(AliasCleanerInterface::class);
$this->urlRedecorator = $this->createMock(UrlRedecoratorInterface::class);
$this->aliasCleaner = new AliasCleaner($this->innerAliasCleaner, $this->urlRedecorator);
$this->IOService = $this->createMock(IOServiceInterface::class);
$this->aliasCleaner = new AliasCleaner($this->innerAliasCleaner, $this->urlRedecorator, $this->IOService);
}

public function testRemoveAliases()
{
$originalPath = 'foo/bar/test.jpg';
$originalPath = 'var/storage/image/foo/bar/test.jpg';
$uri = '/var/storage/image/foo/bar/test.jpg';
$binaryFile = new BinaryFile(['id' => 'foo/bar/test.jpg']);

$this->urlRedecorator
->expects($this->once())
->method('redecorateFromTarget')
->with($originalPath);
->with($originalPath)
->willReturn($uri);

$this->IOService
->expects($this->once())
->method('loadBinaryFileByUri')
->willReturn($binaryFile);

$this->innerAliasCleaner
->expects($this->once())
Expand Down

0 comments on commit ba8025d

Please sign in to comment.