diff --git a/lib/Cron/Workers/StatsNotifications/Worker.php b/lib/Cron/Workers/StatsNotifications/Worker.php index 0ddb8793f3..2255042700 100644 --- a/lib/Cron/Workers/StatsNotifications/Worker.php +++ b/lib/Cron/Workers/StatsNotifications/Worker.php @@ -100,11 +100,11 @@ class Worker { ->withStatistics(); } - private function prepareContext(Newsletter $newsletter, NewsletterLink $link) { + private function prepareContext(Newsletter $newsletter, NewsletterLink $link = null) { $clicked = ($newsletter->statistics['clicked'] * 100) / $newsletter->total_sent; $opened = ($newsletter->statistics['opened'] * 100) / $newsletter->total_sent; $unsubscribed = ($newsletter->statistics['unsubscribed'] * 100) / $newsletter->total_sent; - return [ + $context = [ 'subject' => $newsletter->subject, 'preheader' => sprintf(_x( '%1$s%% opens, %2$s%% clicks, %3$s%% unsubscribes in a nutshell.', 'newsletter open rate, click rate and unsubscribe rate', 'mailpoet'), @@ -112,14 +112,18 @@ class Worker { number_format($opened, 2), number_format($unsubscribed, 2) ), - 'topLinkClicks' => (int)$link->clicksCount, - 'topLink' => $link->url, + $context['topLinkClicks'] = 0, 'linkSettings' => get_site_url(null, '/wp-admin/admin.php?page=mailpoet-settings#basics'), 'linkStats' => get_site_url(null, '/wp-admin/admin.php?page=mailpoet-newsletters#/stats/' . $newsletter->id()), 'premiumPluginActive' => is_plugin_active('mailpoet-premium/mailpoet-premium.php'), 'clicked' => $clicked, 'opened' => $opened, ]; + if($link) { + $context['topLinkClicks'] = (int)$link->clicksCount; + $context['topLink'] = $link->url; + } + return $context; } private function markTaskAsFinished(ScheduledTask $task) { diff --git a/lib/Models/NewsletterLink.php b/lib/Models/NewsletterLink.php index a9b92ea365..b29f2f2586 100644 --- a/lib/Models/NewsletterLink.php +++ b/lib/Models/NewsletterLink.php @@ -7,7 +7,7 @@ class NewsletterLink extends Model { public static $_table = MP_NEWSLETTER_LINKS_TABLE; static function findTopLinkForNewsletter(Newsletter $newsletter) { - return self::selectExpr('links.*') + $link = self::selectExpr('links.*') ->selectExpr('count(*)', 'clicksCount') ->tableAlias('links') ->innerJoin(StatisticsClicks::$_table, @@ -18,6 +18,10 @@ class NewsletterLink extends Model { ->orderByDesc('clicksCount') ->limit(1) ->findOne(); + if(!$link) { + return null; + } + return $link; } } diff --git a/tests/integration/Cron/Workers/StatsNotifications/WorkerTest.php b/tests/integration/Cron/Workers/StatsNotifications/WorkerTest.php index ac249ea5cc..03d5df385e 100644 --- a/tests/integration/Cron/Workers/StatsNotifications/WorkerTest.php +++ b/tests/integration/Cron/Workers/StatsNotifications/WorkerTest.php @@ -186,4 +186,36 @@ class WorkerTest extends \MailPoetTest { $this->stats_notifications->process(); } + function testItWorksForNewsletterWithNoStats() { + $newsletter = Newsletter::createOrUpdate([ + 'subject' => 'Email Subject2', + 'type' => Newsletter::TYPE_STANDARD, + ]); + $sending_task = ScheduledTask::createOrUpdate([ + 'type' => 'sending', + 'status' => ScheduledTask::STATUS_COMPLETED, + ]); + $stats_notifications_task = ScheduledTask::createOrUpdate([ + 'type' => Worker::TASK_TYPE, + 'status' => ScheduledTask::STATUS_SCHEDULED, + 'scheduled_at' => '2016-01-02 12:13:14', + 'processed_at' => null, + ]); + StatsNotification::createOrUpdate([ + 'newsletter_id' => $newsletter->id(), + 'task_id' => $stats_notifications_task->id(), + ]); + SendingQueue::createOrUpdate([ + 'newsletter_rendered_subject' => 'Email Subject2', + 'task_id' => $sending_task->id(), + 'newsletter_id' => $newsletter->id(), + 'count_processed' => 15, + ]); + + $this->mailer->expects($this->once()) + ->method('send'); + + $this->stats_notifications->process(); + } + }