forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwpHttpPollingSyncServer.php
More file actions
1259 lines (1018 loc) · 33.6 KB
/
wpHttpPollingSyncServer.php
File metadata and controls
1259 lines (1018 loc) · 33.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Tests for the WP_HTTP_Polling_Sync_Server REST endpoint.
*
* @package gutenberg
* @subpackage Collaboration
*
* @group collaboration
* @group restapi
*/
class Tests_Collaboration_WpHttpPollingSyncServer extends WP_Test_REST_Controller_Testcase {
protected static int $editor_id;
protected static int $subscriber_id;
protected static int $post_id;
protected static int $category_id;
protected static int $tag_id;
protected static int $comment_id;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
self::$subscriber_id = $factory->user->create( array( 'role' => 'subscriber' ) );
self::$post_id = $factory->post->create( array( 'post_author' => self::$editor_id ) );
self::$category_id = $factory->category->create();
self::$tag_id = $factory->tag->create();
self::$comment_id = $factory->comment->create( array( 'comment_post_ID' => self::$post_id ) );
// Enable option in setUpBeforeClass to ensure REST routes are registered.
update_option( 'wp_collaboration_enabled', 1 );
}
public static function wpTearDownAfterClass() {
self::delete_user( self::$editor_id );
self::delete_user( self::$subscriber_id );
delete_option( 'wp_collaboration_enabled' );
wp_delete_post( self::$post_id, true );
wp_delete_term( self::$category_id, 'category' );
wp_delete_term( self::$tag_id, 'post_tag' );
wp_delete_comment( self::$comment_id, true );
}
public function set_up() {
parent::set_up();
// Enable option for tests.
update_option( 'wp_collaboration_enabled', 1 );
// Reset storage post ID cache to ensure clean state after transaction rollback.
$reflection = new ReflectionProperty( 'WP_Sync_Post_Meta_Storage', 'storage_post_ids' );
if ( PHP_VERSION_ID < 80100 ) {
$reflection->setAccessible( true );
}
$reflection->setValue( null, array() );
}
/**
* Builds a room request array for the sync endpoint.
*
* @param string $room Room identifier.
* @param int $client_id Client ID.
* @param int $cursor Cursor value for the 'after' parameter.
* @param array $awareness Awareness state.
* @param array $updates Array of updates.
* @return array Room request data.
*/
private function build_room( $room, $client_id = 1, $cursor = 0, $awareness = array(), $updates = array() ) {
if (
null !== $awareness &&
! empty( $awareness ) &&
! array_key_exists( 'collaboratorInfo', $awareness ) &&
! array_key_exists( 'editorState', $awareness )
) {
$awareness = array( 'editorState' => $awareness );
}
return array(
'after' => $cursor,
'awareness' => $awareness,
'client_id' => $client_id,
'room' => $room,
'updates' => $updates,
);
}
/**
* Builds a room request without adapting legacy test awareness into the
* current awareness protocol shape.
*
* @param string $room Room identifier.
* @param int $client_id Client ID.
* @param int $cursor Cursor value for the 'after' parameter.
* @param mixed $awareness Awareness state.
* @param array $updates Array of updates.
* @return array Room request data.
*/
private function build_raw_room( $room, $client_id = 1, $cursor = 0, $awareness = array(), $updates = array() ) {
return array(
'after' => $cursor,
'awareness' => $awareness,
'client_id' => $client_id,
'room' => $room,
'updates' => $updates,
);
}
/**
* Dispatches a sync request with the given rooms.
*
* @param array $rooms Array of room request data.
* @return WP_REST_Response Response object.
*/
private function dispatch_sync( $rooms ) {
$request = new WP_REST_Request( 'POST', '/wp-sync/v1/updates' );
$request->set_body_params( array( 'rooms' => $rooms ) );
return rest_get_server()->dispatch( $request );
}
/**
* Returns the default room identifier for the test post.
*
* @return string Room identifier.
*/
private function get_post_room() {
return 'postType/post:' . self::$post_id;
}
/*
* Required abstract method implementations.
*
* The sync endpoint is a single POST endpoint, not a standard CRUD controller.
* Methods that don't apply are stubbed with @doesNotPerformAssertions.
*/
public function test_register_routes() {
$routes = rest_get_server()->get_routes();
$this->assertArrayHasKey( '/wp-sync/v1/updates', $routes );
}
/**
* @doesNotPerformAssertions
*/
public function test_context_param() {
// Not applicable for sync endpoint.
}
/**
* @doesNotPerformAssertions
*/
public function test_get_items() {
// Not applicable for sync endpoint.
}
/**
* @doesNotPerformAssertions
*/
public function test_get_item() {
// Not applicable for sync endpoint.
}
public function test_create_item() {
wp_set_current_user( self::$editor_id );
$response = $this->dispatch_sync( array( $this->build_room( $this->get_post_room() ) ) );
$this->assertSame( 200, $response->get_status() );
}
/**
* @doesNotPerformAssertions
*/
public function test_update_item() {
// Not applicable for sync endpoint.
}
/**
* @doesNotPerformAssertions
*/
public function test_delete_item() {
// Not applicable for sync endpoint.
}
/**
* @doesNotPerformAssertions
*/
public function test_prepare_item() {
// Not applicable for sync endpoint.
}
/**
* @doesNotPerformAssertions
*/
public function test_get_item_schema() {
// Not applicable for sync endpoint.
}
/*
* Permission tests.
*/
public function test_sync_requires_authentication() {
wp_set_current_user( 0 );
$response = $this->dispatch_sync( array( $this->build_room( $this->get_post_room() ) ) );
$this->assertErrorResponse( 'rest_cannot_edit', $response, 401 );
}
public function test_sync_post_requires_edit_capability() {
wp_set_current_user( self::$subscriber_id );
$response = $this->dispatch_sync( array( $this->build_room( $this->get_post_room() ) ) );
$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
}
public function test_sync_post_allowed_with_edit_capability() {
wp_set_current_user( self::$editor_id );
$response = $this->dispatch_sync( array( $this->build_room( $this->get_post_room() ) ) );
$this->assertSame( 200, $response->get_status() );
}
public function test_sync_post_type_collection_requires_edit_posts_capability() {
wp_set_current_user( self::$subscriber_id );
$response = $this->dispatch_sync( array( $this->build_room( 'postType/post' ) ) );
$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
}
public function test_sync_post_type_collection_allowed_with_edit_posts_capability() {
wp_set_current_user( self::$editor_id );
$response = $this->dispatch_sync( array( $this->build_room( 'postType/post' ) ) );
$this->assertSame( 200, $response->get_status() );
}
public function test_sync_root_collection_allowed() {
wp_set_current_user( self::$editor_id );
$response = $this->dispatch_sync( array( $this->build_room( 'root/site' ) ) );
$this->assertSame( 200, $response->get_status() );
}
public function test_sync_taxonomy_collection_allowed() {
wp_set_current_user( self::$editor_id );
$response = $this->dispatch_sync( array( $this->build_room( 'taxonomy/category' ) ) );
$this->assertSame( 200, $response->get_status() );
}
public function test_sync_unknown_collection_kind_rejected() {
wp_set_current_user( self::$editor_id );
$response = $this->dispatch_sync( array( $this->build_room( 'unknown/entity' ) ) );
$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
}
public function test_sync_non_posttype_entity_with_object_id_rejected() {
wp_set_current_user( self::$editor_id );
$response = $this->dispatch_sync( array( $this->build_room( 'root/site:123' ) ) );
$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
}
public function test_sync_nonexistent_post_rejected() {
wp_set_current_user( self::$editor_id );
$response = $this->dispatch_sync( array( $this->build_room( 'postType/post:999999' ) ) );
$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
}
public function test_sync_permission_checked_per_room() {
wp_set_current_user( self::$editor_id );
// First room is allowed, second room is forbidden.
$response = $this->dispatch_sync(
array(
$this->build_room( $this->get_post_room() ),
$this->build_room( 'unknown/entity' ),
)
);
$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
}
/**
* @ticket 64890
*/
public function test_sync_malformed_object_id_rejected() {
wp_set_current_user( self::$editor_id );
$response = $this->dispatch_sync( array( $this->build_room( 'postType/post:1abc' ) ) );
$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
}
/**
* @ticket 64890
*/
public function test_sync_zero_object_id_rejected(): void {
wp_set_current_user( self::$editor_id );
$response = $this->dispatch_sync( array( $this->build_room( 'postType/post:0' ) ) );
$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
}
/**
* @ticket 64890
*/
public function test_sync_post_type_mismatch_rejected(): void {
wp_set_current_user( self::$editor_id );
// The test post is of type 'post', not 'page'.
$response = $this->dispatch_sync( array( $this->build_room( 'postType/page:' . self::$post_id ) ) );
$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
}
/**
* @ticket 64890
*/
public function test_sync_taxonomy_term_allowed(): void {
wp_set_current_user( self::$editor_id );
$response = $this->dispatch_sync( array( $this->build_room( 'taxonomy/category:' . self::$category_id ) ) );
$this->assertSame( 200, $response->get_status() );
}
/**
* @ticket 64890
*/
public function test_sync_nonexistent_taxonomy_term_rejected(): void {
wp_set_current_user( self::$editor_id );
$response = $this->dispatch_sync( array( $this->build_room( 'taxonomy/category:999999' ) ) );
$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
}
/**
* @ticket 64890
*/
public function test_sync_taxonomy_term_wrong_taxonomy_rejected(): void {
wp_set_current_user( self::$editor_id );
// The tag term exists in 'post_tag', not 'category'.
$response = $this->dispatch_sync( array( $this->build_room( 'taxonomy/category:' . self::$tag_id ) ) );
$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
}
/**
* @ticket 64890
*/
public function test_sync_comment_allowed(): void {
wp_set_current_user( self::$editor_id );
$response = $this->dispatch_sync( array( $this->build_room( 'root/comment:' . self::$comment_id ) ) );
$this->assertSame( 200, $response->get_status() );
}
/**
* @ticket 64890
*/
public function test_sync_nonexistent_comment_rejected(): void {
wp_set_current_user( self::$editor_id );
$response = $this->dispatch_sync( array( $this->build_room( 'root/comment:999999' ) ) );
$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
}
/**
* @ticket 64890
*/
public function test_sync_nonexistent_post_type_collection_rejected(): void {
wp_set_current_user( self::$editor_id );
$response = $this->dispatch_sync( array( $this->build_room( 'postType/nonexistent_type' ) ) );
$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
}
/*
* Validation tests.
*/
public function test_sync_invalid_room_format_rejected() {
wp_set_current_user( self::$editor_id );
$response = $this->dispatch_sync(
array(
$this->build_room( 'invalid-room-format' ),
)
);
$this->assertSame( 400, $response->get_status() );
}
/**
* Verifies that schema type validation rejects a non-string value for the
* update 'data' field, confirming that per-arg schema validation still runs
* with a route-level validate_callback registered.
*
* @ticket 64890
*/
public function test_sync_rejects_non_string_update_data(): void {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'POST', '/wp-sync/v1/updates' );
$request->set_body_params(
array(
'rooms' => array(
array(
'after' => 0,
'awareness' => array( 'user' => 'test' ),
'client_id' => 1,
'room' => $this->get_post_room(),
'updates' => array(
array(
'data' => 12345,
'type' => 'update',
),
),
),
),
)
);
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
/**
* Verifies that schema enum validation rejects an invalid update type,
* confirming that per-arg schema validation still runs with a route-level
* validate_callback registered.
*
* @ticket 64890
*/
public function test_sync_rejects_invalid_update_type_enum(): void {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'POST', '/wp-sync/v1/updates' );
$request->set_body_params(
array(
'rooms' => array(
array(
'after' => 0,
'awareness' => array( 'user' => 'test' ),
'client_id' => 1,
'room' => $this->get_post_room(),
'updates' => array(
array(
'data' => 'dGVzdA==',
'type' => 'invalid_type',
),
),
),
),
)
);
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
/**
* Verifies that schema required-field validation rejects a room missing
* the 'client_id' field, confirming that per-arg schema validation still
* runs with a route-level validate_callback registered.
*
* @ticket 64890
*/
public function test_sync_rejects_missing_required_room_field(): void {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'POST', '/wp-sync/v1/updates' );
$request->set_body_params(
array(
'rooms' => array(
array(
'after' => 0,
'awareness' => array( 'user' => 'test' ),
// 'client_id' deliberately omitted.
'room' => $this->get_post_room(),
'updates' => array(),
),
),
)
);
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
/**
* Verifies that the maxItems constraint rejects a request with more rooms
* than MAX_ROOMS_PER_REQUEST.
*
* @ticket 64890
*/
public function test_sync_rejects_rooms_exceeding_max_items(): void {
wp_set_current_user( self::$editor_id );
$rooms = array();
for ( $i = 0; $i < WP_HTTP_Polling_Sync_Server::MAX_ROOMS_PER_REQUEST + 1; $i++ ) {
$rooms[] = $this->build_room( 'root/site', $i + 1 );
}
$response = $this->dispatch_sync( $rooms );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
/**
* Verifies that the maxLength constraint rejects update data exceeding
* MAX_UPDATE_DATA_SIZE.
*
* @ticket 64890
*/
public function test_sync_rejects_update_data_exceeding_max_length(): void {
wp_set_current_user( self::$editor_id );
$oversized_data = str_repeat( 'a', WP_HTTP_Polling_Sync_Server::MAX_UPDATE_DATA_SIZE + 1 );
$request = new WP_REST_Request( 'POST', '/wp-sync/v1/updates' );
$request->set_body_params(
array(
'rooms' => array(
array(
'after' => 0,
'awareness' => array( 'user' => 'test' ),
'client_id' => 1,
'room' => $this->get_post_room(),
'updates' => array(
array(
'data' => $oversized_data,
'type' => 'update',
),
),
),
),
)
);
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
}
/**
* Verifies that the route-level validate_callback rejects a request body
* exceeding MAX_BODY_SIZE.
*
* @ticket 64890
*/
public function test_sync_rejects_oversized_request_body(): void {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'POST', '/wp-sync/v1/updates' );
// Set valid parsed params so per-arg schema validation passes first.
$request->set_body_params(
array(
'rooms' => array(
$this->build_room( $this->get_post_room() ),
),
)
);
// Set an oversized raw body to trigger the route-level validate_callback.
$request->set_body( str_repeat( 'x', WP_HTTP_Polling_Sync_Server::MAX_BODY_SIZE + 1 ) );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_sync_body_too_large', $response, 413 );
}
/*
* Response format tests.
*/
public function test_sync_response_structure() {
wp_set_current_user( self::$editor_id );
$response = $this->dispatch_sync( array( $this->build_room( $this->get_post_room() ) ) );
$this->assertSame( 200, $response->get_status() );
$data = $response->get_data();
$this->assertArrayHasKey( 'rooms', $data );
$this->assertCount( 1, $data['rooms'] );
$room_data = $data['rooms'][0];
$this->assertArrayHasKey( 'room', $room_data );
$this->assertArrayHasKey( 'awareness', $room_data );
$this->assertArrayHasKey( 'updates', $room_data );
$this->assertArrayHasKey( 'end_cursor', $room_data );
$this->assertArrayHasKey( 'total_updates', $room_data );
$this->assertArrayHasKey( 'should_compact', $room_data );
}
public function test_sync_response_room_matches_request() {
wp_set_current_user( self::$editor_id );
$room = $this->get_post_room();
$response = $this->dispatch_sync( array( $this->build_room( $room ) ) );
$data = $response->get_data();
$this->assertSame( $room, $data['rooms'][0]['room'] );
}
public function test_sync_end_cursor_is_positive_integer() {
wp_set_current_user( self::$editor_id );
$response = $this->dispatch_sync( array( $this->build_room( $this->get_post_room() ) ) );
$data = $response->get_data();
$this->assertIsInt( $data['rooms'][0]['end_cursor'] );
$this->assertGreaterThanOrEqual( 0, $data['rooms'][0]['end_cursor'] );
}
public function test_sync_empty_updates_returns_zero_total() {
wp_set_current_user( self::$editor_id );
$response = $this->dispatch_sync( array( $this->build_room( $this->get_post_room() ) ) );
$data = $response->get_data();
$this->assertSame( 0, $data['rooms'][0]['total_updates'] );
$this->assertEmpty( $data['rooms'][0]['updates'] );
}
/*
* Update tests.
*/
public function test_sync_update_delivered_to_other_client() {
wp_set_current_user( self::$editor_id );
$room = $this->get_post_room();
$update = array(
'type' => 'update',
'data' => 'dGVzdCBkYXRh',
);
// Client 1 sends an update.
$this->dispatch_sync(
array(
$this->build_room( $room, 1, 0, array( 'user' => 'client1' ), array( $update ) ),
)
);
// Client 2 requests updates from the beginning.
$response = $this->dispatch_sync(
array(
$this->build_room( $room, 2, 0 ),
)
);
$data = $response->get_data();
$updates = $data['rooms'][0]['updates'];
$this->assertNotEmpty( $updates );
$types = wp_list_pluck( $updates, 'type' );
$this->assertContains( 'update', $types );
}
public function test_sync_own_updates_not_returned() {
wp_set_current_user( self::$editor_id );
$room = $this->get_post_room();
$update = array(
'type' => 'update',
'data' => 'b3duIGRhdGE=',
);
// Client 1 sends an update.
$response = $this->dispatch_sync(
array(
$this->build_room( $room, 1, 0, array( 'user' => 'client1' ), array( $update ) ),
)
);
$data = $response->get_data();
$updates = $data['rooms'][0]['updates'];
// Client 1 should not see its own non-compaction update.
$this->assertEmpty( $updates );
}
public function test_sync_step1_update_stored_and_returned() {
wp_set_current_user( self::$editor_id );
$room = $this->get_post_room();
$update = array(
'type' => 'sync_step1',
'data' => 'c3RlcDE=',
);
// Client 1 sends sync_step1.
$this->dispatch_sync(
array(
$this->build_room( $room, 1, 0, array( 'user' => 'client1' ), array( $update ) ),
)
);
// Client 2 should see the sync_step1 update.
$response = $this->dispatch_sync(
array(
$this->build_room( $room, 2, 0 ),
)
);
$data = $response->get_data();
$types = wp_list_pluck( $data['rooms'][0]['updates'], 'type' );
$this->assertContains( 'sync_step1', $types );
}
public function test_sync_step2_update_stored_and_returned() {
wp_set_current_user( self::$editor_id );
$room = $this->get_post_room();
$update = array(
'type' => 'sync_step2',
'data' => 'c3RlcDI=',
);
// Client 1 sends sync_step2.
$this->dispatch_sync(
array(
$this->build_room( $room, 1, 0, array( 'user' => 'client1' ), array( $update ) ),
)
);
// Client 2 should see the sync_step2 update.
$response = $this->dispatch_sync(
array(
$this->build_room( $room, 2, 0 ),
)
);
$data = $response->get_data();
$types = wp_list_pluck( $data['rooms'][0]['updates'], 'type' );
$this->assertContains( 'sync_step2', $types );
}
public function test_sync_multiple_updates_in_single_request() {
wp_set_current_user( self::$editor_id );
$room = $this->get_post_room();
$updates = array(
array(
'type' => 'sync_step1',
'data' => 'c3RlcDE=',
),
array(
'type' => 'update',
'data' => 'dXBkYXRl',
),
);
// Client 1 sends multiple updates.
$this->dispatch_sync(
array(
$this->build_room( $room, 1, 0, array( 'user' => 'client1' ), $updates ),
)
);
// Client 2 should see both updates.
$response = $this->dispatch_sync(
array(
$this->build_room( $room, 2, 0 ),
)
);
$data = $response->get_data();
$room_updates = $data['rooms'][0]['updates'];
$this->assertCount( 2, $room_updates );
$this->assertSame( 2, $data['rooms'][0]['total_updates'] );
}
public function test_sync_update_data_preserved() {
wp_set_current_user( self::$editor_id );
$room = $this->get_post_room();
$update = array(
'type' => 'update',
'data' => 'cHJlc2VydmVkIGRhdGE=',
);
// Client 1 sends an update.
$this->dispatch_sync(
array(
$this->build_room( $room, 1, 0, array( 'user' => 'client1' ), array( $update ) ),
)
);
// Client 2 should receive the exact same data.
$response = $this->dispatch_sync(
array(
$this->build_room( $room, 2, 0 ),
)
);
$data = $response->get_data();
$room_updates = $data['rooms'][0]['updates'];
$this->assertSame( 'cHJlc2VydmVkIGRhdGE=', $room_updates[0]['data'] );
$this->assertSame( 'update', $room_updates[0]['type'] );
}
public function test_sync_total_updates_increments() {
wp_set_current_user( self::$editor_id );
$room = $this->get_post_room();
$update = array(
'type' => 'update',
'data' => 'dGVzdA==',
);
// Send three updates from different clients.
$this->dispatch_sync(
array(
$this->build_room( $room, 1, 0, array( 'user' => 'c1' ), array( $update ) ),
)
);
$this->dispatch_sync(
array(
$this->build_room( $room, 2, 0, array( 'user' => 'c2' ), array( $update ) ),
)
);
$this->dispatch_sync(
array(
$this->build_room( $room, 3, 0, array( 'user' => 'c3' ), array( $update ) ),
)
);
// Any client should see total_updates = 3.
$response = $this->dispatch_sync(
array(
$this->build_room( $room, 4, 0 ),
)
);
$data = $response->get_data();
$this->assertSame( 3, $data['rooms'][0]['total_updates'] );
}
/*
* Compaction tests.
*/
public function test_sync_should_compact_is_false_below_threshold() {
wp_set_current_user( self::$editor_id );
$room = $this->get_post_room();
$update = array(
'type' => 'update',
'data' => 'dGVzdA==',
);
// Client 1 sends a single update.
$response = $this->dispatch_sync(
array(
$this->build_room( $room, 1, 0, array( 'user' => 'c1' ), array( $update ) ),
)
);
$data = $response->get_data();
$this->assertFalse( $data['rooms'][0]['should_compact'] );
}
public function test_sync_should_compact_is_true_above_threshold_for_compactor() {
wp_set_current_user( self::$editor_id );
$room = $this->get_post_room();
$updates = array();
for ( $i = 0; $i < 51; $i++ ) {
$updates[] = array(
'type' => 'update',
'data' => base64_encode( "update-$i" ),
);
}
// Client 1 sends enough updates to exceed the compaction threshold.
$this->dispatch_sync(
array(
$this->build_room( $room, 1, 0, array( 'user' => 'c1' ), $updates ),
)
);
// Client 1 polls again. It is the lowest (only) client, so it is the compactor.
$response = $this->dispatch_sync(
array(
$this->build_room( $room, 1, 0, array( 'user' => 'c1' ) ),
)
);
$data = $response->get_data();
$this->assertTrue( $data['rooms'][0]['should_compact'] );
}
public function test_sync_should_compact_is_false_for_non_compactor() {
wp_set_current_user( self::$editor_id );
$room = $this->get_post_room();
$updates = array();
for ( $i = 0; $i < 51; $i++ ) {
$updates[] = array(
'type' => 'update',
'data' => base64_encode( "update-$i" ),
);
}
// Client 1 sends enough updates to exceed the compaction threshold.
$this->dispatch_sync(
array(
$this->build_room( $room, 1, 0, array( 'user' => 'c1' ), $updates ),
)
);
// Client 2 (higher ID than client 1) should not be the compactor.
$response = $this->dispatch_sync(
array(
$this->build_room( $room, 2, 0, array( 'user' => 'c2' ) ),
)
);
$data = $response->get_data();
$this->assertFalse( $data['rooms'][0]['should_compact'] );
}
public function test_sync_stale_compaction_succeeds_when_newer_compaction_exists() {
wp_set_current_user( self::$editor_id );
$room = $this->get_post_room();
$update = array(
'type' => 'update',
'data' => 'dGVzdA==',
);
// Client 1 sends an update to seed the room.
$response = $this->dispatch_sync(
array(
$this->build_room( $room, 1, 0, array( 'user' => 'c1' ), array( $update ) ),
)
);
$end_cursor = $response->get_data()['rooms'][0]['end_cursor'];
// Client 2 sends a compaction at the current cursor.
$compaction = array(
'type' => 'compaction',
'data' => 'Y29tcGFjdGVk',
);
$this->dispatch_sync(
array(
$this->build_room( $room, 2, $end_cursor, array( 'user' => 'c2' ), array( $compaction ) ),
)
);
// Client 3 sends a stale compaction at cursor 0. The server should find
// client 2's compaction in the updates after cursor 0 and silently discard
// this one.
$stale_compaction = array(
'type' => 'compaction',
'data' => 'c3RhbGU=',
);
$response = $this->dispatch_sync(
array(
$this->build_room( $room, 3, 0, array( 'user' => 'c3' ), array( $stale_compaction ) ),
)
);
$this->assertSame( 200, $response->get_status() );
// Verify the newer compaction is preserved and the stale one was not stored.
$response = $this->dispatch_sync(
array(
$this->build_room( $room, 4, 0, array( 'user' => 'c4' ) ),
)
);
$update_data = wp_list_pluck( $response->get_data()['rooms'][0]['updates'], 'data' );
$this->assertContains( 'Y29tcGFjdGVk', $update_data, 'The newer compaction should be preserved.' );
$this->assertNotContains( 'c3RhbGU=', $update_data, 'The stale compaction should not be stored.' );
}