<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[37892] trunk/tests/phpunit/tests/oembed/WpEmbed.php: Embeds: Add tests for the `WP_Embed` class.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/37892">37892</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/37892","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>swissspidy</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-06-28 20:30:15 +0000 (Tue, 28 Jun 2016)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Embeds: Add tests for the `WP_Embed` class.

Fixes <a href="https://core.trac.wordpress.org/ticket/37214">#37214</a>. See <a href="https://core.trac.wordpress.org/ticket/34115">#34115</a>.</pre>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsoembedWpEmbedphp">trunk/tests/phpunit/tests/oembed/WpEmbed.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunktestsphpunittestsoembedWpEmbedphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/oembed/WpEmbed.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/oembed/WpEmbed.php                              (rev 0)
+++ trunk/tests/phpunit/tests/oembed/WpEmbed.php        2016-06-28 20:30:15 UTC (rev 37892)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,250 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group oembed
+ */
+class Tests_WP_Embed extends WP_UnitTestCase {
+       /**
+        * @var WP_Embed
+        */
+       protected $wp_embed;
+
+       public function setUp() {
+               $this->wp_embed = new WP_Embed();
+       }
+
+       public function _embed_handler_callback( $matches, $attr, $url, $rawattr ) {
+               return sprintf( 'Embedded %s', $url );
+       }
+
+       public function _pre_oembed_result_callback() {
+               return '<b>Embedded content</b>';
+       }
+
+       public function test_maybe_run_ajax_cache_should_return_nothing_if_there_is_no_post() {
+               $this->expectOutputString('');
+               $this->wp_embed->maybe_run_ajax_cache();
+       }
+
+       public function test_maybe_run_ajax_cache_should_return_nothing_if_there_is_no_message() {
+               $GLOBALS['post'] = $this->factory()->post->create_and_get( array(
+                       'post_title' => 'Hello World',
+               ) );
+
+               $this->expectOutputString('');
+
+               $this->wp_embed->maybe_run_ajax_cache();
+               unset( $GLOBALS['post'] );
+       }
+
+       public function test_maybe_run_ajax_cache_should_return_javascript() {
+               $GLOBALS['post'] = $this->factory()->post->create_and_get( array(
+                       'post_title' => 'Hello World',
+               ) );
+               $_GET['message'] = 'foo';
+
+               $url    = admin_url( 'admin-ajax.php?action=oembed-cache&post=' . $GLOBALS['post']->ID, 'relative' );
+               $actual = get_echo( array( $this->wp_embed, 'maybe_run_ajax_cache' ) );
+
+               unset( $GLOBALS['post'] );
+               unset( $GLOBALS['message'] );
+
+               $this->assertContains( $url, $actual );
+       }
+
+       public function test_wp_maybe_load_embeds() {
+               $this->assertEqualSets( array( 10, 9999 ), array_keys( $GLOBALS['wp_embed']->handlers ) );
+               $this->assertEqualSets( array(
+                       'youtube_embed_url',
+               ), array_keys( $GLOBALS['wp_embed']->handlers[10] ) );
+               $this->assertEqualSets( array(
+                       'audio',
+                       'video',
+               ), array_keys( $GLOBALS['wp_embed']->handlers[9999] ) );
+       }
+
+       public function test_wp_embed_register_handler() {
+               $handle   = rand_str();
+               $regex    = '#https?://example\.com/embed/([^/]+)#i';
+               $callback = array( $this, '_embed_handler_callback' );
+
+               wp_embed_register_handler( $handle, $regex, $callback );
+
+               $expected = array(
+                       'regex'    => $regex,
+                       'callback' => $callback,
+               );
+               $actual   = $GLOBALS['wp_embed']->handlers[10];
+
+               wp_embed_unregister_handler( $handle );
+
+               $this->assertContains( $expected, $actual );
+       }
+
+       public function test_wp_embed_unregister_handler() {
+               $this->assertArrayHasKey( 'youtube_embed_url', $GLOBALS['wp_embed']->handlers[10] );
+
+               wp_embed_unregister_handler( 'youtube_embed_url' );
+
+               $handlers = $GLOBALS['wp_embed']->handlers[10];
+
+               // Restore.
+               wp_embed_register_handler( 'youtube_embed_url', '#https?://(www.)?youtube\.com/(?:v|embed)/([^/]+)#i', 'wp_embed_handler_youtube' );
+
+               $this->assertArrayNotHasKey( 'youtube_embed_url', $handlers );
+       }
+
+       public function test_autoembed_should_do_nothing_without_matching_handler() {
+               $content = "\nhttp://example.com/embed/foo\n";
+
+               $actual = $this->wp_embed->autoembed( $content );
+               $this->assertEquals( $content, $actual );
+       }
+
+       public function test_autoembed_should_return_modified_content() {
+               $handle   = rand_str();
+               $regex    = '#https?://example\.com/embed/([^/]+)#i';
+               $callback = array( $this, '_embed_handler_callback' );
+
+               wp_embed_register_handler( $handle, $regex, $callback );
+
+               $content = "\nhttp://example.com/embed/foo\n";
+
+               $actual = $GLOBALS['wp_embed']->autoembed( $content );
+               wp_embed_unregister_handler( $handle );
+
+               $this->assertEquals( "\nEmbedded http://example.com/embed/foo\n", $actual );
+       }
+
+       public function test_delete_oembed_caches() {
+               $post_id = $this->factory()->post->create();
+
+               add_post_meta( $post_id, '_oembed_foo', 'bar' );
+               add_post_meta( $post_id, '_oembed_foo', 'baz' );
+               add_post_meta( $post_id, '_oembed_baz', 'foobar', true );
+
+               $this->wp_embed->delete_oembed_caches( $post_id );
+
+               $this->assertEquals( array(), get_post_meta( $post_id, '_oembed_foo' ) );
+               $this->assertEquals( array(), get_post_meta( $post_id, '_oembed_baz' ) );
+       }
+
+       public function test_cache_oembed_invalid_post_type() {
+               $post_id = $this->factory()->post->create( array( 'post_type' => 'nav_menu_item' ) );
+
+               $this->wp_embed->cache_oembed( $post_id );
+               $this->assertNotSame( $post_id, $this->wp_embed->post_ID );
+       }
+
+       public function test_cache_oembed_empty_content() {
+               $post_id = $this->factory()->post->create( array( 'post_content' => '' ) );
+
+               $this->wp_embed->cache_oembed( $post_id );
+               $this->assertNotSame( $post_id, $this->wp_embed->post_ID );
+       }
+
+       public function test_cache_oembed_for_post() {
+               $url           = 'https://example.com/';
+               $expected      = '<b>Embedded content</b>';
+               $key_suffix    = md5( $url . serialize( wp_embed_defaults( $url ) ) );
+               $cachekey      = '_oembed_' . $key_suffix;
+               $cachekey_time = '_oembed_time_' . $key_suffix;
+
+               $post_id = $this->factory()->post->create( array( 'post_content' => 'https://example.com/' ) );
+
+               add_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
+               $this->wp_embed->cache_oembed( $post_id );
+               remove_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
+
+               $this->assertSame( $post_id, $this->wp_embed->post_ID );
+               $this->assertEquals( $expected, get_post_meta( $post_id, $cachekey, true ) );
+               $this->assertNotEmpty( get_post_meta( $post_id, $cachekey_time, true ) );
+       }
+
+       public function test_shortcode_should_cache_data_in_post_meta_for_known_post() {
+               $GLOBALS['post'] = $this->factory()->post->create_and_get();
+               $url             = 'https://example.com/';
+               $expected        = '<b>Embedded content</b>';
+               $key_suffix      = md5( $url . serialize( wp_embed_defaults( $url ) ) );
+               $cachekey        = '_oembed_' . $key_suffix;
+               $cachekey_time   = '_oembed_time_' . $key_suffix;
+
+               add_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
+               $actual = $this->wp_embed->shortcode( array(), $url );
+               remove_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
+
+               $this->assertEquals( $expected, $actual );
+
+               $this->assertEquals( $expected, get_post_meta( $GLOBALS['post']->ID, $cachekey, true ) );
+               $this->assertNotEmpty( get_post_meta( $GLOBALS['post']->ID, $cachekey_time, true ) );
+
+               // Result should be cached.
+               $actual = $this->wp_embed->shortcode( array(), $url );
+               $this->assertEquals( $expected, $actual );
+       }
+
+       public function test_shortcode_should_cache_failure_in_post_meta_for_known_post() {
+               $GLOBALS['post'] = $this->factory()->post->create_and_get();
+               $url             = 'https://example.com/';
+               $expected        = '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>';
+               $key_suffix      = md5( $url . serialize( wp_embed_defaults( $url ) ) );
+               $cachekey        = '_oembed_' . $key_suffix;
+               $cachekey_time   = '_oembed_time_' . $key_suffix;
+
+               add_filter( 'pre_oembed_result', '__return_empty_string' );
+               $actual = $this->wp_embed->shortcode( array(), $url );
+               remove_filter( 'pre_oembed_result', '__return_empty_string' );
+
+               $this->assertEquals( $expected, $actual );
+
+               $this->assertEquals( '{{unknown}}', get_post_meta( $GLOBALS['post']->ID, $cachekey, true ) );
+               $this->assertEmpty( get_post_meta( $GLOBALS['post']->ID, $cachekey_time, true ) );
+
+               // Result should be cached.
+               $actual = $this->wp_embed->shortcode( array(), $url );
+               $this->assertEquals( $expected, $actual );
+       }
+
+       public function test_shortcode_should_get_url_from_src_attribute() {
+               $url    = 'http://example.com/embed/foo';
+               $actual = $this->wp_embed->shortcode( array( 'src' => $url ) );
+
+               $this->assertEquals( '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>', $actual );
+       }
+
+       public function test_shortcode_should_return_empty_string_for_missing_url() {
+               $this->assertEmpty( $this->wp_embed->shortcode( array() ) );
+       }
+
+       public function test_shortcode_should_make_link_for_unknown_url() {
+               $url    = 'http://example.com/embed/foo';
+               $actual = $this->wp_embed->shortcode( array(), $url );
+
+               $this->assertEquals( '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>', $actual );
+       }
+
+       public function test_run_shortcode_url_only() {
+               $url    = 'http://example.com/embed/foo';
+               $actual = $this->wp_embed->run_shortcode( '[embed]' . $url . '[/embed]' );
+               $this->assertEquals( '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>', $actual );
+       }
+
+       public function test_maybe_make_link() {
+               $url    = 'http://example.com/embed/foo';
+               $actual = $this->wp_embed->maybe_make_link( $url );
+
+               $this->assertEquals( '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>', $actual );
+       }
+
+       public function test_maybe_make_link_return_false_on_fail() {
+               $this->wp_embed->return_false_on_fail = true;
+               $this->assertFalse( $this->wp_embed->maybe_make_link( 'http://example.com/' ) );
+       }
+
+       public function test_maybe_make_link_do_not_link_if_unknown() {
+               $url = 'http://example.com/';
+
+               $this->wp_embed->linkifunknown = false;
+               $this->assertEquals( $url, $this->wp_embed->maybe_make_link( $url ) );
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/oembed/WpEmbed.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>

</body>
</html>