View source
<?php
namespace Drupal\Tests\reroute_email\Functional;
use Drupal\Component\Render\FormattableMarkup;
class TestEmailFormTest extends RerouteEmailTestBase {
public function testFormTestEmail() {
$this
->configureRerouteEmail(TRUE, $this->rerouteDestination);
$this
->drupalGet('admin/config/development/reroute_email/test');
$this
->assertFieldByName('subject', t('Reroute Email Test'), 'The expected default value was found for the Subject field.');
$post = [
'to' => 'to@example.com',
'cc' => 'cc@example.com',
'bcc' => 'bcc@example.com',
'subject' => 'Test Reroute Email Test Email Form',
'body' => 'Testing email rerouting and the Test Email form',
];
$this
->drupalPostForm('admin/config/development/reroute_email/test', $post, t('Send email'));
$this
->assertText(t('Test email submitted for delivery from test form.'));
$mails = $this
->getMails();
$mail = end($mails);
$this
->assertMail('to', $this->rerouteDestination, new FormattableMarkup('To email address was rerouted to @address.', [
'@address' => $this->rerouteDestination,
]));
$this
->assertEmailOriginallyTo($post['to']);
$this
->assertTrue($mail['headers']['X-Rerouted-Original-Cc'] == $post['cc'], new FormattableMarkup('X-Rerouted-Original-Cc is correctly set to submitted value: @address', [
'@address' => $post['cc'],
]));
$this
->assertTrue($mail['headers']['X-Rerouted-Original-Bcc'] == $post['bcc'], new FormattableMarkup('X-Rerouted-Original-Cc is correctly set to submitted value: @address', [
'@address' => $post['bcc'],
]));
$copy_headers = [
'cc' => t('Originally cc: @cc', [
'@cc' => $mail['headers']['X-Rerouted-Original-Cc'],
]),
'bcc' => t('Originally bcc: @bcc', [
'@bcc' => $mail['headers']['X-Rerouted-Original-Bcc'],
]),
];
foreach ($copy_headers as $header => $message_line) {
$has_header = (bool) preg_match("/{$message_line}/", $mail['body']);
$this
->assertTrue($has_header, new FormattableMarkup('Found the correct "@header" line in the body.', [
'@header' => $header,
]));
}
$this
->assertMail('subject', $post['subject'], new FormattableMarkup('Subject is correctly set to submitted value: @subject', [
'@subject' => $post['subject'],
]));
$this
->assertFalse(strpos($mail['body'], $post['body']) === FALSE, 'Body contains the value submitted through the form.');
$post = [
'to' => 'To address invalid format',
'cc' => 'Cc address invalid format',
'bcc' => 'Bcc address invalid format',
];
$this
->drupalPostForm('admin/config/development/reroute_email/test', $post, t('Send email'));
$this
->assertText(t('Test email submitted for delivery from test form.'));
$this
->assertMail('to', $this->rerouteDestination, new FormattableMarkup('To email address was rerouted to @address.', [
'@address' => $this->rerouteDestination,
]));
$this
->assertEmailOriginallyTo($post['to']);
$mails = $this
->getMails();
$mail = end($mails);
$this
->assertTrue($mail['headers']['X-Rerouted-Original-Cc'] == $post['cc'], new FormattableMarkup('X-Rerouted-Original-Cc is correctly set to submitted value: @address', [
'@address' => $post['cc'],
]));
$this
->assertTrue($mail['headers']['X-Rerouted-Original-Bcc'] == $post['bcc'], new FormattableMarkup('X-Rerouted-Original-Cc is correctly set to submitted value: @address', [
'@address' => $post['bcc'],
]));
$this
->configureRerouteEmail(FALSE);
$this
->drupalPostForm('admin/config/development/reroute_email/test', $post, t('Send email'));
$mails = $this
->getMails();
$mail = end($mails);
$this
->assertMail('to', $post['to'], new FormattableMarkup('To email address is correctly set to submitted value: @address.', [
'@address' => $post['to'],
]));
$this
->verbose(new FormattableMarkup('Sent email values: <pre>@mail</pre>', [
'@mail' => var_export($mail, TRUE),
]));
$this
->assertTrue($mail['headers']['Cc'] == $post['cc'], new FormattableMarkup('Cc is correctly set to submitted value: @address', [
'@address' => $post['cc'],
]));
$this
->assertTrue($mail['headers']['Bcc'] == $post['bcc'], new FormattableMarkup('Bcc is correctly set to submitted value: @address', [
'@address' => $post['bcc'],
]));
}
}