View source
<?php
namespace Drupal\Tests\reroute_email\Functional;
use Drupal\Component\Render\FormattableMarkup;
class ContactTest extends RerouteEmailTestBase {
public static $modules = [
'reroute_email',
'contact',
];
protected $confirmationMessage;
public function setUp() {
$this->permissions[] = 'administer contact forms';
$this->permissions[] = 'access site-wide contact form';
$this->confirmationMessage = 'Your message has been sent.';
parent::setUp();
$this
->drupalPostForm('admin/structure/contact/add', [
'label' => 'feedback',
'id' => 'feedback',
'recipients' => $this->originalDestination,
'message' => $this->confirmationMessage,
'selected' => TRUE,
], 'Save');
$this
->assertResponse(200, 'Contact form named "feedback" added.');
\Drupal::service('config.factory')
->getEditable('contact.settings')
->set('flood.limit', 1000)
->set('flood.interval', 60);
}
public function testBasicNotification() {
$additional_destination = 'additional@example.com';
$this
->configureRerouteEmail(TRUE, $this->rerouteDestination);
$this
->drupalPostForm('admin/structure/contact/manage/feedback', [
'recipients' => $this->originalDestination,
], t('Save'));
$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);
$this
->assertMail('to', $this->rerouteDestination, new FormattableMarkup('Email was rerouted to @address.', [
'@address' => $this->rerouteDestination,
]));
$this
->assertEmailOriginallyTo();
$this
->configureRerouteEmail(NULL, $this->rerouteDestination, "{$this->rerouteDestination}, {$additional_destination}");
$this
->drupalPostForm('admin/structure/contact/manage/feedback', [
'recipients' => $additional_destination,
], t('Save'));
$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.');
$this
->configureRerouteEmail(FALSE);
$this
->drupalPostForm('admin/structure/contact/manage/feedback', [
'recipients' => $this->originalDestination,
], t('Save'));
$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', $this->originalDestination, 'Mail not rerouted - sent to original destination.');
$this
->configureRerouteEmail(TRUE, $this->rerouteDestination, '', FALSE);
$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);
$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.');
}
}