You are here

function SimplenewsSourceTestCase::testSendHTML in Simplenews 7

Same name and namespace in other branches
  1. 7.2 tests/simplenews.test \SimplenewsSourceTestCase::testSendHTML()

Send a newsletter with the HTML format.

File

tests/simplenews.test, line 2724
Simplenews test functions.

Class

SimplenewsSourceTestCase
Test cases for creating and sending newsletters.

Code

function testSendHTML() {
  $this
    ->setUpSubscribers(5);

  // Use custom testing mail system to support HTML mails.
  variable_set('mail_system', array(
    'default-system' => 'SimplenewsHTMLTestingMailSystem',
  ));

  // Set the format to HTML.
  $this
    ->drupalGet('admin/config/services/simplenews');
  $this
    ->clickLink(t('edit newsletter category'));
  $edit_category = 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
    ->drupalPost(NULL, $edit_category, t('Save'));
  $edit = array(
    'title' => $this
      ->randomName(),
    'body[und][0][value]' => "Mail token: <strong>[simplenews-subscriber:mail]</strong>",
  );
  $this
    ->drupalPost('node/add/simplenews', $edit, 'Save');
  $this
    ->assertTrue(preg_match('|node/(\\d+)$|', $this
    ->getUrl(), $matches), 'Node created');
  $node = node_load($matches[1]);

  // Add node to spool.
  simplenews_add_node_to_spool($node);

  // Send mails.
  simplenews_mail_spool();

  // 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.
    $this
      ->assertTrue(strpos($mail['body'], '<h2>' . $node->title . '</h2>') !== FALSE);

    // 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.
    $this
      ->assertTrue(strpos($mail['params']['plaintext'], $mail['to']) !== FALSE);
    $this
      ->assertFalse(strpos($mail['params']['plaintext'], '<strong>'));

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

    // Make sure formatted address is properly encoded.
    $from = '"' . addslashes(mime_header_encode($edit_category['from_name'])) . '" <' . $edit_category['from_address'] . '>';
    $this
      ->assertEqual($from, $mail['from']);

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

    // @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_category['from_address']);
    $this
      ->assertEqual($mail['headers']['X-Confirm-Reading-To'], $edit_category['from_address']);
  }
}