Skip to content

Commit da9526f

Browse files
committed
Refactoring and unit test coverage
1 parent f6ac9e7 commit da9526f

File tree

3 files changed

+110
-15
lines changed

3 files changed

+110
-15
lines changed

app/code/Magento/MediaGallery/Model/Asset/Command/DeleteByDirectoryPath.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ public function execute(string $directoryPath): void
6060
try {
6161
$this->validateDirectoryPath($directoryPath);
6262

63-
if (substr($directoryPath, -1) !== '/') {
64-
$directoryPath .= '/';
65-
}
63+
// Make sure that the path has a trailing slash
64+
$directoryPath = rtrim($directoryPath, '/') . '/';
6665

6766
/** @var AdapterInterface $connection */
6867
$connection = $this->resourceConnection->getConnection();
@@ -86,8 +85,8 @@ public function execute(string $directoryPath): void
8685
*/
8786
private function validateDirectoryPath(string $directoryPath): void
8887
{
89-
if (trim($directoryPath) === '') {
90-
throw new LocalizedException(__('The directory path cannot be empty'));
88+
if (!$directoryPath || trim($directoryPath) === '') {
89+
throw new LocalizedException(__('Cannot remove assets, the directory path does not exist'));
9190
}
9291
}
9392
}

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,7 @@ public function afterDeleteDirectory(StorageSubject $subject, $result, $path)
118118
}
119119

120120
$relativePath = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getRelativePath($path);
121-
122-
if (!$relativePath) {
123-
return $result;
124-
}
125-
126-
try {
127-
$this->deleteMediAssetByDirectoryPath->execute($relativePath);
128-
} catch (\Exception $exception) {
129-
$this->logger->critical($exception);
130-
}
121+
$this->deleteMediAssetByDirectoryPath->execute($relativePath);
131122

132123
return $result;
133124
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaGallery\Test\Unit\Model\Asset\Command;
9+
10+
use Magento\MediaGallery\Model\Asset\Command\DeleteByDirectoryPath;
11+
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Framework\DB\Adapter\AdapterInterface;
13+
use Magento\Framework\Exception\CouldNotDeleteException;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
16+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
17+
use Psr\Log\LoggerInterface;
18+
19+
/**
20+
* Test the DeleteByDirectoryPath command model
21+
*/
22+
class DeleteByDirectoryPathTest extends TestCase
23+
{
24+
private const TABLE_NAME = 'media_gallery_asset';
25+
private const DIRECTORY_PATH = 'test-directory-path/';
26+
27+
/**
28+
* @var ResourceConnection|MockObject
29+
*/
30+
private $resourceConnection;
31+
32+
/**
33+
* @var DeleteByDirectoryPath
34+
*/
35+
private $deleteMediaAssetByDirectoryPath;
36+
37+
/**
38+
* @var AdapterInterface|MockObject
39+
*/
40+
private $adapter;
41+
42+
/**
43+
* @var LoggerInterface|MockObject
44+
*/
45+
private $logger;
46+
47+
/**
48+
* Initialize basic test class mocks
49+
*/
50+
protected function setUp(): void
51+
{
52+
$this->logger = $this->createMock(LoggerInterface::class);
53+
$this->resourceConnection = $this->createMock(ResourceConnection::class);
54+
55+
$this->deleteMediaAssetByDirectoryPath = (new ObjectManager($this))->getObject(
56+
DeleteByDirectoryPath::class,
57+
[
58+
'resourceConnection' => $this->resourceConnection,
59+
'logger' => $this->logger,
60+
]
61+
);
62+
63+
$this->adapter = $this->createMock(AdapterInterface::class);
64+
}
65+
66+
/**
67+
* Test delete media asset by path command
68+
*
69+
* @param string $directoryPath
70+
* @throws CouldNotDeleteException
71+
* @dataProvider directoryPathDataProvider
72+
*/
73+
public function testDeleteByDirectoryPath(string $directoryPath): void
74+
{
75+
if (!empty($directoryPath)) {
76+
$this->resourceConnection->expects($this->once())
77+
->method('getConnection')
78+
->willReturn($this->adapter);
79+
$this->resourceConnection->expects($this->once())
80+
->method('getTableName')
81+
->with(self::TABLE_NAME)
82+
->willReturn('prefix_' . self::TABLE_NAME);
83+
$this->adapter->expects($this->once())
84+
->method('delete')
85+
->with('prefix_' . self::TABLE_NAME, ['path LIKE ?' => self::DIRECTORY_PATH . '%']);
86+
} else {
87+
self::expectException('\Magento\Framework\Exception\CouldNotDeleteException');
88+
}
89+
90+
$this->deleteMediaAssetByDirectoryPath->execute($directoryPath);
91+
}
92+
93+
/**
94+
* Data provider for directory path
95+
*
96+
* @return array
97+
*/
98+
public function directoryPathDataProvider(): array
99+
{
100+
return [
101+
'Existing path' => [self::DIRECTORY_PATH],
102+
'Empty path' => ['']
103+
];
104+
}
105+
}

0 commit comments

Comments
 (0)