From 9d83b9a342b5e25d15fbdd3e6586dbb521fd1510 Mon Sep 17 00:00:00 2001 From: Nimesh Date: Tue, 16 Jun 2026 11:51:49 +0530 Subject: [PATCH] Tests: Add unit tests for the is_sitemap() conditional tag. 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. --- tests/phpunit/includes/abstract-testcase.php | 1 + tests/phpunit/tests/query/isSitemap.php | 213 +++++++++++++++++++ 2 files changed, 214 insertions(+) create mode 100644 tests/phpunit/tests/query/isSitemap.php diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index b8e8598362ec5..55a9924fb23c3 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -1187,6 +1187,7 @@ public function assertQueryTrue( ...$prop ) { 'is_preview', 'is_robots', 'is_favicon', + 'is_sitemap', 'is_search', 'is_single', 'is_singular', diff --git a/tests/phpunit/tests/query/isSitemap.php b/tests/phpunit/tests/query/isSitemap.php new file mode 100644 index 0000000000000..004aa1ed86455 --- /dev/null +++ b/tests/phpunit/tests/query/isSitemap.php @@ -0,0 +1,213 @@ +post->create_many( 3 ); + } + + public function set_up() { + parent::set_up(); + + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); + + create_initial_taxonomies(); + } + + /** + * The property defaults to false on a freshly initialized query. + * + * @covers WP_Query::init + */ + public function test_is_sitemap_defaults_to_false() { + $query = new WP_Query(); + + $this->assertFalse( $query->is_sitemap, 'The $is_sitemap property should default to false.' ); + $this->assertFalse( $query->is_sitemap(), 'WP_Query::is_sitemap() should return false by default.' ); + } + + /** + * The flag is set when the "sitemap" query var is present (sitemap index route). + * + * @covers WP_Query::parse_query + * @covers WP_Query::is_sitemap + */ + public function test_is_sitemap_true_for_sitemap_index() { + $query = new WP_Query( array( 'sitemap' => 'index' ) ); + + $this->assertTrue( $query->is_sitemap, 'The $is_sitemap property should be true for a sitemap query.' ); + $this->assertTrue( $query->is_sitemap(), 'WP_Query::is_sitemap() should return true for a sitemap query.' ); + } + + /** + * The flag is set for a sitemap subtype route (e.g. wp-sitemap-posts-post-1.xml). + * + * @covers WP_Query::parse_query + * @covers WP_Query::is_sitemap + */ + public function test_is_sitemap_true_for_sitemap_subtype() { + $query = new WP_Query( + array( + 'sitemap' => 'posts', + 'sitemap-subtype' => 'post', + 'paged' => 1, + ) + ); + + $this->assertTrue( $query->is_sitemap(), 'WP_Query::is_sitemap() should return true for a sitemap subtype query.' ); + } + + /** + * is_sitemap() must return a boolean. + * + * @covers WP_Query::is_sitemap + */ + public function test_is_sitemap_return_type_is_bool() { + $query = new WP_Query( array( 'sitemap' => 'index' ) ); + + $this->assertIsBool( $query->is_sitemap(), 'WP_Query::is_sitemap() should return a boolean.' ); + } + + /** + * An empty "sitemap" query var must not set the flag. + * + * @covers WP_Query::parse_query + */ + public function test_is_sitemap_false_for_empty_sitemap_var() { + $query = new WP_Query( array( 'sitemap' => '' ) ); + + $this->assertFalse( $query->is_sitemap(), 'An empty "sitemap" query var should not set is_sitemap.' ); + } + + /** + * The sitemap stylesheet route uses the "sitemap-stylesheet" query var, which must + * not flag the query as a sitemap. + * + * @covers WP_Query::parse_query + */ + public function test_is_sitemap_false_for_stylesheet_route() { + $query = new WP_Query( array( 'sitemap-stylesheet' => 'sitemap' ) ); + + $this->assertFalse( $query->is_sitemap(), 'The sitemap stylesheet route should not flag the query as a sitemap.' ); + } + + /** + * is_robots takes precedence over is_sitemap in the parse_query branch. + * + * @covers WP_Query::parse_query + */ + public function test_robots_takes_precedence_over_sitemap() { + $query = new WP_Query( + array( + 'robots' => true, + 'sitemap' => 'index', + ) + ); + + $this->assertTrue( $query->is_robots(), 'is_robots() should be true when the robots query var is set.' ); + $this->assertFalse( $query->is_sitemap(), 'is_sitemap() should be false when is_robots() takes precedence.' ); + } + + /** + * A regular query is never flagged as a sitemap. + * + * @covers WP_Query::is_sitemap + */ + public function test_is_sitemap_false_for_regular_query() { + $post_id = self::factory()->post->create(); + + $query = new WP_Query( array( 'p' => $post_id ) ); + + $this->assertFalse( $query->is_sitemap(), 'A regular post query should not be flagged as a sitemap.' ); + } + + /** + * A sitemap query must not also be treated as the home/front page. + * + * This is the practical motivation for the conditional tag: distinguishing a + * sitemap request from the home page (see #51542). + * + * @covers WP_Query::parse_query + */ + public function test_sitemap_query_is_not_home() { + $query = new WP_Query( array( 'sitemap' => 'index' ) ); + + $this->assertTrue( $query->is_sitemap(), 'The sitemap query should be flagged as a sitemap.' ); + $this->assertFalse( $query->is_home(), 'A sitemap query should not be treated as the home page.' ); + $this->assertFalse( $query->is_front_page(), 'A sitemap query should not be treated as the front page.' ); + } + + /** + * The global is_sitemap() conditional tag reflects the main query. + * + * @covers ::is_sitemap + */ + public function test_global_is_sitemap_reflects_main_query() { + // Prevent WP_Sitemaps from rendering and calling exit during go_to(). + remove_action( 'template_redirect', array( wp_sitemaps_get_server(), 'render_sitemaps' ) ); + + $this->go_to( home_url( '/?sitemap=index' ) ); + + $this->assertTrue( is_sitemap(), 'is_sitemap() should be true on a sitemap request.' ); + + // is_sitemap should be the only conditional that is true for a sitemap request. + $this->assertQueryTrue( 'is_sitemap' ); + } + + /** + * The global is_sitemap() conditional tag is false for a non-sitemap request. + * + * @covers ::is_sitemap + */ + public function test_global_is_sitemap_false_on_home() { + $this->go_to( home_url( '/' ) ); + + $this->assertFalse( is_sitemap(), 'is_sitemap() should be false on the home page.' ); + $this->assertTrue( is_home(), 'is_home() should be true on the home page.' ); + } + + /** + * The global is_sitemap() returns false and triggers a notice when the query + * has not yet run. + * + * @covers ::is_sitemap + * + * @expectedIncorrectUsage is_sitemap + */ + public function test_global_is_sitemap_before_query_is_run() { + $wp_query_temp = $GLOBALS['wp_query']; + unset( $GLOBALS['wp_query'] ); + + $result = is_sitemap(); + + $GLOBALS['wp_query'] = $wp_query_temp; + + $this->assertFalse( $result, 'is_sitemap() should return false before the query is run.' ); + } +}