FIX DebugViewFrendlyErrorFormatter handle of admin_email

This commit is contained in:
Serge Latyntcev 2019-10-03 11:31:43 +13:00
parent 26a4fb38ba
commit 7db524bd90
3 changed files with 55 additions and 6 deletions

View File

@ -49,9 +49,10 @@ class Email extends ViewableData
/**
* This will be set in the config on a site-by-site basis
* @see https://docs.silverstripe.org/en/4/developer_guides/email/#administrator-emails
*
* @config
* @var string The default administrator email address.
* @var string|array The default administrator email address or array of [email => name]
*/
private static $admin_email = null;
@ -792,7 +793,7 @@ class Email extends ViewableData
// Do not interfere with emails styles
Requirements::clear();
// Render plain part
if ($plainTemplate && !$plainPart) {
$plainPart = $this->renderWith($plainTemplate, $this->getData());
@ -809,7 +810,7 @@ class Email extends ViewableData
$htmlPartObject = DBField::create_field('HTMLFragment', $htmlPart);
$plainPart = $htmlPartObject->Plain();
}
// Rendering is finished
Requirements::restore();

View File

@ -2,6 +2,7 @@
namespace SilverStripe\Logging;
use SilverStripe\Core\Convert;
use SilverStripe\Dev\Debug;
use SilverStripe\Control\Director;
use SilverStripe\Control\Email\Email;
@ -127,12 +128,36 @@ class DebugViewFriendlyErrorFormatter implements FormatterInterface
$output = $renderer->renderHeader();
$output .= $renderer->renderInfo("Website Error", $this->getTitle(), $this->getBody());
if (Email::config()->admin_email) {
$mailto = Email::obfuscate(Email::config()->admin_email);
$output .= $renderer->renderParagraph('Contact an administrator: ' . $mailto . '');
if (!is_null($contactInfo = $this->addContactAdministratorInfo())) {
$output .= $renderer->renderParagraph($contactInfo);
}
$output .= $renderer->renderFooter();
return $output;
}
/**
* Generate the line with admin contact info
*
* @return string|null
*/
private function addContactAdministratorInfo()
{
if (!$adminEmail = Email::config()->admin_email) {
return null;
}
if (is_string($adminEmail)) {
return 'Contact an administrator: ' . Email::obfuscate($adminEmail);
}
if (!is_array($adminEmail) || !count($adminEmail)) {
return null;
}
$email = array_keys($adminEmail)[0];
$name = array_values($adminEmail)[0];
return sprintf('Contact %s: %s', Convert::raw2xml($name), Email::obfuscate($email));
}
}

View File

@ -29,6 +29,29 @@ The website server has not been able to respond to your request
Contact an administrator: testy [at] mctest [dot] face
TEXT
;
$this->assertEquals($expected, $formatter->output(404));
}
public function testAdminEmailWithName()
{
Email::config()->set('admin_email', ['testy@mctest.face' => 'The ad&min']);
$formatter = new DebugViewFriendlyErrorFormatter();
$formatter->setTitle("There has been an error");
$formatter->setBody("The website server has not been able to respond to your request");
$expected = <<<TEXT
WEBSITE ERROR
There has been an error
-----------------------
The website server has not been able to respond to your request
Contact The ad&amp;min: testy [at] mctest [dot] face
TEXT
;