Version 1
[yaffs-website] / web / core / modules / system / tests / src / Functional / Mail / MailTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Mail;
4
5 use Drupal\Core\Mail\Plugin\Mail\TestMailCollector;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\system_mail_failure_test\Plugin\Mail\TestPhpMailFailure;
8
9 /**
10  * Performs tests on the pluggable mailing framework.
11  *
12  * @group Mail
13  */
14 class MailTest extends BrowserTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['simpletest', 'system_mail_failure_test'];
22
23   /**
24    * Assert that the pluggable mail system is functional.
25    */
26   public function testPluggableFramework() {
27     // Switch mail backends.
28     $this->config('system.mail')->set('interface.default', 'test_php_mail_failure')->save();
29
30     // Get the default MailInterface class instance.
31     $mail_backend = \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'default', 'key' => 'default']);
32
33     // Assert whether the default mail backend is an instance of the expected
34     // class.
35     $this->assertTrue($mail_backend instanceof TestPhpMailFailure, 'Default mail interface can be swapped.');
36
37     // Add a module-specific mail backend.
38     $this->config('system.mail')->set('interface.mymodule_testkey', 'test_mail_collector')->save();
39
40     // Get the added MailInterface class instance.
41     $mail_backend = \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'mymodule', 'key' => 'testkey']);
42
43     // Assert whether the added mail backend is an instance of the expected
44     // class.
45     $this->assertTrue($mail_backend instanceof TestMailCollector, 'Additional mail interfaces can be added.');
46   }
47
48   /**
49    * Test that message sending may be canceled.
50    *
51    * @see simpletest_mail_alter()
52    */
53   public function testCancelMessage() {
54     $language_interface = \Drupal::languageManager()->getCurrentLanguage();
55
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', []);
60
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);
66
67     // Assert that the message was not actually sent.
68     $this->assertFalse($sent_message, 'Message was canceled.');
69   }
70
71   /**
72    * Checks the From: and Reply-to: headers.
73    */
74   public function testFromAndReplyToHeader() {
75     $language = \Drupal::languageManager()->getCurrentLanguage();
76
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.');
92
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.');
100   }
101
102 }