Skip to content

Commit f0fab44

Browse files
committed
Refactor the MediaGallery plugin responsible for operations after image or diretory deleted
1 parent 1e28f35 commit f0fab44

File tree

4 files changed

+86
-209
lines changed

4 files changed

+86
-209
lines changed

app/code/Magento/MediaGallery/Model/ResourceModel/DeleteAssetsByPaths.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class DeleteAssetsByPaths implements DeleteAssetsByPathsInterface
3232
private $logger;
3333

3434
/**
35-
* DeleteById constructor.
35+
* DeleteAssetsByPaths constructor.
3636
*
3737
* @param ResourceConnection $resourceConnection
3838
* @param LoggerInterface $logger

app/code/Magento/MediaGallery/Plugin/Wysiwyg/Images/Storage.php

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,23 @@
88

99
namespace Magento\MediaGallery\Plugin\Wysiwyg\Images;
1010

11-
use Magento\Framework\Exception\CouldNotDeleteException;
12-
use Magento\MediaGalleryApi\Model\Asset\Command\DeleteByDirectoryPathInterface;
13-
use Magento\MediaGalleryApi\Model\Asset\Command\GetByPathInterface;
14-
use Magento\MediaGalleryApi\Model\Asset\Command\DeleteByPathInterface;
11+
use Magento\Cms\Model\Wysiwyg\Images;
12+
use Magento\MediaGalleryApi\Api\DeleteAssetsByPathsInterface;
1513
use Magento\Cms\Model\Wysiwyg\Images\Storage as StorageSubject;
1614
use Magento\Framework\App\Filesystem\DirectoryList;
1715
use Magento\Framework\Filesystem;
1816
use Psr\Log\LoggerInterface;
19-
use Magento\Framework\Exception\ValidatorException;
2017

2118
/**
2219
* Ensures that metadata is removed from the database when a file is deleted and it is an image
2320
*/
2421
class Storage
2522
{
2623
/**
27-
* @var GetByPathInterface
28-
*/
29-
private $getMediaAssetByPath;
30-
31-
/**
32-
* @var DeleteByPathInterface
24+
* @var DeleteAssetsByPathsInterface
3325
*/
3426
private $deleteMediaAssetByPath;
3527

36-
/**
37-
* @var DeleteByDirectoryPathInterface
38-
*/
39-
private $deleteMediaAssetByDirectoryPath;
40-
4128
/**
4229
* @var Filesystem
4330
*/
@@ -51,22 +38,16 @@ class Storage
5138
/**
5239
* Storage constructor.
5340
*
54-
* @param GetByPathInterface $getMediaAssetByPath
55-
* @param DeleteByPathInterface $deleteMediaAssetByPath
56-
* @param DeleteByDirectoryPathInterface $deleteByDirectoryPath
41+
* @param DeleteAssetsByPathsInterface $deleteMediaAssetByPath
5742
* @param Filesystem $filesystem
5843
* @param LoggerInterface $logger
5944
*/
6045
public function __construct(
61-
GetByPathInterface $getMediaAssetByPath,
62-
DeleteByPathInterface $deleteMediaAssetByPath,
63-
DeleteByDirectoryPathInterface $deleteByDirectoryPath,
46+
DeleteAssetsByPathsInterface $deleteMediaAssetByPath,
6447
Filesystem $filesystem,
6548
LoggerInterface $logger
6649
) {
67-
$this->getMediaAssetByPath = $getMediaAssetByPath;
6850
$this->deleteMediaAssetByPath = $deleteMediaAssetByPath;
69-
$this->deleteMediaAssetByDirectoryPath = $deleteByDirectoryPath;
7051
$this->filesystem = $filesystem;
7152
$this->logger = $logger;
7253
}
@@ -94,7 +75,8 @@ public function afterDeleteFile(StorageSubject $subject, StorageSubject $result,
9475
}
9576

9677
try {
97-
$this->deleteMediaAssetByPath->execute($relativePath);
78+
79+
$this->deleteMediaAssetByPath->execute([$relativePath]);
9880
} catch (\Exception $exception) {
9981
$this->logger->critical($exception);
10082
}
@@ -120,8 +102,8 @@ public function afterDeleteDirectory(StorageSubject $subject, $result, $path)
120102
}
121103

122104
try {
123-
$this->deleteMediaAssetByDirectoryPath->execute($this->getMediaDirectoryRelativePath($path));
124-
} catch (ValidatorException $exception) {
105+
$this->deleteMediaAssetByPath->execute([$this->getMediaDirectoryRelativePath($path)]);
106+
} catch (\Exception $exception) {
125107
$this->logger->critical($exception);
126108
}
127109

app/code/Magento/MediaGallery/Test/Unit/Plugin/Images/StorageTest.php

Lines changed: 0 additions & 144 deletions
This file was deleted.

app/code/Magento/MediaGallery/Test/Unit/Plugin/Wysiwyg/Images/StorageTest.php

Lines changed: 76 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
namespace Magento\MediaGallery\Test\Unit\Plugin\Wysiwyg\Images;
1010

1111
use Magento\Cms\Model\Wysiwyg\Images\Storage as StorageSubject;
12+
use Magento\MediaGalleryApi\Api\DeleteAssetsByPathsInterface;
1213
use Magento\Framework\Filesystem;
1314
use Magento\Framework\Filesystem\Directory\ReadInterface;
14-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
15-
use Magento\MediaGallery\Plugin\Wysiwyg\Images\Storage;
16-
use Magento\MediaGalleryApi\Model\Asset\Command\DeleteByPathInterface;
17-
use Magento\MediaGalleryApi\Model\Asset\Command\GetByPathInterface;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
16+
use Magento\MediaGallery\Plugin\Wysiwyg\Images\Storage as StoragePlugin;
1817
use PHPUnit\Framework\MockObject\MockObject;
1918
use PHPUnit\Framework\TestCase;
2019
use Psr\Log\LoggerInterface;
@@ -24,21 +23,14 @@
2423
*/
2524
class StorageTest extends TestCase
2625
{
27-
const STUB_TARGET = '/stub/test.png';
28-
const STUB_RELATIVE_PATH = 'test.png';
26+
private const STUB_TARGET = '/stub/test.png';
27+
private const STUB_RELATIVE_PATH = 'test.png';
28+
private const NON_STRING_PATH = 2020;
29+
private const INVALID_PATH = '&&';
30+
private const VALID_PATH = 'test-directory-path/';
2931

3032
/**
31-
* @var Storage
32-
*/
33-
private $storage;
34-
35-
/**
36-
* @var GetByPathInterface|MockObject
37-
*/
38-
private $getMediaAssetByPathMock;
39-
40-
/**
41-
* @var DeleteByPathInterface|MockObject
33+
* @var DeleteAssetsByPathsInterface|MockObject
4234
*/
4335
private $deleteMediaAssetByPathMock;
4436

@@ -63,37 +55,84 @@ class StorageTest extends TestCase
6355
private $readInterfaceMock;
6456

6557
/**
66-
* @inheritDoc
58+
* @var StoragePlugin
6759
*/
68-
protected function setUp()
60+
private $storage;
61+
62+
/**
63+
* @inheritdoc
64+
*/
65+
protected function setUp(): void
6966
{
70-
$this->storageSubjectMock = $this->createMock(StorageSubject::class);
67+
$this->deleteMediaAssetByPathMock = $this->createMock(DeleteAssetsByPathsInterface::class);
7168
$this->filesystemMock = $this->createMock(Filesystem::class);
72-
$this->getMediaAssetByPathMock = $this->createMock(GetByPathInterface::class);
73-
$this->deleteMediaAssetByPathMock = $this->getMockBuilder(DeleteByPathInterface::class)
74-
->disableOriginalConstructor()
75-
->setMethods(['execute'])
76-
->getMockForAbstractClass();
77-
$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
78-
->disableOriginalConstructor()
79-
->setMethods(['critical'])
80-
->getMockForAbstractClass();
81-
$this->readInterfaceMock = $this->getMockBuilder(ReadInterface::class)
82-
->disableOriginalConstructor()
83-
->setMethods(['getRelativePath'])
84-
->getMockForAbstractClass();
85-
86-
$this->storage = (new ObjectManagerHelper($this))->getObject(
87-
Storage::class,
69+
$this->loggerMock = $this->createMock(LoggerInterface::class);
70+
$this->storageSubjectMock = $this->createMock(StorageSubject::class);
71+
$this->readInterfaceMock = $this->createMock(ReadInterface::class);
72+
73+
$this->storage = (new ObjectManager($this))->getObject(
74+
StoragePlugin::class,
8875
[
89-
'getMediaAssetByPath' => $this->getMediaAssetByPathMock,
9076
'deleteMediaAssetByPath' => $this->deleteMediaAssetByPathMock,
9177
'filesystem' => $this->filesystemMock,
9278
'logger' => $this->loggerMock
9379
]
9480
);
9581
}
9682

83+
/**
84+
* @param string $path
85+
*
86+
* @dataProvider pathPathDataProvider
87+
*/
88+
public function testAfterDeleteDirectory(string $path): void
89+
{
90+
$directoryRead = $this->createMock(ReadInterface::class);
91+
$this->filesystemMock->expects($this->any())
92+
->method('getDirectoryRead')
93+
->willReturn($directoryRead);
94+
95+
switch ($path) {
96+
case self::NON_STRING_PATH:
97+
$result = $this->storage->afterDeleteDirectory($this->storageSubjectMock, null, (int)$path);
98+
self::assertNull($result);
99+
break;
100+
case self::INVALID_PATH:
101+
$directoryRead->expects($this->once())
102+
->method('getRelativePath')
103+
->with($path)
104+
->willThrowException(new \Exception());
105+
$this->loggerMock->expects($this->once())
106+
->method('critical');
107+
$this->storage->afterDeleteDirectory($this->storageSubjectMock, null, $path);
108+
break;
109+
case self::VALID_PATH:
110+
$directoryRead->expects($this->once())
111+
->method('getRelativePath')
112+
->with($path)
113+
->willReturn($path);
114+
$this->deleteMediaAssetByPathMock->expects($this->once())
115+
->method('execute')
116+
->with([$path]);
117+
$this->storage->afterDeleteDirectory($this->storageSubjectMock, null, $path);
118+
break;
119+
}
120+
}
121+
122+
/**
123+
* Data provider for path
124+
*
125+
* @return array
126+
*/
127+
public function pathPathDataProvider(): array
128+
{
129+
return [
130+
'Non string path' => [2020],
131+
'Invalid path' => [self::INVALID_PATH],
132+
'Existent path' => [self::VALID_PATH]
133+
];
134+
}
135+
97136
/**
98137
* Test case when an exception is thrown during the method execution.
99138
*/

0 commit comments

Comments
 (0)