Make WordPress Core


Ignore:
Timestamp:
02/21/2018 04:24:30 PM (7 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-pages-controller.php

    r42343 r42724  
    3838
    3939    public function test_register_routes() {
    40         $routes = $this->server->get_routes();
     40        $routes = rest_get_server()->get_routes();
    4141        $this->assertArrayHasKey( '/wp/v2/pages', $routes );
    4242        $this->assertCount( 2, $routes['/wp/v2/pages'] );
     
    4848        // Collection
    4949        $request  = new WP_REST_Request( 'OPTIONS', '/wp/v2/pages' );
    50         $response = $this->server->dispatch( $request );
     50        $response = rest_get_server()->dispatch( $request );
    5151        $data     = $response->get_data();
    5252        $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
     
    5555        $page_id  = $this->factory->post->create( array( 'post_type' => 'page' ) );
    5656        $request  = new WP_REST_Request( 'OPTIONS', '/wp/v2/pages/' . $page_id );
    57         $response = $this->server->dispatch( $request );
     57        $response = rest_get_server()->dispatch( $request );
    5858        $data     = $response->get_data();
    5959        $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
     
    6363    public function test_registered_query_params() {
    6464        $request  = new WP_REST_Request( 'OPTIONS', '/wp/v2/pages' );
    65         $response = $this->server->dispatch( $request );
     65        $response = rest_get_server()->dispatch( $request );
    6666        $data     = $response->get_data();
    6767        $keys     = array_keys( $data['endpoints'][0]['args'] );
     
    105105        );
    106106        $request  = new WP_REST_Request( 'GET', '/wp/v2/pages' );
    107         $response = $this->server->dispatch( $request );
     107        $response = rest_get_server()->dispatch( $request );
    108108        $data     = $response->get_data();
    109109        $this->assertEquals( 1, count( $data ) );
     
    127127        // No parent
    128128        $request  = new WP_REST_Request( 'GET', '/wp/v2/pages' );
    129         $response = $this->server->dispatch( $request );
     129        $response = rest_get_server()->dispatch( $request );
    130130        $data     = $response->get_data();
    131131        $this->assertEquals( 2, count( $data ) );
    132132        // Filter to parent
    133133        $request->set_param( 'parent', $id1 );
    134         $response = $this->server->dispatch( $request );
     134        $response = rest_get_server()->dispatch( $request );
    135135        $data     = $response->get_data();
    136136        $this->assertEquals( 1, count( $data ) );
     
    138138        // Invalid parent should fail
    139139        $request->set_param( 'parent', 'some-slug' );
    140         $response = $this->server->dispatch( $request );
     140        $response = rest_get_server()->dispatch( $request );
    141141        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    142142    }
     
    171171        // No parent
    172172        $request  = new WP_REST_Request( 'GET', '/wp/v2/pages' );
    173         $response = $this->server->dispatch( $request );
     173        $response = rest_get_server()->dispatch( $request );
    174174        $data     = $response->get_data();
    175175        $this->assertEquals( 4, count( $data ) );
    176176        // Filter to parents
    177177        $request->set_param( 'parent', array( $id1, $id3 ) );
    178         $response = $this->server->dispatch( $request );
     178        $response = rest_get_server()->dispatch( $request );
    179179        $data     = $response->get_data();
    180180        $this->assertEquals( 2, count( $data ) );
     
    198198        // No parent
    199199        $request  = new WP_REST_Request( 'GET', '/wp/v2/pages' );
    200         $response = $this->server->dispatch( $request );
     200        $response = rest_get_server()->dispatch( $request );
    201201        $data     = $response->get_data();
    202202        $this->assertEquals( 2, count( $data ) );
    203203        // Filter to parent
    204204        $request->set_param( 'parent_exclude', $id1 );
    205         $response = $this->server->dispatch( $request );
     205        $response = rest_get_server()->dispatch( $request );
    206206        $data     = $response->get_data();
    207207        $this->assertEquals( 1, count( $data ) );
     
    209209        // Invalid parent_exclude should error
    210210        $request->set_param( 'parent_exclude', 'some-slug' );
    211         $response = $this->server->dispatch( $request );
     211        $response = rest_get_server()->dispatch( $request );
    212212        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    213213    }
     
    243243        // No parent
    244244        $request  = new WP_REST_Request( 'GET', '/wp/v2/pages' );
    245         $response = $this->server->dispatch( $request );
     245        $response = rest_get_server()->dispatch( $request );
    246246        $data     = $response->get_data();
    247247        $this->assertEqualSets( array( $id1, $id2, $id3, $id4 ), wp_list_pluck( $data, 'id' ) );
    248248        // Filter to menu_order
    249249        $request->set_param( 'menu_order', 1 );
    250         $response = $this->server->dispatch( $request );
     250        $response = rest_get_server()->dispatch( $request );
    251251        $data     = $response->get_data();
    252252        $this->assertEqualSets( array( $id4 ), wp_list_pluck( $data, 'id' ) );
     
    255255        $request->set_param( 'order', 'asc' );
    256256        $request->set_param( 'orderby', 'menu_order' );
    257         $response = $this->server->dispatch( $request );
     257        $response = rest_get_server()->dispatch( $request );
    258258        $data     = $response->get_data();
    259259        $this->assertEquals( $id1, $data[0]['id'] );
     
    264264        $request = new WP_REST_Request( 'GET', '/wp/v2/pages' );
    265265        $request->set_param( 'menu_order', 'top-first' );
    266         $response = $this->server->dispatch( $request );
     266        $response = rest_get_server()->dispatch( $request );
    267267        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    268268    }
     
    271271        $request = new WP_REST_Request( 'GET', '/wp/v2/pages' );
    272272        $request->set_param( 'per_page', 0 );
    273         $response = $this->server->dispatch( $request );
     273        $response = rest_get_server()->dispatch( $request );
    274274        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    275275        $data = $response->get_data();
     
    278278        $this->assertContains( 'per_page must be between 1 (inclusive) and 100 (inclusive)', $first_error );
    279279        $request->set_param( 'per_page', 101 );
    280         $response = $this->server->dispatch( $request );
     280        $response = rest_get_server()->dispatch( $request );
    281281        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    282282        $data        = $response->get_data();
     
    302302        $request  = new WP_REST_Request( 'GET', '/wp/v2/pages' );
    303303        $request->set_param( 'status', 'draft' );
    304         $response = $this->server->dispatch( $request );
     304        $response = rest_get_server()->dispatch( $request );
    305305        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    306306
    307307        // But they are accessible to authorized users
    308308        wp_set_current_user( self::$editor_id );
    309         $response = $this->server->dispatch( $request );
     309        $response = rest_get_server()->dispatch( $request );
    310310        $data     = $response->get_data();
    311311        $this->assertCount( 1, $data );
     
    317317        $request->set_param( 'after', rand_str() );
    318318        $request->set_param( 'before', rand_str() );
    319         $response = $this->server->dispatch( $request );
     319        $response = rest_get_server()->dispatch( $request );
    320320        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    321321    }
     
    343343        $request->set_param( 'after', '2016-01-15T00:00:00Z' );
    344344        $request->set_param( 'before', '2016-01-17T00:00:00Z' );
    345         $response = $this->server->dispatch( $request );
     345        $response = rest_get_server()->dispatch( $request );
    346346        $data     = $response->get_data();
    347347        $this->assertCount( 1, $data );
     
    356356        $post_id  = $this->factory->post->create();
    357357        $request  = new WP_REST_Request( 'GET', '/wp/v2/pages/' . $post_id );
    358         $response = $this->server->dispatch( $request );
     358        $response = rest_get_server()->dispatch( $request );
    359359        $this->assertEquals( 404, $response->get_status() );
    360360    }
     
    374374        );
    375375        $request->set_body_params( $params );
    376         $response = $this->server->dispatch( $request );
     376        $response = rest_get_server()->dispatch( $request );
    377377
    378378        $data     = $response->get_data();
     
    397397        );
    398398        $request->set_body_params( $params );
    399         $response = $this->server->dispatch( $request );
     399        $response = rest_get_server()->dispatch( $request );
    400400
    401401        $this->assertEquals( 201, $response->get_status() );
     
    420420        );
    421421        $request->set_body_params( $params );
    422         $response = $this->server->dispatch( $request );
     422        $response = rest_get_server()->dispatch( $request );
    423423
    424424        $this->assertErrorResponse( 'rest_post_invalid_id', $response, 400 );
     
    440440        $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/pages/%d', $page_id ) );
    441441        $request->set_param( 'force', 'false' );
    442         $response = $this->server->dispatch( $request );
     442        $response = rest_get_server()->dispatch( $request );
    443443
    444444        $this->assertEquals( 200, $response->get_status() );
     
    466466            )
    467467        );
    468         $response = $this->server->dispatch( $request );
     468        $response = rest_get_server()->dispatch( $request );
    469469
    470470        $this->assertEquals( 200, $response->get_status() );
     
    498498            )
    499499        );
    500         $response = $this->server->dispatch( $request );
     500        $response = rest_get_server()->dispatch( $request );
    501501
    502502        $new_data = $response->get_data();
     
    522522            )
    523523        );
    524         $response = $this->server->dispatch( $request );
     524        $response = rest_get_server()->dispatch( $request );
    525525
    526526        $new_data = $response->get_data();
     
    546546            )
    547547        );
    548         $response = $this->server->dispatch( $request );
     548        $response = rest_get_server()->dispatch( $request );
    549549        $new_data = $response->get_data();
    550550        $this->assertEquals( $page_id1, $new_data['parent'] );
     
    570570            )
    571571        );
    572         $response = $this->server->dispatch( $request );
     572        $response = rest_get_server()->dispatch( $request );
    573573        $new_data = $response->get_data();
    574574        $this->assertEquals( 0, $new_data['parent'] );
     
    584584
    585585        $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/pages/%d', $page_id ) );
    586         $response = $this->server->dispatch( $request );
     586        $response = rest_get_server()->dispatch( $request );
    587587
    588588        $data = $response->get_data();
     
    606606        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/pages/%d', $page_id ) );
    607607        $request->set_param( 'password', '$inthebananastand' );
    608         $response = $this->server->dispatch( $request );
     608        $response = rest_get_server()->dispatch( $request );
    609609
    610610        $data = $response->get_data();
     
    626626        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/pages/%d', $page_id ) );
    627627        $request->set_param( 'password', 'wrongpassword' );
    628         $response = $this->server->dispatch( $request );
     628        $response = rest_get_server()->dispatch( $request );
    629629
    630630        $this->assertErrorResponse( 'rest_post_incorrect_password', $response, 403 );
     
    641641        );
    642642        $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/pages/%d', $page_id ) );
    643         $response = $this->server->dispatch( $request );
     643        $response = rest_get_server()->dispatch( $request );
    644644        $data     = $response->get_data();
    645645        $this->assertEquals( '', $data['content']['rendered'] );
     
    651651    public function test_get_item_schema() {
    652652        $request    = new WP_REST_Request( 'OPTIONS', '/wp/v2/pages' );
    653         $response   = $this->server->dispatch( $request );
     653        $response   = rest_get_server()->dispatch( $request );
    654654        $data       = $response->get_data();
    655655        $properties = $data['schema']['properties'];
Note: See TracChangeset for help on using the changeset viewer.