| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Test get_avatar_url() and get_avatar() |
| 5 | * |
| 6 | * @group avatar |
| 7 | */ |
| 8 | class Tests_Avatar extends WP_UnitTestCase { |
| 9 | const USER_EMAIL = 'UsEr@exaMple.oRg'; |
| 10 | const DELETED_USER_EMAIL = 'dOdO@ExampLe.oRg'; |
| 11 | const GUEST_EMAIL = 'GuesT@eXample.oRg'; |
| 12 | |
| 13 | const USER_EMAIL_MD5 = '572c3489ea700045927076136a969e27'; // 2 == hexdec( "5" ) % 3 |
| 14 | const DELETED_USER_EMAIL_MD5 = '7736907f09ecae4d1400c18f499a61f8'; // 1 == hexdec( "7" ) % 3 |
| 15 | const GUEST_EMAIL_MD5 = 'cde48bd0bea0bbc6bc6bb02a461d946e'; // 0 == hexdec( "c" ) % 3 |
| 16 | |
| 17 | const UNKNOWN_COMMENT_TYPE = 'unknown'; |
| 18 | const ALLOWED_COMMENT_TYPE = 'allowed'; |
| 19 | |
| 20 | var $user_id = 0; |
| 21 | var $deleted_user_id = 0; |
| 22 | |
| 23 | var $comment_by_user = 0; |
| 24 | var $comment_by_deleted_user = 0; |
| 25 | var $comment_by_guest = 0; |
| 26 | var $unknown_comment_type = 0; |
| 27 | var $allowed_comment_type = 0; |
| 28 | |
| 29 | var $post_by_user = 0; |
| 30 | |
| 31 | var $img_structure; |
| 32 | |
| 33 | function setUp() { |
| 34 | parent::setUp(); |
| 35 | |
| 36 | $this->user_id = $this->factory->user->create( array( |
| 37 | 'user_email' => self::USER_EMAIL, |
| 38 | ) ); |
| 39 | |
| 40 | $this->deleted_user_id = $this->factory->user->create( array( |
| 41 | 'user_email' => self::DELETED_USER_EMAIL, |
| 42 | ) ); |
| 43 | |
| 44 | $this->comment_by_user = $this->factory->comment->create( array( |
| 45 | 'user_id' => $this->user_id, |
| 46 | ) ); |
| 47 | |
| 48 | $this->comment_by_deleted_user = $this->factory->comment->create( array( |
| 49 | 'user_id' => $this->deleted_user_id, |
| 50 | 'comment_author_email' => self::DELETED_USER_EMAIL, |
| 51 | ) ); |
| 52 | |
| 53 | $this->comment_by_guest = $this->factory->comment->create( array( |
| 54 | 'user_id' => 0, |
| 55 | 'comment_author_email' => self::GUEST_EMAIL, |
| 56 | ) ); |
| 57 | |
| 58 | $this->comment_by_noone = $this->factory->comment->create( array( |
| 59 | 'user_id' => 0, |
| 60 | 'comment_author_email' => '', |
| 61 | ) ); |
| 62 | |
| 63 | $this->unknown_comment_type = $this->factory->comment->create( array( |
| 64 | 'user_id' => $this->user_id, |
| 65 | 'comment_type' => self::UNKNOWN_COMMENT_TYPE, |
| 66 | ) ); |
| 67 | |
| 68 | $this->allowed_comment_type = $this->factory->comment->create( array( |
| 69 | 'user_id' => $this->user_id, |
| 70 | 'comment_type' => self::ALLOWED_COMMENT_TYPE, |
| 71 | ) ); |
| 72 | |
| 73 | $this->post_by_user = $this->factory->post->create( array( |
| 74 | 'post_author' => $this->user_id, |
| 75 | ) ); |
| 76 | |
| 77 | if ( is_multisite() ) { |
| 78 | wpmu_delete_user( $this->deleted_user_id ); |
| 79 | } else { |
| 80 | wp_delete_user( $this->deleted_user_id ); |
| 81 | } |
| 82 | |
| 83 | $dom = new DomDocument; |
| 84 | $dom->loadHTML( '<img />' ); |
| 85 | $this->img_structure = $dom->documentElement->firstChild->firstChild; |
| 86 | foreach ( array( 'src', 'height', 'width', 'class', 'alt' ) as $name ) { |
| 87 | $this->img_structure->setAttribute( $name, 'true' ); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /* Tests */ |
| 92 | |
| 93 | /** |
| 94 | * Make sure our tests are set up correctly. |
| 95 | * |
| 96 | * @dataProvider md5s |
| 97 | */ |
| 98 | function test_md5( $email, $expected_md5 ) { |
| 99 | $this->assertEquals( $expected_md5, md5( strtolower( trim( $email ) ) ) ); |
| 100 | } |
| 101 | |
| 102 | function md5s() { |
| 103 | return array( |
| 104 | // $email, $expected_md5 |
| 105 | array( self::USER_EMAIL , self::USER_EMAIL_MD5 ), |
| 106 | array( self::DELETED_USER_EMAIL, self::DELETED_USER_EMAIL_MD5 ), |
| 107 | array( self::GUEST_EMAIL , self::GUEST_EMAIL_MD5 ), |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | /* Get Avatar By ... */ |
| 112 | /* User ID */ |
| 113 | |
| 114 | function test_by_user_id() { |
| 115 | $avatar_html = get_avatar( $this->user_id ); |
| 116 | |
| 117 | $dom_img = $this->get_image_element( $avatar_html ); |
| 118 | |
| 119 | $this->assertAvatarMatchesHash( self::USER_EMAIL_MD5, $dom_img ); |
| 120 | } |
| 121 | |
| 122 | function test_by_user_id_deleted() { |
| 123 | $avatar_html = get_avatar( $this->deletede_user_id ); |
| 124 | |
| 125 | $dom_img = $this->get_image_element( $avatar_html ); |
| 126 | |
| 127 | $this->assertAvatarMatchesHash( '', $dom_img ); // User ID does not exist, so no email to hash |
| 128 | |
| 129 | $this->assertAvatarHTMLHasClasses( 'avatar-default', $dom_img ); |
| 130 | } |
| 131 | |
| 132 | function test_by_user_id_bad() { |
| 133 | $avatar_html = get_avatar( 0 ); |
| 134 | |
| 135 | $dom_img = $this->get_image_element( $avatar_html ); |
| 136 | |
| 137 | $this->assertAvatarMatchesHash( '', $dom_img ); |
| 138 | |
| 139 | $this->assertAvatarHTMLHasClasses( 'avatar-default', $dom_img ); |
| 140 | } |
| 141 | |
| 142 | function test_by_user_id_negative() { |
| 143 | $avatar_html = get_avatar( -1 * $this->user_id ); |
| 144 | |
| 145 | $dom_img = $this->get_image_element( $avatar_html ); |
| 146 | |
| 147 | $this->assertAvatarMatchesHash( self::USER_EMAIL_MD5, $dom_img ); |
| 148 | } |
| 149 | |
| 150 | /* Email Address or Hash */ |
| 151 | |
| 152 | function emails() { |
| 153 | return array( |
| 154 | // $email, $expected_hash |
| 155 | array( self::USER_EMAIL , self::USER_EMAIL_MD5 ), |
| 156 | array( ' ' . self::USER_EMAIL . "\t", self::USER_EMAIL_MD5 ), // whitespace should be stripped before MD5ing |
| 157 | array( |
| 158 | sprintf( '%s@md5.gravatar.com', self::USER_EMAIL_MD5 ), // this is how you generate an avatar for a specific MD5 hash |
| 159 | self::USER_EMAIL_MD5 |
| 160 | ), |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @dataProvider emails |
| 166 | */ |
| 167 | function test_by_email( $email, $expected_hash ) { |
| 168 | $avatar_html = get_avatar( $email ); |
| 169 | |
| 170 | $dom_img = $this->get_image_element( $avatar_html ); |
| 171 | |
| 172 | $this->assertAvatarMatchesHash( $expected_hash, $dom_img ); |
| 173 | } |
| 174 | |
| 175 | /* User */ |
| 176 | |
| 177 | function test_by_user() { |
| 178 | $user = get_user_by( 'id', $this->user_id ); |
| 179 | |
| 180 | $avatar_html = get_avatar( $user ); |
| 181 | |
| 182 | $dom_img = $this->get_image_element( $avatar_html ); |
| 183 | |
| 184 | $this->assertAvatarMatchesHash( self::USER_EMAIL_MD5, $dom_img ); |
| 185 | } |
| 186 | |
| 187 | /* Comment */ |
| 188 | |
| 189 | function test_by_comment_by_user() { |
| 190 | $comment = get_comment( $this->comment_by_user ); |
| 191 | |
| 192 | $avatar_html = get_avatar( $comment ); |
| 193 | |
| 194 | $dom_img = $this->get_image_element( $avatar_html ); |
| 195 | |
| 196 | $this->assertAvatarMatchesHash( self::USER_EMAIL_MD5, $dom_img ); |
| 197 | } |
| 198 | |
| 199 | function test_by_comment_by_deleted_user() { |
| 200 | $comment = get_comment( $this->comment_by_deleted_user ); |
| 201 | |
| 202 | $avatar_html = get_avatar( $comment ); |
| 203 | |
| 204 | $dom_img = $this->get_image_element( $avatar_html ); |
| 205 | |
| 206 | $this->assertAvatarMatchesHash( self::DELETED_USER_EMAIL_MD5, $dom_img ); // Comment object has comment_author_email, so should fallback to that after user_id not found |
| 207 | } |
| 208 | |
| 209 | function test_by_comment_by_guest() { |
| 210 | $comment = get_comment( $this->comment_by_guest ); |
| 211 | |
| 212 | $avatar_html = get_avatar( $comment ); |
| 213 | |
| 214 | $dom_img = $this->get_image_element( $avatar_html ); |
| 215 | |
| 216 | $this->assertAvatarMatchesHash( self::GUEST_EMAIL_MD5, $dom_img ); |
| 217 | } |
| 218 | |
| 219 | function test_by_comment_by_noone() { |
| 220 | $comment = get_comment( $this->comment_by_noone ); |
| 221 | |
| 222 | $avatar_html = get_avatar( $comment ); |
| 223 | |
| 224 | $dom_img = $this->get_image_element( $avatar_html ); |
| 225 | |
| 226 | $this->assertAvatarMatchesHash( '', $dom_img ); |
| 227 | } |
| 228 | |
| 229 | function test_by_unknown_comment_type() { |
| 230 | $comment = get_comment( $this->unknown_comment_type ); |
| 231 | |
| 232 | $this->assertEquals( self::UNKNOWN_COMMENT_TYPE, $comment->comment_type ); |
| 233 | |
| 234 | $avatar_html = get_avatar( $comment ); |
| 235 | |
| 236 | $this->assertFalse( $avatar_html ); |
| 237 | } |
| 238 | |
| 239 | function test_by_allowed_comment_type() { |
| 240 | $comment = get_comment( $this->allowed_comment_type ); |
| 241 | |
| 242 | $this->assertEquals( self::ALLOWED_COMMENT_TYPE, $comment->comment_type ); |
| 243 | |
| 244 | add_filter( 'get_avatar_comment_types', array( $this, '_get_avatar_comment_types' ) ); |
| 245 | $avatar_html = get_avatar( $comment ); |
| 246 | remove_filter( 'get_avatar_comment_types', array( $this, '_get_avatar_comment_types' ) ); |
| 247 | |
| 248 | $dom_img = $this->get_image_element( $avatar_html ); |
| 249 | |
| 250 | $this->assertAvatarMatchesHash( self::USER_EMAIL_MD5, $dom_img ); |
| 251 | } |
| 252 | |
| 253 | /* Post */ |
| 254 | |
| 255 | function test_by_post() { |
| 256 | $post = get_post( $this->post_by_user ); |
| 257 | |
| 258 | $avatar_html = get_avatar( $post ); |
| 259 | |
| 260 | $dom_img = $this->get_image_element( $avatar_html ); |
| 261 | |
| 262 | $this->assertAvatarMatchesHash( self::USER_EMAIL_MD5, $dom_img ); |
| 263 | } |
| 264 | |
| 265 | /* Parameters */ |
| 266 | /* Default Values for all parameters */ |
| 267 | function test_default_values() { |
| 268 | $width = $height = 96; // $size = 96 |
| 269 | $d = 'mm'; // $default = '' -> mystery |
| 270 | $f = false; // $force_default = false |
| 271 | $alt = ''; // $alt = '' |
| 272 | $r = 'g'; // $rating = 'G' |
| 273 | |
| 274 | $avatar_html = get_avatar( $this->user_id ); |
| 275 | |
| 276 | // No specifics in input |
| 277 | $dom_img = $this->get_image_element( $avatar_html ); |
| 278 | |
| 279 | // Test output matches expected defaults |
| 280 | |
| 281 | $this->assertAvatarHTMLAttributesMatch( compact( 'width', 'height', 'alt' ), $dom_img ); |
| 282 | |
| 283 | $this->assertAvatarURLQueryParametersMatch( compact( 'd', 'f', 'r' ), $dom_img ); |
| 284 | |
| 285 | // Test default scheme |
| 286 | $this->assertStringStartsWith( 'http://', $dom_img->getAttribute( 'src' ) ); |
| 287 | } |
| 288 | |
| 289 | /* Size */ |
| 290 | |
| 291 | function sizes() { |
| 292 | return array( |
| 293 | // $size, $expected_size |
| 294 | array( 255, 255 ), |
| 295 | array( -255, 255 ), |
| 296 | array( 0, 96 ), |
| 297 | array( 'bad', 96 ), |
| 298 | ); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * @dataProvider sizes |
| 303 | */ |
| 304 | function test_size_parameter( $size, $expected_size ) { |
| 305 | $width = $height = $expected_size; |
| 306 | |
| 307 | $avatar_html = get_avatar( $this->user_id, $size ); |
| 308 | |
| 309 | $dom_img = $this->get_image_element( $avatar_html ); |
| 310 | |
| 311 | $this->assertAvatarHTMLAttributesMatch( compact( 'width', 'height' ), $dom_img ); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * @dataProvider sizes |
| 316 | */ |
| 317 | function test_size_arg( $size, $expected_size ) { |
| 318 | $width = $height = $expected_size; |
| 319 | |
| 320 | $avatar_html = get_avatar( $this->user_id, compact( 'size' ) ); |
| 321 | |
| 322 | $dom_img = $this->get_image_element( $avatar_html ); |
| 323 | |
| 324 | $this->assertAvatarHTMLAttributesMatch( compact( 'width', 'height' ), $dom_img ); |
| 325 | } |
| 326 | |
| 327 | /* Default */ |
| 328 | |
| 329 | function defaults() { |
| 330 | return array( |
| 331 | // $default, $expected_d |
| 332 | array( 'mm' , 'mm' ), |
| 333 | array( 'mystery' , 'mm' ), |
| 334 | array( 'mysteryman' , 'mm' ), |
| 335 | array( '404' , '404' ), |
| 336 | array( 404, '404' ), |
| 337 | array( 'retro' , 'retro' ), |
| 338 | array( 'monsterid' , 'monsterid' ), |
| 339 | array( 'wavatar' , 'wavatar' ), |
| 340 | array( 'identicon' , 'identicon' ), |
| 341 | array( 'blank' , 'blank' ), |
| 342 | array( 'gravatar_default', false ), // There should be no d query string parameter |
| 343 | array( |
| 344 | 'http://example.org/image.jpg?foo=bar%2F&one=two#fragment', |
| 345 | 'http://example.org/image.jpg?foo=bar%2F&one=two#fragment', |
| 346 | ), |
| 347 | ); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * @dataProvider defaults |
| 352 | */ |
| 353 | function test_default_parameter( $default, $expected_d ) { |
| 354 | $d = $expected_d; |
| 355 | $avatar_html = get_avatar( $this->user_id, 96, $default ); |
| 356 | |
| 357 | $dom_img = $this->get_image_element( $avatar_html ); |
| 358 | |
| 359 | $this->assertAvatarURLQueryParametersMatch( compact( 'd' ), $dom_img ); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * @dataProvider defaults |
| 364 | */ |
| 365 | function test_default_arg( $default, $expected_d ) { |
| 366 | $d = $expected_d; |
| 367 | $avatar_html = get_avatar( $this->user_id, compact( 'default' ) ); |
| 368 | |
| 369 | $dom_img = $this->get_image_element( $avatar_html ); |
| 370 | |
| 371 | $this->assertAvatarURLQueryParametersMatch( compact( 'd' ), $dom_img ); |
| 372 | } |
| 373 | |
| 374 | /* Alt */ |
| 375 | |
| 376 | function alts() { |
| 377 | return array( |
| 378 | // $alt |
| 379 | array( 'a\'b"c<d>h&i%j\\k' ), |
| 380 | ); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * @dataProvider alts |
| 385 | */ |
| 386 | function test_alt_parameter( $alt ) { |
| 387 | $avatar_html = get_avatar( $this->user_id, 96, '', $alt ); |
| 388 | |
| 389 | $dom_img = $this->get_image_element( $avatar_html ); |
| 390 | |
| 391 | $this->assertAvatarHTMLAttributesMatch( compact( 'alt' ), $dom_img ); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * @dataProvider alts |
| 396 | */ |
| 397 | function test_alt_arg( $alt ) { |
| 398 | $avatar_html = get_avatar( $this->user_id, compact( 'alt' ) ); |
| 399 | |
| 400 | $dom_img = $this->get_image_element( $avatar_html ); |
| 401 | |
| 402 | $this->assertAvatarHTMLAttributesMatch( compact( 'alt' ), $dom_img ); |
| 403 | } |
| 404 | |
| 405 | /* Extraneous Parameter */ |
| 406 | |
| 407 | // The $size, $default, and $alt parameter should still work even if there are extraneous parameters tacked on the end |
| 408 | function test_extraneous_parameter() { |
| 409 | $s = 255; |
| 410 | $d = '404'; |
| 411 | $alt = 'foo'; |
| 412 | $avatar_html = get_avatar( $this->user_id, $s, $d, $alt, 'extraneous' ); |
| 413 | |
| 414 | $dom_img = $this->get_image_element( $avatar_html ); |
| 415 | |
| 416 | $this->assertAvatarURLQueryParametersMatch( compact( 's', 'd' ), $dom_img ); |
| 417 | |
| 418 | $this->assertAvatarHTMLAttributesMatch( compact( 'alt' ), $dom_img ); |
| 419 | } |
| 420 | |
| 421 | /* Force Default */ |
| 422 | |
| 423 | function test_force_default() { |
| 424 | $force_default = true; |
| 425 | $f = 'y'; |
| 426 | $avatar_html = get_avatar( $this->user_id, compact( 'force_default' ) ); |
| 427 | |
| 428 | $dom_img = $this->get_image_element( $avatar_html ); |
| 429 | |
| 430 | $this->assertAvatarURLQueryParametersMatch( compact( 'f' ), $dom_img ); |
| 431 | |
| 432 | $this->assertAvatarHTMLHasClasses( 'avatar-default', $dom_img ); |
| 433 | } |
| 434 | |
| 435 | /* Rating */ |
| 436 | |
| 437 | function test_rating() { |
| 438 | $rating = 'PG'; |
| 439 | $r = 'pg'; |
| 440 | $avatar_html = get_avatar( $this->user_id, compact( 'rating' ) ); |
| 441 | |
| 442 | $dom_img = $this->get_image_element( $avatar_html ); |
| 443 | |
| 444 | $this->assertAvatarURLQueryParametersMatch( compact( 'r' ), $dom_img ); |
| 445 | } |
| 446 | |
| 447 | /* Scheme */ |
| 448 | |
| 449 | function test_scheme_https() { |
| 450 | $scheme = 'https'; |
| 451 | $avatar_html = get_avatar( $this->user_id, compact( 'scheme' ) ); |
| 452 | |
| 453 | $dom_img = $this->get_image_element( $avatar_html ); |
| 454 | |
| 455 | $this->assertStringStartsWith( 'https://', $dom_img->getAttribute( 'src' ) ); |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * @backupGlobals enabled |
| 460 | */ |
| 461 | function test_scheme_default_over_ssl() { |
| 462 | $_SERVER['HTTPS'] = 'ON'; |
| 463 | |
| 464 | $avatar_html = get_avatar( $this->user_id ); |
| 465 | |
| 466 | $dom_img = $this->get_image_element( $avatar_html ); |
| 467 | |
| 468 | $this->assertStringStartsWith( 'https://', $dom_img->getAttribute( 'src' ) ); |
| 469 | } |
| 470 | |
| 471 | /* Class */ |
| 472 | |
| 473 | function classes() { |
| 474 | return array( |
| 475 | // $class, $expected_classes |
| 476 | array( 'bar foo' , array( 'foo', 'bar' ) ), |
| 477 | array( array( 'foo ', ' bar' ), array( 'foo', 'bar' ) ), |
| 478 | ); |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * @dataProvider classes |
| 483 | */ |
| 484 | function test_class( $class, $expected_classes ) { |
| 485 | $avatar_html = get_avatar( $this->user_id, compact( 'class' ) ); |
| 486 | |
| 487 | $dom_img = $this->get_image_element( $avatar_html ); |
| 488 | |
| 489 | $this->assertAvatarHTMLHasClasses( $expected_classes, $dom_img ); |
| 490 | } |
| 491 | |
| 492 | /* Force Display */ |
| 493 | |
| 494 | function test_force_display() { |
| 495 | update_option( 'show_avatars', false ); // Turn off avatars |
| 496 | |
| 497 | $avatar_html = get_avatar( $this->user_id, array( 'force_display' => true ) ); |
| 498 | |
| 499 | $this->get_image_element( $avatar_html ); |
| 500 | } |
| 501 | |
| 502 | /* Options */ |
| 503 | |
| 504 | function test_option_show_avatars() { |
| 505 | update_option( 'show_avatars', false ); |
| 506 | |
| 507 | $avatar_html = get_avatar( $this->user_id ); |
| 508 | |
| 509 | $this->assertFalse( $avatar_html ); |
| 510 | } |
| 511 | |
| 512 | function test_option_avatar_default() { |
| 513 | update_option( 'avatar_default', '404' ); |
| 514 | |
| 515 | $default = $d = '404'; |
| 516 | $avatar_html = get_avatar( $this->user_id, 96, $default ); |
| 517 | |
| 518 | $dom_img = $this->get_image_element( $avatar_html ); |
| 519 | |
| 520 | $this->assertAvatarURLQueryParametersMatch( compact( 'd' ), $dom_img ); |
| 521 | } |
| 522 | |
| 523 | function test_option_avatar_rating_false() { |
| 524 | update_option( 'avatar_rating', false ); |
| 525 | |
| 526 | $avatar_html = get_avatar( $this->user_id ); |
| 527 | |
| 528 | $dom_img = $this->get_image_element( $avatar_html ); |
| 529 | |
| 530 | $this->assertAvatarURLQueryParametersMatch( array( 'r' => false ), $dom_img ); |
| 531 | } |
| 532 | |
| 533 | function test_option_avatar_rating_PG() { |
| 534 | update_option( 'avatar_rating', 'PG' ); |
| 535 | |
| 536 | $avatar_html = get_avatar( $this->user_id ); |
| 537 | |
| 538 | $dom_img = $this->get_image_element( $avatar_html ); |
| 539 | |
| 540 | $this->assertAvatarURLQueryParametersMatch( array( 'r' => 'pg' ), $dom_img ); |
| 541 | } |
| 542 | |
| 543 | /* Filters */ |
| 544 | |
| 545 | /* pre_get_avatar_url, get_avatar_url */ |
| 546 | |
| 547 | // Used for both get_avatar and get_avatar_url filters |
| 548 | function get_avatar_filters() { |
| 549 | return array( |
| 550 | array( 'pre_get_avatar' ), |
| 551 | array( 'get_avatar' ), |
| 552 | ); |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * @requires function get_avatar_url |
| 557 | * @dataProvider get_avatar_filters |
| 558 | */ |
| 559 | function test_get_avatar_url_filters( $filter ) { |
| 560 | add_filter( "{$filter}_url", '__return_zero' ); // pre_get_avatar_url, get_avatar_url |
| 561 | |
| 562 | $avatar_url = get_avatar_url( $this->user_id ); |
| 563 | |
| 564 | remove_filter( "{$filter}_url", '__return_zero' ); |
| 565 | |
| 566 | $this->assertSame( 0, $avatar_url ); |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * @dataProvider get_avatar_filters |
| 571 | */ |
| 572 | function test_get_avatar_url_filters_and_process_args( $filter ) { |
| 573 | $width = $height = 255; |
| 574 | |
| 575 | add_filter( "{$filter}_url", array( $this, '_get_avatar_url_and_process_args' ), 10, 3 ); // pre_get_avatar_url, get_avatar_url |
| 576 | |
| 577 | $avatar_html = get_avatar( $this->user_id ); |
| 578 | |
| 579 | remove_filter( "{$filter}_url", array( $this, '_get_avatar_url_and_process_args' ), 10, 3 ); |
| 580 | |
| 581 | $dom_img = $this->get_image_element( $avatar_html ); |
| 582 | |
| 583 | $this->assertAvatarHTMLAttributesMatch( compact( 'width', 'height' ), $dom_img ); |
| 584 | |
| 585 | $this->assertAvatarMatchesHash( 'zzz', $dom_img ); |
| 586 | |
| 587 | } |
| 588 | |
| 589 | /* pre_get_avatar, get_avatar */ |
| 590 | |
| 591 | /** |
| 592 | * @dataProvider get_avatar_filters |
| 593 | */ |
| 594 | function test_get_avatar_filters( $filter ) { |
| 595 | add_filter( $filter, '__return_zero' ); // pre_get_avatar, get_avatar |
| 596 | |
| 597 | $avatar_html = get_avatar( $this->user_id ); |
| 598 | |
| 599 | remove_filter( $filter, '__return_zero' ); |
| 600 | |
| 601 | $this->assertSame( 0, $avatar_html ); |
| 602 | } |
| 603 | |
| 604 | /* Domain Sharding */ |
| 605 | |
| 606 | function domain_shards() { |
| 607 | return array( |
| 608 | array( self::GUEST_EMAIL , 0 ), |
| 609 | array( self::DELETED_USER_EMAIL, 1 ), |
| 610 | array( self::USER_EMAIL , 2 ), |
| 611 | ); |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * @dataProvider domain_shards |
| 616 | */ |
| 617 | function test_domain_sharding( $email, $shard ) { |
| 618 | $avatar_html = get_avatar( $email ); |
| 619 | |
| 620 | $dom_img = $this->get_image_element( $avatar_html ); |
| 621 | |
| 622 | $this->assertStringStartsWith( "http://{$shard}.", $dom_img->getAttribute( 'src' ) ); |
| 623 | } |
| 624 | |
| 625 | /* Helpers */ |
| 626 | |
| 627 | /** |
| 628 | * Converts IMG tag HTML to a DOM_Element and asserts that the markup matches the expected structure. |
| 629 | * |
| 630 | * @param string $html_string IMG tag |
| 631 | * |
| 632 | * @return DOM_Element IMG element |
| 633 | */ |
| 634 | private function get_image_element( $html_string ) { |
| 635 | $dom = new DOMDocument; |
| 636 | $success = $dom->loadHTML( $html_string ); |
| 637 | $this->assertTrue( $success, 'Invalid HTML' ); |
| 638 | |
| 639 | $this->assertTrue( $dom->documentElement->firstChild->firstChild->hasAttribute( 'src' ) ); |
| 640 | |
| 641 | $this->assertEqualXMLStructure( $this->img_structure, $dom->documentElement->firstChild->firstChild, true, 'HTML does not match expected value' ); |
| 642 | |
| 643 | return $dom->documentElement->firstChild->firstChild; |
| 644 | } |
| 645 | |
| 646 | // Filter for ::test_by_allowed_comment_type() |
| 647 | function _get_avatar_comment_types( $comment_types ) { |
| 648 | $comment_types[] = self::ALLOWED_COMMENT_TYPE; |
| 649 | |
| 650 | return $comment_types; |
| 651 | } |
| 652 | |
| 653 | // Filter for ::test_get_avatar_url_filters_and_process_args() |
| 654 | function _get_avatar_url_and_process_args( $avatar_url, $id_or_email, &$args ) { |
| 655 | $args['size'] = 255; |
| 656 | |
| 657 | return 'http://0.gravatar.com/avatar/zzz'; |
| 658 | } |
| 659 | |
| 660 | /* Assertions */ |
| 661 | |
| 662 | /** |
| 663 | * @param string $expected_hash |
| 664 | * @param DOM_Element $dom_img |
| 665 | */ |
| 666 | private function assertAvatarMatchesHash( $expected_hash, $dom_img ) { |
| 667 | $src = $dom_img->getAttribute( 'src' ); |
| 668 | |
| 669 | $this->assertRegExp( sprintf( '#/avatar/%s(/|\?|$)#', preg_quote( $expected_hash, '#' ) ), $src, "Hash mismatch: '$src' !~ '$expected_hash'" ); |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * @param array $expected_parameters key/value array of expected query string parameters in the IMG's src URL. To expect a parameter to net be present, pass false as the value. |
| 674 | * @param DOM_Element $dom_img |
| 675 | */ |
| 676 | private function assertAvatarURLQueryParametersMatch( $expected_parameters, $dom_img ) { |
| 677 | $src = $dom_img->getAttribute( 'src' ); |
| 678 | |
| 679 | $query = parse_url( $src, PHP_URL_QUERY ); |
| 680 | wp_parse_str( $query, $query_args ); |
| 681 | |
| 682 | $expected_parameters = wp_parse_args( $expected_parameters ); |
| 683 | |
| 684 | foreach ( $expected_parameters as $key => $value ) { |
| 685 | if ( false === $value ) { |
| 686 | $this->assertArrayNotHasKey( $key, $query_args ); |
| 687 | } else { |
| 688 | $this->assertArrayHasKey( $key, $query_args ); |
| 689 | $this->assertEquals( $value, $query_args[$key] ); |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * @param array $expected_atttributes key/value array of expected HTML attributes on the IMG element. To expect a attribute to be present but not care about the value, pass null as the value. |
| 696 | * @param DOM_Element $dom_img |
| 697 | */ |
| 698 | private function assertAvatarHTMLAttributesMatch( $expected_attributes, $dom_img ) { |
| 699 | foreach ( $expected_attributes as $name => $value ) { |
| 700 | if ( is_null( $value ) ) { |
| 701 | $this->assertTrue( $dom_img->hasAttribute( $name ) ); |
| 702 | } else { |
| 703 | $this->assertEquals( $value, $dom_img->getAttribute( $name ) ); |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | /** |
| 708 | * @param string|array $expected_classes expected classes on the IMG element. |
| 709 | * @param DOM_Element $dom_img |
| 710 | */ |
| 711 | private function assertAvatarHTMLHasClasses( $expected_classes, $dom_img ) { |
| 712 | $expected_classes = preg_split( '/\s+/', join( ' ', (array) $expected_classes ), -1, PREG_SPLIT_NO_EMPTY ); |
| 713 | |
| 714 | $actual_classes = $dom_img->getAttribute( 'class' ); |
| 715 | $actual_classes = preg_split( '/\s+/', join( ' ', (array) $actual_classes ), -1, PREG_SPLIT_NO_EMPTY ); |
| 716 | |
| 717 | foreach ( $expected_classes as $expected_class ) { |
| 718 | $this->assertContains( $expected_class, $actual_classes, "Missing class $expected_class" ); |
| 719 | } |
| 720 | } |
| 721 | } |