Skip to content

Commit fb6fba4

Browse files
committed
Real-time collaboration: fix unit tests.
Fixes autosave controller tests, which began failing when RTC was enabled. The tests now need to cover two different code paths: https://github.com/WordPress/wordpress-develop/blob/15ffb4392eddb43fdd9f31e075ebc85e857664c1/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php#L259 Props czarate. See #64622. git-svn-id: https://develop.svn.wordpress.org/trunk@61697 602fd350-edb4-49c9-b593-d223f7449a82
1 parent c0c55b1 commit fb6fba4

2 files changed

Lines changed: 192 additions & 1 deletion

File tree

tests/phpunit/tests/rest-api/rest-autosaves-controller.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

tests/qunit/fixtures/wp-api-generated.js

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ mockedApiResponse.Schema = {
2020
"wp/v2",
2121
"wp-site-health/v1",
2222
"wp-block-editor/v1",
23-
"wp-abilities/v1"
23+
"wp-abilities/v1",
24+
"wp-sync/v1"
2425
],
2526
"authentication": {
2627
"application-passwords": {
@@ -12698,6 +12699,111 @@ mockedApiResponse.Schema = {
1269812699
}
1269912700
}
1270012701
]
12702+
},
12703+
"/wp-sync/v1": {
12704+
"namespace": "wp-sync/v1",
12705+
"methods": [
12706+
"GET"
12707+
],
12708+
"endpoints": [
12709+
{
12710+
"methods": [
12711+
"GET"
12712+
],
12713+
"args": {
12714+
"namespace": {
12715+
"default": "wp-sync/v1",
12716+
"required": false
12717+
},
12718+
"context": {
12719+
"default": "view",
12720+
"required": false
12721+
}
12722+
}
12723+
}
12724+
],
12725+
"_links": {
12726+
"self": [
12727+
{
12728+
"href": "http://example.org/index.php?rest_route=/wp-sync/v1"
12729+
}
12730+
]
12731+
}
12732+
},
12733+
"/wp-sync/v1/updates": {
12734+
"namespace": "wp-sync/v1",
12735+
"methods": [
12736+
"POST"
12737+
],
12738+
"endpoints": [
12739+
{
12740+
"methods": [
12741+
"POST"
12742+
],
12743+
"args": {
12744+
"rooms": {
12745+
"items": {
12746+
"properties": {
12747+
"after": {
12748+
"minimum": 0,
12749+
"required": true,
12750+
"type": "integer"
12751+
},
12752+
"awareness": {
12753+
"required": true,
12754+
"type": "object"
12755+
},
12756+
"client_id": {
12757+
"minimum": 1,
12758+
"required": true,
12759+
"type": "integer"
12760+
},
12761+
"room": {
12762+
"required": true,
12763+
"type": "string",
12764+
"pattern": "^[^/]+/[^/:]+(?::\\S+)?$"
12765+
},
12766+
"updates": {
12767+
"items": {
12768+
"properties": {
12769+
"data": {
12770+
"type": "string",
12771+
"required": true
12772+
},
12773+
"type": {
12774+
"type": "string",
12775+
"required": true,
12776+
"enum": [
12777+
"compaction",
12778+
"sync_step1",
12779+
"sync_step2",
12780+
"update"
12781+
]
12782+
}
12783+
},
12784+
"required": true,
12785+
"type": "object"
12786+
},
12787+
"minItems": 0,
12788+
"required": true,
12789+
"type": "array"
12790+
}
12791+
},
12792+
"type": "object"
12793+
},
12794+
"type": "array",
12795+
"required": true
12796+
}
12797+
}
12798+
}
12799+
],
12800+
"_links": {
12801+
"self": [
12802+
{
12803+
"href": "http://example.org/index.php?rest_route=/wp-sync/v1/updates"
12804+
}
12805+
]
12806+
}
1270112807
}
1270212808
},
1270312809
"site_logo": 0,

0 commit comments

Comments
 (0)