Ticket #39079: 39079.diff
File 39079.diff, 6.6 KB (added by , 8 years ago) |
---|
-
tests/phpunit/tests/rest-api/rest-posts-controller.php
diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index 886ae3c..78f4fdf 100644
a b class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 20 20 protected static $supported_formats; 21 21 22 22 protected $forbidden_cat; 23 protected $posts_orderby; 23 24 24 25 public static function wpSetUpBeforeClass( $factory ) { 25 26 self::$post_id = $factory->post->create(); … … class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 65 66 public function setUp() { 66 67 parent::setUp(); 67 68 register_post_type( 'youseeme', array( 'supports' => array(), 'show_in_rest' => true ) ); 69 add_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 ); 70 add_filter( 'posts_orderby', array( $this, 'save_posts_orderby' ), 10, 2 ); 71 } 72 73 public function wpSetUpBeforeRequest( $result, $server, $request ) { 74 $this->posts_orderby = array(); 75 return $result; 76 } 77 78 public function save_posts_orderby( $orderby, $query ) { 79 array_push( $this->posts_orderby, $orderby ); 80 return $orderby; 81 } 82 83 public function assertPostsOrderedBy( $pattern ) { 84 global $wpdb; 85 $orderby = str_replace( '{posts}', $wpdb->posts, $pattern ); 86 $this->assertEquals( array( $orderby ), $this->posts_orderby ); 68 87 } 69 88 70 89 public function test_register_routes() { … … class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 214 233 $data = $response->get_data(); 215 234 $this->assertEquals( 2, count( $data ) ); 216 235 $this->assertEquals( $id3, $data[0]['id'] ); 236 $this->assertPostsOrderedBy( '{posts}.post_date DESC' ); 217 237 // Orderby=>include 218 238 $request->set_param( 'orderby', 'include' ); 219 239 $response = $this->server->dispatch( $request ); 220 240 $data = $response->get_data(); 221 241 $this->assertEquals( 2, count( $data ) ); 222 242 $this->assertEquals( $id1, $data[0]['id'] ); 243 $this->assertPostsOrderedBy( 'FIELD( {posts}.ID, 9,11 )' ); 223 244 // Invalid include should error 224 245 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 225 246 $request->set_param( 'include', 'invalid' ); … … class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 428 449 $response = $this->server->dispatch( $request ); 429 450 $data = $response->get_data(); 430 451 $this->assertEquals( 'Apple Sauce', $data[0]['title']['rendered'] ); 452 $this->assertPostsOrderedBy( '{posts}.post_title DESC' ); 431 453 // order=>asc 432 454 $request->set_param( 'order', 'asc' ); 433 455 $response = $this->server->dispatch( $request ); 434 456 $data = $response->get_data(); 435 457 $this->assertEquals( 'Apple Cobbler', $data[0]['title']['rendered'] ); 458 $this->assertPostsOrderedBy( '{posts}.post_title ASC' ); 436 459 // order=>asc,id should fail 437 460 $request->set_param( 'order', 'asc,id' ); 438 461 $response = $this->server->dispatch( $request ); … … class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 470 493 $this->assertEquals( $id3, $data[0]['id'] ); 471 494 $this->assertEquals( $id2, $data[1]['id'] ); 472 495 $this->assertEquals( $id1, $data[2]['id'] ); 496 $this->assertPostsOrderedBy( '{posts}.ID DESC' ); 473 497 } 474 498 475 499 public function test_get_items_with_orderby_slug() { … … class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 486 510 // Default ORDER is DESC. 487 511 $this->assertEquals( 'xyz', $data[0]['slug'] ); 488 512 $this->assertEquals( 'abc', $data[1]['slug'] ); 513 $this->assertPostsOrderedBy( '{posts}.post_name DESC' ); 489 514 } 490 515 491 516 public function test_get_items_with_orderby_relevance() { 492 $this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ) ); 493 $this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ) ); 517 $id1 = $this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ) ); 518 $id2 = $this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ) ); 519 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 520 $request->set_param( 'orderby', 'relevance' ); 521 $request->set_param( 'search', 'relevant' ); 522 $response = $this->server->dispatch( $request ); 523 $this->assertEquals( 200, $response->get_status() ); 524 $data = $response->get_data(); 525 $this->assertCount( 2, $data ); 526 $this->assertEquals( $id1, $data[0]['id'] ); 527 $this->assertEquals( $id2, $data[1]['id'] ); 528 $this->assertPostsOrderedBy( '{posts}.post_title LIKE \'%relevant%\' DESC, {posts}.post_date DESC' ); 529 } 530 531 public function test_get_items_with_orderby_relevance_two_terms() { 532 $id1 = $this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ) ); 533 $id2 = $this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ) ); 534 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 535 $request->set_param( 'orderby', 'relevance' ); 536 $request->set_param( 'search', 'relevant content' ); 537 $response = $this->server->dispatch( $request ); 538 $this->assertEquals( 200, $response->get_status() ); 539 $data = $response->get_data(); 540 $this->assertCount( 2, $data ); 541 $this->assertEquals( $id1, $data[0]['id'] ); 542 $this->assertEquals( $id2, $data[1]['id'] ); 543 $this->assertPostsOrderedBy( '(CASE WHEN {posts}.post_title LIKE \'%relevant content%\' THEN 1 WHEN {posts}.post_title LIKE \'%relevant%\' AND {posts}.post_title LIKE \'%content%\' THEN 2 WHEN {posts}.post_title LIKE \'%relevant%\' OR {posts}.post_title LIKE \'%content%\' THEN 3 WHEN {posts}.post_excerpt LIKE \'%relevant content%\' THEN 4 WHEN {posts}.post_content LIKE \'%relevant content%\' THEN 5 ELSE 6 END), {posts}.post_date DESC' ); 544 } 494 545 546 public function test_get_items_with_orderby_relevance_missing_search() { 495 547 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 496 548 $request->set_param( 'orderby', 'relevance' ); 497 549 $response = $this->server->dispatch( $request ); … … class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 2688 2740 if ( isset( $this->attachment_id ) ) { 2689 2741 $this->remove_added_uploads(); 2690 2742 } 2743 remove_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 ); 2744 remove_filter( 'posts_orderby', array( $this, 'save_posts_orderby' ), 10, 2 ); 2691 2745 parent::tearDown(); 2692 2746 } 2693 2747