Skip to content

Tests: Restore the original $wp_rewrite after each Tests_Rewrite_Tags test. - #16250

Closed
mayankmajeji wants to merge 1 commit into
WordPress:trunkfrom
mayankmajeji:tests/66755-rewrite-tags
Closed

mayankmajeji wants to merge 1 commit into
WordPress:trunkfrom
mayankmajeji:tests/66755-rewrite-tags

Conversation

@mayankmajeji

Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/66755

Tests_Rewrite_Tags::set_up() replaces the global $wp_rewrite with a new WP_Rewrite and never puts the original back, so every later class runs with that new object. The new object does not have the rewrite rules registered while WordPress loaded, such as the sitemap rules from WP_Sitemaps::register_rewrites(), and the rules it generates are kept.

Tests_Canonical_Sitemaps::set_up() adds the sitemap rules again, but they are not in the generated rules. /wp-sitemap-posts-post-1.xml/ then matches the page rule (pagename=wp-sitemap-posts-post-1.xml) instead of the sitemap rule, so there is no sitemap query var and redirect_canonical() does not remove the trailing slash. The index URL fails the same way:

  • test_remove_trailing_slashes_for_sitemap_index_requests() gets /wp-sitemap.xml/ instead of /wp-sitemap.xml.
  • test_remove_trailing_slashes_for_sitemap_requests() gets /wp-sitemap-posts-post-1.xml/ instead of /wp-sitemap-posts-post-1.xml.

Whichever of the two runs first in Tests_Canonical_Sitemaps fails, and both pass on their own.

What changes

  • Tests_Rewrite_Tags keeps the $wp_rewrite from before each test and restores it in a new tear_down(), before parent::tear_down().

Testing

Locally with PHP 8.4.28, MySQL 9.7.4 and PHPUnit 9.6.39, on trunk at r65788.

The config below runs rewrite/rewriteTags.php and then canonical/sitemaps.php.

  • vendor/bin/phpunit -c pair.xml: 1 failure on trunk (test_remove_trailing_slashes_for_sitemap_index_requests(), /wp-sitemap.xml/), OK (45 tests, 159 assertions) with this change.
  • vendor/bin/phpunit -c pair.xml --filter '/^(?!Tests_Canonical_Sitemaps::)|::test_remove_trailing_slashes_for_sitemap_requests$/': 1 failure on trunk (/wp-sitemap-posts-post-1.xml/), OK (13 tests, 126 assertions) with this change.
  • Both the same on multisite (the config with <const name="WP_TESTS_MULTISITE" value="1" /> added).
  • Tests_Rewrite_Tags in default order, with --order-by=reverse, with --order-by=random --random-order-seed=7, and on multisite: OK with this change.
  • --group rewrite (1038 tests, also on multisite), --group sitemaps (209 tests) and --group canonical (221 tests): no failures with this change.
  • Found with --order-by=random --random-order-seed=7 on trunk at r65781 and narrowed to rewrite/rewriteTags.php by bisecting the 370 classes that ran before the sitemap test.
  • Full suite in default order, with this change: OK on single site (32355 tests) and on multisite (33230 tests).
  • PHPCS reports nothing on the changed file.
<phpunit bootstrap="tests/phpunit/includes/bootstrap.php" backupGlobals="false" convertErrorsToExceptions="true" convertWarningsToExceptions="true" convertNoticesToExceptions="true" convertDeprecationsToExceptions="true">
	<testsuites>
		<testsuite name="pair">
			<file>tests/phpunit/tests/rewrite/rewriteTags.php</file>
			<file>tests/phpunit/tests/canonical/sitemaps.php</file>
		</testsuite>
	</testsuites>
	<php>
		<const name="WP_RUN_CORE_TESTS" value="1" />
	</php>
</phpunit>

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Investigation, code, local testing and PR wording. I reviewed the reasoning, the code and the test results, and I take responsibility for the contribution.

…ags` test.

The class replaced the global with a new `WP_Rewrite` that lacks the rules registered at load time, such as the sitemap rules, so later classes resolved sitemap URLs through the page rule. Restore the original object in `tear_down()`.

See #66755.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props mayankmajeji.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the test-isolation change that preserves and restores the global $wp_rewrite instance.

No open issues 🎉

What changed in this PR

This PR improves test isolation by restoring the original global $wp_rewrite after each Tests_Rewrite_Tags test, preventing replacement rewrite state from affecting later tests, including canonical sitemap tests.

Changes:

  • Saves the original WP_Rewrite instance during setup.
  • Restores it during teardown, before calling parent::tear_down().
File Description
tests/​phpunit/​tests/​rewrite/​rewriteTags.php Preserves and restores the global rewrite instance around each test.

Review effort: Balanced

@lancewillett lancewillett left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reproduced the sitemap redirect failure after the rewrite-tag tests on single-site and multisite. Restoring the original rewrite object kept its registered sitemap rules and made both runs pass (13 tests, 126 assertions each).

AI review · gpt-6.2-astra

wporg-sync pushed a commit that referenced this pull request Nov 25, 2026
Keep the sitemap rules registered at bootstrap available to subsequent canonical redirect tests.

Developed in: #16250

Props mayankmajeji.
See #66755.


git-svn-id: https://develop.svn.wordpress.org/trunk@65796 602fd350-edb4-49c9-b593-d223f7449a82
@lancewillett

Copy link
Copy Markdown
Member

wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Nov 25, 2026
Keep the sitemap rules registered at bootstrap available to subsequent canonical redirect tests.

Developed in: WordPress/wordpress-develop#16250

Props mayankmajeji.
See #66755.

Built from https://develop.svn.wordpress.org/trunk@65796


git-svn-id: http://core.svn.wordpress.org/trunk@64825 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

3 participants