3 namespace Drupal\Tests\system\Functional\Mail;
5 use Drupal\Core\Mail\Plugin\Mail\TestMailCollector;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\system_mail_failure_test\Plugin\Mail\TestPhpMailFailure;
10 * Performs tests on the pluggable mailing framework.
14 class MailTest extends BrowserTestBase {
21 public static $modules = ['simpletest', 'system_mail_failure_test'];
24 * Assert that the pluggable mail system is functional.
26 public function testPluggableFramework() {
27 // Switch mail backends.
28 $this->config('system.mail')->set('interface.default', 'test_php_mail_failure')->save();
30 // Get the default MailInterface class instance.
31 $mail_backend = \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'default', 'key' => 'default']);
33 // Assert whether the default mail backend is an instance of the expected
35 $this->assertTrue($mail_backend instanceof TestPhpMailFailure, 'Default mail interface can be swapped.');
37 // Add a module-specific mail backend.
38 $this->config('system.mail')->set('interface.mymodule_testkey', 'test_mail_collector')->save();
40 // Get the added MailInterface class instance.
41 $mail_backend = \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'mymodule', 'key' => 'testkey']);
43 // Assert whether the added mail backend is an instance of the expected
45 $this->assertTrue($mail_backend instanceof TestMailCollector, 'Additional mail interfaces can be added.');
49 * Test that message sending may be canceled.
51 * @see simpletest_mail_alter()
53 public function testCancelMessage() {
54 $language_interface = \Drupal::languageManager()->getCurrentLanguage();
56 // Use the state system collector mail backend.
57 $this->config('system.mail')->set('interface.default', 'test_mail_collector')->save();
58 // Reset the state variable that holds sent messages.
59 \Drupal::state()->set('system.test_mail_collector', []);
61 // Send a test message that simpletest_mail_alter should cancel.
62 \Drupal::service('plugin.manager.mail')->mail('simpletest', 'cancel_test', 'cancel@example.com', $language_interface->getId());
63 // Retrieve sent message.
64 $captured_emails = \Drupal::state()->get('system.test_mail_collector');
65 $sent_message = end($captured_emails);
67 // Assert that the message was not actually sent.
68 $this->assertFalse($sent_message, 'Message was canceled.');
72 * Checks the From: and Reply-to: headers.
74 public function testFromAndReplyToHeader() {
75 $language = \Drupal::languageManager()->getCurrentLanguage();
77 // Use the state system collector mail backend.
78 $this->config('system.mail')->set('interface.default', 'test_mail_collector')->save();
79 // Reset the state variable that holds sent messages.
80 \Drupal::state()->set('system.test_mail_collector', []);
81 // Send an email with a reply-to address specified.
82 $from_email = 'Drupal <simpletest@example.com>';
83 $reply_email = 'someone_else@example.com';
84 \Drupal::service('plugin.manager.mail')->mail('simpletest', 'from_test', 'from_test@example.com', $language, [], $reply_email);
85 // Test that the reply-to email is just the email and not the site name
86 // and default sender email.
87 $captured_emails = \Drupal::state()->get('system.test_mail_collector');
88 $sent_message = end($captured_emails);
89 $this->assertEqual($from_email, $sent_message['headers']['From'], 'Message is sent from the site email account.');
90 $this->assertEqual($reply_email, $sent_message['headers']['Reply-to'], 'Message reply-to headers are set.');
91 $this->assertFalse(isset($sent_message['headers']['Errors-To']), 'Errors-to header must not be set, it is deprecated.');
93 // Send an email and check that the From-header contains the site name.
94 \Drupal::service('plugin.manager.mail')->mail('simpletest', 'from_test', 'from_test@example.com', $language);
95 $captured_emails = \Drupal::state()->get('system.test_mail_collector');
96 $sent_message = end($captured_emails);
97 $this->assertEqual($from_email, $sent_message['headers']['From'], 'Message is sent from the site email account.');
98 $this->assertFalse(isset($sent_message['headers']['Reply-to']), 'Message reply-to is not set if not specified.');
99 $this->assertFalse(isset($sent_message['headers']['Errors-To']), 'Errors-to header must not be set, it is deprecated.');