mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-01 07:40:07 +00:00
Embeds: Who put this REST API infrastructure in my WordPress?
Well, while it's here, we probably should make use of it. The oEmbed endpoint now uses the REST API infrastructure, instead of providing its own. Props swissspidy. Fixes #34207. git-svn-id: https://develop.svn.wordpress.org/trunk@35436 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -2,217 +2,22 @@
|
||||
|
||||
/**
|
||||
* @group oembed
|
||||
* @group restapi
|
||||
*/
|
||||
class Test_oEmbed_Controller extends WP_UnitTestCase {
|
||||
function test_request_with_bad_url() {
|
||||
$request = array(
|
||||
'url' => '',
|
||||
'format' => 'json',
|
||||
'maxwidth' => 600,
|
||||
);
|
||||
|
||||
$legacy_controller = new WP_oEmbed_Controller();
|
||||
|
||||
$this->assertEquals( get_status_header_desc( 404 ), $legacy_controller->dispatch( $request ) );
|
||||
}
|
||||
|
||||
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',
|
||||
) );
|
||||
|
||||
// WP_Query arguments.
|
||||
$request = array(
|
||||
'url' => get_permalink( $post->ID ),
|
||||
'format' => 'json',
|
||||
'maxwidth' => 400,
|
||||
'callback' => '',
|
||||
'oembed' => true,
|
||||
);
|
||||
|
||||
$legacy_controller = new WP_oEmbed_Controller();
|
||||
|
||||
$data = json_decode( $legacy_controller->dispatch( $request ), true );
|
||||
|
||||
$this->assertTrue( is_array( $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( get_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'] );
|
||||
}
|
||||
|
||||
function test_request_jsonp() {
|
||||
$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 = array(
|
||||
'url' => get_permalink( $post->ID ),
|
||||
'format' => 'json',
|
||||
'maxwidth' => 600,
|
||||
'callback' => 'mycallback',
|
||||
);
|
||||
|
||||
$legacy_controller = new WP_oEmbed_Controller();
|
||||
|
||||
$data = $legacy_controller->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 0, strpos( $data, '/**/mycallback(' ) );
|
||||
}
|
||||
|
||||
function test_request_jsonp_invalid_callback() {
|
||||
$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 = array(
|
||||
'url' => get_permalink( $post->ID ),
|
||||
'format' => 'json',
|
||||
'maxwidth' => 600,
|
||||
'callback' => array( 'foo', 'bar' ),
|
||||
);
|
||||
|
||||
$legacy_controller = new WP_oEmbed_Controller();
|
||||
|
||||
$data = $legacy_controller->dispatch( $request );
|
||||
|
||||
$this->assertFalse( strpos( $data, '/**/' ) );
|
||||
}
|
||||
|
||||
function test_request_json_invalid_data() {
|
||||
$request = array(
|
||||
'callback' => '',
|
||||
);
|
||||
|
||||
$legacy_controller = new WP_oEmbed_Controller();
|
||||
|
||||
$this->assertEquals( get_status_header_desc( 501 ), $legacy_controller->json_response( null, $request ) );
|
||||
$this->assertEquals( get_status_header_desc( 501 ), $legacy_controller->json_response( 123, $request ) );
|
||||
$this->assertEquals( get_status_header_desc( 501 ), $legacy_controller->json_response( array(), $request ) );
|
||||
}
|
||||
|
||||
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 = array(
|
||||
'url' => get_permalink( $post->ID ),
|
||||
'format' => 'xml',
|
||||
'maxwidth' => 400,
|
||||
'callback' => '',
|
||||
);
|
||||
|
||||
$legacy_controller = new WP_oEmbed_Controller();
|
||||
|
||||
$data = $legacy_controller->dispatch( $request );
|
||||
|
||||
$data = simplexml_load_string( $data );
|
||||
$this->assertInstanceOf( 'SimpleXMLElement', $data );
|
||||
|
||||
$data = (array) $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( get_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'] );
|
||||
}
|
||||
|
||||
function test_request_xml_invalid_data() {
|
||||
$legacy_controller = new WP_oEmbed_Controller();
|
||||
|
||||
$this->assertEquals( get_status_header_desc( 501 ), $legacy_controller->xml_response( null ) );
|
||||
$this->assertEquals( get_status_header_desc( 501 ), $legacy_controller->xml_response( 123 ) );
|
||||
$this->assertEquals( get_status_header_desc( 501 ), $legacy_controller->xml_response( array() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @group multisite
|
||||
* @var WP_REST_Server
|
||||
*/
|
||||
function test_request_ms_child_in_root_blog() {
|
||||
if ( ! is_multisite() ) {
|
||||
$this->markTestSkipped( __METHOD__ . ' is a multisite-only test.' );
|
||||
}
|
||||
protected $server;
|
||||
|
||||
$child = self::factory()->blog->create();
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
switch_to_blog( $child );
|
||||
/** @var WP_REST_Server $wp_rest_server */
|
||||
global $wp_rest_server;
|
||||
$this->server = $wp_rest_server = new Spy_REST_Server();
|
||||
|
||||
$post = self::factory()->post->create_and_get( array(
|
||||
'post_title' => 'Hello Child Blog',
|
||||
) );
|
||||
|
||||
$request = array(
|
||||
'url' => get_permalink( $post->ID ),
|
||||
'format' => 'json',
|
||||
'maxwidth' => 600,
|
||||
'callback' => '',
|
||||
);
|
||||
|
||||
$legacy_controller = new WP_oEmbed_Controller();
|
||||
|
||||
$data = json_decode( $legacy_controller->dispatch( $request ), true );
|
||||
|
||||
$this->assertInternalType( 'array', $data );
|
||||
$this->assertNotEmpty( $data );
|
||||
|
||||
restore_current_blog();
|
||||
}
|
||||
|
||||
function test_get_oembed_endpoint_url() {
|
||||
$this->assertEquals( home_url() . '/?oembed=true', get_oembed_endpoint_url() );
|
||||
$this->assertEquals( home_url() . '/?oembed=true', get_oembed_endpoint_url( '', 'json' ) );
|
||||
$this->assertEquals( home_url() . '/?oembed=true', get_oembed_endpoint_url( '', 'xml' ) );
|
||||
|
||||
$post_id = self::factory()->post->create();
|
||||
$url = get_permalink( $post_id );
|
||||
$url_encoded = urlencode( $url );
|
||||
|
||||
$this->assertEquals( home_url() . '/?oembed=true&url=' . $url_encoded, get_oembed_endpoint_url( $url ) );
|
||||
$this->assertEquals( home_url() . '/?oembed=true&url=' . $url_encoded . '&format=xml', get_oembed_endpoint_url( $url, 'xml' ) );
|
||||
do_action( 'rest_api_init', $this->server );
|
||||
}
|
||||
|
||||
function test_wp_oembed_ensure_format() {
|
||||
@@ -271,4 +76,236 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
|
||||
|
||||
$this->assertStringEndsWith( $expected, trim( $actual ) );
|
||||
}
|
||||
|
||||
public function test_route_availability() {
|
||||
// Check the route was registered correctly.
|
||||
$filtered_routes = $this->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] );
|
||||
}
|
||||
|
||||
function test_request_with_wrong_method() {
|
||||
$request = new WP_REST_Request( 'POST', '/oembed/1.0/embed' );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'rest_no_route', $data[0]['code'] );
|
||||
}
|
||||
|
||||
function test_request_without_url_param() {
|
||||
$request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'rest_missing_callback_param', $data[0]['code'] );
|
||||
$this->assertEquals( 'url', $data[0]['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 = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'oembed_invalid_url', $data[0]['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 = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertInternalType( 'array', $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 = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertInternalType( '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( get_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'] );
|
||||
}
|
||||
|
||||
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 = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertInternalType( '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( get_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
|
||||
*/
|
||||
function test_request_ms_child_in_root_blog() {
|
||||
if ( ! is_multisite() ) {
|
||||
$this->markTestSkipped( __METHOD__ . ' is a multisite-only test.' );
|
||||
}
|
||||
|
||||
$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 = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertInternalType( 'array', $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 = $this->server->dispatch( $request );
|
||||
$output = get_echo( '_oembed_rest_pre_serve_request', array( true, $response, $request, $this->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 = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertTrue( _oembed_rest_pre_serve_request( true, $response, $request, $this->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 = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertTrue( _oembed_rest_pre_serve_request( true, $response, $request, $this->server ) );
|
||||
}
|
||||
|
||||
function test_get_oembed_endpoint_url() {
|
||||
$this->assertEquals( home_url() . '/?rest_route=/oembed/1.0/embed', get_oembed_endpoint_url() );
|
||||
$this->assertEquals( home_url() . '/?rest_route=/oembed/1.0/embed', get_oembed_endpoint_url( '', 'json' ) );
|
||||
$this->assertEquals( home_url() . '/?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() . '/?rest_route=%2Foembed%2F1.0%2Fembed&url=' . $url_encoded, get_oembed_endpoint_url( $url ) );
|
||||
$this->assertEquals( home_url() . '/?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', '' );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,26 +5,18 @@
|
||||
*/
|
||||
class Tests_oEmbed_Discovery extends WP_UnitTestCase {
|
||||
function test_add_oembed_discovery_links_non_singular() {
|
||||
ob_start();
|
||||
wp_oembed_add_discovery_links();
|
||||
$actual = ob_get_clean();
|
||||
$this->assertSame( '', $actual );
|
||||
$this->assertSame( '', get_echo( 'wp_oembed_add_discovery_links' ) );
|
||||
}
|
||||
|
||||
function test_add_oembed_discovery_links_to_post() {
|
||||
$post_id = self::factory()->post->create();
|
||||
$this->go_to( get_permalink( $post_id ) );
|
||||
|
||||
$this->assertQueryTrue( 'is_single', 'is_singular' );
|
||||
|
||||
ob_start();
|
||||
wp_oembed_add_discovery_links();
|
||||
$actual = ob_get_clean();
|
||||
|
||||
$expected = '<link rel="alternate" type="application/json+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink() ) ) . '" />' . "\n";
|
||||
$expected .= '<link rel="alternate" type="text/xml+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink(), 'xml' ) ) . '" />' . "\n";
|
||||
|
||||
$this->assertEquals( $expected, $actual );
|
||||
$this->assertEquals( $expected, get_echo( 'wp_oembed_add_discovery_links' ) );
|
||||
}
|
||||
|
||||
function test_add_oembed_discovery_links_to_page() {
|
||||
@@ -32,17 +24,12 @@ class Tests_oEmbed_Discovery extends WP_UnitTestCase {
|
||||
'post_type' => 'page'
|
||||
));
|
||||
$this->go_to( get_permalink( $post_id ) );
|
||||
|
||||
$this->assertQueryTrue( 'is_page', 'is_singular' );
|
||||
|
||||
ob_start();
|
||||
wp_oembed_add_discovery_links();
|
||||
$actual = ob_get_clean();
|
||||
|
||||
$expected = '<link rel="alternate" type="application/json+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink() ) ) . '" />' . "\n";
|
||||
$expected .= '<link rel="alternate" type="text/xml+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink(), 'xml' ) ) . '" />' . "\n";
|
||||
|
||||
$this->assertEquals( $expected, $actual );
|
||||
$this->assertEquals( $expected, get_echo( 'wp_oembed_add_discovery_links' ) );
|
||||
}
|
||||
|
||||
function test_add_oembed_discovery_links_to_attachment() {
|
||||
@@ -53,16 +40,11 @@ class Tests_oEmbed_Discovery extends WP_UnitTestCase {
|
||||
) );
|
||||
|
||||
$this->go_to( get_permalink( $attachment_id ) );
|
||||
|
||||
$this->assertQueryTrue( 'is_attachment', 'is_singular', 'is_single' );
|
||||
|
||||
ob_start();
|
||||
wp_oembed_add_discovery_links();
|
||||
$actual = ob_get_clean();
|
||||
|
||||
$expected = '<link rel="alternate" type="application/json+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink() ) ) . '" />' . "\n";
|
||||
$expected .= '<link rel="alternate" type="text/xml+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink(), 'xml' ) ) . '" />' . "\n";
|
||||
|
||||
$this->assertEquals( $expected, $actual );
|
||||
$this->assertEquals( $expected, get_echo( 'wp_oembed_add_discovery_links' ) );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,57 +7,24 @@
|
||||
* @group oembed-headers
|
||||
*/
|
||||
class Tests_oEmbed_HTTP_Headers extends WP_UnitTestCase {
|
||||
function test_request_json_response_headers() {
|
||||
function test_rest_pre_serve_request_headers() {
|
||||
if ( ! function_exists( 'xdebug_get_headers' ) ) {
|
||||
$this->markTestSkipped( 'xdebug is required for this test' );
|
||||
}
|
||||
|
||||
$post = self::factory()->post->create_and_get( array(
|
||||
$post = $this->factory()->post->create_and_get( array(
|
||||
'post_title' => 'Hello World',
|
||||
) );
|
||||
|
||||
$request = array(
|
||||
'url' => get_permalink( $post->ID ),
|
||||
'format' => 'json',
|
||||
'maxwidth' => 600,
|
||||
'callback' => '',
|
||||
);
|
||||
$request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' );
|
||||
$request->set_param( 'url', get_permalink( $post->ID ) );
|
||||
$request->set_param( 'format', 'xml' );
|
||||
|
||||
$legacy_controller = new WP_oEmbed_Controller();
|
||||
$legacy_controller->dispatch( $request );
|
||||
$server = new WP_REST_Server();
|
||||
$response = $server->dispatch( $request );
|
||||
$output = get_echo( '_oembed_rest_pre_serve_request', array( true, $response, $request, $server ) );
|
||||
|
||||
$headers = xdebug_get_headers();
|
||||
|
||||
$this->assertTrue( in_array( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ), $headers ) );
|
||||
$this->assertTrue( in_array( 'X-Content-Type-Options: nosniff', $headers ) );
|
||||
|
||||
$request['callback'] = 'foobar';
|
||||
|
||||
$legacy_controller->dispatch( $request );
|
||||
|
||||
$headers = xdebug_get_headers();
|
||||
|
||||
$this->assertTrue( in_array( 'Content-Type: application/javascript; charset=' . get_option( 'blog_charset' ), $headers ) );
|
||||
$this->assertTrue( in_array( 'X-Content-Type-Options: nosniff', $headers ) );
|
||||
}
|
||||
|
||||
function test_request_xml_response_headers() {
|
||||
if ( ! function_exists( 'xdebug_get_headers' ) ) {
|
||||
$this->markTestSkipped( 'xdebug is required for this test' );
|
||||
}
|
||||
|
||||
$post = self::factory()->post->create_and_get( array(
|
||||
'post_title' => 'Hello World',
|
||||
) );
|
||||
|
||||
$request = array(
|
||||
'url' => get_permalink( $post->ID ),
|
||||
'format' => 'xml',
|
||||
'maxwidth' => 600,
|
||||
);
|
||||
|
||||
$legacy_controller = new WP_oEmbed_Controller();
|
||||
$legacy_controller->dispatch( $request );
|
||||
$this->assertNotEmpty( $output );
|
||||
|
||||
$headers = xdebug_get_headers();
|
||||
|
||||
|
||||
@@ -249,9 +249,7 @@ class Tests_Embed_Template extends WP_UnitTestCase {
|
||||
}
|
||||
|
||||
function test_add_host_js() {
|
||||
ob_start();
|
||||
wp_oembed_add_host_js();
|
||||
ob_end_clean();
|
||||
|
||||
$this->assertTrue( wp_script_is( 'wp-embed' ) );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user