Skip to content

Commit e629c0e

Browse files
committed
ACP2E-3781: Exception 'Cannot create rendition for media asset paths' during image insertion
1 parent db1cba1 commit e629c0e

File tree

3 files changed

+33
-124
lines changed

3 files changed

+33
-124
lines changed

app/code/Magento/MediaGalleryRenditions/Model/Config.php

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,9 @@ public function isEnabled(): bool
8181
*/
8282
public function getWidth(): int
8383
{
84-
try {
85-
return $this->getDatabaseValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_WIDTH_PATH);
86-
} catch (NoSuchEntityException $exception) {
87-
$configData = $this->initialConfig->getData('default');
88-
$width = $configData['system']['media_gallery_renditions']['width'] ?? 0;
89-
return (int) $width;
90-
}
84+
$width = $this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_WIDTH_PATH)
85+
?? $this->initialConfig->getData('default')['system']['media_gallery_renditions']['width'];
86+
return (int)$width;
9187
}
9288

9389
/**
@@ -97,46 +93,8 @@ public function getWidth(): int
9793
*/
9894
public function getHeight(): int
9995
{
100-
try {
101-
return $this->getDatabaseValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_HEIGHT_PATH);
102-
} catch (NoSuchEntityException $exception) {
103-
$configData = $this->initialConfig->getData('default');
104-
$height = $configData['system']['media_gallery_renditions']['height'] ?? 0;
105-
return (int) $height;
106-
}
107-
}
108-
109-
/**
110-
* Get value from database bypassing config cache
111-
*
112-
* @param string $path
113-
* @return int
114-
* @throws NoSuchEntityException
115-
*/
116-
private function getDatabaseValue(string $path): int
117-
{
118-
$connection = $this->resourceConnection->getConnection();
119-
$select = $connection->select()
120-
->from(
121-
[
122-
'config' => $this->resourceConnection->getTableName(self::TABLE_CORE_CONFIG_DATA)
123-
],
124-
[
125-
'value'
126-
]
127-
)
128-
->where('config.path = ?', $path);
129-
$value = $connection->query($select)->fetchColumn();
130-
131-
if ($value === false || $value === null) {
132-
throw new NoSuchEntityException(
133-
__(
134-
'The config value for %path is not saved to database.',
135-
['path' => $path]
136-
)
137-
);
138-
}
139-
140-
return (int) $value;
96+
$height = $this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_HEIGHT_PATH)
97+
?? $this->initialConfig->getData('default')['system']['media_gallery_renditions']['height'];
98+
return (int)$height;
14199
}
142100
}

app/code/Magento/MediaGalleryRenditions/Test/Unit/Model/ConfigTest.php

Lines changed: 23 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
use Magento\Framework\App\Config\Initial;
1111
use Magento\Framework\App\Config\ScopeConfigInterface;
1212
use Magento\Framework\App\ResourceConnection;
13-
use Magento\Framework\DB\Adapter\AdapterInterface;
14-
use Magento\Framework\DB\Select;
1513
use Magento\MediaGalleryRenditions\Model\Config;
1614
use PHPUnit\Framework\MockObject\MockObject;
1715
use PHPUnit\Framework\TestCase;
@@ -38,30 +36,11 @@ class ConfigTest extends TestCase
3836
*/
3937
private $config;
4038

41-
/**
42-
* @var AdapterInterface|MockObject
43-
*/
44-
private $connectionMock;
45-
46-
/**
47-
* @var Select|MockObject
48-
*/
49-
private $selectMock;
50-
5139
protected function setUp(): void
5240
{
5341
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
5442
$this->initialConfigMock = $this->createMock(Initial::class);
5543
$this->resourceConnectionMock = $this->createMock(ResourceConnection::class);
56-
$this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
57-
->addMethods(['fetchColumn'])
58-
->getMockForAbstractClass();
59-
$this->selectMock = $this->createMock(Select::class);
60-
$this->resourceConnectionMock->method('getConnection')
61-
->willReturn($this->connectionMock);
62-
$this->resourceConnectionMock->method('getTableName')
63-
->with('core_config_data')
64-
->willReturn('core_config_data');
6544
$this->config = new Config(
6645
$this->scopeConfigMock,
6746
$this->initialConfigMock,
@@ -70,43 +49,29 @@ protected function setUp(): void
7049
}
7150

7251
/**
73-
* Test getWidth() with successful database retrieval
52+
* Test getWidth() using scopeConfig value
7453
*/
75-
public function testGetWidthSuccess(): void
54+
public function testGetWidthFromScopeConfig(): void
7655
{
7756
$expectedWidth = 800;
78-
$this->connectionMock->method('select')
79-
->willReturn($this->selectMock);
80-
$this->selectMock->method('from')
81-
->willReturnSelf();
82-
$this->selectMock->method('where')
83-
->willReturnSelf();
84-
$this->connectionMock->method('query')
85-
->with($this->selectMock)
86-
->willReturnSelf();
87-
$this->connectionMock->method('fetchColumn')
88-
->willReturn((string)$expectedWidth);
57+
$widthPath = 'system/media_gallery_renditions/width';
58+
$this->scopeConfigMock->method('getValue')
59+
->with($widthPath)
60+
->willReturn($expectedWidth);
8961
$result = $this->config->getWidth();
9062
$this->assertEquals($expectedWidth, $result);
9163
}
9264

9365
/**
94-
* Test getWidth() with empty database result falling back to initial config
66+
* Test getWidth() falling back to initial config
9567
*/
96-
public function testGetWidthFallback(): void
68+
public function testGetWidthFromInitialConfig(): void
9769
{
9870
$expectedWidth = 600;
99-
$this->connectionMock->method('select')
100-
->willReturn($this->selectMock);
101-
$this->selectMock->method('from')
102-
->willReturnSelf();
103-
$this->selectMock->method('where')
104-
->willReturnSelf();
105-
$this->connectionMock->method('query')
106-
->with($this->selectMock)
107-
->willReturnSelf();
108-
$this->connectionMock->method('fetchColumn')
109-
->willReturn(false);
71+
$widthPath = 'system/media_gallery_renditions/width';
72+
$this->scopeConfigMock->method('getValue')
73+
->with($widthPath)
74+
->willReturn(null);
11075
$this->initialConfigMock->method('getData')
11176
->with('default')
11277
->willReturn([
@@ -121,42 +86,28 @@ public function testGetWidthFallback(): void
12186
}
12287

12388
/**
124-
* Test getHeight() with successful database retrieval
89+
* Test getHeight() using scopeConfig value
12590
*/
126-
public function testGetHeightSuccess(): void
91+
public function testGetHeightFromScopeConfig(): void
12792
{
12893
$expectedHeight = 600;
129-
$this->connectionMock->method('select')
130-
->willReturn($this->selectMock);
131-
$this->selectMock->method('from')
132-
->willReturnSelf();
133-
$this->selectMock->method('where')
134-
->willReturnSelf();
135-
$this->connectionMock->method('query')
136-
->with($this->selectMock)
137-
->willReturnSelf();
138-
$this->connectionMock->method('fetchColumn')
139-
->willReturn((string)$expectedHeight);
94+
$heightPath = 'system/media_gallery_renditions/height';
95+
$this->scopeConfigMock->method('getValue')
96+
->with($heightPath)
97+
->willReturn($expectedHeight);
14098
$result = $this->config->getHeight();
14199
$this->assertEquals($expectedHeight, $result);
142100
}
143101

144102
/**
145-
* Test getHeight() with empty database result falling back to initial config
103+
* Test getHeight() falling back to initial config
146104
*/
147-
public function testGetHeightFallback(): void
105+
public function testGetHeightFromInitialConfig(): void
148106
{
149107
$expectedHeight = 400;
150-
$this->connectionMock->method('select')
151-
->willReturn($this->selectMock);
152-
$this->selectMock->method('from')
153-
->willReturnSelf();
154-
$this->selectMock->method('where')
155-
->willReturnSelf();
156-
$this->connectionMock->method('query')
157-
->with($this->selectMock)
158-
->willReturnSelf();
159-
$this->connectionMock->method('fetchColumn')
108+
$heightPath = 'system/media_gallery_renditions/height';
109+
$this->scopeConfigMock->method('getValue')
110+
->with($heightPath)
160111
->willReturn(null);
161112
$this->initialConfigMock->method('getData')
162113
->with('default')

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-zero-or-greater 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-zero-or-greater 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)