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