Skip to content

Commit dafd5fa

Browse files
committed
ACP2E-2568: Image file is Case insensitive in Pagebuilder(Image not showing in Media Gallery when upload both lowercase and uppercase image with same name)
1 parent 3088085 commit dafd5fa

File tree

2 files changed

+391
-0
lines changed

2 files changed

+391
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<?php
2+
/**
3+
* Copyright 2023 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained
13+
* from Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\MediaGallery\Test\Unit\Model\ResourceModel;
18+
19+
use Magento\Framework\App\ResourceConnection;
20+
use Magento\Framework\DB\Adapter\AdapterInterface;
21+
use Magento\Framework\DB\Select;
22+
use Magento\Framework\Exception\CouldNotDeleteException;
23+
use Magento\MediaGallery\Model\ResourceModel\DeleteAssetsByPaths;
24+
use PHPUnit\Framework\MockObject\MockObject;
25+
use PHPUnit\Framework\TestCase;
26+
use Psr\Log\LoggerInterface;
27+
28+
class DeleteAssetsByPathsTest extends TestCase
29+
{
30+
private const TABLE_NAME = 'media_gallery_asset';
31+
32+
/**
33+
* @var DeleteAssetsByPaths
34+
*/
35+
private $deleteAssetsByPaths;
36+
37+
/**
38+
* @var AdapterInterface|MockObject
39+
*/
40+
private $adapter;
41+
42+
/**
43+
* @var Select|MockObject
44+
*/
45+
private $select;
46+
47+
/**
48+
* @var \Zend_Db_Statement_Interface|MockObject
49+
*/
50+
private $statement;
51+
52+
/**
53+
* When deleting an asset by path with mixed case, the asset with exact same path should be deleted
54+
*
55+
* @dataProvider assetDeleteByPathDataProvider
56+
* @throws CouldNotDeleteException
57+
*/
58+
public function testDeleteCorrectAssetByPathWithCaseSensitiveMatches(
59+
array $assets,
60+
string $assetPathToDelete,
61+
int $assetIdToAssert): void
62+
{
63+
$this->adapter->expects($this->once())->method('select')->willReturn($this->select);
64+
$this->select->expects($this->once())->method('from')->willReturnSelf();
65+
$this->select->expects($this->once())->method('where')->willReturnSelf();
66+
$this->adapter
67+
->expects($this->once())
68+
->method('query')
69+
->with($this->select)
70+
->willReturn($this->statement);
71+
$this->statement->expects($this->once())->method('fetchAll')->willReturn($assets);
72+
73+
$this->adapter->expects($this->once())
74+
->method('delete')
75+
->with(self::TABLE_NAME, ['id = ?' => $assetIdToAssert]);
76+
77+
$this->deleteAssetsByPaths->execute([$assetPathToDelete]);
78+
}
79+
80+
protected function setUp(): void
81+
{
82+
$logger = $this->getMockForAbstractClass(LoggerInterface::class);
83+
$resourceConnection = $this->createMock(ResourceConnection::class);
84+
85+
$this->deleteAssetsByPaths = new DeleteAssetsByPaths(
86+
$resourceConnection,
87+
$logger
88+
);
89+
90+
$this->adapter = $this->getMockForAbstractClass(AdapterInterface::class);
91+
$this->select = $this->createMock(Select::class);
92+
$this->statement = $this->createMock(\Zend_Db_Statement_Interface::class);
93+
94+
95+
$resourceConnection->expects($this->any())
96+
->method('getConnection')
97+
->willReturn($this->adapter);
98+
99+
$resourceConnection->expects($this->any())
100+
->method('getTableName')
101+
->willReturn(self::TABLE_NAME);
102+
}
103+
104+
private function assetDeleteByPathDataProvider(): array
105+
{
106+
return [
107+
[
108+
'assets' => $this->getAssets(),
109+
'pathToDelete' => 'catalog/category/folder/image.jpg',
110+
'assetIdToAssertDelete' => 1
111+
],
112+
[
113+
'assets' => $this->getAssets(),
114+
'pathToDelete' => 'catalog/category/folder/Image.jpg',
115+
'assetIdToAssertDelete' => 2
116+
],
117+
[
118+
'assets' => $this->getAssets(),
119+
'pathToDelete' => 'catalog/category/folder/IMAGE.JPG',
120+
'assetIdToAssertDelete' => 3
121+
],
122+
[
123+
'assets' => $this->getAssets(),
124+
'pathToDelete' => 'catalog/category/FOLDER',
125+
'assetIdToAssertDelete' => 4
126+
],
127+
];
128+
}
129+
130+
private function getAssets(): array
131+
{
132+
return [
133+
[
134+
'id' => '1',
135+
'path' => 'catalog/category/folder/image.jpg',
136+
'title' => 'image',
137+
'description' => NULL,
138+
'source' => 'Local',
139+
'hash' => '20b88741b3cfa5749d414a0312c8b909aefbaa1f',
140+
'content_type' => 'image/jpg',
141+
'width' => '1080',
142+
'height' => '1080',
143+
'size' => '53010',
144+
'created_at' => '2023-11-09 16:33:41',
145+
'updated_at' => '2023-11-09 16:33:41',
146+
],
147+
[
148+
'id' => '2',
149+
'path' => 'catalog/category/folder/Image.jpg',
150+
'title' => 'Image',
151+
'description' => NULL,
152+
'source' => 'Local',
153+
'hash' => '20b88741b3cfa5749d414a0312c8b909aefbaa1f',
154+
'content_type' => 'image/jpg',
155+
'width' => '1080',
156+
'height' => '1080',
157+
'size' => '53010',
158+
'created_at' => '2023-11-09 16:34:19',
159+
'updated_at' => '2023-11-09 16:34:19',
160+
],
161+
[
162+
'id' => '3',
163+
'path' => 'catalog/category/folder/IMAGE.JPG',
164+
'title' => 'IMAGE',
165+
'description' => NULL,
166+
'source' => 'Local',
167+
'hash' => '93a7c1f07373afafcd4918379dacf8e3de6a3eca',
168+
'content_type' => 'image/jpg',
169+
'width' => '1080',
170+
'height' => '1080',
171+
'size' => '101827',
172+
'created_at' => '2023-11-09 16:37:36',
173+
'updated_at' => '2023-11-09 16:37:36',
174+
],
175+
[
176+
'id' => '4',
177+
'path' => 'catalog/category/FOLDER/IMAGE.JPG',
178+
'title' => 'IMAGE',
179+
'description' => NULL,
180+
'source' => 'Local',
181+
'hash' => '93a7c1f07373afafcd4918379dacf8e3de6a3eca',
182+
'content_type' => 'image/jpg',
183+
'width' => '1080',
184+
'height' => '1080',
185+
'size' => '101827',
186+
'created_at' => '2023-11-09 16:37:36',
187+
'updated_at' => '2023-11-09 16:37:36',
188+
]
189+
];
190+
}
191+
}
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
/**
3+
* Copyright 2023 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained
13+
* from Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Unit\Model\ResourceModel;
18+
19+
use Magento\Framework\App\ResourceConnection;
20+
use Magento\Framework\DB\Adapter\AdapterInterface;
21+
use Magento\Framework\DB\Select;
22+
use Magento\MediaGallery\Model\ResourceModel\GetAssetsByPaths;
23+
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory;
24+
use PHPUnit\Framework\MockObject\MockObject;
25+
use PHPUnit\Framework\TestCase;
26+
use Psr\Log\LoggerInterface;
27+
28+
class GetAssetsByPathsTest extends TestCase
29+
{
30+
private const TABLE_NAME = 'media_gallery_asset';
31+
32+
/**
33+
* @var GetAssetsByPaths
34+
*/
35+
private $getAssetsByPaths;
36+
37+
/**
38+
* @var AssetInterfaceFactory|MockObject
39+
*/
40+
private $assetInterfaceFactory;
41+
42+
/**
43+
* @var AdapterInterface|MockObject
44+
*/
45+
private $adapter;
46+
47+
/**
48+
* @var Select|MockObject
49+
*/
50+
private $select;
51+
52+
/**
53+
* @var \Zend_Db_Statement_Interface|MockObject
54+
*/
55+
private $statement;
56+
57+
protected function setUp(): void
58+
{
59+
$logger = $this->getMockForAbstractClass(LoggerInterface::class);
60+
$resourceConnection = $this->createMock(ResourceConnection::class);
61+
$this->assetInterfaceFactory = $this->createMock(AssetInterfaceFactory::class);
62+
63+
$this->getAssetsByPaths = new GetAssetsByPaths(
64+
$resourceConnection,
65+
$this->assetInterfaceFactory,
66+
$logger
67+
);
68+
69+
$this->adapter = $this->getMockForAbstractClass(AdapterInterface::class);
70+
$this->select = $this->createMock(Select::class);
71+
$this->statement = $this->createMock(\Zend_Db_Statement_Interface::class);
72+
73+
74+
$resourceConnection->expects($this->any())
75+
->method('getConnection')
76+
->willReturn($this->adapter);
77+
78+
$resourceConnection->expects($this->any())
79+
->method('getTableName')
80+
->willReturn(self::TABLE_NAME);
81+
}
82+
83+
/**
84+
* When getting an asset by path with mixed case, the asset with exact same path should be loaded
85+
*
86+
* @dataProvider assetDeleteByPathDataProvider
87+
*/
88+
public function testGetCorrectAssetByPathWithCaseSensitiveMatches (
89+
array $assets,
90+
int $assetIndex,
91+
int $resultsCount
92+
): void
93+
{
94+
$this->adapter->expects($this->once())->method('select')->willReturn($this->select);
95+
$this->select->expects($this->once())->method('from')->willReturnSelf();
96+
$this->select->expects($this->once())->method('where')->willReturnSelf();
97+
$this->adapter
98+
->expects($this->once())
99+
->method('query')
100+
->with($this->select)
101+
->willReturn($this->statement);
102+
$this->statement->expects($this->once())->method('fetchAll')->willReturn($assets);
103+
104+
$asset = $assets[$assetIndex];
105+
106+
$factoryParameters = [
107+
'id' => $asset['id'],
108+
'path' => $asset['path'],
109+
'title' => $asset['title'],
110+
'description' => $asset['description'],
111+
'source' => $asset['source'],
112+
'hash' => $asset['hash'],
113+
'contentType' => $asset['content_type'],
114+
'width' => $asset['width'],
115+
'height' => $asset['height'],
116+
'size' => $asset['size'],
117+
'createdAt' => $asset['created_at'],
118+
'updatedAt' => $asset['updated_at'],
119+
];
120+
121+
$this->assetInterfaceFactory
122+
->expects($this->exactly($resultsCount))
123+
->method('create')
124+
->with($factoryParameters);
125+
126+
$this->getAssetsByPaths->execute([$asset['path']]);
127+
}
128+
129+
private function getAssets(): array
130+
{
131+
return [
132+
[
133+
'id' => '1',
134+
'path' => 'catalog/category/folder/image.jpg',
135+
'title' => 'image',
136+
'description' => NULL,
137+
'source' => 'Local',
138+
'hash' => '20b88741b3cfa5749d414a0312c8b909aefbaa1f',
139+
'content_type' => 'image/jpg',
140+
'width' => '1080',
141+
'height' => '1080',
142+
'size' => '53010',
143+
'created_at' => '2023-11-09 16:33:41',
144+
'updated_at' => '2023-11-09 16:33:41',
145+
],
146+
[
147+
'id' => '2',
148+
'path' => 'catalog/category/folder/Image.jpg',
149+
'title' => 'Image',
150+
'description' => NULL,
151+
'source' => 'Local',
152+
'hash' => '20b88741b3cfa5749d414a0312c8b909aefbaa1f',
153+
'content_type' => 'image/jpg',
154+
'width' => '1080',
155+
'height' => '1080',
156+
'size' => '53010',
157+
'created_at' => '2023-11-09 16:34:19',
158+
'updated_at' => '2023-11-09 16:34:19',
159+
],
160+
[
161+
'id' => '3',
162+
'path' => 'catalog/category/folder/IMAGE.JPG',
163+
'title' => 'IMAGE',
164+
'description' => NULL,
165+
'source' => 'Local',
166+
'hash' => '93a7c1f07373afafcd4918379dacf8e3de6a3eca',
167+
'content_type' => 'image/jpg',
168+
'width' => '1080',
169+
'height' => '1080',
170+
'size' => '101827',
171+
'created_at' => '2023-11-09 16:37:36',
172+
'updated_at' => '2023-11-09 16:37:36',
173+
],
174+
[
175+
'id' => '4',
176+
'path' => 'catalog/category/FOLDER/IMAGE.JPG',
177+
'title' => 'IMAGE',
178+
'description' => NULL,
179+
'source' => 'Local',
180+
'hash' => '93a7c1f07373afafcd4918379dacf8e3de6a3eca',
181+
'content_type' => 'image/jpg',
182+
'width' => '1080',
183+
'height' => '1080',
184+
'size' => '101827',
185+
'created_at' => '2023-11-09 16:37:36',
186+
'updated_at' => '2023-11-09 16:37:36',
187+
]
188+
];
189+
}
190+
private function assetDeleteByPathDataProvider(): array
191+
{
192+
return [
193+
[
194+
'assets' => $this->getAssets(),
195+
'assetIndex' => 0,
196+
'resultsCount' => 1
197+
],
198+
];
199+
}
200+
}

0 commit comments

Comments
 (0)