| 350 | |
| 351 | public function post_types_provider() { |
| 352 | return array( |
| 353 | array('page'), |
| 354 | array('post'), |
| 355 | array('attachment'), |
| 356 | ); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * @dataProvider post_types_provider |
| 361 | * @ticket 39603 |
| 362 | */ |
| 363 | public function test_sequential_slug_prefixes($post_type) { |
| 364 | $posts_num = 3; |
| 365 | $post_factory = self::factory()->post; |
| 366 | |
| 367 | for ($i = 1; $i <= $posts_num; $i++) { |
| 368 | $id = $post_factory->create( array( |
| 369 | 'post_type' => $post_type, |
| 370 | 'post_status' => 'publish', |
| 371 | 'post_title' => "Generated {$post_type}" |
| 372 | )); |
| 373 | |
| 374 | $inserted_object = $post_factory->get_object_by_id($id); |
| 375 | |
| 376 | if ($i == 1) |
| 377 | $this->assertSame("generated-{$post_type}", $inserted_object->post_name); |
| 378 | else |
| 379 | $this->assertSame("generated-{$post_type}-{$i}", $inserted_object->post_name); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * @dataProvider post_types_provider |
| 385 | * @ticket 39603 |
| 386 | */ |
| 387 | public function test_exact_slug_match_should_be_used_for_prefixed_slug_generation($post_type) { |
| 388 | $post_factory = self::factory()->post; |
| 389 | |
| 390 | $postarr_proto = array( |
| 391 | 'post_type' => $post_type, |
| 392 | 'post_status' => 'publish', |
| 393 | 'post_title' => null, |
| 394 | ); |
| 395 | |
| 396 | // create post, post-2, post-3, and post names which have 'post' substrings in their names |
| 397 | foreach (array('Post', 'Post', 'Post', 'Postman', 'Compost', 'Imposter') as $post_title) { |
| 398 | $postarr = $postarr_proto; |
| 399 | $postarr['post_title'] = $post_title; |
| 400 | $post_factory->create($postarr); |
| 401 | } |
| 402 | |
| 403 | // check if previous post names have influence on prefix generation |
| 404 | $postarr = $postarr_proto; |
| 405 | $postarr['post_title'] = 'Post'; |
| 406 | $id = $post_factory->create($postarr); |
| 407 | |
| 408 | $inserted_object = $post_factory->get_object_by_id($id); |
| 409 | $this->assertSame("post-4", $inserted_object->post_name); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * @dataProvider post_types_provider |
| 414 | * @ticket 39603 |
| 415 | */ |
| 416 | public function test_post_creation_time_should_not_influence_prefixed_slug_generation($post_type) { |
| 417 | $post_factory = self::factory()->post; |
| 418 | |
| 419 | $postarr = array( |
| 420 | 'post_type' => $post_type, |
| 421 | 'post_status' => 'publish', |
| 422 | 'post_title' => null, |
| 423 | ); |
| 424 | |
| 425 | $postarr['post_title'] = 'foo'; |
| 426 | $post_1 = $post_factory->create($postarr); |
| 427 | |
| 428 | $postarr['post_title'] = 'bar'; |
| 429 | $post_factory->create($postarr); |
| 430 | |
| 431 | // rename the slug of 'foo' to 'bar' |
| 432 | $post_factory->update_object($post_1, array('post_name' => 'bar')); |
| 433 | $renamed_post_1 = $post_factory->get_object_by_id($post_1); |
| 434 | $this->assertSame('bar-2', $renamed_post_1->post_name); |
| 435 | |
| 436 | $postarr['post_title'] = 'bar'; |
| 437 | $post_3 = $post_factory->create($postarr); |
| 438 | $saved_post_3 = $post_factory->get_object_by_id($post_3); |
| 439 | $this->assertSame('bar-3', $saved_post_3->post_name); |
| 440 | } |
| 441 | /** |
| 442 | * @ticket 39603 |
| 443 | * dataProvider post_types_provider |
| 444 | */ |
| 445 | public function test_slug_prefixes_should_fill_gaps() { |
| 446 | $post_type = 'post'; |
| 447 | $post_factory = self::factory()->post; |
| 448 | $postarr_proto = array ( |
| 449 | 'post_type' => $post_type, |
| 450 | 'post_status' => 'publish', |
| 451 | 'post_title' => 'Generated Post', |
| 452 | ); |
| 453 | |
| 454 | // create generated-post and generated-post-2 |
| 455 | $postarr = $postarr_proto; |
| 456 | $post_factory->create($postarr); |
| 457 | $post_factory->create($postarr); |
| 458 | |
| 459 | // create a post with explicitly named slug 'generated-post-4' |
| 460 | $postarr2 = $postarr_proto; |
| 461 | $postarr2['post_name'] = 'generated-post-4'; |
| 462 | $post_factory->create($postarr2); |
| 463 | |
| 464 | // next automatically found unique suffix must be between 2 and 4 |
| 465 | $id = $post_factory->create($postarr); |
| 466 | $last_saved_post = $post_factory->get_object_by_id($id); |
| 467 | $this->assertSame('generated-post-3', $last_saved_post->post_name); |
| 468 | } |
| 469 | |
| 470 | public function slugs_provider() { |
| 471 | return array( |
| 472 | array( |
| 473 | 'your-subject', |
| 474 | array('your-subject'), |
| 475 | 'your-subject-2', |
| 476 | ), |
| 477 | array( |
| 478 | 'your-subject', |
| 479 | array('your-subject', 'your-subject-2'), |
| 480 | 'your-subject-3', |
| 481 | ), |
| 482 | array( |
| 483 | 'your-subject', |
| 484 | array('your-subject', 'your-subject-3'), |
| 485 | 'your-subject-2', |
| 486 | ), |
| 487 | array( |
| 488 | 'your-subject', |
| 489 | array('your-subject', 'your-subject-3', 'your-subject-5', 'your-subject-7', 'your-subject-9'), |
| 490 | 'your-subject-2', |
| 491 | ), |
| 492 | ); |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * @ticket 39603 |
| 497 | * @dataProvider slugs_provider |
| 498 | */ |
| 499 | public function test_get_first_available_slug($base_slug, $slugs, $anticipated_slug) { |
| 500 | $this->assertSame($anticipated_slug, _get_first_available_slug($base_slug, $slugs)); |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * As we change SAVEQUERIES constant here (super global), it must be run in a separate process |
| 505 | * |
| 506 | * @ticket 39603 |
| 507 | * @dataProvider post_types_provider |
| 508 | * @runInSeparateProcess |
| 509 | */ |
| 510 | public function test_constant_amount_of_sql_queries_should_be_used_for_prefixed_slug_generation($post_type) { |
| 511 | global $wpdb; |
| 512 | define('SAVEQUERIES', true); |
| 513 | |
| 514 | $inserts_num = 10; |
| 515 | |
| 516 | $postarr = array( |
| 517 | 'post_type' => $post_type, |
| 518 | 'post_status' => 'publish', |
| 519 | 'post_title' => 'Generated Post', |
| 520 | ); |
| 521 | |
| 522 | $queries_first_insert = null; |
| 523 | $queries_total = 0; |
| 524 | |
| 525 | // Let's skip the first post, because it's slug can't be prefixed in a clean test environment |
| 526 | // (we only need to count queries issued for posts with prefixed slugs) |
| 527 | $id = wp_insert_post($postarr); |
| 528 | //echo "Post with id={$id} has been created" . PHP_EOL; |
| 529 | |
| 530 | for ($i = 1; $i <= $inserts_num; $i++) { |
| 531 | $old_number_of_queries = count($wpdb->queries); |
| 532 | $id = wp_insert_post($postarr); |
| 533 | |
| 534 | $queries_issued = count($wpdb->queries) - $old_number_of_queries; |
| 535 | //echo "Post #{$i} with id={$id} has been created, for which {$queries_issued} queries issued" . PHP_EOL; |
| 536 | |
| 537 | if ($queries_first_insert === null) |
| 538 | $queries_first_insert = $queries_issued; |
| 539 | |
| 540 | $queries_total += $queries_issued; |
| 541 | } |
| 542 | |
| 543 | // we are not very strict and allow some post inserts have more queries than the first post creation had |
| 544 | $queries_avg = floor($queries_total / $inserts_num); |
| 545 | |
| 546 | $this->assertLessThanOrEqual($queries_first_insert, $queries_avg, |
| 547 | "The average number of queries should be not greater than the number of first queries on creating prefixed posts of type '{$post_type}'"); |
| 548 | |
| 549 | } |