| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group oembed |
| 5 | */ |
| 6 | class Tests_oEmbed extends WP_UnitTestCase { |
| 7 | |
| 8 | /** |
| 9 | * An HTTP API response. |
| 10 | * |
| 11 | * @var array|WP_Error |
| 12 | */ |
| 13 | protected $http_response; |
| 14 | |
| 15 | public function setUp() { |
| 16 | parent::setUp(); |
| 17 | |
| 18 | require_once ABSPATH . WPINC . '/class-oembed.php'; |
| 19 | $this->oembed = _wp_oembed_get_object(); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Test the tests |
| 24 | * |
| 25 | * @ticket 32360 |
| 26 | * |
| 27 | * @dataProvider oEmbedProviderData |
| 28 | */ |
| 29 | public function testOembedTestURLsMatch( $match, array $urls ) { |
| 30 | |
| 31 | if ( empty( $urls ) ) { |
| 32 | $this->markTestIncomplete(); |
| 33 | } |
| 34 | |
| 35 | foreach ( $urls as $url ) { |
| 36 | |
| 37 | $msg = sprintf( 'Test URL: %s', $url ); |
| 38 | |
| 39 | // Ensure the test URL is actually matched by the provider regex |
| 40 | if ( true === $this->oembed->providers[ $match ][1] ) { |
| 41 | |
| 42 | $this->assertSame( 1, preg_match( $match, $url ), $msg ); |
| 43 | |
| 44 | } else { |
| 45 | |
| 46 | // @TODO convert non-regex into regex and test |
| 47 | |
| 48 | $this->markTestSkipped( $msg ); |
| 49 | |
| 50 | } |
| 51 | |
| 52 | } |
| 53 | |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @group external-oembed |
| 58 | * @ticket 32360 |
| 59 | * |
| 60 | * @dataProvider oEmbedProviderData |
| 61 | */ |
| 62 | public function testOembedProviderReturnsExpectedResponse( $match, array $urls ) { |
| 63 | |
| 64 | if ( empty( $urls ) ) { |
| 65 | $this->markTestIncomplete(); |
| 66 | } |
| 67 | |
| 68 | $this->setup_http_hooks(); |
| 69 | |
| 70 | $test_args = array( |
| 71 | array( |
| 72 | 'discover' => false, |
| 73 | 'width' => 500, |
| 74 | 'height' => 500, |
| 75 | ), |
| 76 | // array( |
| 77 | // 'discover' => false, |
| 78 | // 'width' => '_INVALID_', |
| 79 | // 'height' => '_INVALID_', |
| 80 | // ), |
| 81 | ); |
| 82 | |
| 83 | // @TODO consider testing every URL as http and https |
| 84 | // @TODO test schemes of URLs in response |
| 85 | |
| 86 | foreach ( $urls as $url ) { |
| 87 | |
| 88 | foreach ( $test_args as $args ) { |
| 89 | |
| 90 | $msg = sprintf( "- Test URL: %s", $url ); |
| 91 | |
| 92 | $provider = $this->oembed->get_provider( $url, array( |
| 93 | 'discover' => false, |
| 94 | ) ); |
| 95 | $this->assertNotFalse( $provider, $msg ); |
| 96 | |
| 97 | $data = $this->oembed->fetch( $provider, $url, $args ); |
| 98 | |
| 99 | $r = $this->http_response; |
| 100 | |
| 101 | $msg .= sprintf( "\n- oEmbed URL: %s", $r['url'] ); |
| 102 | |
| 103 | // `WP_oEmbed::fetch()` only returns boolean false, so we need to hook into the HTTP API to get its error |
| 104 | if ( is_wp_error( $r['response'] ) ) { |
| 105 | $error_message = $r['response']->get_error_message(); |
| 106 | if ( empty( $error_message ) ) { |
| 107 | $error_message = '- no message -'; |
| 108 | } |
| 109 | |
| 110 | $this->fail( sprintf( "%s (%s)\n%s", $error_message, $r['response']->get_error_code(), $msg ) ); |
| 111 | } |
| 112 | |
| 113 | $this->assertSame( 200, wp_remote_retrieve_response_code( $r['response'] ), $msg ); |
| 114 | |
| 115 | $query = parse_url( $r['url'], PHP_URL_QUERY ); |
| 116 | parse_str( $query, $query_vars ); |
| 117 | |
| 118 | $this->assertArrayHasKey( 'maxheight', $query_vars, $msg ); |
| 119 | $this->assertArrayHasKey( 'maxwidth', $query_vars, $msg ); |
| 120 | $this->assertArrayHasKey( 'url', $query_vars, $msg ); |
| 121 | |
| 122 | // Check the `content-type` header is as expected based on the requested format |
| 123 | switch ( $query_vars['format'] ) { |
| 124 | |
| 125 | case 'json': |
| 126 | $this->assertStringStartsWith( 'application/json', wp_remote_retrieve_header( $r['response'], 'content-type' ), $msg ); |
| 127 | break; |
| 128 | |
| 129 | case 'xml': |
| 130 | $this->assertStringStartsWith( 'text/xml', wp_remote_retrieve_header( $r['response'], 'content-type' ), $msg ); |
| 131 | break; |
| 132 | |
| 133 | } |
| 134 | |
| 135 | $this->assertNotFalse( $data, $msg ); |
| 136 | |
| 137 | // Check for required response parameters |
| 138 | $this->assertObjectHasAttribute( 'type', $data, $msg ); |
| 139 | $this->assertObjectHasAttribute( 'version', $data, $msg ); |
| 140 | $this->assertEquals( '1.0', $data->version, $msg ); |
| 141 | |
| 142 | switch ( $data->type ) { |
| 143 | |
| 144 | case 'photo': |
| 145 | |
| 146 | // Check for required response parameters |
| 147 | $this->assertObjectHasAttribute( 'url', $data, $msg ); |
| 148 | $this->assertObjectHasAttribute( 'width', $data, $msg ); |
| 149 | $this->assertObjectHasAttribute( 'height', $data, $msg ); |
| 150 | |
| 151 | // Validate response parameters |
| 152 | $this->assertNotEmpty( $data->url, $msg ); |
| 153 | $this->assertInternalType( 'string', $data->url, $msg ); |
| 154 | |
| 155 | break; |
| 156 | |
| 157 | case 'video': |
| 158 | |
| 159 | // Check for required response parameters |
| 160 | $this->assertObjectHasAttribute( 'html', $data, $msg ); |
| 161 | $this->assertObjectHasAttribute( 'width', $data, $msg ); |
| 162 | $this->assertObjectHasAttribute( 'height', $data, $msg ); |
| 163 | |
| 164 | // Validate response parameters |
| 165 | $this->assertNotEmpty( $data->html, $msg ); |
| 166 | $this->assertInternalType( 'string', $data->html, $msg ); |
| 167 | |
| 168 | // Test for invalid data in response parameters |
| 169 | $this->assertNotContains( '_INVALID_', $data->html, $msg ); |
| 170 | |
| 171 | break; |
| 172 | |
| 173 | case 'rich': |
| 174 | |
| 175 | // Check for required response parameters |
| 176 | $this->assertObjectHasAttribute( 'html', $data, $msg ); |
| 177 | $this->assertObjectHasAttribute( 'width', $data, $msg ); |
| 178 | $this->assertObjectHasAttribute( 'height', $data, $msg ); |
| 179 | |
| 180 | // Validate response parameters |
| 181 | $this->assertNotEmpty( $data->html, $msg ); |
| 182 | $this->assertInternalType( 'string', $data->html, $msg ); |
| 183 | |
| 184 | // Test for invalid data in response parameters |
| 185 | $this->assertNotContains( '_INVALID_', $data->html, $msg ); |
| 186 | |
| 187 | break; |
| 188 | |
| 189 | case 'link': |
| 190 | |
| 191 | // Check for required response parameters |
| 192 | $this->assertObjectHasAttribute( 'title', $data, $msg ); |
| 193 | |
| 194 | // Validate response parameters |
| 195 | $this->assertNotEmpty( $data->title, $msg ); |
| 196 | $this->assertInternalType( 'string', $data->title, $msg ); |
| 197 | |
| 198 | break; |
| 199 | |
| 200 | default: |
| 201 | |
| 202 | $this->fail( sprintf( "Invalid value for the 'type' response parameter\n%s", $msg ) ); |
| 203 | |
| 204 | break; |
| 205 | |
| 206 | } |
| 207 | |
| 208 | if ( isset( $data->thumbnail_width ) ) { |
| 209 | |
| 210 | // Validate response parameter |
| 211 | $this->assertTrue( is_numeric( $data->thumbnail_width ), $msg ); |
| 212 | $this->assertLessThanOrEqual( intval( $query_vars['maxwidth'] ), $data->thumbnail_width, $msg ); |
| 213 | |
| 214 | } |
| 215 | |
| 216 | if ( isset( $data->thumbnail_height ) ) { |
| 217 | |
| 218 | // Validate response parameter |
| 219 | $this->assertTrue( is_numeric( $data->thumbnail_height ), $msg ); |
| 220 | $this->assertLessThanOrEqual( intval( $query_vars['maxheight'] ), $data->thumbnail_height, $msg ); |
| 221 | |
| 222 | } |
| 223 | |
| 224 | if ( isset( $data->width ) ) { |
| 225 | |
| 226 | // Validate response parameter |
| 227 | $this->assertNotEmpty( $data->width, $msg ); |
| 228 | $this->assertTrue( is_numeric( $data->width ), $msg ); |
| 229 | $this->assertLessThanOrEqual( intval( $query_vars['maxwidth'] ), $data->width, $msg ); |
| 230 | |
| 231 | } |
| 232 | |
| 233 | if ( isset( $data->height ) ) { |
| 234 | |
| 235 | // Validate response parameter |
| 236 | $this->assertNotEmpty( $data->height, $msg ); |
| 237 | $this->assertTrue( is_numeric( $data->height ), $msg ); |
| 238 | $this->assertLessThanOrEqual( intval( $query_vars['maxheight'] ), $data->height, $msg ); |
| 239 | |
| 240 | } |
| 241 | |
| 242 | } |
| 243 | |
| 244 | } |
| 245 | |
| 246 | $this->teardown_http_hooks(); |
| 247 | |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Test the tests |
| 252 | * |
| 253 | * @ticket 32360 |
| 254 | */ |
| 255 | public function testOembedTestsCoverAllProviders() { |
| 256 | |
| 257 | $tests = wp_list_pluck( $this->oEmbedProviderData(), 0 ); |
| 258 | $providers = array_keys( $this->oembed->providers ); |
| 259 | $missing = array_diff( $providers, $tests ); |
| 260 | |
| 261 | $this->assertEmpty( $missing, sprintf( "These oEmbed providers are not tested:\n- %s", implode( "\n- ", $missing ) ) ); |
| 262 | |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Test the tests |
| 267 | * |
| 268 | * @ticket 32360 |
| 269 | * |
| 270 | */ |
| 271 | public function testOembedTestsAreAllUseful() { |
| 272 | |
| 273 | $tests = wp_list_pluck( $this->oEmbedProviderData(), 0 ); |
| 274 | $providers = array_keys( $this->oembed->providers ); |
| 275 | $useless = array_diff( $tests, $providers ); |
| 276 | |
| 277 | $this->assertEmpty( $useless, sprintf( "These tests do not cover any oEmbed provider:\n- %s", implode( "\n- ", $useless ) ) ); |
| 278 | |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Data provider for our oEmbed tests |
| 283 | * |
| 284 | * @return array |
| 285 | */ |
| 286 | public function oEmbedProviderData() { |
| 287 | return array( |
| 288 | array( |
| 289 | '#http://((m|www)\.)?youtube\.com/watch.*#i', |
| 290 | array( |
| 291 | 'http://youtube.com/watch?v=zdtD19tXX30', |
| 292 | 'http://m.youtube.com/watch?v=QkP_rOCBrpY', |
| 293 | ), |
| 294 | ), |
| 295 | array( |
| 296 | '#https://((m|www)\.)?youtube\.com/watch.*#i', |
| 297 | array( |
| 298 | 'https://www.youtube.com/watch?v=bDRQRdFaFEo', |
| 299 | 'https://m.youtube.com/watch?v=yfUflij74P4', |
| 300 | ), |
| 301 | ), |
| 302 | array( |
| 303 | '#http://((m|www)\.)?youtube\.com/playlist.*#i', |
| 304 | array( |
| 305 | 'http://www.youtube.com/playlist?list=PLC7D2959C96B8D27B', |
| 306 | 'http://m.youtube.com/playlist?list=PLEC422D53B7588DC7', |
| 307 | ), |
| 308 | ), |
| 309 | array( |
| 310 | '#https://((m|www)\.)?youtube\.com/playlist.*#i', |
| 311 | array( |
| 312 | 'https://youtube.com/playlist?list=PL93B9F6B77FBB0160', |
| 313 | 'https://m.youtube.com/playlist?list=PL1AC02C68F976A10F', |
| 314 | ), |
| 315 | ), |
| 316 | array( |
| 317 | '#http://youtu\.be/.*#i', |
| 318 | array( |
| 319 | 'http://youtu.be/nfWlot6h_JM?list=PLirAqAtl_h2r5g8xGajEwdXd3x1sZh8hC', |
| 320 | ), |
| 321 | ), |
| 322 | array( |
| 323 | '#https://youtu\.be/.*#i', |
| 324 | array( |
| 325 | 'https://youtu.be/U8SYRUYfs_I', |
| 326 | ), |
| 327 | ), |
| 328 | array( |
| 329 | '#https?://(.+\.)?vimeo\.com/.*#i', |
| 330 | array( |
| 331 | 'https://vimeo.com/12339198', |
| 332 | ), |
| 333 | ), |
| 334 | array( |
| 335 | '#https?://(www\.)?dailymotion\.com/.*#i', |
| 336 | array( |
| 337 | 'http://www.dailymotion.com/video/x27bwvb_how-to-wake-up-better_news', |
| 338 | ), |
| 339 | ), |
| 340 | array( |
| 341 | 'http://dai.ly/*', |
| 342 | array( |
| 343 | 'http://dai.ly/x33exze', |
| 344 | ), |
| 345 | ), |
| 346 | array( |
| 347 | '#https?://(www\.)?flickr\.com/.*#i', |
| 348 | array( |
| 349 | 'http://www.flickr.com/photos/bon/14004280667/', |
| 350 | ), |
| 351 | ), |
| 352 | array( |
| 353 | '#https?://flic\.kr/.*#i', |
| 354 | array( |
| 355 | 'https://flic.kr/p/6BFrbQ', |
| 356 | ), |
| 357 | ), |
| 358 | array( |
| 359 | '#https?://(.+\.)?smugmug\.com/.*#i', |
| 360 | array( |
| 361 | 'https://fotoeffects.smugmug.com/Daily-shots-for-the-dailies/Dailies/6928550_9gMRmv/476011624_WhGWpts#!i=476011624&k=WhGWpts', |
| 362 | ), |
| 363 | ), |
| 364 | array( |
| 365 | '#https?://(www\.)?hulu\.com/watch/.*#i', |
| 366 | array( |
| 367 | 'http://www.hulu.com/watch/807443', |
| 368 | ), |
| 369 | ), |
| 370 | array( |
| 371 | 'http://i*.photobucket.com/albums/*', |
| 372 | array( |
| 373 | 'http://i415.photobucket.com/albums/pp236/Keefers_/Keffers%20Animals/funny-cats-a10.jpg', |
| 374 | ), |
| 375 | ), |
| 376 | array( |
| 377 | 'http://gi*.photobucket.com/groups/*', |
| 378 | array( |
| 379 | // ?? |
| 380 | ), |
| 381 | ), |
| 382 | array( |
| 383 | '#https?://(www\.)?scribd\.com/doc/.*#i', |
| 384 | array( |
| 385 | 'http://www.scribd.com/doc/110799637/Synthesis-of-Knowledge-Effects-of-Fire-and-Thinning-Treatments-on-Understory-Vegetation-in-Dry-U-S-Forests', |
| 386 | ), |
| 387 | ), |
| 388 | array( |
| 389 | '#https?://wordpress.tv/.*#i', |
| 390 | array( |
| 391 | 'http://wordpress.tv/2015/08/18/billie/', |
| 392 | ), |
| 393 | ), |
| 394 | array( |
| 395 | '#https?://(.+\.)?polldaddy\.com/.*#i', |
| 396 | array( |
| 397 | 'https://camikaos.polldaddy.com/s/2015-wordpress-community-summit-dates', |
| 398 | ), |
| 399 | ), |
| 400 | array( |
| 401 | '#https?://poll\.fm/.*#i', |
| 402 | array( |
| 403 | // ?? |
| 404 | ), |
| 405 | ), |
| 406 | array( |
| 407 | '#https?://(www\.)?funnyordie\.com/videos/.*#i', |
| 408 | array( |
| 409 | 'http://www.funnyordie.com/videos/e5ef40bf2a/cute-overload', |
| 410 | ), |
| 411 | ), |
| 412 | array( |
| 413 | '#https?://(www\.)?twitter\.com/.+?/status(es)?/.*#i', |
| 414 | array( |
| 415 | 'https://twitter.com/WordPress/status/633718182335922177', |
| 416 | ), |
| 417 | ), |
| 418 | array( |
| 419 | '#https?://vine.co/v/.*#i', |
| 420 | array( |
| 421 | 'https://vine.co/v/OjiLun5LuQ6', |
| 422 | ), |
| 423 | ), |
| 424 | array( |
| 425 | '#https?://(www\.)?soundcloud\.com/.*#i', |
| 426 | array( |
| 427 | 'https://soundcloud.com/steveaoki/kid-cudi-pursuit-of-happiness', |
| 428 | ), |
| 429 | ), |
| 430 | array( |
| 431 | '#https?://(.+?\.)?slideshare\.net/.*#i', |
| 432 | array( |
| 433 | 'http://www.slideshare.net/haraldf/business-quotes-for-2011', |
| 434 | ), |
| 435 | ), |
| 436 | array( |
| 437 | '#https?://instagr(\.am|am\.com)/p/.*#i', |
| 438 | array( |
| 439 | 'https://instagram.com/p/68WqXbTcfl/', |
| 440 | 'http://instagr.am/p/MRM3HQy6kh/', |
| 441 | ), |
| 442 | ), |
| 443 | array( |
| 444 | '#https?://(www\.)?rdio\.com/.*#i', |
| 445 | array( |
| 446 | 'https://www.rdio.com/artist/Wolf_Alice/album/My_Love_Is_Cool_1/', |
| 447 | ), |
| 448 | ), |
| 449 | array( |
| 450 | '#https?://rd\.io/x/.*#i', |
| 451 | array( |
| 452 | 'http://rd.io/x/Rl4F5xw-bsh5/', |
| 453 | ), |
| 454 | ), |
| 455 | array( |
| 456 | '#https?://(open|play)\.spotify\.com/.*#i', |
| 457 | array( |
| 458 | 'https://open.spotify.com/track/2i1KmyEXN3pNLwdxAWSGcg', |
| 459 | ), |
| 460 | ), |
| 461 | array( |
| 462 | '#https?://(.+\.)?imgur\.com/.*#i', |
| 463 | array( |
| 464 | 'https://imgur.com/a/WdJim', |
| 465 | 'http://i.imgur.com/mbOPX2L.png', |
| 466 | ), |
| 467 | ), |
| 468 | array( |
| 469 | '#https?://(www\.)?meetu(\.ps|p\.com)/.*#i', |
| 470 | array( |
| 471 | 'https://www.meetup.com/WordPress-Amsterdam/events/224346396/', |
| 472 | 'http://meetu.ps/2L533w', |
| 473 | ), |
| 474 | ), |
| 475 | array( |
| 476 | '#https?://(www\.)?issuu\.com/.+/docs/.+#i', |
| 477 | array( |
| 478 | 'http://issuu.com/vmagazine/docs/v87', |
| 479 | ), |
| 480 | ), |
| 481 | array( |
| 482 | '#https?://(www\.)?collegehumor\.com/video/.*#i', |
| 483 | array( |
| 484 | 'http://www.collegehumor.com/video/2862877/jake-and-amir-math', |
| 485 | ), |
| 486 | ), |
| 487 | array( |
| 488 | '#https?://(www\.)?mixcloud\.com/.*#i', |
| 489 | array( |
| 490 | 'http://www.mixcloud.com/8_8s/disclosurefriends/', |
| 491 | ), |
| 492 | ), |
| 493 | array( |
| 494 | '#https?://(www\.|embed\.)?ted\.com/talks/.*#i', |
| 495 | array( |
| 496 | 'http://www.ted.com/talks/rodney_mullen_pop_an_ollie_and_innovate', |
| 497 | ), |
| 498 | ), |
| 499 | array( |
| 500 | '#https?://(www\.)?(animoto|video214)\.com/play/.*#i', |
| 501 | array( |
| 502 | 'https://animoto.com/play/MlRRgXHhoT8gOZyHanM6TA', |
| 503 | ), |
| 504 | ), |
| 505 | array( |
| 506 | '#https?://(.+)\.tumblr\.com/post/.*#i', |
| 507 | array( |
| 508 | 'http://yahoo.tumblr.com/post/50902111638/tumblr-yahoo', |
| 509 | ), |
| 510 | ), |
| 511 | array( |
| 512 | '#https?://(www\.)?kickstarter\.com/projects/.*#i', |
| 513 | array( |
| 514 | 'https://www.kickstarter.com/projects/zackdangerbrown/potato-salad', |
| 515 | ), |
| 516 | ), |
| 517 | array( |
| 518 | '#https?://kck\.st/.*#i', |
| 519 | array( |
| 520 | 'http://kck.st/1ukxHcx', |
| 521 | ), |
| 522 | ), |
| 523 | array( |
| 524 | '#https?://cloudup\.com/.*#i', |
| 525 | array( |
| 526 | 'https://cloudup.com/cWX2Bi5DmfJ', |
| 527 | ), |
| 528 | ), |
| 529 | array( |
| 530 | '#https?://(www\.)?reverbnation\.com/.*#i', |
| 531 | array( |
| 532 | 'http://www.reverbnation.com/coralbones/song/18668300-rising-sand', |
| 533 | 'https://www.reverbnation.com/coralbones', |
| 534 | ), |
| 535 | ), |
| 536 | ); |
| 537 | } |
| 538 | |
| 539 | protected function setup_http_hooks() { |
| 540 | add_action( 'http_api_debug', array( $this, 'action_http_api_debug' ), 99, 5 ); |
| 541 | } |
| 542 | |
| 543 | protected function teardown_http_hooks() { |
| 544 | remove_action( 'http_api_debug', array( $this, 'action_http_api_debug' ), 99 ); |
| 545 | $this->http_response = null; |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * Debugging action for the HTTP API response. |
| 550 | * |
| 551 | * @param array|WP_Error $response The HTTP response. |
| 552 | * @param string $action The debug action. |
| 553 | * @param string $class The HTTP transport class name. |
| 554 | * @param array $args HTTP request arguments. |
| 555 | * @param string $url The request URL. |
| 556 | */ |
| 557 | public function action_http_api_debug( $response, $action, $class, $args, $url ) { |
| 558 | |
| 559 | if ( 'response' !== $action ) { |
| 560 | return; |
| 561 | } |
| 562 | |
| 563 | $this->http_response = compact( 'response', 'args', 'url' ); |
| 564 | |
| 565 | } |
| 566 | |
| 567 | } |