| | 273 | public function test_canonical_meta_tag() { |
| | 274 | $now = time(); |
| | 275 | $post_id = self::factory()->post->create( array( |
| | 276 | 'post_content' => rand_str() . "\n\n<!--nextpage-->\n\n" . rand_str(), |
| | 277 | ) ); |
| | 278 | |
| | 279 | $this->set_permalink_structure( '' ); |
| | 280 | |
| | 281 | $url1 = add_query_arg( array( |
| | 282 | 'page' => 2, |
| | 283 | 'p' => $post_id, |
| | 284 | ), home_url() ); |
| | 285 | $url2 = add_query_arg( array( |
| | 286 | 'p' => $post_id, |
| | 287 | 'page' => 2, |
| | 288 | ), home_url() ); |
| | 289 | |
| | 290 | // Ensure the two URLs differ. |
| | 291 | // If add_query_arg changes in the future and sorts by key, these |
| | 292 | // tests will need to be rewritten to check different URLs return |
| | 293 | // the same rel_canonical meta tag. |
| | 294 | $this->assertNotEquals( ' ' . $url1, $url2 ); |
| | 295 | |
| | 296 | $this->go_to( $url1 ); |
| | 297 | ob_start(); |
| | 298 | rel_canonical(); |
| | 299 | $canonical1 = ob_get_clean(); |
| | 300 | |
| | 301 | $this->go_to( $url2 ); |
| | 302 | ob_start(); |
| | 303 | rel_canonical(); |
| | 304 | $canonical2 = ob_get_clean(); |
| | 305 | |
| | 306 | $this->assertEquals( $canonical1, $canonical2 ); |
| | 307 | |
| | 308 | $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); |
| | 309 | |
| | 310 | $permalink = get_permalink( $post_id ); |
| | 311 | $permalink_page_two = untrailingslashit( $permalink ) . '/2/'; |
| | 312 | $canonical3 = '<link rel="canonical" href="' . esc_url( $permalink_page_two ) . "\" />\n"; |
| | 313 | |
| | 314 | $this->go_to( $permalink_page_two ); |
| | 315 | ob_start(); |
| | 316 | rel_canonical(); |
| | 317 | $canonical4 = ob_get_clean(); |
| | 318 | |
| | 319 | $this->assertEquals( $canonical3, $canonical4 ); |
| | 320 | } |
| | 321 | |