You are here

public function EasyEmailSendTest::testSendHtmlGeneratePlainText in Easy Email 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Functional/EasyEmailSendTest.php \Drupal\Tests\easy_email\Functional\EasyEmailSendTest::testSendHtmlGeneratePlainText()

Tests email sending with plain text version generated from HTML version

Throws

\Behat\Mink\Exception\ExpectationException

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

File

tests/src/Functional/EasyEmailSendTest.php, line 433

Class

EasyEmailSendTest
Class EasyEmailSendTest

Namespace

Drupal\Tests\easy_email\Functional

Code

public function testSendHtmlGeneratePlainText() {
  $template_id = 'test_plain_text_generated';
  $template_label = 'Test: Plain Text Generated';
  $template = $this
    ->createTemplate([
    'id' => $template_id,
    'label' => $template_label,
  ]);
  $this
    ->addUserField($template, 'field_user', 'User');
  $this
    ->drupalGet('admin/structure/email-templates/templates');
  $this
    ->assertSession()
    ->pageTextContains($template_id);
  $this
    ->assertSession()
    ->pageTextContains($template_label);
  $template
    ->setRecipient([
    'test@example.com',
  ])
    ->setSubject('Test Email with Generated Plain Text')
    ->setBodyHtml([
    'value' => '<p>This is a test email (HTML) for user account [easy_email:field_user:0:entity:account-name].</p>',
    'format' => 'html',
  ])
    ->setGenerateBodyPlain(TRUE)
    ->save();
  $this
    ->drupalGet('admin/content/email/add/' . $template
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('field_user');
  $user1 = $this
    ->createUser();
  $this
    ->submitForm([
    'field_user[0][target_id]' => $user1
      ->getAccountName() . ' (' . $user1
      ->id() . ')',
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Created new email.');
  $this
    ->assertSession()
    ->pageTextContains('Email sent.');

  /** @var \Drupal\Core\Config\ImmutableConfig $site_config */
  $site_config = \Drupal::config('system.site');
  $this
    ->assertSession()
    ->elementTextContains('css', '[data-drupal-selector="inbox-preview"] .body-preview', 'This is a test email (HTML) for user account ' . $user1
    ->getDisplayName() . '.');
  $html_body_iframe = $this
    ->assertSession()
    ->elementExists('css', '[data-drupal-selector="html-body"] iframe');
  $html_body_url = $this
    ->getIframeUrlAndQuery($html_body_iframe);
  $plain_body_iframe = $this
    ->assertSession()
    ->elementExists('css', '[data-drupal-selector="plain-body"] iframe');
  $plain_body_url = $this
    ->getIframeUrlAndQuery($plain_body_iframe);
  $this
    ->drupalGet($html_body_url['path'], [
    'query' => $html_body_url['query'],
  ]);
  $this
    ->assertSession()
    ->responseContains('<p>This is a test email (HTML) for user account ' . $user1
    ->getAccountName() . '.</p>');
  $this
    ->drupalGet($plain_body_url['path'], [
    'query' => $plain_body_url['query'],
  ]);
  $this
    ->assertSession()
    ->responseContains('This is a test email (HTML) for user account ' . $user1
    ->getAccountName() . '.');
  $emails = $this
    ->getSentEmails([]);
  $this
    ->assertEquals(1, count($emails));
  $email = reset($emails);
  $this
    ->assertEquals($template
    ->id(), $email['key']);
  $this
    ->assertStringContainsString('<p>This is a test email (HTML) for user account ' . $user1
    ->getAccountName() . '.</p>', (string) $email['body']);
  $this
    ->assertStringContainsString('This is a test email (HTML) for user account ' . $user1
    ->getAccountName() . '.', (string) $email['plain']);
}