| | 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 | /** |
| | 443 | * As we change SAVEQUERIES constant here (super global), it must be run in a separate process |
| | 444 | * |
| | 445 | * @ticket 39603 |
| | 446 | * @dataProvider post_types_provider |
| | 447 | * @runInSeparateProcess |
| | 448 | */ |
| | 449 | public function test_constant_amount_of_sql_queries_should_be_used_for_prefixed_slug_generation($post_type) { |
| | 450 | global $wpdb; |
| | 451 | define('SAVEQUERIES', true); |
| | 452 | |
| | 453 | $inserts_num = 10; |
| | 454 | |
| | 455 | $postarr = array( |
| | 456 | 'post_type' => $post_type, |
| | 457 | 'post_status' => 'publish', |
| | 458 | 'post_title' => 'Generated Post', |
| | 459 | ); |
| | 460 | |
| | 461 | $queries_first_insert = null; |
| | 462 | $queries_total = 0; |
| | 463 | |
| | 464 | // Let's skip the first post, because it's slug can't be prefixed in a clean test environment |
| | 465 | // (we only need to count queries issued for posts with prefixed slugs) |
| | 466 | $id = wp_insert_post($postarr); |
| | 467 | //echo "Post with id={$id} has been created" . PHP_EOL; |
| | 468 | |
| | 469 | for ($i = 1; $i <= $inserts_num; $i++) { |
| | 470 | $old_number_of_queries = count($wpdb->queries); |
| | 471 | $id = wp_insert_post($postarr); |
| | 472 | |
| | 473 | $queries_issued = count($wpdb->queries) - $old_number_of_queries; |
| | 474 | //echo "Post #{$i} with id={$id} has been created, for which {$queries_issued} queries issued" . PHP_EOL; |
| | 475 | |
| | 476 | if ($queries_first_insert === null) |
| | 477 | $queries_first_insert = $queries_issued; |
| | 478 | |
| | 479 | $queries_total += $queries_issued; |
| | 480 | } |
| | 481 | |
| | 482 | // we are not very strict and allow some post inserts have more queries than the first post creation had |
| | 483 | $queries_avg = floor($queries_total / $inserts_num); |
| | 484 | |
| | 485 | $this->assertLessThanOrEqual($queries_first_insert, $queries_avg, |
| | 486 | "The average number of queries should be not greater than the number of first queries on creating prefixed posts of type '{$post_type}'"); |
| | 487 | |
| | 488 | } |