diff --git a/includes/simplenews.mail.inc b/includes/simplenews.mail.inc index d05758f..f3eaede 100644 --- a/includes/simplenews.mail.inc +++ b/includes/simplenews.mail.inc @@ -224,6 +224,7 @@ function simplenews_mail_spool($limit = SIMPLENEWS_UNLIMITED, array $conditions simplenews_impersonate_user(drupal_anonymous_user()); $count_fail = $count_success = 0; + $sent = array(); _simplenews_measure_usec(TRUE); @@ -240,6 +241,12 @@ function simplenews_mail_spool($limit = SIMPLENEWS_UNLIMITED, array $conditions simplenews_update_spool(array($msid), $row_result); if ($row_result['status'] == SIMPLENEWS_SPOOL_DONE) { $count_success++; + if (!isset($sent[$row->actual_nid])) { + $sent[$row->actual_nid] = 1; + } + else { + $sent[$row->actual_nid]++; + } } if ($row_result['error']) { $count_fail++; @@ -267,12 +274,28 @@ function simplenews_mail_spool($limit = SIMPLENEWS_UNLIMITED, array $conditions simplenews_update_spool(array($msid), $row_result); if ($row_result['status'] == SIMPLENEWS_SPOOL_DONE) { $count_success++; + if (isset($row->actual_nid)) { + if (!isset($sent[$row->actual_nid])) { + $sent[$row->actual_nid] = 1; + } + else { + $sent[$row->actual_nid]++; + } + } } if ($row_result['error']) { $count_fail++; } } + // Update subscriber count. + foreach ($sent as $nid => $count) { + db_update('simplenews_newsletter') + ->condition('nid', $nid) + ->expression('sent_subscriber_count', 'sent_subscriber_count + :count', array(':count' => $count)) + ->execute(); + } + // Report sent result and elapsed time. On Windows systems getrusage() is // not implemented and hence no elapsed time is available. if (function_exists('getrusage')) { diff --git a/simplenews.install b/simplenews.install index 03caf85..3bddae9 100644 --- a/simplenews.install +++ b/simplenews.install @@ -854,4 +854,40 @@ function simplenews_update_7008() { 'default' => '', )); } +} + +/** + * Add the sent_subsriber_countcolumn to {simplenews_newsletter} if missing. + */ +function simplenews_update_7009() { + if (!db_field_exists('simplenews_newsletter', 'sent_subscriber_count')) { + db_add_field('simplenews_newsletter', 'sent_subscriber_count', array( + 'description' => 'The count of subscribers to the newsletter when it was sent.', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + )); + } +} + +/** + * Update empty sent subscriber count column to current subscriber count. + */ +function simplenews_update_7010() { + // Assume that already sent newsletters that have a sent subscriber count of + // 0 have been sent to all subscribers. Do a update query per newsletter + // category, to avoid having to re-execute the subquery on every row. + $tids = db_query('SELECT tid FROM {simplenews_category}')->fetchCol(); + foreach ($tids as $tid) { + $count = db_query('SELECT COUNT(*) FROM {simplenews_subscription} ss WHERE status = 1 AND tid = :tid', array(':tid' => $tid))->fetchField(); + db_update('simplenews_newsletter') + ->fields(array( + 'sent_subscriber_count' => $count, + )) + // 2 equals SIMPLENEWS_STATUS_SEND_READY. + ->condition('status', 2) + ->condition('sent_subscriber_count', 0) + ->condition('tid', $tid) + ->execute(); + } } \ No newline at end of file diff --git a/tests/simplenews.test b/tests/simplenews.test index 92237c9..c5bd3ce 100644 --- a/tests/simplenews.test +++ b/tests/simplenews.test @@ -1710,10 +1710,12 @@ class SimpleNewsI18nTestCase extends SimplenewsTestCase { // Sign up two users, one in english, another in spanish. $english_mail = $this->randomEmail(); $spanish_mail = $this->randomEmail(); + $spanish_mail2 = $this->randomEmail(); $tid = $this->getRandomNewsletter(); simplenews_subscribe_user($english_mail, $tid, FALSE, 'english', 'en'); simplenews_subscribe_user($spanish_mail, $tid, FALSE, 'spanish', 'es'); + simplenews_subscribe_user($spanish_mail2, $tid, FALSE, 'spanish', 'es'); // Translate category. $vocabulary = taxonomy_vocabulary_machine_name_load('newsletter'); @@ -1776,7 +1778,7 @@ class SimpleNewsI18nTestCase extends SimplenewsTestCase { $this->drupalPost(NULL, $edit, t('Submit')); simplenews_cron(); - $this->assertEqual(2, count($this->drupalGetMails())); + $this->assertEqual(3, count($this->drupalGetMails())); $category = simplenews_category_load($this->getRandomNewsletter()); @@ -1788,7 +1790,7 @@ class SimpleNewsI18nTestCase extends SimplenewsTestCase { $this->assertEqual('[' . $category->name . '] ' . $english_node->title, $mail['subject']); $node_url = url('node/' . $english_node->nid, array('language' => $languages['en'], 'absolute' => TRUE)); } - elseif ($mail['to'] == $spanish_mail) { + elseif ($mail['to'] == $spanish_mail || $mail['to'] == $spanish_mail2) { $this->assertEqual('es', $mail['language']); $this->assertEqual('[' . $es_name . '] ' . $spanish_node->title, $mail['subject']); $node_url = url('node/' . $spanish_node->nid, array('language' => $languages['es'], 'absolute' => TRUE)); @@ -1801,6 +1803,12 @@ class SimpleNewsI18nTestCase extends SimplenewsTestCase { $this->assertTrue(strpos($mail['body'], $node_url) !== FALSE); } + // Verify sent subscriber count for each node. + $newsletter = simplenews_newsletter_load($english_node->nid); + $this->assertEqual(1, $newsletter->sent_subscriber_count, 'subscriber count is correct for english'); + $newsletter = simplenews_newsletter_load($spanish_node->nid); + $this->assertEqual(2, $newsletter->sent_subscriber_count, 'subscriber count is correct for spanish'); + // Make sure the language of a node can be changed. $english = array( 'title' => $this->randomName(), @@ -2134,6 +2142,9 @@ class SimplenewsSendTestCase extends SimplenewsTestCase { unset($this->subscribers[$mail['to']]); } $this->assertEqual(0, count($this->subscribers), t('all subscribers have been received a mail')); + + $newsletter = simplenews_newsletter_load($node->nid); + $this->assertEqual(5, $newsletter->sent_subscriber_count, 'subscriber count is correct'); } /** @@ -2241,8 +2252,8 @@ class SimplenewsSendTestCase extends SimplenewsTestCase { // Verify state. $newsletter = simplenews_newsletter_load($node->nid); - $this->assertEqual(SIMPLENEWS_STATUS_SEND_PENDING, $newsletter->status, t('Newsletter sending pending.')); + $this->assertEqual(3, $newsletter->sent_subscriber_count, 'subscriber count is correct'); $spooled = db_query('SELECT COUNT(*) FROM {simplenews_mail_spool} WHERE nid = :nid', array(':nid' => $node->nid))->fetchField(); $this->assertEqual(2, $spooled, t('2 mails remaining in spool.')); @@ -2267,6 +2278,8 @@ class SimplenewsSendTestCase extends SimplenewsTestCase { unset($this->subscribers[$mail['to']]); } $this->assertEqual(0, count($this->subscribers), t('all subscribers have been received a mail')); + $newsletter = simplenews_newsletter_load($node->nid); + $this->assertEqual(5, $newsletter->sent_subscriber_count, 'subscriber count is correct'); } /**