Skip to content
This repository has been archived by the owner on Sep 24, 2018. It is now read-only.

Return full user object when deleting user #1253

Merged
merged 5 commits into from
May 21, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/endpoints/class-wp-rest-users-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,16 @@ public function delete_item( $request ) {
}
}

$mock_request = new WP_REST_Request( 'GET', '/wp/users/' . $id );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this should fail?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. Because it's not routing through the server, the request can be whatever. I guess ultimately it should route through the server, so it's not hard-coupled to this controller.

$mock_request->set_param( 'context', 'edit' );
$orig_user = $this->prepare_item_for_response( $user, $mock_request );

$result = wp_delete_user( $id, $reassign );

if ( ! $result ) {
return new WP_Error( 'rest_cannot_delete', __( 'The user cannot be deleted.' ), array( 'status' => 500 ) );
} else {
return array( 'message' => __( 'Deleted user' ) );
return $orig_user;
}
}

Expand Down
5 changes: 4 additions & 1 deletion tests/test-rest-users-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,17 +597,20 @@ public function test_update_user_invalid_id() {
}

public function test_delete_item() {
$user_id = $this->factory->user->create();
$user_id = $this->factory->user->create( array( 'display_name' => 'Deleted User' ) );

$this->allow_user_to_manage_multisite();
wp_set_current_user( $this->user );

$userdata = get_userdata( $user_id ); // cache for later
$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) );
$response = $this->server->dispatch( $request );

$this->assertNotInstanceOf( 'WP_Error', $response );
$response = rest_ensure_response( $response );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( 'Deleted User', $data['name'] );
}

public function test_delete_user_without_permission() {
Expand Down