You are here

public function MailTest::testFromAndReplyToHeader in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Mail/MailTest.php \Drupal\system\Tests\Mail\MailTest::testFromAndReplyToHeader()

Checks the From: and Reply-to: headers.

File

core/modules/system/src/Tests/Mail/MailTest.php, line 79
Contains \Drupal\system\Tests\Mail\MailTest.

Class

MailTest
Performs tests on the pluggable mailing framework.

Namespace

Drupal\system\Tests\Mail

Code

public function testFromAndReplyToHeader() {
  $language = \Drupal::languageManager()
    ->getCurrentLanguage();

  // Use the state system collector mail backend.
  $this
    ->config('system.mail')
    ->set('interface.default', 'test_mail_collector')
    ->save();

  // Reset the state variable that holds sent messages.
  \Drupal::state()
    ->set('system.test_mail_collector', array());

  // Send an email with a reply-to address specified.
  $from_email = 'Drupal <simpletest@example.com>';
  $reply_email = 'someone_else@example.com';
  \Drupal::service('plugin.manager.mail')
    ->mail('simpletest', 'from_test', 'from_test@example.com', $language, array(), $reply_email);

  // Test that the reply-to email is just the email and not the site name
  // and default sender email.
  $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.');

  // Send an email and check that the From-header contains the site name.
  \Drupal::service('plugin.manager.mail')
    ->mail('simpletest', 'from_test', 'from_test@example.com', $language);
  $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.');
}