View source
<?php
namespace Drupal\Tests\webform\Functional\Handler;
use Drupal\webform\Element\WebformSelectOther;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\Tests\webform\Functional\WebformBrowserTestBase;
class WebformHandlerEmailBasicTest extends WebformBrowserTestBase {
protected static $testWebforms = [
'test_handler_email',
];
public function testBasicEmailHandler() {
$admin_user = $this
->drupalCreateUser([
'administer webform',
]);
$webform = Webform::load('test_handler_email');
$this
->postSubmission($webform);
$sent_email = $this
->getLastEmail();
$this
->assertEqual($sent_email['key'], 'test_handler_email_email');
$this
->assertEqual($sent_email['reply-to'], "John Smith <from@example.com>");
$this
->assertStringContainsString('Submitted by: Anonymous', $sent_email['body']);
$this
->assertStringContainsString('First name: John', $sent_email['body']);
$this
->assertStringContainsString('Last name: Smith', $sent_email['body']);
$this
->assertEqual($sent_email['headers']['From'], 'John Smith <from@example.com>');
$this
->assertEqual($sent_email['headers']['Cc'], 'cc@example.com');
$this
->assertEqual($sent_email['headers']['Bcc'], 'bcc@example.com');
$sent_email = $this
->getLastEmail();
$this
->assertEqual($sent_email['reply-to'], "John Smith <from@example.com>");
$webform
->setSetting('results_disabled', TRUE)
->save();
$this
->postSubmission($webform, [
'first_name' => 'Jane',
'last_name' => 'Doe',
]);
$sent_email = $this
->getLastEmail();
$this
->assertStringContainsString('First name: Jane', $sent_email['body']);
$this
->assertStringContainsString('Last name: Doe', $sent_email['body']);
$webform
->setSetting('results_disabled', FALSE)
->save();
$this
->drupalLogin($admin_user);
$body = implode(PHP_EOL, [
'full name: [webform_submission:values:first_name] [webform_submission:values:last_name]',
'uuid: [webform_submission:uuid]',
'sid: [webform_submission:sid]',
'date: [webform_submission:created]',
'ip-address: [webform_submission:ip-address]',
'user: [webform_submission:user]',
'url: [webform_submission:url]',
'edit-url: [webform_submission:url:edit-form]',
'Test that "double quotes" are not encoded.',
]);
$this
->drupalPostForm('/admin/structure/webform/manage/test_handler_email/handlers/email/edit', [
'settings[body]' => WebformSelectOther::OTHER_OPTION,
'settings[body_custom_text]' => $body,
], 'Save');
$sid = $this
->postSubmission($webform);
$webform_submission = WebformSubmission::load($sid);
$sent_email = $this
->getLastEmail();
$this
->assertStringContainsString('full name: John Smith', $sent_email['body']);
$this
->assertStringContainsString('uuid: ' . $webform_submission->uuid->value, $sent_email['body']);
$this
->assertStringContainsString('sid: ' . $sid, $sent_email['body']);
$date_value = \Drupal::service('date.formatter')
->format($webform_submission->created->value, 'medium');
$this
->assertStringContainsString('date: ' . $date_value, $sent_email['body']);
$this
->assertStringContainsString('ip-address: ' . $webform_submission->remote_addr->value, $sent_email['body']);
$this
->assertStringContainsString('user: ' . $admin_user
->label(), $sent_email['body']);
$this
->assertStringContainsString("url:", $sent_email['body']);
$this
->assertStringContainsString($webform_submission
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString(), $sent_email['body']);
$this
->assertStringContainsString("edit-url:", $sent_email['body']);
$this
->assertStringContainsString($webform_submission
->toUrl('edit-form', [
'absolute' => TRUE,
])
->toString(), $sent_email['body']);
$this
->assertStringContainsString('Test that "double quotes" are not encoded.', $sent_email['body']);
$edit = [
'settings[subject][select]' => '[webform_submission:values:subject:raw]',
'settings[body]' => '_other_',
'settings[body_custom_text]' => '[webform_submission:values][webform_submission:values:message:value]',
];
$this
->drupalPostForm('/admin/structure/webform/manage/test_handler_email/handlers/email/edit', $edit, 'Save');
$edit = [
'first_name' => '"<first_name>"',
'last_name' => '"<last_name>"',
'subject' => 'This has <removed> & "special" \'characters\'',
'message' => 'This has <not_removed> & "special" \'characters\'',
];
$this
->postSubmission($webform, $edit);
$sent_email = $this
->getLastEmail();
$this
->assertEqual($sent_email['reply-to'], '"first_name\\" \\"last_name" <from@example.com>');
$this
->assertEqual($sent_email['subject'], 'This has & "special" \'characters\'');
$this
->assertEqual($sent_email['body'], 'First name: ""
Last name: ""
Email: from@example.com
Subject: This has & "special" \'characters\'
Message:
This has & "special" \'characters\'
This has & "special" \'characters\'
');
$this
->assertEqual($sent_email['params']['body'], 'First name: "<first_name>"
Last name: "<last_name>"
Email: from@example.com
Subject: This has <removed> & "special" \'characters\'
Message:
This has <not_removed> & "special" \'characters\'
This has <not_removed> & "special" \'characters\'');
}
}