You are here

function SimplenewsSendTestCase::testDelete in Simplenews 6.2

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

Create a newsletter, send mails and then delete.

File

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

Class

SimplenewsSendTestCase
Test cases for creating and sending newsletters.

Code

function testDelete() {

  // Verify that the newsletter settings are shown.
  $this
    ->drupalGet('node/add/simplenews');
  $this
    ->assertText(t('Newsletter'));

  // Prevent deleting the mail spool entries automatically.
  variable_set('simplenews_spool_expire', 1);
  $edit = array(
    'title' => $this
      ->randomName(),
    'taxonomy[' . variable_get('simplenews_vid', '') . ']' => $this
      ->getRandomNewsletter(),
  );
  $this
    ->drupalPost(NULL, $edit, 'Save');
  $this
    ->assertTrue(preg_match('|node/(\\d+)$|', $this
    ->getUrl(), $matches), 'Node created');
  $node = node_load($matches[1], NULL, TRUE);
  $this
    ->clickLink(t('Newsletter'));
  $this
    ->assertText(t('Send one test newsletter to the test address'));
  $this
    ->assertText(t('Send newsletter'));

  // Verify state.
  $this
    ->assertEqual(SIMPLENEWS_STATUS_SEND_NOT, $node->simplenews['s_status'], t('Newsletter not sent yet.'));

  // Send now.
  $this
    ->drupalPost(NULL, array(
    'simplenews[send]' => SIMPLENEWS_COMMAND_SEND_NOW,
  ), t('Submit'));

  // Verify state.
  $node = node_load($node->nid, NULL, TRUE);
  $this
    ->assertEqual(SIMPLENEWS_STATUS_SEND_PENDING, $node->simplenews['s_status'], t('Newsletter sending pending.'));
  $spooled = db_result(db_query('SELECT COUNT(*) FROM {simplenews_mail_spool} WHERE nid = %d', $node->nid));
  $this
    ->assertEqual(5, $spooled, t('5 mails remaining in spool.'));

  // Verify that deleting isn't possible right now.
  // @todo: Feature not supported by 6.x-2.x.

  //$this->clickLink(t('Edit'));

  //$this->assertText(t("You can't delete this newsletter because it has not been sent to all its subscribers."));

  //$this->assertNoText(t('Delete'));

  // Send mails.
  simplenews_cron();

  // Verify state.
  $node = node_load($node->nid, NULL, TRUE);
  $this
    ->assertEqual(SIMPLENEWS_STATUS_SEND_READY, $node->simplenews['s_status'], t('Newsletter sending finished'));
  $spooled = db_result(db_query('SELECT COUNT(*) FROM {simplenews_mail_spool} WHERE nid = %d', $node->nid));
  $this
    ->assertEqual(5, $spooled, t('Mails are kept in simplenews_mail_spool after being sent.'));

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

  // Now delete.
  $this
    ->clickLink(t('Edit'));
  $this
    ->drupalPost(NULL, array(), t('Delete'));
  $this
    ->drupalPost(NULL, array(), t('Delete'));

  // Verify.
  $spooled = db_result(db_query('SELECT COUNT(*) FROM {simplenews_mail_spool} WHERE nid = %d', $node->nid));
  $this
    ->assertEqual(0, $spooled, t('No mails remaining in spool.'));
}