Make WordPress Core


Ignore:
Timestamp:
07/07/2023 05:51:11 PM (9 months ago)
Author:
adamsilverstein
Message:

Revisions: return existing autosave after saving with unchanged data.

Correctly return the existing autosave when an unchanged autosave is saved, instead of returning an error.

Fix regressions after r55154 where an error and not the original autosave was returned when saving with unchanged data (for example, clicking the preview button repeatedly). Returning the autosave (ID) is the expected behavior for the endpoint.

Follow up to [55154]

Props Mamaduka, jeroenrotty, mrfoxtalbot.
Fixes #58739.

File:
1 edited

Legend:

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

    r55457 r56163  
    689689        );
    690690        $post_id   = wp_insert_post( $post_data );
    691 
    692         wp_set_current_user( self::$editor_id );
    693 
     691        wp_set_current_user( self::$editor_id );
     692
     693        // Make a small change create the initial autosave.
    694694        $autosave_data = array(
    695             'post_content' => $post_data['post_content'],
    696         );
    697 
    698         // Create autosaves response.
    699         $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $post_id . '/autosaves' );
     695            'post_content' => 'Test post content changed',
     696        );
     697        $request       = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $post_id . '/autosaves' );
    700698        $request->add_header( 'Content-Type', 'application/json' );
    701699        $request->set_body( wp_json_encode( $autosave_data ) );
    702 
    703         $response = rest_get_server()->dispatch( $request );
    704         $data     = $response->get_data();
    705 
    706         $this->assertSame( 400, $response->get_status(), 'Response status is not 400.' );
    707         $this->assertSame( 'rest_autosave_no_changes', $data['code'], 'Response "code" is not "rest_autosave_no_changes"' );
     700        $response = rest_get_server()->dispatch( $request );
     701
     702        $this->assertSame( 200, $response->get_status() );
     703
     704        // Store the first autosave ID.
     705        $autosave = $response->get_data();
     706
     707        // Try creating an autosave using the REST endpoint with unchanged content.
     708        $request->set_body( wp_json_encode( $autosave_data ) );
     709
     710        $response = rest_get_server()->dispatch( $request );
     711        $data     = $response->get_data();
     712
     713        $this->assertSame( 200, $response->get_status() );
     714        $this->assertSame( $autosave['id'], $data['id'], 'Original autosave was not returned' );
    708715    }
    709716}
Note: See TracChangeset for help on using the changeset viewer.