function SimplenewsSendTestCase::testSendNowCron in Simplenews 7
Same name and namespace in other branches
- 6.2 tests/simplenews.test \SimplenewsSendTestCase::testSendNowCron()
- 7.2 tests/simplenews.test \SimplenewsSendTestCase::testSendNowCron()
Send a newsletter without using cron.
File
- tests/
simplenews.test, line 2304 - Simplenews test functions.
Class
- SimplenewsSendTestCase
- Test cases for creating and sending newsletters.
Code
function testSendNowCron() {
// Verify that the newsletter settings are shown.
$this
->drupalGet('node/add/simplenews');
$this
->assertText(t('Newsletter category'));
$edit = array(
'title' => $this
->randomName(),
);
// Try preview first.
$this
->drupalPost(NULL, $edit, t('Preview'));
// Then save.
$this
->drupalPost(NULL, array(), t('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'));
$this
->assertNoText(t('Send newsletter when published'), t('Send on publish is not shown for published nodes.'));
// Verify state.
$newsletter = simplenews_newsletter_load($node->nid);
$this
->assertEqual(SIMPLENEWS_STATUS_SEND_NOT, $newsletter->status, t('Newsletter not sent yet.'));
// Send now.
$this
->drupalPost(NULL, array(
'simplenews[send]' => SIMPLENEWS_COMMAND_SEND_NOW,
), t('Submit'));
// Verify state.
$newsletter = simplenews_newsletter_load($node->nid);
$this
->assertEqual(SIMPLENEWS_STATUS_SEND_PENDING, $newsletter->status, t('Newsletter sending pending.'));
// Verify that no mails have been sent yet.
$mails = $this
->drupalGetMails();
$this
->assertEqual(0, count($mails), t('No mails were sent yet.'));
$spooled = db_query('SELECT COUNT(*) FROM {simplenews_mail_spool} WHERE nid = :nid', array(
':nid' => $node->nid,
))
->fetchField();
$this
->assertEqual(5, $spooled, t('5 mails have been added to the mail spool'));
// Check warning message on node edit form.
$this
->clickLink(t('Edit'));
$this
->assertText(t('This newsletter issue is currently being sent. Any changes will be reflected in the e-mails which have not been sent yet.'));
// Run cron.
simplenews_cron();
// Verify state.
$newsletter = simplenews_newsletter_load($node->nid);
$this
->assertEqual(SIMPLENEWS_STATUS_SEND_READY, $newsletter->status, t('Newsletter sending finished.'));
$spooled = db_query('SELECT COUNT(*) FROM {simplenews_mail_spool} WHERE nid = :nid', array(
':nid' => $node->nid,
))
->fetchField();
$this
->assertEqual(0, $spooled, t('No mails remaining in spool.'));
// 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'));
}