Skip to content

Commit e786d51

Browse files
#29315: \Magento\Config\Model\Config\Source\Email\Template::toOptionArray throws error when setPath() is not called first
- Guard against an edge case where toOptionArray is called without having set a path first
1 parent 89d3cda commit e786d51

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

app/code/Magento/Config/Model/Config/Source/Email/Template.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ public function toOptionArray()
6060
$this->_coreRegistry->register('config_system_email_template', $collection);
6161
}
6262
$options = $collection->toOptionArray();
63-
$templateId = str_replace('/', '_', $this->getPath());
64-
$templateLabel = $this->_emailConfig->getTemplateLabel($templateId);
65-
$templateLabel = __('%1 (Default)', $templateLabel);
66-
array_unshift($options, ['value' => $templateId, 'label' => $templateLabel]);
63+
if (!empty($this->getPath())) {
64+
$templateId = str_replace('/', '_', $this->getPath());
65+
$templateLabel = $this->_emailConfig->getTemplateLabel($templateId);
66+
$templateLabel = __('%1 (Default)', $templateLabel);
67+
array_unshift($options, ['value' => $templateId, 'label' => $templateLabel]);
68+
}
6769
return $options;
6870
}
6971
}

app/code/Magento/Config/Test/Unit/Model/Config/Source/Email/TemplateTest.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,48 @@ public function testToOptionArray()
102102
$this->_model->setPath('template/new');
103103
$this->assertEquals($expectedResult, $this->_model->toOptionArray());
104104
}
105-
}
105+
106+
public function testToOptionArrayWithoutPath()
107+
{
108+
$collection = $this->createMock(Collection::class);
109+
$collection->expects(
110+
$this->once()
111+
)->method(
112+
'toOptionArray'
113+
)->willReturn(
114+
[
115+
['value' => 'template_one', 'label' => 'Template One'],
116+
['value' => 'template_two', 'label' => 'Template Two'],
117+
]
118+
);
119+
120+
$this->_coreRegistry->expects(
121+
$this->once()
122+
)->method(
123+
'registry'
124+
)->with(
125+
'config_system_email_template'
126+
)->willReturn(
127+
$collection
128+
);
129+
130+
$this->_emailConfig->expects(
131+
$this->never()
132+
)->method(
133+
'getTemplateLabel'
134+
)->with('')
135+
->willThrowException(new \UnexpectedValueException("Email template '' is not defined."));
136+
137+
$expectedResult = [
138+
[
139+
'value' => 'template_one',
140+
'label' => 'Template One',
141+
],
142+
[
143+
'value' => 'template_two',
144+
'label' => 'Template Two',
145+
],
146+
];
147+
148+
$this->assertEquals($expectedResult, $this->_model->toOptionArray());
149+
}}

0 commit comments

Comments
 (0)