mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 12:20:22 +00:00
Embeds: Add oEmbed provider support.
For the past 6 years, WordPress has operated as an oEmbed consumer, allowing users to easily embed content from other sites. By adding oEmbed provider support, this allows any oEmbed consumer to embed posts from WordPress sites. In addition to creating an oEmbed provider, WordPress' oEmbed consumer code has been enhanced to work with any site that provides oEmbed data (as long as it matches some strict security rules), and provides a preview from within the post editor. For security, embeds appear within a sandboxed iframe - the iframe content is a template that can be styled or replaced entirely by the theme on the provider site. Props swissspidy, pento, melchoyce, netweb, pfefferle, johnbillion, extendwings, davidbinda, danielbachhuber, SergeyBiryukov, afercia Fixes #32522. git-svn-id: https://develop.svn.wordpress.org/trunk@34903 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
<group>ajax</group>
|
||||
<group>ms-files</group>
|
||||
<group>external-http</group>
|
||||
<group>oembed-headers</group>
|
||||
</exclude>
|
||||
</groups>
|
||||
<php>
|
||||
|
||||
@@ -0,0 +1,273 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group oembed
|
||||
*/
|
||||
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( 'Invalid URL.', $legacy_controller->dispatch( $request ) );
|
||||
}
|
||||
|
||||
function test_request_json() {
|
||||
$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',
|
||||
) );
|
||||
|
||||
// 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 = $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 = 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 = $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 = 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( 'Not implemented', $legacy_controller->json_response( null, $request ) );
|
||||
$this->assertEquals( 'Not implemented', $legacy_controller->json_response( 123, $request ) );
|
||||
$this->assertEquals( 'Not implemented', $legacy_controller->json_response( array(), $request ) );
|
||||
}
|
||||
|
||||
function test_request_xml() {
|
||||
$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 = 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( 'Not implemented', $legacy_controller->xml_response( null ) );
|
||||
$this->assertEquals( 'Not implemented', $legacy_controller->xml_response( 123 ) );
|
||||
$this->assertEquals( 'Not implemented', $legacy_controller->xml_response( array() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @group multisite
|
||||
*/
|
||||
function test_request_ms_child_in_root_blog() {
|
||||
if ( ! is_multisite() ) {
|
||||
$this->markTestSkipped( __METHOD__ . ' is a multisite-only test.' );
|
||||
}
|
||||
|
||||
$child = $this->factory->blog->create();
|
||||
|
||||
switch_to_blog( $child );
|
||||
|
||||
$post = $this->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 = $this->factory->post->create();
|
||||
$url = get_permalink( $post_id );
|
||||
|
||||
$this->assertEquals( home_url() . '/?oembed=true&url=' . $url, get_oembed_endpoint_url( $url ) );
|
||||
$this->assertEquals( home_url() . '/?oembed=true&url=' . $url . '&format=xml', get_oembed_endpoint_url( $url, 'xml' ) );
|
||||
}
|
||||
|
||||
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 ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group oembed
|
||||
*/
|
||||
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 );
|
||||
}
|
||||
|
||||
function test_add_oembed_discovery_links_to_post() {
|
||||
$post_id = $this->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 );
|
||||
}
|
||||
|
||||
function test_add_oembed_discovery_links_to_page() {
|
||||
$post_id = $this->factory->post->create( array(
|
||||
'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 );
|
||||
}
|
||||
|
||||
function test_add_oembed_discovery_links_to_attachment() {
|
||||
$post_id = $this->factory->post->create();
|
||||
$file = DIR_TESTDATA . '/images/canola.jpg';
|
||||
$attachment_id = $this->factory->attachment->create_object( $file, $post_id, array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
) );
|
||||
|
||||
$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 );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group oembed
|
||||
*/
|
||||
class Tests_Filter_oEmbed_Result extends WP_UnitTestCase {
|
||||
function test_filter_oembed_result_trusted_malicious_iframe() {
|
||||
$html = '<p></p><iframe onload="alert(1)"></iframe>';
|
||||
|
||||
$actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), 'https://www.youtube.com/watch?v=dQw4w9WgXcQ' );
|
||||
|
||||
$this->assertEquals( $html, $actual );
|
||||
}
|
||||
|
||||
function test_filter_oembed_result_with_untrusted_provider() {
|
||||
$html = '<p></p><iframe onload="alert(1)" src="http://example.com/sample-page/"></iframe>';
|
||||
$actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), 'http://example.com/sample-page/' );
|
||||
|
||||
$matches = array();
|
||||
preg_match( '|src=".*#\?secret=([\w\d]+)" data-secret="([\w\d]+)"|', $actual, $matches );
|
||||
|
||||
$this->assertTrue( isset( $matches[1] ) );
|
||||
$this->assertTrue( isset( $matches[2] ) );
|
||||
$this->assertEquals( $matches[1], $matches[2] );
|
||||
}
|
||||
|
||||
function test_filter_oembed_result_only_one_iframe_is_allowed() {
|
||||
$html = '<div><iframe></iframe><iframe></iframe><p></p></div>';
|
||||
$actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );
|
||||
|
||||
$this->assertEquals( '<iframe sandbox="allow-scripts" security="restricted"></iframe>', $actual );
|
||||
}
|
||||
|
||||
function test_filter_oembed_result_with_newlines() {
|
||||
$html = <<<EOD
|
||||
<script>var = 1;</script>
|
||||
<iframe></iframe>
|
||||
<iframe></iframe>
|
||||
<p></p>
|
||||
EOD;
|
||||
|
||||
$actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );
|
||||
|
||||
$this->assertEquals( '<iframe sandbox="allow-scripts" security="restricted"></iframe>', $actual );
|
||||
}
|
||||
|
||||
function test_filter_oembed_result_without_iframe() {
|
||||
$html = '<span>Hello</span><p>World</p>';
|
||||
$actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );
|
||||
|
||||
$this->assertFalse( $actual );
|
||||
|
||||
$html = '<div><p></p></div><script></script>';
|
||||
$actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );
|
||||
|
||||
$this->assertFalse( $actual );
|
||||
}
|
||||
|
||||
function test_filter_oembed_result_secret_param_available() {
|
||||
$html = '<iframe src="https://wordpress.org"></iframe>';
|
||||
$actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );
|
||||
|
||||
$matches = array();
|
||||
preg_match( '|src="https://wordpress.org#\?secret=([\w\d]+)" data-secret="([\w\d]+)"|', $actual, $matches );
|
||||
|
||||
$this->assertTrue( isset( $matches[1] ) );
|
||||
$this->assertTrue( isset( $matches[2] ) );
|
||||
$this->assertEquals( $matches[1], $matches[2] );
|
||||
}
|
||||
|
||||
function test_filter_oembed_result_wrong_type_provided() {
|
||||
$actual = wp_filter_oembed_result( 'some string', (object) array( 'type' => 'link' ), '' );
|
||||
|
||||
$this->assertEquals( 'some string', $actual );
|
||||
}
|
||||
|
||||
function test_filter_oembed_result_invalid_result() {
|
||||
$this->assertFalse( wp_filter_oembed_result( false, (object) array( 'type' => 'rich' ), '' ) );
|
||||
$this->assertFalse( wp_filter_oembed_result( '', (object) array( 'type' => 'rich' ), '' ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group oembed
|
||||
*/
|
||||
class Tests_oEmbed_Response_Data extends WP_UnitTestCase {
|
||||
function test_get_oembed_response_data_non_existent_post() {
|
||||
$this->assertFalse( get_oembed_response_data( 0, 100 ) );
|
||||
}
|
||||
|
||||
function test_get_oembed_response_data() {
|
||||
$post = $this->factory->post->create_and_get( array(
|
||||
'post_title' => 'Some Post',
|
||||
) );
|
||||
|
||||
$data = get_oembed_response_data( $post, 400 );
|
||||
|
||||
$this->assertEqualSets( array(
|
||||
'version' => '1.0',
|
||||
'provider_name' => get_bloginfo( 'name' ),
|
||||
'provider_url' => get_home_url( '/' ),
|
||||
'author_name' => get_bloginfo( 'name' ),
|
||||
'author_url' => get_home_url( '/' ),
|
||||
'title' => 'Some Post',
|
||||
'type' => 'rich',
|
||||
'width' => 400,
|
||||
'height' => 225,
|
||||
'html' => get_post_embed_html( $post, 400, 225 ),
|
||||
), $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_oembed_response_data with an author.
|
||||
*/
|
||||
function test_get_oembed_response_data_author() {
|
||||
$user_id = $this->factory->user->create( array(
|
||||
'display_name' => 'John Doe',
|
||||
) );
|
||||
|
||||
$post = $this->factory->post->create_and_get( array(
|
||||
'post_title' => 'Some Post',
|
||||
'post_author' => $user_id,
|
||||
) );
|
||||
|
||||
$data = get_oembed_response_data( $post, 400 );
|
||||
|
||||
$this->assertEqualSets( array(
|
||||
'version' => '1.0',
|
||||
'provider_name' => get_bloginfo( 'name' ),
|
||||
'provider_url' => get_home_url( '/' ),
|
||||
'author_name' => 'John Doe',
|
||||
'author_url' => get_author_posts_url( $user_id ),
|
||||
'title' => 'Some Post',
|
||||
'type' => 'rich',
|
||||
'width' => 400,
|
||||
'height' => 225,
|
||||
'html' => get_post_embed_html( $post, 400, 225 ),
|
||||
), $data );
|
||||
}
|
||||
|
||||
function test_get_oembed_response_link() {
|
||||
remove_filter( 'oembed_response_data', 'get_oembed_response_data_rich' );
|
||||
|
||||
$post = $this->factory->post->create_and_get( array(
|
||||
'post_title' => 'Some Post',
|
||||
) );
|
||||
|
||||
$data = get_oembed_response_data( $post, 600 );
|
||||
|
||||
$this->assertEqualSets( array(
|
||||
'version' => '1.0',
|
||||
'provider_name' => get_bloginfo( 'name' ),
|
||||
'provider_url' => get_home_url( '/' ),
|
||||
'author_name' => get_bloginfo( 'name' ),
|
||||
'author_url' => get_home_url( '/' ),
|
||||
'title' => 'Some Post',
|
||||
'type' => 'link',
|
||||
), $data );
|
||||
|
||||
add_filter( 'oembed_response_data', 'get_oembed_response_data_rich', 10, 4 );
|
||||
}
|
||||
|
||||
function test_get_oembed_response_data_with_draft_post() {
|
||||
$post = $this->factory->post->create_and_get( array(
|
||||
'post_status' => 'draft',
|
||||
) );
|
||||
|
||||
$this->assertFalse( get_oembed_response_data( $post, 100 ) );
|
||||
}
|
||||
|
||||
function test_get_oembed_response_data_with_scheduled_post() {
|
||||
$post = $this->factory->post->create_and_get( array(
|
||||
'post_status' => 'future',
|
||||
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 day' ) ),
|
||||
) );
|
||||
|
||||
$this->assertFalse( get_oembed_response_data( $post, 100 ) );
|
||||
}
|
||||
|
||||
function test_get_oembed_response_data_with_private_post() {
|
||||
$post = $this->factory->post->create_and_get( array(
|
||||
'post_status' => 'private',
|
||||
) );
|
||||
|
||||
$this->assertFalse( get_oembed_response_data( $post, 100 ) );
|
||||
}
|
||||
|
||||
function test_get_oembed_response_data_maxwidth_too_high() {
|
||||
$post = $this->factory->post->create_and_get();
|
||||
|
||||
$data = get_oembed_response_data( $post, 1000 );
|
||||
|
||||
$this->assertEquals( 600, $data['width'] );
|
||||
$this->assertEquals( 338, $data['height'] );
|
||||
}
|
||||
|
||||
function test_get_oembed_response_data_maxwidth_too_low() {
|
||||
$post = $this->factory->post->create_and_get();
|
||||
|
||||
$data = get_oembed_response_data( $post, 100 );
|
||||
|
||||
$this->assertEquals( 200, $data['width'] );
|
||||
$this->assertEquals( 200, $data['height'] );
|
||||
}
|
||||
|
||||
function test_get_oembed_response_data_maxwidth_invalid() {
|
||||
$post = $this->factory->post->create_and_get();
|
||||
|
||||
$data = get_oembed_response_data( $post, '400;" DROP TABLES' );
|
||||
|
||||
$this->assertEquals( 400, $data['width'] );
|
||||
$this->assertEquals( 225, $data['height'] );
|
||||
|
||||
$data = get_oembed_response_data( $post, "lol this isn't even a number?!?!?" );
|
||||
|
||||
$this->assertEquals( 200, $data['width'] );
|
||||
$this->assertEquals( 200, $data['height'] );
|
||||
}
|
||||
|
||||
function test_get_oembed_response_data_with_thumbnail() {
|
||||
$post = $this->factory->post->create_and_get();
|
||||
$file = DIR_TESTDATA . '/images/canola.jpg';
|
||||
$attachment_id = $this->factory->attachment->create_object( $file, $post->ID, array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
) );
|
||||
set_post_thumbnail( $post, $attachment_id );
|
||||
|
||||
$data = get_oembed_response_data( $post, 400 );
|
||||
|
||||
$this->assertArrayHasKey( 'thumbnail_url', $data );
|
||||
$this->assertArrayHasKey( 'thumbnail_width', $data );
|
||||
$this->assertArrayHasKey( 'thumbnail_height', $data );
|
||||
$this->assertTrue( 400 >= $data['thumbnail_width'] );
|
||||
}
|
||||
|
||||
function test_get_oembed_response_data_for_attachment() {
|
||||
$parent = $this->factory->post->create();
|
||||
$file = DIR_TESTDATA . '/images/canola.jpg';
|
||||
$post = $this->factory->attachment->create_object( $file, $parent, array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
) );
|
||||
|
||||
$data = get_oembed_response_data( $post, 400 );
|
||||
|
||||
$this->assertArrayHasKey( 'thumbnail_url', $data );
|
||||
$this->assertArrayHasKey( 'thumbnail_width', $data );
|
||||
$this->assertArrayHasKey( 'thumbnail_height', $data );
|
||||
$this->assertTrue( 400 >= $data['thumbnail_width'] );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @runTestsInSeparateProcesses
|
||||
* @preserveGlobalState disabled
|
||||
* @group oembed
|
||||
* @group oembed-headers
|
||||
*/
|
||||
class Tests_oEmbed_HTTP_Headers extends WP_UnitTestCase {
|
||||
function test_request_json_response_headers() {
|
||||
if ( ! function_exists( 'xdebug_get_headers' ) ) {
|
||||
$this->markTestSkipped( 'xdebug is required for this test' );
|
||||
}
|
||||
|
||||
$post = $this->factory->post->create_and_get( array(
|
||||
'post_title' => 'Hello World',
|
||||
) );
|
||||
|
||||
$request = array(
|
||||
'url' => get_permalink( $post->ID ),
|
||||
'format' => 'json',
|
||||
'maxwidth' => 600,
|
||||
'callback' => '',
|
||||
);
|
||||
|
||||
$legacy_controller = new WP_oEmbed_Controller();
|
||||
$legacy_controller->dispatch( $request );
|
||||
|
||||
$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 = $this->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 );
|
||||
|
||||
$headers = xdebug_get_headers();
|
||||
|
||||
$this->assertTrue( in_array( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), $headers ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group oembed
|
||||
*/
|
||||
class Tests_Post_Embed_URL extends WP_UnitTestCase {
|
||||
function test_get_post_embed_url_non_existent_post() {
|
||||
$embed_url = get_post_embed_url( 0 );
|
||||
$this->assertFalse( $embed_url );
|
||||
}
|
||||
|
||||
function test_get_post_embed_url_with_pretty_permalinks() {
|
||||
update_option( 'permalink_structure', '/%postname%' );
|
||||
|
||||
$post_id = $this->factory->post->create();
|
||||
$permalink = get_permalink( $post_id );
|
||||
$embed_url = get_post_embed_url( $post_id );
|
||||
|
||||
$this->assertEquals( user_trailingslashit( trailingslashit( $permalink ) . 'embed' ), $embed_url );
|
||||
|
||||
update_option( 'permalink_structure', '' );
|
||||
}
|
||||
|
||||
function test_get_post_embed_url_with_ugly_permalinks() {
|
||||
$post_id = $this->factory->post->create();
|
||||
$permalink = get_permalink( $post_id );
|
||||
$embed_url = get_post_embed_url( $post_id );
|
||||
|
||||
$this->assertEquals( $permalink . '&embed=true', $embed_url );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,257 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group oembed
|
||||
*/
|
||||
class Tests_Embed_Template extends WP_UnitTestCase {
|
||||
function test_oembed_output_post() {
|
||||
$user = $this->factory->user->create_and_get( array(
|
||||
'display_name' => 'John Doe',
|
||||
) );
|
||||
|
||||
$post_id = $this->factory->post->create( array(
|
||||
'post_author' => $user->ID,
|
||||
'post_title' => 'Hello World',
|
||||
'post_content' => 'Foo Bar',
|
||||
'post_excerpt' => 'Bar Baz',
|
||||
) );
|
||||
$this->go_to( get_post_embed_url( $post_id ) );
|
||||
|
||||
$this->assertQueryTrue( 'is_single', 'is_singular' );
|
||||
|
||||
ob_start();
|
||||
include( ABSPATH . WPINC . '/embed-template.php' );
|
||||
$actual = ob_get_clean();
|
||||
|
||||
$doc = new DOMDocument();
|
||||
$this->assertTrue( $doc->loadHTML( $actual ) );
|
||||
$this->assertTrue( false === strpos( $actual, 'Page not found' ) );
|
||||
$this->assertTrue( false !== strpos( $actual, 'Hello World' ) );
|
||||
}
|
||||
|
||||
function test_oembed_output_post_with_thumbnail() {
|
||||
$post_id = $this->factory->post->create( array(
|
||||
'post_title' => 'Hello World',
|
||||
'post_content' => 'Foo Bar',
|
||||
'post_excerpt' => 'Bar Baz',
|
||||
) );
|
||||
$file = DIR_TESTDATA . '/images/canola.jpg';
|
||||
$attachment_id = $this->factory->attachment->create_object( $file, $post_id, array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
) );
|
||||
set_post_thumbnail( $post_id, $attachment_id );
|
||||
|
||||
$this->go_to( get_post_embed_url( $post_id ) );
|
||||
|
||||
$this->assertQueryTrue( 'is_single', 'is_singular' );
|
||||
|
||||
ob_start();
|
||||
include( ABSPATH . WPINC . '/embed-template.php' );
|
||||
$actual = ob_get_clean();
|
||||
|
||||
$doc = new DOMDocument();
|
||||
$this->assertTrue( $doc->loadHTML( $actual ) );
|
||||
$this->assertFalse( strpos( $actual, 'Page not found' ) );
|
||||
$this->assertTrue( false !== strpos( $actual, 'Hello World' ) );
|
||||
$this->assertTrue( false !== strpos( $actual, 'canola.jpg' ) );
|
||||
}
|
||||
|
||||
function test_oembed_output_404() {
|
||||
$this->go_to( home_url( '/?p=123&embed=true' ) );
|
||||
$GLOBALS['wp_query']->query_vars['embed'] = true;
|
||||
|
||||
$this->assertQueryTrue( 'is_404' );
|
||||
|
||||
ob_start();
|
||||
include( ABSPATH . WPINC . '/embed-template.php' );
|
||||
$actual = ob_get_clean();
|
||||
|
||||
$doc = new DOMDocument();
|
||||
$this->assertTrue( $doc->loadHTML( $actual ) );
|
||||
$this->assertTrue( false !== strpos( $actual, 'Page not found' ) );
|
||||
}
|
||||
|
||||
function test_oembed_output_attachment() {
|
||||
$post = $this->factory->post->create_and_get();
|
||||
$file = DIR_TESTDATA . '/images/canola.jpg';
|
||||
$attachment_id = $this->factory->attachment->create_object( $file, $post->ID, array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
'post_title' => 'Hello World',
|
||||
'post_content' => 'Foo Bar',
|
||||
'post_excerpt' => 'Bar Baz',
|
||||
) );
|
||||
|
||||
$this->go_to( get_post_embed_url( $attachment_id ) );
|
||||
|
||||
$this->assertQueryTrue( 'is_single', 'is_singular', 'is_attachment' );
|
||||
|
||||
ob_start();
|
||||
include( ABSPATH . WPINC . '/embed-template.php' );
|
||||
$actual = ob_get_clean();
|
||||
|
||||
$doc = new DOMDocument();
|
||||
$this->assertTrue( $doc->loadHTML( $actual ) );
|
||||
$this->assertFalse( strpos( $actual, 'Page not found' ) );
|
||||
$this->assertTrue( false !== strpos( $actual, 'Hello World' ) );
|
||||
$this->assertTrue( false !== strpos( $actual, 'canola.jpg' ) );
|
||||
}
|
||||
|
||||
function test_oembed_output_draft_post() {
|
||||
$post_id = $this->factory->post->create( array(
|
||||
'post_title' => 'Hello World',
|
||||
'post_content' => 'Foo Bar',
|
||||
'post_excerpt' => 'Bar Baz',
|
||||
'post_status' => 'draft',
|
||||
) );
|
||||
|
||||
$this->go_to( get_post_embed_url( $post_id ) );
|
||||
|
||||
$this->assertQueryTrue( 'is_404' );
|
||||
|
||||
ob_start();
|
||||
include( ABSPATH . WPINC . '/embed-template.php' );
|
||||
$actual = ob_get_clean();
|
||||
|
||||
$doc = new DOMDocument();
|
||||
$this->assertTrue( $doc->loadHTML( $actual ) );
|
||||
$this->assertTrue( false !== strpos( $actual, 'Page not found' ) );
|
||||
}
|
||||
|
||||
function test_oembed_output_scheduled_post() {
|
||||
$post_id = $this->factory->post->create( array(
|
||||
'post_title' => 'Hello World',
|
||||
'post_content' => 'Foo Bar',
|
||||
'post_excerpt' => 'Bar Baz',
|
||||
'post_status' => 'future',
|
||||
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 day' ) ),
|
||||
) );
|
||||
|
||||
$this->go_to( get_post_embed_url( $post_id ) );
|
||||
|
||||
$this->assertQueryTrue( 'is_404' );
|
||||
|
||||
ob_start();
|
||||
include( ABSPATH . WPINC . '/embed-template.php' );
|
||||
$actual = ob_get_clean();
|
||||
|
||||
$doc = new DOMDocument();
|
||||
$this->assertTrue( $doc->loadHTML( $actual ) );
|
||||
$this->assertTrue( false !== strpos( $actual, 'Page not found' ) );
|
||||
}
|
||||
|
||||
function test_oembed_output_private_post() {
|
||||
$post_id = $this->factory->post->create( array(
|
||||
'post_title' => 'Hello World',
|
||||
'post_content' => 'Foo Bar',
|
||||
'post_excerpt' => 'Bar Baz',
|
||||
'post_status' => 'private',
|
||||
) );
|
||||
|
||||
$this->go_to( get_post_embed_url( $post_id ) );
|
||||
|
||||
$this->assertQueryTrue( 'is_404' );
|
||||
|
||||
ob_start();
|
||||
include( ABSPATH . WPINC . '/embed-template.php' );
|
||||
$actual = ob_get_clean();
|
||||
|
||||
$doc = new DOMDocument();
|
||||
$this->assertTrue( $doc->loadHTML( $actual ) );
|
||||
$this->assertTrue( false !== strpos( $actual, 'Page not found' ) );
|
||||
}
|
||||
|
||||
function test_oembed_output_private_post_with_permissions() {
|
||||
$user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
|
||||
wp_set_current_user( $user_id );
|
||||
|
||||
$post_id = $this->factory->post->create( array(
|
||||
'post_title' => 'Hello World',
|
||||
'post_content' => 'Foo Bar',
|
||||
'post_excerpt' => 'Bar Baz',
|
||||
'post_status' => 'private',
|
||||
'post_author' => $user_id,
|
||||
) );
|
||||
|
||||
$this->go_to( get_post_embed_url( $post_id ) );
|
||||
|
||||
$this->assertQueryTrue( 'is_single', 'is_singular' );
|
||||
|
||||
ob_start();
|
||||
include( ABSPATH . WPINC . '/embed-template.php' );
|
||||
$actual = ob_get_clean();
|
||||
|
||||
$doc = new DOMDocument();
|
||||
$this->assertTrue( $doc->loadHTML( $actual ) );
|
||||
$this->assertTrue( false === strpos( $actual, 'Page not found' ) );
|
||||
$this->assertTrue( false !== strpos( $actual, 'Hello World' ) );
|
||||
}
|
||||
|
||||
function test_wp_oembed_excerpt_more_no_embed() {
|
||||
$GLOBALS['wp_query'] = new WP_Query();
|
||||
|
||||
$this->assertEquals( 'foo bar', wp_oembed_excerpt_more( 'foo bar' ) );
|
||||
}
|
||||
|
||||
function test_wp_oembed_excerpt_more() {
|
||||
$post_id = $this->factory->post->create( array(
|
||||
'post_content' => 'Foo Bar',
|
||||
) );
|
||||
|
||||
$this->assertEquals( '', wp_oembed_excerpt_more( '' ) );
|
||||
|
||||
$this->go_to( get_post_embed_url( $post_id ) );
|
||||
|
||||
$actual = wp_oembed_excerpt_more( '' );
|
||||
|
||||
$expected = sprintf(
|
||||
'… <a class="wp-embed-more" href="%s" target="_top">Read more</a>',
|
||||
get_the_permalink()
|
||||
);
|
||||
|
||||
$this->assertEquals( $expected, $actual );
|
||||
}
|
||||
|
||||
function test_is_embed_post() {
|
||||
$this->assertFalse( is_embed() );
|
||||
|
||||
$post_id = $this->factory->post->create();
|
||||
$this->go_to( get_post_embed_url( $post_id ) );
|
||||
$this->assertTrue( is_embed() );
|
||||
}
|
||||
|
||||
function test_is_embed_attachment() {
|
||||
$post_id = $this->factory->post->create();
|
||||
$file = DIR_TESTDATA . '/images/canola.jpg';
|
||||
$attachment_id = $this->factory->attachment->create_object( $file, $post_id, array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
) );
|
||||
$this->go_to( get_post_embed_url( $attachment_id ) );
|
||||
$this->assertTrue( is_embed() );
|
||||
}
|
||||
|
||||
function test_is_embed_404() {
|
||||
$this->go_to( home_url( '/?p=12345&embed=true' ) );
|
||||
$this->assertTrue( is_embed() );
|
||||
}
|
||||
|
||||
function test_get_post_embed_html_non_existent_post() {
|
||||
$this->assertFalse( get_post_embed_html( 0, 200, 200 ) );
|
||||
$this->assertFalse( get_post_embed_html( null, 200, 200 ) );
|
||||
}
|
||||
|
||||
function test_get_post_embed_html() {
|
||||
$post_id = $this->factory->post->create();
|
||||
|
||||
$expected = '<iframe sandbox="allow-scripts" security="restricted" src="' . esc_url( get_post_embed_url( $post_id ) ) . '" width="200" height="200" title="Embedded WordPress Post" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" class="wp-embedded-content"></iframe>';
|
||||
|
||||
$this->assertStringEndsWith( $expected, get_post_embed_html( $post_id, 200, 200 ) );
|
||||
}
|
||||
|
||||
function test_add_host_js() {
|
||||
ob_start();
|
||||
wp_oembed_add_host_js();
|
||||
ob_end_clean();
|
||||
|
||||
$this->assertTrue( wp_script_is( 'wp-oembed' ) );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user