Add Acceptance test helper for triggering background job

It may take up to one minute for Action Scheduler to start.
This commit adds a helper method that sets WP Cron trigger for action scheduler and
MailPoet action in Action Scheduler to run immediately and triggers WP Cron
that triggers action scheduler by page reload.

[MAILPOET-4274]
This commit is contained in:
Rostislav Wolny
2022-05-20 13:32:48 +02:00
committed by Veljko V
parent 2c2c9dba6a
commit 791a0631b5
10 changed files with 60 additions and 0 deletions

View File

@@ -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();
}
}

View File

@@ -66,6 +66,7 @@ class ReceivePostNotificationCest {
$stmt = $entityManager->getConnection()->prepare($sql);
$stmt->executeStatement();
$i->triggerMailPoetActionScheduler();
// confirm newsletter has been sent
$i->amOnMailpoetPage('Emails');

View File

@@ -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));

View File

@@ -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));

View File

@@ -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);

View File

@@ -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));

View File

@@ -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);

View File

@@ -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

View File

@@ -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

View File

@@ -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);