View source
<?php
namespace Drupal\Tests\webform\Functional\Handler;
use Drupal\webform\Entity\Webform;
use Drupal\Tests\webform\Functional\WebformBrowserTestBase;
class WebformHandlerEmailAdvancedTest extends WebformBrowserTestBase {
public static $modules = [
'filter',
'file',
'webform',
];
protected static $testWebforms = [
'test_handler_email_advanced',
];
protected function setUp() {
parent::setUp();
$this
->createFilters();
}
public function testAdvancedEmailHandler() {
global $base_url;
$webform = Webform::load('test_handler_email_advanced');
$this
->drupalLogin($this->rootUser);
$this
->postSubmissionTest($webform);
$sent_email = $this
->getLastEmail();
$this
->assertEqual($sent_email['headers']['Return-Path'], 'return_path@example.com');
$this
->assertEqual($sent_email['headers']['Sender'], 'sender_name <sender_mail@example.com>');
$this
->assertEqual($sent_email['headers']['Reply-to'], 'reply_to@example.com');
$this
->assertEqual($sent_email['params']['custom_parameter'], 'test');
$this
->assertArrayNotHasKey('parameters', $sent_email['params']);
$webform
->getHandler('email')
->setSettings([
'reply_to' => '',
'return_path' => '',
'sender_mail' => '',
'sender_name' => '',
]);
$webform
->save();
$this
->postSubmissionTest($webform);
$sent_email = $this
->getLastEmail();
$this
->assertNotEqual($sent_email['headers']['Return-Path'], 'return_path@example.com');
$this
->assertNotEqual($sent_email['headers']['Sender'], 'sender_name <sender_mail@example.com>');
$this
->assertNotEqual($sent_email['headers']['Reply-to'], 'reply_to@example.com');
$this
->assertEqual($sent_email['headers']['Return-Path'], $sent_email['params']['from_mail']);
$this
->assertEqual($sent_email['headers']['Sender'], $sent_email['params']['from_mail']);
$this
->assertEqual($sent_email['headers']['Reply-to'], $sent_email['headers']['From']);
\Drupal::configFactory()
->getEditable('webform.settings')
->set('mail.default_reply_to', 'default_reply_to@example.com')
->set('mail.default_return_path', 'default_return_path@example.com')
->save();
$this
->postSubmissionTest($webform);
$sent_email = $this
->getLastEmail();
$this
->assertEqual($sent_email['headers']['Return-Path'], 'default_return_path@example.com');
$this
->assertEqual($sent_email['headers']['Sender'], 'default_return_path@example.com');
$this
->assertEqual($sent_email['headers']['Reply-to'], 'default_reply_to@example.com');
\Drupal::configFactory()
->getEditable('system.site')
->set('mail', 'system_site@example.com')
->save();
\Drupal::configFactory()
->getEditable('webform.settings')
->set('mail.default_reply_to', '[site:mail]')
->set('mail.default_return_path', '[site:mail]')
->save();
$this
->postSubmissionTest($webform);
$sent_email = $this
->getLastEmail();
$this
->assertEqual($sent_email['headers']['Return-Path'], 'system_site@example.com');
$this
->assertEqual($sent_email['headers']['Sender'], 'system_site@example.com');
$this
->assertEqual($sent_email['headers']['Reply-to'], 'system_site@example.com');
\Drupal::configFactory()
->getEditable('webform.settings')
->set('mail.default_sender_mail', 'default_sender_mail@example.com')
->set('mail.default_sender_name', 'Default Sender Name')
->save();
$this
->postSubmissionTest($webform);
$sent_email = $this
->getLastEmail();
$this
->assertEqual($sent_email['headers']['Sender'], 'Default Sender Name <default_sender_mail@example.com>');
$edit = [
'first_name' => 'John',
'last_name' => 'Smith',
'email' => 'from@example.com',
'subject' => 'This has <removed>"special" \'chararacters\'',
'message[value]' => '<p><em>Please enter a message.</em> Test that double "quotes" are not encoded.</p>',
'checkbox' => FALSE,
];
$this
->postSubmissionTest($webform, $edit);
$sid = $this
->getLastSubmissionId($webform);
$sent_email = $this
->getLastEmail();
$this
->assertEqual($sent_email['subject'], 'This has "special" \'chararacters\'');
$this
->assertStringContainsString('<b>First name</b><br />John<br /><br />', $sent_email['params']['body']);
$this
->assertStringContainsString('<b>Last name</b><br />Smith<br /><br />', $sent_email['params']['body']);
$this
->assertStringContainsString('<b>Email</b><br /><a href="mailto:from@example.com">from@example.com</a><br /><br />', $sent_email['params']['body']);
$this
->assertStringContainsString('<b>Subject</b><br />This has <removed>"special" 'chararacters'<br /><br />', $sent_email['params']['body']);
$this
->assertStringContainsString('<b>Message</b><br /><p><em>Please enter a message.</em> Test that double "quotes" are not encoded.</p><br /><br />', $sent_email['params']['body']);
$this
->assertStringContainsString('<p style="color:yellow"><em>Custom styled HTML markup</em></p>', $sent_email['params']['body']);
$this
->assertStringContainsString('<b>File</b><br />', $sent_email['params']['body']);
$this
->assertStringNotContainsString('<b>Optional</b><br />{Empty}<br /><br />', $sent_email['params']['body']);
$this
->assertStringNotContainsString('<b>Checkbox/b><br />Yes<br /><br />', $sent_email['params']['body']);
$this
->assertEqual($sent_email['params']['attachments'][0]['filecontent'], "this is a sample txt file\nit has two lines\n");
$this
->assertEqual($sent_email['params']['attachments'][0]['filename'], 'file.txt');
$this
->assertEqual($sent_email['params']['attachments'][0]['filemime'], 'text/plain');
$this
->drupalGet("admin/structure/webform/manage/test_handler_email_advanced/submission/{$sid}/resend");
$this
->assertRaw('<strong><a href="' . $base_url . '/system/files/webform/test_handler_email_advanced/6/file.txt">file.txt</a></strong> (text/plain) - 43 bytes');
$this
->drupalPostForm("admin/structure/webform/manage/test_handler_email_advanced/submission/{$sid}/resend", [
'message[body][value]' => 'Testing 123…',
], 'Resend message');
$sent_email = $this
->getLastEmail();
$this
->assertStringNotContainsString('<b>First name</b><br />John<br /><br />', $sent_email['params']['body']);
$this
->debug($sent_email['params']['body']);
$this
->assertEqual($sent_email['params']['body'], 'Testing 123…');
$this
->assertEqual($sent_email['params']['attachments'][0]['filecontent'], "this is a sample txt file\nit has two lines\n");
$this
->assertEqual($sent_email['params']['attachments'][0]['filename'], 'file.txt');
$this
->assertEqual($sent_email['params']['attachments'][0]['filemime'], 'text/plain');
$email_handler = $webform
->getHandler('email');
$email_handler
->setSetting('exclude_attachments', TRUE);
$webform
->save();
$this
->postSubmissionTest($webform);
$sent_email = $this
->getLastEmail();
$this
->assertStringNotContainsString('<b>File</b><br />', $sent_email['params']['body']);
$this
->assertArrayHasKey('filecontent', $sent_email['params']['attachments'][0]);
$email_handler
->setSetting('excluded_elements', [
'file' => 'file',
]);
$webform
->save();
$this
->postSubmissionTest($webform);
$sent_email = $this
->getLastEmail();
$this
->assertStringNotContainsString('<b>File</b><br />', $sent_email['params']['body']);
$this
->assertFalse(isset($sent_email['params']['attachments'][0]['filecontent']));
$this
->postSubmission($webform);
$sent_email = $this
->getLastEmail();
$this
->assertStringNotContainsString('<b>Optional</b><br />{Empty}<br /><br />', $sent_email['params']['body']);
$email_handler
->setSettings([
'exclude_empty' => FALSE,
'exclude_empty_checkbox' => FALSE,
]);
$webform
->save();
$this
->postSubmission($webform);
$sent_email = $this
->getLastEmail();
$this
->assertStringContainsString('<b>Optional</b><br />{Empty}<br /><br />', $sent_email['params']['body']);
$this
->assertStringContainsString('<b>Checkbox</b><br />No<br /><br />', $sent_email['params']['body']);
$this
->drupalLogout();
$this
->postSubmission($webform);
$sent_email = $this
->getLastEmail();
$this
->assertStringContainsString('<b>Notes</b><br />These notes are private.<br /><br />', $sent_email['params']['body']);
$webform
->getHandler('email')
->setSetting('ignore_access', FALSE);
$webform
->save();
$this
->postSubmission($webform);
$sent_email = $this
->getLastEmail();
$this
->assertStringNotContainsString('<b>Notes</b><br />These notes are private.<br /><br />', $sent_email['params']['body']);
}
}