- Timestamp:
- 02/01/2017 08:30:17 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php
r39991 r40037 21 21 22 22 protected $forbidden_cat; 23 protected $posts_orderby; 23 24 24 25 public static function wpSetUpBeforeClass( $factory ) { … … 67 68 parent::setUp(); 68 69 register_post_type( 'youseeme', array( 'supports' => array(), 'show_in_rest' => true ) ); 70 add_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 ); 71 add_filter( 'posts_orderby', array( $this, 'save_posts_orderby' ), 10, 2 ); 72 } 73 74 public function wpSetUpBeforeRequest( $result, $server, $request ) { 75 $this->posts_orderby = array(); 76 return $result; 77 } 78 79 public function save_posts_orderby( $orderby, $query ) { 80 array_push( $this->posts_orderby, $orderby ); 81 return $orderby; 82 } 83 84 public function assertPostsOrderedBy( $pattern ) { 85 global $wpdb; 86 $orderby = str_replace( '{posts}', $wpdb->posts, $pattern ); 87 $this->assertEquals( array( $orderby ), $this->posts_orderby ); 69 88 } 70 89 … … 225 244 $this->assertEquals( 2, count( $data ) ); 226 245 $this->assertEquals( $id3, $data[0]['id'] ); 246 $this->assertPostsOrderedBy( '{posts}.post_date DESC' ); 227 247 // Orderby=>include 228 248 $request->set_param( 'orderby', 'include' ); … … 231 251 $this->assertEquals( 2, count( $data ) ); 232 252 $this->assertEquals( $id1, $data[0]['id'] ); 253 $this->assertPostsOrderedBy( "FIELD( {posts}.ID, $id1,$id3 )" ); 233 254 // Invalid include should error 234 255 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); … … 439 460 $data = $response->get_data(); 440 461 $this->assertEquals( 'Apple Sauce', $data[0]['title']['rendered'] ); 462 $this->assertPostsOrderedBy( '{posts}.post_title DESC' ); 441 463 // order=>asc 442 464 $request->set_param( 'order', 'asc' ); … … 444 466 $data = $response->get_data(); 445 467 $this->assertEquals( 'Apple Cobbler', $data[0]['title']['rendered'] ); 468 $this->assertPostsOrderedBy( '{posts}.post_title ASC' ); 446 469 // order=>asc,id should fail 447 470 $request->set_param( 'order', 'asc,id' ); … … 481 504 $this->assertEquals( $id2, $data[1]['id'] ); 482 505 $this->assertEquals( $id1, $data[2]['id'] ); 506 $this->assertPostsOrderedBy( '{posts}.ID DESC' ); 483 507 } 484 508 … … 497 521 $this->assertEquals( 'xyz', $data[0]['slug'] ); 498 522 $this->assertEquals( 'abc', $data[1]['slug'] ); 523 $this->assertPostsOrderedBy( '{posts}.post_name DESC' ); 499 524 } 500 525 501 526 public function test_get_items_with_orderby_relevance() { 502 $this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ) ); 503 $this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ) ); 504 527 $id1 = $this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ) ); 528 $id2 = $this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ) ); 529 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 530 $request->set_param( 'orderby', 'relevance' ); 531 $request->set_param( 'search', 'relevant' ); 532 $response = $this->server->dispatch( $request ); 533 $this->assertEquals( 200, $response->get_status() ); 534 $data = $response->get_data(); 535 $this->assertCount( 2, $data ); 536 $this->assertEquals( $id1, $data[0]['id'] ); 537 $this->assertEquals( $id2, $data[1]['id'] ); 538 $this->assertPostsOrderedBy( '{posts}.post_title LIKE \'%relevant%\' DESC, {posts}.post_date DESC' ); 539 } 540 541 public function test_get_items_with_orderby_relevance_two_terms() { 542 $id1 = $this->factory->post->create( array( 'post_title' => 'Title is more relevant', 'post_content' => 'Content is', 'post_status' => 'publish' ) ); 543 $id2 = $this->factory->post->create( array( 'post_title' => 'Title is', 'post_content' => 'Content is less relevant', 'post_status' => 'publish' ) ); 544 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 545 $request->set_param( 'orderby', 'relevance' ); 546 $request->set_param( 'search', 'relevant content' ); 547 $response = $this->server->dispatch( $request ); 548 $this->assertEquals( 200, $response->get_status() ); 549 $data = $response->get_data(); 550 $this->assertCount( 2, $data ); 551 $this->assertEquals( $id1, $data[0]['id'] ); 552 $this->assertEquals( $id2, $data[1]['id'] ); 553 $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' ); 554 } 555 556 public function test_get_items_with_orderby_relevance_missing_search() { 505 557 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 506 558 $request->set_param( 'orderby', 'relevance' ); … … 2720 2772 $this->remove_added_uploads(); 2721 2773 } 2774 remove_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 ); 2775 remove_filter( 'posts_orderby', array( $this, 'save_posts_orderby' ), 10, 2 ); 2722 2776 parent::tearDown(); 2723 2777 }
Note: See TracChangeset
for help on using the changeset viewer.