Use doctrine instead of Idiom in test for PostNotification

[MAILPOET-2946]
This commit is contained in:
Jan Lysý
2020-10-20 09:52:14 +02:00
committed by Veljko V
parent d5aac0778d
commit a15a74d94f
4 changed files with 215 additions and 179 deletions

View File

@ -616,8 +616,12 @@ parameters:
path: ../../tests/integration/Newsletter/Scheduler/AutomaticEmailTest.php path: ../../tests/integration/Newsletter/Scheduler/AutomaticEmailTest.php
- -
message: "#^Cannot access property \\$value on MailPoet\\\\Models\\\\NewsletterOption\\|false\\.$#" message: "#^Access to an undefined property MailPoet\\\\Tasks\\\\Sending\\:\\:\\$scheduledAt\\.$#"
count: 5 count: 2
path: ../../tests/integration/Newsletter/Scheduler/PostNotificationTest.php
-
message: "#^Access to an undefined property MailPoet\\\\Tasks\\\\Sending\\:\\:\\$priority\\.$#"
count: 2
path: ../../tests/integration/Newsletter/Scheduler/PostNotificationTest.php path: ../../tests/integration/Newsletter/Scheduler/PostNotificationTest.php
- -

View File

@ -5,6 +5,7 @@ namespace MailPoet\API\JSON\ResponseBuilders;
use Codeception\Util\Stub; use Codeception\Util\Stub;
use MailPoet\DI\ContainerWrapper; use MailPoet\DI\ContainerWrapper;
use MailPoet\Entities\NewsletterEntity; use MailPoet\Entities\NewsletterEntity;
use MailPoet\Newsletter\NewslettersRepository;
use MailPoet\Newsletter\Statistics\NewsletterStatistics; use MailPoet\Newsletter\Statistics\NewsletterStatistics;
use MailPoet\Newsletter\Statistics\NewsletterStatisticsRepository; use MailPoet\Newsletter\Statistics\NewsletterStatisticsRepository;
use MailPoetVendor\Doctrine\ORM\EntityManager; use MailPoetVendor\Doctrine\ORM\EntityManager;
@ -28,12 +29,13 @@ class NewslettersResponseBuilderTest extends \MailPoetTest {
'revenue' => null, 'revenue' => null,
], ],
]; ];
$repository = Stub::make(NewsletterStatisticsRepository::class, [ $newsletterStatsRepository = Stub::make(NewsletterStatisticsRepository::class, [
'getTotalSentCount' => $stats['total_sent'], 'getTotalSentCount' => $stats['total_sent'],
'getChildrenCount' => $stats['children_count'], 'getChildrenCount' => $stats['children_count'],
'getStatistics' => new NewsletterStatistics(4, 6, 2, 10, null), 'getStatistics' => new NewsletterStatistics(4, 6, 2, 10, null),
]); ]);
$responseBuilder = new NewslettersResponseBuilder($em, $repository); $newsletterRepository = Stub::make(NewslettersRepository::class);
$responseBuilder = new NewslettersResponseBuilder($em, $newsletterRepository, $newsletterStatsRepository);
$response = $responseBuilder->build($newsletter, [ $response = $responseBuilder->build($newsletter, [
NewslettersResponseBuilder::RELATION_CHILDREN_COUNT, NewslettersResponseBuilder::RELATION_CHILDREN_COUNT,
NewslettersResponseBuilder::RELATION_TOTAL_SENT, NewslettersResponseBuilder::RELATION_TOTAL_SENT,

View File

@ -62,6 +62,7 @@ class NewslettersTest extends \MailPoetTest {
[ [
'newslettersResponseBuilder' => new NewslettersResponseBuilder( 'newslettersResponseBuilder' => new NewslettersResponseBuilder(
$this->diContainer->get(EntityManager::class), $this->diContainer->get(EntityManager::class),
new NewslettersRepository($this->diContainer->get(EntityManager::class)),
new NewsletterStatisticsRepository( new NewsletterStatisticsRepository(
$this->diContainer->get(EntityManager::class), $this->diContainer->get(EntityManager::class),
$this->makeEmpty(WCHelper::class) $this->makeEmpty(WCHelper::class)

View File

@ -3,92 +3,129 @@
namespace MailPoet\Newsletter\Scheduler; namespace MailPoet\Newsletter\Scheduler;
use MailPoet\Config\Hooks; use MailPoet\Config\Hooks;
use MailPoet\DI\ContainerWrapper; use MailPoet\Entities\NewsletterEntity;
use MailPoet\Entities\NewsletterOptionEntity;
use MailPoet\Entities\NewsletterOptionFieldEntity;
use MailPoet\Entities\NewsletterPostEntity;
use MailPoet\Entities\ScheduledTaskEntity;
use MailPoet\Entities\ScheduledTaskSubscriberEntity;
use MailPoet\Entities\SendingQueueEntity;
use MailPoet\Entities\SubscriberEntity;
use MailPoet\Models\Newsletter; 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\SendingQueue;
use MailPoet\Models\Subscriber; use MailPoet\Newsletter\NewsletterPostsRepository;
use MailPoet\Newsletter\NewslettersRepository;
use MailPoet\Newsletter\Options\NewsletterOptionFieldsRepository;
use MailPoet\Newsletter\Options\NewsletterOptionsRepository;
use MailPoet\Tasks\Sending as SendingTask; use MailPoet\Tasks\Sending as SendingTask;
use MailPoet\WP\Functions as WPFunctions; use MailPoet\WP\Functions as WPFunctions;
use MailPoet\WP\Posts as WPPosts; use MailPoet\WP\Posts as WPPosts;
use MailPoetVendor\Carbon\Carbon; use MailPoetVendor\Carbon\Carbon;
use MailPoetVendor\Idiorm\ORM;
class PostNotificationTest extends \MailPoetTest { class PostNotificationTest extends \MailPoetTest {
/** @var PostNotificationScheduler */ /** @var PostNotificationScheduler */
private $postNotificationScheduler; private $postNotificationScheduler;
/** @var NewslettersRepository */
private $newslettersRepository;
/** @var NewsletterPostsRepository */
private $newsletterPostsRepository;
/** @var NewsletterOptionsRepository */
private $newsletterOptionsRepository;
/** @var NewsletterOptionFieldsRepository */
private $newsletterOptionFieldsRepository;
/** @var Hooks */
private $hooks;
public function _before() { public function _before() {
parent::_before(); parent::_before();
$this->postNotificationScheduler = new PostNotificationScheduler; $this->postNotificationScheduler = $this->diContainer->get(PostNotificationScheduler::class);
$this->newslettersRepository = $this->diContainer->get(NewslettersRepository::class);
$this->newsletterPostsRepository = $this->diContainer->get(NewsletterPostsRepository::class);
$this->newsletterOptionsRepository = $this->diContainer->get(NewsletterOptionsRepository::class);
$this->newsletterOptionFieldsRepository = $this->diContainer->get(NewsletterOptionFieldsRepository::class);
$this->hooks = $this->diContainer->get(Hooks::class);
} }
public function testItCreatesPostNotificationSendingTask() { public function testItCreatesPostNotificationSendingTask() {
$newsletter = $this->_createNewsletter(); $newsletter = $this->createNewsletter();
$newsletter->schedule = '* 5 * * *'; $this->createNewsletterOptions($newsletter, [
NewsletterOptionFieldEntity::NAME_SCHEDULE => '* 5 * * *',
]);
// new queue record should be created // new queue record should be created
$queue = $this->postNotificationScheduler->createPostNotificationSendingTask($newsletter); $queue = $this->postNotificationScheduler->createPostNotificationSendingTask($newsletter);
expect(SendingQueue::findMany())->count(1); assert($queue instanceof SendingTask);
expect($queue->newsletterId)->equals($newsletter->id); expect(SendingQueue::where('newsletter_id', $newsletter->getId())->findMany())->count(1);
expect($queue->newsletterId)->equals($newsletter->getId());
expect($queue->status)->equals(SendingQueue::STATUS_SCHEDULED); expect($queue->status)->equals(SendingQueue::STATUS_SCHEDULED);
expect($queue->scheduledAt)->equals(Scheduler::getNextRunDate('* 5 * * *')); expect($queue->scheduledAt)->equals(Scheduler::getNextRunDate('* 5 * * *'));
expect($queue->priority)->equals(SendingQueue::PRIORITY_MEDIUM); expect($queue->priority)->equals(SendingQueue::PRIORITY_MEDIUM);
// duplicate queue record should not be created // duplicate queue record should not be created
$newsletterId = $newsletter->getId();
$this->entityManager->clear();
$newsletter = $this->newslettersRepository->findOneById($newsletterId);
assert($newsletter instanceof NewsletterEntity);
$this->postNotificationScheduler->createPostNotificationSendingTask($newsletter); $this->postNotificationScheduler->createPostNotificationSendingTask($newsletter);
expect(SendingQueue::findMany())->count(1); expect(SendingQueue::where('newsletter_id', $newsletter->getId())->findMany())->count(1);
} }
public function testItCreatesPostNotificationSendingTaskIfAPausedNotificationExists() { public function testItCreatesPostNotificationSendingTaskIfAPausedNotificationExists() {
$newsletter = $this->_createNewsletter(); $newsletter = $this->createNewsletter();
$newsletter->schedule = '* 5 * * *'; $this->createNewsletterOptions($newsletter, [
NewsletterOptionFieldEntity::NAME_SCHEDULE => '* 5 * * *',
]);
// new queue record should be created // new queue record should be created
$queueToBePaused = $this->postNotificationScheduler->createPostNotificationSendingTask($newsletter); $queueToBePaused = $this->postNotificationScheduler->createPostNotificationSendingTask($newsletter);
assert($queueToBePaused instanceof SendingTask);
$queueToBePaused->task()->pause(); $queueToBePaused->task()->pause();
// another queue record should be created because the first one was paused // another queue record should be created because the first one was paused
$newsletter->schedule = '* 10 * * *'; // different time to not clash with the first queue $scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
assert($scheduleOption instanceof NewsletterOptionEntity);
$scheduleOption->setValue('* 10 * * *'); // different time to not clash with the first queue
$this->newsletterOptionsRepository->flush();
$queue = $this->postNotificationScheduler->createPostNotificationSendingTask($newsletter); $queue = $this->postNotificationScheduler->createPostNotificationSendingTask($newsletter);
expect(SendingQueue::findMany())->count(2); assert($queue instanceof SendingTask);
expect($queue->newsletterId)->equals($newsletter->id); expect(SendingQueue::where('newsletter_id', $newsletter->getId())->findMany())->count(2);
expect($queue->newsletterId)->equals($newsletter->getId());
expect($queue->status)->equals(SendingQueue::STATUS_SCHEDULED); expect($queue->status)->equals(SendingQueue::STATUS_SCHEDULED);
expect($queue->scheduledAt)->equals(Scheduler::getNextRunDate('* 10 * * *')); expect($queue->scheduledAt)->equals(Scheduler::getNextRunDate('* 10 * * *'));
expect($queue->priority)->equals(SendingQueue::PRIORITY_MEDIUM); expect($queue->priority)->equals(SendingQueue::PRIORITY_MEDIUM);
// duplicate queue record should not be created // duplicate queue record should not be created
$newsletterId = $newsletter->getId();
$this->entityManager->clear();
$newsletter = $this->newslettersRepository->findOneById($newsletterId);
assert($newsletter instanceof NewsletterEntity);
$this->postNotificationScheduler->createPostNotificationSendingTask($newsletter); $this->postNotificationScheduler->createPostNotificationSendingTask($newsletter);
expect(SendingQueue::findMany())->count(2); expect(SendingQueue::where('newsletter_id', $newsletter->getId())->findMany())->count(2);
} }
public function tesIttDoesNotSchedulePostNotificationWhenNotificationWasAlreadySentForPost() { public function testItDoesNotSchedulePostNotificationWhenNotificationWasAlreadySentForPost() {
$newsletter = $this->_createNewsletter(); $postId = 10;
$newsletterPost = NewsletterPost::create(); $newsletter = $this->createNewsletter();
$newsletterPost->newsletterId = $newsletter->id; $newsletterPost = $this->createPost($newsletter, $postId);
$newsletterPost->postId = 10;
$newsletterPost->save();
// queue is not created when notification was already sent for the post // queue is not created when notification was already sent for the post
$this->postNotificationScheduler->schedulePostNotification($postId = 10); $this->postNotificationScheduler->schedulePostNotification($postId);
$queue = SendingQueue::findTaskByNewsletterId($newsletter->id) $queue = SendingQueue::findTaskByNewsletterId($newsletter->getId())
->findOne(); ->findOne();
expect($queue)->false(); expect($queue)->false();
} }
public function testItSchedulesPostNotification() { public function testItSchedulesPostNotification() {
$newsletter = $this->_createNewsletter(); $newsletter = $this->createNewsletter();
$this->_createNewsletterOptions( $this->createNewsletterOptions($newsletter, [
$newsletter->id, NewsletterOptionFieldEntity::NAME_SCHEDULE => '0 5 * * *',
[ ]);
'schedule' => '0 5 * * *',
]
);
// queue is created and scheduled for delivery one day later at 5 a.m. // queue is created and scheduled for delivery one day later at 5 a.m.
$this->postNotificationScheduler->schedulePostNotification($postId = 10); $this->postNotificationScheduler->schedulePostNotification($postId = 10);
@ -97,147 +134,127 @@ class PostNotificationTest extends \MailPoetTest {
$nextRunDate = ($currentTime->hour < 5) ? $nextRunDate = ($currentTime->hour < 5) ?
$currentTime : $currentTime :
$currentTime->addDay(); $currentTime->addDay();
$queue = SendingQueue::findTaskByNewsletterId($newsletter->id) $queue = SendingQueue::findTaskByNewsletterId($newsletter->getId())
->findOne(); ->findOne();
expect($queue->scheduledAt)->startsWith($nextRunDate->format('Y-m-d 05:00')); expect($queue->scheduledAt)->startsWith($nextRunDate->format('Y-m-d 05:00'));
} }
public function testItProcessesPostNotificationScheduledForDailyDelivery() { public function testItProcessesPostNotificationScheduledForDailyDelivery() {
$newsletterOptionField = NewsletterOptionField::create(); $newsletter = $this->createNewsletter();
$newsletterOptionField->name = 'schedule'; $this->createNewsletterOptions($newsletter, [
$newsletterOptionField->newsletterType = Newsletter::TYPE_NOTIFICATION; NewsletterOptionFieldEntity::NAME_INTERVAL_TYPE => PostNotificationScheduler::INTERVAL_DAILY,
$newsletterOptionField->save(); NewsletterOptionFieldEntity::NAME_MONTH_DAY => null,
NewsletterOptionFieldEntity::NAME_NTH_WEEK_DAY => null,
NewsletterOptionFieldEntity::NAME_WEK_DAY => null,
NewsletterOptionFieldEntity::NAME_TIME_OF_DAY => 50400, // 2 p.m.
NewsletterOptionFieldEntity::NAME_SCHEDULE => '* * * * *',
]);
// daily notification is scheduled at 14:00
$newsletter = (object)[
'id' => 1,
'intervalType' => PostNotificationScheduler::INTERVAL_DAILY,
'monthDay' => null,
'nthWeekDay' => null,
'weekDay' => null,
'timeOfDay' => 50400, // 2 p.m.
];
$this->postNotificationScheduler->processPostNotificationSchedule($newsletter); $this->postNotificationScheduler->processPostNotificationSchedule($newsletter);
$newsletterOption = NewsletterOption::where('newsletter_id', $newsletter->id)
->where('option_field_id', $newsletterOptionField->id) $scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
->findOne(); assert($scheduleOption instanceof NewsletterOptionEntity);
$currentTime = 1483275600; // Sunday, 1 January 2017 @ 1:00pm (UTC) $currentTime = 1483275600; // Sunday, 1 January 2017 @ 1:00pm (UTC)
expect(Scheduler::getNextRunDate($newsletterOption->value, $currentTime)) expect(Scheduler::getNextRunDate($scheduleOption->getValue(), $currentTime))
->equals('2017-01-01 14:00:00'); ->equals('2017-01-01 14:00:00');
} }
public function testItProcessesPostNotificationScheduledForWeeklyDelivery() { public function testItProcessesPostNotificationScheduledForWeeklyDelivery() {
$newsletterOptionField = NewsletterOptionField::create(); $newsletter = $this->createNewsletter();
$newsletterOptionField->name = 'schedule';
$newsletterOptionField->newsletterType = Newsletter::TYPE_NOTIFICATION;
$newsletterOptionField->save();
// weekly notification is scheduled every Tuesday at 14:00 // weekly notification is scheduled every Tuesday at 14:00
$newsletter = (object)[ $this->createNewsletterOptions($newsletter, [
'id' => 1, NewsletterOptionFieldEntity::NAME_INTERVAL_TYPE => PostNotificationScheduler::INTERVAL_WEEKLY,
'intervalType' => PostNotificationScheduler::INTERVAL_WEEKLY, NewsletterOptionFieldEntity::NAME_MONTH_DAY => null,
'monthDay' => null, NewsletterOptionFieldEntity::NAME_NTH_WEEK_DAY => null,
'nthWeekDay' => null, NewsletterOptionFieldEntity::NAME_WEK_DAY => Carbon::TUESDAY,
'weekDay' => Carbon::TUESDAY, NewsletterOptionFieldEntity::NAME_TIME_OF_DAY => 50400, // 2 p.m.
'timeOfDay' => 50400, // 2 p.m. NewsletterOptionFieldEntity::NAME_SCHEDULE => '* * * * *',
]; ]);
$this->postNotificationScheduler->processPostNotificationSchedule($newsletter); $this->postNotificationScheduler->processPostNotificationSchedule($newsletter);
$newsletterOption = NewsletterOption::where('newsletter_id', $newsletter->id)
->where('option_field_id', $newsletterOptionField->id) $scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
->findOne(); assert($scheduleOption instanceof NewsletterOptionEntity);
$currentTime = 1483275600; // Sunday, 1 January 2017 @ 1:00pm (UTC) $currentTime = 1483275600; // Sunday, 1 January 2017 @ 1:00pm (UTC)
expect(Scheduler::getNextRunDate($newsletterOption->value, $currentTime)) expect(Scheduler::getNextRunDate($scheduleOption->getValue(), $currentTime))
->equals('2017-01-03 14:00:00'); ->equals('2017-01-03 14:00:00');
} }
public function testItProcessesPostNotificationScheduledForMonthlyDeliveryOnSpecificDay() { public function testItProcessesPostNotificationScheduledForMonthlyDeliveryOnSpecificDay() {
$newsletterOptionField = NewsletterOptionField::create(); $newsletter = $this->createNewsletter();
$newsletterOptionField->name = 'schedule';
$newsletterOptionField->newsletterType = Newsletter::TYPE_NOTIFICATION;
$newsletterOptionField->save();
// monthly notification is scheduled every 20th day at 14:00 // monthly notification is scheduled every 20th day at 14:00
$newsletter = (object)[ $this->createNewsletterOptions($newsletter, [
'id' => 1, NewsletterOptionFieldEntity::NAME_INTERVAL_TYPE => PostNotificationScheduler::INTERVAL_MONTHLY,
'intervalType' => PostNotificationScheduler::INTERVAL_MONTHLY, NewsletterOptionFieldEntity::NAME_MONTH_DAY => 19,
'monthDay' => 19, // 20th (count starts from 0) NewsletterOptionFieldEntity::NAME_NTH_WEEK_DAY => null,
'nthWeekDay' => null, NewsletterOptionFieldEntity::NAME_WEK_DAY => null,
'weekDay' => null, NewsletterOptionFieldEntity::NAME_TIME_OF_DAY => 50400, // 2 p.m.
'timeOfDay' => 50400,// 2 p.m. NewsletterOptionFieldEntity::NAME_SCHEDULE => '* * * * *',
]; ]);
$this->postNotificationScheduler->processPostNotificationSchedule($newsletter); $this->postNotificationScheduler->processPostNotificationSchedule($newsletter);
$newsletterOption = NewsletterOption::where('newsletter_id', $newsletter->id) $scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
->where('option_field_id', $newsletterOptionField->id) assert($scheduleOption instanceof NewsletterOptionEntity);
->findOne();
$currentTime = 1483275600; // Sunday, 1 January 2017 @ 1:00pm (UTC) $currentTime = 1483275600; // Sunday, 1 January 2017 @ 1:00pm (UTC)
expect(Scheduler::getNextRunDate($newsletterOption->value, $currentTime)) expect(Scheduler::getNextRunDate($scheduleOption->getValue(), $currentTime))
->equals('2017-01-19 14:00:00'); ->equals('2017-01-19 14:00:00');
} }
public function testItProcessesPostNotificationScheduledForMonthlyDeliveryOnLastWeekDay() { public function testItProcessesPostNotificationScheduledForMonthlyDeliveryOnLastWeekDay() {
$newsletterOptionField = NewsletterOptionField::create(); $newsletter = $this->createNewsletter();
$newsletterOptionField->name = 'schedule';
$newsletterOptionField->newsletterType = Newsletter::TYPE_NOTIFICATION;
$newsletterOptionField->save();
// monthly notification is scheduled every last Saturday at 14:00 // monthly notification is scheduled every last Saturday at 14:00
$newsletter = (object)[ $this->createNewsletterOptions($newsletter, [
'id' => 1, NewsletterOptionFieldEntity::NAME_INTERVAL_TYPE => PostNotificationScheduler::INTERVAL_NTHWEEKDAY,
'intervalType' => PostNotificationScheduler::INTERVAL_NTHWEEKDAY, NewsletterOptionFieldEntity::NAME_MONTH_DAY => null,
'monthDay' => null, NewsletterOptionFieldEntity::NAME_NTH_WEEK_DAY => 'L', // L = last
'nthWeekDay' => 'L', // L = last NewsletterOptionFieldEntity::NAME_WEK_DAY => Carbon::SATURDAY,
'weekDay' => Carbon::SATURDAY, NewsletterOptionFieldEntity::NAME_TIME_OF_DAY => 50400, // 2 p.m.
'timeOfDay' => 50400,// 2 p.m. NewsletterOptionFieldEntity::NAME_SCHEDULE => '* * * * *',
]; ]);
$this->postNotificationScheduler->processPostNotificationSchedule($newsletter); $this->postNotificationScheduler->processPostNotificationSchedule($newsletter);
$newsletterOption = NewsletterOption::where('newsletter_id', $newsletter->id) $scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
->where('option_field_id', $newsletterOptionField->id) assert($scheduleOption instanceof NewsletterOptionEntity);
->findOne();
$currentTime = 1485694800; // Sunday, 29 January 2017 @ 1:00pm (UTC) $currentTime = 1485694800; // Sunday, 29 January 2017 @ 1:00pm (UTC)
expect(Scheduler::getNextRunDate($newsletterOption->value, $currentTime)) expect(Scheduler::getNextRunDate($scheduleOption->getValue(), $currentTime))
->equals('2017-02-25 14:00:00'); ->equals('2017-02-25 14:00:00');
} }
public function testItProcessesPostNotificationScheduledForImmediateDelivery() { public function testItProcessesPostNotificationScheduledForImmediateDelivery() {
$newsletterOptionField = NewsletterOptionField::create(); $newsletter = $this->createNewsletter();
$newsletterOptionField->name = 'schedule';
$newsletterOptionField->newsletterType = Newsletter::TYPE_NOTIFICATION;
$newsletterOptionField->save();
// notification is scheduled immediately (next minute) // notification is scheduled immediately (next minute)
$newsletter = (object)[ $this->createNewsletterOptions($newsletter, [
'id' => 1, NewsletterOptionFieldEntity::NAME_INTERVAL_TYPE => PostNotificationScheduler::INTERVAL_IMMEDIATELY,
'intervalType' => PostNotificationScheduler::INTERVAL_IMMEDIATELY, NewsletterOptionFieldEntity::NAME_MONTH_DAY => null,
'monthDay' => null, NewsletterOptionFieldEntity::NAME_NTH_WEEK_DAY => null,
'nthWeekDay' => null, NewsletterOptionFieldEntity::NAME_WEK_DAY => null,
'weekDay' => null, NewsletterOptionFieldEntity::NAME_TIME_OF_DAY => null,
'timeOfDay' => null, NewsletterOptionFieldEntity::NAME_SCHEDULE => '* * * * *',
]; ]);
$this->postNotificationScheduler->processPostNotificationSchedule($newsletter); $this->postNotificationScheduler->processPostNotificationSchedule($newsletter);
$newsletterOption = NewsletterOption::where('newsletter_id', $newsletter->id) $scheduleOption = $newsletter->getOption(NewsletterOptionFieldEntity::NAME_SCHEDULE);
->where('option_field_id', $newsletterOptionField->id) assert($scheduleOption instanceof NewsletterOptionEntity);
->findOne();
$currentTime = 1483275600; // Sunday, 1 January 2017 @ 1:00pm (UTC) $currentTime = 1483275600; // Sunday, 1 January 2017 @ 1:00pm (UTC)
expect(Scheduler::getNextRunDate($newsletterOption->value, $currentTime)) expect(Scheduler::getNextRunDate($scheduleOption->getValue(), $currentTime))
->equals('2017-01-01 13:01:00'); ->equals('2017-01-01 13:01:00');
} }
public function testUnsearchablePostTypeDoesNotSchedulePostNotification() { public function testUnsearchablePostTypeDoesNotSchedulePostNotification() {
$hook = ContainerWrapper::getInstance()->get(Hooks::class); $newsletter = $this->createNewsletter();
$newsletter = $this->_createNewsletter(); $this->createNewsletterOptions($newsletter, [
NewsletterOptionFieldEntity::NAME_INTERVAL_TYPE => PostNotificationScheduler::INTERVAL_IMMEDIATELY,
$this->_createNewsletterOptions( NewsletterOptionFieldEntity::NAME_SCHEDULE => '* * * * *',
$newsletter->id, ]);
[
'intervalType' => PostNotificationScheduler::INTERVAL_IMMEDIATELY,
'schedule' => '* * * * *',
]
);
$this->_removePostNotificationHooks(); $this->_removePostNotificationHooks();
register_post_type('post', ['exclude_from_search' => true]); register_post_type('post', ['exclude_from_search' => true]);
$hook->setupPostNotifications(); $this->hooks->setupPostNotifications();
$postData = [ $postData = [
'post_title' => 'title', 'post_title' => 'title',
@ -245,38 +262,37 @@ class PostNotificationTest extends \MailPoetTest {
]; ];
wp_insert_post($postData); wp_insert_post($postData);
$queue = SendingQueue::findTaskByNewsletterId($newsletter->id)->findOne(); $queue = SendingQueue::findTaskByNewsletterId($newsletter->getId())->findOne();
expect($queue)->equals(false); expect($queue)->equals(false);
$this->_removePostNotificationHooks(); $this->_removePostNotificationHooks();
register_post_type('post', ['exclude_from_search' => false]); register_post_type('post', ['exclude_from_search' => false]);
$hook->setupPostNotifications(); $this->hooks->setupPostNotifications();
wp_insert_post($postData); wp_insert_post($postData);
$queue = SendingQueue::findTaskByNewsletterId($newsletter->id)->findOne(); $queue = SendingQueue::findTaskByNewsletterId($newsletter->getId())->findOne();
expect($queue)->notequals(false); expect($queue)->notequals(false);
} }
public function testSchedulerWontRunIfUnsentNotificationHistoryExists() { public function testSchedulerWontRunIfUnsentNotificationHistoryExists() {
$newsletter = $this->_createNewsletter(); $newsletter = $this->createNewsletter();
$this->_createNewsletterOptions( $this->createNewsletterOptions($newsletter, [
$newsletter->id, NewsletterOptionFieldEntity::NAME_INTERVAL_TYPE => PostNotificationScheduler::INTERVAL_IMMEDIATELY,
[ NewsletterOptionFieldEntity::NAME_SCHEDULE => '* * * * *',
'intervalType' => PostNotificationScheduler::INTERVAL_IMMEDIATELY, ]);
'schedule' => '* * * * *',
]
);
$notificationHistory = Newsletter::create(); $notificationHistory = new NewsletterEntity();
$notificationHistory->type = Newsletter::TYPE_NOTIFICATION_HISTORY; $notificationHistory->setType(Newsletter::TYPE_NOTIFICATION_HISTORY);
$notificationHistory->status = Newsletter::STATUS_SENDING; $notificationHistory->setStatus(Newsletter::STATUS_SENDING);
$notificationHistory->parentId = $newsletter->id; $notificationHistory->setParent($newsletter);
$notificationHistory->save(); $notificationHistory->setSubject($newsletter->getSubject());
$this->newslettersRepository->persist($notificationHistory);
$this->newslettersRepository->flush();
$sendingTask = SendingTask::create(); $sendingTask = SendingTask::create();
$sendingTask->newsletterId = $notificationHistory->id; $sendingTask->newsletterId = $notificationHistory->getId();
$sendingTask->status = SendingQueue::STATUS_SCHEDULED; $sendingTask->status = SendingQueue::STATUS_SCHEDULED;
$sendingTask->save(); $sendingTask->save();
@ -284,9 +300,13 @@ class PostNotificationTest extends \MailPoetTest {
'post_title' => 'title', 'post_title' => 'title',
'post_status' => 'publish', 'post_status' => 'publish',
]; ];
// because the hooks work after deserialization entityManager incorrectly, we need to remove the hooks and setup them again
$this->_removePostNotificationHooks();
$this->hooks->setupPostNotifications();
wp_insert_post($postData); wp_insert_post($postData);
$queue = SendingQueue::findTaskByNewsletterId($newsletter->id)->findOne(); $queue = SendingQueue::findTaskByNewsletterId($newsletter->getId())->findOne();
expect($queue)->equals(false); expect($queue)->equals(false);
} }
@ -298,46 +318,55 @@ class PostNotificationTest extends \MailPoetTest {
10 10
); );
} }
} }
public function _createNewsletter() { private function createNewsletter(): NewsletterEntity {
$newsletter = Newsletter::create(); $newsletter = new NewsletterEntity();
$newsletter->type = Newsletter::TYPE_NOTIFICATION; $newsletter->setType(Newsletter::TYPE_NOTIFICATION);
$newsletter->status = Newsletter::STATUS_ACTIVE; $newsletter->setStatus(Newsletter::STATUS_ACTIVE);
$newsletter->save(); $newsletter->setSubject('Testing subject');
expect($newsletter->getErrors())->false(); $this->newslettersRepository->persist($newsletter);
$this->newslettersRepository->flush();
return $newsletter; return $newsletter;
} }
public function _createNewsletterOptions($newsletterId, $options) { private function createPost(NewsletterEntity $newsletter, int $postId): NewsletterPostEntity {
foreach ($options as $option => $value) { $newsletterPost = new NewsletterPostEntity($newsletter, $postId);
$newsletterOptionField = NewsletterOptionField::where('name', $option)->findOne(); $this->newsletterPostsRepository->persist($newsletterPost);
if (!$newsletterOptionField) { $this->newsletterPostsRepository->flush();
$newsletterOptionField = NewsletterOptionField::create(); return $newsletterPost;
$newsletterOptionField->name = $option;
$newsletterOptionField->newsletterType = Newsletter::TYPE_NOTIFICATION;
$newsletterOptionField->save();
expect($newsletterOptionField->getErrors())->false();
} }
$newsletterOption = NewsletterOption::create(); private function createNewsletterOptions(NewsletterEntity $newsletter, array $options) {
$newsletterOption->optionFieldId = (int)$newsletterOptionField->id; foreach ($options as $name => $value) {
$newsletterOption->newsletterId = $newsletterId; $newsletterOptionField = $this->newsletterOptionFieldsRepository->findOneBy([
$newsletterOption->value = $value; 'name' => $name,
$newsletterOption->save(); ]);
expect($newsletterOption->getErrors())->false(); if ($newsletterOptionField === null) {
$newsletterOptionField = new NewsletterOptionFieldEntity();
$newsletterOptionField->setName($name);
$newsletterOptionField->setNewsletterType(NewsletterEntity::TYPE_NOTIFICATION);
$this->newsletterOptionFieldsRepository->persist($newsletterOptionField);
} }
$scheduleOption = new NewsletterOptionEntity($newsletter, $newsletterOptionField);
$scheduleOption->setValue($value);
$newsletter->getOptions()->add($scheduleOption);
$this->newsletterOptionsRepository->persist($scheduleOption);
}
$this->newsletterOptionsRepository->flush();
} }
public function _after() { public function _after() {
Carbon::setTestNow(); Carbon::setTestNow();
ORM::raw_execute('TRUNCATE ' . Newsletter::$_table); $this->truncateEntity(NewsletterEntity::class);
ORM::raw_execute('TRUNCATE ' . NewsletterOption::$_table); $this->truncateEntity(NewsletterOptionEntity::class);
ORM::raw_execute('TRUNCATE ' . NewsletterOptionField::$_table); $this->truncateEntity(NewsletterOptionFieldEntity::class);
ORM::raw_execute('TRUNCATE ' . NewsletterPost::$_table); $this->truncateEntity(NewsletterPostEntity::class);
ORM::raw_execute('TRUNCATE ' . ScheduledTask::$_table); $this->truncateEntity(ScheduledTaskEntity::class);
ORM::raw_execute('TRUNCATE ' . ScheduledTaskSubscriber::$_table); $this->truncateEntity(ScheduledTaskSubscriberEntity::class);
ORM::raw_execute('TRUNCATE ' . SendingQueue::$_table); $this->truncateEntity(SubscriberEntity::class);
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table); $this->truncateEntity(SendingQueueEntity::class);
} }
} }