Make WordPress Core


Ignore:
Timestamp:
11/03/2016 08:04:59 PM (7 years ago)
Author:
rachelbaker
Message:

REST API: Modify the structure of our DELETE responses to be more explicit.

Add the deleted property to the root of the Response object to communicate if the delete action was successful. Move the state of the resource prior to the delete request under a new previous property. As a result DELETE responses are now structured like so:

{ deleted: true, previous: { ... } }

Also includes helpful information to DELETE requests for resources that are not trashable.

Props timmydcrawford, rmccue, jnylen0.
Fixes #38494.

File:
1 edited

Legend:

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

    r39105 r39126  
    11551155        $this->assertEquals( 200, $response->get_status() );
    11561156        $data = $response->get_data();
    1157         $this->assertEquals( 'Deleted User', $data['name'] );
     1157        $this->assertTrue( $data['deleted'] );
     1158        $this->assertEquals( 'Deleted User', $data['previous']['name'] );
     1159    }
     1160
     1161    public function test_delete_item_no_trash() {
     1162        $user_id = $this->factory->user->create( array( 'display_name' => 'Deleted User' ) );
     1163
     1164        $this->allow_user_to_manage_multisite();
     1165        wp_set_current_user( self::$user );
     1166
     1167        $userdata = get_userdata( $user_id ); // cache for later
     1168
     1169        $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) );
     1170        $response = $this->server->dispatch( $request );
     1171        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
     1172
     1173        $request->set_param( 'force', 'false' );
     1174        $response = $this->server->dispatch( $request );
     1175        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
     1176
     1177        // Ensure the user still exists
     1178        $user = get_user_by( 'id', $user_id );
     1179        $this->assertNotEmpty( $user );
    11581180    }
    11591181
     
    11711193        $this->assertEquals( 200, $response->get_status() );
    11721194        $data = $response->get_data();
    1173         $this->assertEquals( 'Deleted User', $data['name'] );
    1174     }
    1175 
    1176     public function test_delete_item_no_trash() {
    1177         $user_id = $this->factory->user->create( array( 'display_name' => 'Deleted User' ) );
    1178 
    1179         $this->allow_user_to_manage_multisite();
    1180         wp_set_current_user( self::$user );
    1181 
    1182         $userdata = get_userdata( $user_id ); // cache for later
    1183         $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) );
    1184         $response = $this->server->dispatch( $request );
    1185         $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
    1186 
    1187         // Ensure the user still exists
    1188         $user = get_user_by( 'id', $user_id );
    1189         $this->assertNotEmpty( $user );
     1195        $this->assertTrue( $data['deleted'] );
     1196        $this->assertEquals( 'Deleted User', $data['previous']['name'] );
    11901197    }
    11911198
    11921199    public function test_delete_current_item_no_trash() {
    1193         $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
     1200        $user_id = $this->factory->user->create( array( 'role' => 'administrator', 'display_name' => 'Deleted User' ) );
    11941201
    11951202        wp_set_current_user( $user_id );
     
    11971204        update_site_option( 'site_admins', array( $user->user_login ) );
    11981205
    1199         $userdata = get_userdata( $user_id ); // cache for later
    12001206        $request = new WP_REST_Request( 'DELETE', '/wp/v2/users/me' );
     1207        $response = $this->server->dispatch( $request );
     1208        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
     1209
     1210        $request->set_param( 'force', 'false' );
    12011211        $response = $this->server->dispatch( $request );
    12021212        $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
Note: See TracChangeset for help on using the changeset viewer.