Only schedule notification for standard newsletters

[MAILPOET-1571]
This commit is contained in:
Pavel Dohnal
2019-01-08 15:53:47 +01:00
parent 1d34613b17
commit 6452e83476
5 changed files with 63 additions and 27 deletions

View File

@@ -5,7 +5,6 @@ namespace MailPoet\Test\Cron\Workers;
use MailPoet\Cron\Workers\StatsNotifications;
use MailPoet\Models\Newsletter;
use MailPoet\Models\ScheduledTask;
use MailPoet\Models\SendingQueue;
use MailPoet\Models\Setting;
use MailPoet\Models\StatsNotification;
@@ -24,7 +23,7 @@ class StatsNotificationsTest extends \MailPoetTest {
function testShouldSchedule() {
$newsletter_id = 5;
$newsletter = Newsletter::createOrUpdate(['id' => $newsletter_id]);
$newsletter = Newsletter::createOrUpdate(['id' => $newsletter_id, 'type' => Newsletter::TYPE_STANDARD]);
$this->stats_notifications->schedule($newsletter);
$notification = StatsNotification::where('newsletter_id', $newsletter_id)->findOne();
expect($notification)->isInstanceOf(StatsNotification::class);
@@ -38,19 +37,19 @@ class StatsNotificationsTest extends \MailPoetTest {
'enabled' => false,
'address' => 'email@example.com'
]);
$newsletter = Newsletter::createOrUpdate(['id' => $newsletter_id]);
$newsletter = Newsletter::createOrUpdate(['id' => $newsletter_id, 'type' => Newsletter::TYPE_STANDARD]);
$this->stats_notifications->schedule($newsletter);
$queue = SendingQueue::where('newsletter_id', $newsletter_id)->findOne();
expect($queue)->isEmpty();
$notification = StatsNotification::where('newsletter_id', $newsletter_id)->findOne();
expect($notification)->isEmpty();
}
function testShouldNotScheduleIfSettingsMissing() {
$newsletter_id = 7;
Setting::setValue(StatsNotifications::SETTINGS_KEY, []);
$newsletter = Newsletter::createOrUpdate(['id' => $newsletter_id]);
$newsletter = Newsletter::createOrUpdate(['id' => $newsletter_id, 'type' => Newsletter::TYPE_STANDARD]);
$this->stats_notifications->schedule($newsletter);
$queue = SendingQueue::where('newsletter_id', $newsletter_id)->findOne();
expect($queue)->isEmpty();
$notification = StatsNotification::where('newsletter_id', $newsletter_id)->findOne();
expect($notification)->isEmpty();
}
function testShouldNotScheduleIfEmailIsMissing() {
@@ -58,10 +57,10 @@ class StatsNotificationsTest extends \MailPoetTest {
Setting::setValue(StatsNotifications::SETTINGS_KEY, [
'enabled' => true,
]);
$newsletter = Newsletter::createOrUpdate(['id' => $newsletter_id]);
$newsletter = Newsletter::createOrUpdate(['id' => $newsletter_id, 'type' => Newsletter::TYPE_STANDARD]);
$this->stats_notifications->schedule($newsletter);
$queue = SendingQueue::where('newsletter_id', $newsletter_id)->findOne();
expect($queue)->isEmpty();
$notification = StatsNotification::where('newsletter_id', $newsletter_id)->findOne();
expect($notification)->isEmpty();
}
function testShouldNotScheduleIfEmailIsEmpty() {
@@ -70,10 +69,10 @@ class StatsNotificationsTest extends \MailPoetTest {
'enabled' => true,
'address' => ' '
]);
$newsletter = Newsletter::createOrUpdate(['id' => $newsletter_id]);
$newsletter = Newsletter::createOrUpdate(['id' => $newsletter_id, 'type' => Newsletter::TYPE_STANDARD]);
$this->stats_notifications->schedule($newsletter);
$queue = SendingQueue::where('newsletter_id', $newsletter_id)->findOne();
expect($queue)->isEmpty();
$notification = StatsNotification::where('newsletter_id', $newsletter_id)->findOne();
expect($notification)->isEmpty();
}
function testShouldNotScheduleIfAlreadyScheduled() {
@@ -83,18 +82,26 @@ class StatsNotificationsTest extends \MailPoetTest {
'status' => ScheduledTask::STATUS_SCHEDULED,
'scheduled_at' => '2017-01-02 12:13:14',
]);
$existing_queue = SendingQueue::createOrUpdate([
$existing_notification = StatsNotification::createOrUpdate([
'newsletter_id' => $newsletter_id,
'task_id' => $existing_task->id,
]);
$newsletter = Newsletter::createOrUpdate(['id' => $newsletter_id]);
$newsletter = Newsletter::createOrUpdate(['id' => $newsletter_id, 'type' => Newsletter::TYPE_STANDARD]);
$this->stats_notifications->schedule($newsletter);
$queues = SendingQueue::where('newsletter_id', $newsletter_id)->findMany();
expect($queues)->count(1);
$tasks = ScheduledTask::where('id', $queues[0]->task_id)->findMany();
$notifications = StatsNotification::where('newsletter_id', $newsletter_id)->findMany();
expect($notifications)->count(1);
$tasks = ScheduledTask::where('id', $notifications[0]->task_id)->findMany();
expect($tasks)->count(1);
expect($existing_queue->id)->equals($queues[0]->id);
expect($existing_notification->id)->equals($notifications[0]->id);
expect($existing_task->id)->equals($tasks[0]->id);
}
function testShouldNotScheduleIfInvalidType() {
$newsletter_id = 11;
$newsletter = Newsletter::createOrUpdate(['id' => $newsletter_id, Newsletter::TYPE_WELCOME]);
$this->stats_notifications->schedule($newsletter);
$notification = StatsNotification::where('newsletter_id', $newsletter_id)->findOne();
expect($notification)->isEmpty();
}
}