From 99e5de94314b7659857023e7e2e2f39160d4329b Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Wed, 30 Nov 2016 03:27:22 +0000 Subject: [PATCH] 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. Merge of [39371] to the 4.7 branch. Props jnylen0. See #38975. git-svn-id: https://develop.svn.wordpress.org/branches/4.7@39372 602fd350-edb4-49c9-b593-d223f7449a82 --- .../tests/rest-api/rest-posts-controller.php | 16 ++++++++++++++++ .../tests/rest-api/rest-tags-controller.php | 19 +++++++++++++++++++ .../tests/rest-api/rest-users-controller.php | 17 +++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index 70db2e2bd3..8f8bc73476 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -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 ); diff --git a/tests/phpunit/tests/rest-api/rest-tags-controller.php b/tests/phpunit/tests/rest-api/rest-tags-controller.php index 4c461d347d..bdca9cb852 100644 --- a/tests/phpunit/tests/rest-api/rest-tags-controller.php +++ b/tests/phpunit/tests/rest-api/rest-tags-controller.php @@ -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 ); diff --git a/tests/phpunit/tests/rest-api/rest-users-controller.php b/tests/phpunit/tests/rest-api/rest-users-controller.php index 8da864f3a8..62da9228fe 100644 --- a/tests/phpunit/tests/rest-api/rest-users-controller.php +++ b/tests/phpunit/tests/rest-api/rest-users-controller.php @@ -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' ) );