Skip to content

Commit db1cba1

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

File tree

2 files changed

+191
-5
lines changed

2 files changed

+191
-5
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
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;
1213
use Magento\Framework\App\ResourceConnection;
1314
use Magento\Framework\Exception\NoSuchEntityException;
@@ -28,20 +29,28 @@ class Config
2829
*/
2930
private $scopeConfig;
3031

32+
/**
33+
* @var Initial
34+
*/
35+
private $initialConfig;
36+
3137
/**
3238
* @var ResourceConnection
3339
*/
3440
private $resourceConnection;
3541

3642
/**
3743
* @param ScopeConfigInterface $scopeConfig
44+
* @param Initial $initialConfig
3845
* @param ResourceConnection $resourceConnection
3946
*/
4047
public function __construct(
4148
ScopeConfigInterface $scopeConfig,
49+
Initial $initialConfig,
4250
ResourceConnection $resourceConnection
4351
) {
4452
$this->scopeConfig = $scopeConfig;
53+
$this->initialConfig = $initialConfig;
4554
$this->resourceConnection = $resourceConnection;
4655
}
4756

@@ -75,7 +84,9 @@ public function getWidth(): int
7584
try {
7685
return $this->getDatabaseValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_WIDTH_PATH);
7786
} catch (NoSuchEntityException $exception) {
78-
return (int) $this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_WIDTH_PATH);
87+
$configData = $this->initialConfig->getData('default');
88+
$width = $configData['system']['media_gallery_renditions']['width'] ?? 0;
89+
return (int) $width;
7990
}
8091
}
8192

@@ -89,7 +100,9 @@ public function getHeight(): int
89100
try {
90101
return $this->getDatabaseValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_HEIGHT_PATH);
91102
} catch (NoSuchEntityException $exception) {
92-
return (int) $this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_HEIGHT_PATH);
103+
$configData = $this->initialConfig->getData('default');
104+
$height = $configData['system']['media_gallery_renditions']['height'] ?? 0;
105+
return (int) $height;
93106
}
94107
}
95108

@@ -115,7 +128,7 @@ private function getDatabaseValue(string $path): int
115128
->where('config.path = ?', $path);
116129
$value = $connection->query($select)->fetchColumn();
117130

118-
if ($value === false) {
131+
if ($value === false || $value === null) {
119132
throw new NoSuchEntityException(
120133
__(
121134
'The config value for %path is not saved to database.',
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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\Framework\App\ResourceConnection;
13+
use Magento\Framework\DB\Adapter\AdapterInterface;
14+
use Magento\Framework\DB\Select;
15+
use Magento\MediaGalleryRenditions\Model\Config;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class ConfigTest extends TestCase
20+
{
21+
/**
22+
* @var ScopeConfigInterface|MockObject
23+
*/
24+
private $scopeConfigMock;
25+
26+
/**
27+
* @var Initial|MockObject
28+
*/
29+
private $initialConfigMock;
30+
31+
/**
32+
* @var ResourceConnection|MockObject
33+
*/
34+
private $resourceConnectionMock;
35+
36+
/**
37+
* @var Config
38+
*/
39+
private $config;
40+
41+
/**
42+
* @var AdapterInterface|MockObject
43+
*/
44+
private $connectionMock;
45+
46+
/**
47+
* @var Select|MockObject
48+
*/
49+
private $selectMock;
50+
51+
protected function setUp(): void
52+
{
53+
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
54+
$this->initialConfigMock = $this->createMock(Initial::class);
55+
$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');
65+
$this->config = new Config(
66+
$this->scopeConfigMock,
67+
$this->initialConfigMock,
68+
$this->resourceConnectionMock
69+
);
70+
}
71+
72+
/**
73+
* Test getWidth() with successful database retrieval
74+
*/
75+
public function testGetWidthSuccess(): void
76+
{
77+
$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);
89+
$result = $this->config->getWidth();
90+
$this->assertEquals($expectedWidth, $result);
91+
}
92+
93+
/**
94+
* Test getWidth() with empty database result falling back to initial config
95+
*/
96+
public function testGetWidthFallback(): void
97+
{
98+
$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);
110+
$this->initialConfigMock->method('getData')
111+
->with('default')
112+
->willReturn([
113+
'system' => [
114+
'media_gallery_renditions' => [
115+
'width' => $expectedWidth
116+
]
117+
]
118+
]);
119+
$result = $this->config->getWidth();
120+
$this->assertEquals($expectedWidth, $result);
121+
}
122+
123+
/**
124+
* Test getHeight() with successful database retrieval
125+
*/
126+
public function testGetHeightSuccess(): void
127+
{
128+
$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);
140+
$result = $this->config->getHeight();
141+
$this->assertEquals($expectedHeight, $result);
142+
}
143+
144+
/**
145+
* Test getHeight() with empty database result falling back to initial config
146+
*/
147+
public function testGetHeightFallback(): void
148+
{
149+
$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')
160+
->willReturn(null);
161+
$this->initialConfigMock->method('getData')
162+
->with('default')
163+
->willReturn([
164+
'system' => [
165+
'media_gallery_renditions' => [
166+
'height' => $expectedHeight
167+
]
168+
]
169+
]);
170+
$result = $this->config->getHeight();
171+
$this->assertEquals($expectedHeight, $result);
172+
}
173+
}

0 commit comments

Comments
 (0)