You are here

function SimplenewsSendTestCase::testSendMultipleNoCron in Simplenews 7.2

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

Send a newsletter using cron.

File

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

Class

SimplenewsSendTestCase
Test cases for creating and sending newsletters.

Code

function testSendMultipleNoCron() {

  // Disable cron.
  variable_set('simplenews_use_cron', FALSE);

  // Verify that the newsletter settings are shown.
  $nodes = array();
  for ($i = 0; $i < 3; $i++) {
    $this
      ->drupalGet('node/add/simplenews');
    $this
      ->assertText(t('Newsletter'));
    $edit = array(
      'title' => $this
        ->randomName(),
      // The last newsletter shouldn't be published.
      'status' => $i != 2,
    );
    $this
      ->drupalPost(NULL, $edit, 'Save');
    $this
      ->assertTrue(preg_match('|node/(\\d+)$|', $this
      ->getUrl(), $matches), 'Node created');
    $nodes[] = node_load($matches[1]);

    // Verify state.
    $this
      ->assertEqual(SIMPLENEWS_STATUS_SEND_NOT, simplenews_issue_status(current($nodes)), t('Newsletter not sent yet.'));
  }

  // Send the first and last newsletter on the newsletter overview.
  list($first, $second, $unpublished) = $nodes;
  $edit = array(
    'issues[' . $first->nid . ']' => $first->nid,
    'issues[' . $unpublished->nid . ']' => $unpublished->nid,
    'operation' => 'activate',
  );
  $this
    ->drupalPost('admin/content/simplenews', $edit, t('Update'));
  $this
    ->assertText(t('Sent the following newsletters: @title.', array(
    '@title' => $first->title,
  )));
  $this
    ->assertText(t('Newsletter @title is unpublished and will be sent on publish.', array(
    '@title' => $unpublished->title,
  )));

  // Verify states.
  entity_get_controller('node')
    ->resetCache();
  $this
    ->assertEqual(SIMPLENEWS_STATUS_SEND_READY, simplenews_issue_status(node_load($first->nid)), t('First Newsletter sending finished'));
  $this
    ->assertEqual(SIMPLENEWS_STATUS_SEND_NOT, simplenews_issue_status(node_load($second->nid)), t('Second Newsletter not sent'));
  $this
    ->assertEqual(SIMPLENEWS_STATUS_SEND_PUBLISH, simplenews_issue_status(node_load($unpublished->nid)), t('Newsletter set to send on publish'));

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