Commit 915c8407ca5 for woocommerce
commit 915c8407ca50121584bbaa53431b3513b2550d94
Author: Adrian Moldovan <3854374+adimoldovan@users.noreply.github.com>
Date: Thu Nov 12 16:36:58 2026 +0200
Fix flaky Back in Stock unsubscribe E2E test (#70919)
wp-env has no mail transport, so wp_mail() fails. When wp_mail() fails,
the notifications batch cancels the sign-up. The spec made wp_mail()
succeed only on its own queue request. If WP-Cron or the Action Scheduler
async runner ran the batch first, the batch cancelled the sign-up.
The E2E helper plugin now makes wp_mail() succeed while the batch job
runs, whichever runner starts it.
diff --git a/plugins/woocommerce/changelog/fix-e2e-bis-unsubscribe-sent-notification-flake b/plugins/woocommerce/changelog/fix-e2e-bis-unsubscribe-sent-notification-flake
new file mode 100644
index 00000000000..97b97b900d3
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-e2e-bis-unsubscribe-sent-notification-flake
@@ -0,0 +1,4 @@
+Significance: patch
+Type: dev
+Comment: Fix a flaky Back in Stock Notifications E2E test; no production change.
+
diff --git a/plugins/woocommerce/tests/e2e/test-plugins/woocommerce-e2e-test-helper/woocommerce-e2e-test-helper.php b/plugins/woocommerce/tests/e2e/test-plugins/woocommerce-e2e-test-helper/woocommerce-e2e-test-helper.php
index 821eb8e31bc..8bd0b1f9f28 100644
--- a/plugins/woocommerce/tests/e2e/test-plugins/woocommerce-e2e-test-helper/woocommerce-e2e-test-helper.php
+++ b/plugins/woocommerce/tests/e2e/test-plugins/woocommerce-e2e-test-helper/woocommerce-e2e-test-helper.php
@@ -1,7 +1,7 @@
<?php
/**
* Plugin Name: WooCommerce E2E Test Helper
- * Description: Always-on utilities for the WooCommerce E2E suite: cookie-driven filter overrides, synchronous Action Scheduler processing, a REST API for feature flags, options and theme switching, and fixed overrides that remove production timing delays.
+ * Description: Always-on utilities for the WooCommerce E2E suite: cookie-driven filter overrides, synchronous Action Scheduler processing, a REST API for feature flags, options and theme switching, fixed overrides that remove production timing delays, and a mail override for Back in Stock Notifications batches.
* Version: 1.0.0
* Requires PHP: 7.4
* Author: WooCommerce
@@ -330,6 +330,38 @@ add_filter(
PHP_INT_MAX
);
+/*
+ * -----------------------------------------------------------------------------
+ * Mail overrides
+ * -----------------------------------------------------------------------------
+ */
+
+/*
+ * Report Back in Stock Notifications emails as sent while a notifications batch runs.
+ *
+ * wp-env has no mail transport, so `wp_mail()` fails after WP Mail Logging logs the message,
+ * and a failed send cancels the sign-up. Hooked to the batch job so it applies whichever runner
+ * processes it. Not global, because other specs assert the failed-send path.
+ */
+
+/**
+ * Remove the `pre_wp_mail` short-circuit added for the batch.
+ */
+function woocommerce_e2e_remove_mail_short_circuit(): void {
+ remove_filter( 'pre_wp_mail', '__return_true' );
+}
+
+add_action(
+ 'wc_send_stock_notifications_batch',
+ function () {
+ add_filter( 'pre_wp_mail', '__return_true' );
+ },
+ PHP_INT_MIN
+);
+add_action( 'wc_send_stock_notifications_batch', 'woocommerce_e2e_remove_mail_short_circuit', PHP_INT_MAX );
+// The PHP_INT_MAX remover does not run when the job throws, and the queue runner moves on to the next job.
+add_action( 'action_scheduler_failed_execution', 'woocommerce_e2e_remove_mail_short_circuit' );
+
/*
* -----------------------------------------------------------------------------
* Shipping methods
diff --git a/plugins/woocommerce/tests/e2e/utils/back-in-stock-notifications.ts b/plugins/woocommerce/tests/e2e/utils/back-in-stock-notifications.ts
index 71e406383a2..0f917815a9b 100644
--- a/plugins/woocommerce/tests/e2e/utils/back-in-stock-notifications.ts
+++ b/plugins/woocommerce/tests/e2e/utils/back-in-stock-notifications.ts
@@ -1083,18 +1083,13 @@ export async function getEmailLinkById(
/**
* Run any pending Action Scheduler jobs via the process-waiting-actions mu-plugin.
*
- * wp-env has no mail transport, so `wp_mail()` returns false even though WP
- * Mail Logging has already captured the message. A failed send cancels the
- * sign-up instead of marking it sent, so `wp_mail()` is short-circuited to
- * report success for this context through the `e2e-filters` cookie. It is
- * scoped here rather than in the helper plugin because other specs assert
- * the failure path.
+ * The E2E test helper plugin makes the batch's emails report as sent, so the
+ * sign-up is marked sent whichever request runs the batch.
*
* @param {Page} page Playwright page (can be on any URL).
*/
export async function triggerStockNotificationsBatch(
page: Page
): Promise< void > {
- await setFilterValue( page, 'pre_wp_mail', true );
await page.goto( '?process-waiting-actions' );
}