Skip to content

Commit 08228b7

Browse files
committed
Merge branch 'ACP2E-3781' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-04-03-2025
2 parents 522dd18 + bd3c606 commit 08228b7

File tree

3 files changed

+134
-58
lines changed

3 files changed

+134
-58
lines changed
Lines changed: 14 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2020 Adobe
4+
* All Rights Reserved.
55
*/
66

77
declare(strict_types=1);
88

99
namespace Magento\MediaGalleryRenditions\Model;
1010

11+
use Magento\Framework\App\Config\Initial;
1112
use Magento\Framework\App\Config\ScopeConfigInterface;
12-
use Magento\Framework\App\ResourceConnection;
13-
use Magento\Framework\Exception\NoSuchEntityException;
1413

1514
/**
1615
* Class responsible for providing access to Media Gallery Renditions system configuration.
1716
*/
1817
class Config
1918
{
20-
private const TABLE_CORE_CONFIG_DATA = 'core_config_data';
2119
private const XML_PATH_MEDIA_GALLERY_ENABLED = 'system/media_gallery/enabled';
2220
private const XML_PATH_ENABLED = 'system/media_gallery_renditions/enabled';
2321
private const XML_PATH_MEDIA_GALLERY_RENDITIONS_WIDTH_PATH = 'system/media_gallery_renditions/width';
@@ -29,20 +27,20 @@ class Config
2927
private $scopeConfig;
3028

3129
/**
32-
* @var ResourceConnection
30+
* @var Initial
3331
*/
34-
private $resourceConnection;
32+
private $initialConfig;
3533

3634
/**
3735
* @param ScopeConfigInterface $scopeConfig
38-
* @param ResourceConnection $resourceConnection
36+
* @param Initial $initialConfig
3937
*/
4038
public function __construct(
4139
ScopeConfigInterface $scopeConfig,
42-
ResourceConnection $resourceConnection
40+
Initial $initialConfig
4341
) {
4442
$this->scopeConfig = $scopeConfig;
45-
$this->resourceConnection = $resourceConnection;
43+
$this->initialConfig = $initialConfig;
4644
}
4745

4846
/**
@@ -72,11 +70,9 @@ public function isEnabled(): bool
7270
*/
7371
public function getWidth(): int
7472
{
75-
try {
76-
return $this->getDatabaseValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_WIDTH_PATH);
77-
} catch (NoSuchEntityException $exception) {
78-
return (int) $this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_WIDTH_PATH);
79-
}
73+
$width = $this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_WIDTH_PATH)
74+
?? $this->initialConfig->getData('default')['system']['media_gallery_renditions']['width'];
75+
return (int)$width;
8076
}
8177

8278
/**
@@ -86,44 +82,8 @@ public function getWidth(): int
8682
*/
8783
public function getHeight(): int
8884
{
89-
try {
90-
return $this->getDatabaseValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_HEIGHT_PATH);
91-
} catch (NoSuchEntityException $exception) {
92-
return (int) $this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_HEIGHT_PATH);
93-
}
94-
}
95-
96-
/**
97-
* Get value from database bypassing config cache
98-
*
99-
* @param string $path
100-
* @return int
101-
* @throws NoSuchEntityException
102-
*/
103-
private function getDatabaseValue(string $path): int
104-
{
105-
$connection = $this->resourceConnection->getConnection();
106-
$select = $connection->select()
107-
->from(
108-
[
109-
'config' => $this->resourceConnection->getTableName(self::TABLE_CORE_CONFIG_DATA)
110-
],
111-
[
112-
'value'
113-
]
114-
)
115-
->where('config.path = ?', $path);
116-
$value = $connection->query($select)->fetchColumn();
117-
118-
if ($value === false) {
119-
throw new NoSuchEntityException(
120-
__(
121-
'The config value for %path is not saved to database.',
122-
['path' => $path]
123-
)
124-
);
125-
}
126-
127-
return (int) $value;
85+
$height = $this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_HEIGHT_PATH)
86+
?? $this->initialConfig->getData('default')['system']['media_gallery_renditions']['height'];
87+
return (int)$height;
12888
}
12989
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaGalleryRenditions\Test\Unit\Model;
9+
10+
use Magento\Framework\App\Config\Initial;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\MediaGalleryRenditions\Model\Config;
13+
use PHPUnit\Framework\MockObject\MockObject;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class ConfigTest extends TestCase
17+
{
18+
/**
19+
* @var ScopeConfigInterface|MockObject
20+
*/
21+
private $scopeConfigMock;
22+
23+
/**
24+
* @var Initial|MockObject
25+
*/
26+
private $initialConfigMock;
27+
28+
/**
29+
* @var Config
30+
*/
31+
private $config;
32+
33+
protected function setUp(): void
34+
{
35+
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
36+
$this->initialConfigMock = $this->createMock(Initial::class);
37+
$this->config = new Config(
38+
$this->scopeConfigMock,
39+
$this->initialConfigMock
40+
);
41+
}
42+
43+
/**
44+
* Test getWidth() using scopeConfig value
45+
*/
46+
public function testGetWidthFromScopeConfig(): void
47+
{
48+
$expectedWidth = 800;
49+
$widthPath = 'system/media_gallery_renditions/width';
50+
$this->scopeConfigMock->method('getValue')
51+
->with($widthPath)
52+
->willReturn($expectedWidth);
53+
$result = $this->config->getWidth();
54+
$this->assertEquals($expectedWidth, $result);
55+
}
56+
57+
/**
58+
* Test getWidth() falling back to initial config
59+
*/
60+
public function testGetWidthFromInitialConfig(): void
61+
{
62+
$expectedWidth = 600;
63+
$widthPath = 'system/media_gallery_renditions/width';
64+
$this->scopeConfigMock->method('getValue')
65+
->with($widthPath)
66+
->willReturn(null);
67+
$this->initialConfigMock->method('getData')
68+
->with('default')
69+
->willReturn([
70+
'system' => [
71+
'media_gallery_renditions' => [
72+
'width' => $expectedWidth
73+
]
74+
]
75+
]);
76+
$result = $this->config->getWidth();
77+
$this->assertEquals($expectedWidth, $result);
78+
}
79+
80+
/**
81+
* Test getHeight() using scopeConfig value
82+
*/
83+
public function testGetHeightFromScopeConfig(): void
84+
{
85+
$expectedHeight = 600;
86+
$heightPath = 'system/media_gallery_renditions/height';
87+
$this->scopeConfigMock->method('getValue')
88+
->with($heightPath)
89+
->willReturn($expectedHeight);
90+
$result = $this->config->getHeight();
91+
$this->assertEquals($expectedHeight, $result);
92+
}
93+
94+
/**
95+
* Test getHeight() falling back to initial config
96+
*/
97+
public function testGetHeightFromInitialConfig(): void
98+
{
99+
$expectedHeight = 400;
100+
$heightPath = 'system/media_gallery_renditions/height';
101+
$this->scopeConfigMock->method('getValue')
102+
->with($heightPath)
103+
->willReturn(null);
104+
$this->initialConfigMock->method('getData')
105+
->with('default')
106+
->willReturn([
107+
'system' => [
108+
'media_gallery_renditions' => [
109+
'height' => $expectedHeight
110+
]
111+
]
112+
]);
113+
$result = $this->config->getHeight();
114+
$this->assertEquals($expectedHeight, $result);
115+
}
116+
}

app/code/Magento/MediaGalleryRenditions/etc/adminhtml/system.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2020 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
@@ -18,12 +18,12 @@
1818
Changing these settings will update all generated images.</comment>
1919
<field id="width" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="0" showInStore="0">
2020
<label>Maximum Width</label>
21-
<validate>validate-zero-or-greater validate-digits</validate>
21+
<validate>validate-greater-than-zero validate-digits required-entry</validate>
2222
<comment>Enter the maximum width of an image in pixels.</comment>
2323
</field>
2424
<field id="height" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="0" showInStore="0">
2525
<label>Maximum Height</label>
26-
<validate>validate-zero-or-greater validate-digits</validate>
26+
<validate>validate-greater-than-zero validate-digits required-entry</validate>
2727
<comment>Enter the maximum height of an image in pixels.</comment>
2828
</field>
2929
</group>

0 commit comments

Comments
 (0)