Skip to content

Commit

Permalink
Merge pull request #35 from openeuropa/EWPP-772
Browse files Browse the repository at this point in the history
EWPP-772: Remove HTML markup in the contact form emails.
  • Loading branch information
upchuk authored Mar 5, 2021
2 parents ea9c35e + 1d37e28 commit 398a190
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 5 deletions.
8 changes: 7 additions & 1 deletion oe_contact_forms.module
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function oe_contact_forms_form_contact_form_form_alter(&$form, FormStateInterfac
],
];

// Adding checkbox with ajax callback that managed addionals fields.
// Adding checkbox with ajax callback that manages additional fields.
$form['is_corporate_form'] = [
'#type' => 'checkbox',
'#title' => t('Is corporate form'),
Expand Down Expand Up @@ -673,6 +673,12 @@ function oe_contact_forms_mail_alter(&$message) {

// Change subject for corporate mail, copy and reply.
$message['subject'] = $contact_form->getThirdPartySetting('oe_contact_forms', 'email_subject');

// Set back the auto-reply in case contact_storage re-rendered it.
// @see contact_storage_mail_alter.
if ($message['key'] === 'page_autoreply') {
$message['body'] = [$contact_form->getReply()];
}
}

/**
Expand Down
81 changes: 77 additions & 4 deletions tests/src/FunctionalJavascript/MessageFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,7 @@ public function testCorporateForm(): void {
$this->assertTrue($captured_emails[0]['to'] === $topics['0']['topic_email_address']);
// Make sure we have an autoreply.
$this->assertTrue($captured_emails[1]['id'] === 'contact_page_autoreply');
// Assert fields in autoreply.
$this->assertTrue(strpos($captured_emails[1]['body'], 'Test subject') !== FALSE);
$this->assertTrue(strpos($captured_emails[1]['body'], 'Test message') !== FALSE);
$this->assertTrue(strpos($captured_emails[1]['body'], $topics['0']['topic_name']) !== FALSE);
$this->assertTestEmailBodies($captured_emails);

// Assert that instead of the user being redirected to the homepage,
// they are redirected to the same, contact form page.
Expand Down Expand Up @@ -280,4 +277,80 @@ public function testDefaultFormNotChanged(): void {
$this->assertUrl('/');
}

/**
* Asserts the sent emails are correctly formatted.
*
* @param array $captured_emails
* The captured emails that were sent out: main one and auto-reply.
*
* @see self::testCorporateForm
*/
protected function assertTestEmailBodies(array $captured_emails): void {
// First email is the outgoing one.
$expected = <<<EOF
tester (not verified) ([email protected]) sent a message using the contact
form at http://web:8080/build/contact/oe_contact_form.
The sender's name
tester
The sender's email
[email protected]
Subject
Test subject
Message
Test message
Topic
Changed name
EOF;

$this->assertEquals(preg_replace('/\s+/', ' ', $expected), preg_replace('/\s+/', ' ', $captured_emails[0]['body']));

// The second is the autoreply.
$expected = <<<EOF
this is a autoreply
The sender's name
tester
The sender's email
[email protected]
Subject
Test subject
Message
Test message
Topic
Changed name
EOF;

$this->assertEquals(preg_replace('/\s+/', ' ', $expected), preg_replace('/\s+/', ' ', $captured_emails[1]['body']));
}

}

0 comments on commit 398a190

Please sign in to comment.