Move post notification scheduling to service

[MAILPOET-2348]
This commit is contained in:
Pavel Dohnal
2019-09-09 11:04:31 +02:00
committed by Jack Kitterhing
parent 446af84e6c
commit 3d750c5bb8
10 changed files with 505 additions and 419 deletions

View File

@ -20,6 +20,7 @@ use MailPoet\Models\Setting;
use MailPoet\Models\Subscriber;
use MailPoet\Newsletter\NewslettersRepository;
use MailPoet\Newsletter\Renderer\Renderer;
use MailPoet\Newsletter\Scheduler\PostNotificationScheduler;
use MailPoet\Newsletter\Scheduler\Scheduler;
use MailPoet\Newsletter\Url as NewsletterUrl;
use MailPoet\Services\AuthorizedEmailsController;
@ -59,6 +60,9 @@ class Newsletters extends APIEndpoint {
/** @var NewslettersResponseBuilder */
private $newsletters_response_builder;
/** @var PostNotificationScheduler */
private $post_notification_scheduler;
function __construct(
Listing\BulkActionController $bulk_action,
Listing\Handler $listing_handler,
@ -67,7 +71,8 @@ class Newsletters extends APIEndpoint {
SettingsController $settings,
AuthorizedEmailsController $authorized_emails_controller,
NewslettersRepository $newsletters_repository,
NewslettersResponseBuilder $newsletters_response_builder
NewslettersResponseBuilder $newsletters_response_builder,
PostNotificationScheduler $post_notification_scheduler
) {
$this->bulk_action = $bulk_action;
$this->listing_handler = $listing_handler;
@ -77,6 +82,7 @@ class Newsletters extends APIEndpoint {
$this->authorized_emails_controller = $authorized_emails_controller;
$this->newsletters_repository = $newsletters_repository;
$this->newsletters_response_builder = $newsletters_response_builder;
$this->post_notification_scheduler = $post_notification_scheduler;
}
function get($data = []) {
@ -208,7 +214,7 @@ class Newsletters extends APIEndpoint {
// if this is a post notification, process newsletter options and update its schedule
if ($newsletter->type === Newsletter::TYPE_NOTIFICATION) {
// generate the new schedule from options and get the new "next run" date
$newsletter->schedule = Scheduler::processPostNotificationSchedule($newsletter);
$newsletter->schedule = $this->post_notification_scheduler->processPostNotificationSchedule($newsletter);
$next_run_date = Scheduler::getNextRunDate($newsletter->schedule);
// find previously scheduled jobs and reschedule them using the new "next run" date
SendingQueue::findTaskByNewsletterId($newsletter->id)
@ -283,7 +289,7 @@ class Newsletters extends APIEndpoint {
->set('scheduled_at', $next_run_date)
->save();
}
Scheduler::createPostNotificationSendingTask($newsletter);
$this->post_notification_scheduler->createPostNotificationSendingTask($newsletter);
}
$newsletter = Newsletter::findOne($newsletter->id);
@ -601,7 +607,7 @@ class Newsletters extends APIEndpoint {
$data['type'] === Newsletter::TYPE_NOTIFICATION
) {
$newsletter = Newsletter::filter('filterWithOptions', $data['type'])->findOne($newsletter->id);
Scheduler::processPostNotificationSchedule($newsletter);
$this->post_notification_scheduler->processPostNotificationSchedule($newsletter);
}
$newsletter = Newsletter::findOne($newsletter->id);

View File

@ -2,6 +2,7 @@
namespace MailPoet\Config;
use MailPoet\Newsletter\Scheduler\PostNotificationScheduler;
use MailPoet\Settings\SettingsController;
use MailPoet\Statistics\Track\WooCommercePurchases;
use MailPoet\Subscription\Comment;
@ -41,6 +42,9 @@ class Hooks {
/** @var WooCommercePurchases */
private $woocommerce_purchases;
/** @var PostNotificationScheduler */
private $post_notification_scheduler;
function __construct(
Form $subscription_form,
Comment $subscription_comment,
@ -50,7 +54,8 @@ class Hooks {
WPFunctions $wp,
WooCommerceSubscription $woocommerce_subscription,
WooCommerceSegment $woocommerce_segment,
WooCommercePurchases $woocommerce_purchases
WooCommercePurchases $woocommerce_purchases,
PostNotificationScheduler $post_notification_scheduler
) {
$this->subscription_form = $subscription_form;
$this->subscription_comment = $subscription_comment;
@ -61,6 +66,7 @@ class Hooks {
$this->woocommerce_subscription = $woocommerce_subscription;
$this->woocommerce_segment = $woocommerce_segment;
$this->woocommerce_purchases = $woocommerce_purchases;
$this->post_notification_scheduler = $post_notification_scheduler;
}
function init() {
@ -299,7 +305,7 @@ class Hooks {
function setupPostNotifications() {
$this->wp->addAction(
'transition_post_status',
'\MailPoet\Newsletter\Scheduler\Scheduler::transitionHook',
[$this->post_notification_scheduler, 'transitionHook'],
10, 3
);
}

View File

@ -184,6 +184,7 @@ class ContainerConfigurator implements IContainerConfigurator {
$container->autowire(\MailPoet\Newsletter\AutomatedLatestContent::class)->setPublic(true);
$container->autowire(\MailPoet\Newsletter\NewslettersRepository::class);
$container->autowire(\MailPoet\Newsletter\Scheduler\WelcomeScheduler::class);
$container->autowire(\MailPoet\Newsletter\Scheduler\PostNotificationScheduler::class);
// Util
$container->autowire(\MailPoet\Util\Cookies::class);
$container->autowire(\MailPoet\Util\Url::class)->setPublic(true);

View File

@ -20,6 +20,7 @@ if (!defined('ABSPATH')) exit;
* @property string|object|null $meta
* @property string|array $subscribers
* @property string|null $deleted_at
* @property string $scheduled_at
*/
class SendingQueue extends Model {

View File

@ -0,0 +1,130 @@
<?php
namespace MailPoet\Newsletter\Scheduler;
use MailPoet\Logging\Logger;
use MailPoet\Models\Newsletter;
use MailPoet\Models\NewsletterOption;
use MailPoet\Models\NewsletterOptionField;
use MailPoet\Models\NewsletterPost;
use MailPoet\Models\ScheduledTask;
use MailPoet\Models\SendingQueue;
use MailPoet\Tasks\Sending as SendingTask;
use MailPoet\WP\Posts;
class PostNotificationScheduler {
function transitionHook($new_status, $old_status, $post) {
Logger::getLogger('post-notifications')->addInfo(
'transition post notification hook initiated',
[
'post_id' => $post->ID,
'new_status' => $new_status,
'old_status' => $old_status,
]
);
$types = Posts::getTypes();
if (($new_status !== 'publish') || !isset($types[$post->post_type])) {
return;
}
$this->schedulePostNotification($post->ID);
}
function schedulePostNotification($post_id) {
Logger::getLogger('post-notifications')->addInfo(
'schedule post notification hook',
['post_id' => $post_id]
);
$newsletters = Scheduler::getNewsletters(Newsletter::TYPE_NOTIFICATION);
if (!count($newsletters)) return false;
foreach ($newsletters as $newsletter) {
$post = NewsletterPost::where('newsletter_id', $newsletter->id)
->where('post_id', $post_id)
->findOne();
if ($post === false) {
$this->createPostNotificationSendingTask($newsletter);
}
}
}
function createPostNotificationSendingTask($newsletter) {
$existing_notification_history = Newsletter::tableAlias('newsletters')
->where('newsletters.parent_id', $newsletter->id)
->where('newsletters.type', Newsletter::TYPE_NOTIFICATION_HISTORY)
->where('newsletters.status', Newsletter::STATUS_SENDING)
->join(
MP_SENDING_QUEUES_TABLE,
'queues.newsletter_id = newsletters.id',
'queues'
)
->join(
MP_SCHEDULED_TASKS_TABLE,
'queues.task_id = tasks.id',
'tasks'
)
->whereNotEqual('tasks.status', ScheduledTask::STATUS_PAUSED)
->findOne();
if ($existing_notification_history) {
return;
}
$next_run_date = Scheduler::getNextRunDate($newsletter->schedule);
if (!$next_run_date) return;
// do not schedule duplicate queues for the same time
$existing_queue = SendingQueue::findTaskByNewsletterId($newsletter->id)
->where('tasks.scheduled_at', $next_run_date)
->findOne();
if ($existing_queue) return;
$sending_task = SendingTask::create();
$sending_task->newsletter_id = $newsletter->id;
$sending_task->status = SendingQueue::STATUS_SCHEDULED;
$sending_task->scheduled_at = $next_run_date;
$sending_task->save();
Logger::getLogger('post-notifications')->addInfo(
'schedule post notification',
['sending_task' => $sending_task->id(), 'scheduled_at' => $next_run_date]
);
return $sending_task;
}
function processPostNotificationSchedule($newsletter) {
$interval_type = $newsletter->intervalType;
$hour = (int)$newsletter->timeOfDay / Scheduler::SECONDS_IN_HOUR;
$week_day = $newsletter->weekDay;
$month_day = $newsletter->monthDay;
$nth_week_day = ($newsletter->nthWeekDay === Scheduler::LAST_WEEKDAY_FORMAT) ?
$newsletter->nthWeekDay :
'#' . $newsletter->nthWeekDay;
switch ($interval_type) {
case Scheduler::INTERVAL_IMMEDIATE:
case Scheduler::INTERVAL_DAILY:
$schedule = sprintf('0 %s * * *', $hour);
break;
case Scheduler::INTERVAL_WEEKLY:
$schedule = sprintf('0 %s * * %s', $hour, $week_day);
break;
case Scheduler::INTERVAL_NTHWEEKDAY:
$schedule = sprintf('0 %s ? * %s%s', $hour, $week_day, $nth_week_day);
break;
case Scheduler::INTERVAL_MONTHLY:
$schedule = sprintf('0 %s %s * *', $hour, $month_day);
break;
case Scheduler::INTERVAL_IMMEDIATELY:
default:
$schedule = '* * * * *';
break;
}
$option_field = NewsletterOptionField::where('name', 'schedule')->findOne();
$relation = NewsletterOption::where('newsletter_id', $newsletter->id)
->where('option_field_id', $option_field->id)
->findOne();
if (!$relation) {
$relation = NewsletterOption::create();
$relation->newsletter_id = $newsletter->id;
$relation->option_field_id = $option_field->id;
}
$relation->value = $schedule;
$relation->save();
return $relation->value;
}
}

View File

@ -3,17 +3,12 @@
namespace MailPoet\Newsletter\Scheduler;
use Carbon\Carbon;
use MailPoet\Logging\Logger;
use MailPoet\Models\Newsletter;
use MailPoet\Models\NewsletterOption;
use MailPoet\Models\NewsletterOptionField;
use MailPoet\Models\NewsletterPost;
use MailPoet\Models\ScheduledTask;
use MailPoet\Models\ScheduledTaskSubscriber;
use MailPoet\Models\SendingQueue;
use MailPoet\Tasks\Sending as SendingTask;
use MailPoet\WP\Functions as WPFunctions;
use MailPoet\WP\Posts;
class Scheduler {
const SECONDS_IN_HOUR = 3600;
@ -26,39 +21,6 @@ class Scheduler {
const INTERVAL_MONTHLY = 'monthly';
const INTERVAL_NTHWEEKDAY = 'nthWeekDay';
static function transitionHook($new_status, $old_status, $post) {
Logger::getLogger('post-notifications')->addInfo(
'transition post notification hook initiated',
[
'post_id' => $post->ID,
'new_status' => $new_status,
'old_status' => $old_status,
]
);
$types = Posts::getTypes();
if (($new_status !== 'publish') || !isset($types[$post->post_type])) {
return;
}
self::schedulePostNotification($post->ID);
}
static function schedulePostNotification($post_id) {
Logger::getLogger('post-notifications')->addInfo(
'schedule post notification hook',
['post_id' => $post_id]
);
$newsletters = self::getNewsletters(Newsletter::TYPE_NOTIFICATION);
if (!count($newsletters)) return false;
foreach ($newsletters as $newsletter) {
$post = NewsletterPost::where('newsletter_id', $newsletter->id)
->where('post_id', $post_id)
->findOne();
if ($post === false) {
self::createPostNotificationSendingTask($newsletter);
}
}
}
static function scheduleAutomaticEmail($group, $event, $scheduling_condition = false, $subscriber_id = false, $meta = false) {
$newsletters = self::getNewsletters(Newsletter::TYPE_AUTOMATIC, $group);
if (empty($newsletters)) return false;
@ -152,86 +114,6 @@ class Scheduler {
$task->save();
}
static function createPostNotificationSendingTask($newsletter) {
$existing_notification_history = Newsletter::tableAlias('newsletters')
->where('newsletters.parent_id', $newsletter->id)
->where('newsletters.type', Newsletter::TYPE_NOTIFICATION_HISTORY)
->where('newsletters.status', Newsletter::STATUS_SENDING)
->join(
MP_SENDING_QUEUES_TABLE,
'queues.newsletter_id = newsletters.id',
'queues'
)
->join(
MP_SCHEDULED_TASKS_TABLE,
'queues.task_id = tasks.id',
'tasks'
)
->whereNotEqual('tasks.status', ScheduledTask::STATUS_PAUSED)
->findOne();
if ($existing_notification_history) {
return;
}
$next_run_date = self::getNextRunDate($newsletter->schedule);
if (!$next_run_date) return;
// do not schedule duplicate queues for the same time
$existing_queue = SendingQueue::findTaskByNewsletterId($newsletter->id)
->where('tasks.scheduled_at', $next_run_date)
->findOne();
if ($existing_queue) return;
$sending_task = SendingTask::create();
$sending_task->newsletter_id = $newsletter->id;
$sending_task->status = SendingQueue::STATUS_SCHEDULED;
$sending_task->scheduled_at = $next_run_date;
$sending_task->save();
Logger::getLogger('post-notifications')->addInfo(
'schedule post notification',
['sending_task' => $sending_task->id(), 'scheduled_at' => $next_run_date]
);
return $sending_task;
}
static function processPostNotificationSchedule($newsletter) {
$interval_type = $newsletter->intervalType;
$hour = (int)$newsletter->timeOfDay / self::SECONDS_IN_HOUR;
$week_day = $newsletter->weekDay;
$month_day = $newsletter->monthDay;
$nth_week_day = ($newsletter->nthWeekDay === self::LAST_WEEKDAY_FORMAT) ?
$newsletter->nthWeekDay :
'#' . $newsletter->nthWeekDay;
switch ($interval_type) {
case self::INTERVAL_IMMEDIATE:
case self::INTERVAL_DAILY:
$schedule = sprintf('0 %s * * *', $hour);
break;
case self::INTERVAL_WEEKLY:
$schedule = sprintf('0 %s * * %s', $hour, $week_day);
break;
case self::INTERVAL_NTHWEEKDAY:
$schedule = sprintf('0 %s ? * %s%s', $hour, $week_day, $nth_week_day);
break;
case self::INTERVAL_MONTHLY:
$schedule = sprintf('0 %s %s * *', $hour, $month_day);
break;
case self::INTERVAL_IMMEDIATELY:
default:
$schedule = '* * * * *';
break;
}
$option_field = NewsletterOptionField::where('name', 'schedule')->findOne();
$relation = NewsletterOption::where('newsletter_id', $newsletter->id)
->where('option_field_id', $option_field->id)
->findOne();
if (!$relation) {
$relation = NewsletterOption::create();
$relation->newsletter_id = $newsletter->id;
$relation->option_field_id = $option_field->id;
}
$relation->value = $schedule;
$relation->save();
return $relation->value;
}
static function getNextRunDate($schedule, $from_timestamp = false) {
$wp = new WPFunctions();
$from_timestamp = ($from_timestamp) ? $from_timestamp : $wp->currentTime('timestamp');

View File

@ -22,6 +22,7 @@ use MailPoet\Models\Segment;
use MailPoet\Models\SubscriberSegment;
use MailPoet\Models\SendingQueue;
use MailPoet\Newsletter\NewslettersRepository;
use MailPoet\Newsletter\Scheduler\PostNotificationScheduler;
use MailPoet\Newsletter\Scheduler\Scheduler;
use MailPoet\Newsletter\Url;
use MailPoet\Router\Router;
@ -130,7 +131,8 @@ class NewslettersTest extends \MailPoetTest {
new SettingsController(),
$this->make(AuthorizedEmailsController::class, ['onNewsletterUpdate' => Expected::never()]),
ContainerWrapper::getInstance()->get(NewslettersRepository::class),
ContainerWrapper::getInstance()->get(NewslettersResponseBuilder::class)
ContainerWrapper::getInstance()->get(NewslettersResponseBuilder::class),
ContainerWrapper::getInstance()->get(PostNotificationScheduler::class)
);
$response = $this->endpoint->get(['id' => $this->newsletter->id]);
expect($response->status)->equals(APIResponse::STATUS_OK);
@ -172,7 +174,8 @@ class NewslettersTest extends \MailPoetTest {
new SettingsController(),
$this->make(AuthorizedEmailsController::class, ['onNewsletterUpdate' => Expected::once()]),
ContainerWrapper::getInstance()->get(NewslettersRepository::class),
ContainerWrapper::getInstance()->get(NewslettersResponseBuilder::class)
ContainerWrapper::getInstance()->get(NewslettersResponseBuilder::class),
ContainerWrapper::getInstance()->get(PostNotificationScheduler::class)
);
$response = $this->endpoint->save($valid_data);
@ -542,7 +545,8 @@ class NewslettersTest extends \MailPoetTest {
new SettingsController(),
$this->make(AuthorizedEmailsController::class, ['onNewsletterUpdate' => Expected::never()]),
ContainerWrapper::getInstance()->get(NewslettersRepository::class),
ContainerWrapper::getInstance()->get(NewslettersResponseBuilder::class)
ContainerWrapper::getInstance()->get(NewslettersResponseBuilder::class),
ContainerWrapper::getInstance()->get(PostNotificationScheduler::class)
);
$response = $this->endpoint->duplicate(['id' => $this->newsletter->id]);

View File

@ -8,6 +8,6 @@ class HooksTest extends \MailPoetTest {
function testItHooksSchedulerToMultiplePostTypes() {
$hooks = ContainerWrapper::getInstance()->get(Hooks::class);
$hooks->setupPostNotifications();
expect(has_filter('transition_post_status', '\MailPoet\Newsletter\Scheduler\Scheduler::transitionHook'))->notEmpty();
expect(has_filter('transition_post_status'))->notEmpty();
}
}

View File

@ -0,0 +1,345 @@
<?php
namespace MailPoet\Newsletter\Scheduler;
use Carbon\Carbon;
use MailPoet\Config\Hooks;
use MailPoet\DI\ContainerWrapper;
use MailPoet\Models\Newsletter;
use MailPoet\Models\NewsletterOption;
use MailPoet\Models\NewsletterOptionField;
use MailPoet\Models\NewsletterPost;
use MailPoet\Models\ScheduledTask;
use MailPoet\Models\ScheduledTaskSubscriber;
use MailPoet\Models\SendingQueue;
use MailPoet\Models\Subscriber;
use MailPoet\Tasks\Sending as SendingTask;
use MailPoet\WP\Posts as WPPosts;
class PostNotificationTest extends \MailPoetTest {
/** @var PostNotificationScheduler */
private $post_notification_scheduler;
function _before() {
parent::_before();
$this->post_notification_scheduler = new PostNotificationScheduler;
}
function testItCreatesPostNotificationSendingTask() {
$newsletter = $this->_createNewsletter();
$newsletter->schedule = '* 5 * * *';
// new queue record should be created
$queue = $this->post_notification_scheduler->createPostNotificationSendingTask($newsletter);
expect(SendingQueue::findMany())->count(1);
expect($queue->newsletter_id)->equals($newsletter->id);
expect($queue->status)->equals(SendingQueue::STATUS_SCHEDULED);
expect($queue->scheduled_at)->equals(Scheduler::getNextRunDate('* 5 * * *'));
expect($queue->priority)->equals(SendingQueue::PRIORITY_MEDIUM);
// duplicate queue record should not be created
$this->post_notification_scheduler->createPostNotificationSendingTask($newsletter);
expect(SendingQueue::findMany())->count(1);
}
function testItCreatesPostNotificationSendingTaskIfAPausedNotificationExists() {
$newsletter = $this->_createNewsletter();
$newsletter->schedule = '* 5 * * *';
// new queue record should be created
$queue_to_be_paused = $this->post_notification_scheduler->createPostNotificationSendingTask($newsletter);
$queue_to_be_paused->task()->pause();
// another queue record should be created because the first one was paused
$newsletter->schedule = '* 10 * * *'; // different time to not clash with the first queue
$queue = $this->post_notification_scheduler->createPostNotificationSendingTask($newsletter);
expect(SendingQueue::findMany())->count(2);
expect($queue->newsletter_id)->equals($newsletter->id);
expect($queue->status)->equals(SendingQueue::STATUS_SCHEDULED);
expect($queue->scheduled_at)->equals(Scheduler::getNextRunDate('* 10 * * *'));
expect($queue->priority)->equals(SendingQueue::PRIORITY_MEDIUM);
// duplicate queue record should not be created
$this->post_notification_scheduler->createPostNotificationSendingTask($newsletter);
expect(SendingQueue::findMany())->count(2);
}
function tesIttDoesNotSchedulePostNotificationWhenNotificationWasAlreadySentForPost() {
$newsletter = $this->_createNewsletter();
$newsletter_post = NewsletterPost::create();
$newsletter_post->newsletter_id = $newsletter->id;
$newsletter_post->post_id = 10;
$newsletter_post->save();
// queue is not created when notification was already sent for the post
$this->post_notification_scheduler->schedulePostNotification($post_id = 10);
$queue = SendingQueue::findTaskByNewsletterId($newsletter->id)
->findOne();
expect($queue)->false();
}
function testItSchedulesPostNotification() {
$newsletter = $this->_createNewsletter();
$this->_createNewsletterOptions(
$newsletter->id,
[
'schedule' => '0 5 * * *',
]
);
// queue is created and scheduled for delivery one day later at 5 a.m.
$this->post_notification_scheduler->schedulePostNotification($post_id = 10);
$current_time = Carbon::createFromTimestamp(current_time('timestamp'));
Carbon::setTestNow($current_time); // mock carbon to return current time
$next_run_date = ($current_time->hour < 5) ?
$current_time :
$current_time->addDay();
$queue = SendingQueue::findTaskByNewsletterId($newsletter->id)
->findOne();
expect($queue->scheduled_at)->startsWith($next_run_date->format('Y-m-d 05:00'));
}
function testItProcessesPostNotificationScheduledForDailyDelivery() {
$newsletter_option_field = NewsletterOptionField::create();
$newsletter_option_field->name = 'schedule';
$newsletter_option_field->newsletter_type = Newsletter::TYPE_NOTIFICATION;
$newsletter_option_field->save();
// daily notification is scheduled at 14:00
$newsletter = (object)[
'id' => 1,
'intervalType' => Scheduler::INTERVAL_DAILY,
'monthDay' => null,
'nthWeekDay' => null,
'weekDay' => null,
'timeOfDay' => 50400, // 2 p.m.
];
$this->post_notification_scheduler->processPostNotificationSchedule($newsletter);
$newsletter_option = NewsletterOption::where('newsletter_id', $newsletter->id)
->where('option_field_id', $newsletter_option_field->id)
->findOne();
$current_time = 1483275600; // Sunday, 1 January 2017 @ 1:00pm (UTC)
expect(Scheduler::getNextRunDate($newsletter_option->value, $current_time))
->equals('2017-01-01 14:00:00');
}
function testItProcessesPostNotificationScheduledForWeeklyDelivery() {
$newsletter_option_field = NewsletterOptionField::create();
$newsletter_option_field->name = 'schedule';
$newsletter_option_field->newsletter_type = Newsletter::TYPE_NOTIFICATION;
$newsletter_option_field->save();
// weekly notification is scheduled every Tuesday at 14:00
$newsletter = (object)[
'id' => 1,
'intervalType' => Scheduler::INTERVAL_WEEKLY,
'monthDay' => null,
'nthWeekDay' => null,
'weekDay' => Carbon::TUESDAY,
'timeOfDay' => 50400, // 2 p.m.
];
$this->post_notification_scheduler->processPostNotificationSchedule($newsletter);
$newsletter_option = NewsletterOption::where('newsletter_id', $newsletter->id)
->where('option_field_id', $newsletter_option_field->id)
->findOne();
$current_time = 1483275600; // Sunday, 1 January 2017 @ 1:00pm (UTC)
expect(Scheduler::getNextRunDate($newsletter_option->value, $current_time))
->equals('2017-01-03 14:00:00');
}
function testItProcessesPostNotificationScheduledForMonthlyDeliveryOnSpecificDay() {
$newsletter_option_field = NewsletterOptionField::create();
$newsletter_option_field->name = 'schedule';
$newsletter_option_field->newsletter_type = Newsletter::TYPE_NOTIFICATION;
$newsletter_option_field->save();
// monthly notification is scheduled every 20th day at 14:00
$newsletter = (object)[
'id' => 1,
'intervalType' => Scheduler::INTERVAL_MONTHLY,
'monthDay' => 19, // 20th (count starts from 0)
'nthWeekDay' => null,
'weekDay' => null,
'timeOfDay' => 50400,// 2 p.m.
];
$this->post_notification_scheduler->processPostNotificationSchedule($newsletter);
$newsletter_option = NewsletterOption::where('newsletter_id', $newsletter->id)
->where('option_field_id', $newsletter_option_field->id)
->findOne();
$current_time = 1483275600; // Sunday, 1 January 2017 @ 1:00pm (UTC)
expect(Scheduler::getNextRunDate($newsletter_option->value, $current_time))
->equals('2017-01-19 14:00:00');
}
function testItProcessesPostNotificationScheduledForMonthlyDeliveryOnLastWeekDay() {
$newsletter_option_field = NewsletterOptionField::create();
$newsletter_option_field->name = 'schedule';
$newsletter_option_field->newsletter_type = Newsletter::TYPE_NOTIFICATION;
$newsletter_option_field->save();
// monthly notification is scheduled every last Saturday at 14:00
$newsletter = (object)[
'id' => 1,
'intervalType' => Scheduler::INTERVAL_NTHWEEKDAY,
'monthDay' => null,
'nthWeekDay' => 'L', // L = last
'weekDay' => Carbon::SATURDAY,
'timeOfDay' => 50400,// 2 p.m.
];
$this->post_notification_scheduler->processPostNotificationSchedule($newsletter);
$newsletter_option = NewsletterOption::where('newsletter_id', $newsletter->id)
->where('option_field_id', $newsletter_option_field->id)
->findOne();
$current_time = 1485694800; // Sunday, 29 January 2017 @ 1:00pm (UTC)
expect(Scheduler::getNextRunDate($newsletter_option->value, $current_time))
->equals('2017-02-25 14:00:00');
}
function testItProcessesPostNotificationScheduledForImmediateDelivery() {
$newsletter_option_field = NewsletterOptionField::create();
$newsletter_option_field->name = 'schedule';
$newsletter_option_field->newsletter_type = Newsletter::TYPE_NOTIFICATION;
$newsletter_option_field->save();
// notification is scheduled immediately (next minute)
$newsletter = (object)[
'id' => 1,
'intervalType' => Scheduler::INTERVAL_IMMEDIATELY,
'monthDay' => null,
'nthWeekDay' => null,
'weekDay' => null,
'timeOfDay' => null,
];
$this->post_notification_scheduler->processPostNotificationSchedule($newsletter);
$newsletter_option = NewsletterOption::where('newsletter_id', $newsletter->id)
->where('option_field_id', $newsletter_option_field->id)
->findOne();
$current_time = 1483275600; // Sunday, 1 January 2017 @ 1:00pm (UTC)
expect(Scheduler::getNextRunDate($newsletter_option->value, $current_time))
->equals('2017-01-01 13:01:00');
}
function testUnsearchablePostTypeDoesNotSchedulePostNotification() {
$hook = ContainerWrapper::getInstance()->get(Hooks::class);
$newsletter = $this->_createNewsletter();
$this->_createNewsletterOptions(
$newsletter->id,
[
'intervalType' => Scheduler::INTERVAL_IMMEDIATELY,
'schedule' => '* * * * *',
]
);
$this->_removePostNotificationHooks();
register_post_type('post', ['exclude_from_search' => true]);
$hook->setupPostNotifications();
$post_data = [
'post_title' => 'title',
'post_status' => 'publish',
];
wp_insert_post($post_data);
$queue = SendingQueue::findTaskByNewsletterId($newsletter->id)->findOne();
expect($queue)->equals(false);
$this->_removePostNotificationHooks();
register_post_type('post', ['exclude_from_search' => false]);
$hook->setupPostNotifications();
wp_insert_post($post_data);
$queue = SendingQueue::findTaskByNewsletterId($newsletter->id)->findOne();
expect($queue)->notequals(false);
}
function testSchedulerWontRunIfUnsentNotificationHistoryExists() {
$newsletter = $this->_createNewsletter();
$this->_createNewsletterOptions(
$newsletter->id,
[
'intervalType' => Scheduler::INTERVAL_IMMEDIATELY,
'schedule' => '* * * * *',
]
);
$notification_history = Newsletter::create();
$notification_history->type = Newsletter::TYPE_NOTIFICATION_HISTORY;
$notification_history->status = Newsletter::STATUS_SENDING;
$notification_history->parent_id = $newsletter->id;
$notification_history->save();
$sending_task = SendingTask::create();
$sending_task->newsletter_id = $notification_history->id;
$sending_task->status = SendingQueue::STATUS_SCHEDULED;
$sending_task->save();
$post_data = [
'post_title' => 'title',
'post_status' => 'publish',
];
wp_insert_post($post_data);
$queue = SendingQueue::findTaskByNewsletterId($newsletter->id)->findOne();
expect($queue)->equals(false);
}
function _removePostNotificationHooks() {
foreach (WPPosts::getTypes() as $post_type) {
remove_filter(
'publish_' . $post_type,
[$this->post_notification_scheduler, 'transitionHook'],
10
);
}
}
function _createNewsletter() {
$newsletter = Newsletter::create();
$newsletter->type = Newsletter::TYPE_NOTIFICATION;
$newsletter->status = Newsletter::STATUS_ACTIVE;
$newsletter->save();
expect($newsletter->getErrors())->false();
return $newsletter;
}
function _createNewsletterOptions($newsletter_id, $options) {
foreach ($options as $option => $value) {
$newsletter_option_field = NewsletterOptionField::where('name', $option)->findOne();
if (!$newsletter_option_field) {
$newsletter_option_field = NewsletterOptionField::create();
$newsletter_option_field->name = $option;
$newsletter_option_field->newsletter_type = Newsletter::TYPE_NOTIFICATION;
$newsletter_option_field->save();
expect($newsletter_option_field->getErrors())->false();
}
$newsletter_option = NewsletterOption::create();
$newsletter_option->option_field_id = $newsletter_option_field->id;
$newsletter_option->newsletter_id = $newsletter_id;
$newsletter_option->value = $value;
$newsletter_option->save();
expect($newsletter_option->getErrors())->false();
}
}
function _after() {
Carbon::setTestNow();
\ORM::raw_execute('TRUNCATE ' . Newsletter::$_table);
\ORM::raw_execute('TRUNCATE ' . NewsletterOption::$_table);
\ORM::raw_execute('TRUNCATE ' . NewsletterOptionField::$_table);
\ORM::raw_execute('TRUNCATE ' . NewsletterPost::$_table);
\ORM::raw_execute('TRUNCATE ' . ScheduledTask::$_table);
\ORM::raw_execute('TRUNCATE ' . ScheduledTaskSubscriber::$_table);
\ORM::raw_execute('TRUNCATE ' . SendingQueue::$_table);
\ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
}
}

View File

@ -78,201 +78,6 @@ class SchedulerTest extends \MailPoetTest {
->equals('2016-04-20 16:00:00');
}
function testItCreatesPostNotificationSendingTask() {
$newsletter = $this->_createNewsletter();
$newsletter->schedule = '* 5 * * *';
// new queue record should be created
$queue = Scheduler::createPostNotificationSendingTask($newsletter);
expect(SendingQueue::findMany())->count(1);
expect($queue->newsletter_id)->equals($newsletter->id);
expect($queue->status)->equals(SendingQueue::STATUS_SCHEDULED);
expect($queue->scheduled_at)->equals(Scheduler::getNextRunDate('* 5 * * *'));
expect($queue->priority)->equals(SendingQueue::PRIORITY_MEDIUM);
// duplicate queue record should not be created
Scheduler::createPostNotificationSendingTask($newsletter);
expect(SendingQueue::findMany())->count(1);
}
function testItCreatesPostNotificationSendingTaskIfAPausedNotificationExists() {
$newsletter = $this->_createNewsletter();
$newsletter->schedule = '* 5 * * *';
// new queue record should be created
$queue_to_be_paused = Scheduler::createPostNotificationSendingTask($newsletter);
$queue_to_be_paused->task()->pause();
// another queue record should be created because the first one was paused
$newsletter->schedule = '* 10 * * *'; // different time to not clash with the first queue
$queue = Scheduler::createPostNotificationSendingTask($newsletter);
expect(SendingQueue::findMany())->count(2);
expect($queue->newsletter_id)->equals($newsletter->id);
expect($queue->status)->equals(SendingQueue::STATUS_SCHEDULED);
expect($queue->scheduled_at)->equals(Scheduler::getNextRunDate('* 10 * * *'));
expect($queue->priority)->equals(SendingQueue::PRIORITY_MEDIUM);
// duplicate queue record should not be created
Scheduler::createPostNotificationSendingTask($newsletter);
expect(SendingQueue::findMany())->count(2);
}
function tesIttDoesNotSchedulePostNotificationWhenNotificationWasAlreadySentForPost() {
$newsletter = $this->_createNewsletter();
$newsletter_post = NewsletterPost::create();
$newsletter_post->newsletter_id = $newsletter->id;
$newsletter_post->post_id = 10;
$newsletter_post->save();
// queue is not created when notification was already sent for the post
Scheduler::schedulePostNotification($post_id = 10);
$queue = SendingQueue::findTaskByNewsletterId($newsletter->id)
->findOne();
expect($queue)->false();
}
function testItSchedulesPostNotification() {
$newsletter = $this->_createNewsletter();
$this->_createNewsletterOptions(
$newsletter->id,
Newsletter::TYPE_NOTIFICATION,
[
'schedule' => '0 5 * * *',
]
);
// queue is created and scheduled for delivery one day later at 5 a.m.
Scheduler::schedulePostNotification($post_id = 10);
$current_time = Carbon::createFromTimestamp(current_time('timestamp'));
Carbon::setTestNow($current_time); // mock carbon to return current time
$next_run_date = ($current_time->hour < 5) ?
$current_time :
$current_time->addDay();
$queue = SendingQueue::findTaskByNewsletterId($newsletter->id)
->findOne();
expect($queue->scheduled_at)->startsWith($next_run_date->format('Y-m-d 05:00'));
}
function testItProcessesPostNotificationScheduledForDailyDelivery() {
$newsletter_option_field = NewsletterOptionField::create();
$newsletter_option_field->name = 'schedule';
$newsletter_option_field->newsletter_type = Newsletter::TYPE_NOTIFICATION;
$newsletter_option_field->save();
// daily notification is scheduled at 14:00
$newsletter = (object)[
'id' => 1,
'intervalType' => Scheduler::INTERVAL_DAILY,
'monthDay' => null,
'nthWeekDay' => null,
'weekDay' => null,
'timeOfDay' => 50400, // 2 p.m.
];
Scheduler::processPostNotificationSchedule($newsletter);
$newsletter_option = NewsletterOption::where('newsletter_id', $newsletter->id)
->where('option_field_id', $newsletter_option_field->id)
->findOne();
$current_time = 1483275600; // Sunday, 1 January 2017 @ 1:00pm (UTC)
expect(Scheduler::getNextRunDate($newsletter_option->value, $current_time))
->equals('2017-01-01 14:00:00');
}
function testItProcessesPostNotificationScheduledForWeeklyDelivery() {
$newsletter_option_field = NewsletterOptionField::create();
$newsletter_option_field->name = 'schedule';
$newsletter_option_field->newsletter_type = Newsletter::TYPE_NOTIFICATION;
$newsletter_option_field->save();
// weekly notification is scheduled every Tuesday at 14:00
$newsletter = (object)[
'id' => 1,
'intervalType' => Scheduler::INTERVAL_WEEKLY,
'monthDay' => null,
'nthWeekDay' => null,
'weekDay' => Carbon::TUESDAY,
'timeOfDay' => 50400, // 2 p.m.
];
Scheduler::processPostNotificationSchedule($newsletter);
$newsletter_option = NewsletterOption::where('newsletter_id', $newsletter->id)
->where('option_field_id', $newsletter_option_field->id)
->findOne();
$current_time = 1483275600; // Sunday, 1 January 2017 @ 1:00pm (UTC)
expect(Scheduler::getNextRunDate($newsletter_option->value, $current_time))
->equals('2017-01-03 14:00:00');
}
function testItProcessesPostNotificationScheduledForMonthlyDeliveryOnSpecificDay() {
$newsletter_option_field = NewsletterOptionField::create();
$newsletter_option_field->name = 'schedule';
$newsletter_option_field->newsletter_type = Newsletter::TYPE_NOTIFICATION;
$newsletter_option_field->save();
// monthly notification is scheduled every 20th day at 14:00
$newsletter = (object)[
'id' => 1,
'intervalType' => Scheduler::INTERVAL_MONTHLY,
'monthDay' => 19, // 20th (count starts from 0)
'nthWeekDay' => null,
'weekDay' => null,
'timeOfDay' => 50400,// 2 p.m.
];
Scheduler::processPostNotificationSchedule($newsletter);
$newsletter_option = NewsletterOption::where('newsletter_id', $newsletter->id)
->where('option_field_id', $newsletter_option_field->id)
->findOne();
$current_time = 1483275600; // Sunday, 1 January 2017 @ 1:00pm (UTC)
expect(Scheduler::getNextRunDate($newsletter_option->value, $current_time))
->equals('2017-01-19 14:00:00');
}
function testItProcessesPostNotificationScheduledForMonthlyDeliveryOnLastWeekDay() {
$newsletter_option_field = NewsletterOptionField::create();
$newsletter_option_field->name = 'schedule';
$newsletter_option_field->newsletter_type = Newsletter::TYPE_NOTIFICATION;
$newsletter_option_field->save();
// monthly notification is scheduled every last Saturday at 14:00
$newsletter = (object)[
'id' => 1,
'intervalType' => Scheduler::INTERVAL_NTHWEEKDAY,
'monthDay' => null,
'nthWeekDay' => 'L', // L = last
'weekDay' => Carbon::SATURDAY,
'timeOfDay' => 50400,// 2 p.m.
];
Scheduler::processPostNotificationSchedule($newsletter);
$newsletter_option = NewsletterOption::where('newsletter_id', $newsletter->id)
->where('option_field_id', $newsletter_option_field->id)
->findOne();
$current_time = 1485694800; // Sunday, 29 January 2017 @ 1:00pm (UTC)
expect(Scheduler::getNextRunDate($newsletter_option->value, $current_time))
->equals('2017-02-25 14:00:00');
}
function testItProcessesPostNotificationScheduledForImmediateDelivery() {
$newsletter_option_field = NewsletterOptionField::create();
$newsletter_option_field->name = 'schedule';
$newsletter_option_field->newsletter_type = Newsletter::TYPE_NOTIFICATION;
$newsletter_option_field->save();
// notification is scheduled immediately (next minute)
$newsletter = (object)[
'id' => 1,
'intervalType' => Scheduler::INTERVAL_IMMEDIATELY,
'monthDay' => null,
'nthWeekDay' => null,
'weekDay' => null,
'timeOfDay' => null,
];
Scheduler::processPostNotificationSchedule($newsletter);
$newsletter_option = NewsletterOption::where('newsletter_id', $newsletter->id)
->where('option_field_id', $newsletter_option_field->id)
->findOne();
$current_time = 1483275600; // Sunday, 1 January 2017 @ 1:00pm (UTC)
expect(Scheduler::getNextRunDate($newsletter_option->value, $current_time))
->equals('2017-01-01 13:01:00');
}
function testItCreatesScheduledAutomaticEmailSendingTaskForUser() {
$newsletter = $this->_createNewsletter(Newsletter::TYPE_AUTOMATIC);
$this->_createNewsletterOptions(
@ -439,91 +244,7 @@ class SchedulerTest extends \MailPoetTest {
->equals($current_time->addHours(2)->format('Y-m-d H:i'));
}
function testUnsearchablePostTypeDoesNotSchedulePostNotification() {
$hook = ContainerWrapper::getInstance()->get(Hooks::class);
$newsletter = $this->_createNewsletter(Newsletter::TYPE_NOTIFICATION);
$this->_createNewsletterOptions(
$newsletter->id,
Newsletter::TYPE_NOTIFICATION,
[
'intervalType' => Scheduler::INTERVAL_IMMEDIATELY,
'schedule' => '* * * * *',
]
);
$this->_removePostNotificationHooks();
register_post_type('post', ['exclude_from_search' => true]);
$hook->setupPostNotifications();
$post_data = [
'post_title' => 'title',
'post_status' => 'publish',
];
wp_insert_post($post_data);
$queue = SendingQueue::findTaskByNewsletterId($newsletter->id)->findOne();
expect($queue)->equals(false);
$this->_removePostNotificationHooks();
register_post_type('post', ['exclude_from_search' => false]);
$hook->setupPostNotifications();
wp_insert_post($post_data);
$queue = SendingQueue::findTaskByNewsletterId($newsletter->id)->findOne();
expect($queue)->notequals(false);
}
function testSchedulerWontRunIfUnsentNotificationHistoryExists() {
$newsletter = $this->_createNewsletter(Newsletter::TYPE_NOTIFICATION);
$this->_createNewsletterOptions(
$newsletter->id,
Newsletter::TYPE_NOTIFICATION,
[
'intervalType' => Scheduler::INTERVAL_IMMEDIATELY,
'schedule' => '* * * * *',
]
);
$notification_history = Newsletter::create();
$notification_history->type = Newsletter::TYPE_NOTIFICATION_HISTORY;
$notification_history->status = Newsletter::STATUS_SENDING;
$notification_history->parent_id = $newsletter->id;
$notification_history->save();
$sending_task = SendingTask::create();
$sending_task->newsletter_id = $notification_history->id;
$sending_task->status = SendingQueue::STATUS_SCHEDULED;
$sending_task->save();
$post_data = [
'post_title' => 'title',
'post_status' => 'publish',
];
wp_insert_post($post_data);
$queue = SendingQueue::findTaskByNewsletterId($newsletter->id)->findOne();
expect($queue)->equals(false);
}
function _createQueue(
$newsletter_id,
$scheduled_at = null,
$status = SendingQueue::STATUS_SCHEDULED
) {
$queue = SendingTask::create();
$queue->status = $status;
$queue->newsletter_id = $newsletter_id;
$queue->scheduled_at = $scheduled_at;
$queue->save();
expect($queue->getErrors())->false();
return $queue;
}
function _createNewsletter(
private function _createNewsletter(
$type = Newsletter::TYPE_NOTIFICATION,
$status = Newsletter::STATUS_ACTIVE
) {
@ -535,7 +256,7 @@ class SchedulerTest extends \MailPoetTest {
return $newsletter;
}
function _createNewsletterOptions($newsletter_id, $newsletter_type, $options) {
private function _createNewsletterOptions($newsletter_id, $newsletter_type, $options) {
foreach ($options as $option => $value) {
$newsletter_option_field = NewsletterOptionField::where('name', $option)->findOne();
if (!$newsletter_option_field) {
@ -555,16 +276,6 @@ class SchedulerTest extends \MailPoetTest {
}
}
function _removePostNotificationHooks() {
foreach (WPPosts::getTypes() as $post_type) {
remove_filter(
'publish_' . $post_type,
'\MailPoet\Newsletter\Scheduler\Scheduler::transitionHook',
10, 1
);
}
}
function _after() {
Carbon::setTestNow();
\ORM::raw_execute('TRUNCATE ' . Newsletter::$_table);