diff --git a/core/modules/system/lib/Drupal/system/Tests/Mail/MailTest.php b/core/modules/system/lib/Drupal/system/Tests/Mail/MailTest.php index 927b7b1..e84bca3 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Mail/MailTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Mail/MailTest.php @@ -8,20 +8,19 @@ namespace Drupal\system\Tests\Mail; use Drupal\Core\Language\Language; -use Drupal\Core\Mail\MailInterface; use Drupal\simpletest\WebTestBase; /** * Defines a mail class used for testing. */ -class MailTest extends WebTestBase implements MailInterface { +class MailTest extends WebTestBase { /** * Modules to enable. * * @var array */ - public static $modules = array('simpletest'); + public static $modules = array('simpletest', 'system_mail_failure_test'); /** * The most recent message that was sent through the test case. @@ -39,24 +38,18 @@ public static function getInfo() { ); } - function setUp() { - parent::setUp(); - - // Set MailTestCase (i.e. this class) as the SMTP library - \Drupal::config('system.mail')->set('interface.default', 'Drupal\system\Tests\Mail\MailTest')->save(); - } - /** * Assert that the pluggable mail system is functional. */ public function testPluggableFramework() { - $language_interface = language(Language::TYPE_INTERFACE); + // Switch mail backends. + \Drupal::config('system.mail')->set('interface.default', 'TestPhpMailFailure')->save(); - // Use MailTestCase for sending a message. - drupal_mail('simpletest', 'mail_test', 'testing@example.com', $language_interface->id); + // Get the default MailInterface class instance. + $mail_backend = drupal_mail_system('default', 'default'); - // Assert whether the message was sent through the send function. - $this->assertEqual(self::$sent_message['to'], 'testing@example.com', 'Pluggable mail system is extendable.'); + // Assert whether the default mail backend is an instance of the expected class. + $this->assertTrue($mail_backend instanceof \Drupal\system_mail_failure_test\Plugin\Mail\TestPhpMailFailure, 'Pluggable mail system is extendable.'); } /** @@ -67,14 +60,19 @@ public function testPluggableFramework() { public function testCancelMessage() { $language_interface = language(Language::TYPE_INTERFACE); - // Reset the class variable holding a copy of the last sent message. - self::$sent_message = NULL; + // Use the state system collector mail backend. + \Drupal::config('system.mail')->set('interface.default', 'TestMailCollector')->save(); + // Reset the state variable that holds sent messages. + \Drupal::state()->set('system.test_mail_collector', array()); // Send a test message that simpletest_mail_alter should cancel. drupal_mail('simpletest', 'cancel_test', 'cancel@example.com', $language_interface->id); + // Retrieve sent message. + $captured_emails = \Drupal::state()->get('system.test_mail_collector'); + $sent_message = end($captured_emails); // Assert that the message was not actually sent. - $this->assertNull(self::$sent_message, 'Message was canceled.'); + $this->assertFalse($sent_message, 'Message was canceled.'); } /** @@ -83,45 +81,28 @@ public function testCancelMessage() { public function testFromAndReplyToHeader() { global $language; - // Reset the class variable holding a copy of the last sent message. - self::$sent_message = NULL; + // Use the state system collector mail backend. + \Drupal::config('system.mail')->set('interface.default', 'TestMailCollector')->save(); + // Reset the state variable that holds sent messages. + \Drupal::state()->set('system.test_mail_collector', array()); // Send an e-mail with a reply-to address specified. $from_email = 'Drupal '; $reply_email = 'someone_else@example.com'; drupal_mail('simpletest', 'from_test', 'from_test@example.com', $language, array(), $reply_email); // Test that the reply-to e-mail is just the e-mail and not the site name and // default sender e-mail. - $this->assertEqual($from_email, self::$sent_message['headers']['From'], 'Message is sent from the site email account.'); - $this->assertEqual($reply_email, self::$sent_message['headers']['Reply-to'], 'Message reply-to headers are set.'); - $this->assertFalse(isset(self::$sent_message['headers']['Errors-To']), 'Errors-to header must not be set, it is deprecated.'); + $captured_emails = \Drupal::state()->get('system.test_mail_collector'); + $sent_message = end($captured_emails); + $this->assertEqual($from_email, $sent_message['headers']['From'], 'Message is sent from the site email account.'); + $this->assertEqual($reply_email, $sent_message['headers']['Reply-to'], 'Message reply-to headers are set.'); + $this->assertFalse(isset($sent_message['headers']['Errors-To']), 'Errors-to header must not be set, it is deprecated.'); - self::$sent_message = NULL; // Send an e-mail and check that the From-header contains the site name. drupal_mail('simpletest', 'from_test', 'from_test@example.com', $language); - $this->assertEqual($from_email, self::$sent_message['headers']['From'], 'Message is sent from the site email account.'); - $this->assertFalse(isset(self::$sent_message['headers']['Reply-to']), 'Message reply-to is not set if not specified.'); - $this->assertFalse(isset(self::$sent_message['headers']['Errors-To']), 'Errors-to header must not be set, it is deprecated.'); - } - - /** - * Concatenate and wrap the e-mail body for plain-text mails. - * - * @see \Drupal\Core\Mail\PhpMail - */ - public function format(array $message) { - // Join the body array into one string. - $message['body'] = implode("\n\n", $message['body']); - // Convert any HTML to plain-text. - $message['body'] = drupal_html_to_text($message['body']); - // Wrap the mail body for sending. - $message['body'] = drupal_wrap_mail($message['body']); - return $message; - } - - /** - * Send function that is called through the mail system. - */ - public function mail(array $message) { - self::$sent_message = $message; + $captured_emails = \Drupal::state()->get('system.test_mail_collector'); + $sent_message = end($captured_emails); + $this->assertEqual($from_email, $sent_message['headers']['From'], 'Message is sent from the site email account.'); + $this->assertFalse(isset($sent_message['headers']['Reply-to']), 'Message reply-to is not set if not specified.'); + $this->assertFalse(isset($sent_message['headers']['Errors-To']), 'Errors-to header must not be set, it is deprecated.'); } } diff --git a/core/modules/user/lib/Drupal/user/Tests/UserCreateFailMailTest.php b/core/modules/user/lib/Drupal/user/Tests/UserCreateFailMailTest.php index abe6aa9..128576c 100644 --- a/core/modules/user/lib/Drupal/user/Tests/UserCreateFailMailTest.php +++ b/core/modules/user/lib/Drupal/user/Tests/UserCreateFailMailTest.php @@ -19,7 +19,7 @@ class UserCreateFailMailTest extends WebTestBase { * * @var array */ - public static $modules = array('system_mail_test'); + public static $modules = array('system_mail_failure_test'); public static function getInfo() { return array(