Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tests/phpunit/includes/abstract-testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
9 changes: 9 additions & 0 deletions tests/phpunit/includes/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
42 changes: 42 additions & 0 deletions tests/phpunit/tests/rewrite/addRewriteEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading