Ticket #44033: 44033.3.diff
File 44033.3.diff, 5.6 KB (added by , 7 years ago) |
---|
-
src/wp-includes/link-template.php
diff --git src/wp-includes/link-template.php src/wp-includes/link-template.php index 815c539..f102b60 100644
function get_avatar_url( $id_or_email, $args = null ) { 3927 3927 return $args['url']; 3928 3928 } 3929 3929 3930 3931 /** 3932 * Is this an allowed comment type for retrieving avatars. 3933 * 3934 * @since 4.9.7 3935 * 3936 * @param string $comment_type Comment Type 3937 * 3938 * @return bool Whether the comment type is allowed for retrieving avatars. 3939 */ 3940 function is_avatar_comment_type( $comment_type ) { 3941 /** 3942 * Filters the list of allowed comment types for retrieving avatars. 3943 * 3944 * @since 3.0.0 3945 * 3946 * @param array $types An array of content types. Default only contains 'comment'. 3947 */ 3948 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); 3949 3950 return in_array( $comment_type, (array) $allowed_comment_types, true ); 3951 } 3952 3953 3930 3954 /** 3931 3955 * Retrieves default data about the avatar. 3932 3956 * … … function get_avatar_data( $id_or_email, $args = null ) { 4071 4095 // Post Object 4072 4096 $user = get_user_by( 'id', (int) $id_or_email->post_author ); 4073 4097 } elseif ( $id_or_email instanceof WP_Comment ) { 4074 /** 4075 * Filters the list of allowed comment types for retrieving avatars. 4076 * 4077 * @since 3.0.0 4078 * 4079 * @param array $types An array of content types. Default only contains 'comment'. 4080 */ 4081 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); 4082 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) { 4098 if ( ! is_avatar_comment_type( get_comment_type( $id_or_email ) ) ) { 4083 4099 $args['url'] = false; 4084 4100 /** This filter is documented in wp-includes/link-template.php */ 4085 4101 return apply_filters( 'get_avatar_data', $args, $id_or_email ); -
tests/phpunit/tests/avatar.php
diff --git tests/phpunit/tests/avatar.php tests/phpunit/tests/avatar.php index b2a767f..3722f6e 100644
class Tests_Avatar extends WP_UnitTestCase { 240 240 return $this->fakeURL; 241 241 } 242 242 243 /** 244 * The `get_avatar_data()` function should return gravatar url when comment type allowed to retrieve avatars. 245 * 246 * @ticket 44033 247 */ 248 public function test_get_avatar_data_should_return_gravatar_url_when_input_avatar_comment_type() { 249 $comment_type = 'comment'; 250 $comment = self::factory()->comment->create_and_get( 251 array( 252 'comment_author_email' => 'commenter@example.com', 253 'comment_type' => $comment_type, 254 ) 255 ); 256 257 $actual_data = get_avatar_data( $comment ); 258 259 $this->assertTrue( is_avatar_comment_type( $comment_type ) ); 260 $this->assertRegexp( '|^http?://[0-9]+.gravatar.com/avatar/[0-9a-f]{32}\?|', $actual_data['url'] ); 261 } 262 263 /** 264 * The `get_avatar_data()` function should return invalid url when comment type not allowed to retrieve avatars. 265 * 266 * @ticket 44033 267 */ 268 public function test_get_avatar_data_should_return_invalid_url_when_input_not_avatar_comment_type() { 269 $comment_type = 'review'; 270 $comment = self::factory()->comment->create_and_get( 271 array( 272 'comment_author_email' => 'commenter@example.com', 273 'comment_type' => $comment_type, 274 ) 275 ); 276 277 $actual_data = get_avatar_data( $comment ); 278 279 $this->assertFalse( is_avatar_comment_type( $comment_type ) ); 280 $this->assertFalse( $actual_data['url'] ); 281 } 282 243 283 } -
new file tests/phpunit/tests/comment/isAvatarCommentType.php
diff --git tests/phpunit/tests/comment/isAvatarCommentType.php tests/phpunit/tests/comment/isAvatarCommentType.php new file mode 100644 index 0000000..d1a37ef
- + 1 <?php 2 /** 3 * Test cases for the `is_avatar_comment_type()` function. 4 * 5 * @package WordPress\UnitTests 6 * 7 * @since 4.9.7 8 */ 9 10 /** 11 * Tests_Comment_IsAvatarCommentType class. 12 * 13 * @group comment 14 * @covers is_avatar_comment_type 15 * 16 * @since 4.9.7 17 */ 18 class Tests_Comment_IsAvatarCommentType extends WP_UnitTestCase { 19 /** 20 * Test the `is_avatar_comment_type()` function. 21 * 22 * @since 4.9.7 23 * 24 * @dataProvider data_is_avatar_comment_type 25 */ 26 public function test_function( $comment_type, $expected ) { 27 $this->assertSame( $expected, is_avatar_comment_type( $comment_type ) ); 28 } 29 30 /** 31 * Dataprovider for `is_avatar_comment_type()`. 32 * 33 * @since 4.9.7 34 * 35 * @return array { 36 * @type array { 37 * @type string Comment type. 38 * @type bool Expected values. 39 * } 40 * } 41 */ 42 public function data_is_avatar_comment_type() { 43 return array( 44 array( null, false ), 45 array( '', false ), 46 array( 'non-existing-comment-type', false ), 47 array( 'comment', true ), 48 ); 49 } 50 51 /** 52 * The function should be filterable with the `get_avatar_comment_types` filter. 53 * 54 * @since 4.9.7 55 */ 56 public function test_function_should_be_filterable() { 57 $this->assertFalse( is_avatar_comment_type( 'review' ) ); 58 59 add_filter( 'get_avatar_comment_types', array( $this, '_filter_avatar_comment_types' ) ); 60 $actual_comment = is_avatar_comment_type( 'comment' ); 61 $actual_review = is_avatar_comment_type( 'review' ); 62 remove_filter( 'get_avatar_comment_types', array( $this, '_filter_avatar_comment_types' ) ); 63 64 $this->assertTrue( $actual_comment ); 65 $this->assertTrue( $actual_review ); 66 } 67 68 /** 69 * Filters callback that modifies the list of allowed comment types for retrieving avatars. 70 * 71 * @since 4.9.7 72 * 73 * @param array $types An array of content types. 74 * @return array $types An array of content types. 75 */ 76 public function _filter_avatar_comment_types( $types ) { 77 $types[] = 'review'; 78 return $types; 79 } 80 81 }