- Timestamp:
- 02/25/2017 05:02:17 AM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php
r40120 r40122 21 21 22 22 protected $forbidden_cat; 23 protected $posts_ orderby;23 protected $posts_clauses; 24 24 25 25 public static function wpSetUpBeforeClass( $factory ) { … … 69 69 register_post_type( 'youseeme', array( 'supports' => array(), 'show_in_rest' => true ) ); 70 70 add_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 ); 71 add_filter( 'posts_ orderby', array( $this, 'save_posts_orderby' ), 10, 2 );71 add_filter( 'posts_clauses', array( $this, 'save_posts_clauses' ), 10, 2 ); 72 72 } 73 73 74 74 public function wpSetUpBeforeRequest( $result, $server, $request ) { 75 $this->posts_ orderby= array();75 $this->posts_clauses = array(); 76 76 return $result; 77 77 } 78 78 79 public function save_posts_ orderby( $orderby, $query ) {80 array_push( $this->posts_ orderby, $orderby );79 public function save_posts_clauses( $orderby, $query ) { 80 array_push( $this->posts_clauses, $orderby ); 81 81 return $orderby; 82 82 } 83 83 84 public function assertPostsClause( $clause, $pattern ) { 85 global $wpdb; 86 $expected_clause = str_replace( '{posts}', $wpdb->posts, $pattern ); 87 $this->assertCount( 1, $this->posts_clauses ); 88 $this->assertEquals( $expected_clause, $this->posts_clauses[0][ $clause ] ); 89 } 90 84 91 public function assertPostsOrderedBy( $pattern ) { 85 global $wpdb; 86 $orderby = str_replace( '{posts}', $wpdb->posts, $pattern ); 87 $this->assertEquals( array( $orderby ), $this->posts_orderby ); 92 $this->assertPostsClause( 'orderby', $pattern ); 93 } 94 95 public function assertPostsWhere( $pattern ) { 96 $this->assertPostsClause( 'where', $pattern ); 88 97 } 89 98 … … 691 700 } 692 701 693 public function test_get_items_sticky _query() {702 public function test_get_items_sticky() { 694 703 $id1 = self::$post_id; 695 704 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); … … 712 721 } 713 722 714 public function test_get_items_sticky_with_ post__in_query() {723 public function test_get_items_sticky_with_include() { 715 724 $id1 = self::$post_id; 716 725 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); … … 726 735 $this->assertCount( 0, $response->get_data() ); 727 736 737 // FIXME Since this request returns zero posts, the query is executed twice. 738 $this->assertCount( 2, $this->posts_clauses ); 739 $this->posts_clauses = array_slice( $this->posts_clauses, 0, 1 ); 740 741 $this->assertPostsWhere( " AND {posts}.ID IN (0) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" ); 742 728 743 update_option( 'sticky_posts', array( $id1, $id2 ) ); 729 744 … … 739 754 $post = $posts[0]; 740 755 $this->assertEquals( $id1, $post['id'] ); 741 } 742 743 public function test_get_items_not_sticky_query() { 756 757 $this->assertPostsWhere( " AND {posts}.ID IN ($id1) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" ); 758 } 759 760 public function test_get_items_sticky_no_sticky_posts() { 761 $id1 = self::$post_id; 762 763 update_option( 'sticky_posts', array() ); 764 765 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 766 $request->set_param( 'sticky', true ); 767 768 $response = $this->server->dispatch( $request ); 769 $this->assertCount( 0, $response->get_data() ); 770 771 // FIXME Since this request returns zero posts, the query is executed twice. 772 $this->assertCount( 2, $this->posts_clauses ); 773 $this->posts_clauses = array_slice( $this->posts_clauses, 0, 1 ); 774 775 $this->assertPostsWhere( " AND {posts}.ID IN (0) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" ); 776 } 777 778 public function test_get_items_sticky_with_include_no_sticky_posts() { 779 $id1 = self::$post_id; 780 781 update_option( 'sticky_posts', array() ); 782 783 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 784 $request->set_param( 'sticky', true ); 785 $request->set_param( 'include', array( $id1 ) ); 786 787 $response = $this->server->dispatch( $request ); 788 $this->assertCount( 0, $response->get_data() ); 789 790 // FIXME Since this request returns zero posts, the query is executed twice. 791 $this->assertCount( 2, $this->posts_clauses ); 792 $this->posts_clauses = array_slice( $this->posts_clauses, 0, 1 ); 793 794 $this->assertPostsWhere( " AND {posts}.ID IN (0) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" ); 795 } 796 797 public function test_get_items_not_sticky() { 744 798 $id1 = self::$post_id; 745 799 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); … … 756 810 $post = $posts[0]; 757 811 $this->assertEquals( $id1, $post['id'] ); 758 } 759 760 public function test_get_items_sticky_with_post__not_in_query() { 812 813 $this->assertPostsWhere( " AND {posts}.ID NOT IN ($id2) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" ); 814 } 815 816 public function test_get_items_not_sticky_with_exclude() { 761 817 $id1 = self::$post_id; 762 818 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); … … 775 831 $post = $posts[0]; 776 832 $this->assertEquals( $id1, $post['id'] ); 833 834 $this->assertPostsWhere( " AND {posts}.ID NOT IN ($id3,$id2) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" ); 835 } 836 837 public function test_get_items_not_sticky_with_exclude_no_sticky_posts() { 838 $id1 = self::$post_id; 839 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 840 $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) ); 841 842 update_option( 'sticky_posts', array() ); 843 844 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 845 $request->set_param( 'sticky', false ); 846 $request->set_param( 'exclude', array( $id3 ) ); 847 848 $response = $this->server->dispatch( $request ); 849 $this->assertCount( 2, $response->get_data() ); 850 851 $posts = $response->get_data(); 852 $ids = wp_list_pluck( $posts, 'id' ); 853 sort( $ids ); 854 $this->assertEquals( array( $id1, $id2 ), $ids ); 855 856 $this->assertPostsWhere( " AND {posts}.ID NOT IN ($id3) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" ); 777 857 } 778 858 … … 2971 3051 } 2972 3052 remove_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 ); 2973 remove_filter( 'posts_ orderby', array( $this, 'save_posts_orderby' ), 10, 2 );3053 remove_filter( 'posts_clauses', array( $this, 'save_posts_clauses' ), 10, 2 ); 2974 3054 parent::tearDown(); 2975 3055 }
Note: See TracChangeset
for help on using the changeset viewer.