| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group rewrite |
| 5 | * @ticket 5305 |
| 6 | */ |
| 7 | class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { |
| 8 | private $old_current_user; |
| 9 | |
| 10 | public function setUp() { |
| 11 | parent::setUp(); |
| 12 | $this->author_id = $this->factory->user->create( array( 'role' => 'editor' ) ); |
| 13 | } |
| 14 | |
| 15 | public function test_go_to_year_segment_collision_without_title() { |
| 16 | global $wp_rewrite, $wpdb; |
| 17 | $wp_rewrite->init(); |
| 18 | $wp_rewrite->set_permalink_structure( '/%postname%/' ); |
| 19 | $wp_rewrite->flush_rules(); |
| 20 | |
| 21 | $id = $this->factory->post->create( array( |
| 22 | 'post_author' => $this->author_id, |
| 23 | 'post_status' => 'publish', |
| 24 | 'post_content' => rand_str(), |
| 25 | 'post_title' => '', |
| 26 | 'post_name' => '2015', |
| 27 | 'post_date' => '2015-02-01 01:00:00' |
| 28 | ) ); |
| 29 | |
| 30 | // Force an ID that resembles a year format |
| 31 | $wpdb->update( |
| 32 | $wpdb->posts, |
| 33 | array( |
| 34 | 'ID' => '2015', |
| 35 | 'guid' => 'http://example.org/?p=2015' |
| 36 | ), |
| 37 | array( 'ID' => $id ) |
| 38 | ); |
| 39 | |
| 40 | $this->go_to( get_permalink( '2015' ) ); |
| 41 | |
| 42 | $this->assertQueryTrue( 'is_single', 'is_singular' ); |
| 43 | |
| 44 | $wp_rewrite->set_permalink_structure(''); |
| 45 | } |
| 46 | |
| 47 | public function test_url_to_postid_year_segment_collision_without_title() { |
| 48 | global $wp_rewrite, $wpdb; |
| 49 | $wp_rewrite->init(); |
| 50 | $wp_rewrite->set_permalink_structure( '/%postname%/' ); |
| 51 | $wp_rewrite->flush_rules(); |
| 52 | |
| 53 | $id = $this->factory->post->create( array( |
| 54 | 'post_author' => $this->author_id, |
| 55 | 'post_status' => 'publish', |
| 56 | 'post_content' => rand_str(), |
| 57 | 'post_title' => '', |
| 58 | 'post_name' => '2015', |
| 59 | 'post_date' => '2015-02-01 01:00:00' |
| 60 | ) ); |
| 61 | |
| 62 | // Force an ID that resembles a year format |
| 63 | $wpdb->update( |
| 64 | $wpdb->posts, |
| 65 | array( |
| 66 | 'ID' => '2015', |
| 67 | 'guid' => 'http://example.org/?p=2015' |
| 68 | ), |
| 69 | array( 'ID' => $id ) |
| 70 | ); |
| 71 | |
| 72 | $this->assertEquals( '2015', url_to_postid( get_permalink( '2015' ) ) ); |
| 73 | |
| 74 | $wp_rewrite->set_permalink_structure(''); |
| 75 | } |
| 76 | |
| 77 | public function test_go_to_year_segment_collision_with_title() { |
| 78 | global $wp_rewrite; |
| 79 | $wp_rewrite->init(); |
| 80 | $wp_rewrite->set_permalink_structure( '/%postname%/' ); |
| 81 | $wp_rewrite->flush_rules(); |
| 82 | |
| 83 | $id = $this->factory->post->create( array( |
| 84 | 'post_author' => $this->author_id, |
| 85 | 'post_status' => 'publish', |
| 86 | 'post_content' => rand_str(), |
| 87 | 'post_title' => '2015', |
| 88 | 'post_date' => '2015-02-01 01:00:00', |
| 89 | ) ); |
| 90 | |
| 91 | $this->go_to( get_permalink( $id ) ); |
| 92 | |
| 93 | $this->assertQueryTrue( 'is_single', 'is_singular' ); |
| 94 | |
| 95 | $wp_rewrite->set_permalink_structure(''); |
| 96 | } |
| 97 | |
| 98 | public function test_url_to_postid_year_segment_collision_with_title() { |
| 99 | global $wp_rewrite; |
| 100 | $wp_rewrite->init(); |
| 101 | $wp_rewrite->set_permalink_structure( '/%postname%/' ); |
| 102 | $wp_rewrite->flush_rules(); |
| 103 | |
| 104 | $id = $this->factory->post->create( array( |
| 105 | 'post_author' => $this->author_id, |
| 106 | 'post_status' => 'publish', |
| 107 | 'post_content' => rand_str(), |
| 108 | 'post_title' => '2015', |
| 109 | 'post_date' => '2015-02-01 01:00:00', |
| 110 | ) ); |
| 111 | |
| 112 | $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); |
| 113 | |
| 114 | $wp_rewrite->set_permalink_structure(''); |
| 115 | } |
| 116 | |
| 117 | public function test_go_to_month_segment_collision_without_title() { |
| 118 | global $wp_rewrite; |
| 119 | $wp_rewrite->init(); |
| 120 | $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); |
| 121 | $wp_rewrite->flush_rules(); |
| 122 | |
| 123 | $id = $this->factory->post->create( array( |
| 124 | 'post_author' => $this->author_id, |
| 125 | 'post_status' => 'publish', |
| 126 | 'post_content' => rand_str(), |
| 127 | 'post_title' => '', |
| 128 | 'post_name' => '02', |
| 129 | 'post_date' => '2015-02-01 01:00:00', |
| 130 | ) ); |
| 131 | |
| 132 | $this->go_to( get_permalink( $id ) ); |
| 133 | |
| 134 | $this->assertQueryTrue( 'is_single', 'is_singular' ); |
| 135 | |
| 136 | $wp_rewrite->set_permalink_structure(''); |
| 137 | } |
| 138 | |
| 139 | public function test_url_to_postid_month_segment_collision_without_title() { |
| 140 | global $wp_rewrite; |
| 141 | $wp_rewrite->init(); |
| 142 | $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); |
| 143 | $wp_rewrite->flush_rules(); |
| 144 | |
| 145 | $id = $this->factory->post->create( array( |
| 146 | 'post_author' => $this->author_id, |
| 147 | 'post_status' => 'publish', |
| 148 | 'post_content' => rand_str(), |
| 149 | 'post_title' => '', |
| 150 | 'post_name' => '02', |
| 151 | 'post_date' => '2015-02-01 01:00:00', |
| 152 | ) ); |
| 153 | |
| 154 | $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); |
| 155 | |
| 156 | $wp_rewrite->set_permalink_structure(''); |
| 157 | } |
| 158 | |
| 159 | public function test_go_to_month_segment_collision_without_title_no_leading_zero() { |
| 160 | global $wp_rewrite; |
| 161 | $wp_rewrite->init(); |
| 162 | $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); |
| 163 | $wp_rewrite->flush_rules(); |
| 164 | |
| 165 | $id = $this->factory->post->create( array( |
| 166 | 'post_author' => $this->author_id, |
| 167 | 'post_status' => 'publish', |
| 168 | 'post_content' => rand_str(), |
| 169 | 'post_title' => '', |
| 170 | 'post_name' => '2', |
| 171 | 'post_date' => '2015-02-01 01:00:00', |
| 172 | ) ); |
| 173 | |
| 174 | $this->go_to( get_permalink( $id ) ); |
| 175 | |
| 176 | $this->assertQueryTrue( 'is_single', 'is_singular' ); |
| 177 | |
| 178 | $wp_rewrite->set_permalink_structure(''); |
| 179 | } |
| 180 | |
| 181 | public function test_url_to_postid_month_segment_collision_without_title_no_leading_zero() { |
| 182 | global $wp_rewrite; |
| 183 | $wp_rewrite->init(); |
| 184 | $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); |
| 185 | $wp_rewrite->flush_rules(); |
| 186 | |
| 187 | $id = $this->factory->post->create( array( |
| 188 | 'post_author' => $this->author_id, |
| 189 | 'post_status' => 'publish', |
| 190 | 'post_content' => rand_str(), |
| 191 | 'post_title' => '', |
| 192 | 'post_name' => '2', |
| 193 | 'post_date' => '2015-02-01 01:00:00', |
| 194 | ) ); |
| 195 | |
| 196 | $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); |
| 197 | |
| 198 | $wp_rewrite->set_permalink_structure(''); |
| 199 | } |
| 200 | |
| 201 | public function test_go_to_month_segment_collision_with_title() { |
| 202 | global $wp_rewrite; |
| 203 | $wp_rewrite->init(); |
| 204 | $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); |
| 205 | $wp_rewrite->flush_rules(); |
| 206 | |
| 207 | $id = $this->factory->post->create( array( |
| 208 | 'post_author' => $this->author_id, |
| 209 | 'post_status' => 'publish', |
| 210 | 'post_content' => rand_str(), |
| 211 | 'post_title' => '02', |
| 212 | 'post_date' => '2015-02-01 01:00:00', |
| 213 | ) ); |
| 214 | |
| 215 | $this->go_to( get_permalink( $id ) ); |
| 216 | |
| 217 | $this->assertQueryTrue( 'is_single', 'is_singular' ); |
| 218 | |
| 219 | $wp_rewrite->set_permalink_structure(''); |
| 220 | } |
| 221 | |
| 222 | public function test_url_to_postid_month_segment_collision_with_title() { |
| 223 | global $wp_rewrite; |
| 224 | $wp_rewrite->init(); |
| 225 | $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); |
| 226 | $wp_rewrite->flush_rules(); |
| 227 | |
| 228 | $id = $this->factory->post->create( array( |
| 229 | 'post_author' => $this->author_id, |
| 230 | 'post_status' => 'publish', |
| 231 | 'post_content' => rand_str(), |
| 232 | 'post_title' => '02', |
| 233 | 'post_date' => '2015-02-01 01:00:00', |
| 234 | ) ); |
| 235 | |
| 236 | $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); |
| 237 | |
| 238 | $wp_rewrite->set_permalink_structure(''); |
| 239 | } |
| 240 | |
| 241 | public function test_go_to_month_segment_collision_with_title_no_leading_zero() { |
| 242 | global $wp_rewrite; |
| 243 | $wp_rewrite->init(); |
| 244 | $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); |
| 245 | $wp_rewrite->flush_rules(); |
| 246 | |
| 247 | $id = $this->factory->post->create( array( |
| 248 | 'post_author' => $this->author_id, |
| 249 | 'post_status' => 'publish', |
| 250 | 'post_content' => rand_str(), |
| 251 | 'post_title' => '2', |
| 252 | 'post_date' => '2015-02-01 01:00:00', |
| 253 | ) ); |
| 254 | |
| 255 | $this->go_to( get_permalink( $id ) ); |
| 256 | |
| 257 | $this->assertQueryTrue( 'is_single', 'is_singular' ); |
| 258 | |
| 259 | $wp_rewrite->set_permalink_structure(''); |
| 260 | } |
| 261 | |
| 262 | public function test_url_to_postid_month_segment_collision_with_title_no_leading_zero() { |
| 263 | global $wp_rewrite; |
| 264 | $wp_rewrite->init(); |
| 265 | $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); |
| 266 | $wp_rewrite->flush_rules(); |
| 267 | |
| 268 | $id = $this->factory->post->create( array( |
| 269 | 'post_author' => $this->author_id, |
| 270 | 'post_status' => 'publish', |
| 271 | 'post_content' => rand_str(), |
| 272 | 'post_title' => '2', |
| 273 | 'post_date' => '2015-02-01 01:00:00', |
| 274 | ) ); |
| 275 | |
| 276 | $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); |
| 277 | |
| 278 | $wp_rewrite->set_permalink_structure(''); |
| 279 | } |
| 280 | |
| 281 | public function test_go_to_day_segment_collision_without_title() { |
| 282 | global $wp_rewrite; |
| 283 | $wp_rewrite->init(); |
| 284 | $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); |
| 285 | $wp_rewrite->flush_rules(); |
| 286 | |
| 287 | $id = $this->factory->post->create( array( |
| 288 | 'post_author' => $this->author_id, |
| 289 | 'post_status' => 'publish', |
| 290 | 'post_content' => rand_str(), |
| 291 | 'post_title' => '', |
| 292 | 'post_name' => '01', |
| 293 | 'post_date' => '2015-02-01 01:00:00', |
| 294 | ) ); |
| 295 | |
| 296 | $this->go_to( get_permalink( $id ) ); |
| 297 | |
| 298 | $this->assertQueryTrue( 'is_single', 'is_singular' ); |
| 299 | |
| 300 | $wp_rewrite->set_permalink_structure(''); |
| 301 | } |
| 302 | |
| 303 | public function test_url_to_postid_day_segment_collision_without_title() { |
| 304 | global $wp_rewrite; |
| 305 | $wp_rewrite->init(); |
| 306 | $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); |
| 307 | $wp_rewrite->flush_rules(); |
| 308 | |
| 309 | $id = $this->factory->post->create( array( |
| 310 | 'post_author' => $this->author_id, |
| 311 | 'post_status' => 'publish', |
| 312 | 'post_content' => rand_str(), |
| 313 | 'post_title' => '', |
| 314 | 'post_name' => '01', |
| 315 | 'post_date' => '2015-02-01 01:00:00', |
| 316 | ) ); |
| 317 | |
| 318 | $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); |
| 319 | |
| 320 | $wp_rewrite->set_permalink_structure(''); |
| 321 | } |
| 322 | |
| 323 | public function test_go_to_day_segment_collision_with_title() { |
| 324 | global $wp_rewrite; |
| 325 | $wp_rewrite->init(); |
| 326 | $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); |
| 327 | $wp_rewrite->flush_rules(); |
| 328 | |
| 329 | $id = $this->factory->post->create( array( |
| 330 | 'post_author' => $this->author_id, |
| 331 | 'post_status' => 'publish', |
| 332 | 'post_content' => rand_str(), |
| 333 | 'post_title' => '01', |
| 334 | 'post_date' => '2015-02-01 01:00:00', |
| 335 | ) ); |
| 336 | |
| 337 | $this->go_to( get_permalink( $id ) ); |
| 338 | |
| 339 | $this->assertQueryTrue( 'is_single', 'is_singular' ); |
| 340 | |
| 341 | $wp_rewrite->set_permalink_structure(''); |
| 342 | } |
| 343 | |
| 344 | public function test_url_to_postid_day_segment_collision_with_title() { |
| 345 | global $wp_rewrite; |
| 346 | $wp_rewrite->init(); |
| 347 | $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); |
| 348 | $wp_rewrite->flush_rules(); |
| 349 | |
| 350 | $id = $this->factory->post->create( array( |
| 351 | 'post_author' => $this->author_id, |
| 352 | 'post_status' => 'publish', |
| 353 | 'post_content' => rand_str(), |
| 354 | 'post_title' => '01', |
| 355 | 'post_date' => '2015-02-01 01:00:00', |
| 356 | ) ); |
| 357 | |
| 358 | $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); |
| 359 | |
| 360 | $wp_rewrite->set_permalink_structure(''); |
| 361 | } |
| 362 | |
| 363 | public function test_numeric_slug_permalink_conflicts_should_only_be_resolved_for_the_main_query() { |
| 364 | global $wp_rewrite; |
| 365 | $wp_rewrite->init(); |
| 366 | $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); |
| 367 | $wp_rewrite->flush_rules(); |
| 368 | |
| 369 | $id = $this->factory->post->create( array( |
| 370 | 'post_author' => $this->author_id, |
| 371 | 'post_status' => 'publish', |
| 372 | 'post_content' => rand_str(), |
| 373 | 'post_title' => '01', |
| 374 | 'post_date' => '2015-02-01 01:00:00', |
| 375 | ) ); |
| 376 | |
| 377 | $q = new WP_Query( array( |
| 378 | 'year' => '2015', |
| 379 | 'monthnum' => '02', |
| 380 | 'day' => '01', |
| 381 | ) ); |
| 382 | |
| 383 | $this->assertTrue( $q->is_day ); |
| 384 | $this->assertFalse( $q->is_single ); |
| 385 | |
| 386 | $wp_rewrite->set_permalink_structure(''); |
| 387 | } |
| 388 | |
| 389 | public function test_date_slug_collision_should_distinguish_valid_pagination_from_date() { |
| 390 | global $wp_rewrite; |
| 391 | $wp_rewrite->init(); |
| 392 | $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); |
| 393 | $wp_rewrite->flush_rules(); |
| 394 | |
| 395 | $id = $this->factory->post->create( array( |
| 396 | 'post_author' => $this->author_id, |
| 397 | 'post_status' => 'publish', |
| 398 | 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3', |
| 399 | 'post_title' => '02', |
| 400 | 'post_date' => '2015-02-01 01:00:00', |
| 401 | ) ); |
| 402 | |
| 403 | $this->go_to( get_permalink( $id ) . '1' ); |
| 404 | |
| 405 | $this->assertFalse( is_day() ); |
| 406 | } |
| 407 | |
| 408 | public function test_date_slug_collision_should_distinguish_too_high_pagination_from_date() { |
| 409 | global $wp_rewrite; |
| 410 | $wp_rewrite->init(); |
| 411 | $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); |
| 412 | $wp_rewrite->flush_rules(); |
| 413 | |
| 414 | $id = $this->factory->post->create( array( |
| 415 | 'post_author' => $this->author_id, |
| 416 | 'post_status' => 'publish', |
| 417 | 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3', |
| 418 | 'post_title' => '02', |
| 419 | 'post_date' => '2015-02-05 01:00:00', |
| 420 | ) ); |
| 421 | |
| 422 | $this->go_to( get_permalink( $id ) . '5' ); |
| 423 | |
| 424 | $this->assertTrue( is_day() ); |
| 425 | } |
| 426 | |
| 427 | public function test_date_slug_collision_should_not_require_pagination_query_var() { |
| 428 | global $wp_rewrite; |
| 429 | $wp_rewrite->init(); |
| 430 | $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); |
| 431 | $wp_rewrite->flush_rules(); |
| 432 | |
| 433 | $id = $this->factory->post->create( array( |
| 434 | 'post_author' => $this->author_id, |
| 435 | 'post_status' => 'publish', |
| 436 | 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3', |
| 437 | 'post_title' => '02', |
| 438 | 'post_date' => '2015-02-05 01:00:00', |
| 439 | ) ); |
| 440 | |
| 441 | $this->go_to( get_permalink( $id ) ); |
| 442 | |
| 443 | $this->assertQueryTrue( 'is_single', 'is_singular' ); |
| 444 | $this->assertFalse( is_date() ); |
| 445 | } |
| 446 | |
| 447 | public function test_date_slug_collision_should_be_ignored_when_pagination_var_is_present_but_post_does_not_have_multiple_pages() { |
| 448 | global $wp_rewrite; |
| 449 | $wp_rewrite->init(); |
| 450 | $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); |
| 451 | $wp_rewrite->flush_rules(); |
| 452 | |
| 453 | $id = $this->factory->post->create( array( |
| 454 | 'post_author' => $this->author_id, |
| 455 | 'post_status' => 'publish', |
| 456 | 'post_content' => 'This post does not have pagination.', |
| 457 | 'post_title' => '02', |
| 458 | 'post_date' => '2015-02-05 01:00:00', |
| 459 | ) ); |
| 460 | |
| 461 | $this->go_to( get_permalink( $id ) . '5' ); |
| 462 | |
| 463 | $this->assertTrue( is_day() ); |
| 464 | } |
| 465 | } |