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-attachments-controller.php

    r42423 r42724  
    7777
    7878    public function test_register_routes() {
    79         $routes = $this->server->get_routes();
     79        $routes = rest_get_server()->get_routes();
    8080        $this->assertArrayHasKey( '/wp/v2/media', $routes );
    8181        $this->assertCount( 2, $routes['/wp/v2/media'] );
     
    128128        // Collection
    129129        $request  = new WP_REST_Request( 'OPTIONS', '/wp/v2/media' );
    130         $response = $this->server->dispatch( $request );
     130        $response = rest_get_server()->dispatch( $request );
    131131        $data     = $response->get_data();
    132132        $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
     
    140140        );
    141141        $request       = new WP_REST_Request( 'OPTIONS', '/wp/v2/media/' . $attachment_id );
    142         $response      = $this->server->dispatch( $request );
     142        $response      = rest_get_server()->dispatch( $request );
    143143        $data          = $response->get_data();
    144144        $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
     
    148148    public function test_registered_query_params() {
    149149        $request  = new WP_REST_Request( 'OPTIONS', '/wp/v2/media' );
    150         $response = $this->server->dispatch( $request );
     150        $response = rest_get_server()->dispatch( $request );
    151151        $data     = $response->get_data();
    152152        $keys     = array_keys( $data['endpoints'][0]['args'] );
     
    195195        );
    196196        $request  = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/media/%d', $id1 ) );
    197         $response = $this->server->dispatch( $request );
     197        $response = rest_get_server()->dispatch( $request );
    198198        $data     = $response->get_data();
    199199        $keys     = array_keys( $data['endpoints'][0]['args'] );
     
    225225        );
    226226        $request        = new WP_REST_Request( 'GET', '/wp/v2/media' );
    227         $response       = $this->server->dispatch( $request );
     227        $response       = rest_get_server()->dispatch( $request );
    228228        $data           = $response->get_data();
    229229        $this->assertCount( 2, $data );
     
    259259        );
    260260        $request        = new WP_REST_Request( 'GET', '/wp/v2/media' );
    261         $response       = $this->server->dispatch( $request );
     261        $response       = rest_get_server()->dispatch( $request );
    262262
    263263        $data = $response->get_data();
     
    276276        );
    277277        $request  = new WP_REST_Request( 'GET', '/wp/v2/media' );
    278         $response = $this->server->dispatch( $request );
     278        $response = rest_get_server()->dispatch( $request );
    279279        $data     = $response->get_data();
    280280        $this->assertEquals( $id1, $data[0]['id'] );
    281281        // media_type=video
    282282        $request->set_param( 'media_type', 'video' );
    283         $response = $this->server->dispatch( $request );
     283        $response = rest_get_server()->dispatch( $request );
    284284        $this->assertCount( 0, $response->get_data() );
    285285        // media_type=image
    286286        $request->set_param( 'media_type', 'image' );
    287         $response = $this->server->dispatch( $request );
     287        $response = rest_get_server()->dispatch( $request );
    288288        $data     = $response->get_data();
    289289        $this->assertEquals( $id1, $data[0]['id'] );
     
    297297        );
    298298        $request  = new WP_REST_Request( 'GET', '/wp/v2/media' );
    299         $response = $this->server->dispatch( $request );
     299        $response = rest_get_server()->dispatch( $request );
    300300        $data     = $response->get_data();
    301301        $this->assertEquals( $id1, $data[0]['id'] );
    302302        // mime_type=image/png
    303303        $request->set_param( 'mime_type', 'image/png' );
    304         $response = $this->server->dispatch( $request );
     304        $response = rest_get_server()->dispatch( $request );
    305305        $this->assertCount( 0, $response->get_data() );
    306306        // mime_type=image/jpeg
    307307        $request->set_param( 'mime_type', 'image/jpeg' );
    308         $response = $this->server->dispatch( $request );
     308        $response = rest_get_server()->dispatch( $request );
    309309        $data     = $response->get_data();
    310310        $this->assertEquals( $id1, $data[0]['id'] );
     
    327327        // all attachments
    328328        $request  = new WP_REST_Request( 'GET', '/wp/v2/media' );
    329         $response = $this->server->dispatch( $request );
     329        $response = rest_get_server()->dispatch( $request );
    330330        $this->assertEquals( 2, count( $response->get_data() ) );
    331331        $request = new WP_REST_Request( 'GET', '/wp/v2/media' );
    332332        // attachments without a parent
    333333        $request->set_param( 'parent', 0 );
    334         $response = $this->server->dispatch( $request );
     334        $response = rest_get_server()->dispatch( $request );
    335335        $data     = $response->get_data();
    336336        $this->assertEquals( 1, count( $data ) );
     
    339339        $request = new WP_REST_Request( 'GET', '/wp/v2/media' );
    340340        $request->set_param( 'parent', $post_id );
    341         $response = $this->server->dispatch( $request );
     341        $response = rest_get_server()->dispatch( $request );
    342342        $data     = $response->get_data();
    343343        $this->assertEquals( 1, count( $data ) );
     
    346346        $request = new WP_REST_Request( 'GET', '/wp/v2/media' );
    347347        $request->set_param( 'parent', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    348         $response = $this->server->dispatch( $request );
     348        $response = rest_get_server()->dispatch( $request );
    349349        $data     = $response->get_data();
    350350        $this->assertEquals( 0, count( $data ) );
     
    362362        $request->set_param( 'status', 'publish' );
    363363        $request->set_param( 'context', 'edit' );
    364         $response = $this->server->dispatch( $request );
     364        $response = rest_get_server()->dispatch( $request );
    365365        $data     = $response->get_data();
    366366        $this->assertCount( 3, $data );
     
    380380        $request        = new WP_REST_Request( 'GET', '/wp/v2/media' );
    381381        $request->set_param( 'status', 'private' );
    382         $response = $this->server->dispatch( $request );
     382        $response = rest_get_server()->dispatch( $request );
    383383        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    384384        // Properly authorized users can make the request
    385385        wp_set_current_user( self::$editor_id );
    386         $response = $this->server->dispatch( $request );
     386        $response = rest_get_server()->dispatch( $request );
    387387        $this->assertEquals( 200, $response->get_status() );
    388388        $data = $response->get_data();
     
    409409        $request        = new WP_REST_Request( 'GET', '/wp/v2/media' );
    410410        $request->set_param( 'status', array( 'private', 'trash' ) );
    411         $response = $this->server->dispatch( $request );
     411        $response = rest_get_server()->dispatch( $request );
    412412        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    413413        // Properly authorized users can make the request
    414414        wp_set_current_user( self::$editor_id );
    415         $response = $this->server->dispatch( $request );
     415        $response = rest_get_server()->dispatch( $request );
    416416        $this->assertEquals( 200, $response->get_status() );
    417417        $data = $response->get_data();
     
    429429        $request->set_param( 'after', rand_str() );
    430430        $request->set_param( 'before', rand_str() );
    431         $response = $this->server->dispatch( $request );
     431        $response = rest_get_server()->dispatch( $request );
    432432        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    433433    }
     
    458458        $request->set_param( 'after', '2016-01-15T00:00:00Z' );
    459459        $request->set_param( 'before', '2016-01-17T00:00:00Z' );
    460         $response = $this->server->dispatch( $request );
     460        $response = rest_get_server()->dispatch( $request );
    461461        $data     = $response->get_data();
    462462        $this->assertCount( 1, $data );
     
    473473        update_post_meta( $attachment_id, '_wp_attachment_image_alt', 'Sample alt text' );
    474474        $request  = new WP_REST_Request( 'GET', '/wp/v2/media/' . $attachment_id );
    475         $response = $this->server->dispatch( $request );
     475        $response = rest_get_server()->dispatch( $request );
    476476        $this->check_get_post_response( $response );
    477477        $data = $response->get_data();
     
    491491
    492492        $request            = new WP_REST_Request( 'GET', '/wp/v2/media/' . $attachment_id );
    493         $response           = $this->server->dispatch( $request );
     493        $response           = rest_get_server()->dispatch( $request );
    494494        $data               = $response->get_data();
    495495        $image_src          = wp_get_attachment_image_src( $attachment_id, 'rest-api-test' );
     
    517517
    518518        $request  = new WP_REST_Request( 'GET', '/wp/v2/media/' . $attachment_id );
    519         $response = $this->server->dispatch( $request );
     519        $response = rest_get_server()->dispatch( $request );
    520520        $data     = $response->get_data();
    521521        remove_filter( 'wp_get_attachment_image_src', '__return_false' );
     
    535535        );
    536536        $request    = new WP_REST_Request( 'GET', '/wp/v2/media/' . $id1 );
    537         $response   = $this->server->dispatch( $request );
     537        $response   = rest_get_server()->dispatch( $request );
    538538        $this->assertEquals( 401, $response->get_status() );
    539539    }
     
    547547        );
    548548        $request       = new WP_REST_Request( 'GET', sprintf( '/wp/v2/media/%d', $attachment_id ) );
    549         $response      = $this->server->dispatch( $request );
     549        $response      = rest_get_server()->dispatch( $request );
    550550        $data          = $response->get_data();
    551551
     
    563563        );
    564564        $request       = new WP_REST_Request( 'GET', sprintf( '/wp/v2/media/%d', $attachment_id ) );
    565         $response      = $this->server->dispatch( $request );
     565        $response      = rest_get_server()->dispatch( $request );
    566566
    567567        $this->assertErrorResponse( 'rest_forbidden', $response, 401 );
     
    580580
    581581        $request->set_body( file_get_contents( $this->test_file ) );
    582         $response = $this->server->dispatch( $request );
     582        $response = rest_get_server()->dispatch( $request );
    583583        $data     = $response->get_data();
    584584
     
    611611        );
    612612        $request->set_header( 'Content-MD5', md5_file( $this->test_file2 ) );
    613         $response = $this->server->dispatch( $request );
     613        $response = rest_get_server()->dispatch( $request );
    614614        $this->assertEquals( 201, $response->get_status() );
    615615        $data = $response->get_data();
     
    631631        );
    632632        $request->set_header( 'Content-MD5', md5_file( $this->test_file ) );
    633         $response = $this->server->dispatch( $request );
     633        $response = rest_get_server()->dispatch( $request );
    634634        $this->assertEquals( 201, $response->get_status() );
    635635    }
     
    649649        );
    650650        $request->set_header( 'Content-MD5', md5_file( $this->test_file ) );
    651         $response = $this->server->dispatch( $request );
     651        $response = rest_get_server()->dispatch( $request );
    652652        $this->assertEquals( 201, $response->get_status() );
    653653    }
     
    656656        wp_set_current_user( self::$author_id );
    657657        $request  = new WP_REST_Request( 'POST', '/wp/v2/media' );
    658         $response = $this->server->dispatch( $request );
     658        $response = rest_get_server()->dispatch( $request );
    659659        $this->assertErrorResponse( 'rest_upload_no_data', $response, 400 );
    660660    }
     
    664664        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    665665        $request->set_body( file_get_contents( $this->test_file ) );
    666         $response = $this->server->dispatch( $request );
     666        $response = rest_get_server()->dispatch( $request );
    667667        $this->assertErrorResponse( 'rest_upload_no_content_type', $response, 400 );
    668668    }
     
    673673        $request->set_header( 'Content-Type', 'image/jpeg' );
    674674        $request->set_body( file_get_contents( $this->test_file ) );
    675         $response = $this->server->dispatch( $request );
     675        $response = rest_get_server()->dispatch( $request );
    676676        $this->assertErrorResponse( 'rest_upload_no_content_disposition', $response, 400 );
    677677    }
     
    684684        $request->set_header( 'Content-MD5', 'abc123' );
    685685        $request->set_body( file_get_contents( $this->test_file ) );
    686         $response = $this->server->dispatch( $request );
     686        $response = rest_get_server()->dispatch( $request );
    687687        $this->assertErrorResponse( 'rest_upload_hash_mismatch', $response, 412 );
    688688    }
     
    702702        );
    703703        $request->set_header( 'Content-MD5', 'abc123' );
    704         $response = $this->server->dispatch( $request );
     704        $response = rest_get_server()->dispatch( $request );
    705705        $this->assertErrorResponse( 'rest_upload_hash_mismatch', $response, 412 );
    706706    }
     
    709709        wp_set_current_user( self::$contributor_id );
    710710        $request  = new WP_REST_Request( 'POST', '/wp/v2/media' );
    711         $response = $this->server->dispatch( $request );
     711        $response = rest_get_server()->dispatch( $request );
    712712        $this->assertErrorResponse( 'rest_cannot_create', $response, 403 );
    713713    }
     
    718718        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    719719        $request->set_param( 'post', $post_id );
    720         $response = $this->server->dispatch( $request );
     720        $response = rest_get_server()->dispatch( $request );
    721721        $this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
    722722    }
     
    727727        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    728728        $request->set_param( 'post', $post_id );
    729         $response = $this->server->dispatch( $request );
     729        $response = rest_get_server()->dispatch( $request );
    730730        $this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
    731731    }
     
    745745        $request->set_body( file_get_contents( $this->test_file ) );
    746746        $request->set_param( 'post', $attachment_id );
    747         $response = $this->server->dispatch( $request );
     747        $response = rest_get_server()->dispatch( $request );
    748748        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    749749    }
     
    757757        $request->set_body( file_get_contents( $this->test_file ) );
    758758        $request->set_param( 'alt_text', 'test alt text' );
    759         $response   = $this->server->dispatch( $request );
     759        $response   = rest_get_server()->dispatch( $request );
    760760        $attachment = $response->get_data();
    761761        $this->assertEquals( 'test alt text', $attachment['alt_text'] );
     
    769769        $request->set_body( file_get_contents( $this->test_file ) );
    770770        $request->set_param( 'alt_text', '<script>alert(document.cookie)</script>' );
    771         $response   = $this->server->dispatch( $request );
     771        $response   = rest_get_server()->dispatch( $request );
    772772        $attachment = $response->get_data();
    773773        $this->assertEquals( '', $attachment['alt_text'] );
     
    788788        $request->set_param( 'description', 'Without a description, my attachment is descriptionless.' );
    789789        $request->set_param( 'alt_text', 'Alt text is stored outside post schema.' );
    790         $response   = $this->server->dispatch( $request );
     790        $response   = rest_get_server()->dispatch( $request );
    791791        $data       = $response->get_data();
    792792        $attachment = get_post( $data['id'] );
     
    818818        $request    = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id );
    819819        $request->set_param( 'post', $new_parent );
    820         $this->server->dispatch( $request );
     820        rest_get_server()->dispatch( $request );
    821821
    822822        $attachment = get_post( $attachment_id );
     
    835835        $request       = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id );
    836836        $request->set_param( 'caption', 'This is a better caption.' );
    837         $response = $this->server->dispatch( $request );
     837        $response = rest_get_server()->dispatch( $request );
    838838        $this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
    839839    }
     
    857857        $request       = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id );
    858858        $request->set_param( 'post', $attachment_id );
    859         $response = $this->server->dispatch( $request );
     859        $response = rest_get_server()->dispatch( $request );
    860860        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
    861861    }
     
    871871            $request->set_param( $name, $value );
    872872        }
    873         $response = $this->server->dispatch( $request );
     873        $response = rest_get_server()->dispatch( $request );
    874874        $this->assertEquals( 201, $response->get_status() );
    875875        $actual_output = $response->get_data();
     
    903903            $request->set_param( $name, $value );
    904904        }
    905         $response = $this->server->dispatch( $request );
     905        $response = rest_get_server()->dispatch( $request );
    906906        $this->assertEquals( 200, $response->get_status() );
    907907        $actual_output = $response->get_data();
     
    11211121        $request          = new WP_REST_Request( 'DELETE', '/wp/v2/media/' . $attachment_id );
    11221122        $request['force'] = true;
    1123         $response         = $this->server->dispatch( $request );
     1123        $response         = rest_get_server()->dispatch( $request );
    11241124        $this->assertEquals( 200, $response->get_status() );
    11251125    }
     
    11361136        // Attempt trashing
    11371137        $request  = new WP_REST_Request( 'DELETE', '/wp/v2/media/' . $attachment_id );
    1138         $response = $this->server->dispatch( $request );
     1138        $response = rest_get_server()->dispatch( $request );
    11391139        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
    11401140
    11411141        $request->set_param( 'force', 'false' );
    1142         $response = $this->server->dispatch( $request );
     1142        $response = rest_get_server()->dispatch( $request );
    11431143        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
    11441144
     
    11581158        );
    11591159        $request       = new WP_REST_Request( 'DELETE', '/wp/v2/media/' . $attachment_id );
    1160         $response      = $this->server->dispatch( $request );
     1160        $response      = rest_get_server()->dispatch( $request );
    11611161        $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
    11621162    }
     
    11731173        $attachment = get_post( $attachment_id );
    11741174        $request    = new WP_REST_Request( 'GET', sprintf( '/wp/v2/media/%d', $attachment_id ) );
    1175         $response   = $this->server->dispatch( $request );
     1175        $response   = rest_get_server()->dispatch( $request );
    11761176        $data       = $response->get_data();
    11771177        $this->check_post_data( $attachment, $data, 'view', $response->get_links() );
     
    11811181    public function test_get_item_schema() {
    11821182        $request    = new WP_REST_Request( 'OPTIONS', '/wp/v2/media' );
    1183         $response   = $this->server->dispatch( $request );
     1183        $response   = rest_get_server()->dispatch( $request );
    11841184        $data       = $response->get_data();
    11851185        $properties = $data['schema']['properties'];
     
    12351235        $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/media' );
    12361236
    1237         $response = $this->server->dispatch( $request );
     1237        $response = rest_get_server()->dispatch( $request );
    12381238        $data     = $response->get_data();
    12391239        $this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] );
     
    12491249        $request = new WP_REST_Request( 'GET', '/wp/v2/media/' . $attachment_id );
    12501250
    1251         $response = $this->server->dispatch( $request );
     1251        $response = rest_get_server()->dispatch( $request );
    12521252        $this->assertArrayHasKey( 'my_custom_int', $response->data );
    12531253
     
    12881288        );
    12891289
    1290         $response = $this->server->dispatch( $request );
     1290        $response = rest_get_server()->dispatch( $request );
    12911291
    12921292        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     
    13121312        $request = new WP_REST_Request( 'GET', '/wp/v2/media' );
    13131313        $request->set_param( 'search', $filename );
    1314         $response = $this->server->dispatch( $request );
     1314        $response = rest_get_server()->dispatch( $request );
    13151315        $data     = $response->get_data();
    13161316
Note: See TracChangeset for help on using the changeset viewer.