You are here

function SimplenewsSendTestCase::testProgrammaticNewsletter in Simplenews 7.2

Same name and namespace in other branches
  1. 7 tests/simplenews.test \SimplenewsSendTestCase::testProgrammaticNewsletter()

Creates and sends a node using the API.

File

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

Class

SimplenewsSendTestCase
Test cases for creating and sending newsletters.

Code

function testProgrammaticNewsletter() {

  // Create a very basic node.
  $node = new stdClass();
  $node->type = 'simplenews';
  $node->title = $this
    ->randomName();
  $node->uid = 0;
  $node->status = 1;
  $node->language = LANGUAGE_NONE;
  simplenews_issue_newsletter_id($node, $this
    ->getRandomNewsletter());
  node_save($node);

  // Send the node.
  simplenews_add_node_to_spool($node);

  // Send mails.
  simplenews_mail_spool();
  simplenews_clear_spool();

  // Update sent status for newsletter admin panel.
  simplenews_send_status_update();

  // Verify mails.
  $mails = $this
    ->drupalGetMails();
  $this
    ->assertEqual(5, count($mails), t('All mails were sent.'));
  foreach ($mails as $mail) {
    $this
      ->assertEqual($mail['subject'], '[Drupal newsletter] ' . $node->title, t('Mail has correct subject'));
    $this
      ->assertTrue(isset($this->subscribers[$mail['to']]), t('Found valid recipient'));
    unset($this->subscribers[$mail['to']]);
  }
  $this
    ->assertEqual(0, count($this->subscribers), t('all subscribers have been received a mail'));

  // Create another node.
  $node = new stdClass();
  $node->type = 'simplenews';
  $node->title = $this
    ->randomName();
  $node->uid = 0;
  $node->status = 1;
  $node->language = LANGUAGE_NONE;
  simplenews_issue_newsletter_id($node, $this
    ->getRandomNewsletter());
  node_save($node);

  // Send the node.
  simplenews_add_node_to_spool($node);

  // Make sure that they have been added.
  $this
    ->assertEqual(simplenews_count_spool(), 5);

  // Mark them as pending, fake a currently running send process.
  $this
    ->assertEqual(count(simplenews_get_spool(2)), 2);

  // Those two should be excluded from the count now.
  $this
    ->assertEqual(simplenews_count_spool(), 3);

  // Get two additional spool entries.
  $this
    ->assertEqual(count(simplenews_get_spool(2)), 2);

  // Now only one should be returned by the count.
  $this
    ->assertEqual(simplenews_count_spool(), 1);
}