You are here

public function RerouteEmailContactTestCase::testBasicNotification in Reroute Email 7

Basic tests of email rerouting for emails sent through the Contact forms.

The Core Contact email form is submitted several times with different Email Rerouting settings: Rerouting enabled or disabled, Body injection enabled or disabled, several recipients with or without whitelist.

File

./reroute_email.test, line 181
Test the Reroute Email module.

Class

RerouteEmailContactTestCase
Tests email rerouting for the site-wide Core Contact form.

Code

public function testBasicNotification() {

  // Additional destination email address used for testing the whitelist.
  $additional_destination = "additional@example.com";

  // Configure to reroute normally to {$this->rerouteDestination}.
  $this
    ->configureRerouteEmail($this->rerouteDestination);

  // Configure the contact settings to send to $original_destination.
  $this
    ->drupalPost('admin/structure/contact/edit/1', array(
    'recipients' => $this->originalDestination,
  ), t('Save'));

  // Go to the contact page and send an email.
  $post = array(
    'subject' => "Test test test",
    'message' => 'This is a test',
  );
  $this
    ->drupalPost("contact", $post, t("Send message"));
  $this
    ->assertText(t("Your message has been sent"));
  $mails = $this
    ->drupalGetMails();
  $mail = end($mails);

  // Check status message is displayed to let users know email was rerouted.
  $this
    ->assertRaw(t('Submitted email, with ID: <em>@message_id</em>, was rerouted to configured address: <em>@reroute_target</em>. For more details please refer to Reroute Email settings.', array(
    '@message_id' => $mail['id'],
    '@reroute_target' => $this->rerouteDestination,
  )));
  $this
    ->assertMail('to', $this->rerouteDestination, format_string("Email was rerouted to @address", array(
    '@address' => $this->rerouteDestination,
  )));

  // Check if original destination email address is in rerouted email body.
  $this
    ->assertEmailOriginallyTo();
  $this
    ->assertTrue(strpos($mail['body'], 'Originally to') !== FALSE, 'Body does contain "Originally to"');

  // Now try sending to one of the additional email addresses that should
  // not be rerouted. Configure two email addresses in reroute form.
  // Body injection is still turned on.
  $this
    ->configureRerouteEmail($this->rerouteDestination, $additional_destination);

  // Configure the contact settings to point to the additional recipient.
  $this
    ->drupalPost('admin/structure/contact/edit/1', array(
    'recipients' => $additional_destination,
  ), t('Save'));

  // Go to the contact page and send an email.
  $post = array(
    'subject' => "Test test test",
    'message' => 'This is a test',
  );
  $this
    ->drupalPost("contact", $post, t("Send message"));
  $this
    ->assertText(t("Your message has been sent"));
  $this
    ->assertMail('to', $additional_destination, 'Email was not rerouted because destination was in whitelist');

  // Now change the configuration to disable reroute and set the original
  // email recipients.
  $this
    ->configureRerouteEmail(NULL, NULL, FALSE);

  // Set the contact form to send to original_destination.
  $this
    ->drupalPost('admin/structure/contact/edit/1', array(
    'recipients' => $this->originalDestination,
  ), t('Save'));

  // Go to the contact page and send an email.
  $post = array(
    'subject' => "Test test test",
    'message' => 'This is a test',
  );
  $this
    ->drupalPost("contact", $post, t("Send message"));
  $this
    ->assertText(t("Your message has been sent"));
  $mails = $this
    ->drupalGetMails();
  $mail = end($mails);

  // Check status message is not displayed because email was not rerouted.
  $this
    ->assertNoRaw(t('Submitted email, with ID: <em>@message_id</em>, was rerouted to configured address: <em>@reroute_target</em>. For more details please refer to Reroute Email settings.', array(
    '@message_id' => $mail['id'],
    '@reroute_target' => $this->rerouteDestination,
  )));

  // Mail should not be rerouted - should go to $original_destination.
  $this
    ->assertMail('to', $this->originalDestination, 'Mail not rerouted - sent to original destination.');
  $this
    ->verbose(t("Email 'to' was: <pre>@mail_to</pre>", array(
    '@mail_to' => $mail['to'],
  )));

  // Configure to reroute without body injection and without status message.
  $this
    ->configureRerouteEmail(NULL, NULL, TRUE, FALSE, FALSE);

  // Go to the contact page and send an email.
  $post = array(
    'subject' => "Test test test",
    'message' => 'This is a test',
  );
  $this
    ->drupalPost("contact", $post, t("Send message"));
  $this
    ->assertText(t("Your message has been sent"));
  $mails = $this
    ->drupalGetMails();
  $mail = end($mails);

  // Check status message is not displayed because it is disabled.
  $this
    ->assertNoRaw(t('Submitted email, with ID: <em>@message_id</em>, was rerouted to configured address: <em>@reroute_target</em>. For more details please refer to Reroute Email settings.', array(
    '@message_id' => $mail['id'],
    '@reroute_target' => $this->rerouteDestination,
  )));

  // There should be nothing in the body except the contact message - no
  // body injection like 'Originally to'.
  $this
    ->assertTrue(strpos($mail['body'], 'Originally to') === FALSE, 'Body does not contain "Originally to"');
  $this
    ->assertTrue($mail['headers']['X-Rerouted-Original-To'] == $this->originalDestination, 'X-Rerouted-Original-To is correctly set to the original destination email');
}