You are here

public function YamlFormHandlerEmailBasicTest::testBasicEmailHandler in YAML Form 8

Test basic email handler.

File

src/Tests/YamlFormHandlerEmailBasicTest.php, line 19

Class

YamlFormHandlerEmailBasicTest
Tests for basic email form handler functionality.

Namespace

Drupal\yamlform\Tests

Code

public function testBasicEmailHandler() {

  /** @var \Drupal\yamlform\YamlFormInterface $yamlform_handler_email */
  $yamlform_handler_email = YamlForm::load('test_handler_email');

  // Create a submission using the test form's default values.
  $this
    ->drupalLogout();
  $this
    ->postSubmission($yamlform_handler_email);

  // Check sending a basic email via a submission.
  $sent_email = $this
    ->getLastEmail();
  $this
    ->assertEqual($sent_email['reply-to'], "from@example.com <John Smith>");
  $this
    ->assertContains($sent_email['body'], 'Submitted by: Anonymous');
  $this
    ->assertContains($sent_email['body'], 'First name: John');
  $this
    ->assertContains($sent_email['body'], 'Last name: Smith');
  $this
    ->assertEqual($sent_email['headers']['From'], 'from@example.com');
  $this
    ->assertEqual($sent_email['headers']['Cc'], 'cc@example.com');
  $this
    ->assertEqual($sent_email['headers']['Bcc'], 'bcc@example.com');

  // Check sending with the saving of results disabled.
  $yamlform_handler_email
    ->setSetting('results_disabled', TRUE)
    ->save();
  $this
    ->postSubmission($yamlform_handler_email, [
    'first_name' => 'Jane',
    'last_name' => 'Doe',
  ]);
  $sent_email = $this
    ->getLastEmail();
  $this
    ->assertContains($sent_email['body'], 'First name: Jane');
  $this
    ->assertContains($sent_email['body'], 'Last name: Doe');
  $yamlform_handler_email
    ->setSetting('results_disabled', FALSE)
    ->save();

  // Check sending a custom email using tokens.
  $this
    ->drupalLogin($this->adminFormUser);
  $body = implode("\n", [
    'full name: [yamlform_submission:values:first_name] [yamlform_submission:values:last_name]',
    'uuid: [yamlform_submission:uuid]',
    'sid: [yamlform_submission:sid]',
    'date: [yamlform_submission:created]',
    'ip-address: [yamlform_submission:ip-address]',
    'user: [yamlform_submission:user]',
    'url: [yamlform_submission:url]',
    'edit-url: [yamlform_submission:url:edit-form]',
    'Test that "double quotes" are not encoded.',
  ]);
  $this
    ->drupalPostForm('admin/structure/yamlform/manage/test_handler_email/handlers/email/edit', [
    'settings[body]' => YamlFormSelectOther::OTHER_OPTION,
    'settings[body_custom_text]' => $body,
  ], t('Save'));
  $sid = $this
    ->postSubmission($yamlform_handler_email);

  /** @var \Drupal\yamlform\YamlFormSubmissionInterface $yamlform_submission */
  $yamlform_submission = YamlFormSubmission::load($sid);
  $sent_email = $this
    ->getLastEmail();
  $this
    ->assertContains($sent_email['body'], 'full name: John Smith');
  $this
    ->assertContains($sent_email['body'], 'uuid: ' . $yamlform_submission->uuid->value);
  $this
    ->assertContains($sent_email['body'], 'sid: ' . $sid);
  $this
    ->assertContains($sent_email['body'], 'date: ' . \Drupal::service('date.formatter')
    ->format($yamlform_submission->created->value, 'medium'));
  $this
    ->assertContains($sent_email['body'], 'ip-address: ' . $yamlform_submission->remote_addr->value);
  $this
    ->assertContains($sent_email['body'], 'user: ' . $this->adminFormUser
    ->label());
  $this
    ->assertContains($sent_email['body'], "url:");
  $this
    ->assertContains($sent_email['body'], $yamlform_submission
    ->toUrl('canonical', [
    'absolute' => TRUE,
  ])
    ->toString());
  $this
    ->assertContains($sent_email['body'], "edit-url:");
  $this
    ->assertContains($sent_email['body'], $yamlform_submission
    ->toUrl('edit-form', [
    'absolute' => TRUE,
  ])
    ->toString());
  $this
    ->assertContains($sent_email['body'], 'Test that "double quotes" are not encoded.');
}