@@ -570,6 +570,9 @@ public function test_rest_autosave_published_post() {
570570 }
571571
572572 public function test_rest_autosave_draft_post_same_author () {
573+ $ original_option = get_option ( 'enable_real_time_collaboration ' );
574+ update_option ( 'enable_real_time_collaboration ' , false );
575+
573576 wp_set_current_user ( self ::$ editor_id );
574577
575578 $ post_data = array (
@@ -604,6 +607,7 @@ public function test_rest_autosave_draft_post_same_author() {
604607 $ this ->assertSame ( $ post_data ['post_excerpt ' ], $ post ->post_excerpt );
605608
606609 wp_delete_post ( $ post_id );
610+ update_option ( 'enable_real_time_collaboration ' , $ original_option );
607611 }
608612
609613 public function test_rest_autosave_draft_post_different_author () {
@@ -744,6 +748,9 @@ public function test_get_item_sets_up_postdata() {
744748 }
745749
746750 public function test_update_item_draft_page_with_parent () {
751+ $ original_option = get_option ( 'enable_real_time_collaboration ' );
752+ update_option ( 'enable_real_time_collaboration ' , false );
753+
747754 wp_set_current_user ( self ::$ editor_id );
748755 $ request = new WP_REST_Request ( 'POST ' , '/wp/v2/pages/ ' . self ::$ child_draft_page_id . '/autosaves ' );
749756 $ request ->add_header ( 'Content-Type ' , 'application/x-www-form-urlencoded ' );
@@ -761,6 +768,7 @@ public function test_update_item_draft_page_with_parent() {
761768
762769 $ this ->assertSame ( self ::$ child_draft_page_id , $ data ['id ' ] );
763770 $ this ->assertSame ( self ::$ parent_page_id , $ data ['parent ' ] );
771+ update_option ( 'enable_real_time_collaboration ' , $ original_option );
764772 }
765773
766774 public function test_schema_validation_is_applied () {
@@ -920,4 +928,81 @@ public static function data_head_request_with_specified_fields_returns_success_r
920928 'get_items request ' => array ( '/wp/v2/posts/%d ' ),
921929 );
922930 }
931+
932+ /**
933+ * When real-time collaboration is enabled, autosaving a draft post by the
934+ * same author should create a revision instead of updating the post directly.
935+ */
936+ public function test_rest_autosave_draft_post_same_author_with_rtc () {
937+ $ original_option = get_option ( 'enable_real_time_collaboration ' );
938+ update_option ( 'enable_real_time_collaboration ' , true );
939+
940+ wp_set_current_user ( self ::$ editor_id );
941+
942+ $ post_data = array (
943+ 'post_content ' => 'Test post content ' ,
944+ 'post_title ' => 'Test post title ' ,
945+ 'post_excerpt ' => 'Test post excerpt ' ,
946+ );
947+ $ post_id = wp_insert_post ( $ post_data );
948+
949+ $ autosave_data = array (
950+ 'id ' => $ post_id ,
951+ 'content ' => 'Updated post \ content ' ,
952+ 'title ' => 'Updated post title ' ,
953+ );
954+
955+ $ request = new WP_REST_Request ( 'POST ' , '/wp/v2/posts/ ' . self ::$ post_id . '/autosaves ' );
956+ $ request ->add_header ( 'Content-Type ' , 'application/json ' );
957+ $ request ->set_body ( wp_json_encode ( $ autosave_data ) );
958+
959+ $ response = rest_get_server ()->dispatch ( $ request );
960+ $ new_data = $ response ->get_data ();
961+ $ post = get_post ( $ post_id );
962+
963+ // With RTC enabled, a revision is created instead of updating the post.
964+ $ this ->assertNotSame ( $ post_id , $ new_data ['id ' ] );
965+ $ this ->assertSame ( $ post_id , $ new_data ['parent ' ] );
966+
967+ // The autosave revision should have the updated content.
968+ $ this ->assertSame ( $ autosave_data ['content ' ], $ new_data ['content ' ]['raw ' ] );
969+ $ this ->assertSame ( $ autosave_data ['title ' ], $ new_data ['title ' ]['raw ' ] );
970+
971+ // The draft post should not be updated.
972+ $ this ->assertSame ( $ post_data ['post_content ' ], $ post ->post_content );
973+ $ this ->assertSame ( $ post_data ['post_title ' ], $ post ->post_title );
974+ $ this ->assertSame ( $ post_data ['post_excerpt ' ], $ post ->post_excerpt );
975+
976+ wp_delete_post ( $ post_id );
977+ update_option ( 'enable_real_time_collaboration ' , $ original_option );
978+ }
979+
980+ /**
981+ * When real-time collaboration is enabled, autosaving a draft page with
982+ * a parent should create a revision instead of updating the page directly.
983+ */
984+ public function test_update_item_draft_page_with_parent_with_rtc () {
985+ $ original_option = get_option ( 'enable_real_time_collaboration ' );
986+ update_option ( 'enable_real_time_collaboration ' , true );
987+
988+ wp_set_current_user ( self ::$ editor_id );
989+ $ request = new WP_REST_Request ( 'POST ' , '/wp/v2/pages/ ' . self ::$ child_draft_page_id . '/autosaves ' );
990+ $ request ->add_header ( 'Content-Type ' , 'application/x-www-form-urlencoded ' );
991+
992+ $ params = $ this ->set_post_data (
993+ array (
994+ 'id ' => self ::$ child_draft_page_id ,
995+ 'author ' => self ::$ editor_id ,
996+ )
997+ );
998+
999+ $ request ->set_body_params ( $ params );
1000+ $ response = rest_get_server ()->dispatch ( $ request );
1001+ $ data = $ response ->get_data ();
1002+
1003+ // With RTC enabled, a revision is created instead of updating the page.
1004+ $ this ->assertNotSame ( self ::$ child_draft_page_id , $ data ['id ' ] );
1005+ $ this ->assertSame ( self ::$ child_draft_page_id , $ data ['parent ' ] );
1006+ update_option ( 'enable_real_time_collaboration ' , $ original_option );
1007+ }
9231008}
0 commit comments