You are here

function SimplenewsSourceTest::testSendHTML in Simplenews 8

Send a newsletter with the HTML format.

File

src/Tests/SimplenewsSourceTest.php, line 177
Simplenews source test functions.

Class

SimplenewsSourceTest
Test cases for creating and sending newsletters.

Namespace

Drupal\simplenews\Tests

Code

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 = array(
    '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 = array(
    // Always use a character that is escaped.
    'title[0][value]' => $this
      ->randomString() . '\'<',
    'body[0][value]' => "Mail token: <strong>[simplenews-subscriber:mail]</strong>",
    'simplenews_issue' => 'default',
  );
  $this
    ->drupalPostForm('node/add/simplenews_issue', $edit, 'Save');
  $this
    ->assertTrue(preg_match('|node/(\\d+)$|', $this
    ->getUrl(), $matches), 'Node created');
  $node = Node::load($matches[1]);

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

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

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

  // Test that tokens are correctly replaced.
  foreach (array_slice($this
    ->drupalGetMails(), 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"'));

    // 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']);
  }
}