diff --git a/mailpoet/tests/_support/AcceptanceTester.php b/mailpoet/tests/_support/AcceptanceTester.php index ee8c6f4129..070c2bc2b4 100644 --- a/mailpoet/tests/_support/AcceptanceTester.php +++ b/mailpoet/tests/_support/AcceptanceTester.php @@ -677,4 +677,45 @@ class AcceptanceTester extends \Codeception\Actor { } return (int)$result === 0; } + + /** + * Some tests rely on background job processing. + * The processing runs in 1 minute interval (default Action Scheduler interval) + * This method triggers the processing immediately so that tests don't have to wait. + */ + public function triggerMailPoetActionScheduler(): void { + $i = $this; + // Set Action Scheduler in WP Cron to run immediately + $wpCronOption = $i->grabOptionFromDatabase('cron'); + if (!is_array($wpCronOption)) { + return; + } + $actionSchedulerRunKey = null; + foreach ($wpCronOption as $timestampKey => $cronRun) { + if (is_array($cronRun) && array_key_exists('action_scheduler_run_queue' ,$cronRun)) { + $actionSchedulerRunKey = $timestampKey; + } + } + if (!$actionSchedulerRunKey) { + return; + } + $actionSchedulerJob = $wpCronOption[$actionSchedulerRunKey]; + $wpCronOption[time() - 100] = $actionSchedulerJob; + unset($wpCronOption[$actionSchedulerRunKey]); + unset($wpCronOption['version']); + ksort($wpCronOption); + $wpCronOption['version'] = 2; + $i->importSql([ + "UPDATE mp_options SET option_value = '" . serialize($wpCronOption) . "' WHERE option_name = 'cron';", + "DELETE FROM mp_options WHERE option_name = '_transient_doing_cron';", // Remove WP Cron lock + ]); + + // Schedule MailPoet trigger in action scheduler to run immediately + $i->importSql([ + "UPDATE mp_actionscheduler_actions SET scheduled_date_gmt = SUBTIME(now(), '01:00:00'), scheduled_date_local = SUBTIME(now(), '01:00:00') WHERE hook = 'mailpoet/cron/daemon-trigger' AND status = 'pending';", + ]); + + //Page reload triggers WP Cron and it triggers Action Scheduler + $i->reloadPage(); + } } diff --git a/mailpoet/tests/acceptance/Newsletters/ReceivePostNotificationCest.php b/mailpoet/tests/acceptance/Newsletters/ReceivePostNotificationCest.php index 74b4e7beca..89674996a4 100644 --- a/mailpoet/tests/acceptance/Newsletters/ReceivePostNotificationCest.php +++ b/mailpoet/tests/acceptance/Newsletters/ReceivePostNotificationCest.php @@ -66,6 +66,7 @@ class ReceivePostNotificationCest { $stmt = $entityManager->getConnection()->prepare($sql); $stmt->executeStatement(); + $i->triggerMailPoetActionScheduler(); // confirm newsletter has been sent $i->amOnMailpoetPage('Emails'); diff --git a/mailpoet/tests/acceptance/Newsletters/ReceiveStandardEmailCest.php b/mailpoet/tests/acceptance/Newsletters/ReceiveStandardEmailCest.php index ab143800e3..679335907e 100644 --- a/mailpoet/tests/acceptance/Newsletters/ReceiveStandardEmailCest.php +++ b/mailpoet/tests/acceptance/Newsletters/ReceiveStandardEmailCest.php @@ -49,6 +49,8 @@ class ReceiveStandardEmailCest { $i->click('Send'); $i->waitForEmailSendingOrSent(); + $i->triggerMailPoetActionScheduler(); + $i->wantTo('confirm newsletter is received'); $i->checkEmailWasReceived('Hi, John Doe ' . $specialChars); $i->click(Locator::contains('span.subject', 'Hi, John Doe ' . $specialChars)); diff --git a/mailpoet/tests/acceptance/Newsletters/ReceiveWelcomeEmailCest.php b/mailpoet/tests/acceptance/Newsletters/ReceiveWelcomeEmailCest.php index 04ee587e9a..18371967d7 100644 --- a/mailpoet/tests/acceptance/Newsletters/ReceiveWelcomeEmailCest.php +++ b/mailpoet/tests/acceptance/Newsletters/ReceiveWelcomeEmailCest.php @@ -52,6 +52,7 @@ class ReceiveWelcomeEmailCest { $i->click('I confirm my subscription!'); $i->switchToNextTab(); $i->reloadPage(); + $i->triggerMailPoetActionScheduler(); // check for welcome email $i->checkEmailWasReceived($welcomeNewsletterName); $i->click(Locator::contains('span.subject', $welcomeNewsletterName)); diff --git a/mailpoet/tests/acceptance/Newsletters/SendCategoryPurchaseEmailCest.php b/mailpoet/tests/acceptance/Newsletters/SendCategoryPurchaseEmailCest.php index 44e8052ff1..f01eb65274 100644 --- a/mailpoet/tests/acceptance/Newsletters/SendCategoryPurchaseEmailCest.php +++ b/mailpoet/tests/acceptance/Newsletters/SendCategoryPurchaseEmailCest.php @@ -42,6 +42,8 @@ class SendCategoryPurchaseEmailCest { $userEmail = Security::generateRandomString() . '-user@email.example'; $i->orderProduct($product, $userEmail); + $i->triggerMailPoetActionScheduler(); + $i->checkEmailWasReceived($emailSubject); $i->click(Locator::contains('span.subject', $emailSubject)); $i->waitForText($userEmail, 20); diff --git a/mailpoet/tests/acceptance/Newsletters/SendFirstPurchaseEmailCest.php b/mailpoet/tests/acceptance/Newsletters/SendFirstPurchaseEmailCest.php index aaaa9e9843..a1be6df5b7 100644 --- a/mailpoet/tests/acceptance/Newsletters/SendFirstPurchaseEmailCest.php +++ b/mailpoet/tests/acceptance/Newsletters/SendFirstPurchaseEmailCest.php @@ -37,6 +37,9 @@ class SendFirstPurchaseEmailCest { $userEmail = 'user@email.test'; $i->orderProduct($product, $userEmail); + + $i->triggerMailPoetActionScheduler(); + $i->checkEmailWasReceived($emailSubject); $i->click(Locator::contains('span.subject', $emailSubject)); diff --git a/mailpoet/tests/acceptance/Newsletters/SendProductPurchaseEmailCest.php b/mailpoet/tests/acceptance/Newsletters/SendProductPurchaseEmailCest.php index 315d9efcf5..56b7fdfa87 100644 --- a/mailpoet/tests/acceptance/Newsletters/SendProductPurchaseEmailCest.php +++ b/mailpoet/tests/acceptance/Newsletters/SendProductPurchaseEmailCest.php @@ -38,6 +38,8 @@ class SendProductPurchaseEmailCest { $userEmail = 'user2@email.test'; $i->orderProduct($product, $userEmail); + $i->triggerMailPoetActionScheduler(); + $i->checkEmailWasReceived($emailSubject); $i->click(Locator::contains('span.subject', $emailSubject)); $i->waitForText($userEmail, 20); diff --git a/mailpoet/tests/acceptance/Settings/RevenueTrackingCookieCest.php b/mailpoet/tests/acceptance/Settings/RevenueTrackingCookieCest.php index 07dafa08d3..ff5dca2f85 100644 --- a/mailpoet/tests/acceptance/Settings/RevenueTrackingCookieCest.php +++ b/mailpoet/tests/acceptance/Settings/RevenueTrackingCookieCest.php @@ -45,6 +45,8 @@ class RevenueTrackingCookieCest { $i->click('Send'); $i->waitForEmailSendingOrSent(); + $i->triggerMailPoetActionScheduler(); + $i->logOut(); $i->checkEmailWasReceived($newsletterSubject); @@ -83,6 +85,8 @@ class RevenueTrackingCookieCest { $i->click('Send'); $i->waitForEmailSendingOrSent(); + $i->triggerMailPoetActionScheduler(); + $i->logOut(); $i->checkEmailWasReceived($newsletterSubject); // click a link in the newsletter and check the cookie has NOT been created diff --git a/mailpoet/tests/acceptance/Settings/WooCommerceSetupPageCest.php b/mailpoet/tests/acceptance/Settings/WooCommerceSetupPageCest.php index 6d478ed562..c763e4c8c5 100644 --- a/mailpoet/tests/acceptance/Settings/WooCommerceSetupPageCest.php +++ b/mailpoet/tests/acceptance/Settings/WooCommerceSetupPageCest.php @@ -47,6 +47,9 @@ class WooCommerceSetupPageCest { $i->moveMouseOver('[data-automation-id="segment_name_WooCommerce Customers"]'); $i->click('[data-automation-id="view_subscribers_WooCommerce Customers"]'); $i->waitForListingItemsToLoad(); + + $i->triggerMailPoetActionScheduler(); + $i->canSee($registeredCustomer['email']); $i->reloadPage(); // It takes more time to sync guest user diff --git a/mailpoet/tests/acceptance/Subscribers/SubscriberCookieCest.php b/mailpoet/tests/acceptance/Subscribers/SubscriberCookieCest.php index 49d7023314..c54d0e1dfc 100644 --- a/mailpoet/tests/acceptance/Subscribers/SubscriberCookieCest.php +++ b/mailpoet/tests/acceptance/Subscribers/SubscriberCookieCest.php @@ -137,6 +137,7 @@ class SubscriberCookieCest { $i->selectOptionInSelect2('Test list'); $i->click('Send'); $i->waitForEmailSendingOrSent(); + $i->triggerMailPoetActionScheduler(); // click on a preview link $i->resetCookie(self::SUBSCRIBER_COOKIE_NAME);