Skip to content

Commit 3cddbc2

Browse files
committed
Merge remote-tracking branch 'origin/MC-30234' into 2.4-develop-pr8
2 parents 051b907 + a8c981b commit 3cddbc2

File tree

2 files changed

+91
-57
lines changed

2 files changed

+91
-57
lines changed

app/code/Magento/Catalog/Model/Product/Image/ParamsBuilder.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,10 @@ private function getWatermark(string $type, int $scopeId = null): array
132132
if ($file) {
133133
$size = explode(
134134
'x',
135-
$this->scopeConfig->getValue(
135+
(string) $this->scopeConfig->getValue(
136136
"design/watermark/{$type}_size",
137-
ScopeInterface::SCOPE_STORE
137+
ScopeInterface::SCOPE_STORE,
138+
$scopeId
138139
)
139140
);
140141
$opacity = $this->scopeConfig->getValue(

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

Lines changed: 88 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Catalog\Test\Unit\Model\Product\Image;
89

@@ -11,10 +12,15 @@
1112
use Magento\Framework\App\Area;
1213
use Magento\Framework\App\Config\ScopeConfigInterface;
1314
use Magento\Framework\Config\View;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1416
use Magento\Framework\View\ConfigInterface;
1517
use Magento\Store\Model\ScopeInterface;
18+
use PHPUnit\Framework\TestCase;
1619

17-
class ParamsBuilderTest extends \PHPUnit\Framework\TestCase
20+
/**
21+
* Test product image params builder
22+
*/
23+
class ParamsBuilderTest extends TestCase
1824
{
1925
/**
2026
* @var ScopeConfigInterface
@@ -30,10 +36,17 @@ class ParamsBuilderTest extends \PHPUnit\Framework\TestCase
3036
* @var ParamsBuilder
3137
*/
3238
private $model;
39+
/**
40+
* @var array
41+
*/
42+
private $scopeConfigData = [];
3343

44+
/**
45+
* @inheritDoc
46+
*/
3447
protected function setUp()
3548
{
36-
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
49+
$objectManager = new ObjectManager($this);
3750
$this->scopeConfig = $this->createMock(ScopeConfigInterface::class);
3851
$this->viewConfig = $this->createMock(ConfigInterface::class);
3952
$this->model = $objectManager->getObject(
@@ -43,28 +56,37 @@ protected function setUp()
4356
'viewConfig' => $this->viewConfig,
4457
]
4558
);
59+
$this->scopeConfigData = [];
60+
$this->scopeConfig->method('getValue')
61+
->willReturnCallback(
62+
function ($path, $scopeType, $scopeCode) {
63+
return $this->scopeConfigData[$path][$scopeType][$scopeCode] ?? null;
64+
}
65+
);
4666
}
4767

4868
/**
49-
* Test watermark location.
69+
* Test build() with different parameters and config values
70+
*
71+
* @param int $scopeId
72+
* @param array $config
73+
* @param array $imageArguments
74+
* @param array $expected
75+
* @dataProvider buildDataProvider
5076
*/
51-
public function testWatermarkLocation()
77+
public function testBuild(int $scopeId, array $config, array $imageArguments, array $expected)
5278
{
53-
$imageArguments = [
54-
'type' => 'type',
55-
'height' => 'image_height',
56-
'width' => 'image_width',
57-
'angle' => 'angle',
58-
'background' => [1, 2, 3]
79+
$this->scopeConfigData[Image::XML_PATH_JPEG_QUALITY][ScopeConfigInterface::SCOPE_TYPE_DEFAULT][null] = 80;
80+
foreach ($config as $path => $value) {
81+
$this->scopeConfigData[$path][ScopeInterface::SCOPE_STORE][$scopeId] = $value;
82+
}
83+
$imageArguments += [
84+
'type' => 'image',
85+
'height' => '600',
86+
'width' => '400',
87+
'angle' => '45',
88+
'background' => [110, 64, 224]
5989
];
60-
$scopeId = 1;
61-
$quality = 100;
62-
$file = 'file';
63-
$width = 'width';
64-
$height = 'height';
65-
$size = "{$width}x{$height}";
66-
$opacity = 'opacity';
67-
$position = 'position';
6890

6991
$viewMock = $this->createMock(View::class);
7092
$viewMock->expects($this->once())
@@ -77,51 +99,16 @@ public function testWatermarkLocation()
7799
->with(['area' => Area::AREA_FRONTEND])
78100
->willReturn($viewMock);
79101

80-
$this->scopeConfig->expects($this->exactly(5))->method('getValue')->withConsecutive(
81-
[
82-
Image::XML_PATH_JPEG_QUALITY
83-
],
84-
[
85-
"design/watermark/{$imageArguments['type']}_image",
86-
ScopeInterface::SCOPE_STORE,
87-
$scopeId,
88-
],
89-
[
90-
"design/watermark/{$imageArguments['type']}_size",
91-
ScopeInterface::SCOPE_STORE],
92-
[
93-
"design/watermark/{$imageArguments['type']}_imageOpacity",
94-
ScopeInterface::SCOPE_STORE,
95-
$scopeId
96-
],
97-
[
98-
"design/watermark/{$imageArguments['type']}_position",
99-
ScopeInterface::SCOPE_STORE,
100-
$scopeId
101-
]
102-
)->willReturnOnConsecutiveCalls(
103-
$quality,
104-
$file,
105-
$size,
106-
$opacity,
107-
$position
108-
);
109-
110102
$actual = $this->model->build($imageArguments, $scopeId);
111-
$expected = [
103+
$expected += [
112104
'image_type' => $imageArguments['type'],
113105
'background' => $imageArguments['background'],
114106
'angle' => $imageArguments['angle'],
115-
'quality' => $quality,
107+
'quality' => 80,
116108
'keep_aspect_ratio' => true,
117109
'keep_frame' => true,
118110
'keep_transparency' => true,
119111
'constrain_only' => true,
120-
'watermark_file' => $file,
121-
'watermark_image_opacity' => $opacity,
122-
'watermark_position' => $position,
123-
'watermark_width' => $width,
124-
'watermark_height' => $height,
125112
'image_height' => $imageArguments['height'],
126113
'image_width' => $imageArguments['width'],
127114
];
@@ -131,4 +118,50 @@ public function testWatermarkLocation()
131118
$actual
132119
);
133120
}
121+
122+
/**
123+
* Provides test scenarios for
124+
*
125+
* @return array
126+
*/
127+
public function buildDataProvider()
128+
{
129+
return [
130+
'watermark config' => [
131+
1,
132+
[
133+
'design/watermark/small_image_image' => 'stores/1/magento-logo.png',
134+
'design/watermark/small_image_size' => '60x40',
135+
'design/watermark/small_image_imageOpacity' => '50',
136+
'design/watermark/small_image_position' => 'bottom-right',
137+
],
138+
[
139+
'type' => 'small_image'
140+
],
141+
[
142+
'watermark_file' => 'stores/1/magento-logo.png',
143+
'watermark_image_opacity' => '50',
144+
'watermark_position' => 'bottom-right',
145+
'watermark_width' => '60',
146+
'watermark_height' => '40',
147+
]
148+
],
149+
'watermark config empty' => [
150+
1,
151+
[
152+
'design/watermark/small_image_image' => 'stores/1/magento-logo.png',
153+
],
154+
[
155+
'type' => 'small_image'
156+
],
157+
[
158+
'watermark_file' => 'stores/1/magento-logo.png',
159+
'watermark_image_opacity' => null,
160+
'watermark_position' => null,
161+
'watermark_width' => null,
162+
'watermark_height' => null,
163+
]
164+
]
165+
];
166+
}
134167
}

0 commit comments

Comments
 (0)