Commit 6b8085f5f65 for woocommerce
commit 6b8085f5f653a2348a42ae9989876e52b97bc8e5
Author: Vlad Olaru <vlad.olaru@automattic.com>
Date: Tue Nov 10 16:41:28 2026 +0200
[tests] Drop per-class REQUEST_URI restores the base test case now covers (#69639)
* test: Put the setUp() customer back after each WC_Unit_Test_Case test
WC()->customer outlives a test, so a test that changes it leaks the
change into every later one. #69117 handles the leaks it found with
the same copied code in five classes: snapshot get_shipping( 'edit' )
in setUp() and set each field back in tearDown(). That still misses
tests that swap in another customer (WC_Abstract_Order_Test does, for
an admin with a billing address in MV), billing and tax exemption
changes, and the change set the setters write to.
WC_Unit_Test_Case already resets the WC() singleton state the database
rollback misses. Keep the customer it saw at setUp() and that
customer's internal state (id, data, changes, is_vat_exempt and
calculated_shipping, read through a closure bound to WC_Customer), and
in clear_wc_singleton_state() put that instance back on WC() and write
the state back directly, so no setter touches the change set. Drop the
five per-class copies and the two tearDown() overrides left with
nothing to do. OrderControllerTests keeps setting its own shipping
location, which its invalid-address tests need.
UnitTestCaseTearDownTest swaps in another customer and changes billing,
shipping and tax exemption, then asserts the same instance, data,
change set and exemption come back. Removing the swap-back makes it
fail.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Restore all of $_SERVER after each WC_Unit_Test_Case test
WP_UnitTestCase::go_to() sets $_SERVER['REQUEST_URI'], and WordPress
resets $_SERVER only for its own core tests (WP_RUN_CORE_TESTS), so a
plugin suite keeps whatever a test leaves behind. 16 of the 17
WooCommerce test classes that call go_to() never put REQUEST_URI back,
and RateLimitsTests changes the client IP and user agent.
#69117 fixes the one leak with a victim in the class that causes it:
ProductPageIntegrationTest restores REQUEST_URI, because after it
visits /?product=variable-product, WC_Cart_Test's form handler test
sees every plain-permalink product URL as the page it is on.
Snapshot the whole superglobal at the end of setUp() and put it back in
tearDown(), in its own finally block so a failing singleton reset
cannot skip it. Drop the per-class restore in ProductPageIntegrationTest.
UnitTestCaseTearDownTest covers go_to(), a changed user agent and a
removed REMOTE_ADDR; removing the restore makes it and the
ProductPageIntegrationTest and WC_Cart_Test pair fail again.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Restore Constants overrides after each WC_Unit_Test_Case test
Constants::set_constant() keeps overrides in a static store that
neither the per-test rollback nor the hook restore resets, so every
test that sets one has to clear it again, and one that forgets leaks
the override into every later test. #69117 found two such leaks
(PHP_OS, WCPAY_VERSION_NUMBER), and about 30 tests carry their own
clear today.
Snapshot Constants::$set_constants at the end of setUp() and put it
back at teardown. Restoring the snapshot, rather than clearing every
override, keeps any override set before the test (in
setUpBeforeClass(), for example). The restore runs in the same finally
block as the $_SERVER restore, so a failure while clearing the WC()
singletons does not skip it.
Refs #69117
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Drop Constants cleanup the base test case now covers
WC_Unit_Test_Case now restores Constants overrides after every test,
so the per-test clears in its descendants only repeat that work.
Remove them: tearDown() clears, clears in end-of-test finally blocks,
the save-and-restore of WCPAY_VERSION_NUMBER in the two payments REST
integration tests, RemoteLoggerTest's copy of the whole store, and
the end-of-test clears in the logging SettingsTest, which were not in a
finally block and leaked on a failed assertion. Three tearDown()
methods, and two try/finally blocks in WooPaymentsTest, were left with
nothing else to do and go too.
Clears that set up a later step of the same test stay
(wc-update-functions-test.php and the logging SettingsTest).
Refs #69117
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Restore the block type registry after each WC_Unit_Test_Case test
WP_Block_Type_Registry is a singleton that outlives every test, and
neither the rollback nor the hook restore touches it. Tests that
register a mock, replace a real block type, or unregister one have to
undo it by hand with a list of names that must match the test body,
and a missed name leaks into later tests (#69117 fixed five such
leaks, each with its own list).
Snapshot the registered block types at the end of setUp(). At
teardown, unregister any name that is new or now points to another
WP_Block_Type, and register again any setUp() entry that is missing.
Block types registered before the test, in setUpBeforeClass() or at
bootstrap, stay as they were.
Refs #69117
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test(blocks): Drop block type cleanup the base test case now covers
WC_Unit_Test_Case now puts the block type registry back after every
test, so move the block tests that kept their own cleanup onto it and
remove that cleanup: the hand-written name lists in BlockTypesController
and Blocks\Checkout, the save-and-register-again of the original block
in MiniCart, MiniCartFooterBlock and CouponCodeTest, and the test block
in BlockHooksTests.
AddToCartWithOptions' wishlist test unregistered the real
add-to-wishlist-button block and never registered it again; the base
restore now does. Its mock blocks are still registered in setUp(),
which now happens for every test, since teardown removes them.
Checkout, MiniCart, MiniCartFooterBlock, CouponCodeTest, BlockHooksTests
and AddToCartWithOptions move from WP_UnitTestCase to
WC_Unit_Test_Case, which also clears the cart, notices and the other
WC() state after each of their tests.
Refs #69117
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Assert the block registry test has two block types to work with
test_restore_block_type_registry_undoes_registry_changes() replaces
the first block type registered at setUp() and removes the second. With
fewer than two it would stop on an undefined offset instead of a clear
failure. Core registers many block types, so this never happens today;
the assertion states the precondition.
Raised by CodeRabbit on PR #69738.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Drop per-class REQUEST_URI restores the base now covers
WC_Unit_Test_Case now snapshots all of $_SERVER at the end of setUp()
and puts it back in tearDown(), REQUEST_URI included.
That left ten test classes carrying their own copy of the same
cleanup: a property or local holding the original value, a save in
setUp(), setUpBeforeClass() or the test body, and a restore in
tearDown(), tearDownAfterClass() or at the end of the test. The copies
disagreed on details (some sanitized or unslashed the value, some
turned a missing key into an empty string), and none of them did
anything the base does not now do more exactly.
Remove those copies and keep every line that sets REQUEST_URI to a
value a test needs. Changed classes:
- WC_Admin_Functions_Test: in-test save and end-of-test restore.
- WC_Helper_API_Backoff_Test: property, setUp() save, tearDown()
restore.
- WC_Admin_Product_Export_View_Test: save and restore inside
render_export_view(); the try/finally stays for the other globals.
- WC_Form_Handler_Test: property, setUp() save, tearDown() restore.
- WooCommerce_Test and WC_Product_Data_Store_CPT_Test: static
property plus setUpBeforeClass()/tearDownAfterClass() overrides that
only existed for the URI.
- WC_REST_Authentication_Tests: had_request_uri/original_request_uri
properties, setUp() save, tearDown() restore.
- WC_WCCOM_Site_Test: REQUEST_URI dropped from SERVER_KEYS; the loop
still restores the other server keys.
- BlockRegistrationContextTest: property, setUp() save, tearDown()
restore.
- LegacySelect2UsageTrackerTest: property, setUp() save, tearDown()
restore, and the in-test save/restore in the rate-limit test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test: Drop the $_SERVER snapshot WC_Tracks_Test no longer needs
The server details test saved all of $_SERVER and put it back in a
finally block, so its host, referrer and query-laden REQUEST_URI did
not reach later tests. WC_Tracks_Test extends WC_Unit_Test_Case, which
now restores all of $_SERVER after every test (#69738), so the copy
repeats what the base already does.
A probe test that asserts HTTP_REFERER, the example.com host and the
utm_source URI are gone passes after this test in both orders, and
fails after it once the base restore is disabled.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
diff --git a/plugins/woocommerce/changelog/test-drop-redundant-request-uri-restores b/plugins/woocommerce/changelog/test-drop-redundant-request-uri-restores
new file mode 100644
index 00000000000..44ec7b54ad6
--- /dev/null
+++ b/plugins/woocommerce/changelog/test-drop-redundant-request-uri-restores
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Remove per-class REQUEST_URI restores that WC_Unit_Test_Case now does; no production change.
diff --git a/plugins/woocommerce/tests/php/includes/admin/class-wc-admin-functions-test.php b/plugins/woocommerce/tests/php/includes/admin/class-wc-admin-functions-test.php
index 66143f12dab..e7681c5afd2 100644
--- a/plugins/woocommerce/tests/php/includes/admin/class-wc-admin-functions-test.php
+++ b/plugins/woocommerce/tests/php/includes/admin/class-wc-admin-functions-test.php
@@ -32,16 +32,12 @@ class WC_Admin_Functions_Test extends \WC_Unit_Test_Case {
}
// Test with REQUEST_URI.
- $default_uri = isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$_SERVER['REQUEST_URI'] = '/wp-admin/admin.php?page=wc-admin&foo=bar';
$this->assertEquals( admin_url( 'admin.php?page=wc-admin&foo=bar' ), wc_get_current_admin_url() );
// Test if nonce gets removed.
$_SERVER['REQUEST_URI'] = '/wp-admin/admin.php?page=wc-admin&_wpnonce=xxxxxxxxxxxx';
$this->assertEquals( admin_url( 'admin.php?page=wc-admin' ), wc_get_current_admin_url() );
-
- // Restore REQUEST_URI.
- $_SERVER['REQUEST_URI'] = $default_uri;
}
/**
diff --git a/plugins/woocommerce/tests/php/includes/admin/helper/class-wc-helper-api-backoff-test.php b/plugins/woocommerce/tests/php/includes/admin/helper/class-wc-helper-api-backoff-test.php
index 6252538ebb2..224676ee85c 100644
--- a/plugins/woocommerce/tests/php/includes/admin/helper/class-wc-helper-api-backoff-test.php
+++ b/plugins/woocommerce/tests/php/includes/admin/helper/class-wc-helper-api-backoff-test.php
@@ -12,21 +12,12 @@ declare(strict_types=1);
*/
class WC_Helper_API_Backoff_Test extends WC_Unit_Test_Case {
- /**
- * The REQUEST_URI value present before the test ran, restored on tear down.
- *
- * @var string|null
- */
- private $original_request_uri;
-
/**
* Set up before each test.
*/
public function setUp(): void {
parent::setUp();
- // Saved raw to restore verbatim on tear down; not used for any logic.
- $this->original_request_uri = $_SERVER['REQUEST_URI'] ?? null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
// Default to a non-refresh request so is_rate_limited() is not bypassed.
$_SERVER['REQUEST_URI'] = '/wp-admin/admin.php?page=wc-admin';
@@ -39,12 +30,6 @@ class WC_Helper_API_Backoff_Test extends WC_Unit_Test_Case {
public function tearDown(): void {
$this->cleanup_transients();
- if ( null === $this->original_request_uri ) {
- unset( $_SERVER['REQUEST_URI'] );
- } else {
- $_SERVER['REQUEST_URI'] = $this->original_request_uri;
- }
-
parent::tearDown();
}
diff --git a/plugins/woocommerce/tests/php/includes/admin/views/class-wc-admin-product-export-view-test.php b/plugins/woocommerce/tests/php/includes/admin/views/class-wc-admin-product-export-view-test.php
index e669bd7a45c..5d3b92d1421 100644
--- a/plugins/woocommerce/tests/php/includes/admin/views/class-wc-admin-product-export-view-test.php
+++ b/plugins/woocommerce/tests/php/includes/admin/views/class-wc-admin-product-export-view-test.php
@@ -119,8 +119,6 @@ class WC_Admin_Product_Export_View_Test extends WC_Unit_Test_Case {
$original_request = $_REQUEST; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Snapshot test globals before constructing the isolated request.
$original_user_id = get_current_user_id();
$original_buffer_level = ob_get_level();
- $had_request_uri = isset( $_SERVER['REQUEST_URI'] );
- $original_request_uri = $had_request_uri ? $_SERVER['REQUEST_URI'] : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Preserve the exact pre-test server value for restoration.
$had_wp_scripts = isset( $wp_scripts );
$original_scripts_queue = $had_wp_scripts ? $wp_scripts->queue : array();
$original_scripts_to_do = $had_wp_scripts ? $wp_scripts->to_do : array();
@@ -149,11 +147,6 @@ class WC_Admin_Product_Export_View_Test extends WC_Unit_Test_Case {
$_GET = $original_get; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Restore the exact globals captured before the test request.
$_REQUEST = $original_request; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Restore the exact globals captured before the test request.
- if ( $had_request_uri ) {
- $_SERVER['REQUEST_URI'] = $original_request_uri;
- } else {
- unset( $_SERVER['REQUEST_URI'] );
- }
if ( $had_wp_scripts ) {
$wp_scripts->queue = $original_scripts_queue;
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-form-handler-test.php b/plugins/woocommerce/tests/php/includes/class-wc-form-handler-test.php
index 1be65b1cf39..eb18b8ca0af 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-form-handler-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-form-handler-test.php
@@ -23,13 +23,6 @@ class WC_Form_Handler_Test extends WC_Unit_Test_Case {
*/
private array $original_get = array();
- /**
- * Original request URI.
- *
- * @var string|null
- */
- private ?string $original_request_uri = null;
-
/**
* Original POST data.
*
@@ -64,8 +57,6 @@ class WC_Form_Handler_Test extends WC_Unit_Test_Case {
public function setUp(): void {
parent::setUp();
- $this->original_request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_url( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : null;
-
$this->original_get = $_GET; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$this->original_post = $_POST; // phpcs:ignore WordPress.Security.NonceVerification.Missing
$this->original_request = $_REQUEST; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
@@ -86,12 +77,7 @@ class WC_Form_Handler_Test extends WC_Unit_Test_Case {
remove_filter( 'wp_redirect', array( $this, 'intercept_redirect' ) );
$this->reset_cancel_order_handled_flag();
- $_GET = $this->original_get;
- if ( null === $this->original_request_uri ) {
- unset( $_SERVER['REQUEST_URI'] );
- } else {
- $_SERVER['REQUEST_URI'] = $this->original_request_uri;
- }
+ $_GET = $this->original_get;
$_POST = $this->original_post;
$_REQUEST = $this->original_request;
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-tracks-test.php b/plugins/woocommerce/tests/php/includes/class-wc-tracks-test.php
index a42f10004f2..0277a7c6547 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-tracks-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-tracks-test.php
@@ -22,21 +22,15 @@ class WC_Tracks_Test extends \WC_Unit_Test_Case {
* @testdox Server details retain only shared allowlisted query parameters.
*/
public function test_server_details_retain_only_shared_allowlisted_query_parameters(): void {
- $server_snapshot = $_SERVER;
+ $_SERVER['REQUEST_SCHEME'] = 'https';
+ $_SERVER['HTTP_HOST'] = 'example.com';
+ $_SERVER['REQUEST_URI'] = '/wp-json/wc/v3/products?utm_source=google&orderby=price&aff=partner&utm_campaign=spring&per_page=20';
+ $_SERVER['HTTP_REFERER'] = 'https://example.com/wp-admin/admin.php?page=wc-admin&utm_medium=referral#activity';
- try {
- $_SERVER['REQUEST_SCHEME'] = 'https';
- $_SERVER['HTTP_HOST'] = 'example.com';
- $_SERVER['REQUEST_URI'] = '/wp-json/wc/v3/products?utm_source=google&orderby=price&aff=partner&utm_campaign=spring&per_page=20';
- $_SERVER['HTTP_REFERER'] = 'https://example.com/wp-admin/admin.php?page=wc-admin&utm_medium=referral#activity';
+ $details = WC_Tracks::get_server_details();
- $details = WC_Tracks::get_server_details();
-
- $this->assertSame( 'https://example.com/wp-json/wc/v3/products?utm_source=google&aff=partner&utm_campaign=spring', $details['_dl'] );
- $this->assertSame( 'https://example.com/wp-admin/admin.php?page=wc-admin&utm_medium=referral', $details['_dr'] );
- } finally {
- $_SERVER = $server_snapshot;
- }
+ $this->assertSame( 'https://example.com/wp-json/wc/v3/products?utm_source=google&aff=partner&utm_campaign=spring', $details['_dl'] );
+ $this->assertSame( 'https://example.com/wp-admin/admin.php?page=wc-admin&utm_medium=referral', $details['_dr'] );
}
/**
diff --git a/plugins/woocommerce/tests/php/includes/class-woocommerce-test.php b/plugins/woocommerce/tests/php/includes/class-woocommerce-test.php
index 0698ff7c204..6f042493b32 100644
--- a/plugins/woocommerce/tests/php/includes/class-woocommerce-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-woocommerce-test.php
@@ -9,23 +9,6 @@ use Automattic\WooCommerce\Internal\Utilities\LegacyRestApiStub;
*/
class WooCommerce_Test extends \WC_Unit_Test_Case {
- /**
- * The default URI.
- *
- * @var string
- */
- private static $default_uri;
-
- /**
- * Store the default URI.
- *
- */
- public static function setUpBeforeClass(): void {
- parent::setUpBeforeClass();
- self::$default_uri = isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
- }
-
-
/**
* Setup test data. Called before every test.
*/
@@ -40,15 +23,6 @@ class WooCommerce_Test extends \WC_Unit_Test_Case {
wp_set_current_user( $this->user );
}
-
- /**
- * Restore the default URI.
- */
- public static function tearDownAfterClass(): void {
- parent::tearDownAfterClass();
- $_SERVER['REQUEST_URI'] = self::$default_uri;
- }
-
/**
* Test that the $api property is defined and holds an instance of LegacyRestApiStub
* (the Legacy REST API was removed in WooCommerce 9.0).
diff --git a/plugins/woocommerce/tests/php/includes/data-stores/class-wc-product-data-store-cpt-test.php b/plugins/woocommerce/tests/php/includes/data-stores/class-wc-product-data-store-cpt-test.php
index fe1928e8f0d..2c2c2203097 100644
--- a/plugins/woocommerce/tests/php/includes/data-stores/class-wc-product-data-store-cpt-test.php
+++ b/plugins/woocommerce/tests/php/includes/data-stores/class-wc-product-data-store-cpt-test.php
@@ -8,21 +8,6 @@ use Automattic\WooCommerce\Internal\CostOfGoodsSold\CogsAwareUnitTestSuiteTrait;
class WC_Product_Data_Store_CPT_Test extends WC_Unit_Test_Case {
use CogsAwareUnitTestSuiteTrait;
- /**
- * The default URI.
- *
- * @var string
- */
- private static $default_uri;
-
- /**
- * Store the default URI.
- */
- public static function setUpBeforeClass(): void {
- parent::setUpBeforeClass();
- self::$default_uri = isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
- }
-
/**
* Runs after each test.
*/
@@ -33,14 +18,6 @@ class WC_Product_Data_Store_CPT_Test extends WC_Unit_Test_Case {
remove_all_filters( 'woocommerce_save_cogs_value' );
}
- /**
- * Restore the default URI.
- */
- public static function tearDownAfterClass(): void {
- parent::tearDownAfterClass();
- $_SERVER['REQUEST_URI'] = self::$default_uri;
- }
-
/**
* @testdox Variations should appear when searching for parent product's SKU.
*/
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/class-wc-rest-authentication-tests.php b/plugins/woocommerce/tests/php/includes/rest-api/class-wc-rest-authentication-tests.php
index 88219b4907a..c2a9c5299fe 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/class-wc-rest-authentication-tests.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/class-wc-rest-authentication-tests.php
@@ -18,20 +18,6 @@ class WC_REST_Authentication_Tests extends WC_REST_Unit_Test_Case {
*/
private $original_resolved_route;
- /**
- * Whether REQUEST_URI was set before the test ran.
- *
- * @var bool
- */
- private $had_request_uri;
-
- /**
- * The request URI the test ran with, verbatim.
- *
- * @var string|null
- */
- private $original_request_uri;
-
/**
* The authentication state WC_REST_Authentication held before the test ran.
*
@@ -52,13 +38,6 @@ class WC_REST_Authentication_Tests extends WC_REST_Unit_Test_Case {
$this->original_resolved_route = $wp->query_vars['rest_route'] ?? null;
unset( $wp->query_vars['rest_route'] );
- $this->had_request_uri = array_key_exists( 'REQUEST_URI', $_SERVER );
- // Stored and restored verbatim, so tearDown leaves $_SERVER exactly as it found it. Sanitizing
- // would drop the characters these tests use to tell a WooCommerce route from one that only looks
- // like it, and unslashing would undo what wp_magic_quotes() put there.
- // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Round-tripped between $_SERVER and $_SERVER, never output or stored.
- $this->original_request_uri = $this->had_request_uri ? $_SERVER['REQUEST_URI'] : null;
-
foreach ( array( 'user', 'error', 'auth_method' ) as $name ) {
$this->original_authentication_state[ $name ] = $this->authentication_property( $name )->getValue( $this->sut );
}
@@ -76,12 +55,6 @@ class WC_REST_Authentication_Tests extends WC_REST_Unit_Test_Case {
$wp->query_vars['rest_route'] = $this->original_resolved_route;
}
- if ( $this->had_request_uri ) {
- $_SERVER['REQUEST_URI'] = $this->original_request_uri;
- } else {
- unset( $_SERVER['REQUEST_URI'] );
- }
-
foreach ( $this->original_authentication_state as $name => $value ) {
$this->authentication_property( $name )->setValue( $this->sut, $value );
}
diff --git a/plugins/woocommerce/tests/php/includes/wccom-site/class-wc-wccom-site-test.php b/plugins/woocommerce/tests/php/includes/wccom-site/class-wc-wccom-site-test.php
index bff37c773cd..4d4c540436c 100644
--- a/plugins/woocommerce/tests/php/includes/wccom-site/class-wc-wccom-site-test.php
+++ b/plugins/woocommerce/tests/php/includes/wccom-site/class-wc-wccom-site-test.php
@@ -10,10 +10,10 @@ class WC_WCCOM_Site_Test extends WC_Unit_Test_Case {
private const ACCESS_TOKEN_SECRET = 'test-access-token-secret';
- private const SERVER_KEYS = array( 'REQUEST_URI', 'REQUEST_METHOD', 'HTTP_HOST', 'HTTP_AUTHORIZATION', 'HTTP_X_WOO_SIGNATURE' );
+ private const SERVER_KEYS = array( 'REQUEST_METHOD', 'HTTP_HOST', 'HTTP_AUTHORIZATION', 'HTTP_X_WOO_SIGNATURE' );
/**
- * $_SERVER values the test overwrites, as they were before it ran.
+ * $_SERVER values the test overwrites, as they were before it ran. WC_Unit_Test_Case restores REQUEST_URI.
*
* @var array<string, mixed>
*/
diff --git a/plugins/woocommerce/tests/php/src/Blocks/Domain/BlockRegistrationContextTest.php b/plugins/woocommerce/tests/php/src/Blocks/Domain/BlockRegistrationContextTest.php
index 397152244f7..fba8466b1a0 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/Domain/BlockRegistrationContextTest.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/Domain/BlockRegistrationContextTest.php
@@ -18,13 +18,6 @@ class BlockRegistrationContextTest extends WC_Unit_Test_Case {
*/
private BlockRegistrationContext $sut;
- /**
- * The original REQUEST_URI, restored after each test.
- *
- * @var string
- */
- private string $original_uri;
-
/**
* The original $pagenow, restored after each test.
*
@@ -38,7 +31,6 @@ class BlockRegistrationContextTest extends WC_Unit_Test_Case {
public function setUp(): void {
parent::setUp();
$this->sut = new BlockRegistrationContext();
- $this->original_uri = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$this->original_pagenow = $GLOBALS['pagenow'] ?? '';
}
@@ -46,8 +38,7 @@ class BlockRegistrationContextTest extends WC_Unit_Test_Case {
* Restore the request globals after each test.
*/
public function tearDown(): void {
- $_SERVER['REQUEST_URI'] = $this->original_uri;
- $GLOBALS['pagenow'] = $this->original_pagenow; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
+ $GLOBALS['pagenow'] = $this->original_pagenow; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
unset( $_GET['rest_route'], $_GET['wc-ajax'], $_GET['page'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
set_current_screen( 'front' );
parent::tearDown();
diff --git a/plugins/woocommerce/tests/php/src/Internal/LegacyAssets/LegacySelect2UsageTrackerTest.php b/plugins/woocommerce/tests/php/src/Internal/LegacyAssets/LegacySelect2UsageTrackerTest.php
index eac41c7e731..d378755bb58 100644
--- a/plugins/woocommerce/tests/php/src/Internal/LegacyAssets/LegacySelect2UsageTrackerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/LegacyAssets/LegacySelect2UsageTrackerTest.php
@@ -31,22 +31,13 @@ class LegacySelect2UsageTrackerTest extends WC_Unit_Test_Case {
'my-extension-transitive',
);
- /**
- * Original request URI.
- *
- * @var string|null
- */
- private ?string $original_request_uri = null;
-
/**
* Set up test fixtures.
*/
public function setUp(): void {
parent::setUp();
- // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Test fixture preserves the raw request URI for restoration.
- $this->original_request_uri = isset( $_SERVER['REQUEST_URI'] ) ? (string) $_SERVER['REQUEST_URI'] : null;
- $_SERVER['REQUEST_URI'] = '/';
+ $_SERVER['REQUEST_URI'] = '/';
$this->reset_scripts();
$this->register_legacy_select2_scripts();
$this->sut = new LegacySelect2UsageTracker();
@@ -59,12 +50,6 @@ class LegacySelect2UsageTrackerTest extends WC_Unit_Test_Case {
$this->reset_scripts();
set_current_screen( 'front' );
- if ( null === $this->original_request_uri ) {
- unset( $_SERVER['REQUEST_URI'] );
- } else {
- $_SERVER['REQUEST_URI'] = $this->original_request_uri;
- }
-
parent::tearDown();
}
@@ -284,7 +269,6 @@ class LegacySelect2UsageTrackerTest extends WC_Unit_Test_Case {
* @testdox Should record each detected usage event only once per week.
*/
public function test_records_each_detected_usage_event_only_once_per_week(): void {
- $original_request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : null;
$_SERVER['REQUEST_URI'] = '/shop/?filter=featured';
$event = array(
'context' => 'frontend',
@@ -379,12 +363,6 @@ class LegacySelect2UsageTrackerTest extends WC_Unit_Test_Case {
$this->assertSame( 2, $sut->usage_event_calls, 'Each detection should scan the script registry before rate limiting the exact usage event.' );
$this->delete_usage_event_transient( $event );
-
- if ( null === $original_request_uri ) {
- unset( $_SERVER['REQUEST_URI'] );
- } else {
- $_SERVER['REQUEST_URI'] = $original_request_uri;
- }
}
/**