Make WordPress Core

Changeset 39372


Ignore:
Timestamp:
11/30/2016 03:27:22 AM (7 years ago)
Author:
pento
Message:

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.

Location:
branches/4.7
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/4.7

  • branches/4.7/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r39353 r39372  
    16471647        $this->assertEquals( $params['content'], $post->post_content );
    16481648        $this->assertEquals( $params['excerpt'], $post->post_excerpt );
     1649    }
     1650
     1651    public function test_update_item_no_change() {
     1652        wp_set_current_user( self::$editor_id );
     1653        $post = get_post( self::$post_id );
     1654
     1655        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1656        $request->set_param( 'author', $post->post_author );
     1657
     1658        // Run twice to make sure that the update still succeeds even if no DB
     1659        // rows are updated.
     1660        $response = $this->server->dispatch( $request );
     1661        $this->check_update_post_response( $response );
     1662
     1663        $response = $this->server->dispatch( $request );
     1664        $this->check_update_post_response( $response );
    16491665    }
    16501666
  • branches/4.7/tests/phpunit/tests/rest-api/rest-tags-controller.php

    r39193 r39372  
    553553        $this->assertEquals( 'New Description', $data['description'] );
    554554        $this->assertEquals( 'new-slug', $data['slug'] );
     555    }
     556
     557    public function test_update_item_no_change() {
     558        wp_set_current_user( self::$administrator );
     559        $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
     560
     561        $request = new WP_REST_Request( 'PUT', '/wp/v2/tags/' . $term->term_id );
     562
     563        $response = $this->server->dispatch( $request );
     564        $this->assertEquals( 200, $response->get_status() );
     565        $request->set_param( 'slug', $term->slug );
     566
     567        // Run twice to make sure that the update still succeeds even if no DB
     568        // rows are updated.
     569        $response = $this->server->dispatch( $request );
     570        $this->assertEquals( 200, $response->get_status() );
     571
     572        $response = $this->server->dispatch( $request );
     573        $this->assertEquals( 200, $response->get_status() );
    555574    }
    556575
  • branches/4.7/tests/phpunit/tests/rest-api/rest-users-controller.php

    r39293 r39372  
    10861086        // as per https://core.trac.wordpress.org/ticket/21429
    10871087        $this->assertEquals( $pw_before, $user->user_pass );
     1088    }
     1089
     1090    public function test_update_item_no_change() {
     1091        $this->allow_user_to_manage_multisite();
     1092        wp_set_current_user( self::$user );
     1093        $user = get_userdata( self::$editor );
     1094
     1095        $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', self::$editor ) );
     1096        $request->set_param( 'slug', $user->user_nicename );
     1097
     1098        // Run twice to make sure that the update still succeeds even if no DB
     1099        // rows are updated.
     1100        $response = $this->server->dispatch( $request );
     1101        $this->assertEquals( 200, $response->get_status() );
     1102
     1103        $response = $this->server->dispatch( $request );
     1104        $this->assertEquals( 200, $response->get_status() );
    10881105    }
    10891106
Note: See TracChangeset for help on using the changeset viewer.