function SimplenewsSendTestCase::testSendNowCronThrottle in Simplenews 7.2
Same name and namespace in other branches
- 6.2 tests/simplenews.test \SimplenewsSendTestCase::testSendNowCronThrottle()
- 7 tests/simplenews.test \SimplenewsSendTestCase::testSendNowCronThrottle()
Send a newsletter using cron and a low throttle.
File
- tests/
simplenews.test, line 2361 - Simplenews test functions.
Class
- SimplenewsSendTestCase
- Test cases for creating and sending newsletters.
Code
function testSendNowCronThrottle() {
variable_set('simplenews_throttle', 3);
// Verify that the newsletter settings are shown.
$this
->drupalGet('node/add/simplenews');
$this
->assertText(t('Newsletter'));
$edit = array(
'title' => $this
->randomName(),
);
$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'));
$this
->assertNoText(t('Send newsletter when published'), t('Send on publish is not shown for published nodes.'));
// 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_NOW,
), t('Submit'));
// Verify state.
entity_get_controller('node')
->resetCache();
$this
->assertEqual(SIMPLENEWS_STATUS_SEND_PENDING, simplenews_issue_status(node_load($node->nid)), 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 entity_id = :nid AND entity_type = :type', array(
':nid' => $node->nid,
':type' => 'node',
))
->fetchField();
$this
->assertEqual(5, $spooled, t('5 mails have been added to the mail spool'));
// Run cron for the first time.
simplenews_cron();
// Verify state.
entity_get_controller('node')
->resetCache();
$node = node_load($node->nid);
$this
->assertEqual(SIMPLENEWS_STATUS_SEND_PENDING, simplenews_issue_status($node), t('Newsletter sending pending.'));
$this
->assertEqual(3, simplenews_issue_sent_count('node', $node), 'subscriber count is correct');
$spooled = db_query('SELECT COUNT(*) FROM {simplenews_mail_spool} WHERE entity_id = :nid AND entity_type = :type', array(
':nid' => $node->nid,
':type' => 'node',
))
->fetchField();
$this
->assertEqual(2, $spooled, t('2 mails remaining in spool.'));
// Run cron for the second time.
simplenews_cron();
// Verify state.
entity_get_controller('node')
->resetCache();
$this
->assertEqual(SIMPLENEWS_STATUS_SEND_READY, simplenews_issue_status(node_load($node->nid)), t('Newsletter sending finished.'));
$spooled = db_query('SELECT COUNT(*) FROM {simplenews_mail_spool} WHERE entity_id = :nid AND entity_type = :type', array(
':nid' => $node->nid,
':type' => 'node',
))
->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'));
$this
->assertEqual(5, simplenews_issue_sent_count('node', node_load($node->nid)));
}