Make WordPress Core

Ticket #44033: 44033.3.diff

File 44033.3.diff, 5.6 KB (added by birgire, 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 ) { 
    39273927        return $args['url'];
    39283928}
    39293929
     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 */
     3940function 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
    39303954/**
    39313955 * Retrieves default data about the avatar.
    39323956 *
    function get_avatar_data( $id_or_email, $args = null ) { 
    40714095                // Post Object
    40724096                $user = get_user_by( 'id', (int) $id_or_email->post_author );
    40734097        } 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 ) ) ) {
    40834099                        $args['url'] = false;
    40844100                        /** This filter is documented in wp-includes/link-template.php */
    40854101                        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 { 
    240240                return $this->fakeURL;
    241241        }
    242242
     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
    243283}
  • 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 */
     18class 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}