-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathrest-revisions-controller.php
More file actions
1164 lines (1022 loc) · 40.1 KB
/
rest-revisions-controller.php
File metadata and controls
1164 lines (1022 loc) · 40.1 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
/**
* Unit tests covering WP_REST_Revisions_Controller functionality.
*
* @package WordPress
* @subpackage REST API
*
* @group restapi
*/
class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase {
protected static $post_id;
protected static $post_id_2;
protected static $page_id;
protected static $editor_id;
protected static $contributor_id;
private $total_revisions;
private $revisions;
private $revision_1;
private $revision_id1;
private $revision_2;
private $revision_id2;
private $revision_3;
private $revision_id3;
private $revision_2_1_id;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$post_id = $factory->post->create();
self::$post_id_2 = $factory->post->create();
self::$page_id = $factory->post->create( array( 'post_type' => 'page' ) );
self::$editor_id = $factory->user->create(
array(
'role' => 'editor',
)
);
self::$contributor_id = $factory->user->create(
array(
'role' => 'contributor',
)
);
wp_set_current_user( self::$editor_id );
wp_update_post(
array(
'post_content' => 'This content is better.',
'ID' => self::$post_id,
)
);
wp_update_post(
array(
'post_content' => 'This content is marvelous.',
'ID' => self::$post_id,
)
);
wp_update_post(
array(
'post_content' => 'This content is fantastic.',
'ID' => self::$post_id,
)
);
wp_update_post(
array(
'post_content' => 'A second post.',
'ID' => self::$post_id_2,
)
);
wp_update_post(
array(
'post_content' => 'A second post. How prolific.',
'ID' => self::$post_id_2,
)
);
wp_set_current_user( 0 );
}
public static function wpTearDownAfterClass() {
// Also deletes revisions.
wp_delete_post( self::$post_id, true );
wp_delete_post( self::$post_id_2, true );
wp_delete_post( self::$page_id, true );
self::delete_user( self::$editor_id );
self::delete_user( self::$contributor_id );
}
public function set_up() {
parent::set_up();
// Set first post revision vars.
$revisions = wp_get_post_revisions( self::$post_id );
$this->total_revisions = count( $revisions );
$this->revisions = $revisions;
$this->revision_1 = array_pop( $revisions );
$this->revision_id1 = $this->revision_1->ID;
$this->revision_2 = array_pop( $revisions );
$this->revision_id2 = $this->revision_2->ID;
$this->revision_3 = array_pop( $revisions );
$this->revision_id3 = $this->revision_3->ID;
// Set second post revision vars.
$revisions = wp_get_post_revisions( self::$post_id_2 );
$post_2_revision = array_pop( $revisions );
$this->revision_2_1_id = $post_2_revision->ID;
}
public function _filter_map_meta_cap_remove_no_allow_revisions( $caps, $cap, $user_id, $args ) {
if ( 'delete_post' !== $cap || empty( $args ) ) {
return $caps;
}
$post = get_post( $args[0] );
if ( ! $post || 'revision' !== $post->post_type ) {
return $caps;
}
$key = array_search( 'do_not_allow', $caps, true );
if ( false !== $key ) {
unset( $caps[ $key ] );
}
return $caps;
}
public function test_register_routes() {
$routes = rest_get_server()->get_routes();
$this->assertArrayHasKey( '/wp/v2/posts/(?P<parent>[\d]+)/revisions', $routes );
$this->assertArrayHasKey( '/wp/v2/posts/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)', $routes );
$this->assertArrayHasKey( '/wp/v2/pages/(?P<parent>[\d]+)/revisions', $routes );
$this->assertArrayHasKey( '/wp/v2/pages/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)', $routes );
}
public function test_context_param() {
// Collection.
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSameSets( array( 'view', 'edit', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] );
// Single.
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_1->ID );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSameSets( array( 'view', 'edit', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] );
}
public function test_get_items() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( 200, $response->get_status() );
$this->assertCount( $this->total_revisions, $data );
// Reverse chronology.
$this->assertSame( $this->revision_id3, $data[0]['id'] );
$this->check_get_revision_response( $data[0], $this->revision_3 );
$this->assertSame( $this->revision_id2, $data[1]['id'] );
$this->check_get_revision_response( $data[1], $this->revision_2 );
$this->assertSame( $this->revision_id1, $data[2]['id'] );
$this->check_get_revision_response( $data[2], $this->revision_1 );
}
/**
* @ticket 56481
*/
public function test_get_items_with_head_request_should_not_prepare_revisions_data() {
wp_set_current_user( self::$editor_id );
$hook_name = 'rest_prepare_revision';
$filter = new MockAction();
$callback = array( $filter, 'filter' );
add_filter( $hook_name, $callback );
$request = new WP_REST_Request( 'HEAD', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$response = rest_get_server()->dispatch( $request );
remove_filter( $hook_name, $callback );
$this->assertNotWPError( $response );
$response = rest_ensure_response( $response );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
$this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
* @dataProvider data_readable_http_methods
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_no_permission( $method ) {
wp_set_current_user( 0 );
$request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/revisions' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_read', $response, 401 );
wp_set_current_user( self::$contributor_id );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_read', $response, 403 );
}
/**
* Data provider intended to provide HTTP method names for testing GET and HEAD requests.
*
* @return array
*/
public static function data_readable_http_methods() {
return array(
'GET request' => array( 'GET' ),
'HEAD request' => array( 'HEAD' ),
);
}
/**
* @dataProvider data_readable_http_methods
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_missing_parent( $method ) {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( $method, '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 );
}
/**
* @dataProvider data_readable_http_methods
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_invalid_parent_post_type( $method ) {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$page_id . '/revisions' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 );
}
public function test_get_item() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
$this->check_get_revision_response( $response, $this->revision_1 );
$fields = array(
'author',
'date',
'date_gmt',
'modified',
'modified_gmt',
'guid',
'id',
'meta',
'parent',
'slug',
'title',
'excerpt',
'content',
);
$data = $response->get_data();
$this->assertSameSets( $fields, array_keys( $data ) );
$this->assertSame( self::$editor_id, $data['author'] );
}
/**
* @dataProvider data_readable_http_methods
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_item_should_allow_adding_headers_via_filter( $method ) {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
$hook_name = 'rest_prepare_revision';
$filter = new MockAction();
$callback = array( $filter, 'filter' );
add_filter( $hook_name, $callback );
$header_filter = new class() {
public static function add_custom_header( $response ) {
$response->header( 'X-Test-Header', 'Test' );
return $response;
}
};
add_filter( $hook_name, array( $header_filter, 'add_custom_header' ) );
$response = rest_get_server()->dispatch( $request );
remove_filter( $hook_name, $callback );
remove_filter( $hook_name, array( $header_filter, 'add_custom_header' ) );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
$this->assertSame( 1, $filter->get_call_count(), 'The "' . $hook_name . '" filter was not called when it should be for GET/HEAD requests.' );
$headers = $response->get_headers();
$this->assertArrayHasKey( 'X-Test-Header', $headers, 'The "X-Test-Header" header should be present in the response.' );
$this->assertSame( 'Test', $headers['X-Test-Header'], 'The "X-Test-Header" header value should be equal to "Test".' );
if ( 'GET' === $method ) {
return null;
}
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
* @dataProvider data_head_request_with_specified_fields_returns_success_response
* @ticket 56481
*
* @param string $path The path to test.
*/
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'HEAD', sprintf( $path, self::$post_id, $this->revision_id1 ) );
$request->set_param( '_fields', 'id' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* Data provider intended to provide paths for testing HEAD requests.
*
* @return array
*/
public static function data_head_request_with_specified_fields_returns_success_response() {
return array(
'get_item request' => array( '/wp/v2/posts/%d/revisions/%d' ),
'get_items request' => array( '/wp/v2/posts/%d/revisions' ),
);
}
public function test_get_item_embed_context() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
$request->set_param( 'context', 'embed' );
$response = rest_get_server()->dispatch( $request );
$fields = array(
'author',
'date',
'id',
'parent',
'slug',
'title',
'excerpt',
);
$data = $response->get_data();
$this->assertSameSets( $fields, array_keys( $data ) );
}
/**
* @dataProvider data_readable_http_methods
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_item_no_permission( $method ) {
wp_set_current_user( 0 );
$request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_read', $response, 401 );
wp_set_current_user( self::$contributor_id );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_read', $response, 403 );
}
/**
* @dataProvider data_readable_http_methods
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_item_missing_parent( $method ) {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( $method, '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions/' . $this->revision_id1 );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 );
}
/**
* @dataProvider data_readable_http_methods
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_item_invalid_parent_post_type( $method ) {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$page_id . '/revisions/' . $this->revision_id1 );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 );
}
/**
* @ticket 59875
*/
public function test_get_item_valid_parent_id() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( self::$post_id, $data['parent'], "The returned revision's id should match the parent id." );
$this->check_get_revision_response( $response, $this->revision_1 );
}
/**
* @dataProvider data_readable_http_methods
* @ticket 59875
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_item_invalid_parent_id( $method ) {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_2_1_id );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_revision_parent_id_mismatch', $response, 404 );
$expected_message = 'The revision does not belong to the specified parent with id of "' . self::$post_id . '"';
$this->assertSame( $expected_message, $response->as_error()->get_error_messages()[0], 'The message must contain the correct parent ID.' );
}
public function test_delete_item() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
$request->set_param( 'force', true );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
$this->assertNotNull( get_post( $this->revision_id1 ) );
}
/**
* @ticket 49645
*/
public function test_delete_item_parent_check() {
wp_set_current_user( self::$contributor_id );
$request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
$request->set_param( 'force', true );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
$this->assertNotNull( get_post( $this->revision_id1 ) );
}
/**
* @ticket 43709
*/
public function test_delete_item_remove_do_not_allow() {
wp_set_current_user( self::$editor_id );
add_filter( 'map_meta_cap', array( $this, '_filter_map_meta_cap_remove_no_allow_revisions' ), 10, 4 );
$request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
$request->set_param( 'force', true );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
$this->assertNull( get_post( $this->revision_id1 ) );
}
/**
* @ticket 43709
*/
public function test_delete_item_cannot_delete_parent() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
$request->set_param( 'force', true );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
$this->assertNotNull( get_post( $this->revision_id1 ) );
}
/**
* @ticket 38494
* @ticket 43709
*/
public function test_delete_item_no_trash() {
wp_set_current_user( self::$editor_id );
add_filter( 'map_meta_cap', array( $this, '_filter_map_meta_cap_remove_no_allow_revisions' ), 10, 4 );
$request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
$request->set_param( 'force', 'false' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
// Ensure the revision still exists.
$this->assertNotNull( get_post( $this->revision_id1 ) );
}
public function test_delete_item_no_permission() {
wp_set_current_user( self::$contributor_id );
$request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
}
public function test_prepare_item() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
$this->check_get_revision_response( $response, $this->revision_1 );
}
public function test_prepare_item_limit_fields() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
$endpoint = new WP_REST_Revisions_Controller( 'post' );
$request->set_param( 'context', 'edit' );
$request->set_param( '_fields', 'id,slug' );
$revision = get_post( $this->revision_id1 );
$response = $endpoint->prepare_item_for_response( $revision, $request );
$this->assertSame(
array(
'id',
'slug',
),
array_keys( $response->get_data() )
);
}
public function test_get_item_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertCount( 13, $properties );
$this->assertArrayHasKey( 'author', $properties );
$this->assertArrayHasKey( 'content', $properties );
$this->assertArrayHasKey( 'date', $properties );
$this->assertArrayHasKey( 'date_gmt', $properties );
$this->assertArrayHasKey( 'excerpt', $properties );
$this->assertArrayHasKey( 'guid', $properties );
$this->assertArrayHasKey( 'id', $properties );
$this->assertArrayHasKey( 'modified', $properties );
$this->assertArrayHasKey( 'modified_gmt', $properties );
$this->assertArrayHasKey( 'parent', $properties );
$this->assertArrayHasKey( 'slug', $properties );
$this->assertArrayHasKey( 'title', $properties );
$this->assertArrayHasKey( 'meta', $properties );
}
public function test_create_item() {
$request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_no_route', $response, 404 );
}
public function test_update_item() {
$request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_no_route', $response, 404 );
}
public function test_get_additional_field_registration() {
$schema = array(
'type' => 'integer',
'description' => 'Some integer of mine',
'enum' => array( 1, 2, 3, 4 ),
'context' => array( 'view', 'edit' ),
);
register_rest_field(
'post-revision',
'my_custom_int',
array(
'schema' => $schema,
'get_callback' => array( $this, 'additional_field_get_callback' ),
'update_callback' => array( $this, 'additional_field_update_callback' ),
)
);
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] );
$this->assertSame( $schema, $data['schema']['properties']['my_custom_int'] );
wp_set_current_user( 1 );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
$response = rest_get_server()->dispatch( $request );
$this->assertArrayHasKey( 'my_custom_int', $response->data );
global $wp_rest_additional_fields;
$wp_rest_additional_fields = array();
}
public function additional_field_get_callback( $response_data, $field_name ) {
return get_post_meta( $response_data['id'], $field_name, true );
}
public function additional_field_update_callback( $value, $post, $field_name ) {
update_post_meta( $post->ID, $field_name, $value );
}
protected function check_get_revision_response( $response, $revision ) {
if ( $response instanceof WP_REST_Response ) {
$links = $response->get_links();
$response = $response->get_data();
} else {
$this->assertArrayHasKey( '_links', $response );
$links = $response['_links'];
}
$this->assertEquals( $revision->post_author, $response['author'] );
$rendered_content = apply_filters( 'the_content', $revision->post_content );
$this->assertSame( $rendered_content, $response['content']['rendered'] );
$this->assertSame( mysql_to_rfc3339( $revision->post_date ), $response['date'] );
$this->assertSame( mysql_to_rfc3339( $revision->post_date_gmt ), $response['date_gmt'] );
$rendered_excerpt = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $revision->post_excerpt, $revision ) );
$this->assertSame( $rendered_excerpt, $response['excerpt']['rendered'] );
$rendered_guid = apply_filters( 'get_the_guid', $revision->guid, $revision->ID );
$this->assertSame( $rendered_guid, $response['guid']['rendered'] );
$this->assertSame( $revision->ID, $response['id'] );
$this->assertSame( mysql_to_rfc3339( $revision->post_modified ), $response['modified'] );
$this->assertSame( mysql_to_rfc3339( $revision->post_modified_gmt ), $response['modified_gmt'] );
$this->assertSame( $revision->post_name, $response['slug'] );
$rendered_title = get_the_title( $revision->ID );
$this->assertSame( $rendered_title, $response['title']['rendered'] );
$parent = get_post( $revision->post_parent );
$parent_controller = new WP_REST_Posts_Controller( $parent->post_type );
$parent_object = get_post_type_object( $parent->post_type );
$parent_base = ! empty( $parent_object->rest_base ) ? $parent_object->rest_base : $parent_object->name;
$this->assertSame( rest_url( '/wp/v2/' . $parent_base . '/' . $revision->post_parent ), $links['parent'][0]['href'] );
}
public function test_get_item_sets_up_postdata() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
rest_get_server()->dispatch( $request );
$post = get_post();
$parent_post_id = wp_is_post_revision( $post->ID );
$this->assertSame( $post->ID, $this->revision_id1 );
$this->assertSame( $parent_post_id, self::$post_id );
}
/**
* Test the pagination header of the first page.
*
* @dataProvider data_readable_http_methods
* @ticket 40510
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_pagination_header_of_the_first_page( $method ) {
wp_set_current_user( self::$editor_id );
$rest_route = '/wp/v2/posts/' . self::$post_id . '/revisions';
$per_page = 2;
$total_pages = (int) ceil( $this->total_revisions / $per_page );
$page = 1; // First page.
$request = new WP_REST_Request( $method, $rest_route );
$request->set_query_params(
array(
'per_page' => $per_page,
'page' => $page,
)
);
$response = rest_get_server()->dispatch( $request );
$headers = $response->get_headers();
$this->assertSame( $this->total_revisions, $headers['X-WP-Total'] );
$this->assertSame( $total_pages, $headers['X-WP-TotalPages'] );
$next_link = add_query_arg(
array(
'per_page' => $per_page,
'page' => $page + 1,
),
rest_url( $rest_route )
);
$this->assertStringNotContainsString( 'rel="prev"', $headers['Link'] );
$this->assertStringContainsString( '<' . $next_link . '>; rel="next"', $headers['Link'] );
}
/**
* Test that the 'protected' property is not included in the revision schema's content.
*
* Verifies the fix for the issue where revision schema incorrectly included
* 'protected' properties that don't actually exist in revision responses.
*
* @ticket 53417
*/
public function test_revision_schema_content_does_not_have_protected() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertArrayHasKey( 'content', $properties );
$this->assertArrayHasKey( 'properties', $properties['content'] );
$this->assertArrayNotHasKey(
'protected',
$properties['content']['properties'],
'The "protected" property should not be present in revision content schema'
);
$this->assertArrayHasKey( 'raw', $properties['content']['properties'] );
$this->assertArrayHasKey( 'rendered', $properties['content']['properties'] );
$this->assertArrayHasKey( 'block_version', $properties['content']['properties'] );
}
/**
* Test that the 'protected' property is not included in the revision schema's excerpt.
*
* Verifies the fix for the issue where revision schema incorrectly included
* 'protected' properties that don't actually exist in revision responses.
*
* @ticket 53417
*/
public function test_revision_schema_excerpt_does_not_have_protected() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertArrayHasKey( 'excerpt', $properties );
$this->assertArrayHasKey( 'properties', $properties['excerpt'] );
$this->assertArrayNotHasKey(
'protected',
$properties['excerpt']['properties'],
'The "protected" property should not be present in revision excerpt schema'
);
$this->assertArrayHasKey( 'raw', $properties['excerpt']['properties'] );
$this->assertArrayHasKey( 'rendered', $properties['excerpt']['properties'] );
}
/**
* Test that revision response data does not include the 'protected' property in excerpt.
*
* Verifies that actual revision responses don't include the 'protected' property,
* matching the corrected schema.
*
* @ticket 53417
*/
public function test_revision_response_excerpt_does_not_include_protected() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
$data = $response->get_data();
$this->assertArrayHasKey( 'excerpt', $data );
$this->assertArrayNotHasKey(
'protected',
$data['excerpt'],
'The "protected" property should not be present in revision excerpt response'
);
$this->assertArrayHasKey( 'rendered', $data['excerpt'] );
}
/**
* Test the pagination header of the last page.
*
* @dataProvider data_readable_http_methods
* @ticket 40510
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_pagination_header_of_the_last_page( $method ) {
wp_set_current_user( self::$editor_id );
$rest_route = '/wp/v2/posts/' . self::$post_id . '/revisions';
$per_page = 2;
$total_pages = (int) ceil( $this->total_revisions / $per_page );
$page = 2; // Last page.
$request = new WP_REST_Request( $method, $rest_route );
$request->set_query_params(
array(
'per_page' => $per_page,
'page' => $page,
)
);
$response = rest_get_server()->dispatch( $request );
$headers = $response->get_headers();
$this->assertSame( $this->total_revisions, $headers['X-WP-Total'] );
$this->assertSame( $total_pages, $headers['X-WP-TotalPages'] );
$prev_link = add_query_arg(
array(
'per_page' => $per_page,
'page' => $page - 1,
),
rest_url( $rest_route )
);
$this->assertStringContainsString( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
}
/**
* Test that invalid 'per_page' query should error.
*
* @dataProvider data_readable_http_methods
* @ticket 40510
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_invalid_per_page_should_error( $method ) {
wp_set_current_user( self::$editor_id );
$per_page = -1; // Invalid number.
$expected_error = 'rest_invalid_param';
$expected_status = 400;
$request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/revisions' );
$request->set_param( 'per_page', $per_page );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( $expected_error, $response, $expected_status );
}
/**
* Test that out of bounds 'page' query should error.
*
* @dataProvider data_readable_http_methods
* @ticket 40510
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_out_of_bounds_page_should_error( $method ) {
wp_set_current_user( self::$editor_id );
$per_page = 2;
$total_pages = (int) ceil( $this->total_revisions / $per_page );
$page = $total_pages + 1; // Out of bound page.
$expected_error = 'rest_revision_invalid_page_number';
$expected_status = 400;
$request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/revisions' );
$request->set_query_params(
array(
'per_page' => $per_page,
'page' => $page,
)
);
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( $expected_error, $response, $expected_status );
}
/**
* Test that impossibly high 'page' query should error.
*
* @dataProvider data_readable_http_methods
* @ticket 40510
* @ticket 56481
*
* @param string $method The HTTP method to use.
*/
public function test_get_items_invalid_max_pages_should_error( $method ) {
wp_set_current_user( self::$editor_id );
$per_page = 2;
$page = REST_TESTS_IMPOSSIBLY_HIGH_NUMBER; // Invalid number.
$expected_error = 'rest_revision_invalid_page_number';
$expected_status = 400;
$request = new WP_REST_Request( $method, '/wp/v2/posts/' . self::$post_id . '/revisions' );
$request->set_query_params(
array(
'per_page' => $per_page,
'page' => $page,
)
);
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( $expected_error, $response, $expected_status );
}
/**
* Test the search query.
*
* @ticket 40510
*/
public function test_get_items_search_query() {
wp_set_current_user( self::$editor_id );
$search_string = 'better';
$expected_count = 1;
$expected_content = 'This content is better.';
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$request->set_param( 'search', $search_string );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertCount( $expected_count, $data );
$this->assertStringContainsString( $expected_content, $data[0]['content']['rendered'] );
}
/**
* Test that the default query should fetch all revisions.
*
* @ticket 40510
*/
public function test_get_items_default_query_should_fetch_all_revisions() {
wp_set_current_user( self::$editor_id );
$expected_count = $this->total_revisions;
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$response = rest_get_server()->dispatch( $request );
$this->assertCount( $expected_count, $response->get_data() );
}
/**
* Test that 'offset' query shouldn't work without 'per_page' (fallback -1).
*
* @ticket 40510
*/
public function test_get_items_offset_should_not_work_without_per_page() {
wp_set_current_user( self::$editor_id );
$offset = 1;
$expected_count = $this->total_revisions;
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$request->set_param( 'offset', $offset );
$response = rest_get_server()->dispatch( $request );
$this->assertCount( $expected_count, $response->get_data() );
}
/**
* Test that 'offset' query should work with 'per_page'.
*
* @ticket 40510
*/
public function test_get_items_offset_should_work_with_per_page() {
wp_set_current_user( self::$editor_id );
$per_page = 2;
$offset = 1;
$expected_count = 2;
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$request->set_query_params(
array(
'offset' => $offset,
'per_page' => $per_page,
)
);
$response = rest_get_server()->dispatch( $request );
$this->assertCount( $expected_count, $response->get_data() );
}
/**
* Test that 'offset' query should take priority over 'page'.
*
* @ticket 40510
*/
public function test_get_items_offset_should_take_priority_over_page() {
wp_set_current_user( self::$editor_id );
$per_page = 2;
$offset = 1;
$page = 1;
$expected_count = 2;
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$request->set_query_params(
array(
'offset' => $offset,
'per_page' => $per_page,
'page' => $page,
)
);
$response = rest_get_server()->dispatch( $request );
$this->assertCount( $expected_count, $response->get_data() );
}
/**
* Test that 'offset' query, as the total revisions count, should return empty data.
*
* @ticket 40510
*/
public function test_get_items_total_revisions_offset_should_return_empty_data() {
wp_set_current_user( self::$editor_id );
$per_page = 2;
$offset = $this->total_revisions;