diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index 55a9924fb23c3..695f8713f6b69 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -183,6 +183,10 @@ public function tear_down() { $wp_query = $wp_the_query; $wp = new WP(); + if ( isset( $GLOBALS['_wp_tests_initial_public_query_vars'] ) ) { + $wp->public_query_vars = $GLOBALS['_wp_tests_initial_public_query_vars']; + } + // Reset globals related to the post loop and `setup_postdata()`. $post_globals = array( 'post', 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' ); foreach ( $post_globals as $global ) { diff --git a/tests/phpunit/includes/bootstrap.php b/tests/phpunit/includes/bootstrap.php index e308dcb5a6e1c..4594d5fba747b 100644 --- a/tests/phpunit/includes/bootstrap.php +++ b/tests/phpunit/includes/bootstrap.php @@ -299,6 +299,15 @@ function wp_tests_options( $value ) { // Load WordPress. require_once ABSPATH . 'wp-settings.php'; +/* + * Preserve public query variables registered by plugins during bootstrap. + * + * Core tests reset these variables between tests, while plugin tests expect + * registrations made on `init` to remain available throughout the test run. + */ +if ( ! defined( 'WP_RUN_CORE_TESTS' ) || ! WP_RUN_CORE_TESTS ) { + $GLOBALS['_wp_tests_initial_public_query_vars'] = $GLOBALS['wp']->public_query_vars; +} // Override the PHPMailer. require_once __DIR__ . '/mock-mailer.php'; diff --git a/tests/phpunit/tests/rewrite/addRewriteEndpoint.php b/tests/phpunit/tests/rewrite/addRewriteEndpoint.php index 6527f32929917..20c3c53c1da4b 100644 --- a/tests/phpunit/tests/rewrite/addRewriteEndpoint.php +++ b/tests/phpunit/tests/rewrite/addRewriteEndpoint.php @@ -47,6 +47,48 @@ public function test_should_register_query_using_query_var_param_if_not_null() { $this->assertContains( 'bar', $GLOBALS['wp']->public_query_vars ); } + /** + * Sets up the non-core test-suite state before the test case is torn down. + * + * @ticket 37207 + * + * @return array{exists: bool, value: string[]|null} Previous global state. + */ + public function test_should_preserve_query_vars_registered_during_plugin_bootstrap() { + $previous_state = array( + 'exists' => isset( $GLOBALS['_wp_tests_initial_public_query_vars'] ), + 'value' => $GLOBALS['_wp_tests_initial_public_query_vars'] ?? null, + ); + + add_rewrite_endpoint( 'plugin-endpoint', EP_ROOT ); + $this->assertContains( 'plugin-endpoint', $GLOBALS['wp']->public_query_vars ); + + $GLOBALS['_wp_tests_initial_public_query_vars'] = $GLOBALS['wp']->public_query_vars; + + return $previous_state; + } + + /** + * Verifies the query variables after the preceding test's tear down. + * + * @ticket 37207 + * + * @depends test_should_preserve_query_vars_registered_during_plugin_bootstrap + * + * @param array{exists: bool, value: string[]|null} $previous_state Previous global state. + */ + public function test_should_restore_query_vars_registered_during_plugin_bootstrap( $previous_state ) { + try { + $this->assertContains( 'plugin-endpoint', $GLOBALS['wp']->public_query_vars ); + } finally { + if ( $previous_state['exists'] ) { + $GLOBALS['_wp_tests_initial_public_query_vars'] = $previous_state['value']; + } else { + unset( $GLOBALS['_wp_tests_initial_public_query_vars'] ); + } + } + } + /** * @ticket 25143 */