You are here

public function SimplenewsSourceTest::testSendHtml in Simplenews 8.2

Same name and namespace in other branches
  1. 3.x tests/src/Functional/SimplenewsSourceTest.php \Drupal\Tests\simplenews\Functional\SimplenewsSourceTest::testSendHtml()

Send a newsletter with the HTML format.

File

tests/src/Functional/SimplenewsSourceTest.php, line 175

Class

SimplenewsSourceTest
Test cases for creating and sending newsletters.

Namespace

Drupal\Tests\simplenews\Functional

Code

public function testSendHtml() {
  $this
    ->setUpSubscribers(5);

  // Use custom testing mail system to support HTML mails.
  $mail_config = $this
    ->config('system.mail');
  $mail_config
    ->set('interface.default', 'test_simplenews_html_mail');
  $mail_config
    ->save();

  // Set the format to HTML.
  $this
    ->drupalGet('admin/config/services/simplenews');
  $this
    ->clickLink(t('Edit'));
  $edit_newsletter = [
    'format' => 'html',
    // Use umlaut to provoke mime encoding.
    'from_name' => 'Drupäl',
    // @todo: This shouldn't be necessary, default value is missing. Probably
    // should not be required.
    'from_address' => $this
      ->randomEmail(),
    // Request a confirmation receipt.
    'receipt' => TRUE,
  ];
  $this
    ->drupalPostForm(NULL, $edit_newsletter, t('Save'));
  $this
    ->clickLink(t('Edit'));
  $edit = [
    // Always use a character that is escaped.
    'title[0][value]' => $this
      ->randomString() . '\'<',
    'body[0][value]' => "Mail token: <strong>[simplenews-subscriber:mail]</strong>",
    'simplenews_issue[target_id]' => 'default',
  ];
  $this
    ->drupalPostForm('node/add/simplenews_issue', $edit, 'Save');
  $this
    ->assertEqual(1, preg_match('|node/(\\d+)$|', $this
    ->getUrl(), $matches), 'Node created');
  $node = Node::load($matches[1]);

  // Add node to spool.
  \Drupal::service('simplenews.spool_storage')
    ->addIssue($node);

  // Send mails.
  \Drupal::service('simplenews.mailer')
    ->sendSpool();

  // Make sure that 5 mails have been sent.
  $this
    ->assertEqual(5, count($this
    ->getMails()));

  // Test that tokens are correctly replaced.
  foreach (array_slice($this
    ->getMails(), 0, 3) as $mail) {

    // Verify title.
    preg_match('|<h2>(.*)</h2>|', $mail['body'], $matches);
    $this
      ->assertEqual(Html::decodeEntities($matches[1]), $node
      ->getTitle());

    // Verify the format/content type.
    $this
      ->assertEqual($mail['params']['format'], 'text/html');
    $this
      ->assertEqual($mail['params']['plain'], NULL);
    $this
      ->assertEqual($mail['headers']['Content-Type'], 'text/html; charset=UTF-8');

    // Make sure that the same mail was used in the body token as it has been
    // sent to.
    $this
      ->assertTrue(strpos($mail['body'], '<strong>' . $mail['to'] . '</strong>') !== FALSE);

    // Make sure the body is only attached once.
    $this
      ->assertEqual(1, preg_match_all('/Mail token/', $mail['body'], $matches));

    // Check the plaintext version, both params][plaintext (Mime Mail) and
    // plain (Swiftmailer).
    $this
      ->assertTrue(strpos($mail['params']['plaintext'], $mail['to']) !== FALSE);
    $this
      ->assertFalse(strpos($mail['params']['plaintext'], '<strong>'));
    $this
      ->assertEqual($mail['params']['plaintext'], $mail['plain']);

    // Make sure the body is only attached once.
    $this
      ->assertEqual(1, preg_match_all('/Mail token/', $mail['params']['plaintext'], $matches));

    // Check the attachments and files arrays.
    $this
      ->assertTrue(is_array($mail['params']['attachments']));
    $this
      ->assertEqual($mail['params']['attachments'], $mail['params']['files']);

    // Make sure formatted address is properly encoded.
    $from = '"' . addslashes(Unicode::mimeHeaderEncode($edit_newsletter['from_name'])) . '" <' . $edit_newsletter['from_address'] . '>';
    $this
      ->assertEqual($from, $mail['reply-to']);

    // And make sure it won't get encoded twice.
    $this
      ->assertEqual($from, Unicode::mimeHeaderEncode($mail['reply-to']));

    // @todo: Improve this check, there are currently two spaces, not sure
    // where they are coming from.
    $this
      ->assertTrue(strpos($mail['body'], 'class="newsletter-footer"') !== FALSE);

    // Verify receipt headers.
    $this
      ->assertEqual($mail['headers']['Disposition-Notification-To'], $edit_newsletter['from_address']);
    $this
      ->assertEqual($mail['headers']['X-Confirm-Reading-To'], $edit_newsletter['from_address']);
  }
}