Skip to content

Commit c39007b

Browse files
committed
ACP2E-2093: Issue when uploading image with the same name while deleting the old image
- Fixed the CR comments.
1 parent eb5dc54 commit c39007b

File tree

2 files changed

+163
-18
lines changed

2 files changed

+163
-18
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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\Catalog\Test\Unit\Model\Product\Image;
9+
10+
use Magento\Catalog\Model\Product\Image\ConvertImageMiscParamsToReadableFormat;
11+
use PHPUnit\Framework\MockObject\MockObject;
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* Test convert image misc params to readable format
16+
*/
17+
class ConvertImageMiscParamsToReadableFormatTest extends TestCase
18+
{
19+
/**
20+
* @var ConvertImageMiscParamsToReadableFormat|MockObject
21+
*/
22+
protected ConvertImageMiscParamsToReadableFormat|MockObject $model;
23+
24+
protected function setUp(): void
25+
{
26+
$this->model = new ConvertImageMiscParamsToReadableFormat();
27+
}
28+
29+
/**
30+
* @param array $data
31+
* @return void
32+
* @dataProvider createDataProvider
33+
*/
34+
public function testConvertImageMiscParamsToReadableFormat(array $data): void
35+
{
36+
$this->assertEquals(
37+
$data['expectedMiscParamsWithArray'],
38+
$this->model->convertImageMiscParamsToReadableFormat($data['convertImageParamsToReadableFormatWithArray'])
39+
);
40+
$this->assertEquals(
41+
$data['expectedMiscParamsWithOutArray'],
42+
$this->model->convertImageMiscParamsToReadableFormat($data['convertImageParamsToReadableFormatWithOutArray'])
43+
);
44+
}
45+
46+
/**
47+
* @return array
48+
*/
49+
public function createDataProvider(): array
50+
{
51+
return [
52+
$this->getTestDataWithAttributes()
53+
];
54+
}
55+
56+
/**
57+
* @return array
58+
*/
59+
private function getTestDataWithAttributes(): array
60+
{
61+
return [
62+
'data' => [
63+
'convertImageParamsToReadableFormatWithArray' => [
64+
'image_height' => '50',
65+
'image_width' => '100',
66+
'quality' => '80',
67+
'angle' => '90',
68+
'keep_aspect_ratio' => 'proportional',
69+
'keep_frame' => 'frame',
70+
'keep_transparency' => 'transparency',
71+
'constrain_only' => 'constrainonly',
72+
'background' => [255,255,255]
73+
],
74+
'convertImageParamsToReadableFormatWithOutArray' => [],
75+
'expectedMiscParamsWithArray' => [
76+
'image_height' => 'h:50',
77+
'image_width' => 'w:100',
78+
'quality' => 'q:80',
79+
'angle' => 'r:90',
80+
'keep_aspect_ratio' => 'proportional',
81+
'keep_frame' => 'frame',
82+
'keep_transparency' => 'transparency',
83+
'constrain_only' => 'doconstrainonly',
84+
'background' => 'rgb255,255,255'
85+
],
86+
'expectedMiscParamsWithOutArray' => [
87+
'image_height' => 'h:empty',
88+
'image_width' => 'w:empty',
89+
'quality' => 'q:empty',
90+
'angle' => 'r:empty',
91+
'keep_aspect_ratio' => 'nonproportional',
92+
'keep_frame' => 'noframe',
93+
'keep_transparency' => 'notransparency',
94+
'constrain_only' => 'notconstrainonly',
95+
'background' => 'nobackground'
96+
]
97+
]
98+
];
99+
}
100+
}

app/code/Magento/Catalog/Test/Unit/Model/Product/Image/RemoveDeletedImagesFromCacheTest.php

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Magento\Catalog\Model\Product\Media\Config;
1414
use Magento\Framework\Config\View;
1515
use Magento\Framework\Encryption\EncryptorInterface;
16+
use Magento\Framework\Exception\FileSystemException;
17+
use Magento\Framework\Phrase;
1618
use Magento\Framework\Filesystem;
1719
use Magento\Framework\Filesystem\Directory\Write;
1820
use Magento\Framework\View\ConfigInterface;
@@ -25,44 +27,44 @@
2527
class RemoveDeletedImagesFromCacheTest extends TestCase
2628
{
2729
/**
28-
* @var RemoveDeletedImagesFromCache|MockObject
30+
* @var MockObject|RemoveDeletedImagesFromCache
2931
*/
30-
protected $model;
32+
protected RemoveDeletedImagesFromCache|MockObject $model;
3133

3234
/**
3335
* @var ConfigInterface|MockObject
3436
*/
35-
protected $presentationConfig;
37+
protected ConfigInterface|MockObject $presentationConfig;
3638

3739
/**
3840
* @var EncryptorInterface|MockObject
3941
*/
40-
protected $encryptor;
42+
protected EncryptorInterface|MockObject $encryptor;
4143

4244
/**
4345
* @var Config|MockObject
4446
*/
45-
protected $mediaConfig;
47+
protected Config|MockObject $mediaConfig;
4648

4749
/**
48-
* @var Write|MockObject
50+
* @var MockObject|Write
4951
*/
50-
protected $mediaDirectory;
52+
protected Write|MockObject $mediaDirectory;
5153

5254
/**
53-
* @var ParamsBuilder|MockObject
55+
* @var MockObject|ParamsBuilder
5456
*/
55-
protected $imageParamsBuilder;
57+
protected ParamsBuilder|MockObject $imageParamsBuilder;
5658

5759
/**
5860
* @var ConvertImageMiscParamsToReadableFormat|MockObject
5961
*/
60-
protected $convertImageMiscParamsToReadableFormat;
62+
protected ConvertImageMiscParamsToReadableFormat|MockObject $convertImageMiscParamsToReadableFormat;
6163

6264
/**
63-
* @var View|MockObject
65+
* @var MockObject|View
6466
*/
65-
protected $viewMock;
67+
protected View|MockObject $viewMock;
6668

6769
protected function setUp(): void
6870
{
@@ -102,6 +104,55 @@ protected function setUp(): void
102104
* @dataProvider createDataProvider
103105
*/
104106
public function testRemoveDeletedImagesFromCache(array $data): void
107+
{
108+
$this->getRespectiveMethodMockObjForRemoveDeletedImagesFromCache($data);
109+
110+
$this->mediaDirectory->expects($this->once())
111+
->method('delete')
112+
->willReturn(true);
113+
114+
$this->model->removeDeletedImagesFromCache(['i/m/image.jpg']);
115+
}
116+
117+
/**
118+
* @param array $data
119+
* @return void
120+
* @dataProvider createDataProvider
121+
*/
122+
public function testRemoveDeletedImagesFromCacheWithException(array $data): void
123+
{
124+
$this->getRespectiveMethodMockObjForRemoveDeletedImagesFromCache($data);
125+
126+
$this->expectException('Exception');
127+
$this->expectExceptionMessage('Unable to write file into directory product/cache. Access forbidden.');
128+
129+
$exception = new FileSystemException(
130+
new Phrase('Unable to write file into directory product/cache. Access forbidden.')
131+
);
132+
133+
$this->mediaDirectory->expects($this->once())
134+
->method('delete')
135+
->willThrowException($exception);
136+
137+
$this->model->removeDeletedImagesFromCache(['i/m/image.jpg']);
138+
}
139+
140+
/**
141+
* @return void
142+
*/
143+
public function testRemoveDeletedImagesFromCacheWithEmptyFiles(): void
144+
{
145+
$this->assertEquals(
146+
null,
147+
$this->model->removeDeletedImagesFromCache([])
148+
);
149+
}
150+
151+
/**
152+
* @param array $data
153+
* @return void
154+
*/
155+
public function getRespectiveMethodMockObjForRemoveDeletedImagesFromCache(array $data): void
105156
{
106157
$this->presentationConfig->expects($this->once())
107158
->method('getViewConfig')
@@ -127,12 +178,6 @@ public function testRemoveDeletedImagesFromCache(array $data): void
127178
$this->mediaConfig->expects($this->once())
128179
->method('getBaseMediaPath')
129180
->willReturn('catalog/product');
130-
131-
$this->mediaDirectory->expects($this->once())
132-
->method('delete')
133-
->willReturn(true);
134-
135-
$this->model->removeDeletedImagesFromCache(['i/m/image.jpg']);
136181
}
137182

138183
/**

0 commit comments

Comments
 (0)