Commit 1e28935f9e6 for woocommerce

commit 1e28935f9e6ba3f2132d5262a278a7a76c2a7e33
Author: Vlad Olaru <vlad.olaru@automattic.com>
Date:   Sun Nov 15 22:43:21 2026 +0200

    [tests] Add test hooks after parent::setUp() so they stay out of the hook snapshot (#70989)

    test: Add test hooks after parent::setUp() so they stay out of the snapshot

    WordPress takes its hook snapshot inside the first test's parent::setUp()
    in a process and restores it after every test. A hook a class adds in
    setUp() before that call is in the snapshot whenever the class runs
    first (a filtered run, a split CI job, random order), and every later
    test in the process then starts with it. Run alone first, each of these
    classes put its filter into the snapshot; the v4 REST ones turned the
    rest-api-v4 feature on for the rest of the process.

    Add the hooks after parent::setUp(): the v4 REST feature filter in 11
    classes (WC_REST_Unit_Test_Case registers routes on the first request,
    so the filter is still in place in time), the WC_Structured_Data
    instance, and the save-for-later filter. The Store API batch tests
    register their routes inside setUp(), so they add the batch methods
    filter after the parent call and build the Store API server again.

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

diff --git a/plugins/woocommerce/changelog/test-hooks-before-parent-setup b/plugins/woocommerce/changelog/test-hooks-before-parent-setup
new file mode 100644
index 00000000000..506ec7e97cc
--- /dev/null
+++ b/plugins/woocommerce/changelog/test-hooks-before-parent-setup
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Add test hooks after the base test setup so they stay out of the shared hook snapshot; no production change.
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-structured-data-test.php b/plugins/woocommerce/tests/php/includes/class-wc-structured-data-test.php
index c62207434ee..09b37e46256 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-structured-data-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-structured-data-test.php
@@ -17,9 +17,9 @@ class WC_Structured_Data_Test extends \WC_Unit_Test_Case {
 	 * @return void
 	 */
 	public function setUp(): void {
+		parent::setUp();
 		include_once WC_ABSPATH . 'includes/class-wc-structured-data.php';
 		$this->structured_data = new WC_Structured_Data();
-		parent::setUp();
 	}

 	/**
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Customers/class-wc-rest-customers-v4-controller-tests.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Customers/class-wc-rest-customers-v4-controller-tests.php
index f064d1a87fe..513f6e66d57 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Customers/class-wc-rest-customers-v4-controller-tests.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Customers/class-wc-rest-customers-v4-controller-tests.php
@@ -97,8 +97,8 @@ class WC_REST_Customers_V4_Controller_Tests extends WC_REST_Unit_Test_Case {
 	 * Setup our test server, endpoints, and user info.
 	 */
 	public function setUp(): void {
-		$this->enable_rest_api_v4_feature();
 		parent::setUp();
+		$this->enable_rest_api_v4_feature();

 		// Create schema instances with dependency injection.
 		$this->customer_schema = new CustomerSchema();
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/OrderNotes/class-wc-rest-order-notes-v4-controller-tests.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/OrderNotes/class-wc-rest-order-notes-v4-controller-tests.php
index 17f176ace33..e153cb2b5d6 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/OrderNotes/class-wc-rest-order-notes-v4-controller-tests.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/OrderNotes/class-wc-rest-order-notes-v4-controller-tests.php
@@ -73,8 +73,8 @@ class WC_REST_Order_Notes_V4_Controller_Tests extends WC_REST_Unit_Test_Case {
 	 * Setup our test server, endpoints, and user info.
 	 */
 	public function setUp(): void {
-		$this->enable_rest_api_v4_feature();
 		parent::setUp();
+		$this->enable_rest_api_v4_feature();

 		// Create schema instance with dependency injection.
 		$order_note_schema = new \Automattic\WooCommerce\Internal\RestApi\Routes\V4\OrderNotes\Schema\OrderNoteSchema();
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Orders/class-wc-rest-orders-v4-can-be-refunded-test.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Orders/class-wc-rest-orders-v4-can-be-refunded-test.php
index 05c0cff6d10..45ea7915f58 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Orders/class-wc-rest-orders-v4-can-be-refunded-test.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Orders/class-wc-rest-orders-v4-can-be-refunded-test.php
@@ -59,8 +59,8 @@ class WC_REST_Orders_V4_Can_Be_Refunded_Test extends WC_REST_Unit_Test_Case {
 	 * Set up test fixtures.
 	 */
 	public function setUp(): void {
-		self::enable_rest_api_v4_feature();
 		parent::setUp();
+		self::enable_rest_api_v4_feature();

 		wp_set_current_user( self::$user_id );
 	}
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Orders/class-wc-rest-orders-v4-controller-tests.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Orders/class-wc-rest-orders-v4-controller-tests.php
index 4a2061a0257..b292682eb0f 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Orders/class-wc-rest-orders-v4-controller-tests.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Orders/class-wc-rest-orders-v4-controller-tests.php
@@ -90,8 +90,8 @@ class WC_REST_Orders_V4_Controller_Tests extends WC_REST_Unit_Test_Case {
 	 * Setup our test server, endpoints, and user info.
 	 */
 	public function setUp(): void {
-		$this->enable_rest_api_v4_feature();
 		parent::setUp();
+		$this->enable_rest_api_v4_feature();

 		// Create schema instances with dependency injection.
 		$order_item_schema     = new OrderItemSchema();
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Refunds/class-wc-rest-refunds-v4-controller-tests.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Refunds/class-wc-rest-refunds-v4-controller-tests.php
index 35218b00d55..9264182e799 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Refunds/class-wc-rest-refunds-v4-controller-tests.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Refunds/class-wc-rest-refunds-v4-controller-tests.php
@@ -141,8 +141,8 @@ class WC_REST_Refunds_V4_Controller_Tests extends WC_REST_Unit_Test_Case {
 	 * Setup our test server, endpoints, and user info.
 	 */
 	public function setUp(): void {
-		$this->enable_rest_api_v4_feature();
 		parent::setUp();
+		$this->enable_rest_api_v4_feature();
 		self::enable_direct_product_attribute_lookup_updates();

 		// Create schema instances with dependency injection.
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Refunds/class-wc-rest-refunds-v4-preview-tests.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Refunds/class-wc-rest-refunds-v4-preview-tests.php
index 0149885004e..204578c828d 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Refunds/class-wc-rest-refunds-v4-preview-tests.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Refunds/class-wc-rest-refunds-v4-preview-tests.php
@@ -86,8 +86,8 @@ class WC_REST_Refunds_V4_Preview_Tests extends WC_REST_Unit_Test_Case {
 	 * Setup our test server, endpoints, and user info.
 	 */
 	public function setUp(): void {
-		$this->enable_rest_api_v4_feature();
 		parent::setUp();
+		$this->enable_rest_api_v4_feature();

 		wp_set_current_user( self::$user_id );
 	}
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Settings/class-wc-rest-general-settings-v4-controller-test.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Settings/class-wc-rest-general-settings-v4-controller-test.php
index ed3500c1e4c..cc873756192 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Settings/class-wc-rest-general-settings-v4-controller-test.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version4/Settings/class-wc-rest-general-settings-v4-controller-test.php
@@ -41,7 +41,9 @@ class WC_REST_General_Settings_V4_Controller_Test extends WC_REST_Unit_Test_Case
 	 * Setup.
 	 */
 	public function setUp(): void {
-		// Set up the feature flag before parent::setUp() to ensure the feature is enabled.
+		parent::setUp();
+
+		// REST routes are registered on the first request, so the feature can be enabled here.
 		$feature_filter = function ( $features ) {
 			$features[] = 'rest-api-v4';
 			return $features;
@@ -49,8 +51,6 @@ class WC_REST_General_Settings_V4_Controller_Test extends WC_REST_Unit_Test_Case

 		add_filter( 'woocommerce_admin_features', $feature_filter );

-		parent::setUp();
-
 		// This is to reset the country after the test.
 		$this->prev_default_country = get_option( 'woocommerce_default_country' );
 	}
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Batch.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Batch.php
index 670711f0c34..2b198763042 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Batch.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Batch.php
@@ -17,6 +17,9 @@ class Batch extends ControllerTestCase {
 	 * Setup test product data. Called before every test.
 	 */
 	protected function setUp(): void {
+		parent::setUp();
+
+		// Allow GET in batches, then build the routes again: the parent registered them before the filter.
 		add_filter(
 			'__experimental_woocommerce_store_api_batch_request_methods',
 			function ( $methods ) {
@@ -24,7 +27,7 @@ class Batch extends ControllerTestCase {
 				return $methods;
 			}
 		);
-		parent::setUp();
+		$this->initialize_store_api_server();

 		$fixtures = new FixtureData();

diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Schemas/ShopperListSchemaTest.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Schemas/ShopperListSchemaTest.php
index 1b884b2cc22..8090c958dc5 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Schemas/ShopperListSchemaTest.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Schemas/ShopperListSchemaTest.php
@@ -36,13 +36,13 @@ class ShopperListSchemaTest extends WC_Unit_Test_Case {
 	 * Set up.
 	 */
 	public function setUp(): void {
+		parent::setUp();
+
 		// `saved-for-later` depends on the `cart_save_for_later` feature
 		// flag. Filter the option read so `ShopperList::get_by_slug()`
 		// returns a list without writing the option to the database.
 		add_filter( 'pre_option_woocommerce_cart_save_for_later_enabled', array( $this, 'filter_save_for_later_enabled' ) );

-		parent::setUp();
-
 		$formatters = new Formatters();
 		$formatters->register( 'money', MoneyFormatter::class );
 		$formatters->register( 'html', HtmlFormatter::class );
diff --git a/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Products/ProductsAddToCartTest.php b/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Products/ProductsAddToCartTest.php
index 55f0ac10d4b..4bc5bb69dde 100644
--- a/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Products/ProductsAddToCartTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Products/ProductsAddToCartTest.php
@@ -35,6 +35,8 @@ class ProductsAddToCartTest extends WC_REST_Unit_Test_Case {
 	 * Setup our test server, endpoints, and user info.
 	 */
 	public function setUp(): void {
+		parent::setUp();
+
 		// Enable the REST API v4 feature.
 		add_filter(
 			'woocommerce_admin_features',
@@ -43,7 +45,6 @@ class ProductsAddToCartTest extends WC_REST_Unit_Test_Case {
 				return $features;
 			},
 		);
-		parent::setUp();
 		$this->endpoint = new ProductsController();
 		wp_set_current_user( self::$user );
 	}
diff --git a/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Products/ProductsControllerTest.php b/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Products/ProductsControllerTest.php
index 89b609d693f..b95b9e532c6 100644
--- a/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Products/ProductsControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Products/ProductsControllerTest.php
@@ -189,8 +189,8 @@ class ProductsControllerTest extends WC_Unit_Test_Case {
 	 * Setup our test server, endpoints, and user info.
 	 */
 	public function setUp(): void {
-		$this->enable_rest_api_v4_feature();
 		parent::setUp();
+		$this->enable_rest_api_v4_feature();
 		$this->endpoint = new ProductsController();
 		$this->reset_rest_server();
 		wp_set_current_user( self::$administrator_id );
diff --git a/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Settings/Email/EmailSettingsControllerTest.php b/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Settings/Email/EmailSettingsControllerTest.php
index a1b0d33b22a..399184e6cdb 100644
--- a/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Settings/Email/EmailSettingsControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Settings/Email/EmailSettingsControllerTest.php
@@ -46,7 +46,9 @@ class EmailSettingsControllerTest extends WC_REST_Unit_Test_Case {
 	 * Setup.
 	 */
 	public function setUp(): void {
-		// Enable the v4 REST API feature before bootstrapping.
+		parent::setUp();
+
+		// Enable the v4 REST API feature.
 		$feature_filter = function ( $features ) {
 			$features[] = 'rest-api-v4';
 			return $features;
@@ -54,7 +56,6 @@ class EmailSettingsControllerTest extends WC_REST_Unit_Test_Case {

 		add_filter( 'woocommerce_admin_features', $feature_filter );

-		parent::setUp();
 		// Enable block email editor feature to test reply-to fields.
 		update_option( 'woocommerce_feature_block_email_editor_enabled', 'yes' );

diff --git a/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Settings/Emails/EmailsSettingsControllerTest.php b/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Settings/Emails/EmailsSettingsControllerTest.php
index 3309fbf7c5c..986c114c724 100644
--- a/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Settings/Emails/EmailsSettingsControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/RestApi/Routes/V4/Settings/Emails/EmailsSettingsControllerTest.php
@@ -110,16 +110,17 @@ class EmailsSettingsControllerTest extends WC_Unit_Test_Case {
 		$this->previous_emails_instance = $instance_property->getValue();
 		$this->previous_emails          = $this->previous_emails_instance ? $this->previous_emails_instance->emails : array();

-		// Enable the v4 REST API feature before bootstrapping.
+		$this->prev_options['woocommerce_feature_block_email_editor_enabled'] = get_option( 'woocommerce_feature_block_email_editor_enabled', null );
+
+		parent::setUp();
+
+		// Enable the v4 REST API feature.
 		$feature_filter = function ( $features ) {
 			$features[] = 'rest-api-v4';
 			return $features;
 		};

 		add_filter( 'woocommerce_admin_features', $feature_filter );
-		$this->prev_options['woocommerce_feature_block_email_editor_enabled'] = get_option( 'woocommerce_feature_block_email_editor_enabled', null );
-
-		parent::setUp();

 		// Enable block email editor feature.
 		update_option( 'woocommerce_feature_block_email_editor_enabled', 'yes' );