3 namespace Drupal\Tests\system\Functional\Mail;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Mail\Plugin\Mail\TestMailCollector;
7 use Drupal\Tests\BrowserTestBase;
8 use Drupal\system_mail_failure_test\Plugin\Mail\TestPhpMailFailure;
11 * Performs tests on the pluggable mailing framework.
15 class MailTest extends BrowserTestBase {
22 public static $modules = ['simpletest', 'system_mail_failure_test'];
25 * Assert that the pluggable mail system is functional.
27 public function testPluggableFramework() {
28 // Switch mail backends.
29 $this->config('system.mail')->set('interface.default', 'test_php_mail_failure')->save();
31 // Get the default MailInterface class instance.
32 $mail_backend = \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'default', 'key' => 'default']);
34 // Assert whether the default mail backend is an instance of the expected
36 $this->assertTrue($mail_backend instanceof TestPhpMailFailure, 'Default mail interface can be swapped.');
38 // Add a module-specific mail backend.
39 $this->config('system.mail')->set('interface.mymodule_testkey', 'test_mail_collector')->save();
41 // Get the added MailInterface class instance.
42 $mail_backend = \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'mymodule', 'key' => 'testkey']);
44 // Assert whether the added mail backend is an instance of the expected
46 $this->assertTrue($mail_backend instanceof TestMailCollector, 'Additional mail interfaces can be added.');
50 * Test that message sending may be canceled.
52 * @see simpletest_mail_alter()
54 public function testCancelMessage() {
55 $language_interface = \Drupal::languageManager()->getCurrentLanguage();
57 // Use the state system collector mail backend.
58 $this->config('system.mail')->set('interface.default', 'test_mail_collector')->save();
59 // Reset the state variable that holds sent messages.
60 \Drupal::state()->set('system.test_mail_collector', []);
62 // Send a test message that simpletest_mail_alter should cancel.
63 \Drupal::service('plugin.manager.mail')->mail('simpletest', 'cancel_test', 'cancel@example.com', $language_interface->getId());
64 // Retrieve sent message.
65 $captured_emails = \Drupal::state()->get('system.test_mail_collector');
66 $sent_message = end($captured_emails);
68 // Assert that the message was not actually sent.
69 $this->assertFalse($sent_message, 'Message was canceled.');
73 * Checks the From: and Reply-to: headers.
75 public function testFromAndReplyToHeader() {
76 $language = \Drupal::languageManager()->getCurrentLanguage();
78 // Use the state system collector mail backend.
79 $this->config('system.mail')->set('interface.default', 'test_mail_collector')->save();
80 // Reset the state variable that holds sent messages.
81 \Drupal::state()->set('system.test_mail_collector', []);
82 // Send an email with a reply-to address specified.
83 $from_email = 'Drupal <simpletest@example.com>';
84 $reply_email = 'someone_else@example.com';
85 \Drupal::service('plugin.manager.mail')->mail('simpletest', 'from_test', 'from_test@example.com', $language, [], $reply_email);
86 // Test that the reply-to email is just the email and not the site name
87 // and default sender email.
88 $captured_emails = \Drupal::state()->get('system.test_mail_collector');
89 $sent_message = end($captured_emails);
90 $this->assertEqual($from_email, $sent_message['headers']['From'], 'Message is sent from the site email account.');
91 $this->assertEqual($reply_email, $sent_message['headers']['Reply-to'], 'Message reply-to headers are set.');
92 $this->assertFalse(isset($sent_message['headers']['Errors-To']), 'Errors-to header must not be set, it is deprecated.');
94 // Test that long site names containing characters that need MIME encoding
96 $this->config('system.site')->set('name', 'Drépal this is a very long test sentence to test what happens with very long site names')->save();
97 // Send an email and check that the From-header contains the site name.
98 \Drupal::service('plugin.manager.mail')->mail('simpletest', 'from_test', 'from_test@example.com', $language);
99 $captured_emails = \Drupal::state()->get('system.test_mail_collector');
100 $sent_message = end($captured_emails);
101 $this->assertEquals('=?UTF-8?B?RHLDqXBhbCB0aGlzIGlzIGEgdmVyeSBsb25nIHRlc3Qgc2VudGVuY2UgdG8gdGU=?= <simpletest@example.com>', $sent_message['headers']['From'], 'From header is correctly encoded.');
102 $this->assertEquals('Drépal this is a very long test sentence to te <simpletest@example.com>', Unicode::mimeHeaderDecode($sent_message['headers']['From']), 'From header is correctly encoded.');
103 $this->assertFalse(isset($sent_message['headers']['Reply-to']), 'Message reply-to is not set if not specified.');
104 $this->assertFalse(isset($sent_message['headers']['Errors-To']), 'Errors-to header must not be set, it is deprecated.');