diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php
index 6b2da40eb3..1eacba38e8 100644
a
|
b
|
class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller { |
371 | 371 | } |
372 | 372 | } |
373 | 373 | |
374 | | if ( ! $autosave_is_different ) { |
375 | | return new WP_Error( |
376 | | 'rest_autosave_no_changes', |
377 | | __( 'There is nothing to save. The autosave and the post content are the same.' ), |
378 | | array( 'status' => 400 ) |
379 | | ); |
380 | | } |
381 | | |
382 | 374 | $user_id = get_current_user_id(); |
383 | 375 | |
384 | 376 | // Store one autosave per author. If there is already an autosave, overwrite it. |
385 | 377 | $old_autosave = wp_get_post_autosave( $post_id, $user_id ); |
386 | 378 | |
| 379 | if ( ! $autosave_is_different && $old_autosave ) { |
| 380 | // Nothing to save, return the existing autosave. |
| 381 | return $old_autosave->ID; |
| 382 | } |
| 383 | |
387 | 384 | if ( $old_autosave ) { |
388 | 385 | $new_autosave['ID'] = $old_autosave->ID; |
389 | 386 | $new_autosave['post_author'] = $user_id; |
diff --git a/tests/phpunit/tests/rest-api/rest-autosaves-controller.php b/tests/phpunit/tests/rest-api/rest-autosaves-controller.php
index b043b5e073..e987a5f699 100644
a
|
b
|
class WP_Test_REST_Autosaves_Controller extends WP_Test_REST_Post_Type_Controlle |
688 | 688 | 'post_status' => 'publish', |
689 | 689 | ); |
690 | 690 | $post_id = wp_insert_post( $post_data ); |
691 | | |
692 | 691 | wp_set_current_user( self::$editor_id ); |
693 | 692 | |
| 693 | // Make a small change create the initial autosave. |
694 | 694 | $autosave_data = array( |
695 | | 'post_content' => $post_data['post_content'], |
| 695 | 'post_content' => 'Test post content changed', |
696 | 696 | ); |
697 | | |
698 | | // Create autosaves response. |
699 | | $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $post_id . '/autosaves' ); |
| 697 | $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $post_id . '/autosaves' ); |
700 | 698 | $request->add_header( 'Content-Type', 'application/json' ); |
701 | 699 | $request->set_body( wp_json_encode( $autosave_data ) ); |
| 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 ) ); |
702 | 709 | |
703 | 710 | $response = rest_get_server()->dispatch( $request ); |
704 | 711 | $data = $response->get_data(); |
705 | 712 | |
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"' ); |
| 713 | $this->assertSame( 200, $response->get_status() ); |
| 714 | $this->assertSame( $autosave['id'], $data['id'], 'Original autosave was not returned' ); |
708 | 715 | } |
709 | 716 | } |