function SimplenewsSendTestCase::testSendPublishNoCron in Simplenews 7.2
Same name and namespace in other branches
- 7 tests/simplenews.test \SimplenewsSendTestCase::testSendPublishNoCron()
 
Send a newsletter on publish without using cron.
File
- tests/
simplenews.test, line 2505  - Simplenews test functions.
 
Class
- SimplenewsSendTestCase
 - Test cases for creating and sending newsletters.
 
Code
function testSendPublishNoCron() {
  // Disable cron.
  variable_set('simplenews_use_cron', FALSE);
  // Verify that the newsletter settings are shown.
  $this
    ->drupalGet('node/add/simplenews');
  $this
    ->assertText(t('Newsletter'));
  $edit = array(
    'title' => $this
      ->randomName(),
    'status' => FALSE,
  );
  $this
    ->drupalPost(NULL, $edit, 'Save');
  $this
    ->assertTrue(preg_match('|node/(\\d+)$|', $this
    ->getUrl(), $matches), 'Node created');
  $node = node_load($matches[1]);
  $this
    ->clickLink(t('Newsletter'));
  $this
    ->assertText(t('Send one test newsletter to the test address'));
  $this
    ->assertText(t('Send newsletter when published'), t('Send on publish is shown'));
  // Verify state.
  entity_get_controller('node')
    ->resetCache();
  $this
    ->assertEqual(SIMPLENEWS_STATUS_SEND_NOT, simplenews_issue_status(node_load($node->nid)), t('Newsletter not sent yet.'));
  // Send now.
  $this
    ->drupalPost(NULL, array(
    'simplenews[send]' => SIMPLENEWS_COMMAND_SEND_PUBLISH,
  ), t('Submit'));
  // Verify state.
  entity_get_controller('node')
    ->resetCache(array(
    $node->nid,
  ));
  $this
    ->assertEqual(SIMPLENEWS_STATUS_SEND_PUBLISH, simplenews_issue_status(node_load($node->nid)), t('Newsletter set up for sending on publish.'));
  $this
    ->clickLink(t('Edit'));
  $update = array(
    'status' => TRUE,
  );
  $this
    ->drupalPost(NULL, $update, t('Save'));
  // Send on publish does not send immediately.
  entity_get_controller('node')
    ->resetCache(array(
    $node->nid,
  ));
  simplenews_mail_attempt_immediate_send(array(), FALSE);
  // Verify state.
  entity_get_controller('node')
    ->resetCache(array(
    $node->nid,
  ));
  $this
    ->assertEqual(SIMPLENEWS_STATUS_SEND_READY, simplenews_issue_status(node_load($node->nid)), t('Newsletter sending finished'));
  // @todo test sent subscriber count.
  // 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] ' . $edit['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'));
}