WpEmbed.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. /**
  3. * @group oembed
  4. */
  5. class Tests_WP_Embed extends WP_UnitTestCase {
  6. /**
  7. * @var WP_Embed
  8. */
  9. protected $wp_embed;
  10. public function setUp() {
  11. parent::setUp();
  12. $this->wp_embed = new WP_Embed();
  13. }
  14. public function _embed_handler_callback( $matches, $attr, $url, $rawattr ) {
  15. return sprintf( 'Embedded %s', $url );
  16. }
  17. public function _pre_oembed_result_callback() {
  18. return '<b>Embedded content</b>';
  19. }
  20. public function test_maybe_run_ajax_cache_should_return_nothing_if_there_is_no_post() {
  21. $this->expectOutputString( '' );
  22. $this->wp_embed->maybe_run_ajax_cache();
  23. }
  24. public function test_maybe_run_ajax_cache_should_return_nothing_if_there_is_no_message() {
  25. $GLOBALS['post'] = $this->factory()->post->create_and_get(
  26. array(
  27. 'post_title' => 'Hello World',
  28. )
  29. );
  30. $this->expectOutputString( '' );
  31. $this->wp_embed->maybe_run_ajax_cache();
  32. unset( $GLOBALS['post'] );
  33. }
  34. public function test_maybe_run_ajax_cache_should_return_javascript() {
  35. $GLOBALS['post'] = $this->factory()->post->create_and_get(
  36. array(
  37. 'post_title' => 'Hello World',
  38. )
  39. );
  40. $_GET['message'] = 'foo';
  41. $url = admin_url( 'admin-ajax.php?action=oembed-cache&post=' . $GLOBALS['post']->ID, 'relative' );
  42. $actual = get_echo( array( $this->wp_embed, 'maybe_run_ajax_cache' ) );
  43. unset( $GLOBALS['post'] );
  44. unset( $GLOBALS['message'] );
  45. $this->assertContains( $url, $actual );
  46. }
  47. public function test_wp_maybe_load_embeds() {
  48. $this->assertSameSets( array( 10, 9999 ), array_keys( $GLOBALS['wp_embed']->handlers ) );
  49. $this->assertSameSets(
  50. array(
  51. 'youtube_embed_url',
  52. ),
  53. array_keys( $GLOBALS['wp_embed']->handlers[10] )
  54. );
  55. $this->assertSameSets(
  56. array(
  57. 'audio',
  58. 'video',
  59. ),
  60. array_keys( $GLOBALS['wp_embed']->handlers[9999] )
  61. );
  62. }
  63. public function test_wp_embed_register_handler() {
  64. $handle = __FUNCTION__;
  65. $regex = '#https?://example\.com/embed/([^/]+)#i';
  66. $callback = array( $this, '_embed_handler_callback' );
  67. wp_embed_register_handler( $handle, $regex, $callback );
  68. $expected = array(
  69. 'regex' => $regex,
  70. 'callback' => $callback,
  71. );
  72. $actual = $GLOBALS['wp_embed']->handlers[10];
  73. wp_embed_unregister_handler( $handle );
  74. $this->assertContains( $expected, $actual );
  75. }
  76. public function test_wp_embed_unregister_handler() {
  77. $this->assertArrayHasKey( 'youtube_embed_url', $GLOBALS['wp_embed']->handlers[10] );
  78. wp_embed_unregister_handler( 'youtube_embed_url' );
  79. $handlers = $GLOBALS['wp_embed']->handlers[10];
  80. // Restore.
  81. wp_embed_register_handler( 'youtube_embed_url', '#https?://(www.)?youtube\.com/(?:v|embed)/([^/]+)#i', 'wp_embed_handler_youtube' );
  82. $this->assertArrayNotHasKey( 'youtube_embed_url', $handlers );
  83. }
  84. /**
  85. * @group external-http
  86. */
  87. public function test_autoembed_should_do_nothing_without_matching_handler() {
  88. $content = "\nhttp://example.com/embed/foo\n";
  89. $actual = $this->wp_embed->autoembed( $content );
  90. $this->assertSame( $content, $actual );
  91. }
  92. /**
  93. * @group external-http
  94. */
  95. public function test_autoembed_should_return_modified_content() {
  96. $handle = __FUNCTION__;
  97. $regex = '#https?://example\.com/embed/([^/]+)#i';
  98. $callback = array( $this, '_embed_handler_callback' );
  99. wp_embed_register_handler( $handle, $regex, $callback );
  100. $content = "\nhttp://example.com/embed/foo\n";
  101. $actual = $GLOBALS['wp_embed']->autoembed( $content );
  102. wp_embed_unregister_handler( $handle );
  103. $this->assertSame( "\nEmbedded http://example.com/embed/foo\n", $actual );
  104. }
  105. public function test_delete_oembed_caches() {
  106. $post_id = $this->factory()->post->create();
  107. add_post_meta( $post_id, '_oembed_foo', 'bar' );
  108. add_post_meta( $post_id, '_oembed_foo', 'baz' );
  109. add_post_meta( $post_id, '_oembed_baz', 'foobar', true );
  110. $this->wp_embed->delete_oembed_caches( $post_id );
  111. $this->assertSame( array(), get_post_meta( $post_id, '_oembed_foo' ) );
  112. $this->assertSame( array(), get_post_meta( $post_id, '_oembed_baz' ) );
  113. }
  114. public function test_cache_oembed_invalid_post_type() {
  115. $post_id = $this->factory()->post->create( array( 'post_type' => 'nav_menu_item' ) );
  116. $this->wp_embed->cache_oembed( $post_id );
  117. $this->assertNotSame( $post_id, $this->wp_embed->post_ID );
  118. }
  119. public function test_cache_oembed_empty_content() {
  120. $post_id = $this->factory()->post->create( array( 'post_content' => '' ) );
  121. $this->wp_embed->cache_oembed( $post_id );
  122. $this->assertNotSame( $post_id, $this->wp_embed->post_ID );
  123. }
  124. public function test_cache_oembed_for_post() {
  125. $url = 'https://example.com/';
  126. $expected = '<b>Embedded content</b>';
  127. $key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
  128. $cachekey = '_oembed_' . $key_suffix;
  129. $cachekey_time = '_oembed_time_' . $key_suffix;
  130. $post_id = $this->factory()->post->create( array( 'post_content' => 'https://example.com/' ) );
  131. add_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
  132. $this->wp_embed->cache_oembed( $post_id );
  133. remove_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
  134. $this->assertSame( $post_id, $this->wp_embed->post_ID );
  135. $this->assertSame( $expected, get_post_meta( $post_id, $cachekey, true ) );
  136. $this->assertNotEmpty( get_post_meta( $post_id, $cachekey_time, true ) );
  137. }
  138. public function test_shortcode_should_get_cached_data_from_post_meta_for_known_post() {
  139. global $post;
  140. $post = $this->factory()->post->create_and_get();
  141. $url = 'https://example.com/';
  142. $expected = '<b>Embedded content</b>';
  143. $key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
  144. $cachekey = '_oembed_' . $key_suffix;
  145. add_post_meta( $post->ID, $cachekey, $expected );
  146. add_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
  147. $actual = $this->wp_embed->shortcode( array(), $url );
  148. remove_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
  149. $actual_2 = $this->wp_embed->shortcode( array(), $url );
  150. $cached = get_post_meta( $post->ID, $cachekey, true );
  151. // Cleanup.
  152. unset( $post );
  153. $this->assertSame( $expected, $actual );
  154. $this->assertSame( $expected, $actual_2 );
  155. $this->assertSame( $expected, $cached );
  156. }
  157. public function test_shortcode_should_get_cached_failure_from_post_meta_for_known_post() {
  158. global $post;
  159. $post = $this->factory()->post->create_and_get();
  160. $url = 'https://example.com/';
  161. $expected = '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>';
  162. $key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
  163. $cachekey = '_oembed_' . $key_suffix;
  164. $cachekey_time = '_oembed_time_' . $key_suffix;
  165. add_post_meta( $post->ID, $cachekey, '{{unknown}}' );
  166. add_post_meta( $post->ID, $cachekey_time, 0 );
  167. add_filter( 'pre_oembed_result', '__return_empty_string' );
  168. $actual = $this->wp_embed->shortcode( array(), $url );
  169. remove_filter( 'pre_oembed_result', '__return_empty_string' );
  170. // Result should be cached.
  171. $actual_2 = $this->wp_embed->shortcode( array(), $url );
  172. $cached = get_post_meta( $post->ID, $cachekey, true );
  173. $cached_time = get_post_meta( $post->ID, $cachekey_time, true );
  174. // Cleanup.
  175. unset( $post );
  176. $this->assertSame( $expected, $actual );
  177. $this->assertSame( '{{unknown}}', $cached );
  178. $this->assertEmpty( $cached_time );
  179. $this->assertSame( $expected, $actual_2 );
  180. }
  181. /**
  182. * @ticket 34115
  183. */
  184. public function test_shortcode_should_cache_data_in_custom_post() {
  185. $url = 'https://example.com/';
  186. $expected = '<b>Embedded content</b>';
  187. $key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
  188. add_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
  189. $actual = $this->wp_embed->shortcode( array(), $url );
  190. remove_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
  191. $oembed_post_id = $this->wp_embed->find_oembed_post_id( $key_suffix );
  192. $post_content = get_post( $oembed_post_id )->post_content;
  193. // Result should be cached.
  194. $actual_2 = $this->wp_embed->shortcode( array(), $url );
  195. wp_delete_post( $oembed_post_id );
  196. $this->assertNotNull( $oembed_post_id );
  197. $this->assertSame( $expected, $post_content );
  198. $this->assertSame( $expected, $actual );
  199. $this->assertSame( $expected, $actual_2 );
  200. }
  201. /**
  202. * @ticket 34115
  203. */
  204. public function test_shortcode_should_cache_failure_in_custom_post() {
  205. $url = 'https://example.com/';
  206. $expected = '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>';
  207. $key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
  208. add_filter( 'pre_oembed_result', '__return_empty_string' );
  209. $actual = $this->wp_embed->shortcode( array(), $url );
  210. remove_filter( 'pre_oembed_result', '__return_empty_string' );
  211. $oembed_post_id = $this->wp_embed->find_oembed_post_id( $key_suffix );
  212. $post_content = get_post( $oembed_post_id )->post_content;
  213. // Result should be cached.
  214. $actual_2 = $this->wp_embed->shortcode( array(), $url );
  215. wp_delete_post( $oembed_post_id );
  216. $this->assertSame( $expected, $actual );
  217. $this->assertSame( $expected, $actual_2 );
  218. $this->assertNotNull( $oembed_post_id );
  219. $this->assertSame( '{{unknown}}', $post_content );
  220. }
  221. /**
  222. * Test that parsing an embed shortcode should cause oembed_cache to be updated.
  223. *
  224. * @ticket 42310
  225. */
  226. public function test_shortcode_should_update_custom_post() {
  227. add_filter( 'oembed_ttl', '__return_zero' );
  228. $url = 'https://example.com/';
  229. $embedded = '<b>Embedded content</b>';
  230. $key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
  231. add_filter( 'pre_oembed_result', '__return_empty_string' );
  232. $this->wp_embed->shortcode( array(), $url );
  233. remove_filter( 'pre_oembed_result', '__return_empty_string' );
  234. $oembed_post_id = $this->wp_embed->find_oembed_post_id( $key_suffix );
  235. $this->assertSame( '{{unknown}}', get_post( $oembed_post_id )->post_content );
  236. $previous_usecache = $this->wp_embed->usecache;
  237. $this->wp_embed->usecache = false;
  238. // The update cannot be empty because empty responses won't overwrite the cache.
  239. add_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
  240. $this->wp_embed->shortcode( array(), $url );
  241. remove_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
  242. $this->assertSame( $embedded, get_post( $oembed_post_id )->post_content );
  243. $this->wp_embed->usecache = $previous_usecache;
  244. remove_filter( 'oembed_ttl', '__return_zero' );
  245. }
  246. /**
  247. * @group external-http
  248. */
  249. public function test_shortcode_should_get_url_from_src_attribute() {
  250. $url = 'http://example.com/embed/foo';
  251. $actual = $this->wp_embed->shortcode( array( 'src' => $url ) );
  252. $this->assertSame( '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>', $actual );
  253. }
  254. /**
  255. * @group external-http
  256. */
  257. public function test_shortcode_should_return_empty_string_for_missing_url() {
  258. $this->assertEmpty( $this->wp_embed->shortcode( array() ) );
  259. }
  260. /**
  261. * @group external-http
  262. */
  263. public function test_shortcode_should_make_link_for_unknown_url() {
  264. $url = 'http://example.com/embed/foo';
  265. $actual = $this->wp_embed->shortcode( array(), $url );
  266. $this->assertSame( '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>', $actual );
  267. }
  268. /**
  269. * @group external-http
  270. */
  271. public function test_run_shortcode_url_only() {
  272. $url = 'http://example.com/embed/foo';
  273. $actual = $this->wp_embed->run_shortcode( '[embed]' . $url . '[/embed]' );
  274. $this->assertSame( '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>', $actual );
  275. }
  276. public function test_maybe_make_link() {
  277. $url = 'http://example.com/embed/foo';
  278. $actual = $this->wp_embed->maybe_make_link( $url );
  279. $this->assertSame( '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>', $actual );
  280. }
  281. public function test_maybe_make_link_return_false_on_fail() {
  282. $this->wp_embed->return_false_on_fail = true;
  283. $this->assertFalse( $this->wp_embed->maybe_make_link( 'http://example.com/' ) );
  284. }
  285. public function test_maybe_make_link_do_not_link_if_unknown() {
  286. $url = 'http://example.com/';
  287. $this->wp_embed->linkifunknown = false;
  288. $this->assertSame( $url, $this->wp_embed->maybe_make_link( $url ) );
  289. }
  290. }