Skip to content

Tests: Add unit tests for is_sitemap() (#51543)#2

Merged
masteradhoc merged 1 commit into
masteradhoc:51543-is-sitemap-conditionalfrom
nimesh-xecurify:51543-unit-tests
Jun 16, 2026
Merged

Tests: Add unit tests for is_sitemap() (#51543)#2
masteradhoc merged 1 commit into
masteradhoc:51543-is-sitemap-conditionalfrom
nimesh-xecurify:51543-unit-tests

Conversation

@nimesh-xecurify

Copy link
Copy Markdown

This PR adds unit test coverage for is_sitemap() from WordPress#12142, targeted at the 51543-is-sitemap-conditional branch so the tests show up directly in that PR.

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

What's added

tests/phpunit/tests/query/isSitemap.php — 12 tests (12 assertions, all green locally):

  • WP_Query::$is_sitemap defaults to false
  • The flag is set for the sitemap index route (sitemap=index) and for subtype routes (e.g. sitemap=posts&sitemap-subtype=post)
  • WP_Query::is_sitemap() returns a bool
  • An empty sitemap query var does not set the flag
  • The sitemap stylesheet route (sitemap-stylesheet) is not flagged as a sitemap
  • is_robots() takes precedence over is_sitemap() (matching the parse_query() elseif chain)
  • A regular post query is never flagged as a sitemap
  • A sitemap request is not treated as the home/front page (the practical motivation from #51542)
  • The global is_sitemap() conditional tag reflects the main query (verified via go_to() + assertQueryTrue())
  • is_sitemap() is false on the home page
  • The global is_sitemap() triggers _doing_it_wrong() and returns false before the query is run

It also adds is_sitemap to WP_UnitTestCase::assertQueryTrue() next to the existing is_robots/is_favicon entries, for consistency.

Notes for reviewers

  • The go_to()-based tests remove the render_sitemaps action from template_redirect so the renderer does not exit mid-test, and create published posts so the request isn't turned into a 404.
  • That last point surfaces a behavioral edge case worth confirming: WP::handle_404() exempts admin/robots/favicon (class-wp.php), but not sitemaps. On a site with zero published posts, a sitemap request falls through to set_404(), which resets $is_sitemap via init_query_flags(). For normal sites the underlying query returns posts so the flag survives, but it may be worth deciding whether handle_404() should also exempt is_sitemap().

Test run

OK (12 tests, 20 assertions)

PHPCS (WordPress-Core) clean on both changed files; the existing Tests_Query_Conditionals suite still passes (121 tests) after the assertQueryTrue() addition.

Adds test coverage for the is_sitemap() conditional query tag and the
WP_Query::$is_sitemap property introduced for #51543.

Tests cover the WP_Query property default, the sitemap index and subtype
routes, the sitemap stylesheet route exclusion, is_robots() precedence,
the guarantee that a sitemap request is not treated as the home/front
page, the global is_sitemap() conditional tag, and the _doing_it_wrong()
notice when called before the query is run.

Also adds is_sitemap to WP_UnitTestCase::assertQueryTrue() alongside the
existing is_robots and is_favicon conditionals for consistency.

See #51543.
@masteradhoc masteradhoc merged commit 40d0969 into masteradhoc:51543-is-sitemap-conditional Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants