WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/21/2018 04:24:30 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Tests: Replace use of $this->server with rest_get_server() for better memory recycling.

Props danielbachhuber.
Fixes #41641.

File:
1 edited

Legend:

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

    r42354 r42724  
    3434
    3535    public function test_register_routes() {
    36         $routes = $this->server->get_routes();
     36        $routes = rest_get_server()->get_routes();
    3737        $this->assertArrayHasKey( '/wp/v2/categories', $routes );
    3838        $this->assertArrayHasKey( '/wp/v2/categories/(?P<id>[\d]+)', $routes );
     
    4242        // Collection
    4343        $request  = new WP_REST_Request( 'OPTIONS', '/wp/v2/categories' );
    44         $response = $this->server->dispatch( $request );
     44        $response = rest_get_server()->dispatch( $request );
    4545        $data     = $response->get_data();
    4646        $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
     
    4949        $category1 = $this->factory->category->create( array( 'name' => 'Season 5' ) );
    5050        $request   = new WP_REST_Request( 'OPTIONS', '/wp/v2/categories/' . $category1 );
    51         $response  = $this->server->dispatch( $request );
     51        $response  = rest_get_server()->dispatch( $request );
    5252        $data      = $response->get_data();
    5353        $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
     
    5757    public function test_registered_query_params() {
    5858        $request  = new WP_REST_Request( 'OPTIONS', '/wp/v2/categories' );
    59         $response = $this->server->dispatch( $request );
     59        $response = rest_get_server()->dispatch( $request );
    6060        $data     = $response->get_data();
    6161        $keys     = array_keys( $data['endpoints'][0]['args'] );
     
    8181    public function test_get_items() {
    8282        $request  = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    83         $response = $this->server->dispatch( $request );
     83        $response = rest_get_server()->dispatch( $request );
    8484        $this->check_get_taxonomy_terms_response( $response );
    8585    }
     
    8989        $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    9090        $request->set_param( 'context', 'edit' );
    91         $response = $this->server->dispatch( $request );
     91        $response = rest_get_server()->dispatch( $request );
    9292        $this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
    9393    }
     
    100100        $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    101101        $request->set_param( 'hide_empty', true );
    102         $response = $this->server->dispatch( $request );
     102        $response = rest_get_server()->dispatch( $request );
    103103        $data     = $response->get_data();
    104104        $this->assertEquals( 2, count( $data ) );
     
    108108        // Confirm the empty category "Uncategorized" category appears.
    109109        $request->set_param( 'hide_empty', 'false' );
    110         $response = $this->server->dispatch( $request );
     110        $response = rest_get_server()->dispatch( $request );
    111111        $data     = $response->get_data();
    112112        $this->assertEquals( 3, count( $data ) );
     
    130130        $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    131131        $request->set_param( 'parent', 0 );
    132         $response = $this->server->dispatch( $request );
     132        $response = rest_get_server()->dispatch( $request );
    133133
    134134        $this->assertEquals( 200, $response->get_status() );
     
    160160        $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    161161        $request->set_param( 'parent', '0' );
    162         $response = $this->server->dispatch( $request );
     162        $response = rest_get_server()->dispatch( $request );
    163163
    164164        $this->assertEquals( 200, $response->get_status() );
     
    178178        $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    179179        $request->set_param( 'parent', $parent1 );
    180         $response = $this->server->dispatch( $request );
     180        $response = rest_get_server()->dispatch( $request );
    181181
    182182        $this->assertEquals( 200, $response->get_status() );
     
    189189        $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    190190        $request->set_param( 'page', 0 );
    191         $response = $this->server->dispatch( $request );
     191        $response = rest_get_server()->dispatch( $request );
    192192        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    193193        $data        = $response->get_data();
     
    203203        // Orderby=>asc
    204204        $request->set_param( 'include', array( $id3, $id1 ) );
    205         $response = $this->server->dispatch( $request );
     205        $response = rest_get_server()->dispatch( $request );
    206206        $data     = $response->get_data();
    207207        $this->assertEquals( 2, count( $data ) );
     
    209209        // Orderby=>include
    210210        $request->set_param( 'orderby', 'include' );
    211         $response = $this->server->dispatch( $request );
     211        $response = rest_get_server()->dispatch( $request );
    212212        $data     = $response->get_data();
    213213        $this->assertEquals( 2, count( $data ) );
     
    219219        $id2      = $this->factory->category->create();
    220220        $request  = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    221         $response = $this->server->dispatch( $request );
     221        $response = rest_get_server()->dispatch( $request );
    222222        $data     = $response->get_data();
    223223        $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
    224224        $this->assertTrue( in_array( $id2, wp_list_pluck( $data, 'id' ), true ) );
    225225        $request->set_param( 'exclude', array( $id2 ) );
    226         $response = $this->server->dispatch( $request );
     226        $response = rest_get_server()->dispatch( $request );
    227227        $data     = $response->get_data();
    228228        $this->assertTrue( in_array( $id1, wp_list_pluck( $data, 'id' ), true ) );
     
    243243        $request->set_param( 'order', 'desc' );
    244244        $request->set_param( 'per_page', 1 );
    245         $response = $this->server->dispatch( $request );
     245        $response = rest_get_server()->dispatch( $request );
    246246        $this->assertEquals( 200, $response->get_status() );
    247247        $data = $response->get_data();
     
    252252        $request->set_param( 'order', 'asc' );
    253253        $request->set_param( 'per_page', 2 );
    254         $response = $this->server->dispatch( $request );
     254        $response = rest_get_server()->dispatch( $request );
    255255        $this->assertEquals( 200, $response->get_status() );
    256256        $data = $response->get_data();
     
    265265        // defaults to orderby=name, order=asc
    266266        $request  = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    267         $response = $this->server->dispatch( $request );
     267        $response = rest_get_server()->dispatch( $request );
    268268        $this->assertEquals( 200, $response->get_status() );
    269269        $data = $response->get_data();
     
    275275        $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    276276        $request->set_param( 'orderby', 'id' );
    277         $response = $this->server->dispatch( $request );
     277        $response = rest_get_server()->dispatch( $request );
    278278        $this->assertEquals( 200, $response->get_status() );
    279279        $data = $response->get_data();
     
    286286        $request->set_param( 'orderby', 'id' );
    287287        $request->set_param( 'order', 'desc' );
    288         $response = $this->server->dispatch( $request );
     288        $response = rest_get_server()->dispatch( $request );
    289289        $data     = $response->get_data();
    290290        $this->assertEquals( 200, $response->get_status() );
     
    302302        $request->set_param( 'orderby', 'include_slugs' );
    303303        $request->set_param( 'slug', array( 'taco', 'burrito', 'chalupa' ) );
    304         $response = $this->server->dispatch( $request );
     304        $response = rest_get_server()->dispatch( $request );
    305305        $data     = $response->get_data();
    306306        $this->assertEquals( 200, $response->get_status() );
     
    340340        $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    341341        $request->set_param( 'post', $post_id );
    342         $response = $this->server->dispatch( $request );
     342        $response = rest_get_server()->dispatch( $request );
    343343        $this->assertEquals( 200, $response->get_status() );
    344344
     
    358358        $request->set_param( 'post', $post_id );
    359359        $request->set_param( 'orderby', 'description' );
    360         $response = $this->server->dispatch( $request );
     360        $response = rest_get_server()->dispatch( $request );
    361361        $this->assertEquals( 200, $response->get_status() );
    362362
     
    368368        // Flip the order
    369369        $request->set_param( 'order', 'desc' );
    370         $response = $this->server->dispatch( $request );
     370        $response = rest_get_server()->dispatch( $request );
    371371        $this->assertEquals( 200, $response->get_status() );
    372372
     
    383383        $request->set_param( 'post', $post_id );
    384384        $request->set_param( 'orderby', 'id' );
    385         $response = $this->server->dispatch( $request );
     385        $response = rest_get_server()->dispatch( $request );
    386386        $this->assertEquals( 200, $response->get_status() );
    387387
     
    419419        $request = new WP_REST_Request( 'GET', '/wp/v2/batman' );
    420420        $request->set_param( 'post', $post_id );
    421         $response = $this->server->dispatch( $request );
     421        $response = rest_get_server()->dispatch( $request );
    422422        $this->assertEquals( 200, $response->get_status() );
    423423
     
    436436        $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    437437        $request->set_param( 'search', 'App' );
    438         $response = $this->server->dispatch( $request );
     438        $response = rest_get_server()->dispatch( $request );
    439439        $this->assertEquals( 200, $response->get_status() );
    440440        $data = $response->get_data();
     
    443443        $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    444444        $request->set_param( 'search', 'Garbage' );
    445         $response = $this->server->dispatch( $request );
     445        $response = rest_get_server()->dispatch( $request );
    446446        $this->assertEquals( 200, $response->get_status() );
    447447        $data = $response->get_data();
     
    454454        $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    455455        $request->set_param( 'slug', 'apple' );
    456         $response = $this->server->dispatch( $request );
     456        $response = rest_get_server()->dispatch( $request );
    457457        $this->assertEquals( 200, $response->get_status() );
    458458        $data = $response->get_data();
     
    471471        $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    472472        $request->set_param( 'parent', $category1 );
    473         $response = $this->server->dispatch( $request );
     473        $response = rest_get_server()->dispatch( $request );
    474474        $data     = $response->get_data();
    475475        $this->assertEquals( 1, count( $data ) );
     
    487487        $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    488488        $request->set_param( 'parent', 'invalid-parent' );
    489         $response = $this->server->dispatch( $request );
     489        $response = rest_get_server()->dispatch( $request );
    490490        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    491491    }
     
    507507
    508508        $request  = new WP_REST_Request( 'GET', '/wp/v2/terms/robin' );
    509         $response = $this->server->dispatch( $request );
     509        $response = rest_get_server()->dispatch( $request );
    510510        $this->assertErrorResponse( 'rest_no_route', $response, 404 );
    511511    }
     
    513513    public function test_get_terms_invalid_taxonomy() {
    514514        $request  = new WP_REST_Request( 'GET', '/wp/v2/invalid-taxonomy' );
    515         $response = $this->server->dispatch( $request );
     515        $response = rest_get_server()->dispatch( $request );
    516516        $this->assertErrorResponse( 'rest_no_route', $response, 404 );
    517517    }
     
    527527        }
    528528        $request  = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    529         $response = $this->server->dispatch( $request );
     529        $response = rest_get_server()->dispatch( $request );
    530530        $headers  = $response->get_headers();
    531531        $this->assertEquals( 50, $headers['X-WP-Total'] );
     
    547547        $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    548548        $request->set_param( 'page', 3 );
    549         $response = $this->server->dispatch( $request );
     549        $response = rest_get_server()->dispatch( $request );
    550550        $headers  = $response->get_headers();
    551551        $this->assertEquals( 51, $headers['X-WP-Total'] );
     
    567567        $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    568568        $request->set_param( 'page', 6 );
    569         $response = $this->server->dispatch( $request );
     569        $response = rest_get_server()->dispatch( $request );
    570570        $headers  = $response->get_headers();
    571571        $this->assertEquals( 51, $headers['X-WP-Total'] );
     
    582582        $request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
    583583        $request->set_param( 'page', 8 );
    584         $response = $this->server->dispatch( $request );
     584        $response = rest_get_server()->dispatch( $request );
    585585        $headers  = $response->get_headers();
    586586        $this->assertEquals( 51, $headers['X-WP-Total'] );
     
    608608        $request->set_param( 'page', 1 );
    609609        $request->set_param( 'per_page', 100 );
    610         $response = $this->server->dispatch( $request );
     610        $response = rest_get_server()->dispatch( $request );
    611611        $headers  = $response->get_headers();
    612612        $this->assertEquals( 18, $headers['X-WP-Total'] );
     
    616616        $request->set_param( 'page', 2 );
    617617        $request->set_param( 'per_page', 100 );
    618         $response = $this->server->dispatch( $request );
     618        $response = rest_get_server()->dispatch( $request );
    619619        $headers  = $response->get_headers();
    620620        $this->assertEquals( 18, $headers['X-WP-Total'] );
     
    625625    public function test_get_item() {
    626626        $request  = new WP_REST_Request( 'GET', '/wp/v2/categories/1' );
    627         $response = $this->server->dispatch( $request );
     627        $response = rest_get_server()->dispatch( $request );
    628628        $this->check_get_taxonomy_term_response( $response );
    629629    }
     
    631631    public function test_get_term_invalid_taxonomy() {
    632632        $request  = new WP_REST_Request( 'GET', '/wp/v2/invalid-taxonomy/1' );
    633         $response = $this->server->dispatch( $request );
     633        $response = rest_get_server()->dispatch( $request );
    634634        $this->assertErrorResponse( 'rest_no_route', $response, 404 );
    635635    }
     
    637637    public function test_get_term_invalid_term() {
    638638        $request  = new WP_REST_Request( 'GET', '/wp/v2/categories/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    639         $response = $this->server->dispatch( $request );
     639        $response = rest_get_server()->dispatch( $request );
    640640        $this->assertErrorResponse( 'rest_term_invalid', $response, 404 );
    641641    }
     
    645645        $request = new WP_REST_Request( 'GET', '/wp/v2/categories/1' );
    646646        $request->set_param( 'context', 'edit' );
    647         $response = $this->server->dispatch( $request );
     647        $response = rest_get_server()->dispatch( $request );
    648648        $this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
    649649    }
     
    659659
    660660        $request  = new WP_REST_Request( 'GET', '/wp/v2/terms/robin/' . $term1 );
    661         $response = $this->server->dispatch( $request );
     661        $response = rest_get_server()->dispatch( $request );
    662662        $this->assertErrorResponse( 'rest_no_route', $response, 404 );
    663663    }
     
    672672        );
    673673        $request  = new WP_REST_Request( 'GET', '/wp/v2/categories/' . $term1 );
    674         $response = $this->server->dispatch( $request );
     674        $response = rest_get_server()->dispatch( $request );
    675675        $this->assertErrorResponse( 'rest_term_invalid', $response, 404 );
    676676    }
     
    682682        $request->set_param( 'description', 'This term is so awesome.' );
    683683        $request->set_param( 'slug', 'so-awesome' );
    684         $response = $this->server->dispatch( $request );
     684        $response = rest_get_server()->dispatch( $request );
    685685        $this->assertEquals( 201, $response->get_status() );
    686686        $headers = $response->get_headers();
     
    702702        $request->set_param( 'name', 'Existing' );
    703703
    704         $response = $this->server->dispatch( $request );
     704        $response = rest_get_server()->dispatch( $request );
    705705        $this->assertEquals( 400, $response->get_status() );
    706706        $data = $response->get_data();
     
    715715        $request = new WP_REST_Request( 'POST', '/wp/v2/invalid-taxonomy' );
    716716        $request->set_param( 'name', 'Invalid Taxonomy' );
    717         $response = $this->server->dispatch( $request );
     717        $response = rest_get_server()->dispatch( $request );
    718718        $this->assertErrorResponse( 'rest_no_route', $response, 404 );
    719719    }
     
    723723        $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
    724724        $request->set_param( 'name', 'Incorrect permissions' );
    725         $response = $this->server->dispatch( $request );
     725        $response = rest_get_server()->dispatch( $request );
    726726        $this->assertErrorResponse( 'rest_cannot_create', $response, 403 );
    727727    }
     
    730730        wp_set_current_user( self::$administrator );
    731731        $request  = new WP_REST_Request( 'POST', '/wp/v2/categories' );
    732         $response = $this->server->dispatch( $request );
     732        $response = rest_get_server()->dispatch( $request );
    733733        $this->assertErrorResponse( 'rest_missing_callback_param', $response, 400 );
    734734    }
     
    740740        $request->set_param( 'name', 'My Awesome Term' );
    741741        $request->set_param( 'parent', $parent['term_id'] );
    742         $response = $this->server->dispatch( $request );
     742        $response = rest_get_server()->dispatch( $request );
    743743        $this->assertEquals( 201, $response->get_status() );
    744744        $data = $response->get_data();
     
    753753        $request->set_param( 'name', 'My Awesome Term' );
    754754        $request->set_param( 'parent', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    755         $response = $this->server->dispatch( $request );
     755        $response = rest_get_server()->dispatch( $request );
    756756        $this->assertErrorResponse( 'rest_term_invalid', $response, 400 );
    757757    }
     
    769769        $request->set_param( 'description', 'New Description' );
    770770        $request->set_param( 'slug', 'new-slug' );
    771         $response = $this->server->dispatch( $request );
     771        $response = rest_get_server()->dispatch( $request );
    772772        $this->assertEquals( 200, $response->get_status() );
    773773        $data = $response->get_data();
     
    781781        $request = new WP_REST_Request( 'POST', '/wp/v2/invalid-taxonomy/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    782782        $request->set_param( 'name', 'Invalid Taxonomy' );
    783         $response = $this->server->dispatch( $request );
     783        $response = rest_get_server()->dispatch( $request );
    784784        $this->assertErrorResponse( 'rest_no_route', $response, 404 );
    785785    }
     
    789789        $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    790790        $request->set_param( 'name', 'Invalid Term' );
    791         $response = $this->server->dispatch( $request );
     791        $response = rest_get_server()->dispatch( $request );
    792792        $this->assertErrorResponse( 'rest_term_invalid', $response, 404 );
    793793    }
     
    798798        $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id );
    799799        $request->set_param( 'name', 'Incorrect permissions' );
    800         $response = $this->server->dispatch( $request );
     800        $response = rest_get_server()->dispatch( $request );
    801801        $this->assertErrorResponse( 'rest_cannot_update', $response, 403 );
    802802    }
     
    809809        $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id );
    810810        $request->set_param( 'parent', $parent->term_id );
    811         $response = $this->server->dispatch( $request );
     811        $response = rest_get_server()->dispatch( $request );
    812812        $this->assertEquals( 200, $response->get_status() );
    813813
     
    822822        $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id );
    823823        $request->set_param( 'parent', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    824         $response = $this->server->dispatch( $request );
     824        $response = rest_get_server()->dispatch( $request );
    825825        $this->assertErrorResponse( 'rest_term_invalid', $response, 400 );
    826826    }
     
    831831        $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id );
    832832        $request->set_param( 'force', true );
    833         $response = $this->server->dispatch( $request );
     833        $response = rest_get_server()->dispatch( $request );
    834834        $this->assertEquals( 200, $response->get_status() );
    835835        $data = $response->get_data();
     
    843843
    844844        $request  = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id );
    845         $response = $this->server->dispatch( $request );
     845        $response = rest_get_server()->dispatch( $request );
    846846        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
    847847
    848848        $request->set_param( 'force', 'false' );
    849         $response = $this->server->dispatch( $request );
     849        $response = rest_get_server()->dispatch( $request );
    850850        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
    851851    }
     
    854854        wp_set_current_user( self::$administrator );
    855855        $request  = new WP_REST_Request( 'DELETE', '/wp/v2/invalid-taxonomy/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    856         $response = $this->server->dispatch( $request );
     856        $response = rest_get_server()->dispatch( $request );
    857857        $this->assertErrorResponse( 'rest_no_route', $response, 404 );
    858858    }
     
    861861        wp_set_current_user( self::$administrator );
    862862        $request  = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    863         $response = $this->server->dispatch( $request );
     863        $response = rest_get_server()->dispatch( $request );
    864864        $this->assertErrorResponse( 'rest_term_invalid', $response, 404 );
    865865    }
     
    869869        $term     = get_term_by( 'id', $this->factory->category->create(), 'category' );
    870870        $request  = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id );
    871         $response = $this->server->dispatch( $request );
     871        $response = rest_get_server()->dispatch( $request );
    872872        $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
    873873    }
     
    877877
    878878        $request  = new WP_REST_Request( 'GET', '/wp/v2/categories/1' );
    879         $response = $this->server->dispatch( $request );
     879        $response = rest_get_server()->dispatch( $request );
    880880        $data     = $response->get_data();
    881881
     
    892892
    893893        $request  = new WP_REST_Request( 'GET', '/wp/v2/categories/' . $child );
    894         $response = $this->server->dispatch( $request );
     894        $response = rest_get_server()->dispatch( $request );
    895895        $data     = $response->get_data();
    896896
     
    905905    public function test_get_item_schema() {
    906906        $request    = new WP_REST_Request( 'OPTIONS', '/wp/v2/categories' );
    907         $response   = $this->server->dispatch( $request );
     907        $response   = rest_get_server()->dispatch( $request );
    908908        $data       = $response->get_data();
    909909        $properties = $data['schema']['properties'];
     
    939939        $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/categories' );
    940940
    941         $response = $this->server->dispatch( $request );
     941        $response = rest_get_server()->dispatch( $request );
    942942        $data     = $response->get_data();
    943943        $this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] );
     
    947947        $request     = new WP_REST_Request( 'GET', '/wp/v2/categories/' . $category_id );
    948948
    949         $response = $this->server->dispatch( $request );
     949        $response = rest_get_server()->dispatch( $request );
    950950        $this->assertArrayHasKey( 'my_custom_int', $response->data );
    951951
Note: See TracChangeset for help on using the changeset viewer.