e8accddb6f4e2f2bd6fbec57e6f082e1a3c4e7f0
[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\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;
9
10 /**
11  * Performs tests on the pluggable mailing framework.
12  *
13  * @group Mail
14  */
15 class MailTest extends BrowserTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['simpletest', 'system_mail_failure_test'];
23
24   /**
25    * Assert that the pluggable mail system is functional.
26    */
27   public function testPluggableFramework() {
28     // Switch mail backends.
29     $this->config('system.mail')->set('interface.default', 'test_php_mail_failure')->save();
30
31     // Get the default MailInterface class instance.
32     $mail_backend = \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'default', 'key' => 'default']);
33
34     // Assert whether the default mail backend is an instance of the expected
35     // class.
36     $this->assertTrue($mail_backend instanceof TestPhpMailFailure, 'Default mail interface can be swapped.');
37
38     // Add a module-specific mail backend.
39     $this->config('system.mail')->set('interface.mymodule_testkey', 'test_mail_collector')->save();
40
41     // Get the added MailInterface class instance.
42     $mail_backend = \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'mymodule', 'key' => 'testkey']);
43
44     // Assert whether the added mail backend is an instance of the expected
45     // class.
46     $this->assertTrue($mail_backend instanceof TestMailCollector, 'Additional mail interfaces can be added.');
47   }
48
49   /**
50    * Test that message sending may be canceled.
51    *
52    * @see simpletest_mail_alter()
53    */
54   public function testCancelMessage() {
55     $language_interface = \Drupal::languageManager()->getCurrentLanguage();
56
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', []);
61
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);
67
68     // Assert that the message was not actually sent.
69     $this->assertFalse($sent_message, 'Message was canceled.');
70   }
71
72   /**
73    * Checks the From: and Reply-to: headers.
74    */
75   public function testFromAndReplyToHeader() {
76     $language = \Drupal::languageManager()->getCurrentLanguage();
77
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.');
93
94     // Test that long site names containing characters that need MIME encoding
95     // works as expected.
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.');
105   }
106
107 }