Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace Drupal\Tests\reroute_email\Functional;
use Drupal\Component\Render\FormattableMarkup;
/**
* Test ability to reroute mail sent from the Contact module form.
*
* @ingroup reroute_email_tests
*
* @group reroute_email
*/
class ContactTest extends RerouteEmailTestBase {
public static $modules = ['reroute_email', 'contact'];
protected $confirmationMessage;
/**
* Enable modules and create user with specific permissions.
*/
public function setUp() {
// Add more permissions to be able to manipulate the contact forms.
$this->permissions[] = 'administer contact forms';
$this->permissions[] = 'access site-wide contact form';
$this->confirmationMessage = 'Your message has been sent.';
parent::setUp();
// Create a "feedback" contact form. Note that the 'message' was added in
// the 8.2.x series, and is not there in 8.1.x, so this could fail in 8.1.x.
$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.');
// Make sure that the flood controls don't break the test.
\Drupal::service('config.factory')->getEditable('contact.settings')
->set('flood.limit', 1000)
->set('flood.interval', 60);
}
/**
* 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.
*/
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.');
}
}