WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/27/2016 02:56:28 AM (5 years ago)
Author:
boonebgorges
Message:

Share fixtures in REST API endpoint tests.

As sparrows' tears shed steadily
Make widest rivers filled,

setUp() routines run prodig'ly
Add minutes to a build.

So cull ye fixtures profligate!
Direct thine frugal gaze!

Our savings here - a half-minute -
When multiplied: Amaze!

See #30017.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php

    r38832 r38975  
    1111  */
    1212class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase {
     13    protected static $post_id;
     14
     15    public static function wpSetUpBeforeClass( $factory ) {
     16        self::$post_id = $factory->post->create();
     17    }
     18
     19    public static function wpTearDownAfterClass() {
     20        wp_delete_post( self::$post_id, true );
     21    }
     22
    1323    public function setUp() {
    1424        parent::setUp();
     
    5565        $this->server = $wp_rest_server = new Spy_REST_Server;
    5666        do_action( 'rest_api_init' );
    57 
    58         $this->post_id = $this->factory->post->create();
    5967    }
    6068
     
    6876
    6977    public function test_get_value() {
    70         add_post_meta( $this->post_id, 'test_single', 'testvalue' );
    71 
    72         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     78        add_post_meta( self::$post_id, 'test_single', 'testvalue' );
     79
     80        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    7381        $response = $this->server->dispatch( $request );
    7482
     
    8795     */
    8896    public function test_get_multi_value() {
    89         add_post_meta( $this->post_id, 'test_multi', 'value1' );
    90         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     97        add_post_meta( self::$post_id, 'test_multi', 'value1' );
     98        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    9199
    92100        $response = $this->server->dispatch( $request );
     
    100108
    101109        // Check after an update.
    102         add_post_meta( $this->post_id, 'test_multi', 'value2' );
     110        add_post_meta( self::$post_id, 'test_multi', 'value2' );
    103111
    104112        $response = $this->server->dispatch( $request );
     
    114122     */
    115123    public function test_get_unregistered() {
    116         add_post_meta( $this->post_id, 'test_unregistered', 'value1' );
    117         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     124        add_post_meta( self::$post_id, 'test_unregistered', 'value1' );
     125        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    118126
    119127        $response = $this->server->dispatch( $request );
     
    129137     */
    130138    public function test_get_registered_no_api_access() {
    131         add_post_meta( $this->post_id, 'test_no_rest', 'for_the_wicked' );
    132         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     139        add_post_meta( self::$post_id, 'test_no_rest', 'for_the_wicked' );
     140        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    133141
    134142        $response = $this->server->dispatch( $request );
     
    144152     */
    145153    public function test_get_registered_api_disabled() {
    146         add_post_meta( $this->post_id, 'test_rest_disabled', 'sleepless_nights' );
    147         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     154        add_post_meta( self::$post_id, 'test_rest_disabled', 'sleepless_nights' );
     155        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    148156
    149157        $response = $this->server->dispatch( $request );
     
    177185        do_action( 'rest_api_init' );
    178186
    179         add_post_meta( $this->post_id, 'test_string', 42 );
    180         add_post_meta( $this->post_id, 'test_number', '42' );
    181         add_post_meta( $this->post_id, 'test_bool', 1 );
    182 
    183         $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     187        add_post_meta( self::$post_id, 'test_string', 42 );
     188        add_post_meta( self::$post_id, 'test_number', '42' );
     189        add_post_meta( self::$post_id, 'test_bool', 1 );
     190
     191        $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    184192        $response = $this->server->dispatch( $request );
    185193        $this->assertEquals( 200, $response->get_status() );
     
    206214    public function test_set_value() {
    207215        // Ensure no data exists currently.
    208         $values = get_post_meta( $this->post_id, 'test_single', false );
     216        $values = get_post_meta( self::$post_id, 'test_single', false );
    209217        $this->assertEmpty( $values );
    210218
     
    216224            ),
    217225        );
    218         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
    219         $request->set_body_params( $data );
    220 
    221         $response = $this->server->dispatch( $request );
    222         $this->assertEquals( 200, $response->get_status() );
    223 
    224         $meta = get_post_meta( $this->post_id, 'test_single', false );
     226        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     227        $request->set_body_params( $data );
     228
     229        $response = $this->server->dispatch( $request );
     230        $this->assertEquals( 200, $response->get_status() );
     231
     232        $meta = get_post_meta( self::$post_id, 'test_single', false );
    225233        $this->assertNotEmpty( $meta );
    226234        $this->assertCount( 1, $meta );
     
    238246    public function test_set_duplicate_single_value() {
    239247        // Start with an existing metakey and value.
    240         $values = update_post_meta( $this->post_id, 'test_single', 'test_value' );
    241         $this->assertEquals( 'test_value', get_post_meta( $this->post_id, 'test_single', true ) );
     248        $values = update_post_meta( self::$post_id, 'test_single', 'test_value' );
     249        $this->assertEquals( 'test_value', get_post_meta( self::$post_id, 'test_single', true ) );
    242250
    243251        $this->grant_write_permission();
     
    248256            ),
    249257        );
    250         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
    251         $request->set_body_params( $data );
    252 
    253         $response = $this->server->dispatch( $request );
    254         $this->assertEquals( 200, $response->get_status() );
    255 
    256         $meta = get_post_meta( $this->post_id, 'test_single', true );
     258        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     259        $request->set_body_params( $data );
     260
     261        $response = $this->server->dispatch( $request );
     262        $this->assertEquals( 200, $response->get_status() );
     263
     264        $meta = get_post_meta( self::$post_id, 'test_single', true );
    257265        $this->assertNotEmpty( $meta );
    258266        $this->assertEquals( 'test_value', $meta );
     
    276284        wp_set_current_user( 0 );
    277285
    278         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     286        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    279287        $request->set_body_params( $data );
    280288
     
    283291
    284292        // Check that the value wasn't actually updated.
    285         $this->assertEmpty( get_post_meta( $this->post_id, 'test_single', false ) );
     293        $this->assertEmpty( get_post_meta( self::$post_id, 'test_single', false ) );
    286294    }
    287295
     
    298306        $this->grant_write_permission();
    299307
    300         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     308        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    301309        $request->set_body_params( $data );
    302310
    303311        $response = $this->server->dispatch( $request );
    304312        $this->assertErrorResponse( 'rest_cannot_update', $response, 403 );
    305         $this->assertEmpty( get_post_meta( $this->post_id, 'test_bad_auth', false ) );
     313        $this->assertEmpty( get_post_meta( self::$post_id, 'test_bad_auth', false ) );
    306314    }
    307315
     
    318326        $this->grant_write_permission();
    319327
    320         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     328        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    321329        $request->set_body_params( $data );
    322330
     
    336344    public function test_set_value_multiple() {
    337345        // Ensure no data exists currently.
    338         $values = get_post_meta( $this->post_id, 'test_multi', false );
     346        $values = get_post_meta( self::$post_id, 'test_multi', false );
    339347        $this->assertEmpty( $values );
    340348
     
    346354            ),
    347355        );
    348         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
    349         $request->set_body_params( $data );
    350 
    351         $response = $this->server->dispatch( $request );
    352         $this->assertEquals( 200, $response->get_status() );
    353 
    354         $meta = get_post_meta( $this->post_id, 'test_multi', false );
     356        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     357        $request->set_body_params( $data );
     358
     359        $response = $this->server->dispatch( $request );
     360        $this->assertEquals( 200, $response->get_status() );
     361
     362        $meta = get_post_meta( self::$post_id, 'test_multi', false );
    355363        $this->assertNotEmpty( $meta );
    356364        $this->assertCount( 1, $meta );
     
    368376        $this->assertEquals( 200, $response->get_status() );
    369377
    370         $meta = get_post_meta( $this->post_id, 'test_multi', false );
     378        $meta = get_post_meta( self::$post_id, 'test_multi', false );
    371379        $this->assertNotEmpty( $meta );
    372380        $this->assertCount( 2, $meta );
     
    379387     */
    380388    public function test_set_value_remove_one() {
    381         add_post_meta( $this->post_id, 'test_multi', 'c' );
    382         add_post_meta( $this->post_id, 'test_multi', 'n' );
    383         add_post_meta( $this->post_id, 'test_multi', 'n' );
     389        add_post_meta( self::$post_id, 'test_multi', 'c' );
     390        add_post_meta( self::$post_id, 'test_multi', 'n' );
     391        add_post_meta( self::$post_id, 'test_multi', 'n' );
    384392
    385393        $this->grant_write_permission();
     
    390398            ),
    391399        );
    392         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
    393         $request->set_body_params( $data );
    394 
    395         $response = $this->server->dispatch( $request );
    396         $this->assertEquals( 200, $response->get_status() );
    397 
    398         $meta = get_post_meta( $this->post_id, 'test_multi', false );
     400        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     401        $request->set_body_params( $data );
     402
     403        $response = $this->server->dispatch( $request );
     404        $this->assertEquals( 200, $response->get_status() );
     405
     406        $meta = get_post_meta( self::$post_id, 'test_multi', false );
    399407        $this->assertNotEmpty( $meta );
    400408        $this->assertCount( 2, $meta );
     
    408416    public function test_set_value_multiple_unauthenticated() {
    409417        // Ensure no data exists currently.
    410         $values = get_post_meta( $this->post_id, 'test_multi', false );
     418        $values = get_post_meta( self::$post_id, 'test_multi', false );
    411419        $this->assertEmpty( $values );
    412420
     
    418426            ),
    419427        );
    420         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     428        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    421429        $request->set_body_params( $data );
    422430
     
    424432        $this->assertErrorResponse( 'rest_cannot_edit', $response, 401 );
    425433
    426         $meta = get_post_meta( $this->post_id, 'test_multi', false );
     434        $meta = get_post_meta( self::$post_id, 'test_multi', false );
    427435        $this->assertEmpty( $meta );
    428436    }
     
    440448        $this->grant_write_permission();
    441449
    442         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     450        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    443451        $request->set_body_params( $data );
    444452
    445453        $response = $this->server->dispatch( $request );
    446454        $this->assertErrorResponse( 'rest_cannot_update', $response, 403 );
    447         $this->assertEmpty( get_post_meta( $this->post_id, 'test_bad_auth_multi', false ) );
     455        $this->assertEmpty( get_post_meta( self::$post_id, 'test_bad_auth_multi', false ) );
    448456    }
    449457
    450458    public function test_add_multi_value_db_error() {
    451459        // Ensure no data exists currently.
    452         $values = get_post_meta( $this->post_id, 'test_multi', false );
     460        $values = get_post_meta( self::$post_id, 'test_multi', false );
    453461        $this->assertEmpty( $values );
    454462
     
    460468            ),
    461469        );
    462         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     470        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    463471        $request->set_body_params( $data );
    464472
     
    479487
    480488    public function test_remove_multi_value_db_error() {
    481         add_post_meta( $this->post_id, 'test_multi', 'val1' );
    482         $values = get_post_meta( $this->post_id, 'test_multi', false );
     489        add_post_meta( self::$post_id, 'test_multi', 'val1' );
     490        $values = get_post_meta( self::$post_id, 'test_multi', false );
    483491        $this->assertEquals( array( 'val1' ), $values );
    484492
     
    490498            ),
    491499        );
    492         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     500        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    493501        $request->set_body_params( $data );
    494502
     
    509517
    510518    public function test_delete_value() {
    511         add_post_meta( $this->post_id, 'test_single', 'val1' );
    512         $current = get_post_meta( $this->post_id, 'test_single', true );
     519        add_post_meta( self::$post_id, 'test_single', 'val1' );
     520        $current = get_post_meta( self::$post_id, 'test_single', true );
    513521        $this->assertEquals( 'val1', $current );
    514522
     
    520528            ),
    521529        );
    522         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
    523         $request->set_body_params( $data );
    524 
    525         $response = $this->server->dispatch( $request );
    526         $this->assertEquals( 200, $response->get_status() );
    527 
    528         $meta = get_post_meta( $this->post_id, 'test_single', false );
     530        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     531        $request->set_body_params( $data );
     532
     533        $response = $this->server->dispatch( $request );
     534        $this->assertEquals( 200, $response->get_status() );
     535
     536        $meta = get_post_meta( self::$post_id, 'test_single', false );
    529537        $this->assertEmpty( $meta );
    530538    }
     
    534542     */
    535543    public function test_delete_value_blocked() {
    536         add_post_meta( $this->post_id, 'test_bad_auth', 'val1' );
    537         $current = get_post_meta( $this->post_id, 'test_bad_auth', true );
     544        add_post_meta( self::$post_id, 'test_bad_auth', 'val1' );
     545        $current = get_post_meta( self::$post_id, 'test_bad_auth', true );
    538546        $this->assertEquals( 'val1', $current );
    539547
     
    545553            ),
    546554        );
    547         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     555        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    548556        $request->set_body_params( $data );
    549557
     
    551559        $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
    552560
    553         $meta = get_post_meta( $this->post_id, 'test_bad_auth', true );
     561        $meta = get_post_meta( self::$post_id, 'test_bad_auth', true );
    554562        $this->assertEquals( 'val1', $meta );
    555563    }
     
    559567     */
    560568    public function test_delete_value_db_error() {
    561         add_post_meta( $this->post_id, 'test_single', 'val1' );
    562         $current = get_post_meta( $this->post_id, 'test_single', true );
     569        add_post_meta( self::$post_id, 'test_single', 'val1' );
     570        $current = get_post_meta( self::$post_id, 'test_single', true );
    563571        $this->assertEquals( 'val1', $current );
    564572
     
    570578            ),
    571579        );
    572         $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     580        $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    573581        $request->set_body_params( $data );
    574582        /**
     
    588596
    589597    public function test_get_schema() {
    590         $request = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
     598        $request = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    591599        $response = $this->server->dispatch( $request );
    592600
Note: See TracChangeset for help on using the changeset viewer.