Make WordPress Core

Changeset 46657


Ignore:
Timestamp:
11/05/2019 08:41:12 PM (7 years ago)
Author:
SergeyBiryukov
Message:

REST API: Speed up pagination unit tests by creating less fixtures and reusing them where possible.

Includes minor documentation and code layout fixes for better readability.

See #30017, #48145.

Location:
trunk/tests/phpunit/tests/rest-api
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r46586 r46657  
    14521452
    14531453        public function test_search_item_by_filename() {
    1454                 $id  = $this->factory->attachment->create_object(
     1454                $id1 = $this->factory->attachment->create_object(
    14551455                        $this->test_file,
    14561456                        0,
  • trunk/tests/phpunit/tests/rest-api/rest-categories-controller.php

    r46586 r46657  
    1616        protected static $subscriber;
    1717
     18        protected static $category_ids     = array();
     19        protected static $total_categories = 30;
     20        protected static $per_page         = 50;
     21
    1822        public static function wpSetUpBeforeClass( $factory ) {
    1923                self::$administrator = $factory->user->create(
     
    3236                        )
    3337                );
     38
     39                // Set up categories for pagination tests.
     40                for ( $i = 0; $i < self::$total_categories - 1; $i++ ) {
     41                        $category_ids[] = $factory->category->create(
     42                                array(
     43                                        'name' => "Category {$i}",
     44                                )
     45                        );
     46                }
    3447        }
    3548
     
    3750                self::delete_user( self::$administrator );
    3851                self::delete_user( self::$subscriber );
     52
     53                // Remove categories for pagination tests.
     54                foreach ( self::$category_ids as $category_id ) {
     55                        wp_delete_term( $category_id, 'category' );
     56                }
    3957        }
    4058
     
    137155
    138156        public function test_get_items() {
    139                 $request  = new WP_REST_Request( 'GET', '/wp/v2/categories' );
     157                $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
     158                $request->set_param( 'per_page', self::$per_page );
    140159                $response = rest_get_server()->dispatch( $request );
    141160                $this->check_get_taxonomy_terms_response( $response );
     
    144163        public function test_get_items_invalid_permission_for_context() {
    145164                wp_set_current_user( 0 );
     165
    146166                $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    147167                $request->set_param( 'context', 'edit' );
     
    154174                $category1 = $this->factory->category->create( array( 'name' => 'Season 5' ) );
    155175                $category2 = $this->factory->category->create( array( 'name' => 'The Be Sharps' ) );
     176
     177                $total_categories = self::$total_categories + 2;
     178
    156179                wp_set_object_terms( $post_id, array( $category1, $category2 ), 'category' );
    157                 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
     180
     181                $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
     182                $request->set_param( 'per_page', self::$per_page );
    158183                $request->set_param( 'hide_empty', true );
    159184                $response = rest_get_server()->dispatch( $request );
     
    167192                $response = rest_get_server()->dispatch( $request );
    168193                $data     = $response->get_data();
    169                 $this->assertEquals( 3, count( $data ) );
     194                $this->assertEquals( $total_categories, count( $data ) );
    170195        }
    171196
     
    185210                        )
    186211                );
    187                 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
     212
     213                $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
     214                $request->set_param( 'per_page', self::$per_page );
    188215                $request->set_param( 'parent', 0 );
    189216                $response = rest_get_server()->dispatch( $request );
     
    215242                        )
    216243                );
    217                 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
     244
     245                $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
     246                $request->set_param( 'per_page', self::$per_page );
    218247                $request->set_param( 'parent', '0' );
    219248                $response = rest_get_server()->dispatch( $request );
     
    255284        public function test_get_items_include_query() {
    256285                $id1 = $this->factory->category->create();
    257                 $this->factory->category->create();
    258                 $id3     = $this->factory->category->create();
    259                 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    260                 // Orderby=>asc
    261                 $request->set_param( 'include', array( $id3, $id1 ) );
     286                $id2 = $this->factory->category->create();
     287
     288                $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
     289
     290                // 'orderby' => 'asc'.
     291                $request->set_param( 'include', array( $id2, $id1 ) );
    262292                $response = rest_get_server()->dispatch( $request );
    263293                $data     = $response->get_data();
    264294                $this->assertEquals( 2, count( $data ) );
    265295                $this->assertEquals( $id1, $data[0]['id'] );
    266                 // Orderby=>include
     296
     297                // 'orderby' => 'include'.
    267298                $request->set_param( 'orderby', 'include' );
    268299                $response = rest_get_server()->dispatch( $request );
    269300                $data     = $response->get_data();
    270301                $this->assertEquals( 2, count( $data ) );
    271                 $this->assertEquals( $id3, $data[0]['id'] );
     302                $this->assertEquals( $id2, $data[0]['id'] );
    272303        }
    273304
    274305        public function test_get_items_exclude_query() {
    275                 $id1      = $this->factory->category->create();
    276                 $id2      = $this->factory->category->create();
    277                 $request  = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    278                 $response = rest_get_server()->dispatch( $request );
    279                 $data     = $response->get_data();
    280                 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
    281                 $this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
     306                $id1 = $this->factory->category->create();
     307                $id2 = $this->factory->category->create();
     308
     309                $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
     310                $request->set_param( 'per_page', self::$per_page );
     311                $response = rest_get_server()->dispatch( $request );
     312                $data     = $response->get_data();
     313                $ids      = wp_list_pluck( $data, 'id' );
     314                $this->assertTrue( in_array( $id1, $ids, true ) );
     315                $this->assertTrue( in_array( $id2, $ids, true ) );
     316
    282317                $request->set_param( 'exclude', array( $id2 ) );
    283318                $response = rest_get_server()->dispatch( $request );
    284319                $data     = $response->get_data();
    285                 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
    286                 $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
     320                $ids      = wp_list_pluck( $data, 'id' );
     321                $this->assertTrue( in_array( $id1, $ids, true ) );
     322                $this->assertFalse( in_array( $id2, $ids, true ) );
    287323        }
    288324
     
    290326                $this->factory->category->create( array( 'name' => 'Apple' ) );
    291327                $this->factory->category->create( array( 'name' => 'Banana' ) );
     328
    292329                /*
    293330                 * Tests:
     
    305342                $this->assertEquals( 1, count( $data ) );
    306343                $this->assertEquals( 'Uncategorized', $data[0]['name'] );
     344
    307345                $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    308346                $request->set_param( 'orderby', 'name' );
     
    320358                $this->factory->category->create( array( 'name' => 'Apple' ) );
    321359                $this->factory->category->create( array( 'name' => 'Banana' ) );
    322                 // defaults to orderby=name, order=asc
     360
     361                // Defaults to 'orderby' => 'name', 'order' => 'asc'.
    323362                $request  = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    324363                $response = rest_get_server()->dispatch( $request );
     
    328367                $this->assertEquals( 'Banana', $data[1]['name'] );
    329368                $this->assertEquals( 'Cantaloupe', $data[2]['name'] );
    330                 $this->assertEquals( 'Uncategorized', $data[3]['name'] );
    331                 // orderby=id, with default order=asc
     369
     370                // 'orderby' => 'id', with default 'order' => 'asc'.
    332371                $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    333372                $request->set_param( 'orderby', 'id' );
     
    335374                $this->assertEquals( 200, $response->get_status() );
    336375                $data = $response->get_data();
    337                 $this->assertEquals( 'Uncategorized', $data[0]['name'] );
    338                 $this->assertEquals( 'Cantaloupe', $data[1]['name'] );
    339                 $this->assertEquals( 'Apple', $data[2]['name'] );
    340                 $this->assertEquals( 'Banana', $data[3]['name'] );
    341                 // orderby=id, order=desc
     376                $this->assertEquals( 'Category 0', $data[1]['name'] );
     377                $this->assertEquals( 'Category 1', $data[2]['name'] );
     378                $this->assertEquals( 'Category 2', $data[3]['name'] );
     379
     380                // 'orderby' => 'id', 'order' => 'desc'.
    342381                $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    343382                $request->set_param( 'orderby', 'id' );
     
    487526                $this->factory->category->create( array( 'name' => 'Apple' ) );
    488527                $this->factory->category->create( array( 'name' => 'Banana' ) );
     528
    489529                /*
    490530                 * Tests:
     
    498538                $this->assertEquals( 1, count( $data ) );
    499539                $this->assertEquals( 'Apple', $data[0]['name'] );
     540
    500541                $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    501542                $request->set_param( 'search', 'Garbage' );
     
    509550                $this->factory->category->create( array( 'name' => 'Apple' ) );
    510551                $this->factory->category->create( array( 'name' => 'Banana' ) );
     552
    511553                $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    512554                $request->set_param( 'slug', 'apple' );
     
    526568                        )
    527569                );
     570
    528571                $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    529572                $request->set_param( 'parent', $category1 );
     
    535578
    536579        public function test_get_terms_invalid_parent_arg() {
    537                 $category1 = $this->factory->category->create( array( 'name' => 'Parent' ) );
    538                 $this->factory->category->create(
    539                         array(
    540                                 'name'   => 'Child',
    541                                 'parent' => $category1,
    542                         )
    543                 );
    544580                $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    545581                $request->set_param( 'parent', 'invalid-parent' );
     
    575611
    576612        public function test_get_terms_pagination_headers() {
    577                 // Start of the index + Uncategorized default term
    578                 for ( $i = 0; $i < 49; $i++ ) {
    579                         $this->factory->category->create(
    580                                 array(
    581                                         'name' => "Category {$i}",
    582                                 )
    583                         );
    584                 }
     613                $total_categories = self::$total_categories;
     614                $total_pages      = (int) ceil( $total_categories / 10 );
     615
     616                // Start of the index + Uncategorized default term.
    585617                $request  = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    586618                $response = rest_get_server()->dispatch( $request );
    587619                $headers  = $response->get_headers();
    588                 $this->assertEquals( 50, $headers['X-WP-Total'] );
    589                 $this->assertEquals( 5, $headers['X-WP-TotalPages'] );
     620                $this->assertEquals( $total_categories, $headers['X-WP-Total'] );
     621                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    590622                $this->assertCount( 10, $response->get_data() );
    591623                $next_link = add_query_arg(
     
    597629                $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
    598630                $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    599                 // 3rd page
    600                 $this->factory->category->create(
    601                         array(
    602                                 'name' => 'Category 51',
    603                         )
    604                 );
     631
     632                // 3rd page.
     633                $this->factory->category->create();
     634                $total_categories++;
     635                $total_pages++;
    605636                $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    606637                $request->set_param( 'page', 3 );
    607638                $response = rest_get_server()->dispatch( $request );
    608639                $headers  = $response->get_headers();
    609                 $this->assertEquals( 51, $headers['X-WP-Total'] );
    610                 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
     640                $this->assertEquals( $total_categories, $headers['X-WP-Total'] );
     641                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    611642                $this->assertCount( 10, $response->get_data() );
    612643                $prev_link = add_query_arg(
     
    624655                );
    625656                $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    626                 // Last page
    627                 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    628                 $request->set_param( 'page', 6 );
     657
     658                // Last page.
     659                $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
     660                $request->set_param( 'page', $total_pages );
    629661                $response = rest_get_server()->dispatch( $request );
    630662                $headers  = $response->get_headers();
    631                 $this->assertEquals( 51, $headers['X-WP-Total'] );
    632                 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
     663                $this->assertEquals( $total_categories, $headers['X-WP-Total'] );
     664                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    633665                $this->assertCount( 1, $response->get_data() );
    634666                $prev_link = add_query_arg(
    635667                        array(
    636                                 'page' => 5,
     668                                'page' => $total_pages - 1,
    637669                        ),
    638670                        rest_url( 'wp/v2/categories' )
     
    640672                $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    641673                $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
    642                 // Out of bounds
    643                 $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    644                 $request->set_param( 'page', 8 );
     674
     675                // Out of bounds.
     676                $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
     677                $request->set_param( 'page', 100 );
    645678                $response = rest_get_server()->dispatch( $request );
    646679                $headers  = $response->get_headers();
    647                 $this->assertEquals( 51, $headers['X-WP-Total'] );
    648                 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
     680                $this->assertEquals( $total_categories, $headers['X-WP-Total'] );
     681                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    649682                $this->assertCount( 0, $response->get_data() );
    650683                $prev_link = add_query_arg(
    651684                        array(
    652                                 'page' => 6,
     685                                'page' => $total_pages,
    653686                        ),
    654687                        rest_url( 'wp/v2/categories' )
     
    659692
    660693        public function test_get_items_per_page_exceeds_number_of_items() {
    661                 // Start of the index + Uncategorized default term
    662                 for ( $i = 0; $i < 17; $i++ ) {
    663                         $this->factory->category->create(
    664                                 array(
    665                                         'name' => "Category {$i}",
    666                                 )
    667                         );
    668                 }
     694                // Start of the index + Uncategorized default term.
    669695                $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    670696                $request->set_param( 'page', 1 );
     
    672698                $response = rest_get_server()->dispatch( $request );
    673699                $headers  = $response->get_headers();
    674                 $this->assertEquals( 18, $headers['X-WP-Total'] );
     700                $this->assertEquals( self::$total_categories, $headers['X-WP-Total'] );
    675701                $this->assertEquals( 1, $headers['X-WP-TotalPages'] );
    676                 $this->assertCount( 18, $response->get_data() );
     702                $this->assertCount( self::$total_categories, $response->get_data() );
     703
    677704                $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    678705                $request->set_param( 'page', 2 );
     
    680707                $response = rest_get_server()->dispatch( $request );
    681708                $headers  = $response->get_headers();
    682                 $this->assertEquals( 18, $headers['X-WP-Total'] );
     709                $this->assertEquals( self::$total_categories, $headers['X-WP-Total'] );
    683710                $this->assertEquals( 1, $headers['X-WP-TotalPages'] );
    684711                $this->assertCount( 0, $response->get_data() );
     
    738765        public function test_get_item_invalid_permission_for_context() {
    739766                wp_set_current_user( 0 );
     767
    740768                $request = new WP_REST_Request( 'GET', '/wp/v2/categories/1' );
    741769                $request->set_param( 'context', 'edit' );
     
    760788        public function test_get_item_incorrect_taxonomy() {
    761789                register_taxonomy( 'robin', 'post' );
    762                 $term1    = $this->factory->term->create(
     790                $term1 = $this->factory->term->create(
    763791                        array(
    764792                                'name'     => 'Cape',
     
    766794                        )
    767795                );
     796
    768797                $request  = new WP_REST_Request( 'GET', '/wp/v2/categories/' . $term1 );
    769798                $response = rest_get_server()->dispatch( $request );
     
    773802        public function test_create_item() {
    774803                wp_set_current_user( self::$administrator );
     804
    775805                $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
    776806                $request->set_param( 'name', 'My Awesome Term' );
     
    792822        public function test_create_item_term_already_exists() {
    793823                wp_set_current_user( self::$administrator );
     824
    794825                $existing_id = $this->factory->category->create( array( 'name' => 'Existing' ) );
    795826
     
    808839        public function test_create_item_invalid_taxonomy() {
    809840                wp_set_current_user( self::$administrator );
     841
    810842                $request = new WP_REST_Request( 'POST', '/wp/v2/invalid-taxonomy' );
    811843                $request->set_param( 'name', 'Invalid Taxonomy' );
     
    816848        public function test_create_item_incorrect_permissions() {
    817849                wp_set_current_user( self::$subscriber );
     850
    818851                $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
    819852                $request->set_param( 'name', 'Incorrect permissions' );
     
    824857        public function test_create_item_incorrect_permissions_contributor() {
    825858                wp_set_current_user( self::$contributor );
     859
    826860                $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
    827861                $request->set_param( 'name', 'Incorrect permissions' );
     
    832866        public function test_create_item_missing_arguments() {
    833867                wp_set_current_user( self::$administrator );
     868
    834869                $request  = new WP_REST_Request( 'POST', '/wp/v2/categories' );
    835870                $response = rest_get_server()->dispatch( $request );
     
    839874        public function test_create_item_with_parent() {
    840875                wp_set_current_user( self::$administrator );
    841                 $parent  = wp_insert_term( 'test-category', 'category' );
     876
     877                $parent = wp_insert_term( 'test-category', 'category' );
     878
    842879                $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
    843880                $request->set_param( 'name', 'My Awesome Term' );
     
    851888        public function test_create_item_invalid_parent() {
    852889                wp_set_current_user( self::$administrator );
     890
    853891                $term = get_term_by( 'id', $this->factory->category->create(), 'category' );
    854892
     
    862900        public function test_create_item_with_no_parent() {
    863901                wp_set_current_user( self::$administrator );
    864                 $parent  = 0;
     902
     903                $parent = 0;
     904
    865905                $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
    866906                $request->set_param( 'name', 'My Awesome Term' );
     
    874914        public function test_update_item() {
    875915                wp_set_current_user( self::$administrator );
     916
    876917                $orig_args = array(
    877918                        'name'        => 'Original Name',
     
    879920                        'slug'        => 'original-slug',
    880921                );
    881                 $term      = get_term_by( 'id', $this->factory->category->create( $orig_args ), 'category' );
    882                 $request   = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id );
     922
     923                $term = get_term_by( 'id', $this->factory->category->create( $orig_args ), 'category' );
     924
     925                $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id );
    883926                $request->set_param( 'name', 'New Name' );
    884927                $request->set_param( 'description', 'New Description' );
     
    905948        public function test_update_item_invalid_taxonomy() {
    906949                wp_set_current_user( self::$administrator );
     950
    907951                $request = new WP_REST_Request( 'POST', '/wp/v2/invalid-taxonomy/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    908952                $request->set_param( 'name', 'Invalid Taxonomy' );
     
    913957        public function test_update_item_invalid_term() {
    914958                wp_set_current_user( self::$administrator );
     959
    915960                $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    916961                $request->set_param( 'name', 'Invalid Term' );
     
    921966        public function test_update_item_incorrect_permissions() {
    922967                wp_set_current_user( self::$subscriber );
    923                 $term    = get_term_by( 'id', $this->factory->category->create(), 'category' );
     968
     969                $term = get_term_by( 'id', $this->factory->category->create(), 'category' );
     970
    924971                $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id );
    925972                $request->set_param( 'name', 'Incorrect permissions' );
     
    930977        public function test_update_item_parent() {
    931978                wp_set_current_user( self::$administrator );
     979
    932980                $parent = get_term_by( 'id', $this->factory->category->create(), 'category' );
    933981                $term   = get_term_by( 'id', $this->factory->category->create(), 'category' );
     
    9711019        public function test_update_item_invalid_parent() {
    9721020                wp_set_current_user( self::$administrator );
     1021
    9731022                $term = get_term_by( 'id', $this->factory->category->create(), 'category' );
    9741023
     
    9811030        public function test_delete_item() {
    9821031                wp_set_current_user( self::$administrator );
    983                 $term    = get_term_by( 'id', $this->factory->category->create( array( 'name' => 'Deleted Category' ) ), 'category' );
     1032
     1033                $term = get_term_by( 'id', $this->factory->category->create( array( 'name' => 'Deleted Category' ) ), 'category' );
     1034
    9841035                $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id );
    9851036                $request->set_param( 'force', true );
     
    9931044        public function test_delete_item_no_trash() {
    9941045                wp_set_current_user( self::$administrator );
     1046
    9951047                $term = get_term_by( 'id', $this->factory->category->create( array( 'name' => 'Deleted Category' ) ), 'category' );
    9961048
     
    10061058        public function test_delete_item_invalid_taxonomy() {
    10071059                wp_set_current_user( self::$administrator );
     1060
    10081061                $request  = new WP_REST_Request( 'DELETE', '/wp/v2/invalid-taxonomy/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    10091062                $response = rest_get_server()->dispatch( $request );
     
    10131066        public function test_delete_item_invalid_term() {
    10141067                wp_set_current_user( self::$administrator );
     1068
    10151069                $request  = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    10161070                $response = rest_get_server()->dispatch( $request );
     
    10201074        public function test_delete_item_incorrect_permissions() {
    10211075                wp_set_current_user( self::$subscriber );
     1076
    10221077                $term     = get_term_by( 'id', $this->factory->category->create(), 'category' );
    10231078                $request  = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id );
  • trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r46586 r46657  
    2626        protected static $hold_id;
    2727
     28        protected static $comment_ids    = array();
     29        protected static $total_comments = 30;
     30        protected static $per_page       = 50;
     31
    2832        protected $endpoint;
    2933
     
    111115                        )
    112116                );
     117
     118                // Set up comments for pagination tests.
     119                for ( $i = 0; $i < self::$total_comments - 1; $i++ ) {
     120                        $comment_ids[] = $factory->comment->create(
     121                                array(
     122                                        'comment_content' => "Comment {$i}",
     123                                        'comment_post_ID' => self::$post_id,
     124                                )
     125                        );
     126                }
    113127        }
    114128
     
    130144                wp_delete_post( self::$approved_id, true );
    131145                wp_delete_post( self::$hold_id, true );
     146
     147                // Remove comments for pagination tests.
     148                foreach ( self::$comment_ids as $comment_id ) {
     149                        wp_delete_comment( $comment_id, true );
     150                }
    132151        }
    133152
     
    198217
    199218        public function test_get_items() {
    200                 $this->factory->comment->create_post_comments( self::$post_id, 6 );
    201 
    202219                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
     220                $request->set_param( 'per_page', self::$per_page );
    203221
    204222                $response = rest_get_server()->dispatch( $request );
     
    206224
    207225                $comments = $response->get_data();
    208                 // We created 6 comments in this method, plus self::$approved_id.
    209                 $this->assertCount( 7, $comments );
     226                $this->assertCount( self::$total_comments, $comments );
    210227        }
    211228
     
    216233                wp_set_current_user( 0 );
    217234
    218                 $args             = array(
     235                $args = array(
    219236                        'comment_approved' => 1,
    220237                        'comment_post_ID'  => self::$password_id,
    221238                );
     239
    222240                $password_comment = $this->factory->comment->create( $args );
    223241
     
    238256        public function test_get_items_with_password_without_post() {
    239257                wp_set_current_user( 0 );
    240                 $args             = array(
     258
     259                $args = array(
    241260                        'comment_approved' => 1,
    242261                        'comment_post_ID'  => self::$password_id,
    243262                );
     263
    244264                $password_comment = $this->factory->comment->create( $args );
    245265
     
    259279        public function test_get_items_with_password_with_multiple_post() {
    260280                wp_set_current_user( 0 );
    261                 $args             = array(
     281
     282                $args = array(
    262283                        'comment_approved' => 1,
    263284                        'comment_post_ID'  => self::$password_id,
    264285                );
     286
    265287                $password_comment = $this->factory->comment->create( $args );
    266288
     
    276298                wp_set_current_user( 0 );
    277299
    278                 $args             = array(
     300                $args = array(
    279301                        'comment_approved' => 1,
    280302                        'comment_post_ID'  => self::$password_id,
    281303                );
     304
    282305                $password_comment = $this->factory->comment->create( $args );
    283306
     
    294317                wp_set_current_user( self::$admin_id );
    295318
    296                 $args             = array(
     319                $args = array(
    297320                        'comment_approved' => 1,
    298321                        'comment_post_ID'  => self::$password_id,
    299322                );
     323
    300324                $password_comment = $this->factory->comment->create( $args );
    301325
     
    312336                wp_set_current_user( 0 );
    313337
    314                 $args            = array(
     338                $args = array(
    315339                        'comment_approved' => 1,
    316340                        'comment_post_ID'  => self::$private_id,
    317341                );
     342
    318343                $private_comment = $this->factory->comment->create( $args );
    319344
     
    330355                wp_set_current_user( self::$admin_id );
    331356
    332                 $args            = array(
     357                $args = array(
    333358                        'comment_approved' => 1,
    334359                        'comment_post_ID'  => self::$private_id,
    335360                );
     361
    336362                $private_comment = $this->factory->comment->create( $args );
    337363
     
    389415        public function test_get_items_no_permission_for_context() {
    390416                wp_set_current_user( 0 );
     417
    391418                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    392419                $request->set_param( 'context', 'edit' );
     
    396423
    397424        public function test_get_items_no_post() {
     425                wp_set_current_user( self::$admin_id );
     426
    398427                $this->factory->comment->create_post_comments( 0, 2 );
    399                 wp_set_current_user( self::$admin_id );
     428
    400429                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    401430                $request->set_param( 'post', 0 );
     
    408437        public function test_get_items_no_permission_for_no_post() {
    409438                wp_set_current_user( 0 );
     439
    410440                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    411441                $request->set_param( 'post', 0 );
     
    416446        public function test_get_items_edit_context() {
    417447                wp_set_current_user( self::$admin_id );
     448
    418449                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    419450                $request->set_param( 'context', 'edit' );
     
    442473        public function test_get_items_include_query() {
    443474                wp_set_current_user( self::$admin_id );
     475
    444476                $args = array(
    445477                        'comment_approved' => 1,
    446478                        'comment_post_ID'  => self::$post_id,
    447479                );
    448                 $id1  = $this->factory->comment->create( $args );
    449                 $this->factory->comment->create( $args );
    450                 $id3     = $this->factory->comment->create( $args );
     480
     481                $id1 = $this->factory->comment->create( $args );
     482                $id2 = $this->factory->comment->create( $args );
     483
    451484                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    452                 // Order=>asc
     485
     486                // 'order' => 'asc'.
    453487                $request->set_param( 'order', 'asc' );
    454                 $request->set_param( 'include', array( $id3, $id1 ) );
     488                $request->set_param( 'include', array( $id2, $id1 ) );
    455489                $response = rest_get_server()->dispatch( $request );
    456490                $data     = $response->get_data();
    457491                $this->assertEquals( 2, count( $data ) );
    458492                $this->assertEquals( $id1, $data[0]['id'] );
    459                 // Orderby=>include
     493
     494                // 'orderby' => 'include'.
    460495                $request->set_param( 'orderby', 'include' );
    461496                $response = rest_get_server()->dispatch( $request );
    462497                $data     = $response->get_data();
    463498                $this->assertEquals( 2, count( $data ) );
    464                 $this->assertEquals( $id3, $data[0]['id'] );
    465                 // Orderby=>invalid should fail.
     499                $this->assertEquals( $id2, $data[0]['id'] );
     500
     501                // Invalid 'orderby' should error.
    466502                $request->set_param( 'orderby', 'invalid' );
    467503                $response = rest_get_server()->dispatch( $request );
    468504                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    469                 // fails on invalid id.
     505
     506                // Invalid 'include' should error.
    470507                $request->set_param( 'orderby', array( 'include' ) );
    471508                $request->set_param( 'include', array( 'invalid' ) );
     
    476513        public function test_get_items_exclude_query() {
    477514                wp_set_current_user( self::$admin_id );
    478                 $args     = array(
    479                         'comment_approved' => 1,
    480                         'comment_post_ID'  => self::$post_id,
    481                 );
    482                 $id1      = $this->factory->comment->create( $args );
    483                 $id2      = $this->factory->comment->create( $args );
    484                 $request  = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    485                 $response = rest_get_server()->dispatch( $request );
    486                 $data     = $response->get_data();
    487                 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
    488                 $this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
    489                 $request->set_param( 'exclude', array( $id2 ) );
    490                 $response = rest_get_server()->dispatch( $request );
    491                 $data     = $response->get_data();
    492                 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
    493                 $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
    494 
    495                 // fails on invalid id.
    496                 $request->set_param( 'exclude', array( 'invalid' ) );
    497                 $response = rest_get_server()->dispatch( $request );
    498                 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    499         }
    500 
    501         public function test_get_items_offset_query() {
    502                 wp_set_current_user( self::$admin_id );
     515
    503516                $args = array(
    504517                        'comment_approved' => 1,
    505518                        'comment_post_ID'  => self::$post_id,
    506519                );
    507                 $this->factory->comment->create( $args );
    508                 $this->factory->comment->create( $args );
    509                 $this->factory->comment->create( $args );
     520
     521                $id1 = $this->factory->comment->create( $args );
     522                $id2 = $this->factory->comment->create( $args );
     523
     524                $request  = new WP_REST_Request( 'GET', '/wp/v2/comments' );
     525                $response = rest_get_server()->dispatch( $request );
     526                $data     = $response->get_data();
     527                $ids      = wp_list_pluck( $data, 'id' );
     528                $this->assertTrue( in_array( $id1, $ids, true ) );
     529                $this->assertTrue( in_array( $id2, $ids, true ) );
     530
     531                $request->set_param( 'exclude', array( $id2 ) );
     532                $response = rest_get_server()->dispatch( $request );
     533                $data     = $response->get_data();
     534                $ids      = wp_list_pluck( $data, 'id' );
     535                $this->assertTrue( in_array( $id1, $ids, true ) );
     536                $this->assertFalse( in_array( $id2, $ids, true ) );
     537
     538                // Invalid 'exclude' should error.
     539                $request->set_param( 'exclude', array( 'invalid' ) );
     540                $response = rest_get_server()->dispatch( $request );
     541                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     542        }
     543
     544        public function test_get_items_offset_query() {
     545                wp_set_current_user( self::$admin_id );
     546
    510547                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
     548                $request->set_param( 'per_page', self::$per_page );
    511549                $request->set_param( 'offset', 1 );
    512550                $response = rest_get_server()->dispatch( $request );
    513                 $this->assertCount( 3, $response->get_data() );
    514                 // 'offset' works with 'per_page'
     551                $this->assertCount( self::$total_comments - 1, $response->get_data() );
     552
     553                // 'offset' works with 'per_page'.
    515554                $request->set_param( 'per_page', 2 );
    516555                $response = rest_get_server()->dispatch( $request );
    517556                $this->assertCount( 2, $response->get_data() );
    518                 // 'offset' takes priority over 'page'
     557
     558                // 'offset' takes priority over 'page'.
    519559                $request->set_param( 'page', 3 );
    520560                $response = rest_get_server()->dispatch( $request );
    521561                $this->assertCount( 2, $response->get_data() );
    522                 // 'offset' with invalid value errors.
     562
     563                // Invalid 'offset' should error.
    523564                $request->set_param( 'offset', 'moreplease' );
    524565                $response = rest_get_server()->dispatch( $request );
     
    528569        public function test_get_items_order_query() {
    529570                wp_set_current_user( self::$admin_id );
     571
    530572                $args = array(
    531573                        'comment_approved' => 1,
    532574                        'comment_post_ID'  => self::$post_id,
    533575                );
    534                 $this->factory->comment->create( $args );
    535                 $this->factory->comment->create( $args );
    536                 $id3     = $this->factory->comment->create( $args );
     576
     577                $id = $this->factory->comment->create( $args );
     578
    537579                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    538                 // order defaults to 'desc'
     580
     581                // Order defaults to 'desc'.
    539582                $response = rest_get_server()->dispatch( $request );
    540583                $data     = $response->get_data();
    541                 $this->assertEquals( $id3, $data[0]['id'] );
    542                 // order=>asc
     584                $this->assertEquals( $id, $data[0]['id'] );
     585
     586                // 'order' => 'asc'.
    543587                $request->set_param( 'order', 'asc' );
    544588                $response = rest_get_server()->dispatch( $request );
    545589                $data     = $response->get_data();
    546590                $this->assertEquals( self::$approved_id, $data[0]['id'] );
    547                 // order=>asc,id should fail
     591
     592                // 'order' => 'asc,id' should error.
    548593                $request->set_param( 'order', 'asc,id' );
    549594                $response = rest_get_server()->dispatch( $request );
     
    553598        public function test_get_items_private_post_no_permissions() {
    554599                wp_set_current_user( 0 );
     600
    555601                $post_id = $this->factory->post->create( array( 'post_status' => 'private' ) );
     602
    556603                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    557604                $request->set_param( 'post', $post_id );
     
    561608
    562609        public function test_get_items_author_arg() {
    563                 // Authorized
    564                 wp_set_current_user( self::$admin_id );
     610                // Authorized.
     611                wp_set_current_user( self::$admin_id );
     612
    565613                $args = array(
    566614                        'comment_approved' => 1,
     
    568616                        'user_id'          => self::$author_id,
    569617                );
     618
    570619                $this->factory->comment->create( $args );
    571620                $args['user_id'] = self::$subscriber_id;
     
    574623                $this->factory->comment->create( $args );
    575624
    576                 // 'author' limits result to 1 of 3
     625                // Limit to comment author.
    577626                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    578627                $request->set_param( 'author', self::$author_id );
     
    581630                $comments = $response->get_data();
    582631                $this->assertCount( 1, $comments );
    583                 // Multiple authors are supported
     632
     633                // Multiple authors are supported.
    584634                $request->set_param( 'author', array( self::$author_id, self::$subscriber_id ) );
    585635                $response = rest_get_server()->dispatch( $request );
     
    587637                $comments = $response->get_data();
    588638                $this->assertCount( 2, $comments );
    589                 // Invalid author param errors
     639
     640                // Invalid 'author' should error.
    590641                $request->set_param( 'author', 'skippy' );
    591642                $response = rest_get_server()->dispatch( $request );
    592643                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    593                 // Unavailable to unauthenticated; defaults to error
     644
     645                // Unavailable to unauthenticated; defaults to error.
    594646                wp_set_current_user( 0 );
    595647                $request->set_param( 'author', array( self::$author_id, self::$subscriber_id ) );
     
    599651
    600652        public function test_get_items_author_exclude_arg() {
    601                 // Authorized
    602                 wp_set_current_user( self::$admin_id );
     653                // Authorized.
     654                wp_set_current_user( self::$admin_id );
     655
    603656                $args = array(
    604657                        'comment_approved' => 1,
     
    606659                        'user_id'          => self::$author_id,
    607660                );
     661
    608662                $this->factory->comment->create( $args );
    609663                $args['user_id'] = self::$subscriber_id;
     
    612666                $this->factory->comment->create( $args );
    613667
    614                 $request  = new WP_REST_Request( 'GET', '/wp/v2/comments' );
     668                $total_comments = self::$total_comments + 3;
     669
     670                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
     671                $request->set_param( 'per_page', self::$per_page );
    615672                $response = rest_get_server()->dispatch( $request );
    616673                $comments = $response->get_data();
    617                 $this->assertCount( 4, $comments );
    618 
    619                 // 'author_exclude' limits result to 3 of 4
     674                $this->assertCount( $total_comments, $comments );
     675
     676                // Exclude comment author.
    620677                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
     678                $request->set_param( 'per_page', self::$per_page );
    621679                $request->set_param( 'author_exclude', self::$author_id );
    622680                $response = rest_get_server()->dispatch( $request );
    623681                $this->assertEquals( 200, $response->get_status() );
    624682                $comments = $response->get_data();
    625                 $this->assertCount( 3, $comments );
    626                 // 'author_exclude' for both comment authors (2 of 4)
     683                $this->assertCount( $total_comments - 1, $comments );
     684
     685                // Exclude both comment authors.
    627686                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
     687                $request->set_param( 'per_page', self::$per_page );
    628688                $request->set_param( 'author_exclude', array( self::$author_id, self::$subscriber_id ) );
    629689                $response = rest_get_server()->dispatch( $request );
    630690                $this->assertEquals( 200, $response->get_status() );
    631691                $comments = $response->get_data();
    632                 $this->assertCount( 2, $comments );
    633                 // 'author_exclude' for both invalid author
     692                $this->assertCount( $total_comments - 2, $comments );
     693
     694                // 'author_exclude' for invalid author.
    634695                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    635696                $request->set_param( 'author_exclude', 'skippy' );
    636697                $response = rest_get_server()->dispatch( $request );
    637698                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    638                 // Unavailable to unauthenticated; defaults to error
     699
     700                // Unavailable to unauthenticated; defaults to error.
    639701                wp_set_current_user( 0 );
    640702                $request->set_param( 'author_exclude', array( self::$author_id, self::$subscriber_id ) );
     
    654716                $args['comment_parent'] = $parent_id2;
    655717                $this->factory->comment->create( $args );
    656                 // All comments in the database
    657                 $request  = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    658                 $response = rest_get_server()->dispatch( $request );
    659                 $this->assertCount( 5, $response->get_data() );
    660                 // Limit to the parent
     718
     719                $total_comments = self::$total_comments + 4;
     720
     721                // All comments in the database.
     722                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
     723                $request->set_param( 'per_page', self::$per_page );
     724                $response = rest_get_server()->dispatch( $request );
     725                $this->assertCount( $total_comments, $response->get_data() );
     726
     727                // Limit to the parent.
    661728                $request->set_param( 'parent', $parent_id );
    662729                $response = rest_get_server()->dispatch( $request );
    663730                $this->assertCount( 1, $response->get_data() );
    664                 // Limit to two parents
     731
     732                // Limit to two parents.
    665733                $request->set_param( 'parent', array( $parent_id, $parent_id2 ) );
    666734                $response = rest_get_server()->dispatch( $request );
    667735                $this->assertCount( 2, $response->get_data() );
    668                 // Invalid parent should error
     736
     737                // Invalid 'parent' should error.
    669738                $request->set_param( 'parent', 'invalid' );
    670739                $response = rest_get_server()->dispatch( $request );
     
    683752                $args['comment_parent'] = $parent_id2;
    684753                $this->factory->comment->create( $args );
    685                 // All comments in the database
    686                 $request  = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    687                 $response = rest_get_server()->dispatch( $request );
    688                 $this->assertCount( 5, $response->get_data() );
    689                 // Exclude this particular parent
     754
     755                $total_comments = self::$total_comments + 4;
     756
     757                // All comments in the database.
     758                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
     759                $request->set_param( 'per_page', self::$per_page );
     760                $response = rest_get_server()->dispatch( $request );
     761                $this->assertCount( $total_comments, $response->get_data() );
     762
     763                // Exclude this particular parent.
    690764                $request->set_param( 'parent_exclude', $parent_id );
    691765                $response = rest_get_server()->dispatch( $request );
    692                 $this->assertCount( 4, $response->get_data() );
    693                 // Exclude both comment parents
     766                $this->assertCount( $total_comments - 1, $response->get_data() );
     767
     768                // Exclude both comment parents.
    694769                $request->set_param( 'parent_exclude', array( $parent_id, $parent_id2 ) );
    695770                $response = rest_get_server()->dispatch( $request );
    696                 $this->assertCount( 3, $response->get_data() );
    697                 // Invalid parent id should error
     771                $this->assertCount( $total_comments - 2, $response->get_data() );
     772
     773                // Invalid 'parent_exclude' should error.
    698774                $request->set_param( 'parent_exclude', 'invalid' );
    699775                $response = rest_get_server()->dispatch( $request );
     
    703779        public function test_get_items_search_query() {
    704780                wp_set_current_user( self::$admin_id );
    705                 $args                    = array(
     781
     782                $args = array(
    706783                        'comment_approved' => 1,
    707784                        'comment_post_ID'  => self::$post_id,
     
    709786                        'comment_author'   => 'Homer J Simpson',
    710787                );
    711                 $id1                     = $this->factory->comment->create( $args );
    712                 $args['comment_content'] = 'bar';
    713                 $this->factory->comment->create( $args );
    714                 $args['comment_content'] = 'burrito';
    715                 $this->factory->comment->create( $args );
    716                 // 3 comments, plus 1 created in construct
    717                 $request  = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    718                 $response = rest_get_server()->dispatch( $request );
    719                 $this->assertCount( 4, $response->get_data() );
    720                 // One matching comments
     788
     789                $id = $this->factory->comment->create( $args );
     790
     791                $total_comments = self::$total_comments + 1;
     792
     793                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
     794                $request->set_param( 'per_page', self::$per_page );
     795                $response = rest_get_server()->dispatch( $request );
     796                $this->assertCount( $total_comments, $response->get_data() );
     797
     798                // One matching comment.
    721799                $request->set_param( 'search', 'foo' );
    722800                $response = rest_get_server()->dispatch( $request );
    723801                $data     = $response->get_data();
    724802                $this->assertCount( 1, $data );
    725                 $this->assertEquals( $id1, $data[0]['id'] );
     803                $this->assertEquals( $id, $data[0]['id'] );
    726804        }
    727805
    728806        public function test_get_comments_pagination_headers() {
    729                 wp_set_current_user( self::$admin_id );
    730                 // Start of the index
    731                 for ( $i = 0; $i < 49; $i++ ) {
    732                         $this->factory->comment->create(
    733                                 array(
    734                                         'comment_content' => "Comment {$i}",
    735                                         'comment_post_ID' => self::$post_id,
    736                                 )
    737                         );
    738                 }
     807                $total_comments = self::$total_comments;
     808                $total_pages    = (int) ceil( $total_comments / 10 );
     809
     810                wp_set_current_user( self::$admin_id );
     811
     812                // Start of the index.
    739813                $request  = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    740814                $response = rest_get_server()->dispatch( $request );
    741815                $headers  = $response->get_headers();
    742                 $this->assertEquals( 50, $headers['X-WP-Total'] );
    743                 $this->assertEquals( 5, $headers['X-WP-TotalPages'] );
     816                $this->assertEquals( $total_comments, $headers['X-WP-Total'] );
     817                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    744818                $next_link = add_query_arg(
    745819                        array(
     
    750824                $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
    751825                $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    752                 // 3rd page
     826
     827                // 3rd page.
    753828                $this->factory->comment->create(
    754829                        array(
    755                                 'comment_content' => 'Comment 51',
    756830                                'comment_post_ID' => self::$post_id,
    757831                        )
    758832                );
     833                $total_comments++;
     834                $total_pages++;
    759835                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    760836                $request->set_param( 'page', 3 );
    761837                $response = rest_get_server()->dispatch( $request );
    762838                $headers  = $response->get_headers();
    763                 $this->assertEquals( 51, $headers['X-WP-Total'] );
    764                 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
     839                $this->assertEquals( $total_comments, $headers['X-WP-Total'] );
     840                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    765841                $prev_link = add_query_arg(
    766842                        array(
     
    777853                );
    778854                $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    779                 // Last page
     855
     856                // Last page.
    780857                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    781                 $request->set_param( 'page', 6 );
     858                $request->set_param( 'page', $total_pages );
    782859                $response = rest_get_server()->dispatch( $request );
    783860                $headers  = $response->get_headers();
    784                 $this->assertEquals( 51, $headers['X-WP-Total'] );
    785                 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
     861                $this->assertEquals( $total_comments, $headers['X-WP-Total'] );
     862                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    786863                $prev_link = add_query_arg(
    787864                        array(
    788                                 'page' => 5,
     865                                'page' => $total_pages - 1,
    789866                        ),
    790867                        rest_url( '/wp/v2/comments' )
     
    792869                $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    793870                $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
    794                 // Out of bounds
     871
     872                // Out of bounds.
    795873                $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
    796                 $request->set_param( 'page', 8 );
     874                $request->set_param( 'page', 100 );
    797875                $response = rest_get_server()->dispatch( $request );
    798876                $headers  = $response->get_headers();
    799                 $this->assertEquals( 51, $headers['X-WP-Total'] );
    800                 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
     877                $this->assertEquals( $total_comments, $headers['X-WP-Total'] );
     878                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    801879                $prev_link = add_query_arg(
    802880                        array(
    803                                 'page' => 6,
     881                                'page' => $total_pages,
    804882                        ),
    805883                        rest_url( '/wp/v2/comments' )
     
    858936        public function test_prepare_item() {
    859937                wp_set_current_user( self::$admin_id );
     938
    860939                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    861940                $request->set_query_params(
     
    874953        public function test_prepare_item_limit_fields() {
    875954                wp_set_current_user( self::$admin_id );
     955
    876956                $endpoint = new WP_REST_Comments_Controller;
    877957                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
     
    916996        public function test_get_comment_invalid_context() {
    917997                wp_set_current_user( 0 );
     998
    918999                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', self::$approved_id ) );
    9191000                $request->set_param( 'context', 'edit' );
     
    9241005        public function test_get_comment_invalid_post_id() {
    9251006                wp_set_current_user( 0 );
     1007
    9261008                $comment_id = $this->factory->comment->create(
    9271009                        array(
     
    9301012                        )
    9311013                );
    932                 $request    = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id );
    933 
     1014
     1015                $request  = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id );
    9341016                $response = rest_get_server()->dispatch( $request );
    9351017                $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
     
    9381020        public function test_get_comment_invalid_post_id_as_admin() {
    9391021                wp_set_current_user( self::$admin_id );
     1022
    9401023                $comment_id = $this->factory->comment->create(
    9411024                        array(
     
    9441027                        )
    9451028                );
    946                 $request    = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id );
    947 
     1029
     1030                $request  = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id );
    9481031                $response = rest_get_server()->dispatch( $request );
    9491032                $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
     
    9531036                wp_set_current_user( 0 );
    9541037
    955                 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) );
    956 
     1038                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) );
    9571039                $response = rest_get_server()->dispatch( $request );
    9581040                $this->assertErrorResponse( 'rest_cannot_read', $response, 401 );
     
    9621044                wp_set_current_user( self::$admin_id );
    9631045
    964                 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) );
    965 
     1046                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) );
    9661047                $response = rest_get_server()->dispatch( $request );
    9671048                $this->assertEquals( 200, $response->get_status() );
     
    10091090        public function test_get_comment_with_password_without_edit_post_permission() {
    10101091                wp_set_current_user( self::$subscriber_id );
    1011                 $args             = array(
     1092
     1093                $args = array(
    10121094                        'comment_approved' => 1,
    10131095                        'comment_post_ID'  => self::$password_id,
    10141096                );
     1097
    10151098                $password_comment = $this->factory->comment->create( $args );
    1016                 $request          = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $password_comment ) );
    1017                 $response         = rest_get_server()->dispatch( $request );
     1099
     1100                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $password_comment ) );
     1101                $response = rest_get_server()->dispatch( $request );
    10181102                $this->assertErrorResponse( 'rest_cannot_read', $response, 403 );
    10191103        }
     
    10251109                wp_set_current_user( self::$subscriber_id );
    10261110
    1027                 $args             = array(
     1111                $args = array(
    10281112                        'comment_approved' => 1,
    10291113                        'comment_post_ID'  => self::$password_id,
    10301114                );
     1115
    10311116                $password_comment = $this->factory->comment->create( $args );
    10321117
     
    11141199        public function test_create_comment_date( $params, $results ) {
    11151200                wp_set_current_user( self::$admin_id );
     1201
    11161202                update_option( 'timezone_string', $params['timezone_string'] );
    11171203
     
    12341320        public function test_create_comment_missing_required_author_email() {
    12351321                wp_set_current_user( self::$admin_id );
     1322
    12361323                update_option( 'require_name_email', 1 );
    12371324
     
    12521339        public function test_create_comment_empty_required_author_email() {
    12531340                wp_set_current_user( self::$admin_id );
     1341
    12541342                update_option( 'require_name_email', 1 );
    12551343
     
    13431431
    13441432                wp_set_current_user( self::$admin_id );
    1345                 $params  = array(
     1433
     1434                $params = array(
    13461435                        'post'         => self::$post_id,
    13471436                        'author_name'  => 'Comic Book Guy',
     
    13521441                        'date'         => '2014-11-07T10:14:25',
    13531442                );
     1443
    13541444                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
    13551445                $request->add_header( 'content-type', 'application/json' );
     
    13651455        public function test_create_comment_without_type() {
    13661456                $post_id = $this->factory->post->create();
     1457
    13671458                wp_set_current_user( self::$admin_id );
    13681459
     
    14021493        public function test_create_comment_with_invalid_type() {
    14031494                $post_id = $this->factory->post->create();
     1495
    14041496                wp_set_current_user( self::$admin_id );
    14051497
     
    14251517        public function test_create_comment_invalid_email() {
    14261518                $post_id = $this->factory->post->create();
     1519
    14271520                wp_set_current_user( self::$admin_id );
    14281521
     
    15691662        public function test_create_comment_with_status_IP_and_user_agent() {
    15701663                $post_id = $this->factory->post->create();
     1664
    15711665                wp_set_current_user( self::$admin_id );
    15721666
     
    16631757        public function test_create_comment_author_ip_no_permission() {
    16641758                wp_set_current_user( self::$subscriber_id );
    1665                 $params  = array(
     1759
     1760                $params = array(
    16661761                        'author_name'  => 'Comic Book Guy',
    16671762                        'author_email' => 'cbg@androidsdungeon.com',
     
    16711766                        'status'       => 'approved',
    16721767                );
     1768
    16731769                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
    16741770                $request->add_header( 'content-type', 'application/json' );
     
    16801776        public function test_create_comment_author_ip_defaults_to_remote_addr() {
    16811777                wp_set_current_user( self::$admin_id );
     1778
    16821779                $_SERVER['REMOTE_ADDR'] = '127.0.0.2';
    1683                 $params                 = array(
     1780
     1781                $params = array(
    16841782                        'post'         => self::$post_id,
    16851783                        'author_name'  => 'Comic Book Guy',
     
    16881786                        'content'      => 'Worst Comment Ever!',
    16891787                );
    1690                 $request                = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1788
     1789                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
    16911790                $request->add_header( 'content-type', 'application/json' );
    16921791                $request->set_body( wp_json_encode( $params ) );
     
    17001799                wp_set_current_user( self::$admin_id );
    17011800
    1702                 $params  = array(
     1801                $params = array(
    17031802                        'author_name'  => 'Comic Book Guy',
    17041803                        'author_email' => 'cbg@androidsdungeon.com',
     
    17071806                        'status'       => 'approved',
    17081807                );
     1808
    17091809                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
    17101810                $request->add_header( 'content-type', 'application/json' );
     
    17191819                wp_set_current_user( self::$subscriber_id );
    17201820
    1721                 $params  = array(
     1821                $params = array(
    17221822                        'author_name'  => 'Homer Jay Simpson',
    17231823                        'author_email' => 'chunkylover53@aol.com',
     
    17261826                        'author'       => self::$subscriber_id,
    17271827                );
     1828
    17281829                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
    17291830                $request->add_header( 'content-type', 'application/json' );
     
    17571858                wp_set_current_user( self::$subscriber_id );
    17581859
    1759                 $params  = array(
     1860                $params = array(
    17601861                        'post'         => self::$draft_id,
    17611862                        'author_name'  => 'Ishmael',
     
    17651866                        'author'       => self::$subscriber_id,
    17661867                );
    1767                 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
    1768                 $request->add_header( 'content-type', 'application/json' );
    1769                 $request->set_body( wp_json_encode( $params ) );
    1770 
    1771                 $response = rest_get_server()->dispatch( $request );
    1772 
     1868
     1869                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1870                $request->add_header( 'content-type', 'application/json' );
     1871                $request->set_body( wp_json_encode( $params ) );
     1872
     1873                $response = rest_get_server()->dispatch( $request );
    17731874                $this->assertErrorResponse( 'rest_comment_draft_post', $response, 403 );
    17741875        }
     
    17771878                wp_set_current_user( self::$subscriber_id );
    17781879
    1779                 $params  = array(
     1880                $params = array(
    17801881                        'post'         => self::$trash_id,
    17811882                        'author_name'  => 'Ishmael',
     
    17851886                        'author'       => self::$subscriber_id,
    17861887                );
     1888
    17871889                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
    17881890                $request->add_header( 'content-type', 'application/json' );
     
    17971899                wp_set_current_user( self::$subscriber_id );
    17981900
    1799                 $params  = array(
     1901                $params = array(
    18001902                        'post'         => self::$private_id,
    18011903                        'author_name'  => 'Homer Jay Simpson',
     
    18051907                        'author'       => self::$subscriber_id,
    18061908                );
    1807                 $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
    1808                 $request->add_header( 'content-type', 'application/json' );
    1809                 $request->set_body( wp_json_encode( $params ) );
    1810 
    1811                 $response = rest_get_server()->dispatch( $request );
    1812 
     1909
     1910                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1911                $request->add_header( 'content-type', 'application/json' );
     1912                $request->set_body( wp_json_encode( $params ) );
     1913
     1914                $response = rest_get_server()->dispatch( $request );
    18131915                $this->assertErrorResponse( 'rest_cannot_read_post', $response, 403 );
    18141916        }
     
    18171919                wp_set_current_user( self::$subscriber_id );
    18181920
    1819                 $params  = array(
     1921                $params = array(
    18201922                        'post'         => self::$password_id,
    18211923                        'author_name'  => 'Homer Jay Simpson',
     
    18251927                        'author'       => self::$subscriber_id,
    18261928                );
     1929
    18271930                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
    18281931                $request->add_header( 'content-type', 'application/json' );
     
    18351938        public function test_create_item_duplicate() {
    18361939                wp_set_current_user( self::$subscriber_id );
     1940
    18371941                $this->factory->comment->create(
    18381942                        array(
     
    18651969                        )
    18661970                );
     1971
    18671972                wp_set_current_user( self::$subscriber_id );
    18681973
     
    18811986        public function test_create_comment_require_login() {
    18821987                wp_set_current_user( 0 );
     1988
    18831989                update_option( 'comment_registration', 1 );
    18841990                add_filter( 'rest_allow_anonymous_comments', '__return_true' );
     1991
    18851992                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
    18861993                $request->set_param( 'post', self::$post_id );
     
    19982105                wp_set_current_user( self::$subscriber_id );
    19992106
    2000                 $params  = array(
     2107                $params = array(
    20012108                        'post'         => self::$post_id,
    20022109                        'author_name'  => rand_long_str( 246 ),
     
    20062113                        'date'         => '1995-04-30T10:22:00',
    20072114                );
     2115
    20082116                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
    20092117
     
    20212129                wp_set_current_user( self::$subscriber_id );
    20222130
    2023                 $params  = array(
     2131                $params = array(
    20242132                        'post'         => self::$post_id,
    20252133                        'author_name'  => 'Bleeding Gums Murphy',
     
    20292137                        'date'         => '1995-04-30T10:22:00',
    20302138                );
     2139
    20312140                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
    20322141
     
    20442153                wp_set_current_user( self::$subscriber_id );
    20452154
    2046                 $params  = array(
     2155                $params = array(
    20472156                        'post'         => self::$post_id,
    20482157                        'author_name'  => 'Bleeding Gums Murphy',
     
    20522161                        'date'         => '1995-04-30T10:22:00',
    20532162                );
     2163
    20542164                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
    20552165
     
    20672177                wp_set_current_user( self::$subscriber_id );
    20682178
    2069                 $params  = array(
     2179                $params = array(
    20702180                        'post'         => self::$post_id,
    20712181                        'author_name'  => 'Bleeding Gums Murphy',
     
    20752185                        'date'         => '1995-04-30T10:22:00',
    20762186                );
     2187
    20772188                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
    20782189
     
    20872198                wp_set_current_user( self::$subscriber_id );
    20882199
    2089                 $params  = array(
     2200                $params = array(
    20902201                        'post'         => self::$password_id,
    20912202                        'author_name'  => 'Bleeding Gums Murphy',
     
    20942205                        'content'      => 'This isn\'t a saxophone. It\'s an umbrella.',
    20952206                );
     2207
    20962208                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
    20972209
     
    21062218                add_filter( 'rest_allow_anonymous_comments', '__return_true' );
    21072219
    2108                 $params  = array(
     2220                $params = array(
    21092221                        'post'         => self::$password_id,
    21102222                        'author_name'  => 'Bleeding Gums Murphy',
     
    21142226                        'password'     => 'toomanysecrets',
    21152227                );
     2228
    21162229                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
    21172230
     
    21272240                wp_set_current_user( self::$admin_id );
    21282241
    2129                 $params  = array(
     2242                $params = array(
    21302243                        'author'       => self::$subscriber_id,
    21312244                        'author_name'  => 'Disco Stu',
     
    21372250                        'post'         => $post_id,
    21382251                );
     2252
    21392253                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    21402254                $request->add_header( 'content-type', 'application/json' );
     
    21632277        public function test_update_comment_date( $params, $results ) {
    21642278                wp_set_current_user( self::$editor_id );
     2279
    21652280                update_option( 'timezone_string', $params['timezone_string'] );
    21662281
     
    22142329
    22152330                wp_set_current_user( self::$admin_id );
     2331
    22162332                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    22172333                $request->set_param( 'post', $comment->comment_post_ID );
     
    22362352                );
    22372353
    2238                 $params  = array(
     2354                $params = array(
    22392355                        'status' => 'approve',
    22402356                );
     2357
    22412358                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_id ) );
    22422359                $request->add_header( 'content-type', 'application/json' );
     
    22632380                );
    22642381
    2265                 $params  = array(
     2382                $params = array(
    22662383                        'status' => 'approve',
    22672384                );
     2385
    22682386                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_id ) );
    22692387                $request->add_header( 'content-type', 'application/json' );
     
    22832401                wp_set_current_user( self::$admin_id );
    22842402
    2285                 $params  = array(
     2403                $params = array(
    22862404                        'date_gmt' => '2015-05-07T10:14:25',
    22872405                        'content'  => 'I\'ll be deep in the cold, cold ground before I recognize Missouri.',
    22882406                );
     2407
    22892408                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    22902409                $request->add_header( 'content-type', 'application/json' );
     
    23022421        public function test_update_comment_author_email_only() {
    23032422                wp_set_current_user( self::$editor_id );
     2423
    23042424                update_option( 'require_name_email', 1 );
    23052425
     
    23202440        public function test_update_comment_empty_author_name() {
    23212441                wp_set_current_user( self::$editor_id );
     2442
    23222443                update_option( 'require_name_email', 1 );
    23232444
     
    23392460        public function test_update_comment_author_name_only() {
    23402461                wp_set_current_user( self::$admin_id );
     2462
    23412463                update_option( 'require_name_email', 1 );
    23422464
     
    23572479        public function test_update_comment_empty_author_email() {
    23582480                wp_set_current_user( self::$admin_id );
     2481
    23592482                update_option( 'require_name_email', 1 );
    23602483
     
    23972520                wp_set_current_user( self::$admin_id );
    23982521
    2399                 $params  = array(
     2522                $params = array(
    24002523                        'type' => 'trackback',
    24012524                );
     2525
    24022526                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    24032527                $request->add_header( 'content-type', 'application/json' );
     
    24112535                wp_set_current_user( self::$admin_id );
    24122536
    2413                 $params  = array(
     2537                $params = array(
    24142538                        'content' => array(
    24152539                                'raw' => 'What the heck kind of name is Persephone?',
    24162540                        ),
    24172541                );
     2542
    24182543                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    24192544                $request->add_header( 'content-type', 'application/json' );
     
    24642589                wp_set_current_user( self::$subscriber_id );
    24652590
    2466                 $params  = array(
     2591                $params = array(
    24672592                        'content' => 'Oh, they have the internet on computers now!',
    24682593                );
     2594
    24692595                $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    24702596                $request->add_header( 'content-type', 'application/json' );
     
    24882614                add_filter( 'rest_allow_anonymous_comments', '__return_true' );
    24892615
    2490                 $params  = array(
     2616                $params = array(
    24912617                        'content' => 'Disco Stu likes disco music.',
    24922618                );
     2619
    24932620                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$hold_id ) );
    24942621                $request->add_header( 'content-type', 'application/json' );
     
    25052632                wp_set_current_user( self::$moderator_id );
    25062633
    2507                 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    2508                 $params  = array(
     2634                $params = array(
    25092635                        'content' => 'Updated comment.',
    25102636                        'date'    => '2019-10-07T23:14:25',
    25112637                );
     2638
     2639                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    25122640                $request->add_header( 'content-type', 'application/json' );
    25132641                $request->set_body( wp_json_encode( $params ) );
     
    25352663                wp_set_current_user( self::$subscriber_id );
    25362664
    2537                 $params  = array(
     2665                $params = array(
    25382666                        'content' => 'Disco Stu likes disco music.',
    25392667                );
     2668
    25402669                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $private_comment_id ) );
    25412670                $request->add_header( 'content-type', 'application/json' );
     
    25482677        public function test_update_comment_with_children_link() {
    25492678                wp_set_current_user( self::$admin_id );
     2679
    25502680                $comment_id_1 = $this->factory->comment->create(
    25512681                        array(
     
    25902720                wp_set_current_user( self::$admin_id );
    25912721
    2592                 $params  = array(
     2722                $params = array(
    25932723                        'author_name' => rand_long_str( 246 ),
    25942724                        'content'     => 'This isn\'t a saxophone. It\'s an umbrella.',
    25952725                );
     2726
    25962727                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    25972728
     
    26092740                wp_set_current_user( self::$admin_id );
    26102741
    2611                 $params  = array(
     2742                $params = array(
    26122743                        'author_email' => 'murphy@' . rand_long_str( 190 ) . '.com',
    26132744                        'content'      => 'This isn\'t a saxophone. It\'s an umbrella.',
    26142745                );
     2746
    26152747                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    26162748
     
    26282760                wp_set_current_user( self::$admin_id );
    26292761
    2630                 $params  = array(
     2762                $params = array(
    26312763                        'author_url' => 'http://jazz.' . rand_long_str( 185 ) . '.com',
    26322764                        'content'    => 'This isn\'t a saxophone. It\'s an umbrella.',
    26332765                );
     2766
    26342767                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    26352768
     
    26472780                wp_set_current_user( self::$admin_id );
    26482781
    2649                 $params  = array(
     2782                $params = array(
    26502783                        'content' => rand_long_str( 66525 ),
    26512784                );
     2785
    26522786                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    26532787
     
    27122846        public function test_comment_roundtrip_as_editor() {
    27132847                wp_set_current_user( self::$editor_id );
     2848
    27142849                $this->assertEquals( ! is_multisite(), current_user_can( 'unfiltered_html' ) );
    27152850                $this->verify_comment_roundtrip(
     
    27322867        public function test_comment_roundtrip_as_editor_unfiltered_html() {
    27332868                wp_set_current_user( self::$editor_id );
     2869
    27342870                if ( is_multisite() ) {
    27352871                        $this->assertFalse( current_user_can( 'unfiltered_html' ) );
     
    27712907        public function test_comment_roundtrip_as_superadmin() {
    27722908                wp_set_current_user( self::$superadmin_id );
     2909
    27732910                $this->assertTrue( current_user_can( 'unfiltered_html' ) );
    27742911                $this->verify_comment_roundtrip(
     
    27912928        public function test_comment_roundtrip_as_superadmin_unfiltered_html() {
    27922929                wp_set_current_user( self::$superadmin_id );
     2930
    27932931                $this->assertTrue( current_user_can( 'unfiltered_html' ) );
    27942932                $this->verify_comment_roundtrip(
     
    28322970                wp_set_current_user( self::$admin_id );
    28332971
    2834                 $comment_id       = $this->factory->comment->create(
     2972                $comment_id = $this->factory->comment->create(
    28352973                        array(
    28362974                                'comment_approved' => 1,
     
    28392977                        )
    28402978                );
     2979
    28412980                $request          = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
    28422981                $request['force'] = true;
     
    28592998                        )
    28602999                );
    2861                 $request    = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
    2862                 $response   = rest_get_server()->dispatch( $request );
     3000
     3001                $request  = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
     3002                $response = rest_get_server()->dispatch( $request );
    28633003                $this->assertEquals( 200, $response->get_status() );
    28643004                $data     = $response->get_data();
     
    28703010                wp_set_current_user( self::$admin_id );
    28713011
    2872                 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) );
    2873 
     3012                $request  = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) );
    28743013                $response = rest_get_server()->dispatch( $request );
    28753014                $this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 );
     
    28793018                wp_set_current_user( self::$subscriber_id );
    28803019
    2881                 $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    2882 
     3020                $request  = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
    28833021                $response = rest_get_server()->dispatch( $request );
    28843022                $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
     
    28873025        public function test_delete_child_comment_link() {
    28883026                wp_set_current_user( self::$admin_id );
     3027
    28893028                $comment_id_1 = $this->factory->comment->create(
    28903029                        array(
     
    29483087        public function test_get_item_schema_show_avatar() {
    29493088                update_option( 'show_avatars', false );
     3089
    29503090                $request    = new WP_REST_Request( 'OPTIONS', '/wp/v2/users' );
    29513091                $response   = rest_get_server()->dispatch( $request );
     
    29753115                );
    29763116
    2977                 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' );
    2978 
     3117                $request  = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' );
    29793118                $response = rest_get_server()->dispatch( $request );
    29803119                $data     = $response->get_data();
     
    29833122                $this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] );
    29843123
    2985                 $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . self::$approved_id );
    2986 
     3124                $request  = new WP_REST_Request( 'GET', '/wp/v2/comments/' . self::$approved_id );
    29873125                $response = rest_get_server()->dispatch( $request );
    29883126                $this->assertArrayHasKey( 'my_custom_int', $response->data );
  • trunk/tests/phpunit/tests/rest-api/rest-pages-controller.php

    r46586 r46657  
    126126                        )
    127127                );
    128                 // No parent
     128
     129                // No parent.
    129130                $request  = new WP_REST_Request( 'GET', '/wp/v2/pages' );
    130131                $response = rest_get_server()->dispatch( $request );
    131132                $data     = $response->get_data();
    132133                $this->assertEquals( 2, count( $data ) );
    133                 // Filter to parent
     134
     135                // Filter to parent.
    134136                $request->set_param( 'parent', $id1 );
    135137                $response = rest_get_server()->dispatch( $request );
     
    137139                $this->assertEquals( 1, count( $data ) );
    138140                $this->assertEquals( $id2, $data[0]['id'] );
    139                 // Invalid parent should fail
     141
     142                // Invalid 'parent' should error.
    140143                $request->set_param( 'parent', 'some-slug' );
    141144                $response = rest_get_server()->dispatch( $request );
     
    170173                        )
    171174                );
    172                 // No parent
     175
     176                // No parent.
    173177                $request  = new WP_REST_Request( 'GET', '/wp/v2/pages' );
    174178                $response = rest_get_server()->dispatch( $request );
    175179                $data     = $response->get_data();
    176180                $this->assertEquals( 4, count( $data ) );
    177                 // Filter to parents
     181
     182                // Filter to parents.
    178183                $request->set_param( 'parent', array( $id1, $id3 ) );
    179184                $response = rest_get_server()->dispatch( $request );
     
    197202                        )
    198203                );
    199                 // No parent
     204
     205                // No parent.
    200206                $request  = new WP_REST_Request( 'GET', '/wp/v2/pages' );
    201207                $response = rest_get_server()->dispatch( $request );
    202208                $data     = $response->get_data();
    203209                $this->assertEquals( 2, count( $data ) );
    204                 // Filter to parent
     210
     211                // Filter to parent.
    205212                $request->set_param( 'parent_exclude', $id1 );
    206213                $response = rest_get_server()->dispatch( $request );
     
    208215                $this->assertEquals( 1, count( $data ) );
    209216                $this->assertEquals( $id1, $data[0]['id'] );
    210                 // Invalid parent_exclude should error
     217
     218                // Invalid 'parent_exclude' should error.
    211219                $request->set_param( 'parent_exclude', 'some-slug' );
    212220                $response = rest_get_server()->dispatch( $request );
     
    242250                        )
    243251                );
    244                 // No parent
     252
     253                // No parent.
    245254                $request  = new WP_REST_Request( 'GET', '/wp/v2/pages' );
    246255                $response = rest_get_server()->dispatch( $request );
    247256                $data     = $response->get_data();
    248257                $this->assertEqualSets( array( $id1, $id2, $id3, $id4 ), wp_list_pluck( $data, 'id' ) );
    249                 // Filter to menu_order
     258
     259                // Filter to 'menu_order'.
    250260                $request->set_param( 'menu_order', 1 );
    251261                $response = rest_get_server()->dispatch( $request );
    252262                $data     = $response->get_data();
    253263                $this->assertEqualSets( array( $id4 ), wp_list_pluck( $data, 'id' ) );
    254                 // Order by menu order
     264
     265                // Order by 'menu order'.
    255266                $request = new WP_REST_Request( 'GET', '/wp/v2/pages' );
    256267                $request->set_param( 'order', 'asc' );
     
    262273                $this->assertEquals( $id2, $data[2]['id'] );
    263274                $this->assertEquals( $id3, $data[3]['id'] );
    264                 // Invalid menu_order should fail
     275
     276                // Invalid 'menu_order' should error.
    265277                $request = new WP_REST_Request( 'GET', '/wp/v2/pages' );
    266278                $request->set_param( 'menu_order', 'top-first' );
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r46648 r46657  
    2020
    2121        protected static $supported_formats;
     22        protected static $post_ids    = array();
     23        protected static $total_posts = 30;
     24        protected static $per_page    = 50;
    2225
    2326        protected $forbidden_cat;
     
    6265                self::$supported_formats = get_theme_support( 'post-formats' );
    6366                add_theme_support( 'post-formats', array( 'post', 'gallery' ) );
     67
     68                // Set up posts for pagination tests.
     69                for ( $i = 0; $i < self::$total_posts - 1; $i++ ) {
     70                        self::$post_ids[] = $factory->post->create(
     71                                array(
     72                                        'post_title' => "Post {$i}",
     73                                )
     74                        );
     75                }
    6476        }
    6577
     
    7082                } else {
    7183                        remove_theme_support( 'post-formats' );
     84                }
     85
     86                // Remove posts for pagination tests.
     87                foreach ( self::$post_ids as $post_id ) {
     88                        wp_delete_post( $post_id, true );
    7289                }
    7390
     
    204221
    205222                wp_set_current_user( self::$editor_id );
     223
    206224                $request  = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    207225                $response = rest_get_server()->dispatch( $request );
     
    241259                $this->factory->post->create( array( 'post_author' => self::$editor_id ) );
    242260                $this->factory->post->create( array( 'post_author' => self::$author_id ) );
    243                 // All 3 posts
    244                 $request  = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     261
     262                $total_posts = self::$total_posts + 2;
     263
     264                // All posts in the database.
     265                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     266                $request->set_param( 'per_page', self::$per_page );
    245267                $response = rest_get_server()->dispatch( $request );
    246268                $this->assertEquals( 200, $response->get_status() );
    247                 $this->assertEquals( 3, count( $response->get_data() ) );
    248                 // 2 of 3 posts
     269                $this->assertEquals( $total_posts, count( $response->get_data() ) );
     270
     271                // Limit to editor and author.
    249272                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    250273                $request->set_param( 'author', array( self::$editor_id, self::$author_id ) );
     
    254277                $this->assertEquals( 2, count( $data ) );
    255278                $this->assertEqualSets( array( self::$editor_id, self::$author_id ), wp_list_pluck( $data, 'author' ) );
    256                 // 1 of 3 posts
     279
     280                // Limit to editor.
    257281                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    258282                $request->set_param( 'author', self::$editor_id );
     
    267291                $this->factory->post->create( array( 'post_author' => self::$editor_id ) );
    268292                $this->factory->post->create( array( 'post_author' => self::$author_id ) );
    269                 // All 3 posts
    270                 $request  = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     293
     294                $total_posts = self::$total_posts + 2;
     295
     296                // All posts in the database.
     297                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     298                $request->set_param( 'per_page', self::$per_page );
    271299                $response = rest_get_server()->dispatch( $request );
    272300                $this->assertEquals( 200, $response->get_status() );
    273                 $this->assertEquals( 3, count( $response->get_data() ) );
    274                 // 1 of 3 posts
    275                 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     301                $this->assertEquals( $total_posts, count( $response->get_data() ) );
     302
     303                // Exclude editor and author.
     304                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     305                $request->set_param( 'per_page', self::$per_page );
    276306                $request->set_param( 'author_exclude', array( self::$editor_id, self::$author_id ) );
    277307                $response = rest_get_server()->dispatch( $request );
    278308                $this->assertEquals( 200, $response->get_status() );
    279309                $data = $response->get_data();
    280                 $this->assertEquals( 1, count( $data ) );
     310                $this->assertEquals( $total_posts - 2, count( $data ) );
    281311                $this->assertNotEquals( self::$editor_id, $data[0]['author'] );
    282312                $this->assertNotEquals( self::$author_id, $data[0]['author'] );
    283                 // 2 of 3 posts
    284                 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     313
     314                // Exclude editor.
     315                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     316                $request->set_param( 'per_page', self::$per_page );
    285317                $request->set_param( 'author_exclude', self::$editor_id );
    286318                $response = rest_get_server()->dispatch( $request );
    287319                $this->assertEquals( 200, $response->get_status() );
    288320                $data = $response->get_data();
    289                 $this->assertEquals( 2, count( $data ) );
     321                $this->assertEquals( $total_posts - 1, count( $data ) );
    290322                $this->assertNotEquals( self::$editor_id, $data[0]['author'] );
    291323                $this->assertNotEquals( self::$editor_id, $data[1]['author'] );
    292                 // invalid author_exclude errors
     324
     325                // Invalid 'author_exclude' should error.
    293326                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    294327                $request->set_param( 'author_exclude', 'invalid' );
     
    299332        public function test_get_items_include_query() {
    300333                $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    301                 $this->factory->post->create( array( 'post_status' => 'publish' ) );
    302                 $id3     = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    303                 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    304                 // Orderby=>desc
    305                 $request->set_param( 'include', array( $id1, $id3 ) );
     334                $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     335
     336                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     337
     338                // Order defaults to 'desc'.
     339                $request->set_param( 'include', array( $id1, $id2 ) );
    306340                $response = rest_get_server()->dispatch( $request );
    307341                $data     = $response->get_data();
    308342                $this->assertEquals( 2, count( $data ) );
    309                 $this->assertEquals( $id3, $data[0]['id'] );
     343                $this->assertEquals( $id2, $data[0]['id'] );
    310344                $this->assertPostsOrderedBy( '{posts}.post_date DESC' );
    311                 // Orderby=>include
     345
     346                // 'orderby' => 'include'
    312347                $request->set_param( 'orderby', 'include' );
    313348                $response = rest_get_server()->dispatch( $request );
     
    315350                $this->assertEquals( 2, count( $data ) );
    316351                $this->assertEquals( $id1, $data[0]['id'] );
    317                 $this->assertPostsOrderedBy( "FIELD({posts}.ID,$id1,$id3)" );
    318                 // Invalid include should error
     352                $this->assertPostsOrderedBy( "FIELD({posts}.ID,$id1,$id2)" );
     353
     354                // Invalid 'include' should error.
    319355                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    320356                $request->set_param( 'include', 'invalid' );
     
    421457
    422458        public function test_get_items_exclude_query() {
    423                 $id1      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    424                 $id2      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     459                $id1 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     460                $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     461
    425462                $request  = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    426463                $response = rest_get_server()->dispatch( $request );
    427464                $data     = $response->get_data();
    428                 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
    429                 $this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
     465                $ids      = wp_list_pluck( $data, 'id' );
     466                $this->assertTrue( in_array( $id1, $ids, true ) );
     467                $this->assertTrue( in_array( $id2, $ids, true ) );
    430468
    431469                $request->set_param( 'exclude', array( $id2 ) );
    432470                $response = rest_get_server()->dispatch( $request );
    433471                $data     = $response->get_data();
    434                 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
    435                 $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
     472                $ids      = wp_list_pluck( $data, 'id' );
     473                $this->assertTrue( in_array( $id1, $ids, true ) );
     474                $this->assertFalse( in_array( $id2, $ids, true ) );
    436475
    437476                $request->set_param( 'exclude', "$id2" );
    438477                $response = rest_get_server()->dispatch( $request );
    439478                $data     = $response->get_data();
    440                 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
    441                 $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
     479                $ids      = wp_list_pluck( $data, 'id' );
     480                $this->assertTrue( in_array( $id1, $ids, true ) );
     481                $this->assertFalse( in_array( $id2, $ids, true ) );
    442482
    443483                $request->set_param( 'exclude', 'invalid' );
     
    447487
    448488        public function test_get_items_search_query() {
    449                 for ( $i = 0;  $i < 5;  $i++ ) {
    450                         $this->factory->post->create( array( 'post_status' => 'publish' ) );
    451                 }
    452489                $this->factory->post->create(
    453490                        array(
     
    456493                        )
    457494                );
    458                 $request  = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    459                 $response = rest_get_server()->dispatch( $request );
    460                 $this->assertEquals( 7, count( $response->get_data() ) );
     495                $total_posts = self::$total_posts + 1;
     496
     497                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     498                $request->set_param( 'per_page', self::$per_page );
     499                $response = rest_get_server()->dispatch( $request );
     500                $this->assertEquals( $total_posts, count( $response->get_data() ) );
     501
    461502                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    462503                $request->set_param( 'search', 'Search Result' );
     
    480521                        )
    481522                );
     523
    482524                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    483525                $request->set_param( 'slug', 'apple' );
     
    508550                        )
    509551                );
     552
    510553                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    511554                $request->set_param( 'slug', array( 'banana', 'peach' ) );
     
    541584                        )
    542585                );
     586
    543587                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    544588                $request->set_param( 'slug', 'apple,banana' );
     
    557601        public function test_get_items_status_query() {
    558602                wp_set_current_user( 0 );
     603
    559604                $this->factory->post->create( array( 'post_status' => 'draft' ) );
    560                 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     605
     606                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     607                $request->set_param( 'per_page', self::$per_page );
    561608                $request->set_param( 'status', 'publish' );
    562609                $response = rest_get_server()->dispatch( $request );
    563610                $this->assertEquals( 200, $response->get_status() );
    564                 $this->assertEquals( 1, count( $response->get_data() ) );
     611                $this->assertEquals( self::$total_posts, count( $response->get_data() ) );
     612
    565613                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    566614                $request->set_param( 'status', 'draft' );
    567615                $response = rest_get_server()->dispatch( $request );
    568616                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    569                 wp_set_current_user( self::$editor_id );
     617
     618                wp_set_current_user( self::$editor_id );
     619
    570620                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    571621                $request->set_param( 'status', 'draft' );
     
    636686
    637687                wp_set_current_user( self::$private_reader_id );
     688
    638689                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    639690                $request->set_param( 'status', array( 'private', 'future' ) );
     
    645696        public function test_get_items_invalid_status_query() {
    646697                wp_set_current_user( 0 );
     698
    647699                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    648700                $request->set_param( 'status', 'invalid' );
     
    657709                        )
    658710                );
     711
    659712                wp_set_current_user( 0 );
    660713
     
    695748                        )
    696749                );
     750
    697751                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    698752                $request->set_param( 'search', 'Apple' );
    699                 // order defaults to 'desc'
     753
     754                // Order defaults to 'desc'.
    700755                $request->set_param( 'orderby', 'title' );
    701756                $response = rest_get_server()->dispatch( $request );
     
    703758                $this->assertEquals( 'Apple Sauce', $data[0]['title']['rendered'] );
    704759                $this->assertPostsOrderedBy( '{posts}.post_title DESC' );
    705                 // order=>asc
     760
     761                // 'order' => 'asc'.
    706762                $request->set_param( 'order', 'asc' );
    707763                $response = rest_get_server()->dispatch( $request );
     
    709765                $this->assertEquals( 'Apple Cobbler', $data[0]['title']['rendered'] );
    710766                $this->assertPostsOrderedBy( '{posts}.post_title ASC' );
    711                 // order=>asc,id should fail
     767
     768                // 'order' => 'asc,id' should error.
    712769                $request->set_param( 'order', 'asc,id' );
    713770                $response = rest_get_server()->dispatch( $request );
    714771                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    715                 // orderby=>content should fail (invalid param test)
     772
     773                // 'orderby' => 'content' should error (invalid param test).
    716774                $request->set_param( 'order', 'asc' );
    717775                $request->set_param( 'orderby', 'content' );
     
    722780        public function test_get_items_with_orderby_include_without_include_param() {
    723781                $this->factory->post->create( array( 'post_status' => 'publish' ) );
     782
    724783                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    725784                $request->set_param( 'orderby', 'include' );
     
    818877
    819878        public function test_get_items_with_orderby_relevance() {
    820                 $id1     = $this->factory->post->create(
     879                $id1 = $this->factory->post->create(
    821880                        array(
    822881                                'post_title'   => 'Title is more relevant',
     
    825884                        )
    826885                );
    827                 $id2     = $this->factory->post->create(
     886                $id2 = $this->factory->post->create(
    828887                        array(
    829888                                'post_title'   => 'Title is',
     
    832891                        )
    833892                );
     893
    834894                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    835895                $request->set_param( 'orderby', 'relevance' );
     
    845905
    846906        public function test_get_items_with_orderby_relevance_two_terms() {
    847                 $id1     = $this->factory->post->create(
     907                $id1 = $this->factory->post->create(
    848908                        array(
    849909                                'post_title'   => 'Title is more relevant',
     
    852912                        )
    853913                );
    854                 $id2     = $this->factory->post->create(
     914                $id2 = $this->factory->post->create(
    855915                        array(
    856916                                'post_title'   => 'Title is',
     
    859919                        )
    860920                );
     921
    861922                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    862923                $request->set_param( 'orderby', 'relevance' );
     
    879940
    880941        public function test_get_items_offset_query() {
    881                 $id1     = self::$post_id;
    882                 $id2     = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    883                 $id3     = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    884                 $id4     = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    885                 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     942                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     943                $request->set_param( 'per_page', self::$per_page );
    886944                $request->set_param( 'offset', 1 );
    887945                $response = rest_get_server()->dispatch( $request );
    888                 $this->assertCount( 3, $response->get_data() );
    889                 // 'offset' works with 'per_page'
     946                $this->assertCount( self::$total_posts - 1, $response->get_data() );
     947
     948                // 'offset' works with 'per_page'.
    890949                $request->set_param( 'per_page', 2 );
    891950                $response = rest_get_server()->dispatch( $request );
    892951                $this->assertCount( 2, $response->get_data() );
    893                 // 'offset' takes priority over 'page'
     952
     953                // 'offset' takes priority over 'page'.
    894954                $request->set_param( 'page', 2 );
    895955                $response = rest_get_server()->dispatch( $request );
    896956                $this->assertCount( 2, $response->get_data() );
    897                 // Invalid 'offset' should error
     957
     958                // Invalid 'offset' should error.
    898959                $request->set_param( 'offset', 'moreplease' );
    899960                $response = rest_get_server()->dispatch( $request );
     
    903964        public function test_get_items_tags_query() {
    904965                $id1 = self::$post_id;
    905                 $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    906                 $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    907                 $id4 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    908966                $tag = wp_insert_term( 'My Tag', 'post_tag' );
    909967
    910968                wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' );
     969
    911970                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    912971                $request->set_param( 'tags', array( $tag['term_id'] ) );
     
    925984                $tag = wp_insert_term( 'My Tag', 'post_tag' );
    926985
     986                $total_posts = self::$total_posts + 3;
     987
    927988                wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' );
    928                 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     989
     990                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     991                $request->set_param( 'per_page', self::$per_page );
    929992                $request->set_param( 'tags_exclude', array( $tag['term_id'] ) );
    930993
    931994                $response = rest_get_server()->dispatch( $request );
    932995                $data     = $response->get_data();
    933                 $this->assertCount( 3, $data );
     996                $this->assertCount( $total_posts - 1, $data );
    934997                $this->assertEquals( $id4, $data[0]['id'] );
    935998                $this->assertEquals( $id3, $data[1]['id'] );
     
    9401003                $id1      = self::$post_id;
    9411004                $id2      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    942                 $id3      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    943                 $id4      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    9441005                $tag      = wp_insert_term( 'My Tag', 'post_tag' );
    9451006                $category = wp_insert_term( 'My Category', 'category' );
     
    9671028                $id1      = self::$post_id;
    9681029                $id2      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    969                 $id3      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    970                 $id4      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    9711030                $tag      = wp_insert_term( 'My Tag', 'post_tag' );
    9721031                $category = wp_insert_term( 'My Category', 'category' );
     
    9901049        public function test_get_items_tags_and_categories_exclude_query() {
    9911050                $id1      = self::$post_id;
     1051                $id2      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     1052                $tag      = wp_insert_term( 'My Tag', 'post_tag' );
     1053                $category = wp_insert_term( 'My Category', 'category' );
     1054
     1055                wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' );
     1056                wp_set_object_terms( $id2, array( $tag['term_id'] ), 'post_tag' );
     1057                wp_set_object_terms( $id1, array( $category['term_id'] ), 'category' );
     1058
     1059                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     1060                $request->set_param( 'tags', array( $tag['term_id'] ) );
     1061                $request->set_param( 'categories_exclude', array( $category['term_id'] ) );
     1062
     1063                $response = rest_get_server()->dispatch( $request );
     1064                $data     = $response->get_data();
     1065                $this->assertCount( 1, $data );
     1066                $this->assertEquals( $id2, $data[0]['id'] );
     1067
     1068                $request->set_param( 'tags_exclude', array( 'my-tag' ) );
     1069                $response = rest_get_server()->dispatch( $request );
     1070                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     1071        }
     1072
     1073        /**
     1074         * @ticket 44326
     1075         */
     1076        public function test_get_items_tags_or_categories_exclude_query() {
     1077                $id1      = end( self::$post_ids );
    9921078                $id2      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    9931079                $id3      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     
    9961082                $category = wp_insert_term( 'My Category', 'category' );
    9971083
    998                 wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' );
    999                 wp_set_object_terms( $id2, array( $tag['term_id'] ), 'post_tag' );
    1000                 wp_set_object_terms( $id1, array( $category['term_id'] ), 'category' );
    1001 
    1002                 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    1003                 $request->set_param( 'tags', array( $tag['term_id'] ) );
    1004                 $request->set_param( 'categories_exclude', array( $category['term_id'] ) );
    1005 
    1006                 $response = rest_get_server()->dispatch( $request );
    1007                 $data     = $response->get_data();
    1008                 $this->assertCount( 1, $data );
    1009                 $this->assertEquals( $id2, $data[0]['id'] );
    1010 
    1011                 $request->set_param( 'tags_exclude', array( 'my-tag' ) );
    1012                 $response = rest_get_server()->dispatch( $request );
    1013                 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    1014         }
    1015 
    1016         /**
    1017          * @ticket 44326
    1018          */
    1019         public function test_get_items_tags_or_categories_exclude_query() {
    1020                 $id1      = self::$post_id;
    1021                 $id2      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    1022                 $id3      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    1023                 $id4      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    1024                 $tag      = wp_insert_term( 'My Tag', 'post_tag' );
    1025                 $category = wp_insert_term( 'My Category', 'category' );
     1084                $total_posts = self::$total_posts + 3;
    10261085
    10271086                wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' );
     
    10311090
    10321091                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     1092                $request->set_param( 'per_page', self::$per_page );
    10331093                $request->set_param( 'tags', array( $tag['term_id'] ) );
    10341094                $request->set_param( 'categories_exclude', array( $category['term_id'] ) );
     
    10381098                $response = rest_get_server()->dispatch( $request );
    10391099                $data     = $response->get_data();
    1040                 $this->assertCount( 3, $data );
     1100                $this->assertCount( $total_posts - 1, $data );
    10411101                $this->assertEquals( $id4, $data[0]['id'] );
    10421102                $this->assertEquals( $id2, $data[1]['id'] );
     
    10821142                $id1 = self::$post_id;
    10831143                $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    1084                 $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    10851144
    10861145                update_option( 'sticky_posts', array( $id2 ) );
     
    11171176
    11181177        public function test_get_items_sticky_no_sticky_posts() {
    1119                 $id1 = self::$post_id;
    1120 
    11211178                update_option( 'sticky_posts', array() );
    11221179
     
    11541211
    11551212        public function test_get_items_not_sticky() {
    1156                 $id1 = self::$post_id;
     1213                $id1 = end( self::$post_ids );
    11571214                $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    11581215
     1216                $total_posts = self::$total_posts + 1;
     1217
    11591218                update_option( 'sticky_posts', array( $id2 ) );
    11601219
    11611220                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     1221                $request->set_param( 'per_page', self::$per_page );
    11621222                $request->set_param( 'sticky', false );
    11631223
    11641224                $response = rest_get_server()->dispatch( $request );
    1165                 $this->assertCount( 1, $response->get_data() );
     1225                $this->assertCount( $total_posts - 1, $response->get_data() );
    11661226
    11671227                $posts = $response->get_data();
     
    11731233
    11741234        public function test_get_items_not_sticky_with_exclude() {
    1175                 $id1 = self::$post_id;
     1235                $id1 = end( self::$post_ids );
    11761236                $id2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    11771237                $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    11781238
     1239                $total_posts = self::$total_posts + 2;
     1240
    11791241                update_option( 'sticky_posts', array( $id2 ) );
    11801242
    11811243                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     1244                $request->set_param( 'per_page', self::$per_page );
    11821245                $request->set_param( 'sticky', false );
    11831246                $request->set_param( 'exclude', array( $id3 ) );
    11841247
    11851248                $response = rest_get_server()->dispatch( $request );
    1186                 $this->assertCount( 1, $response->get_data() );
     1249                $this->assertCount( $total_posts - 2, $response->get_data() );
    11871250
    11881251                $posts = $response->get_data();
    1189                 $post  = $posts[0];
    1190                 $this->assertEquals( $id1, $post['id'] );
     1252                $ids   = wp_list_pluck( $posts, 'id' );
     1253                $this->assertTrue( in_array( $id1, $ids, true ) );
     1254                $this->assertFalse( in_array( $id2, $ids, true ) );
     1255                $this->assertFalse( in_array( $id3, $ids, true ) );
    11911256
    11921257                $this->assertPostsWhere( " AND {posts}.ID NOT IN ($id3,$id2) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" );
     
    11981263                $id3 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
    11991264
     1265                $total_posts = self::$total_posts + 2;
     1266
    12001267                update_option( 'sticky_posts', array() );
    12011268
    12021269                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     1270                $request->set_param( 'per_page', self::$per_page );
    12031271                $request->set_param( 'sticky', false );
    12041272                $request->set_param( 'exclude', array( $id3 ) );
    12051273
    12061274                $response = rest_get_server()->dispatch( $request );
    1207                 $this->assertCount( 2, $response->get_data() );
     1275                $this->assertCount( $total_posts - 1, $response->get_data() );
    12081276
    12091277                $posts = $response->get_data();
    12101278                $ids   = wp_list_pluck( $posts, 'id' );
    1211                 sort( $ids );
    1212                 $this->assertEquals( array( $id1, $id2 ), $ids );
     1279                $this->assertTrue( in_array( $id1, $ids, true ) );
     1280                $this->assertTrue( in_array( $id2, $ids, true ) );
     1281                $this->assertFalse( in_array( $id3, $ids, true ) );
    12131282
    12141283                $this->assertPostsWhere( " AND {posts}.ID NOT IN ($id3) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" );
     
    12161285
    12171286        public function test_get_items_pagination_headers() {
    1218                 // Start of the index
    1219                 for ( $i = 0; $i < 49; $i++ ) {
    1220                         $this->factory->post->create(
    1221                                 array(
    1222                                         'post_title' => "Post {$i}",
    1223                                 )
    1224                         );
    1225                 }
     1287                $total_posts = self::$total_posts;
     1288                $total_pages = (int) ceil( $total_posts / 10 );
     1289
     1290                // Start of the index.
    12261291                $request  = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    12271292                $response = rest_get_server()->dispatch( $request );
    12281293                $headers  = $response->get_headers();
    1229                 $this->assertEquals( 50, $headers['X-WP-Total'] );
    1230                 $this->assertEquals( 5, $headers['X-WP-TotalPages'] );
     1294                $this->assertEquals( $total_posts, $headers['X-WP-Total'] );
     1295                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    12311296                $next_link = add_query_arg(
    12321297                        array(
     
    12371302                $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
    12381303                $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    1239                 // 3rd page
    1240                 $this->factory->post->create(
    1241                         array(
    1242                                 'post_title' => 'Post 51',
    1243                         )
    1244                 );
     1304
     1305                // 3rd page.
     1306                $this->factory->post->create();
     1307                $total_posts++;
     1308                $total_pages++;
    12451309                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    12461310                $request->set_param( 'page', 3 );
    12471311                $response = rest_get_server()->dispatch( $request );
    12481312                $headers  = $response->get_headers();
    1249                 $this->assertEquals( 51, $headers['X-WP-Total'] );
    1250                 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
     1313                $this->assertEquals( $total_posts, $headers['X-WP-Total'] );
     1314                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    12511315                $prev_link = add_query_arg(
    12521316                        array(
     
    12631327                );
    12641328                $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    1265                 // Last page
    1266                 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    1267                 $request->set_param( 'page', 6 );
     1329
     1330                // Last page.
     1331                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     1332                $request->set_param( 'page', $total_pages );
    12681333                $response = rest_get_server()->dispatch( $request );
    12691334                $headers  = $response->get_headers();
    1270                 $this->assertEquals( 51, $headers['X-WP-Total'] );
    1271                 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
     1335                $this->assertEquals( $total_posts, $headers['X-WP-Total'] );
     1336                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    12721337                $prev_link = add_query_arg(
    12731338                        array(
    1274                                 'page' => 5,
     1339                                'page' => $total_pages - 1,
    12751340                        ),
    12761341                        rest_url( '/wp/v2/posts' )
     
    12791344                $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
    12801345
    1281                 // Out of bounds
    1282                 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    1283                 $request->set_param( 'page', 8 );
     1346                // Out of bounds.
     1347                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     1348                $request->set_param( 'page', 100 );
    12841349                $response = rest_get_server()->dispatch( $request );
    12851350                $headers  = $response->get_headers();
     
    12871352
    12881353                // With query params.
    1289                 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     1354                $total_pages = (int) ceil( $total_posts / 5 );
     1355                $request     = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    12901356                $request->set_query_params(
    12911357                        array(
     
    12961362                $response = rest_get_server()->dispatch( $request );
    12971363                $headers  = $response->get_headers();
    1298                 $this->assertEquals( 51, $headers['X-WP-Total'] );
    1299                 $this->assertEquals( 11, $headers['X-WP-TotalPages'] );
     1364                $this->assertEquals( $total_posts, $headers['X-WP-Total'] );
     1365                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    13001366                $prev_link = add_query_arg(
    13011367                        array(
     
    13211387                // Drafts status query var inaccessible to unauthorized users.
    13221388                wp_set_current_user( 0 );
     1389
    13231390                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    13241391                $request->set_param( 'status', 'draft' );
     
    13281395                // Users with 'read_private_posts' cap shouldn't also be able to view drafts.
    13291396                wp_set_current_user( self::$private_reader_id );
     1397
    13301398                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    13311399                $request->set_param( 'status', 'draft' );
     
    13351403                // But drafts are accessible to authorized users.
    13361404                wp_set_current_user( self::$editor_id );
    1337                 $response = rest_get_server()->dispatch( $request );
    1338                 $data     = $response->get_data();
    1339 
     1405
     1406                $response = rest_get_server()->dispatch( $request );
     1407                $data     = $response->get_data();
    13401408                $this->assertEquals( $draft_id, $data[0]['id'] );
    13411409        }
     
    13481416
    13491417                wp_set_current_user( 0 );
     1418
    13501419                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    13511420                $request->set_param( 'status', 'private' );
     
    13541423
    13551424                wp_set_current_user( self::$private_reader_id );
     1425
    13561426                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    13571427                $request->set_param( 'status', 'private' );
     
    15211591                        )
    15221592                );
     1593
    15231594                wp_set_current_user( 0 );
    15241595
     
    15531624        public function test_get_post_list_context_without_permission() {
    15541625                wp_set_current_user( 0 );
     1626
    15551627                $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    15561628                $request->set_query_params(
     
    15661638        public function test_get_post_context_without_permission() {
    15671639                wp_set_current_user( 0 );
     1640
    15681641                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    15691642                $request->set_query_params(
     
    16051678                );
    16061679
    1607                 $post    = get_post( $post_id );
     1680                $post = get_post( $post_id );
     1681
    16081682                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
    16091683                $request->set_param( 'password', '$inthebananastand' );
     
    16261700                );
    16271701
    1628                 $post    = get_post( $post_id );
     1702                $post = get_post( $post_id );
     1703
    16291704                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
    16301705                $request->set_param( 'password', 'wrongpassword' );
     
    16351710
    16361711        public function test_get_post_with_password_without_permission() {
    1637                 $post_id  = $this->factory->post->create(
     1712                $post_id = $this->factory->post->create(
    16381713                        array(
    16391714                                'post_password' => '$inthebananastand',
     
    16421717                        )
    16431718                );
     1719
    16441720                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
    16451721                $response = rest_get_server()->dispatch( $request );
     
    17041780                        )
    17051781                );
     1782
    17061783                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
    17071784                $request->set_param( 'context', 'edit' );
     
    17141791                register_post_status( 'testpubstatus', array( 'public' => true ) );
    17151792                register_post_status( 'testprivtatus', array( 'public' => false ) );
    1716                 // Public status
     1793
     1794                // Public status.
    17171795                wp_update_post(
    17181796                        array(
     
    17211799                        )
    17221800                );
     1801
    17231802                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    17241803                $response = rest_get_server()->dispatch( $request );
    17251804                $this->assertEquals( 200, $response->get_status() );
    1726                 // Private status
     1805
     1806                // Private status.
    17271807                wp_update_post(
    17281808                        array(
     
    17311811                        )
    17321812                );
     1813
    17331814                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    17341815                $response = rest_get_server()->dispatch( $request );
     
    17481829        public function test_prepare_item_limit_fields() {
    17491830                wp_set_current_user( self::$editor_id );
     1831
    17501832                $endpoint = new WP_REST_Posts_Controller( 'post' );
    17511833                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     
    17751857
    17761858                wp_set_current_user( self::$editor_id );
     1859
    17771860                $endpoint = new WP_REST_Posts_Controller( 'post' );
    1778                 $request  = new WP_REST_REQUEST( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1861                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    17791862
    17801863                $request->set_param( 'context', 'edit' );
     
    18101893
    18111894                wp_set_current_user( self::$editor_id );
     1895
    18121896                $endpoint = new WP_REST_Posts_Controller( 'post' );
    1813                 $request  = new WP_REST_REQUEST( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1897                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    18141898
    18151899                $request->set_param( 'context', 'edit' );
     
    19202004        public function test_create_post_date( $status, $params, $results ) {
    19212005                wp_set_current_user( self::$editor_id );
     2006
    19222007                update_option( 'timezone_string', $params['timezone_string'] );
    19232008
     
    19532038        public function test_create_item_with_template() {
    19542039                wp_set_current_user( self::$editor_id );
     2040
    19552041                add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) );
    19562042
     
    20022088        public function test_create_item_with_template_none() {
    20032089                wp_set_current_user( self::$editor_id );
     2090
    20042091                add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) );
    20052092                update_post_meta( self::$post_id, '_wp_page_template', 'post-my-test-template.php' );
     
    20502137        public function test_create_post_as_contributor() {
    20512138                wp_set_current_user( self::$contributor_id );
     2139
    20522140                update_option( 'timezone_string', 'America/Chicago' );
    20532141
     
    21832271        public function test_create_post_private_without_permission() {
    21842272                wp_set_current_user( self::$author_id );
     2273
    21852274                $user = wp_get_current_user();
    21862275                $user->add_cap( 'publish_posts', false );
     
    22042293        public function test_create_post_publish_without_permission() {
    22052294                wp_set_current_user( self::$author_id );
     2295
    22062296                $user = wp_get_current_user();
    22072297                $user->add_cap( 'publish_posts', false );
     
    24092499
    24102500                $data = $response->get_data();
    2411 
    24122501                $this->assertEquals( '0', $data['password'] );
    24132502        }
     
    25522641                $request->set_body_params( $params );
    25532642                $response = rest_get_server()->dispatch( $request );
    2554                 $new_data = $response->get_data();
    2555                 $this->assertEquals( "Rob O'Rourke's Diary", $new_data['title']['raw'] );
     2643
     2644                $data = $response->get_data();
     2645                $this->assertEquals( "Rob O'Rourke's Diary", $data['title']['raw'] );
    25562646        }
    25572647
    25582648        public function test_create_post_with_categories() {
    25592649                wp_set_current_user( self::$editor_id );
     2650
    25602651                $category = wp_insert_term( 'Test Category', 'category' );
    2561                 $request  = new WP_REST_Request( 'POST', '/wp/v2/posts' );
    2562                 $params   = $this->set_post_data(
     2652
     2653                $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     2654                $params  = $this->set_post_data(
    25632655                        array(
    25642656                                'password'   => 'testing',
     
    25772669        public function test_create_post_with_categories_as_csv() {
    25782670                wp_set_current_user( self::$editor_id );
     2671
    25792672                $category  = wp_insert_term( 'Chicken', 'category' );
    25802673                $category2 = wp_insert_term( 'Ribs', 'category' );
    2581                 $request   = new WP_REST_Request( 'POST', '/wp/v2/posts' );
    2582                 $params    = $this->set_post_data(
     2674
     2675                $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
     2676                $params  = $this->set_post_data(
    25832677                        array(
    25842678                                'categories' => $category['term_id'] . ',' . $category2['term_id'],
     
    25942688        public function test_create_post_with_invalid_categories() {
    25952689                wp_set_current_user( self::$editor_id );
     2690
    25962691                $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
    25972692                $params  = $this->set_post_data(
     
    26182713
    26192714                wp_set_current_user( self::$editor_id );
     2715
    26202716                $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
    26212717                $params  = $this->set_post_data(
     
    26642760        public function test_update_item_no_change() {
    26652761                wp_set_current_user( self::$editor_id );
     2762
    26662763                $post = get_post( self::$post_id );
    26672764
     
    27092806                // Create a new test post.
    27102807                $post_id = $this->factory->post->create();
     2808
    27112809                wp_set_current_user( self::$editor_id );
    27122810
    27132811                // Set the post date to the future.
    27142812                $future_date = '2919-07-29T18:00:00';
    2715                 $request     = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) );
     2813
     2814                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) );
    27162815                $request->add_header( 'content-type', 'application/json' );
    27172816                $params = $this->set_post_data(
     
    27972896        public function test_update_post_without_permission() {
    27982897                wp_set_current_user( self::$editor_id );
     2898
    27992899                $user = wp_get_current_user();
    28002900                $user->add_cap( 'edit_published_posts', false );
     
    29513051        public function test_update_post_date( $status, $params, $results ) {
    29523052                wp_set_current_user( self::$editor_id );
     3053
    29533054                update_option( 'timezone_string', $params['timezone_string'] );
    29543055
     
    30133114
    30143115                wp_set_current_user( self::$editor_id );
     3116
    30153117                update_option( 'timezone_string', 'America/Chicago' );
    30163118
     
    31913293        public function test_update_post_with_empty_password() {
    31923294                wp_set_current_user( self::$editor_id );
     3295
    31933296                wp_update_post(
    31943297                        array(
     
    32813384
    32823385        public function test_update_post_with_categories() {
    3283 
    3284                 wp_set_current_user( self::$editor_id );
     3386                wp_set_current_user( self::$editor_id );
     3387
    32853388                $category = wp_insert_term( 'Test Category', 'category' );
    32863389
     
    33073410                $query = parse_url( $categories_path, PHP_URL_QUERY );
    33083411                parse_str( $query, $args );
     3412
    33093413                $request = new WP_REST_Request( 'GET', $args['rest_route'] );
    33103414                unset( $args['rest_route'] );
     
    33173421
    33183422        public function test_update_post_with_empty_categories() {
    3319 
    3320                 wp_set_current_user( self::$editor_id );
     3423                wp_set_current_user( self::$editor_id );
     3424
    33213425                $category = wp_insert_term( 'Test Category', 'category' );
    33223426                wp_set_object_terms( self::$post_id, $category['term_id'], 'category' );
     
    33433447
    33443448                wp_set_current_user( self::$editor_id );
     3449
    33453450                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    33463451                $params  = $this->set_post_data(
     
    33643469        public function test_update_item_with_template() {
    33653470                wp_set_current_user( self::$editor_id );
     3471
    33663472                add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) );
    33673473
     
    33933499        public function test_update_item_with_template_none() {
    33943500                wp_set_current_user( self::$editor_id );
     3501
    33953502                add_filter( 'theme_post_templates', array( $this, 'filter_theme_post_templates' ) );
    33963503                update_post_meta( self::$post_id, '_wp_page_template', 'post-my-test-template.php' );
     
    34253532         */
    34263533        public function test_update_item_with_same_template_that_no_longer_exists() {
    3427 
    34283534                wp_set_current_user( self::$editor_id );
    34293535
     
    35983704        public function test_post_roundtrip_as_author( $raw, $expected ) {
    35993705                wp_set_current_user( self::$author_id );
     3706
    36003707                $this->assertFalse( current_user_can( 'unfiltered_html' ) );
    36013708                $this->verify_post_roundtrip( $raw, $expected );
     
    36043711        public function test_post_roundtrip_as_editor_unfiltered_html() {
    36053712                wp_set_current_user( self::$editor_id );
     3713
    36063714                if ( is_multisite() ) {
    36073715                        $this->assertFalse( current_user_can( 'unfiltered_html' ) );
     
    36553763        public function test_post_roundtrip_as_superadmin_unfiltered_html() {
    36563764                wp_set_current_user( self::$superadmin_id );
     3765
    36573766                $this->assertTrue( current_user_can( 'unfiltered_html' ) );
    36583767                $this->verify_post_roundtrip(
     
    36813790        public function test_delete_item() {
    36823791                $post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) );
     3792
    36833793                wp_set_current_user( self::$editor_id );
    36843794
     
    36953805        public function test_delete_item_skip_trash() {
    36963806                $post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) );
     3807
    36973808                wp_set_current_user( self::$editor_id );
    36983809
     
    37093820        public function test_delete_item_already_trashed() {
    37103821                $post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) );
    3711                 wp_set_current_user( self::$editor_id );
     3822
     3823                wp_set_current_user( self::$editor_id );
     3824
    37123825                $request  = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/posts/%d', $post_id ) );
    37133826                $response = rest_get_server()->dispatch( $request );
     
    37283841        public function test_delete_post_invalid_post_type() {
    37293842                $page_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
     3843
    37303844                wp_set_current_user( self::$editor_id );
    37313845
     
    39464060                );
    39474061
    3948                 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' );
    3949 
     4062                $request  = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' );
    39504063                $response = rest_get_server()->dispatch( $request );
    39514064                $data     = $response->get_data();
     
    39584071                $post_id = $this->factory->post->create();
    39594072
    3960                 $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
    3961 
     4073                $request  = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
    39624074                $response = rest_get_server()->dispatch( $request );
    39634075                $this->assertArrayHasKey( 'my_custom_int', $response->data );
     
    40444156
    40454157                wp_set_current_user( self::$editor_id );
     4158
    40464159                // Check for error on update.
    40474160                $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     
    40724185
    40734186        public function test_publish_action_ldo_registered() {
    4074 
    40754187                $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ) );
    40764188                $data     = $response->get_data();
     
    40844196
    40854197        public function test_sticky_action_ldo_registered_for_posts() {
    4086 
    40874198                $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ) );
    40884199                $data     = $response->get_data();
     
    40964207
    40974208        public function test_sticky_action_ldo_not_registered_for_non_posts() {
    4098 
    40994209                $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/pages' ) );
    41004210                $data     = $response->get_data();
     
    41084218
    41094219        public function test_author_action_ldo_registered_for_post_types_with_author_support() {
    4110 
    41114220                $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ) );
    41124221                $data     = $response->get_data();
     
    41204229
    41214230        public function test_author_action_ldo_not_registered_for_post_types_without_author_support() {
    4122 
    41234231                remove_post_type_support( 'post', 'author' );
    41244232
     
    41404248
    41414249        public function test_term_action_ldos_registered() {
    4142 
    41434250                $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ) );
    41444251                $data     = $response->get_data();
     
    41604267
    41614268        public function test_action_links_only_available_in_edit_context() {
    4162 
    41634269                wp_set_current_user( self::$author_id );
    41644270
     
    41764282
    41774283        public function test_publish_action_link_exists_for_author() {
    4178 
    41794284                wp_set_current_user( self::$author_id );
    41804285
     
    41924297
    41934298        public function test_publish_action_link_does_not_exist_for_contributor() {
    4194 
    41954299                wp_set_current_user( self::$contributor_id );
    41964300
     
    42084312
    42094313        public function test_sticky_action_exists_for_editor() {
    4210 
    42114314                wp_set_current_user( self::$editor_id );
    42124315
     
    42244327
    42254328        public function test_sticky_action_does_not_exist_for_author() {
    4226 
    42274329                wp_set_current_user( self::$author_id );
    42284330
     
    42404342
    42414343        public function test_sticky_action_does_not_exist_for_non_post_posts() {
    4242 
    42434344                wp_set_current_user( self::$editor_id );
    42444345
     
    42624363
    42634364        public function test_assign_author_action_exists_for_editor() {
    4264 
    42654365                wp_set_current_user( self::$editor_id );
    42664366
     
    42784378
    42794379        public function test_assign_author_action_does_not_exist_for_author() {
    4280 
    42814380                wp_set_current_user( self::$author_id );
    42824381
     
    42944393
    42954394        public function test_assign_author_action_does_not_exist_for_post_types_without_author_support() {
    4296 
    42974395                remove_post_type_support( 'post', 'author' );
    42984396
     
    43124410
    43134411        public function test_create_term_action_exists_for_editor() {
    4314 
    43154412                wp_set_current_user( self::$editor_id );
    43164413
     
    43304427
    43314428        public function test_create_term_action_non_hierarchical_exists_for_author() {
    4332 
    43334429                wp_set_current_user( self::$author_id );
    43344430
     
    43464442
    43474443        public function test_create_term_action_hierarchical_does_not_exists_for_author() {
    4348 
    43494444                wp_set_current_user( self::$author_id );
    43504445
     
    43624457
    43634458        public function test_assign_term_action_exists_for_contributor() {
    4364 
    43654459                wp_set_current_user( self::$contributor_id );
    43664460
     
    43854479        public function test_assign_unfiltered_html_action_superadmin() {
    43864480                $post_id = self::factory()->post->create();
     4481
    43874482                wp_set_current_user( self::$superadmin_id );
     4483
    43884484                $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
    43894485                $request->set_param( 'context', 'edit' );
     
    43954491        public function test_assign_unfiltered_html_action_editor() {
    43964492                $post_id = self::factory()->post->create();
    4397                 wp_set_current_user( self::$editor_id );
     4493
     4494                wp_set_current_user( self::$editor_id );
     4495
    43984496                $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
    43994497                $request->set_param( 'context', 'edit' );
     
    44104508        public function test_assign_unfiltered_html_action_author() {
    44114509                $post_id = self::factory()->post->create();
     4510
    44124511                wp_set_current_user( self::$author_id );
     4512
    44134513                $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
    44144514                $request->set_param( 'context', 'edit' );
     
    44914591         */
    44924592        public function test_putting_same_publish_date_does_not_remove_floating_date() {
    4493 
    44944593                wp_set_current_user( self::$superadmin_id );
    44954594
     
    45274626         */
    45284627        public function test_putting_different_publish_date_removes_floating_date() {
    4529 
    45304628                wp_set_current_user( self::$superadmin_id );
    45314629
     
    45704668         */
    45714669        public function test_publishing_post_with_same_date_removes_floating_date() {
    4572 
    45734670                wp_set_current_user( self::$superadmin_id );
    45744671
  • trunk/tests/phpunit/tests/rest-api/rest-tags-controller.php

    r46586 r46657  
    1717        protected static $subscriber;
    1818
     19        protected static $tag_ids    = array();
     20        protected static $total_tags = 30;
     21        protected static $per_page   = 50;
     22
    1923        public static function wpSetUpBeforeClass( $factory ) {
    2024                self::$superadmin    = $factory->user->create(
     
    4448                        )
    4549                );
     50
    4651                if ( is_multisite() ) {
    4752                        update_site_option( 'site_admins', array( 'superadmin' ) );
     53                }
     54
     55                // Set up tags for pagination tests.
     56                for ( $i = 0; $i < self::$total_tags; $i++ ) {
     57                        $tag_ids[] = $factory->tag->create(
     58                                array(
     59                                        'name' => "Tag {$i}",
     60                                )
     61                        );
    4862                }
    4963        }
     
    5468                self::delete_user( self::$editor );
    5569                self::delete_user( self::$subscriber );
     70
     71                // Remove tags for pagination tests.
     72                foreach ( self::$tag_ids as $tag_id ) {
     73                        wp_delete_term( $tag_id, 'post_tag' );
     74                }
    5675        }
    5776
     
    155174        public function test_get_items() {
    156175                $this->factory->tag->create();
    157                 $request  = new WP_REST_Request( 'GET', '/wp/v2/tags' );
     176
     177                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
     178                $request->set_param( 'per_page', self::$per_page );
    158179                $response = rest_get_server()->dispatch( $request );
    159180                $this->check_get_taxonomy_terms_response( $response );
     
    162183        public function test_get_items_invalid_permission_for_context() {
    163184                wp_set_current_user( 0 );
     185
    164186                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
    165187                $request->set_param( 'context', 'edit' );
     
    172194                $tag1    = $this->factory->tag->create( array( 'name' => 'Season 5' ) );
    173195                $tag2    = $this->factory->tag->create( array( 'name' => 'The Be Sharps' ) );
     196
    174197                wp_set_object_terms( $post_id, array( $tag1, $tag2 ), 'post_tag' );
     198
    175199                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
    176200                $request->set_param( 'hide_empty', true );
     
    180204                $this->assertEquals( 'Season 5', $data[0]['name'] );
    181205                $this->assertEquals( 'The Be Sharps', $data[1]['name'] );
    182                 // invalid value should fail
     206
     207                // Invalid 'hide_empty' should error.
    183208                $request->set_param( 'hide_empty', 'nothanks' );
    184209                $response = rest_get_server()->dispatch( $request );
     
    187212
    188213        public function test_get_items_include_query() {
    189                 $id1     = $this->factory->tag->create();
    190                 $id2     = $this->factory->tag->create();
    191                 $id3     = $this->factory->tag->create();
    192                 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
    193                 // Orderby=>asc
    194                 $request->set_param( 'include', array( $id3, $id1 ) );
     214                $id1 = $this->factory->tag->create();
     215                $id2 = $this->factory->tag->create();
     216
     217                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
     218
     219                // 'orderby' => 'asc'.
     220                $request->set_param( 'include', array( $id2, $id1 ) );
    195221                $response = rest_get_server()->dispatch( $request );
    196222                $data     = $response->get_data();
    197223                $this->assertEquals( 2, count( $data ) );
    198224                $this->assertEquals( $id1, $data[0]['id'] );
    199                 // Orderby=>include
     225
     226                // 'orderby' => 'include'.
    200227                $request->set_param( 'orderby', 'include' );
    201228                $response = rest_get_server()->dispatch( $request );
    202229                $data     = $response->get_data();
    203230                $this->assertEquals( 2, count( $data ) );
    204                 $this->assertEquals( $id3, $data[0]['id'] );
    205                 // Include invalid value shoud fail
     231                $this->assertEquals( $id2, $data[0]['id'] );
     232
     233                // Invalid 'include' should error.
    206234                $request->set_param( 'include', array( 'myterm' ) );
    207235                $response = rest_get_server()->dispatch( $request );
     
    210238
    211239        public function test_get_items_exclude_query() {
    212                 $id1      = $this->factory->tag->create();
    213                 $id2      = $this->factory->tag->create();
    214                 $request  = new WP_REST_Request( 'GET', '/wp/v2/tags' );
     240                $id1 = $this->factory->tag->create();
     241                $id2 = $this->factory->tag->create();
     242
     243                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
     244                $request->set_param( 'per_page', self::$per_page );
    215245                $response = rest_get_server()->dispatch( $request );
    216246                $data     = $response->get_data();
    217                 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
    218                 $this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
     247                $ids      = wp_list_pluck( $data, 'id' );
     248                $this->assertTrue( in_array( $id1, $ids, true ) );
     249                $this->assertTrue( in_array( $id2, $ids, true ) );
     250
    219251                $request->set_param( 'exclude', array( $id2 ) );
    220252                $response = rest_get_server()->dispatch( $request );
    221253                $data     = $response->get_data();
    222                 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
    223                 $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
    224                 // Invalid exclude value should fail
     254                $ids      = wp_list_pluck( $data, 'id' );
     255                $this->assertTrue( in_array( $id1, $ids, true ) );
     256                $this->assertFalse( in_array( $id2, $ids, true ) );
     257
     258                // Invalid 'exclude' should error.
    225259                $request->set_param( 'exclude', array( 'invalid' ) );
    226260                $response = rest_get_server()->dispatch( $request );
     
    229263
    230264        public function test_get_items_offset_query() {
    231                 $id1     = $this->factory->tag->create();
    232                 $id2     = $this->factory->tag->create();
    233                 $id3     = $this->factory->tag->create();
    234                 $id4     = $this->factory->tag->create();
    235                 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
     265                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
     266                $request->set_param( 'per_page', self::$per_page );
    236267                $request->set_param( 'offset', 1 );
    237268                $response = rest_get_server()->dispatch( $request );
    238                 $this->assertCount( 3, $response->get_data() );
    239                 // 'offset' works with 'per_page'
     269                $this->assertCount( self::$total_tags - 1, $response->get_data() );
     270
     271                // 'offset' works with 'per_page'.
    240272                $request->set_param( 'per_page', 2 );
    241273                $response = rest_get_server()->dispatch( $request );
    242274                $this->assertCount( 2, $response->get_data() );
    243                 // 'offset' takes priority over 'page'
     275
     276                // 'offset' takes priority over 'page'.
    244277                $request->set_param( 'page', 3 );
    245278                $response = rest_get_server()->dispatch( $request );
    246279                $this->assertCount( 2, $response->get_data() );
    247                 // 'offset' invalid value shoudl fail
     280
     281                // Invalid 'offset' should error.
    248282                $request->set_param( 'offset', 'moreplease' );
    249283                $response = rest_get_server()->dispatch( $request );
     
    254288        public function test_get_items_orderby_args() {
    255289                $tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) );
    256                 $tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) );
     290                $tag2 = $this->factory->tag->create( array( 'name' => 'Zucchini' ) );
     291
    257292                /*
    258293                 * Tests:
     
    269304                $data = $response->get_data();
    270305                $this->assertEquals( 1, count( $data ) );
    271                 $this->assertEquals( 'Banana', $data[0]['name'] );
     306                $this->assertEquals( 'Zucchini', $data[0]['name'] );
     307
    272308                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
    273309                $request->set_param( 'orderby', 'name' );
     
    279315                $this->assertEquals( 2, count( $data ) );
    280316                $this->assertEquals( 'Apple', $data[0]['name'] );
    281                 // Invalid orderby should fail.
     317
     318                // Invalid 'orderby' should error.
    282319                $request->set_param( 'orderby', 'invalid' );
    283320                $response = rest_get_server()->dispatch( $request );
     
    289326                $tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) );
    290327                $tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) );
    291                 // defaults to orderby=name, order=asc
     328
     329                // Defaults to 'orderby' => 'name', 'order' => 'asc'.
    292330                $request  = new WP_REST_Request( 'GET', '/wp/v2/tags' );
    293331                $response = rest_get_server()->dispatch( $request );
     
    297335                $this->assertEquals( 'Banana', $data[1]['name'] );
    298336                $this->assertEquals( 'Cantaloupe', $data[2]['name'] );
    299                 // orderby=id, with default order=asc
     337
     338                // 'orderby' => 'id', with default 'order' => 'asc'.
    300339                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
    301340                $request->set_param( 'orderby', 'id' );
     
    303342                $this->assertEquals( 200, $response->get_status() );
    304343                $data = $response->get_data();
    305                 $this->assertEquals( 'Cantaloupe', $data[0]['name'] );
    306                 $this->assertEquals( 'Apple', $data[1]['name'] );
    307                 $this->assertEquals( 'Banana', $data[2]['name'] );
    308                 // orderby=id, order=desc
     344                $this->assertEquals( 'Tag 0', $data[0]['name'] );
     345                $this->assertEquals( 'Tag 1', $data[1]['name'] );
     346                $this->assertEquals( 'Tag 2', $data[2]['name'] );
     347
     348                // 'orderby' => 'id', 'order' => 'desc'.
    309349                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
    310350                $request->set_param( 'orderby', 'id' );
     
    339379                $tag2    = $this->factory->tag->create( array( 'name' => 'Marvel' ) );
    340380                $this->factory->tag->create( array( 'name' => 'Dark Horse' ) );
     381
    341382                wp_set_object_terms( $post_id, array( $tag1, $tag2 ), 'post_tag' );
    342383
     
    350391                $this->assertEquals( 'DC', $data[0]['name'] );
    351392
    352                 // Invalid post should error.
     393                // Invalid 'post' should error.
    353394                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
    354395                $request->set_param( 'post', 'invalid-post' );
     
    359400        public function test_get_terms_post_args_paging() {
    360401                $post_id = $this->factory->post->create();
    361                 $tag_ids = array();
    362 
    363                 for ( $i = 0; $i < 30; $i++ ) {
    364                         $tag_ids[] = $this->factory->tag->create(
    365                                 array(
    366                                         'name' => "Tag {$i}",
    367                                 )
    368                         );
    369                 }
    370                 wp_set_object_terms( $post_id, $tag_ids, 'post_tag' );
     402
     403                wp_set_object_terms( $post_id, self::$tag_ids, 'post_tag' );
    371404
    372405                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
     
    433466                );
    434467                $post_id = $this->factory->post->create();
     468
    435469                wp_set_object_terms( $post_id, array( $term1, $term2 ), 'batman' );
    436470
     
    448482                $tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) );
    449483                $tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) );
     484
    450485                /*
    451486                 * Tests:
     
    459494                $this->assertEquals( 1, count( $data ) );
    460495                $this->assertEquals( 'Apple', $data[0]['name'] );
     496
    461497                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
    462498                $request->set_param( 'search', 'Garbage' );
     
    468504
    469505        public function test_get_items_slug_arg() {
    470                 $tag1    = $this->factory->tag->create( array( 'name' => 'Apple' ) );
    471                 $tag2    = $this->factory->tag->create( array( 'name' => 'Banana' ) );
     506                $tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) );
     507                $tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) );
     508
    472509                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
    473510                $request->set_param( 'slug', 'apple' );
     
    484521                $id3 = $this->factory->tag->create( array( 'name' => 'Burrito' ) );
    485522                $this->factory->tag->create( array( 'name' => 'Pizza' ) );
     523
    486524                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
    487525                $request->set_param(
     
    506544                $id3 = $this->factory->tag->create( array( 'name' => 'Burrito' ) );
    507545                $this->factory->tag->create( array( 'name' => 'Pizza' ) );
     546
    508547                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
    509548                $request->set_param( 'slug', 'taco,burrito, enchilada' );
     
    537576
    538577        public function test_get_terms_pagination_headers() {
    539                 // Start of the index
    540                 for ( $i = 0; $i < 50; $i++ ) {
    541                         $this->factory->tag->create(
    542                                 array(
    543                                         'name' => "Tag {$i}",
    544                                 )
    545                         );
    546                 }
     578                $total_tags  = self::$total_tags;
     579                $total_pages = (int) ceil( $total_tags / 10 );
     580
     581                // Start of the index.
    547582                $request  = new WP_REST_Request( 'GET', '/wp/v2/tags' );
    548583                $response = rest_get_server()->dispatch( $request );
    549584                $headers  = $response->get_headers();
    550                 $this->assertEquals( 50, $headers['X-WP-Total'] );
    551                 $this->assertEquals( 5, $headers['X-WP-TotalPages'] );
     585                $this->assertEquals( $total_tags, $headers['X-WP-Total'] );
     586                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    552587                $next_link = add_query_arg(
    553588                        array(
     
    558593                $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
    559594                $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    560                 // 3rd page
    561                 $this->factory->tag->create(
    562                         array(
    563                                 'name' => 'Tag 51',
    564                         )
    565                 );
     595
     596                // 3rd page.
     597                $this->factory->tag->create();
     598                $total_tags++;
     599                $total_pages++;
    566600                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
    567601                $request->set_param( 'page', 3 );
    568602                $response = rest_get_server()->dispatch( $request );
    569603                $headers  = $response->get_headers();
    570                 $this->assertEquals( 51, $headers['X-WP-Total'] );
    571                 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
     604                $this->assertEquals( $total_tags, $headers['X-WP-Total'] );
     605                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    572606                $prev_link = add_query_arg(
    573607                        array(
     
    584618                );
    585619                $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    586                 // Last page
    587                 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
    588                 $request->set_param( 'page', 6 );
     620
     621                // Last page.
     622                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
     623                $request->set_param( 'page', $total_pages );
    589624                $response = rest_get_server()->dispatch( $request );
    590625                $headers  = $response->get_headers();
    591                 $this->assertEquals( 51, $headers['X-WP-Total'] );
    592                 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
     626                $this->assertEquals( $total_tags, $headers['X-WP-Total'] );
     627                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    593628                $prev_link = add_query_arg(
    594629                        array(
    595                                 'page' => 5,
     630                                'page' => $total_pages - 1,
    596631                        ),
    597632                        rest_url( 'wp/v2/tags' )
     
    599634                $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    600635                $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
    601                 // Out of bounds
    602                 $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
    603                 $request->set_param( 'page', 8 );
     636
     637                // Out of bounds.
     638                $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
     639                $request->set_param( 'page', 100 );
    604640                $response = rest_get_server()->dispatch( $request );
    605641                $headers  = $response->get_headers();
    606                 $this->assertEquals( 51, $headers['X-WP-Total'] );
    607                 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
     642                $this->assertEquals( $total_tags, $headers['X-WP-Total'] );
     643                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    608644                $prev_link = add_query_arg(
    609645                        array(
    610                                 'page' => 6,
     646                                'page' => $total_pages,
    611647                        ),
    612648                        rest_url( 'wp/v2/tags' )
     
    624660
    625661        public function test_get_item() {
    626                 $id       = $this->factory->tag->create();
     662                $id = $this->factory->tag->create();
     663
    627664                $request  = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id );
    628665                $response = rest_get_server()->dispatch( $request );
     
    634671         */
    635672        public function test_get_item_meta() {
    636                 $id       = $this->factory->tag->create();
     673                $id = $this->factory->tag->create();
     674
    637675                $request  = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id );
    638676                $response = rest_get_server()->dispatch( $request );
     
    655693         */
    656694        public function test_get_item_meta_registered_for_different_taxonomy() {
    657                 $id       = $this->factory->tag->create();
     695                $id = $this->factory->tag->create();
     696
    658697                $request  = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id );
    659698                $response = rest_get_server()->dispatch( $request );
     
    673712        public function test_get_item_invalid_permission_for_context() {
    674713                $id = $this->factory->tag->create();
     714
    675715                wp_set_current_user( 0 );
     716
    676717                $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id );
    677718                $request->set_param( 'context', 'edit' );
     
    696737        public function test_get_item_incorrect_taxonomy() {
    697738                register_taxonomy( 'robin', 'post' );
    698                 $term1    = $this->factory->term->create(
     739                $term1 = $this->factory->term->create(
    699740                        array(
    700741                                'name'     => 'Cape',
     
    702743                        )
    703744                );
     745
    704746                $request  = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $term1 );
    705747                $response = rest_get_server()->dispatch( $request );
     
    709751        public function test_create_item() {
    710752                wp_set_current_user( self::$administrator );
     753
    711754                $request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
    712755                $request->set_param( 'name', 'My Awesome Term' );
     
    725768        public function test_create_item_contributor() {
    726769                wp_set_current_user( self::$contributor );
     770
    727771                $request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
    728772                $request->set_param( 'name', 'My Awesome Term' );
     
    741785        public function test_create_item_incorrect_permissions() {
    742786                wp_set_current_user( self::$subscriber );
     787
    743788                $request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
    744789                $request->set_param( 'name', 'Incorrect permissions' );
     
    749794        public function test_create_item_missing_arguments() {
    750795                wp_set_current_user( self::$administrator );
     796
    751797                $request  = new WP_REST_Request( 'POST', '/wp/v2/tags' );
    752798                $response = rest_get_server()->dispatch( $request );
     
    766812        public function test_create_item_with_meta() {
    767813                wp_set_current_user( self::$administrator );
     814
    768815                $request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
    769816                $request->set_param( 'name', 'My Awesome Term' );
     
    780827        public function test_create_item_with_meta_wrong_id() {
    781828                wp_set_current_user( self::$administrator );
     829
    782830                $existing_tag_id = $this->factory->tag->create( array( 'name' => 'My Not So Awesome Term' ) );
    783                 $request         = new WP_REST_Request( 'POST', '/wp/v2/tags' );
     831
     832                $request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
    784833                $request->set_param( 'name', 'My Awesome Term' );
    785834                $request->set_param( 'meta', array( 'test_tag_single' => 'hello' ) );
     
    797846        public function test_update_item() {
    798847                wp_set_current_user( self::$administrator );
     848
    799849                $orig_args = array(
    800850                        'name'        => 'Original Name',
     
    802852                        'slug'        => 'original-slug',
    803853                );
    804                 $term      = get_term_by( 'id', $this->factory->tag->create( $orig_args ), 'post_tag' );
    805                 $request   = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id );
     854
     855                $term = get_term_by( 'id', $this->factory->tag->create( $orig_args ), 'post_tag' );
     856
     857                $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id );
    806858                $request->set_param( 'name', 'New Name' );
    807859                $request->set_param( 'description', 'New Description' );
     
    828880        public function test_update_item_no_change() {
    829881                wp_set_current_user( self::$administrator );
     882
    830883                $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
    831884
    832                 $request = new WP_REST_Request( 'PUT', '/wp/v2/tags/' . $term->term_id );
    833 
     885                $request  = new WP_REST_Request( 'PUT', '/wp/v2/tags/' . $term->term_id );
    834886                $response = rest_get_server()->dispatch( $request );
    835887                $this->assertEquals( 200, $response->get_status() );
     
    847899        public function test_update_item_invalid_term() {
    848900                wp_set_current_user( self::$administrator );
     901
    849902                $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    850903                $request->set_param( 'name', 'Invalid Term' );
     
    855908        public function test_update_item_incorrect_permissions() {
    856909                wp_set_current_user( self::$subscriber );
    857                 $term    = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
     910
     911                $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
     912
    858913                $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id );
    859914                $request->set_param( 'name', 'Incorrect permissions' );
     
    867922        public function test_update_item_with_edit_term_cap_granted() {
    868923                wp_set_current_user( self::$subscriber );
    869                 $term    = $this->factory->tag->create_and_get();
     924
     925                $term = $this->factory->tag->create_and_get();
     926
    870927                $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id );
    871928                $request->set_param( 'name', 'New Name' );
     
    892949        public function test_update_item_with_edit_term_cap_revoked() {
    893950                wp_set_current_user( self::$administrator );
    894                 $term    = $this->factory->tag->create_and_get();
     951
     952                $term = $this->factory->tag->create_and_get();
     953
    895954                $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id );
    896955                $request->set_param( 'name', 'New Name' );
     
    912971        public function test_update_item_parent_non_hierarchical_taxonomy() {
    913972                wp_set_current_user( self::$administrator );
     973
    914974                $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
    915975
     
    9601020        public function test_tag_roundtrip_as_editor() {
    9611021                wp_set_current_user( self::$editor );
     1022
    9621023                $this->assertEquals( ! is_multisite(), current_user_can( 'unfiltered_html' ) );
    9631024                $this->verify_tag_roundtrip(
     
    9751036        public function test_tag_roundtrip_as_editor_html() {
    9761037                wp_set_current_user( self::$editor );
     1038
    9771039                if ( is_multisite() ) {
    9781040                        $this->assertFalse( current_user_can( 'unfiltered_html' ) );
     
    10041066        public function test_tag_roundtrip_as_superadmin() {
    10051067                wp_set_current_user( self::$superadmin );
     1068
    10061069                $this->assertTrue( current_user_can( 'unfiltered_html' ) );
    10071070                $this->verify_tag_roundtrip(
     
    10191082        public function test_tag_roundtrip_as_superadmin_html() {
    10201083                wp_set_current_user( self::$superadmin );
     1084
    10211085                $this->assertTrue( current_user_can( 'unfiltered_html' ) );
    10221086                $this->verify_tag_roundtrip(
     
    10341098        public function test_delete_item() {
    10351099                wp_set_current_user( self::$administrator );
    1036                 $term    = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' );
     1100
     1101                $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' );
     1102
    10371103                $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id );
    10381104                $request->set_param( 'force', true );
     
    10461112        public function test_delete_item_no_trash() {
    10471113                wp_set_current_user( self::$administrator );
     1114
    10481115                $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' );
    10491116
     
    10591126        public function test_delete_item_invalid_term() {
    10601127                wp_set_current_user( self::$administrator );
     1128
    10611129                $request  = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    10621130                $response = rest_get_server()->dispatch( $request );
     
    10661134        public function test_delete_item_incorrect_permissions() {
    10671135                wp_set_current_user( self::$subscriber );
    1068                 $term     = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
     1136
     1137                $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
     1138
    10691139                $request  = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id );
    10701140                $response = rest_get_server()->dispatch( $request );
     
    10771147        public function test_delete_item_with_delete_term_cap_granted() {
    10781148                wp_set_current_user( self::$subscriber );
    1079                 $term    = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' );
     1149
     1150                $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' );
     1151
    10801152                $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id );
    10811153                $request->set_param( 'force', true );
     
    11031175        public function test_delete_item_with_delete_term_cap_revoked() {
    11041176                wp_set_current_user( self::$administrator );
    1105                 $term    = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' );
     1177
     1178                $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' );
     1179
    11061180                $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id );
    11071181                $request->set_param( 'force', true );
     
    11221196
    11231197        public function test_prepare_item() {
    1124                 $term     = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
     1198                $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
     1199
    11251200                $request  = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $term->term_id );
    11261201                $response = rest_get_server()->dispatch( $request );
     
    11961271                $this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] );
    11971272
    1198                 $tag_id  = $this->factory->tag->create();
    1199                 $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $tag_id );
    1200 
     1273                $tag_id = $this->factory->tag->create();
     1274
     1275                $request  = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $tag_id );
    12011276                $response = rest_get_server()->dispatch( $request );
    12021277                $this->assertArrayHasKey( 'my_custom_int', $response->data );
     
    12251300
    12261301                wp_set_current_user( self::$administrator );
     1302
    12271303                $tag_id = $this->factory->tag->create();
     1304
    12281305                // Check for error on update.
    12291306                $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/tags/%d', $tag_id ) );
  • trunk/tests/phpunit/tests/rest-api/rest-users-controller.php

    r46586 r46657  
    1616        protected static $draft_editor;
    1717        protected static $subscriber;
    18         protected static $authors = array();
    19         protected static $posts   = array();
     18
     19        protected static $authors     = array();
     20        protected static $posts       = array();
     21        protected static $user_ids    = array();
     22        protected static $total_users = 30;
     23        protected static $per_page    = 50;
     24
    2025        protected static $site;
    2126
     
    101106                        update_site_option( 'site_admins', array( 'superadmin' ) );
    102107                }
     108
     109                // Set up users for pagination tests.
     110                for ( $i = 0; $i < self::$total_users - 10; $i++ ) {
     111                        self::$user_ids[] = $factory->user->create(
     112                                array(
     113                                        'role'         => 'contributor',
     114                                        'display_name' => "User {$i}",
     115                                )
     116                        );
     117                }
    103118        }
    104119
     
    111126                        wp_delete_post( $post, true );
    112127                }
     128
    113129                foreach ( self::$authors as $author ) {
    114130                        self::delete_user( $author );
    115131                }
     132
    116133                _unregister_post_type( 'r_true_p_true' );
    117134                _unregister_post_type( 'r_true_p_false' );
     
    121138                if ( is_multisite() ) {
    122139                        wpmu_delete_blog( self::$site, true );
     140                }
     141
     142                // Remove users for pagination tests.
     143                foreach ( self::$user_ids as $user_id ) {
     144                        self::delete_user( $user_id );
    123145                }
    124146        }
     
    213235
    214236        public function test_get_items_with_edit_context_without_permission() {
    215                 //test with a user not logged in
     237                // Test with a user not logged in.
    216238                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    217239                $request->set_param( 'context', 'edit' );
     
    220242                $this->assertEquals( 401, $response->get_status() );
    221243
    222                 //test with a user logged in but without sufficient capabilities; capability in question: 'list_users'
     244                // Test with a user logged in but without sufficient capabilities;
     245                // capability in question: 'list_users'.
    223246                wp_set_current_user( self::$editor );
     247
    224248                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    225249                $request->set_param( 'context', 'edit' );
     
    281305
    282306        public function test_get_items_pagination_headers() {
    283                 wp_set_current_user( self::$user );
    284                 for ( $i = 0; $i < 44; $i++ ) {
    285                         $this->factory->user->create(
    286                                 array(
    287                                         'name' => "User {$i}",
    288                                 )
    289                         );
    290                 }
     307                $total_users = self::$total_users;
     308                $total_pages = (int) ceil( $total_users / 10 );
     309
     310                wp_set_current_user( self::$user );
     311
     312                // Start of the index.
    291313                $request  = new WP_REST_Request( 'GET', '/wp/v2/users' );
    292314                $response = rest_get_server()->dispatch( $request );
    293315                $headers  = $response->get_headers();
    294                 $this->assertEquals( 54, $headers['X-WP-Total'] );
    295                 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
     316                $this->assertEquals( $total_users, $headers['X-WP-Total'] );
     317                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    296318                $next_link = add_query_arg(
    297319                        array(
     
    302324                $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
    303325                $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    304                 // 3rd page
    305                 $this->factory->user->create(
    306                         array(
    307                                 'name' => 'User 51',
    308                         )
    309                 );
     326
     327                // 3rd page.
     328                $this->factory->user->create();
     329                $total_users++;
     330                $total_pages++;
    310331                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    311332                $request->set_param( 'page', 3 );
    312333                $response = rest_get_server()->dispatch( $request );
    313334                $headers  = $response->get_headers();
    314                 $this->assertEquals( 55, $headers['X-WP-Total'] );
    315                 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
     335                $this->assertEquals( $total_users, $headers['X-WP-Total'] );
     336                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    316337                $prev_link = add_query_arg(
    317338                        array(
     
    328349                );
    329350                $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    330                 // Last page
    331                 $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    332                 $request->set_param( 'page', 6 );
     351
     352                // Last page.
     353                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
     354                $request->set_param( 'page', $total_pages );
    333355                $response = rest_get_server()->dispatch( $request );
    334356                $headers  = $response->get_headers();
    335                 $this->assertEquals( 55, $headers['X-WP-Total'] );
    336                 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
     357                $this->assertEquals( $total_users, $headers['X-WP-Total'] );
     358                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    337359                $prev_link = add_query_arg(
    338360                        array(
    339                                 'page' => 5,
     361                                'page' => $total_pages - 1,
    340362                        ),
    341363                        rest_url( 'wp/v2/users' )
     
    343365                $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    344366                $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
    345                 // Out of bounds
    346                 $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    347                 $request->set_param( 'page', 8 );
     367
     368                // Out of bounds.
     369                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
     370                $request->set_param( 'page', 100 );
    348371                $response = rest_get_server()->dispatch( $request );
    349372                $headers  = $response->get_headers();
    350                 $this->assertEquals( 55, $headers['X-WP-Total'] );
    351                 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
     373                $this->assertEquals( $total_users, $headers['X-WP-Total'] );
     374                $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
    352375                $prev_link = add_query_arg(
    353376                        array(
    354                                 'page' => 6,
     377                                'page' => $total_pages,
    355378                        ),
    356379                        rest_url( 'wp/v2/users' )
     
    362385        public function test_get_items_per_page() {
    363386                wp_set_current_user( self::$user );
    364                 for ( $i = 0; $i < 20; $i++ ) {
    365                         $this->factory->user->create( array( 'display_name' => "User {$i}" ) );
    366                 }
     387
    367388                $request  = new WP_REST_Request( 'GET', '/wp/v2/users' );
    368389                $response = rest_get_server()->dispatch( $request );
    369390                $this->assertEquals( 10, count( $response->get_data() ) );
     391
    370392                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    371393                $request->set_param( 'per_page', 5 );
     
    376398        public function test_get_items_page() {
    377399                wp_set_current_user( self::$user );
    378                 for ( $i = 0; $i < 20; $i++ ) {
    379                         $this->factory->user->create( array( 'display_name' => "User {$i}" ) );
    380                 }
     400
    381401                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    382402                $request->set_param( 'per_page', 5 );
     
    397417        public function test_get_items_orderby_name() {
    398418                wp_set_current_user( self::$user );
     419
    399420                $low_id  = $this->factory->user->create( array( 'display_name' => 'AAAAA' ) );
    400421                $mid_id  = $this->factory->user->create( array( 'display_name' => 'NNNNN' ) );
    401422                $high_id = $this->factory->user->create( array( 'display_name' => 'ZZZZ' ) );
     423
    402424                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    403425                $request->set_param( 'orderby', 'name' );
     
    407429                $data     = $response->get_data();
    408430                $this->assertEquals( $high_id, $data[0]['id'] );
     431
    409432                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    410433                $request->set_param( 'orderby', 'name' );
     
    429452                $response = rest_get_server()->dispatch( $request );
    430453                $data     = $response->get_data();
    431 
    432454                $this->assertEquals( $high_id, $data[0]['id'] );
    433455
     
    455477                $response = rest_get_server()->dispatch( $request );
    456478                $data     = $response->get_data();
    457 
    458479                $this->assertEquals( $high_id, $data[0]['id'] );
    459480
     
    543564        public function test_get_items_offset() {
    544565                wp_set_current_user( self::$user );
    545                 // 9 users created in wpSetUpBeforeClass(), plus default user.
    546                 $this->factory->user->create();
    547                 $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
     566
     567                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
     568                $request->set_param( 'per_page', self::$per_page );
    548569                $request->set_param( 'offset', 1 );
    549570                $response = rest_get_server()->dispatch( $request );
    550                 $this->assertCount( 10, $response->get_data() );
    551                 // 'offset' works with 'per_page'
     571                $this->assertCount( self::$total_users - 1, $response->get_data() );
     572
     573                // 'offset' works with 'per_page'.
    552574                $request->set_param( 'per_page', 2 );
    553575                $response = rest_get_server()->dispatch( $request );
    554576                $this->assertCount( 2, $response->get_data() );
    555                 // 'offset' takes priority over 'page'
     577
     578                // 'offset' takes priority over 'page'.
    556579                $request->set_param( 'page', 3 );
    557580                $response = rest_get_server()->dispatch( $request );
    558581                $this->assertCount( 2, $response->get_data() );
    559                 // 'offset' invalid value should error
     582
     583                // Invalid 'offset' should error.
    560584                $request->set_param( 'offset', 'moreplease' );
    561585                $response = rest_get_server()->dispatch( $request );
     
    565589        public function test_get_items_include_query() {
    566590                wp_set_current_user( self::$user );
    567                 $id1     = $this->factory->user->create();
    568                 $id2     = $this->factory->user->create();
    569                 $id3     = $this->factory->user->create();
    570                 $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    571                 // Orderby=>asc
    572                 $request->set_param( 'include', array( $id3, $id1 ) );
     591
     592                $id1 = $this->factory->user->create();
     593                $id2 = $this->factory->user->create();
     594
     595                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
     596
     597                // 'orderby' => 'asc'.
     598                $request->set_param( 'include', array( $id2, $id1 ) );
    573599                $response = rest_get_server()->dispatch( $request );
    574600                $data     = $response->get_data();
    575601                $this->assertEquals( 2, count( $data ) );
    576602                $this->assertEquals( $id1, $data[0]['id'] );
    577                 // Orderby=>include
     603
     604                // 'orderby' => 'include'.
    578605                $request->set_param( 'orderby', 'include' );
    579606                $response = rest_get_server()->dispatch( $request );
    580607                $data     = $response->get_data();
    581608                $this->assertEquals( 2, count( $data ) );
    582                 $this->assertEquals( $id3, $data[0]['id'] );
    583                 // Invalid include should fail
     609                $this->assertEquals( $id2, $data[0]['id'] );
     610
     611                // Invalid 'include' should error.
    584612                $request->set_param( 'include', 'invalid' );
    585613                $response = rest_get_server()->dispatch( $request );
    586614                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    587                 // No privileges
    588                 $request->set_param( 'include', array( $id3, $id1 ) );
     615
     616                // No privileges.
     617                $request->set_param( 'include', array( $id2, $id1 ) );
    589618                wp_set_current_user( 0 );
    590619                $response = rest_get_server()->dispatch( $request );
     
    596625        public function test_get_items_exclude_query() {
    597626                wp_set_current_user( self::$user );
    598                 $id1     = $this->factory->user->create();
    599                 $id2     = $this->factory->user->create();
    600                 $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    601                 $request->set_param( 'per_page', 20 ); // there are >10 users at this point
    602                 $response = rest_get_server()->dispatch( $request );
    603                 $data     = $response->get_data();
    604                 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
    605                 $this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
     627
     628                $id1 = $this->factory->user->create();
     629                $id2 = $this->factory->user->create();
     630
     631                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
     632                $request->set_param( 'per_page', self::$per_page ); // There are >10 users at this point.
     633                $response = rest_get_server()->dispatch( $request );
     634                $data     = $response->get_data();
     635                $ids      = wp_list_pluck( $data, 'id' );
     636                $this->assertTrue( in_array( $id1, $ids, true ) );
     637                $this->assertTrue( in_array( $id2, $ids, true ) );
     638
    606639                $request->set_param( 'exclude', array( $id2 ) );
    607640                $response = rest_get_server()->dispatch( $request );
    608641                $data     = $response->get_data();
    609                 $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
    610                 $this->assertFalse( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
    611                 // Invalid exlude value should error.
     642                $ids      = wp_list_pluck( $data, 'id' );
     643                $this->assertTrue( in_array( $id1, $ids, true ) );
     644                $this->assertFalse( in_array( $id2, $ids, true ) );
     645
     646                // Invalid 'exclude' should error.
    612647                $request->set_param( 'exclude', 'none-of-those-please' );
    613648                $response = rest_get_server()->dispatch( $request );
     
    617652        public function test_get_items_search() {
    618653                wp_set_current_user( self::$user );
     654
    619655                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    620656                $request->set_param( 'search', 'yololololo' );
    621657                $response = rest_get_server()->dispatch( $request );
    622658                $this->assertEquals( 0, count( $response->get_data() ) );
     659
    623660                $yolo_id = $this->factory->user->create( array( 'display_name' => 'yololololo' ) );
     661
    624662                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    625663                $request->set_param( 'search', 'yololololo' );
     
    633671                        )
    634672                );
     673
    635674                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    636675                $request->set_param( 'search', 'ada' );
     
    643682        public function test_get_items_slug_query() {
    644683                wp_set_current_user( self::$user );
     684
    645685                $this->factory->user->create(
    646686                        array(
     
    649689                        )
    650690                );
    651                 $id2     = $this->factory->user->create(
     691                $id2 = $this->factory->user->create(
    652692                        array(
    653693                                'display_name' => 'Moo',
     
    655695                        )
    656696                );
     697
    657698                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    658699                $request->set_param( 'slug', 'foo' );
     
    665706        public function test_get_items_slug_array_query() {
    666707                wp_set_current_user( self::$user );
     708
    667709                $id1 = $this->factory->user->create(
    668710                        array(
     
    689731                        )
    690732                );
     733
    691734                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    692735                $request->set_param(
     
    709752        public function test_get_items_slug_csv_query() {
    710753                wp_set_current_user( self::$user );
     754
    711755                $id1 = $this->factory->user->create(
    712756                        array(
     
    733777                        )
    734778                );
     779
    735780                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    736781                $request->set_param( 'slug', 'taco,burrito , enchilada' );
     
    747792        public function test_get_items_roles() {
    748793                wp_set_current_user( self::$user );
    749                 $tango   = $this->factory->user->create(
     794
     795                $tango = $this->factory->user->create(
    750796                        array(
    751797                                'display_name' => 'tango',
     
    753799                        )
    754800                );
    755                 $yolo    = $this->factory->user->create(
     801                $yolo  = $this->factory->user->create(
    756802                        array(
    757803                                'display_name' => 'yolo',
     
    759805                        )
    760806                );
     807
    761808                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    762809                $request->set_param( 'roles', 'author,subscriber' );
     
    766813                $this->assertEquals( $tango, $data[1]['id'] );
    767814                $this->assertEquals( $yolo, $data[2]['id'] );
     815
    768816                $request->set_param( 'roles', 'author' );
    769817                $response = rest_get_server()->dispatch( $request );
     
    771819                $this->assertEquals( 1, count( $data ) );
    772820                $this->assertEquals( $yolo, $data[0]['id'] );
     821
    773822                wp_set_current_user( 0 );
     823
    774824                $request->set_param( 'roles', 'author' );
    775825                $response = rest_get_server()->dispatch( $request );
    776826                $this->assertErrorResponse( 'rest_user_cannot_view', $response, 401 );
     827
    777828                wp_set_current_user( self::$editor );
     829
    778830                $request->set_param( 'roles', 'author' );
    779831                $response = rest_get_server()->dispatch( $request );
     
    783835        public function test_get_items_invalid_roles() {
    784836                wp_set_current_user( self::$user );
    785                 $lolz    = $this->factory->user->create(
     837
     838                $lolz = $this->factory->user->create(
    786839                        array(
    787840                                'display_name' => 'lolz',
     
    789842                        )
    790843                );
     844
    791845                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    792846                $request->set_param( 'roles', 'ilovesteak,author' );
     
    795849                $this->assertEquals( 1, count( $data ) );
    796850                $this->assertEquals( $lolz, $data[0]['id'] );
     851
    797852                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    798853                $request->set_param( 'roles', 'steakisgood' );
     
    805860        public function test_get_items_who_author_query() {
    806861                wp_set_current_user( self::$superadmin );
     862
    807863                // First request should include subscriber in the set.
    808864                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
     
    811867                $this->assertEquals( 200, $response->get_status() );
    812868                $this->assertCount( 1, $response->get_data() );
     869
    813870                // Second request should exclude subscriber.
    814871                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
     
    822879        public function test_get_items_who_invalid_query() {
    823880                wp_set_current_user( self::$user );
     881
    824882                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    825883                $request->set_param( 'who', 'editor' );
     
    834892        public function test_get_items_who_unauthorized_query() {
    835893                wp_set_current_user( self::$subscriber );
     894
    836895                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
    837896                $request->set_param( 'who', 'authors' );
     
    842901        public function test_get_item() {
    843902                $user_id = $this->factory->user->create();
    844                 wp_set_current_user( self::$user );
    845 
    846                 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) );
    847 
     903
     904                wp_set_current_user( self::$user );
     905
     906                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) );
    848907                $response = rest_get_server()->dispatch( $request );
    849908                $this->check_get_user_response( $response, 'embed' );
     
    852911        public function test_prepare_item() {
    853912                wp_set_current_user( self::$user );
     913
    854914                $request = new WP_REST_Request;
    855915                $request->set_param( 'context', 'edit' );
     
    861921        public function test_prepare_item_limit_fields() {
    862922                wp_set_current_user( self::$user );
     923
    863924                $request = new WP_REST_Request;
    864925                $request->set_param( 'context', 'edit' );
     
    878939                wp_set_current_user( self::$user );
    879940
    880                 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$editor ) );
    881 
     941                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$editor ) );
    882942                $response = rest_get_server()->dispatch( $request );
    883943
     
    906966        public function test_get_user_empty_capabilities() {
    907967                wp_set_current_user( self::$user );
     968
    908969                $this->allow_user_to_manage_multisite();
    909970
     
    914975                        )
    915976                );
     977
    916978                delete_user_option( $lolz, 'capabilities' );
    917979                delete_user_option( $lolz, 'user_level' );
     980
    918981                $request = new WP_REST_Request( 'GET', '/wp/v2/users/' . $lolz );
    919982                $request->set_param( 'context', 'edit' );
     
    932995        public function test_cannot_get_item_without_permission() {
    933996                wp_set_current_user( self::$editor );
     997
    934998                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$user ) );
    935999                $response = rest_get_server()->dispatch( $request );
     
    9451009        public function test_can_get_item_author_of_rest_true_public_true_authenticated() {
    9461010                wp_set_current_user( self::$editor );
     1011
    9471012                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$authors['r_true_p_true'] ) );
    9481013                $response = rest_get_server()->dispatch( $request );
     
    9641029        public function test_cannot_get_item_author_of_rest_false_public_true_without_permission() {
    9651030                wp_set_current_user( self::$editor );
     1031
    9661032                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$authors['r_false_p_true'] ) );
    9671033                $response = rest_get_server()->dispatch( $request );
     
    9931059                        )
    9941060                );
    995                 $this->post_id   = $this->factory->post->create(
     1061
     1062                $this->post_id = $this->factory->post->create(
    9961063                        array(
    9971064                                'post_author' => $this->author_id,
    9981065                        )
    9991066                );
     1067
    10001068                wp_set_current_user( 0 );
     1069
    10011070                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $this->author_id ) );
    10021071                $response = rest_get_server()->dispatch( $request );
     
    10101079                        )
    10111080                );
     1081
    10121082                wp_set_current_user( 0 );
     1083
    10131084                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $this->author_id ) );
    10141085                $response = rest_get_server()->dispatch( $request );
    10151086                $this->assertEquals( 401, $response->get_status() );
     1087
    10161088                $this->post_id = $this->factory->post->create(
    10171089                        array(
     
    10201092                        )
    10211093                );
    1022                 $response      = rest_get_server()->dispatch( $request );
     1094
     1095                $response = rest_get_server()->dispatch( $request );
    10231096                $this->check_get_user_response( $response, 'embed' );
    10241097        }
     
    10261099        public function test_get_user_with_edit_context() {
    10271100                $user_id = $this->factory->user->create();
     1101
    10281102                $this->allow_user_to_manage_multisite();
    10291103
    10301104                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) );
    10311105                $request->set_param( 'context', 'edit' );
    1032 
    10331106                $response = rest_get_server()->dispatch( $request );
    10341107                $this->check_get_user_response( $response, 'edit' );
     
    10411114                        )
    10421115                );
    1043                 $this->post_id   = $this->factory->post->create(
     1116
     1117                $this->post_id = $this->factory->post->create(
    10441118                        array(
    10451119                                'post_author' => $this->author_id,
    10461120                        )
    10471121                );
     1122
    10481123                wp_set_current_user( 0 );
     1124
    10491125                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $this->author_id ) );
    10501126                $request->set_param( 'context', 'edit' );
     
    10561132                wp_set_current_user( self::$user );
    10571133
    1058                 $request = new WP_REST_Request( 'GET', '/wp/v2/users/me' );
    1059 
     1134                $request  = new WP_REST_Request( 'GET', '/wp/v2/users/me' );
    10601135                $response = rest_get_server()->dispatch( $request );
    10611136                $this->assertEquals( 200, $response->get_status() );
     
    10711146        public function test_get_current_user_without_permission() {
    10721147                wp_set_current_user( 0 );
     1148
    10731149                $request  = new WP_REST_Request( 'GET', '/wp/v2/users/me' );
    10741150                $response = rest_get_server()->dispatch( $request );
    1075 
    10761151                $this->assertErrorResponse( 'rest_not_logged_in', $response, 401 );
    10771152        }
     
    10791154        public function test_create_item() {
    10801155                $this->allow_user_to_manage_multisite();
     1156
    10811157                wp_set_current_user( self::$user );
    10821158
     
    10961172                $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
    10971173                $request->set_body_params( $params );
    1098 
    1099                 $response = rest_get_server()->dispatch( $request );
    1100                 $data     = $response->get_data();
     1174                $response = rest_get_server()->dispatch( $request );
     1175
     1176                $data = $response->get_data();
    11011177                $this->assertEquals( 'http://example.com', $data['url'] );
    11021178                $this->assertEquals( array( 'editor' ), $data['roles'] );
     
    11061182        public function test_create_item_invalid_username() {
    11071183                $this->allow_user_to_manage_multisite();
     1184
    11081185                wp_set_current_user( self::$user );
    11091186
     
    11281205                $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
    11291206                $request->set_body_params( $params );
    1130 
    11311207                $response = rest_get_server()->dispatch( $request );
    11321208                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    11331209
    11341210                $data = $response->get_data();
     1211
    11351212                if ( is_multisite() ) {
    11361213                        $this->assertInternalType( 'array', $data['additional_errors'] );
     
    11531230        public function test_create_item_illegal_username() {
    11541231                $this->allow_user_to_manage_multisite();
     1232
    11551233                wp_set_current_user( self::$user );
    11561234
     
    11721250                $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
    11731251                $request->set_body_params( $params );
    1174 
    11751252                $response = rest_get_server()->dispatch( $request );
    11761253
     
    13201397        public function test_json_create_user() {
    13211398                $this->allow_user_to_manage_multisite();
     1399
    13221400                wp_set_current_user( self::$user );
    13231401
     
    13311409                $request->add_header( 'content-type', 'application/json' );
    13321410                $request->set_body( wp_json_encode( $params ) );
    1333 
    1334                 $response = rest_get_server()->dispatch( $request );
     1411                $response = rest_get_server()->dispatch( $request );
     1412
    13351413                $this->check_add_edit_user_response( $response );
    13361414        }
     
    13551433        public function test_create_user_invalid_id() {
    13561434                $this->allow_user_to_manage_multisite();
     1435
    13571436                wp_set_current_user( self::$user );
    13581437
     
    13741453        public function test_create_user_invalid_email() {
    13751454                $this->allow_user_to_manage_multisite();
     1455
    13761456                wp_set_current_user( self::$user );
    13771457
     
    13921472        public function test_create_user_invalid_role() {
    13931473                $this->allow_user_to_manage_multisite();
     1474
    13941475                wp_set_current_user( self::$user );
    13951476
     
    14201501                        )
    14211502                );
    1422                 $this->allow_user_to_manage_multisite();
     1503
     1504                $this->allow_user_to_manage_multisite();
     1505
    14231506                wp_set_current_user( self::$user );
    14241507
     
    14351518                $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
    14361519                $request->set_body_params( $_POST );
    1437 
    1438                 $response = rest_get_server()->dispatch( $request );
     1520                $response = rest_get_server()->dispatch( $request );
     1521
    14391522                $this->check_add_edit_user_response( $response, true );
    14401523
     
    14561539        public function test_update_item_no_change() {
    14571540                $this->allow_user_to_manage_multisite();
    1458                 wp_set_current_user( self::$user );
     1541
     1542                wp_set_current_user( self::$user );
     1543
    14591544                $user = get_userdata( self::$editor );
    14601545
     
    14841569                        )
    14851570                );
    1486                 $this->allow_user_to_manage_multisite();
     1571
     1572                $this->allow_user_to_manage_multisite();
     1573
    14871574                wp_set_current_user( self::$user );
    14881575
     
    15401627                        )
    15411628                );
    1542                 $this->allow_user_to_manage_multisite();
     1629
     1630                $this->allow_user_to_manage_multisite();
     1631
    15431632                wp_set_current_user( self::$user );
    15441633
     
    15571646                        )
    15581647                );
    1559                 $this->allow_user_to_manage_multisite();
     1648
     1649                $this->allow_user_to_manage_multisite();
     1650
    15601651                wp_set_current_user( self::$user );
    15611652
     
    15801671                        )
    15811672                );
    1582                 $this->allow_user_to_manage_multisite();
     1673
     1674                $this->allow_user_to_manage_multisite();
     1675
    15831676                wp_set_current_user( self::$user );
    15841677
     
    16071700                        )
    16081701                );
    1609                 $this->allow_user_to_manage_multisite();
     1702
     1703                $this->allow_user_to_manage_multisite();
     1704
    16101705                wp_set_current_user( self::$user );
    16111706
     
    16301725                        )
    16311726                );
    1632                 $this->allow_user_to_manage_multisite();
     1727
     1728                $this->allow_user_to_manage_multisite();
     1729
    16331730                wp_set_current_user( self::$user );
    16341731
     
    16501747                        )
    16511748                );
    1652                 $this->allow_user_to_manage_multisite();
     1749
     1750                $this->allow_user_to_manage_multisite();
     1751
    16531752                wp_set_current_user( self::$user );
    16541753
     
    16871786
    16881787                wp_set_current_user( self::$user );
     1788
    16891789                $this->allow_user_to_manage_multisite();
    16901790
     
    17071807
    17081808                wp_set_current_user( self::$user );
     1809
    17091810                $this->allow_user_to_manage_multisite();
    17101811
     
    18101911        public function test_update_user_role_invalid_role() {
    18111912                wp_set_current_user( self::$user );
     1913
    18121914                $this->allow_user_to_manage_multisite();
    18131915
     
    18591961        public function test_update_user_invalid_id() {
    18601962                $this->allow_user_to_manage_multisite();
     1963
    18611964                wp_set_current_user( self::$user );
    18621965
    18631966                $params = array(
    1864                         'id'       => '156',
     1967                        'id'       => '0',
    18651968                        'username' => 'lisasimpson',
    18661969                        'password' => 'DavidHasselhoff',
     
    18871990
    18881991                wp_set_current_user( self::$editor );
     1992
    18891993                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) );
    18901994                $request->set_param( 'roles', array( 'editor' ) );
     
    19042008
    19052009                wp_set_current_user( self::$user );
     2010
    19062011                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) );
    19072012                $request->set_param( 'roles', array( 'editor' ) );
     
    19242029
    19252030                wp_set_current_user( self::$user );
     2031
    19262032                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) );
    19272033                $request->set_param( 'roles', array( 'editor' ) );
     
    19462052        public function test_update_item_invalid_password() {
    19472053                $this->allow_user_to_manage_multisite();
     2054
    19482055                wp_set_current_user( self::$user );
    19492056
    19502057                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', self::$editor ) );
    1951 
    19522058                $request->set_param( 'password', 'no\\backslashes\\allowed' );
    19532059                $response = rest_get_server()->dispatch( $request );
     
    20352141        public function test_user_roundtrip_as_editor() {
    20362142                wp_set_current_user( self::$editor );
     2143
    20372144                $this->assertEquals( ! is_multisite(), current_user_can( 'unfiltered_html' ) );
    20382145                $this->verify_user_roundtrip(
     
    20612168        public function test_user_roundtrip_as_editor_html() {
    20622169                wp_set_current_user( self::$editor );
     2170
    20632171                if ( is_multisite() ) {
    20642172                        $this->assertFalse( current_user_can( 'unfiltered_html' ) );
     
    21122220        public function test_user_roundtrip_as_superadmin() {
    21132221                wp_set_current_user( self::$superadmin );
     2222
    21142223                $this->assertTrue( current_user_can( 'unfiltered_html' ) );
    21152224                $valid_username = is_multisite() ? 'noinvalidcharshere' : 'no-invalid-chars-here';
     
    21402249        public function test_user_roundtrip_as_superadmin_html() {
    21412250                wp_set_current_user( self::$superadmin );
     2251
    21422252                $this->assertTrue( current_user_can( 'unfiltered_html' ) );
    21432253                $valid_username = is_multisite() ? 'noinvalidcharshere' : 'no-invalid-chars-here';
     
    21702280
    21712281                $this->allow_user_to_manage_multisite();
    2172                 wp_set_current_user( self::$user );
    2173 
    2174                 $userdata = get_userdata( $user_id ); // cache for later
     2282
     2283                wp_set_current_user( self::$user );
     2284
     2285                $userdata = get_userdata( $user_id ); // Cache for later.
    21752286                $request  = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) );
    21762287                $request->set_param( 'force', true );
     
    21942305
    21952306                $this->allow_user_to_manage_multisite();
    2196                 wp_set_current_user( self::$user );
    2197 
    2198                 $userdata = get_userdata( $user_id ); // cache for later
     2307
     2308                wp_set_current_user( self::$user );
     2309
     2310                $userdata = get_userdata( $user_id ); // Cache for later.
    21992311
    22002312                $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) );
     
    22142326                $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
    22152327
    2216                 // Ensure the user still exists
     2328                // Ensure the user still exists.
    22172329                $user = get_user_by( 'id', $user_id );
    22182330                $this->assertNotEmpty( $user );
     
    22852397
    22862398                $this->allow_user_to_manage_multisite();
     2399
    22872400                wp_set_current_user( self::$editor );
    22882401
     
    23042417        public function test_delete_user_invalid_id() {
    23052418                $this->allow_user_to_manage_multisite();
     2419
    23062420                wp_set_current_user( self::$user );
    23072421
     
    23172431                $this->allow_user_to_manage_multisite();
    23182432
    2319                 // Test with a new user, to avoid any complications
     2433                // Test with a new user, to avoid any complications.
    23202434                $user_id     = $this->factory->user->create();
    23212435                $reassign_id = $this->factory->user->create();
     
    23262440                );
    23272441
    2328                 // Sanity check to ensure the factory created the post correctly
     2442                // Sanity check to ensure the factory created the post correctly.
    23292443                $post = get_post( $test_post );
    23302444                $this->assertEquals( $user_id, $post->post_author );
    23312445
    2332                 // Delete our test user, and reassign to the new author
    2333                 wp_set_current_user( self::$user );
     2446                wp_set_current_user( self::$user );
     2447
     2448                // Delete our test user, and reassign to the new author.
    23342449                $request          = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) );
    23352450                $request['force'] = true;
     
    23542469
    23552470                $this->allow_user_to_manage_multisite();
     2471
    23562472                wp_set_current_user( self::$user );
    23572473
     
    23742490
    23752491                $this->allow_user_to_manage_multisite();
     2492
    23762493                wp_set_current_user( self::$user );
    23772494
     
    23882505
    23892506                $this->allow_user_to_manage_multisite();
     2507
    23902508                wp_set_current_user( self::$user );
    23912509
     
    24152533
    24162534                $this->allow_user_to_manage_multisite();
     2535
    24172536                wp_set_current_user( self::$user );
    24182537
     
    24422561
    24432562                $this->allow_user_to_manage_multisite();
     2563
    24442564                wp_set_current_user( self::$user );
    24452565
     
    24692589
    24702590                $this->allow_user_to_manage_multisite();
     2591
    24712592                wp_set_current_user( self::$user );
    24722593
     
    25572678                );
    25582679
    2559                 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/users' );
    2560 
     2680                $request  = new WP_REST_Request( 'OPTIONS', '/wp/v2/users' );
    25612681                $response = rest_get_server()->dispatch( $request );
    25622682                $data     = $response->get_data();
     
    25662686
    25672687                wp_set_current_user( 1 );
     2688
    25682689                if ( is_multisite() ) {
    25692690                        $current_user = wp_get_current_user( 1 );
     
    25712692                }
    25722693
    2573                 $request = new WP_REST_Request( 'GET', '/wp/v2/users/1' );
    2574 
     2694                $request  = new WP_REST_Request( 'GET', '/wp/v2/users/1' );
    25752695                $response = rest_get_server()->dispatch( $request );
    25762696                $this->assertArrayHasKey( 'my_custom_int', $response->data );
     
    25822702                        )
    25832703                );
    2584 
    25852704                $response = rest_get_server()->dispatch( $request );
    25862705                $this->assertEquals( 123, get_user_meta( 1, 'my_custom_int', true ) );
     
    25952714                        )
    25962715                );
    2597 
    2598                 $response = rest_get_server()->dispatch( $request );
    2599 
     2716                $response = rest_get_server()->dispatch( $request );
    26002717                $this->assertEquals( 123, $response->data['my_custom_int'] );
    26012718
     
    26232740
    26242741                wp_set_current_user( 1 );
     2742
    26252743                if ( is_multisite() ) {
    26262744                        $current_user = wp_get_current_user( 1 );
     
    26352753                        )
    26362754                );
    2637 
    26382755                $response = rest_get_server()->dispatch( $request );
    26392756
     
    26582775
    26592776                wp_set_current_user( self::$user );
    2660                 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) );
    2661 
     2777
     2778                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) );
    26622779                $response = rest_get_server()->dispatch( $request );
    26632780                $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 );
     
    26782795
    26792796                wp_set_current_user( self::$superadmin );
    2680                 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) );
    2681 
     2797
     2798                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', $user_id ) );
    26822799                $response = rest_get_server()->dispatch( $request );
    26832800                $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 );
     
    26982815
    26992816                wp_set_current_user( self::$user );
     2817
    27002818                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) );
    27012819                $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
    27022820                $request->set_body_params( array( 'first_name' => 'New Name' ) );
    2703 
    27042821                $response = rest_get_server()->dispatch( $request );
    27052822                $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 );
     
    27202837
    27212838                wp_set_current_user( self::$superadmin );
     2839
    27222840                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) );
    27232841                $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
    27242842                $request->set_body_params( array( 'first_name' => 'New Name' ) );
    2725 
    27262843                $response = rest_get_server()->dispatch( $request );
    27272844                $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 );
     
    27422859
    27432860                wp_set_current_user( self::$user );
     2861
    27442862                $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) );
    27452863                $request->set_param( 'force', true );
    27462864                $request->set_param( 'reassign', false );
    2747 
    27482865                $response = rest_get_server()->dispatch( $request );
    27492866                $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 );
     
    27642881
    27652882                wp_set_current_user( self::$superadmin );
     2883
    27662884                $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) );
    27672885                $request->set_param( 'force', true );
    27682886                $request->set_param( 'reassign', false );
    2769 
    27702887                $response = rest_get_server()->dispatch( $request );
    27712888                $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 );
Note: See TracChangeset for help on using the changeset viewer.