You are here

public function ContactTest::testBasicNotification in Reroute Email 8

Same name and namespace in other branches
  1. 2.x tests/src/Functional/ContactTest.php \Drupal\Tests\reroute_email\Functional\ContactTest::testBasicNotification()

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

tests/src/Functional/ContactTest.php, line 56

Class

ContactTest
Test ability to reroute mail sent from the Contact module form.

Namespace

Drupal\Tests\reroute_email\Functional

Code

public function testBasicNotification() {

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

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

  // Configure the contact settings to send to $original_destination.
  $this
    ->drupalPostForm('admin/structure/contact/manage/feedback', [
    'recipients' => $this->originalDestination,
  ], t('Save'));

  // Go to the contact page and send an email.
  $post = [
    'subject[0][value]' => 'Test test test',
    'message[0][value]' => 'This is a test',
  ];
  $this
    ->drupalPostForm('contact', $post, 'Send message');
  $this
    ->assertResponse(200, 'Posted contact form successfully.');
  $this
    ->assertText($this->confirmationMessage);

  // Check rerouted email.
  $this
    ->assertMail('to', $this->rerouteDestination, new FormattableMarkup('Email was rerouted to @address.', [
    '@address' => $this->rerouteDestination,
  ]));
  $this
    ->assertEmailOriginallyTo();

  // 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(NULL, $this->rerouteDestination, "{$this->rerouteDestination}, {$additional_destination}");

  // Configure the contact settings to point to the additional recipient.
  $this
    ->drupalPostForm('admin/structure/contact/manage/feedback', [
    'recipients' => $additional_destination,
  ], t('Save'));

  // Go to the contact page and send an email.
  $post = [
    'subject[0][value]' => 'Test test test',
    'message[0][value]' => 'This is a test',
  ];
  $this
    ->drupalPostForm('contact', $post, t('Send message'));
  $this
    ->assertText($this->confirmationMessage);
  $this
    ->assertMail('to', $additional_destination, 'Email was not rerouted because destination was in whitelist.');

  // Now change the configuration to disable reroute and set the default
  // email recipients (from system.site.mail)
  $this
    ->configureRerouteEmail(FALSE);

  // Set the contact form to send to original_destination.
  $this
    ->drupalPostForm('admin/structure/contact/manage/feedback', [
    'recipients' => $this->originalDestination,
  ], t('Save'));

  // Go to the contact page and send an email.
  $post = [
    'subject[0][value]' => 'Test test test',
    'message[0][value]' => 'This is a test',
  ];
  $this
    ->drupalPostForm('contact', $post, t('Send message'));
  $this
    ->assertText($this->confirmationMessage);

  // Mail should not be rerouted - should go to $original_destination.
  $this
    ->assertMail('to', $this->originalDestination, 'Mail not rerouted - sent to original destination.');

  // Configure to reroute without body injection.
  $this
    ->configureRerouteEmail(TRUE, $this->rerouteDestination, '', FALSE);

  // Go to the contact page and send an email.
  $post = [
    'subject[0][value]' => 'Test test test',
    'message[0][value]' => 'This is a test',
  ];
  $this
    ->drupalPostForm('contact', $post, t('Send message'));
  $this
    ->assertText($this->confirmationMessage);
  $mails = $this
    ->getMails();
  $mail = end($mails);

  // 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
    ->assertEqual($mail['headers']['X-Rerouted-Original-To'], $this->originalDestination, 'X-Rerouted-Original-To is correctly set to the original destination email.');
}