Skip to content

Commit

Permalink
#29315: \Magento\Config\Model\Config\Source\Email\Template::toOptionA…
Browse files Browse the repository at this point in the history
…rray throws error when setPath() is not called first

- Guard against an edge case where toOptionArray is called without having set a path first
  • Loading branch information
guidance-automotan committed Jul 29, 2020
1 parent 89d3cda commit e786d51
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
10 changes: 6 additions & 4 deletions app/code/Magento/Config/Model/Config/Source/Email/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ public function toOptionArray()
$this->_coreRegistry->register('config_system_email_template', $collection);
}
$options = $collection->toOptionArray();
$templateId = str_replace('/', '_', $this->getPath());
$templateLabel = $this->_emailConfig->getTemplateLabel($templateId);
$templateLabel = __('%1 (Default)', $templateLabel);
array_unshift($options, ['value' => $templateId, 'label' => $templateLabel]);
if (!empty($this->getPath())) {
$templateId = str_replace('/', '_', $this->getPath());
$templateLabel = $this->_emailConfig->getTemplateLabel($templateId);
$templateLabel = __('%1 (Default)', $templateLabel);
array_unshift($options, ['value' => $templateId, 'label' => $templateLabel]);
}
return $options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,48 @@ public function testToOptionArray()
$this->_model->setPath('template/new');
$this->assertEquals($expectedResult, $this->_model->toOptionArray());
}
}

public function testToOptionArrayWithoutPath()
{
$collection = $this->createMock(Collection::class);
$collection->expects(
$this->once()
)->method(
'toOptionArray'
)->willReturn(
[
['value' => 'template_one', 'label' => 'Template One'],
['value' => 'template_two', 'label' => 'Template Two'],
]
);

$this->_coreRegistry->expects(
$this->once()
)->method(
'registry'
)->with(
'config_system_email_template'
)->willReturn(
$collection
);

$this->_emailConfig->expects(
$this->never()
)->method(
'getTemplateLabel'
)->with('')
->willThrowException(new \UnexpectedValueException("Email template '' is not defined."));

$expectedResult = [
[
'value' => 'template_one',
'label' => 'Template One',
],
[
'value' => 'template_two',
'label' => 'Template Two',
],
];

$this->assertEquals($expectedResult, $this->_model->toOptionArray());
}}

0 comments on commit e786d51

Please sign in to comment.