|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the is_sitemap() conditional tag and the WP_Query::$is_sitemap property. |
| 5 | + * |
| 6 | + * This exercises both query.php and class-wp-query.php: query vars are fed through |
| 7 | + * WP_Query, then the effects on the wp_query object are tested. |
| 8 | + * |
| 9 | + * @group query |
| 10 | + * @group sitemaps |
| 11 | + */ |
| 12 | +class Tests_Query_IsSitemap extends WP_UnitTestCase { |
| 13 | + |
| 14 | + /** |
| 15 | + * Set up the shared fixture. |
| 16 | + * |
| 17 | + * Published posts are required so that a sitemap request does not turn into a |
| 18 | + * 404 in WP::handle_404(), which would reset the is_sitemap flag via set_404(). |
| 19 | + * |
| 20 | + * @param WP_UnitTest_Factory $factory Factory instance. |
| 21 | + */ |
| 22 | + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void { |
| 23 | + $factory->post->create_many( 3 ); |
| 24 | + } |
| 25 | + |
| 26 | + public function set_up() { |
| 27 | + parent::set_up(); |
| 28 | + |
| 29 | + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); |
| 30 | + |
| 31 | + create_initial_taxonomies(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * The property defaults to false on a freshly initialized query. |
| 36 | + * |
| 37 | + * @ticket 51543 |
| 38 | + * |
| 39 | + * @covers WP_Query::is_sitemap |
| 40 | + */ |
| 41 | + public function test_is_sitemap_defaults_to_false(): void { |
| 42 | + $query = new WP_Query(); |
| 43 | + |
| 44 | + $this->assertFalse( $query->is_sitemap, 'The $is_sitemap property should default to false.' ); |
| 45 | + $this->assertFalse( $query->is_sitemap(), 'WP_Query::is_sitemap() should return false by default.' ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * The property gets reset when initialized. |
| 50 | + * |
| 51 | + * @ticket 51543 |
| 52 | + * |
| 53 | + * @covers WP_Query::init |
| 54 | + * @covers WP_Query::init_query_flags |
| 55 | + */ |
| 56 | + public function test_is_sitemap_gets_reset_to_false(): void { |
| 57 | + $query = new WP_Query(); |
| 58 | + |
| 59 | + $query->is_sitemap = true; |
| 60 | + $query->init(); |
| 61 | + $this->assertFalse( $query->is_sitemap, 'The $is_sitemap property should initialize as false.' ); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * The flag is set when the "sitemap" query var is present (sitemap index route). |
| 66 | + * |
| 67 | + * @ticket 51543 |
| 68 | + * |
| 69 | + * @covers WP_Query::parse_query |
| 70 | + * @covers WP_Query::is_sitemap |
| 71 | + */ |
| 72 | + public function test_is_sitemap_true_for_sitemap_index(): void { |
| 73 | + $query = new WP_Query( array( 'sitemap' => 'index' ) ); |
| 74 | + |
| 75 | + $this->assertTrue( $query->is_sitemap, 'The $is_sitemap property should be true for a sitemap query.' ); |
| 76 | + $this->assertTrue( $query->is_sitemap(), 'WP_Query::is_sitemap() should return true for a sitemap query.' ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * The flag is set for a sitemap subtype route (e.g. wp-sitemap-posts-post-1.xml). |
| 81 | + * |
| 82 | + * @ticket 51543 |
| 83 | + * |
| 84 | + * @covers WP_Query::parse_query |
| 85 | + * @covers WP_Query::is_sitemap |
| 86 | + */ |
| 87 | + public function test_is_sitemap_true_for_sitemap_subtype(): void { |
| 88 | + $query = new WP_Query( |
| 89 | + array( |
| 90 | + 'sitemap' => 'posts', |
| 91 | + 'sitemap-subtype' => 'post', |
| 92 | + 'paged' => 1, |
| 93 | + ) |
| 94 | + ); |
| 95 | + |
| 96 | + $this->assertTrue( $query->is_sitemap(), 'WP_Query::is_sitemap() should return true for a sitemap subtype query.' ); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * An empty "sitemap" query var must not set the flag. |
| 101 | + * |
| 102 | + * @ticket 51543 |
| 103 | + * |
| 104 | + * @covers WP_Query::parse_query |
| 105 | + */ |
| 106 | + public function test_is_sitemap_false_for_empty_sitemap_var(): void { |
| 107 | + $query = new WP_Query( array( 'sitemap' => '' ) ); |
| 108 | + |
| 109 | + $this->assertFalse( $query->is_sitemap(), 'An empty "sitemap" query var should not set is_sitemap.' ); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * The sitemap stylesheet route uses the "sitemap-stylesheet" query var, which must |
| 114 | + * not flag the query as a sitemap. |
| 115 | + * |
| 116 | + * @ticket 51543 |
| 117 | + * |
| 118 | + * @covers WP_Query::parse_query |
| 119 | + */ |
| 120 | + public function test_is_sitemap_false_for_stylesheet_route(): void { |
| 121 | + $query = new WP_Query( array( 'sitemap-stylesheet' => 'sitemap' ) ); |
| 122 | + |
| 123 | + $this->assertFalse( $query->is_sitemap(), 'The sitemap stylesheet route should not flag the query as a sitemap.' ); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * is_robots takes precedence over is_sitemap in the parse_query branch. |
| 128 | + * |
| 129 | + * @ticket 51543 |
| 130 | + * |
| 131 | + * @covers WP_Query::parse_query |
| 132 | + */ |
| 133 | + public function test_robots_takes_precedence_over_sitemap(): void { |
| 134 | + $query = new WP_Query( |
| 135 | + array( |
| 136 | + 'robots' => true, |
| 137 | + 'sitemap' => 'index', |
| 138 | + ) |
| 139 | + ); |
| 140 | + |
| 141 | + $this->assertTrue( $query->is_robots(), 'is_robots() should be true when the robots query var is set.' ); |
| 142 | + $this->assertFalse( $query->is_sitemap(), 'is_sitemap() should be false when is_robots() takes precedence.' ); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * A regular query is never flagged as a sitemap. |
| 147 | + * |
| 148 | + * @ticket 51543 |
| 149 | + * |
| 150 | + * @covers WP_Query::is_sitemap |
| 151 | + */ |
| 152 | + public function test_is_sitemap_false_for_regular_query(): void { |
| 153 | + $post_id = self::factory()->post->create(); |
| 154 | + |
| 155 | + $query = new WP_Query( array( 'p' => $post_id ) ); |
| 156 | + |
| 157 | + $this->assertFalse( $query->is_sitemap(), 'A regular post query should not be flagged as a sitemap.' ); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * A sitemap query must not also be treated as the home/front page. |
| 162 | + * |
| 163 | + * This is the practical motivation for the conditional tag: distinguishing a |
| 164 | + * sitemap request from the home page (see #51542). |
| 165 | + * |
| 166 | + * @ticket 51543 |
| 167 | + * |
| 168 | + * @covers WP_Query::parse_query |
| 169 | + */ |
| 170 | + public function test_sitemap_query_is_not_home(): void { |
| 171 | + $query = new WP_Query( array( 'sitemap' => 'index' ) ); |
| 172 | + |
| 173 | + $this->assertTrue( $query->is_sitemap(), 'The sitemap query should be flagged as a sitemap.' ); |
| 174 | + $this->assertFalse( $query->is_home(), 'A sitemap query should not be treated as the home page.' ); |
| 175 | + $this->assertFalse( $query->is_front_page(), 'A sitemap query should not be treated as the front page.' ); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * The global is_sitemap() conditional tag reflects the main query. |
| 180 | + * |
| 181 | + * @ticket 51543 |
| 182 | + * |
| 183 | + * @covers ::is_sitemap |
| 184 | + */ |
| 185 | + public function test_global_is_sitemap_reflects_main_query(): void { |
| 186 | + // Prevent WP_Sitemaps from rendering and calling exit during go_to(). |
| 187 | + remove_action( 'template_redirect', array( wp_sitemaps_get_server(), 'render_sitemaps' ) ); |
| 188 | + |
| 189 | + $this->go_to( home_url( '/?sitemap=index' ) ); |
| 190 | + |
| 191 | + $this->assertTrue( is_sitemap(), 'is_sitemap() should be true on a sitemap request.' ); |
| 192 | + |
| 193 | + // is_sitemap should be the only conditional that is true for a sitemap request. |
| 194 | + $this->assertQueryTrue( 'is_sitemap' ); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * The global is_sitemap() conditional tag is false for a non-sitemap request. |
| 199 | + * |
| 200 | + * @ticket 51543 |
| 201 | + * |
| 202 | + * @covers ::is_sitemap |
| 203 | + */ |
| 204 | + public function test_global_is_sitemap_false_on_home(): void { |
| 205 | + $this->go_to( home_url( '/' ) ); |
| 206 | + |
| 207 | + $this->assertFalse( is_sitemap(), 'is_sitemap() should be false on the home page.' ); |
| 208 | + $this->assertTrue( is_home(), 'is_home() should be true on the home page.' ); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * The global is_sitemap() returns false and triggers a notice when the query |
| 213 | + * has not yet run. |
| 214 | + * |
| 215 | + * @ticket 51543 |
| 216 | + * |
| 217 | + * @covers ::is_sitemap |
| 218 | + * |
| 219 | + * @expectedIncorrectUsage is_sitemap |
| 220 | + */ |
| 221 | + public function test_global_is_sitemap_before_query_is_run(): void { |
| 222 | + $wp_query_temp = $GLOBALS['wp_query']; |
| 223 | + unset( $GLOBALS['wp_query'] ); |
| 224 | + |
| 225 | + $result = is_sitemap(); |
| 226 | + |
| 227 | + $GLOBALS['wp_query'] = $wp_query_temp; |
| 228 | + |
| 229 | + $this->assertFalse( $result, 'is_sitemap() should return false before the query is run.' ); |
| 230 | + } |
| 231 | +} |
0 commit comments