Skip to content

Commit ba4ba91

Browse files
committed
Merge branch 'ACP2E-1959' of https://github.com/magento-l3/magento2ce into PR-L3-08112023
2 parents de72dfe + 8e95a04 commit ba4ba91

File tree

2 files changed

+130
-8
lines changed

2 files changed

+130
-8
lines changed

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

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@
77

88
namespace Magento\Catalog\Model\Product\Image;
99

10+
use Magento\Framework\App\Area;
1011
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\App\ObjectManager;
1113
use Magento\Framework\View\ConfigInterface;
14+
use Magento\Framework\View\Design\Theme\FlyweightFactory;
15+
use Magento\Framework\View\Design\ThemeInterface;
16+
use Magento\Framework\View\DesignInterface;
1217
use Magento\Store\Model\ScopeInterface;
1318
use Magento\Catalog\Model\Product\Image;
1419

@@ -52,16 +57,42 @@ class ParamsBuilder
5257
*/
5358
private $viewConfig;
5459

60+
/**
61+
* @var DesignInterface
62+
*/
63+
private $design;
64+
65+
/**
66+
* @var FlyweightFactory
67+
*/
68+
private $themeFactory;
69+
70+
/**
71+
* @var ThemeInterface
72+
*/
73+
private $currentTheme;
74+
75+
/**
76+
* @var array
77+
*/
78+
private $themesList = [];
79+
5580
/**
5681
* @param ScopeConfigInterface $scopeConfig
5782
* @param ConfigInterface $viewConfig
83+
* @param DesignInterface|null $designInterface
84+
* @param FlyweightFactory|null $themeFactory
5885
*/
5986
public function __construct(
6087
ScopeConfigInterface $scopeConfig,
61-
ConfigInterface $viewConfig
88+
ConfigInterface $viewConfig,
89+
DesignInterface $designInterface = null,
90+
FlyweightFactory $themeFactory = null
6291
) {
6392
$this->scopeConfig = $scopeConfig;
6493
$this->viewConfig = $viewConfig;
94+
$this->design = $designInterface ?? ObjectManager::getInstance()->get(DesignInterface::class);
95+
$this->themeFactory = $themeFactory ?? ObjectManager::getInstance()->get(FlyweightFactory::class);
6596
}
6697

6798
/**
@@ -75,6 +106,8 @@ public function __construct(
75106
*/
76107
public function build(array $imageArguments, int $scopeId = null): array
77108
{
109+
$this->determineCurrentTheme($scopeId);
110+
78111
$miscParams = [
79112
'image_type' => $imageArguments['type'] ?? null,
80113
'image_height' => $imageArguments['height'] ?? null,
@@ -87,6 +120,25 @@ public function build(array $imageArguments, int $scopeId = null): array
87120
return array_merge($miscParams, $overwritten, $watermark);
88121
}
89122

123+
/**
124+
* Determine the theme assigned to passed scope id
125+
*
126+
* @param int|null $scopeId
127+
* @return void
128+
*/
129+
private function determineCurrentTheme(int $scopeId = null): void
130+
{
131+
if (is_numeric($scopeId) || !$this->currentTheme) {
132+
$themeId = $this->design->getConfigurationDesignTheme(Area::AREA_FRONTEND, ['store' => $scopeId]);
133+
if (isset($this->themesList[$themeId])) {
134+
$this->currentTheme = $this->themesList[$themeId];
135+
} else {
136+
$this->currentTheme = $this->themeFactory->create($themeId);
137+
$this->themesList[$themeId] = $this->currentTheme;
138+
}
139+
}
140+
}
141+
90142
/**
91143
* Overwrite default values
92144
*
@@ -170,7 +222,11 @@ private function getWatermark(string $type, int $scopeId = null): array
170222
*/
171223
private function hasDefaultFrame(): bool
172224
{
173-
return (bool) $this->viewConfig->getViewConfig(['area' => \Magento\Framework\App\Area::AREA_FRONTEND])
174-
->getVarValue('Magento_Catalog', 'product_image_white_borders');
225+
return (bool) $this->viewConfig->getViewConfig(
226+
[
227+
'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
228+
'themeModel' => $this->currentTheme
229+
]
230+
)->getVarValue('Magento_Catalog', 'product_image_white_borders');
175231
}
176232
}

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

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Magento\Framework\Config\View;
1515
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1616
use Magento\Framework\View\ConfigInterface;
17+
use Magento\Framework\View\Design\Theme\FlyweightFactory;
18+
use Magento\Framework\View\Design\ThemeInterface;
19+
use Magento\Framework\View\DesignInterface;
1720
use Magento\Store\Model\ScopeInterface;
1821
use PHPUnit\Framework\TestCase;
1922

@@ -41,6 +44,21 @@ class ParamsBuilderTest extends TestCase
4144
*/
4245
private $scopeConfigData = [];
4346

47+
/**
48+
* @var DesignInterface
49+
*/
50+
private $design;
51+
52+
/**
53+
* @var FlyweightFactory
54+
*/
55+
private $themeFactory;
56+
57+
/**
58+
* @var ThemeInterface
59+
*/
60+
private $theme;
61+
4462
/**
4563
* @inheritDoc
4664
*/
@@ -49,11 +67,19 @@ protected function setUp(): void
4967
$objectManager = new ObjectManager($this);
5068
$this->scopeConfig = $this->getMockForAbstractClass(ScopeConfigInterface::class);
5169
$this->viewConfig = $this->getMockForAbstractClass(ConfigInterface::class);
70+
$this->design = $this->getMockBuilder(DesignInterface::class)
71+
->disableOriginalConstructor()
72+
->getMockForAbstractClass();
73+
$this->themeFactory = $this->createMock(FlyweightFactory::class);
74+
$this->theme = $this->getMockForAbstractClass(ThemeInterface::class);
75+
5276
$this->model = $objectManager->getObject(
5377
ParamsBuilder::class,
5478
[
5579
'scopeConfig' => $this->scopeConfig,
5680
'viewConfig' => $this->viewConfig,
81+
'design' => $this->design,
82+
'themeFactory' => $this->themeFactory
5783
]
5884
);
5985
$this->scopeConfigData = [];
@@ -69,13 +95,21 @@ function ($path, $scopeType, $scopeCode) {
6995
* Test build() with different parameters and config values
7096
*
7197
* @param int $scopeId
98+
* @param string $themeId
99+
* @param bool $keepFrame
72100
* @param array $config
73101
* @param array $imageArguments
74102
* @param array $expected
75103
* @dataProvider buildDataProvider
76104
*/
77-
public function testBuild(int $scopeId, array $config, array $imageArguments, array $expected)
78-
{
105+
public function testBuild(
106+
int $scopeId,
107+
string $themeId,
108+
bool $keepFrame,
109+
array $config,
110+
array $imageArguments,
111+
array $expected
112+
) {
79113
$this->scopeConfigData[Image::XML_PATH_JPEG_QUALITY][ScopeConfigInterface::SCOPE_TYPE_DEFAULT][null] = 80;
80114
foreach ($config as $path => $value) {
81115
$this->scopeConfigData[$path][ScopeInterface::SCOPE_STORE][$scopeId] = $value;
@@ -88,15 +122,23 @@ public function testBuild(int $scopeId, array $config, array $imageArguments, ar
88122
'background' => [110, 64, 224]
89123
];
90124

125+
$this->design->expects($this->once())
126+
->method('getConfigurationDesignTheme')
127+
->willReturn($themeId);
128+
$this->themeFactory->expects($this->once())
129+
->method('create')
130+
->with($themeId)
131+
->willReturn($this->theme);
132+
91133
$viewMock = $this->createMock(View::class);
92134
$viewMock->expects($this->once())
93135
->method('getVarValue')
94136
->with('Magento_Catalog', 'product_image_white_borders')
95-
->willReturn(true);
137+
->willReturn($keepFrame);
96138

97139
$this->viewConfig->expects($this->once())
98140
->method('getViewConfig')
99-
->with(['area' => Area::AREA_FRONTEND])
141+
->with(['area' => Area::AREA_FRONTEND, 'themeModel' => $this->theme])
100142
->willReturn($viewMock);
101143

102144
$actual = $this->model->build($imageArguments, $scopeId);
@@ -106,7 +148,6 @@ public function testBuild(int $scopeId, array $config, array $imageArguments, ar
106148
'angle' => $imageArguments['angle'],
107149
'quality' => 80,
108150
'keep_aspect_ratio' => true,
109-
'keep_frame' => true,
110151
'keep_transparency' => true,
111152
'constrain_only' => true,
112153
'image_height' => $imageArguments['height'],
@@ -129,6 +170,8 @@ public function buildDataProvider()
129170
return [
130171
'watermark config' => [
131172
1,
173+
'1',
174+
true,
132175
[
133176
'design/watermark/small_image_image' => 'stores/1/magento-logo.png',
134177
'design/watermark/small_image_size' => '60x40',
@@ -144,10 +187,32 @@ public function buildDataProvider()
144187
'watermark_position' => 'bottom-right',
145188
'watermark_width' => '60',
146189
'watermark_height' => '40',
190+
'keep_frame' => true
147191
]
148192
],
149193
'watermark config empty' => [
150194
1,
195+
'1',
196+
true,
197+
[
198+
'design/watermark/small_image_image' => 'stores/1/magento-logo.png',
199+
],
200+
[
201+
'type' => 'small_image'
202+
],
203+
[
204+
'watermark_file' => 'stores/1/magento-logo.png',
205+
'watermark_image_opacity' => null,
206+
'watermark_position' => null,
207+
'watermark_width' => null,
208+
'watermark_height' => null,
209+
'keep_frame' => true
210+
]
211+
],
212+
'watermark empty with no border' => [
213+
2,
214+
'2',
215+
false,
151216
[
152217
'design/watermark/small_image_image' => 'stores/1/magento-logo.png',
153218
],
@@ -160,6 +225,7 @@ public function buildDataProvider()
160225
'watermark_position' => null,
161226
'watermark_width' => null,
162227
'watermark_height' => null,
228+
'keep_frame' => false
163229
]
164230
]
165231
];

0 commit comments

Comments
 (0)