REST API: Add tests for empty or "no-op" updates.

The API should allow updates that don't actually change anything.  This allows
clients to, for example, accidentally send the same request twice without
encountering unexpected errors.  This currently works for posts, terms, and
users, so this commit adds test cases accordingly.

See #38700 for issues preventing this from working for comments.

Fixes #38975.


git-svn-id: https://develop.svn.wordpress.org/trunk@39371 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
jnylen0 2016-11-30 03:02:01 +00:00
parent 2d854ef714
commit c716e604c5
3 changed files with 52 additions and 0 deletions

View File

@ -1648,6 +1648,22 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$this->assertEquals( $params['excerpt'], $post->post_excerpt );
}
public function test_update_item_no_change() {
wp_set_current_user( self::$editor_id );
$post = get_post( self::$post_id );
$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
$request->set_param( 'author', $post->post_author );
// Run twice to make sure that the update still succeeds even if no DB
// rows are updated.
$response = $this->server->dispatch( $request );
$this->check_update_post_response( $response );
$response = $this->server->dispatch( $request );
$this->check_update_post_response( $response );
}
public function test_rest_update_post() {
wp_set_current_user( self::$editor_id );

View File

@ -554,6 +554,25 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase {
$this->assertEquals( 'new-slug', $data['slug'] );
}
public function test_update_item_no_change() {
wp_set_current_user( self::$administrator );
$term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
$request = new WP_REST_Request( 'PUT', '/wp/v2/tags/' . $term->term_id );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$request->set_param( 'slug', $term->slug );
// Run twice to make sure that the update still succeeds even if no DB
// rows are updated.
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
}
public function test_update_item_invalid_term() {
wp_set_current_user( self::$administrator );
$request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );

View File

@ -1087,6 +1087,23 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
$this->assertEquals( $pw_before, $user->user_pass );
}
public function test_update_item_no_change() {
$this->allow_user_to_manage_multisite();
wp_set_current_user( self::$user );
$user = get_userdata( self::$editor );
$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', self::$editor ) );
$request->set_param( 'slug', $user->user_nicename );
// Run twice to make sure that the update still succeeds even if no DB
// rows are updated.
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
}
public function test_update_item_existing_email() {
$user1 = $this->factory->user->create( array( 'user_login' => 'test_json_user', 'user_email' => 'testjson@example.com' ) );
$user2 = $this->factory->user->create( array( 'user_login' => 'test_json_user2', 'user_email' => 'testjson2@example.com' ) );