View source
<?php
namespace Drupal\Tests\reroute_email\Functional;
use Drupal\Component\Render\FormattableMarkup;
class UnusualMessageFieldsTest extends RerouteEmailTestBase {
public static $modules = [
'reroute_email',
'reroute_email_test',
'dblog',
];
public function setUp() {
$this->permissions[] = 'access site reports';
parent::setUp();
}
public function testBodyStringRobustHeaders() {
$test_cc_key = 'cC';
$test_bcc_key = 'bCc';
$this
->configureRerouteEmail(TRUE, $this->rerouteDestination);
$test_message = [
'to' => $this->originalDestination,
'params' => [
'body' => 'Test Message body is a string.',
'headers' => [
'test_cc_key' => $test_cc_key,
'test_bcc_key' => $test_bcc_key,
$test_cc_key => 'test_cc_key@example.com',
$test_bcc_key => 'test_bcc_key@example.com',
],
],
];
$langcode = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
\Drupal::getContainer()
->get('plugin.manager.mail')
->mail('reroute_email_test', 'test_reroute_email', $test_message['to'], $langcode, $test_message['params']);
$this
->verbose(new FormattableMarkup('Test email message values: <pre>@test_message</pre>', [
'@test_message' => var_export($test_message, TRUE),
]));
$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();
$this
->assertTrue(strpos($mail['body'], $test_message['params']['body']) !== FALSE, 'Email body contains original message body although it was provided as a string.');
$this
->drupalGet('admin/reports/dblog');
$this
->assertRaw(t('A String was detected in the body'), 'Recorded in recent log messages: a String was detected in the body.');
$this
->assertTrue($mail['headers']['X-Rerouted-Original-Cc'] == $test_message['params']['headers'][$test_cc_key], new FormattableMarkup('X-Rerouted-Original-Cc is correctly set to @test_cc_address, although Cc header message key provided was: @test_cc_key', [
'@test_cc_address' => $test_message['params']['headers'][$test_cc_key],
'@test_cc_key' => $test_cc_key,
]));
$this
->assertTrue($mail['headers']['X-Rerouted-Original-Bcc'] == $test_message['params']['headers'][$test_bcc_key], new FormattableMarkup('X-Rerouted-Original-Bcc is correctly set to @test_bcc_address, although Bcc header message key provided was: @test_bcc_key', [
'@test_bcc_address' => $test_message['params']['headers'][$test_bcc_key],
'@test_bcc_key' => $test_bcc_key,
]));
}
}