forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest-api.php
More file actions
2381 lines (2211 loc) · 56.3 KB
/
rest-api.php
File metadata and controls
2381 lines (2211 loc) · 56.3 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
/**
* REST API functions.
*
* @package WordPress
* @subpackage REST API
*/
require_once ABSPATH . 'wp-admin/includes/admin.php';
require_once ABSPATH . WPINC . '/rest-api.php';
require_once __DIR__ . '/../includes/class-jsonserializable-object.php';
/**
* @group restapi
*/
class Tests_REST_API extends WP_UnitTestCase {
public function setUp() {
parent::setUp();
// Override the normal server with our spying server.
$GLOBALS['wp_rest_server'] = new Spy_REST_Server();
do_action( 'rest_api_init', $GLOBALS['wp_rest_server'] );
}
public function tearDown() {
remove_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) );
parent::tearDown();
}
/**
* Checks that the main classes are loaded.
*/
function test_rest_api_active() {
$this->assertTrue( class_exists( 'WP_REST_Server' ) );
$this->assertTrue( class_exists( 'WP_REST_Request' ) );
$this->assertTrue( class_exists( 'WP_REST_Response' ) );
$this->assertTrue( class_exists( 'WP_REST_Posts_Controller' ) );
}
/**
* The rest_api_init hook should have been registered with init, and should
* have a default priority of 10.
*/
function test_init_action_added() {
$this->assertSame( 10, has_action( 'init', 'rest_api_init' ) );
}
public function test_add_extra_api_taxonomy_arguments() {
$taxonomy = get_taxonomy( 'category' );
$this->assertTrue( $taxonomy->show_in_rest );
$this->assertSame( 'categories', $taxonomy->rest_base );
$this->assertSame( 'WP_REST_Terms_Controller', $taxonomy->rest_controller_class );
$taxonomy = get_taxonomy( 'post_tag' );
$this->assertTrue( $taxonomy->show_in_rest );
$this->assertSame( 'tags', $taxonomy->rest_base );
$this->assertSame( 'WP_REST_Terms_Controller', $taxonomy->rest_controller_class );
}
public function test_add_extra_api_post_type_arguments() {
$post_type = get_post_type_object( 'post' );
$this->assertTrue( $post_type->show_in_rest );
$this->assertSame( 'posts', $post_type->rest_base );
$this->assertSame( 'WP_REST_Posts_Controller', $post_type->rest_controller_class );
$post_type = get_post_type_object( 'page' );
$this->assertTrue( $post_type->show_in_rest );
$this->assertSame( 'pages', $post_type->rest_base );
$this->assertSame( 'WP_REST_Posts_Controller', $post_type->rest_controller_class );
$post_type = get_post_type_object( 'attachment' );
$this->assertTrue( $post_type->show_in_rest );
$this->assertSame( 'media', $post_type->rest_base );
$this->assertSame( 'WP_REST_Attachments_Controller', $post_type->rest_controller_class );
}
/**
* Check that a single route is canonicalized.
*
* Ensures that single and multiple routes are handled correctly.
*/
public function test_route_canonicalized() {
register_rest_route(
'test-ns',
'/test',
array(
'methods' => array( 'GET' ),
'callback' => '__return_null',
'permission_callback' => '__return_true',
)
);
// Check the route was registered correctly.
$endpoints = $GLOBALS['wp_rest_server']->get_raw_endpoint_data();
$this->assertArrayHasKey( '/test-ns/test', $endpoints );
// Check the route was wrapped in an array.
$endpoint = $endpoints['/test-ns/test'];
$this->assertArrayNotHasKey( 'callback', $endpoint );
$this->assertArrayHasKey( 'namespace', $endpoint );
$this->assertSame( 'test-ns', $endpoint['namespace'] );
// Grab the filtered data.
$filtered_endpoints = $GLOBALS['wp_rest_server']->get_routes();
$this->assertArrayHasKey( '/test-ns/test', $filtered_endpoints );
$endpoint = $filtered_endpoints['/test-ns/test'];
$this->assertCount( 1, $endpoint );
$this->assertArrayHasKey( 'callback', $endpoint[0] );
$this->assertArrayHasKey( 'methods', $endpoint[0] );
$this->assertArrayHasKey( 'args', $endpoint[0] );
}
/**
* Check that a single route is canonicalized.
*
* Ensures that single and multiple routes are handled correctly.
*/
public function test_route_canonicalized_multiple() {
register_rest_route(
'test-ns',
'/test',
array(
array(
'methods' => array( 'GET' ),
'callback' => '__return_null',
'permission_callback' => '__return_true',
),
array(
'methods' => array( 'POST' ),
'callback' => '__return_null',
'permission_callback' => '__return_true',
),
)
);
// Check the route was registered correctly.
$endpoints = $GLOBALS['wp_rest_server']->get_raw_endpoint_data();
$this->assertArrayHasKey( '/test-ns/test', $endpoints );
// Check the route was wrapped in an array.
$endpoint = $endpoints['/test-ns/test'];
$this->assertArrayNotHasKey( 'callback', $endpoint );
$this->assertArrayHasKey( 'namespace', $endpoint );
$this->assertSame( 'test-ns', $endpoint['namespace'] );
$filtered_endpoints = $GLOBALS['wp_rest_server']->get_routes();
$endpoint = $filtered_endpoints['/test-ns/test'];
$this->assertCount( 2, $endpoint );
// Check for both methods.
foreach ( array( 0, 1 ) as $key ) {
$this->assertArrayHasKey( 'callback', $endpoint[ $key ] );
$this->assertArrayHasKey( 'methods', $endpoint[ $key ] );
$this->assertArrayHasKey( 'args', $endpoint[ $key ] );
}
}
/**
* Check that routes are merged by default.
*/
public function test_route_merge() {
register_rest_route(
'test-ns',
'/test',
array(
'methods' => array( 'GET' ),
'callback' => '__return_null',
'permission_callback' => '__return_true',
)
);
register_rest_route(
'test-ns',
'/test',
array(
'methods' => array( 'POST' ),
'callback' => '__return_null',
'permission_callback' => '__return_true',
)
);
// Check both routes exist.
$endpoints = $GLOBALS['wp_rest_server']->get_routes();
$endpoint = $endpoints['/test-ns/test'];
$this->assertCount( 2, $endpoint );
}
/**
* Check that we can override routes.
*/
public function test_route_override() {
register_rest_route(
'test-ns',
'/test',
array(
'methods' => array( 'GET' ),
'callback' => '__return_null',
'permission_callback' => '__return_true',
'should_exist' => false,
)
);
register_rest_route(
'test-ns',
'/test',
array(
'methods' => array( 'POST' ),
'callback' => '__return_null',
'permission_callback' => '__return_true',
'should_exist' => true,
),
true
);
// Check we only have one route.
$endpoints = $GLOBALS['wp_rest_server']->get_routes();
$endpoint = $endpoints['/test-ns/test'];
$this->assertCount( 1, $endpoint );
// Check it's the right one.
$this->assertArrayHasKey( 'should_exist', $endpoint[0] );
$this->assertTrue( $endpoint[0]['should_exist'] );
}
/**
* Test that we reject routes without namespaces
*
* @expectedIncorrectUsage register_rest_route
*/
public function test_route_reject_empty_namespace() {
register_rest_route(
'',
'/test-empty-namespace',
array(
'methods' => array( 'POST' ),
'callback' => '__return_null',
'permission_callback' => '__return_true',
),
true
);
$endpoints = $GLOBALS['wp_rest_server']->get_routes();
$this->assertArrayNotHasKey( '/test-empty-namespace', $endpoints );
}
/**
* Test that we reject empty routes
*
* @expectedIncorrectUsage register_rest_route
*/
public function test_route_reject_empty_route() {
register_rest_route(
'/test-empty-route',
'',
array(
'methods' => array( 'POST' ),
'callback' => '__return_null',
'permission_callback' => '__return_true',
),
true
);
$endpoints = $GLOBALS['wp_rest_server']->get_routes();
$this->assertArrayNotHasKey( '/test-empty-route', $endpoints );
}
/**
* The rest_route query variable should be registered.
*/
function test_rest_route_query_var() {
rest_api_init();
$this->assertContains( 'rest_route', $GLOBALS['wp']->public_query_vars );
}
public function test_route_method() {
register_rest_route(
'test-ns',
'/test',
array(
'methods' => array( 'GET' ),
'callback' => '__return_null',
'permission_callback' => '__return_true',
)
);
$routes = $GLOBALS['wp_rest_server']->get_routes();
$this->assertSame( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) );
}
/**
* The 'methods' arg should accept a single value as well as array.
*/
public function test_route_method_string() {
register_rest_route(
'test-ns',
'/test',
array(
'methods' => 'GET',
'callback' => '__return_null',
'permission_callback' => '__return_true',
)
);
$routes = $GLOBALS['wp_rest_server']->get_routes();
$this->assertSame( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) );
}
/**
* The 'methods' arg should accept a single value as well as array.
*/
public function test_route_method_array() {
register_rest_route(
'test-ns',
'/test',
array(
'methods' => array( 'GET', 'POST' ),
'callback' => '__return_null',
'permission_callback' => '__return_true',
)
);
$routes = $GLOBALS['wp_rest_server']->get_routes();
$this->assertSame(
$routes['/test-ns/test'][0]['methods'],
array(
'GET' => true,
'POST' => true,
)
);
}
/**
* The 'methods' arg should a comma-separated string.
*/
public function test_route_method_comma_separated() {
register_rest_route(
'test-ns',
'/test',
array(
'methods' => 'GET,POST',
'callback' => '__return_null',
'permission_callback' => '__return_true',
)
);
$routes = $GLOBALS['wp_rest_server']->get_routes();
$this->assertSame(
$routes['/test-ns/test'][0]['methods'],
array(
'GET' => true,
'POST' => true,
)
);
}
public function test_options_request() {
register_rest_route(
'test-ns',
'/test',
array(
'methods' => 'GET,POST',
'callback' => '__return_null',
'permission_callback' => '__return_true',
)
);
$request = new WP_REST_Request( 'OPTIONS', '/test-ns/test' );
$response = rest_handle_options_request( null, $GLOBALS['wp_rest_server'], $request );
$response = rest_send_allow_header( $response, $GLOBALS['wp_rest_server'], $request );
$headers = $response->get_headers();
$this->assertArrayHasKey( 'Allow', $headers );
$this->assertSame( 'GET, POST', $headers['Allow'] );
}
/**
* Ensure that the OPTIONS handler doesn't kick in for non-OPTIONS requests.
*/
public function test_options_request_not_options() {
register_rest_route(
'test-ns',
'/test',
array(
'methods' => 'GET,POST',
'callback' => '__return_true',
'permission_callback' => '__return_true',
)
);
$request = new WP_REST_Request( 'GET', '/test-ns/test' );
$response = rest_handle_options_request( null, $GLOBALS['wp_rest_server'], $request );
$this->assertNull( $response );
}
/**
* Ensure that result fields are not allowed if no request['_fields'] is present.
*/
public function test_rest_filter_response_fields_no_request_filter() {
$response = new WP_REST_Response();
$response->set_data( array( 'a' => true ) );
$request = array();
$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame( array( 'a' => true ), $response->get_data() );
}
/**
* Ensure that result fields are allowed if request['_fields'] is present.
*/
public function test_rest_filter_response_fields_single_field_filter() {
$response = new WP_REST_Response();
$response->set_data(
array(
'a' => 0,
'b' => 1,
'c' => 2,
)
);
$request = array(
'_fields' => 'b',
);
$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame( array( 'b' => 1 ), $response->get_data() );
}
/**
* Ensure that multiple comma-separated fields may be allowed with request['_fields'].
*/
public function test_rest_filter_response_fields_multi_field_filter() {
$response = new WP_REST_Response();
$response->set_data(
array(
'a' => 0,
'b' => 1,
'c' => 2,
'd' => 3,
'e' => 4,
'f' => 5,
)
);
$request = array(
'_fields' => 'b,c,e',
);
$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame(
array(
'b' => 1,
'c' => 2,
'e' => 4,
),
$response->get_data()
);
}
/**
* Ensure that multiple comma-separated fields may be allowed
* with request['_fields'] using query parameter array syntax.
*/
public function test_rest_filter_response_fields_multi_field_filter_array() {
$response = new WP_REST_Response();
$response->set_data(
array(
'a' => 0,
'b' => 1,
'c' => 2,
'd' => 3,
'e' => 4,
'f' => 5,
)
);
$request = array(
'_fields' => array( 'b', 'c', 'e' ),
);
$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame(
array(
'b' => 1,
'c' => 2,
'e' => 4,
),
$response->get_data()
);
}
/**
* Ensure that request['_fields'] allowed list apply to items in response collections.
*/
public function test_rest_filter_response_fields_numeric_array() {
$response = new WP_REST_Response();
$response->set_data(
array(
array(
'a' => 0,
'b' => 1,
'c' => 2,
),
array(
'a' => 3,
'b' => 4,
'c' => 5,
),
array(
'a' => 6,
'b' => 7,
'c' => 8,
),
)
);
$request = array(
'_fields' => 'b,c',
);
$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame(
array(
array(
'b' => 1,
'c' => 2,
),
array(
'b' => 4,
'c' => 5,
),
array(
'b' => 7,
'c' => 8,
),
),
$response->get_data()
);
}
/**
* Ensure that nested fields may be allowed with request['_fields'].
*
* @ticket 42094
*/
public function test_rest_filter_response_fields_nested_field_filter() {
$response = new WP_REST_Response();
$response->set_data(
array(
'a' => 0,
'b' => array(
'1' => 1,
'2' => 2,
),
'c' => 3,
'd' => array(
'4' => 4,
'5' => 5,
),
)
);
$request = array(
'_fields' => 'b.1,c,d.5',
);
$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame(
array(
'b' => array(
'1' => 1,
),
'c' => 3,
'd' => array(
'5' => 5,
),
),
$response->get_data()
);
}
/**
* Ensure inclusion of deeply nested fields may be controlled with request['_fields'].
*
* @ticket 49648
*/
public function test_rest_filter_response_fields_deeply_nested_field_filter() {
$response = new WP_REST_Response();
$response->set_data(
array(
'field' => array(
'a' => array(
'i' => 'value i',
'ii' => 'value ii',
),
'b' => array(
'iii' => 'value iii',
'iv' => 'value iv',
),
),
)
);
$request = array(
'_fields' => 'field.a.i,field.b.iv',
);
$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame(
array(
'field' => array(
'a' => array(
'i' => 'value i',
),
'b' => array(
'iv' => 'value iv',
),
),
),
$response->get_data()
);
}
/**
* Ensure that specifying a single top-level key in _fields includes that field and all children.
*
* @ticket 48266
*/
public function test_rest_filter_response_fields_top_level_key() {
$response = new WP_REST_Response();
$response->set_data(
array(
'meta' => array(
'key1' => 1,
'key2' => 2,
),
)
);
$request = array(
'_fields' => 'meta',
);
$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame(
array(
'meta' => array(
'key1' => 1,
'key2' => 2,
),
),
$response->get_data()
);
}
/**
* Ensure that a top-level key in _fields supersedes any specified children of that field.
*
* @ticket 48266
*/
public function test_rest_filter_response_fields_child_after_parent() {
$response = new WP_REST_Response();
$response->set_data(
array(
'meta' => array(
'key1' => 1,
'key2' => 2,
),
)
);
$request = array(
'_fields' => 'meta,meta.key1',
);
$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame(
array(
'meta' => array(
'key1' => 1,
'key2' => 2,
),
),
$response->get_data()
);
}
/**
* Ensure that specifying two sibling properties in _fields causes both to be included.
*
* @ticket 48266
*/
public function test_rest_filter_response_fields_include_all_specified_siblings() {
$response = new WP_REST_Response();
$response->set_data(
array(
'meta' => array(
'key1' => 1,
'key2' => 2,
),
)
);
$request = array(
'_fields' => 'meta.key1,meta.key2',
);
$response = rest_filter_response_fields( $response, null, $request );
$this->assertSame(
array(
'meta' => array(
'key1' => 1,
'key2' => 2,
),
),
$response->get_data()
);
}
/**
* @ticket 42094
*/
public function test_rest_is_field_included() {
$fields = array(
'id',
'title',
'content.raw',
'custom.property',
);
$this->assertTrue( rest_is_field_included( 'id', $fields ) );
$this->assertTrue( rest_is_field_included( 'title', $fields ) );
$this->assertTrue( rest_is_field_included( 'title.raw', $fields ) );
$this->assertTrue( rest_is_field_included( 'title.rendered', $fields ) );
$this->assertTrue( rest_is_field_included( 'content', $fields ) );
$this->assertTrue( rest_is_field_included( 'content.raw', $fields ) );
$this->assertTrue( rest_is_field_included( 'custom.property', $fields ) );
$this->assertFalse( rest_is_field_included( 'content.rendered', $fields ) );
$this->assertFalse( rest_is_field_included( 'type', $fields ) );
$this->assertFalse( rest_is_field_included( 'meta', $fields ) );
$this->assertFalse( rest_is_field_included( 'meta.value', $fields ) );
}
/**
* The get_rest_url function should return a URL consistently terminated with a "/",
* whether the blog is configured with pretty permalink support or not.
*/
public function test_rest_url_generation() {
// In pretty permalinks case, we expect a path of wp-json/ with no query.
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
$this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/wp-json/', get_rest_url() );
// In index permalinks case, we expect a path of index.php/wp-json/ with no query.
$this->set_permalink_structure( '/index.php/%year%/%monthnum%/%day%/%postname%/' );
$this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/index.php/wp-json/', get_rest_url() );
// In non-pretty case, we get a query string to invoke the rest router.
$this->set_permalink_structure( '' );
$this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/index.php?rest_route=/', get_rest_url() );
}
/**
* @ticket 34299
*/
public function test_rest_url_scheme() {
$_SERVER['SERVER_NAME'] = parse_url( home_url(), PHP_URL_HOST );
$_siteurl = get_option( 'siteurl' );
set_current_screen( 'edit.php' );
$this->assertTrue( is_admin() );
// Test an HTTP URL.
unset( $_SERVER['HTTPS'] );
$url = get_rest_url();
$this->assertSame( 'http', parse_url( $url, PHP_URL_SCHEME ) );
// Test an HTTPS URL.
$_SERVER['HTTPS'] = 'on';
$url = get_rest_url();
$this->assertSame( 'https', parse_url( $url, PHP_URL_SCHEME ) );
// Switch to an admin request on a different domain name.
$_SERVER['SERVER_NAME'] = 'admin.example.org';
update_option( 'siteurl', 'http://admin.example.org' );
$this->assertNotEquals( $_SERVER['SERVER_NAME'], parse_url( home_url(), PHP_URL_HOST ) );
// Test an HTTP URL.
unset( $_SERVER['HTTPS'] );
$url = get_rest_url();
$this->assertSame( 'http', parse_url( $url, PHP_URL_SCHEME ) );
// Test an HTTPS URL.
$_SERVER['HTTPS'] = 'on';
$url = get_rest_url();
$this->assertSame( 'https', parse_url( $url, PHP_URL_SCHEME ) );
// Reset.
update_option( 'siteurl', $_siteurl );
}
/**
* @ticket 42452
*/
public function test_always_prepend_path_with_slash_in_rest_url_filter() {
$filter = new MockAction();
add_filter( 'rest_url', array( $filter, 'filter' ), 10, 2 );
// Passing no path should return a slash.
get_rest_url();
$args = $filter->get_args();
$this->assertSame( '/', $args[0][1] );
$filter->reset();
// Paths without a prepended slash should have one added.
get_rest_url( null, 'wp/media/' );
$args = $filter->get_args();
$this->assertSame( '/wp/media/', $args[0][1] );
$filter->reset();
// Do not modify paths with a prepended slash.
get_rest_url( null, '/wp/media/' );
$args = $filter->get_args();
$this->assertSame( '/wp/media/', $args[0][1] );
unset( $filter );
}
public function jsonp_callback_provider() {
return array(
// Standard names.
array( 'Springfield', true ),
array( 'shelby.ville', true ),
array( 'cypress_creek', true ),
array( 'KampKrusty1', true ),
// Invalid names.
array( 'ogden-ville', false ),
array( 'north haverbrook', false ),
array( "Terror['Lake']", false ),
array( 'Cape[Feare]', false ),
array( '"NewHorrorfield"', false ),
array( 'Scream\\ville', false ),
);
}
/**
* @dataProvider jsonp_callback_provider
*/
public function test_jsonp_callback_check( $callback, $valid ) {
$this->assertSame( $valid, wp_check_jsonp_callback( $callback ) );
}
public function rest_date_provider() {
return array(
// Valid dates with timezones.
array( '2017-01-16T11:30:00-05:00', gmmktime( 11, 30, 0, 1, 16, 2017 ) + 5 * HOUR_IN_SECONDS ),
array( '2017-01-16T11:30:00-05:30', gmmktime( 11, 30, 0, 1, 16, 2017 ) + 5.5 * HOUR_IN_SECONDS ),
array( '2017-01-16T11:30:00-05', gmmktime( 11, 30, 0, 1, 16, 2017 ) + 5 * HOUR_IN_SECONDS ),
array( '2017-01-16T11:30:00+05', gmmktime( 11, 30, 0, 1, 16, 2017 ) - 5 * HOUR_IN_SECONDS ),
array( '2017-01-16T11:30:00-00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
array( '2017-01-16T11:30:00+00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
array( '2017-01-16T11:30:00Z', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
// Valid dates without timezones.
array( '2017-01-16T11:30:00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
// Invalid dates (TODO: support parsing partial dates as ranges, see #38641).
array( '2017-01-16T11:30:00-5', false ),
array( '2017-01-16T11:30', false ),
array( '2017-01-16T11', false ),
array( '2017-01-16T', false ),
array( '2017-01-16', false ),
array( '2017-01', false ),
array( '2017', false ),
);
}
/**
* @dataProvider rest_date_provider
*/
public function test_rest_parse_date( $string, $value ) {
$this->assertEquals( $value, rest_parse_date( $string ) );
}
public function rest_date_force_utc_provider() {
return array(
// Valid dates with timezones.
array( '2017-01-16T11:30:00-05:00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
array( '2017-01-16T11:30:00-05:30', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
array( '2017-01-16T11:30:00-05', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
array( '2017-01-16T11:30:00+05', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
array( '2017-01-16T11:30:00-00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
array( '2017-01-16T11:30:00+00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
array( '2017-01-16T11:30:00Z', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
// Valid dates without timezones.
array( '2017-01-16T11:30:00', gmmktime( 11, 30, 0, 1, 16, 2017 ) ),
// Invalid dates (TODO: support parsing partial dates as ranges, see #38641).
array( '2017-01-16T11:30:00-5', false ),
array( '2017-01-16T11:30', false ),
array( '2017-01-16T11', false ),
array( '2017-01-16T', false ),
array( '2017-01-16', false ),
array( '2017-01', false ),
array( '2017', false ),
);
}
/**
* @dataProvider rest_date_force_utc_provider
*/
public function test_rest_parse_date_force_utc( $string, $value ) {
$this->assertSame( $value, rest_parse_date( $string, true ) );
}
public function filter_wp_rest_server_class( $class_name ) {
return 'Spy_REST_Server';
}
public function test_register_rest_route_without_server() {
$GLOBALS['wp_rest_server'] = null;
add_filter( 'wp_rest_server_class', array( $this, 'filter_wp_rest_server_class' ) );
register_rest_route(
'test-ns',
'/test',
array(
'methods' => array( 'GET' ),
'callback' => '__return_null',
'permission_callback' => '__return_true',
)
);
$routes = $GLOBALS['wp_rest_server']->get_routes();
$this->assertSame( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) );
}
function test_rest_preload_api_request_with_method() {
$rest_server = $GLOBALS['wp_rest_server'];
$GLOBALS['wp_rest_server'] = null;
$preload_paths = array(
'/wp/v2/types',
array( '/wp/v2/media', 'OPTIONS' ),
);
$preload_data = array_reduce(
$preload_paths,
'rest_preload_api_request',
array()
);
$this->assertSame( array_keys( $preload_data ), array( '/wp/v2/types', 'OPTIONS' ) );
$this->assertArrayHasKey( '/wp/v2/media', $preload_data['OPTIONS'] );
$GLOBALS['wp_rest_server'] = $rest_server;
}
/**
* @ticket 40614
*/
function test_rest_ensure_request_accepts_path_string() {
$request = rest_ensure_request( '/wp/v2/posts' );
$this->assertInstanceOf( 'WP_REST_Request', $request );
$this->assertSame( '/wp/v2/posts', $request->get_route() );
$this->assertSame( 'GET', $request->get_method() );
}
/**
* @dataProvider _dp_rest_parse_embed_param
*/
public function test_rest_parse_embed_param( $expected, $embed ) {
$this->assertSame( $expected, rest_parse_embed_param( $embed ) );
}
public function _dp_rest_parse_embed_param() {
return array(
array( true, '' ),
array( true, null ),
array( true, '1' ),
array( true, 'true' ),
array( true, array() ),
array( array( 'author' ), 'author' ),
array( array( 'author', 'replies' ), 'author,replies' ),
array( array( 'author', 'replies' ), 'author,replies ' ),
array( array( 'wp:term' ), 'wp:term' ),
array( array( 'wp:term', 'wp:attachment' ), 'wp:term,wp:attachment' ),
array( array( 'author' ), array( 'author' ) ),
array( array( 'author', 'replies' ), array( 'author', 'replies' ) ),
array( array( 'https://api.w.org/term' ), 'https://api.w.org/term' ),
array( array( 'https://api.w.org/term', 'https://api.w.org/attachment' ), 'https://api.w.org/term,https://api.w.org/attachment' ),
array( array( 'https://api.w.org/term', 'https://api.w.org/attachment' ), array( 'https://api.w.org/term', 'https://api.w.org/attachment' ) ),
);
}
/**
* @ticket 48819
*
* @dataProvider _dp_rest_filter_response_by_context
*/
public function test_rest_filter_response_by_context( $schema, $data, $expected ) {
$this->assertSame( $expected, rest_filter_response_by_context( $data, $schema, 'view' ) );