Commit aaf4fdfc64d for woocommerce

commit aaf4fdfc64db006402efb2a81ca900925f44ca1b
Author: Vlad Olaru <vlad.olaru@automattic.com>
Date:   Sat Nov 14 21:58:49 2026 +0200

    [tests] Keep the storage-switch filter from leaking out of four order admin tests (#70928)

    test: Keep the storage-switch filter out of the hook snapshot

    ListTableTest, CustomerHistoryTest, OrderMilestoneEasterEggTest and
    Orders\PageControllerTest add wc_allow_changing_orders_storage_while_
    sync_is_pending -> __return_true in setUpBeforeClass(), so they can
    switch order storage while orders are pending sync. WordPress
    snapshots hooks in the first test's set_up() of the process and
    restores that snapshot after every test. When one of these classes
    runs first, the filter is in the snapshot: removing it in
    tearDownAfterClass() lasts only until the next test's restore puts it
    back, and the storage guard stays off for the rest of the run.

    Add the filter right before each class-level storage switch and
    remove it right after, in setUpBeforeClass() and tearDownAfterClass(),
    so it never reaches the snapshot. A probe run two files after each
    class found the filter hooked on the old code, with HPOS on and off,
    and finds nothing now; the classes' own results are unchanged.

    Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

diff --git a/plugins/woocommerce/changelog/test-class-level-filter-leaks b/plugins/woocommerce/changelog/test-class-level-filter-leaks
new file mode 100644
index 00000000000..d71e2e54695
--- /dev/null
+++ b/plugins/woocommerce/changelog/test-class-level-filter-leaks
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Stop four order admin tests leaking the storage-switch safety filter into later tests; no production change.
diff --git a/plugins/woocommerce/tests/php/src/Internal/Admin/OrderMilestoneEasterEggTest.php b/plugins/woocommerce/tests/php/src/Internal/Admin/OrderMilestoneEasterEggTest.php
index 40afa4ffcf6..3b5aabcbedf 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Admin/OrderMilestoneEasterEggTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Admin/OrderMilestoneEasterEggTest.php
@@ -34,12 +34,14 @@ class OrderMilestoneEasterEggTest extends \WC_Unit_Test_Case {
 		parent::setUpBeforeClass();

 		self::$hpos_prev_state = OrderUtil::custom_orders_table_usage_is_enabled();
-		add_filter( 'wc_allow_changing_orders_storage_while_sync_is_pending', '__return_true' );
 		OrderHelper::create_order_custom_table_if_not_exist();

+		// Only around the storage switch: a hook added here stays for the whole run when this class runs first.
+		add_filter( 'wc_allow_changing_orders_storage_while_sync_is_pending', '__return_true' );
 		if ( ! self::$hpos_prev_state ) {
 			OrderHelper::toggle_cot_feature_and_usage( true );
 		}
+		remove_filter( 'wc_allow_changing_orders_storage_while_sync_is_pending', '__return_true' );
 	}

 	/**
@@ -48,10 +50,10 @@ class OrderMilestoneEasterEggTest extends \WC_Unit_Test_Case {
 	public static function tearDownAfterClass(): void {
 		self::clear_hpos_orders();

+		add_filter( 'wc_allow_changing_orders_storage_while_sync_is_pending', '__return_true' );
 		if ( OrderUtil::custom_orders_table_usage_is_enabled() !== self::$hpos_prev_state ) {
 			OrderHelper::toggle_cot_feature_and_usage( self::$hpos_prev_state );
 		}
-
 		remove_filter( 'wc_allow_changing_orders_storage_while_sync_is_pending', '__return_true' );

 		parent::tearDownAfterClass();
diff --git a/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/ListTableTest.php b/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/ListTableTest.php
index 5e869b945cf..4316c4f2191 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/ListTableTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/ListTableTest.php
@@ -32,12 +32,14 @@ class ListTableTest extends \WC_Unit_Test_Case {
 		parent::setUpBeforeClass();

 		self::$hpos_prev_state = OrderUtil::custom_orders_table_usage_is_enabled();
-		add_filter( 'wc_allow_changing_orders_storage_while_sync_is_pending', '__return_true' );
 		OrderHelper::create_order_custom_table_if_not_exist();

+		// Only around the storage switch: a hook added here stays for the whole run when this class runs first.
+		add_filter( 'wc_allow_changing_orders_storage_while_sync_is_pending', '__return_true' );
 		if ( ! self::$hpos_prev_state ) {
 			OrderHelper::toggle_cot_feature_and_usage( true );
 		}
+		remove_filter( 'wc_allow_changing_orders_storage_while_sync_is_pending', '__return_true' );
 	}

 	/**
@@ -46,10 +48,10 @@ class ListTableTest extends \WC_Unit_Test_Case {
 	public static function tearDownAfterClass(): void {
 		self::clear_hpos_orders();

+		add_filter( 'wc_allow_changing_orders_storage_while_sync_is_pending', '__return_true' );
 		if ( OrderUtil::custom_orders_table_usage_is_enabled() !== self::$hpos_prev_state ) {
 			OrderHelper::toggle_cot_feature_and_usage( self::$hpos_prev_state );
 		}
-
 		remove_filter( 'wc_allow_changing_orders_storage_while_sync_is_pending', '__return_true' );

 		parent::tearDownAfterClass();
diff --git a/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/MetaBoxes/CustomerHistoryTest.php b/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/MetaBoxes/CustomerHistoryTest.php
index f8c983c4c06..0666e97d162 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/MetaBoxes/CustomerHistoryTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/MetaBoxes/CustomerHistoryTest.php
@@ -64,12 +64,14 @@ class CustomerHistoryTest extends WC_Unit_Test_Case {
 		parent::setUpBeforeClass();

 		self::$hpos_prev_state = OrderUtil::custom_orders_table_usage_is_enabled();
-		add_filter( 'wc_allow_changing_orders_storage_while_sync_is_pending', '__return_true' );
 		OrderHelper::create_order_custom_table_if_not_exist();

+		// Only around the storage switch: a hook added here stays for the whole run when this class runs first.
+		add_filter( 'wc_allow_changing_orders_storage_while_sync_is_pending', '__return_true' );
 		if ( ! self::$hpos_prev_state ) {
 			OrderHelper::toggle_cot_feature_and_usage( true );
 		}
+		remove_filter( 'wc_allow_changing_orders_storage_while_sync_is_pending', '__return_true' );
 	}

 	/**
diff --git a/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/PageControllerTest.php b/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/PageControllerTest.php
index 908fe121bda..b4a15635d7e 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/PageControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/PageControllerTest.php
@@ -33,12 +33,14 @@ namespace Automattic\WooCommerce\Tests\Internal\Admin\Orders {
 			parent::setUpBeforeClass();

 			self::$hpos_prev_state = OrderUtil::custom_orders_table_usage_is_enabled();
-			add_filter( 'wc_allow_changing_orders_storage_while_sync_is_pending', '__return_true' );
 			OrderHelper::create_order_custom_table_if_not_exist();

+			// Only around the storage switch: a hook added here stays for the whole run when this class runs first.
+			add_filter( 'wc_allow_changing_orders_storage_while_sync_is_pending', '__return_true' );
 			if ( self::$hpos_prev_state ) {
 				OrderHelper::toggle_cot_feature_and_usage( false );
 			}
+			remove_filter( 'wc_allow_changing_orders_storage_while_sync_is_pending', '__return_true' );
 		}

 		/**
@@ -49,8 +51,8 @@ namespace Automattic\WooCommerce\Tests\Internal\Admin\Orders {
 			// the object cache. Flush it first, or update_option() compares against the cached value
 			// and skips the write, then restore unconditionally.
 			wp_cache_flush();
+			add_filter( 'wc_allow_changing_orders_storage_while_sync_is_pending', '__return_true' );
 			OrderHelper::toggle_cot_feature_and_usage( self::$hpos_prev_state );
-
 			remove_filter( 'wc_allow_changing_orders_storage_while_sync_is_pending', '__return_true' );

 			parent::tearDownAfterClass();