Issue #2349725 by AdamPS, alexpott, hexblot, jonathanshaw, Berdir, larowlan, andypost: Core Mail class prints non-overridable errors on the page

merge-requests/1119/head
Alex Pott 2019-05-30 12:17:14 +01:00
parent 8425a0918c
commit 5614f82871
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
3 changed files with 29 additions and 3 deletions

View File

@ -202,7 +202,9 @@ class MailManager extends DefaultPluginManager implements MailManagerInterface {
* @param string $langcode * @param string $langcode
* Language code to use to compose the email. * Language code to use to compose the email.
* @param array $params * @param array $params
* (optional) Parameters to build the email. * (optional) Parameters to build the email. Use the key '_error_message'
* to provide translatable markup to display as a message if an error
* occurs, or set this to false to disable error display.
* @param string|null $reply * @param string|null $reply
* Optional email address to be used to answer. * Optional email address to be used to answer.
* @param bool $send * @param bool $send
@ -311,7 +313,10 @@ class MailManager extends DefaultPluginManager implements MailManagerInterface {
'%to' => $message['to'], '%to' => $message['to'],
'%reply' => $message['reply-to'] ? $message['reply-to'] : $this->t('not set'), '%reply' => $message['reply-to'] ? $message['reply-to'] : $this->t('not set'),
]); ]);
$this->messenger()->addError($this->t('Unable to send email. Contact the site administrator if the problem persists.')); $error_message = $params['_error_message'] ?? $this->t('Unable to send email. Contact the site administrator if the problem persists.');
if ($error_message) {
$this->messenger()->addError($error_message);
}
} }
} }
} }

View File

@ -102,7 +102,9 @@ interface MailManagerInterface extends PluginManagerInterface {
* @param string $langcode * @param string $langcode
* Language code to use to compose the email. * Language code to use to compose the email.
* @param array $params * @param array $params
* (optional) Parameters to build the email. * (optional) Parameters to build the email. Use the key '_error_message'
* to provide translatable markup to display as a message if an error
* occurs, or set this to false to disable error display.
* @param string|null $reply * @param string|null $reply
* Optional email address to be used to answer. * Optional email address to be used to answer.
* @param bool $send * @param bool $send

View File

@ -6,6 +6,7 @@ use Drupal\Component\Utility\Random;
use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\Unicode;
use Drupal\Core\Mail\MailFormatHelper; use Drupal\Core\Mail\MailFormatHelper;
use Drupal\Core\Mail\Plugin\Mail\TestMailCollector; use Drupal\Core\Mail\Plugin\Mail\TestMailCollector;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\Markup; use Drupal\Core\Render\Markup;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\file\Entity\File; use Drupal\file\Entity\File;
@ -51,6 +52,24 @@ class MailTest extends BrowserTestBase {
$this->assertTrue($mail_backend instanceof TestMailCollector, 'Additional mail interfaces can be added.'); $this->assertTrue($mail_backend instanceof TestMailCollector, 'Additional mail interfaces can be added.');
} }
/**
* Assert that the pluggable mail system is functional.
*/
public function testErrorMessageDisplay() {
// Switch mail backends.
$this->config('system.mail')->set('interface.default', 'test_php_mail_failure')->save();
// Test with errors displayed to users.
\Drupal::service('plugin.manager.mail')->mail('default', 'default', 'test@example.com', 'en');
$messages = \Drupal::messenger()->messagesByType(MessengerInterface::TYPE_ERROR);
$this->assertEquals('Unable to send email. Contact the site administrator if the problem persists.', $messages[0]);
\Drupal::messenger()->deleteAll();
// Test without errors displayed to users.
\Drupal::service('plugin.manager.mail')->mail('default', 'default', 'test@example.com', 'en', ['_error_message' => '']);
$this->assertEmpty(\Drupal::messenger()->messagesByType(MessengerInterface::TYPE_ERROR));
}
/** /**
* Test that message sending may be canceled. * Test that message sending may be canceled.
* *