Make WordPress Core

Ticket #57615: 57615.patch

File 57615.patch, 3.1 KB (added by thomasplevy, 3 years ago)
  • src/wp-includes/comment-template.php

    diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php
    index 35d33e4e50..7d059db9af 100644
    a b function get_comment_author( $comment_ID = 0 ) { 
    5959 *                                   Default current comment.
    6060 */
    6161function comment_author( $comment_ID = 0 ) {
    62         $comment = get_comment( $comment_ID );
    63         $author  = get_comment_author( $comment );
     62        $comment    = get_comment( $comment_ID );
     63        $comment_ID = ! empty( $comment->comment_ID ) ? $comment->comment_ID : (string) $comment_ID;
     64        $author     = get_comment_author( $comment );
    6465
    6566        /**
    6667         * Filters the comment author's name for display.
    function comment_author( $comment_ID = 0 ) { 
    7172         * @param string $author     The comment author's username.
    7273         * @param string $comment_ID The comment ID as a numeric string.
    7374         */
    74         echo apply_filters( 'comment_author', $author, $comment->comment_ID );
     75        echo apply_filters( 'comment_author', $author, $comment_ID );
    7576}
    7677
    7778/**
  • new file tests/phpunit/tests/comment/getCommentAuthor.php

    diff --git a/tests/phpunit/tests/comment/getCommentAuthor.php b/tests/phpunit/tests/comment/getCommentAuthor.php
    new file mode 100644
    index 0000000000..83625fe286
    - +  
     1<?php
     2
     3/**
     4 * @group comment
     5 *
     6 * @covers ::get_comment_author
     7 */
     8class Tests_Comment_GetCommentAuthor extends WP_UnitTestCase {
     9        protected static $comments = array();
     10
     11        public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     12                unset( $GLOBALS['comment'] );
     13
     14                $comment_ids                = $factory->comment->create_post_comments( 0, 1 );
     15                $user_generated_comment_ids = $factory->comment->create_post_comments( 0, 1, array(
     16                        'user_id'        => $factory->user->create(),
     17                        'comment_author' => '',
     18                ) );
     19                self::$comments = array_map(
     20                        'get_comment',
     21                        array_merge(
     22                                $comment_ids,
     23                                $user_generated_comment_ids
     24                        )
     25                );
     26        }
     27
     28        public function test_no_comment() {
     29                $author = get_comment_author();
     30                $this->assertSame( 'Anonymous', $author );
     31        }
     32
     33        public function test_invalid_comment() {
     34                $comment            = end( self::$comments );
     35                $invalid_comment_id = $comment->comment_ID + 1;
     36                $author             = get_comment_author( $invalid_comment_id );
     37                $this->assertSame( 'Anonymous', $author );
     38        }
     39
     40        public function test_global_comment() {
     41                $comment            = reset( self::$comments );
     42                $GLOBALS['comment'] = $comment;
     43                $author             = get_comment_author();
     44                $this->assertSame( $comment->comment_author, $author );
     45                unset( $GLOBALS['comment'] );
     46        }
     47
     48        public function test_comment_arg() {
     49                $comment = reset( self::$comments );
     50                $author  = get_comment_author( $comment );
     51                $this->assertSame( $comment->comment_author, $author );
     52        }
     53
     54        public function test_comment_by_registered_user_with_empty_comment_author_name() {
     55                $comment = end( self::$comments );
     56                $user    = get_userdata( $comment->user_id );
     57                $author  = get_comment_author( $comment );
     58                $this->assertSame( $user->display_name, $author );
     59        }
     60
     61}