forked from WordPress/wordpress-develop
/
controller.php
815 lines (673 loc) · 27.2 KB
/
controller.php
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
<?php
/**
* @group oembed
* @group restapi
*/
class Test_oEmbed_Controller extends WP_UnitTestCase {
/**
* @var WP_REST_Server
*/
protected $server;
protected static $editor;
protected static $administrator;
protected static $subscriber;
const YOUTUBE_VIDEO_ID = 'OQSNhk5ICTI';
const INVALID_OEMBED_URL = 'https://www.notreallyanoembedprovider.com/watch?v=awesome-cat-video';
const UNTRUSTED_PROVIDER_URL = 'https://www.untrustedprovider.com';
public static function wpSetUpBeforeClass( $factory ) {
self::$subscriber = $factory->user->create(
array(
'role' => 'subscriber',
)
);
self::$editor = $factory->user->create(
array(
'role' => 'editor',
'user_email' => 'editor@example.com',
)
);
self::$administrator = $factory->user->create(
array(
'role' => 'administrator',
'user_email' => 'administrator@example.com',
)
);
}
public static function wpTearDownAfterClass() {
self::delete_user( self::$subscriber );
self::delete_user( self::$editor );
}
public function setUp(): void {
parent::setUp();
/** @var WP_REST_Server $wp_rest_server */
global $wp_rest_server;
$wp_rest_server = new Spy_REST_Server;
do_action( 'rest_api_init', $wp_rest_server );
add_filter( 'pre_http_request', array( $this, 'mock_embed_request' ), 10, 3 );
add_filter( 'oembed_result', array( $this, 'filter_oembed_result' ), 10, 3 );
$this->request_count = 0;
$this->oembed_result_filter_count = 0;
}
public function tearDown(): void {
parent::tearDown();
/** @var WP_REST_Server $wp_rest_server */
global $wp_rest_server;
$wp_rest_server = null;
remove_filter( 'pre_http_request', array( $this, 'mock_embed_request' ), 10 );
remove_filter( 'oembed_result', array( $this, 'filter_oembed_result' ), 10 );
}
/**
* Count of the number of requests attempted.
*
* @var int
*/
public $request_count = 0;
/**
* Count of the number of times the oembed_result filter was called.
*
* @var int
*/
public $oembed_result_filter_count = 0;
/**
* Intercept oEmbed requests and mock responses.
*
* @param mixed $preempt Whether to preempt an HTTP request's return value. Default false.
* @param mixed $r HTTP request arguments.
* @param string $url The request URL.
* @return array Response data.
*/
public function mock_embed_request( $preempt, $r, $url ) {
unset( $preempt, $r );
$parsed_url = wp_parse_url( $url );
$query = isset( $parsed_url['query'] ) ? $parsed_url['query'] : '';
parse_str( $query, $query_params );
$this->request_count += 1;
// Mock request to YouTube Embed.
if ( ! empty( $query_params['url'] ) && false !== strpos( $query_params['url'], '?v=' . self::YOUTUBE_VIDEO_ID ) ) {
return array(
'response' => array(
'code' => 200,
),
'body' => wp_json_encode(
array(
'version' => '1.0',
'type' => 'video',
'provider_name' => 'YouTube',
'provider_url' => 'https://www.youtube.com',
'thumbnail_width' => $query_params['maxwidth'],
'width' => $query_params['maxwidth'],
'thumbnail_height' => $query_params['maxheight'],
'height' => $query_params['maxheight'],
'html' => '<b>Unfiltered</b><iframe width="' . $query_params['maxwidth'] . '" height="' . $query_params['maxheight'] . '" src="https://www.youtube.com/embed/' . self::YOUTUBE_VIDEO_ID . '?feature=oembed" frameborder="0" allowfullscreen></iframe>',
'author_name' => 'Yosemitebear62',
'thumbnail_url' => 'https://i.ytimg.com/vi/' . self::YOUTUBE_VIDEO_ID . '/hqdefault.jpg',
'title' => 'Yosemitebear Mountain Double Rainbow 1-8-10',
)
),
);
}
if ( self::UNTRUSTED_PROVIDER_URL === $url ) {
return array(
'response' => array(
'code' => 200,
),
'body' => '<html><head><link rel="alternate" type="application/json+oembed" href="' . self::UNTRUSTED_PROVIDER_URL . '" /></head><body></body></html>',
);
}
if ( ! empty( $query_params['url'] ) && false !== strpos( $query_params['url'], self::UNTRUSTED_PROVIDER_URL ) ) {
return array(
'response' => array(
'code' => 200,
),
'body' => wp_json_encode(
array(
'version' => '1.0',
'type' => 'rich',
'provider_name' => 'Untrusted',
'provider_url' => self::UNTRUSTED_PROVIDER_URL,
'html' => '<b>Filtered</b><a href="">Unfiltered</a>',
'author_name' => 'Untrusted Embed Author',
'title' => 'Untrusted Embed',
)
),
);
}
return array(
'response' => array(
'code' => 404,
),
);
}
/**
* Filters 'oembed_result' to ensure correct type.
*
* @param string|false $data The returned oEmbed HTML.
* @param string $url URL of the content to be embedded.
* @param array $args Optional arguments, usually passed from a shortcode.
* @return string
*/
public function filter_oembed_result( $data, $url, $args ) {
if ( ! is_string( $data ) && false !== $data ) {
$this->fail( 'Unexpected type for $data.' );
}
$this->assertIsString( $url );
$this->assertIsArray( $args );
$this->oembed_result_filter_count++;
return $data;
}
function test_wp_oembed_ensure_format() {
$this->assertEquals( 'json', wp_oembed_ensure_format( 'json' ) );
$this->assertEquals( 'xml', wp_oembed_ensure_format( 'xml' ) );
$this->assertEquals( 'json', wp_oembed_ensure_format( 123 ) );
$this->assertEquals( 'json', wp_oembed_ensure_format( 'random' ) );
$this->assertEquals( 'json', wp_oembed_ensure_format( array() ) );
}
function test_oembed_create_xml() {
$actual = _oembed_create_xml(
array(
'foo' => 'bar',
'bar' => 'baz',
'ping' => 'pong',
)
);
$expected = '<oembed><foo>bar</foo><bar>baz</bar><ping>pong</ping></oembed>';
$this->assertStringEndsWith( $expected, trim( $actual ) );
$actual = _oembed_create_xml(
array(
'foo' => array(
'bar' => 'baz',
),
'ping' => 'pong',
)
);
$expected = '<oembed><foo><bar>baz</bar></foo><ping>pong</ping></oembed>';
$this->assertStringEndsWith( $expected, trim( $actual ) );
$actual = _oembed_create_xml(
array(
'foo' => array(
'bar' => array(
'ping' => 'pong',
),
),
'hello' => 'world',
)
);
$expected = '<oembed><foo><bar><ping>pong</ping></bar></foo><hello>world</hello></oembed>';
$this->assertStringEndsWith( $expected, trim( $actual ) );
$actual = _oembed_create_xml(
array(
array(
'foo' => array(
'bar',
),
),
'helloworld',
)
);
$expected = '<oembed><oembed><foo><oembed>bar</oembed></foo></oembed><oembed>helloworld</oembed></oembed>';
$this->assertStringEndsWith( $expected, trim( $actual ) );
}
public function test_route_availability() {
// Check the route was registered correctly.
$filtered_routes = rest_get_server()->get_routes();
$this->assertArrayHasKey( '/oembed/1.0/embed', $filtered_routes );
$route = $filtered_routes['/oembed/1.0/embed'];
$this->assertCount( 1, $route );
$this->assertArrayHasKey( 'callback', $route[0] );
$this->assertArrayHasKey( 'methods', $route[0] );
$this->assertArrayHasKey( 'args', $route[0] );
// Check proxy route registration.
$this->assertArrayHasKey( '/oembed/1.0/proxy', $filtered_routes );
$proxy_route = $filtered_routes['/oembed/1.0/proxy'];
$this->assertCount( 1, $proxy_route );
$this->assertArrayHasKey( 'callback', $proxy_route[0] );
$this->assertArrayHasKey( 'permission_callback', $proxy_route[0] );
$this->assertArrayHasKey( 'methods', $proxy_route[0] );
$this->assertArrayHasKey( 'args', $proxy_route[0] );
}
function test_request_with_wrong_method() {
$request = new WP_REST_Request( 'POST', '/oembed/1.0/embed' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 'rest_no_route', $data['code'] );
}
function test_request_without_url_param() {
$request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 'rest_missing_callback_param', $data['code'] );
$this->assertEquals( 'url', $data['data']['params'][0] );
}
function test_request_with_bad_url() {
$request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' );
$request->set_param( 'url', 'http://google.com/' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 'oembed_invalid_url', $data['code'] );
}
function test_request_invalid_format() {
$post_id = $this->factory()->post->create();
$request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' );
$request->set_param( 'url', get_permalink( $post_id ) );
$request->set_param( 'format', 'random' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertIsArray( $data );
$this->assertNotEmpty( $data );
}
function test_request_json() {
$user = self::factory()->user->create_and_get(
array(
'display_name' => 'John Doe',
)
);
$post = self::factory()->post->create_and_get(
array(
'post_author' => $user->ID,
'post_title' => 'Hello World',
)
);
$request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' );
$request->set_param( 'url', get_permalink( $post->ID ) );
$request->set_param( 'maxwidth', 400 );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertIsArray( $data );
$this->assertNotEmpty( $data );
$this->assertArrayHasKey( 'version', $data );
$this->assertArrayHasKey( 'provider_name', $data );
$this->assertArrayHasKey( 'provider_url', $data );
$this->assertArrayHasKey( 'author_name', $data );
$this->assertArrayHasKey( 'author_url', $data );
$this->assertArrayHasKey( 'title', $data );
$this->assertArrayHasKey( 'type', $data );
$this->assertArrayHasKey( 'width', $data );
$this->assertEquals( '1.0', $data['version'] );
$this->assertEquals( get_bloginfo( 'name' ), $data['provider_name'] );
$this->assertEquals( home_url(), $data['provider_url'] );
$this->assertEquals( $user->display_name, $data['author_name'] );
$this->assertEquals( get_author_posts_url( $user->ID, $user->user_nicename ), $data['author_url'] );
$this->assertEquals( $post->post_title, $data['title'] );
$this->assertEquals( 'rich', $data['type'] );
$this->assertTrue( $data['width'] <= $request['maxwidth'] );
}
/**
* @ticket 34971
*/
function test_request_static_front_page() {
$post = self::factory()->post->create_and_get(
array(
'post_title' => 'Front page',
'post_type' => 'page',
)
);
update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', $post->ID );
$request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' );
$request->set_param( 'url', home_url() );
$request->set_param( 'maxwidth', 400 );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertIsArray( $data );
$this->assertNotEmpty( $data );
$this->assertArrayHasKey( 'version', $data );
$this->assertArrayHasKey( 'provider_name', $data );
$this->assertArrayHasKey( 'provider_url', $data );
$this->assertArrayHasKey( 'author_name', $data );
$this->assertArrayHasKey( 'author_url', $data );
$this->assertArrayHasKey( 'title', $data );
$this->assertArrayHasKey( 'type', $data );
$this->assertArrayHasKey( 'width', $data );
$this->assertEquals( '1.0', $data['version'] );
$this->assertEquals( get_bloginfo( 'name' ), $data['provider_name'] );
$this->assertEquals( home_url(), $data['provider_url'] );
$this->assertEquals( get_bloginfo( 'name' ), $data['author_name'] );
$this->assertEquals( home_url(), $data['author_url'] );
$this->assertEquals( $post->post_title, $data['title'] );
$this->assertEquals( 'rich', $data['type'] );
$this->assertTrue( $data['width'] <= $request['maxwidth'] );
update_option( 'show_on_front', 'posts' );
}
function test_request_xml() {
$user = self::factory()->user->create_and_get(
array(
'display_name' => 'John Doe',
)
);
$post = self::factory()->post->create_and_get(
array(
'post_author' => $user->ID,
'post_title' => 'Hello World',
)
);
$request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' );
$request->set_param( 'url', get_permalink( $post->ID ) );
$request->set_param( 'format', 'xml' );
$request->set_param( 'maxwidth', 400 );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertIsArray( $data );
$this->assertNotEmpty( $data );
$this->assertArrayHasKey( 'version', $data );
$this->assertArrayHasKey( 'provider_name', $data );
$this->assertArrayHasKey( 'provider_url', $data );
$this->assertArrayHasKey( 'author_name', $data );
$this->assertArrayHasKey( 'author_url', $data );
$this->assertArrayHasKey( 'title', $data );
$this->assertArrayHasKey( 'type', $data );
$this->assertArrayHasKey( 'width', $data );
$this->assertEquals( '1.0', $data['version'] );
$this->assertEquals( get_bloginfo( 'name' ), $data['provider_name'] );
$this->assertEquals( home_url(), $data['provider_url'] );
$this->assertEquals( $user->display_name, $data['author_name'] );
$this->assertEquals( get_author_posts_url( $user->ID, $user->user_nicename ), $data['author_url'] );
$this->assertEquals( $post->post_title, $data['title'] );
$this->assertEquals( 'rich', $data['type'] );
$this->assertTrue( $data['width'] <= $request['maxwidth'] );
}
/**
* @group multisite
* @group ms-required
*/
function test_request_ms_child_in_root_blog() {
$child = self::factory()->blog->create();
switch_to_blog( $child );
$post = self::factory()->post->create_and_get(
array(
'post_title' => 'Hello Child Blog',
)
);
$request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' );
$request->set_param( 'url', get_permalink( $post->ID ) );
$request->set_param( 'maxwidth', 400 );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertIsArray( $data );
$this->assertNotEmpty( $data );
restore_current_blog();
}
function test_rest_pre_serve_request() {
$user = $this->factory()->user->create_and_get(
array(
'display_name' => 'John Doe',
)
);
$post = $this->factory()->post->create_and_get(
array(
'post_author' => $user->ID,
'post_title' => 'Hello World',
)
);
$request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' );
$request->set_param( 'url', get_permalink( $post->ID ) );
$request->set_param( 'format', 'xml' );
$response = rest_get_server()->dispatch( $request );
$output = get_echo( '_oembed_rest_pre_serve_request', array( true, $response, $request, rest_get_server() ) );
$xml = simplexml_load_string( $output );
$this->assertInstanceOf( 'SimpleXMLElement', $xml );
}
function test_rest_pre_serve_request_wrong_format() {
$post = $this->factory()->post->create_and_get();
$request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' );
$request->set_param( 'url', get_permalink( $post->ID ) );
$request->set_param( 'format', 'json' );
$response = rest_get_server()->dispatch( $request );
$this->assertTrue( _oembed_rest_pre_serve_request( true, $response, $request, rest_get_server() ) );
}
function test_rest_pre_serve_request_wrong_method() {
$post = $this->factory()->post->create_and_get();
$request = new WP_REST_Request( 'HEAD', '/oembed/1.0/embed' );
$request->set_param( 'url', get_permalink( $post->ID ) );
$request->set_param( 'format', 'xml' );
$response = rest_get_server()->dispatch( $request );
$this->assertTrue( _oembed_rest_pre_serve_request( true, $response, $request, rest_get_server() ) );
}
function test_get_oembed_endpoint_url() {
$this->assertEquals( home_url() . '/index.php?rest_route=/oembed/1.0/embed', get_oembed_endpoint_url() );
$this->assertEquals( home_url() . '/index.php?rest_route=/oembed/1.0/embed', get_oembed_endpoint_url( '', 'json' ) );
$this->assertEquals( home_url() . '/index.php?rest_route=/oembed/1.0/embed', get_oembed_endpoint_url( '', 'xml' ) );
$post_id = $this->factory()->post->create();
$url = get_permalink( $post_id );
$url_encoded = urlencode( $url );
$this->assertEquals( home_url() . '/index.php?rest_route=%2Foembed%2F1.0%2Fembed&url=' . $url_encoded, get_oembed_endpoint_url( $url ) );
$this->assertEquals( home_url() . '/index.php?rest_route=%2Foembed%2F1.0%2Fembed&url=' . $url_encoded . '&format=xml', get_oembed_endpoint_url( $url, 'xml' ) );
}
function test_get_oembed_endpoint_url_pretty_permalinks() {
update_option( 'permalink_structure', '/%postname%' );
$this->assertEquals( home_url() . '/wp-json/oembed/1.0/embed', get_oembed_endpoint_url() );
$this->assertEquals( home_url() . '/wp-json/oembed/1.0/embed', get_oembed_endpoint_url( '', 'xml' ) );
$post_id = $this->factory()->post->create();
$url = get_permalink( $post_id );
$url_encoded = urlencode( $url );
$this->assertEquals( home_url() . '/wp-json/oembed/1.0/embed?url=' . $url_encoded, get_oembed_endpoint_url( $url ) );
$this->assertEquals( home_url() . '/wp-json/oembed/1.0/embed?url=' . $url_encoded . '&format=xml', get_oembed_endpoint_url( $url, 'xml' ) );
update_option( 'permalink_structure', '' );
}
public function test_proxy_without_permission() {
// Test without a login.
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 400, $response->get_status() );
// Test with a user that does not have edit_posts capability.
wp_set_current_user( self::$subscriber );
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$request->set_param( 'url', self::INVALID_OEMBED_URL );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 403, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( $data['code'], 'rest_forbidden' );
}
public function test_proxy_with_invalid_oembed_provider() {
wp_set_current_user( self::$editor );
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$request->set_param( 'url', self::INVALID_OEMBED_URL );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( 'oembed_invalid_url', $data['code'] );
}
public function test_proxy_with_invalid_type() {
wp_set_current_user( self::$editor );
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$request->set_param( 'type', 'xml' );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 400, $response->get_status() );
}
public function test_proxy_with_valid_oembed_provider() {
wp_set_current_user( self::$editor );
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$request->set_param( 'url', 'https://www.youtube.com/watch?v=' . self::YOUTUBE_VIDEO_ID );
$request->set_param( 'maxwidth', 456 );
$request->set_param( 'maxheight', 789 );
$request->set_param( '_wpnonce', wp_create_nonce( 'wp_rest' ) );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 1, $this->request_count );
// Subsequent request is cached and so it should not cause a request.
rest_get_server()->dispatch( $request );
$this->assertEquals( 1, $this->request_count );
// Rest with another user should also be cached.
wp_set_current_user( self::$administrator );
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$request->set_param( 'url', 'https://www.youtube.com/watch?v=' . self::YOUTUBE_VIDEO_ID );
$request->set_param( '_wpnonce', wp_create_nonce( 'wp_rest' ) );
$request->set_param( 'maxwidth', 456 );
$request->set_param( 'maxheight', 789 );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 1, $this->request_count );
// Test data object.
$data = $response->get_data();
$this->assertNotEmpty( $data );
$this->assertIsObject( $data );
$this->assertEquals( 'YouTube', $data->provider_name );
$this->assertEquals( 'https://i.ytimg.com/vi/' . self::YOUTUBE_VIDEO_ID . '/hqdefault.jpg', $data->thumbnail_url );
$this->assertEquals( $data->width, $request['maxwidth'] );
$this->assertEquals( $data->height, $request['maxheight'] );
}
/**
* @ticket 45447
*
* @see wp_maybe_load_embeds()
*/
public function test_proxy_with_classic_embed_provider() {
wp_set_current_user( self::$editor );
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$request->set_param( 'url', 'https://www.youtube.com/embed/' . self::YOUTUBE_VIDEO_ID );
$request->set_param( 'maxwidth', 456 );
$request->set_param( 'maxheight', 789 );
$request->set_param( '_wpnonce', wp_create_nonce( 'wp_rest' ) );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 2, $this->request_count );
// Test data object.
$data = $response->get_data();
$this->assertNotEmpty( $data );
$this->assertIsObject( $data );
$this->assertIsString( $data->html );
$this->assertIsArray( $data->scripts );
}
public function test_proxy_with_invalid_oembed_provider_no_discovery() {
wp_set_current_user( self::$editor );
// If discover is false for an unknown provider, no discovery request should take place.
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$request->set_param( 'url', self::INVALID_OEMBED_URL );
$request->set_param( 'discover', false );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
$this->assertEquals( 0, $this->request_count );
}
public function test_proxy_with_invalid_oembed_provider_with_default_discover_param() {
wp_set_current_user( self::$editor );
// For an unknown provider, a discovery request should happen.
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$request->set_param( 'url', self::INVALID_OEMBED_URL );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
$this->assertEquals( 1, $this->request_count );
}
public function test_proxy_with_invalid_discover_param() {
wp_set_current_user( self::$editor );
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$request->set_param( 'url', self::INVALID_OEMBED_URL );
$request->set_param( 'discover', 'notaboolean' );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 400, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( $data['code'], 'rest_invalid_param' );
}
/**
* @ticket 45142
*/
function test_proxy_with_internal_url() {
wp_set_current_user( self::$editor );
$user = self::factory()->user->create_and_get(
array(
'display_name' => 'John Doe',
)
);
$post = self::factory()->post->create_and_get(
array(
'post_author' => $user->ID,
'post_title' => 'Hello World',
)
);
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$request->set_param( 'url', get_permalink( $post->ID ) );
$request->set_param( 'maxwidth', 400 );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$data = (array) $data;
$this->assertNotEmpty( $data );
$this->assertArrayHasKey( 'version', $data );
$this->assertArrayHasKey( 'provider_name', $data );
$this->assertArrayHasKey( 'provider_url', $data );
$this->assertArrayHasKey( 'author_name', $data );
$this->assertArrayHasKey( 'author_url', $data );
$this->assertArrayHasKey( 'title', $data );
$this->assertArrayHasKey( 'type', $data );
$this->assertArrayHasKey( 'width', $data );
$this->assertEquals( '1.0', $data['version'] );
$this->assertEquals( get_bloginfo( 'name' ), $data['provider_name'] );
$this->assertEquals( home_url(), $data['provider_url'] );
$this->assertEquals( $user->display_name, $data['author_name'] );
$this->assertEquals( get_author_posts_url( $user->ID, $user->user_nicename ), $data['author_url'] );
$this->assertEquals( $post->post_title, $data['title'] );
$this->assertEquals( 'rich', $data['type'] );
$this->assertTrue( $data['width'] <= $request['maxwidth'] );
}
/**
* @ticket 45142
*/
function test_proxy_with_static_front_page_url() {
wp_set_current_user( self::$editor );
$post = self::factory()->post->create_and_get(
array(
'post_title' => 'Front page',
'post_type' => 'page',
'post_author' => 0,
)
);
update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', $post->ID );
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$request->set_param( 'url', home_url() );
$request->set_param( 'maxwidth', 400 );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertIsObject( $data );
$data = (array) $data;
$this->assertNotEmpty( $data );
$this->assertArrayHasKey( 'version', $data );
$this->assertArrayHasKey( 'provider_name', $data );
$this->assertArrayHasKey( 'provider_url', $data );
$this->assertArrayHasKey( 'author_name', $data );
$this->assertArrayHasKey( 'author_url', $data );
$this->assertArrayHasKey( 'title', $data );
$this->assertArrayHasKey( 'type', $data );
$this->assertArrayHasKey( 'width', $data );
$this->assertEquals( '1.0', $data['version'] );
$this->assertEquals( get_bloginfo( 'name' ), $data['provider_name'] );
$this->assertEquals( home_url(), $data['provider_url'] );
$this->assertEquals( get_bloginfo( 'name' ), $data['author_name'] );
$this->assertEquals( home_url(), $data['author_url'] );
$this->assertEquals( $post->post_title, $data['title'] );
$this->assertEquals( 'rich', $data['type'] );
$this->assertTrue( $data['width'] <= $request['maxwidth'] );
update_option( 'show_on_front', 'posts' );
}
/**
* @ticket 45142
*/
public function test_proxy_filters_result_of_untrusted_oembed_provider() {
wp_set_current_user( self::$editor );
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$request->set_param( 'url', self::UNTRUSTED_PROVIDER_URL );
$request->set_param( 'maxwidth', 456 );
$request->set_param( 'maxheight', 789 );
$request->set_param( '_wpnonce', wp_create_nonce( 'wp_rest' ) );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 1, $this->oembed_result_filter_count );
$this->assertIsObject( $data );
$this->assertEquals( 'Untrusted', $data->provider_name );
$this->assertEquals( self::UNTRUSTED_PROVIDER_URL, $data->provider_url );
$this->assertEquals( 'rich', $data->type );
$this->assertFalse( $data->html );
}
/**
* @ticket 45142
*/
public function test_proxy_does_not_filter_result_of_trusted_oembed_provider() {
wp_set_current_user( self::$editor );
$request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$request->set_param( 'url', 'https://www.youtube.com/watch?v=' . self::YOUTUBE_VIDEO_ID );
$request->set_param( 'maxwidth', 456 );
$request->set_param( 'maxheight', 789 );
$request->set_param( '_wpnonce', wp_create_nonce( 'wp_rest' ) );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 1, $this->oembed_result_filter_count );
$this->assertIsObject( $data );
$this->assertStringStartsWith( '<b>Unfiltered</b>', $data->html );
}
}