Make WordPress Core

Ticket #60475: 60475.diff

File 60475.diff, 3.3 KB (added by david.binda, 16 months ago)
  • src/wp-includes/comment-template.php

     
    2424function get_comment_author( $comment_id = 0 ) {
    2525        $comment = get_comment( $comment_id );
    2626
    27         $comment_id = ! empty( $comment->comment_ID ) ? $comment->comment_ID : $comment_id;
     27        $comment_id = ! empty( $comment->comment_ID ) ? $comment->comment_ID : (string) $comment_id;
    2828
    2929        if ( empty( $comment->comment_author ) ) {
    3030                $user = ! empty( $comment->user_id ) ? get_userdata( $comment->user_id ) : false;
  • tests/phpunit/tests/comment/getCommentAuthor.php

     
     1<?php
     2
     3/**
     4 * @group comment
     5 *
     6 * @covers ::get_comment_author
     7 */
     8class Tests_Comment_GetCommentAuthor extends WP_UnitTestCase {
     9
     10        private static $comment;
     11
     12        public static function set_up_before_class() {
     13                parent::set_up_before_class();
     14
     15                self::$comment = self::factory()->comment->create_and_get(
     16                        array(
     17                                'comment_post_ID' => 0,
     18                        )
     19                );
     20        }
     21
     22        public function get_comment_author_filter( $comment_author, $comment_id, $comment ) {
     23                $this->assertSame( $comment_id, self::$comment->comment_ID, 'Comment IDs do not match.' );
     24                $this->assertTrue( is_string( $comment_id ), '$comment_id parameter is not a string.' );
     25
     26                return $comment_author;
     27        }
     28
     29        public function test_comment_author_passes_correct_comment_id_for_comment_object() {
     30                add_filter( 'get_comment_author', array( $this, 'get_comment_author_filter' ), 99, 3 );
     31
     32                get_comment_author( self::$comment );
     33
     34                remove_filter( 'get_comment_author', array( $this, 'get_comment_author_filter' ), 99 );
     35        }
     36
     37        public function test_comment_author_passes_correct_comment_id_for_int() {
     38                add_filter( 'get_comment_author', array( $this, 'get_comment_author_filter' ), 99, 3 );
     39
     40                get_comment_author( self::$comment->comment_ID );
     41
     42                remove_filter( 'get_comment_author', array( $this, 'get_comment_author_filter' ), 99 );
     43        }
     44
     45        public function get_comment_author_filter_non_existent_id( $comment_author, $comment_id, $comment ) {
     46                $this->assertSame( $comment_id, (string) $this->non_existent_comment_id, 'Comment IDs do not match.' );
     47                $this->assertTrue( is_string( $comment_id ), '$comment_id parameter is not a string.' );
     48
     49                return $comment_author;
     50        }
     51
     52        public function test_comment_author_passes_correct_comment_id_for_non_existent_comment() {
     53                add_filter( 'get_comment_author', array( $this, 'get_comment_author_filter_non_existent_id' ), 99, 3 );
     54
     55                $this->non_existent_comment_id = self::$comment->comment_ID + 1;
     56
     57                get_comment_author( $this->non_existent_comment_id ); // non-existent comment ID.
     58
     59                remove_filter( 'get_comment_author', array( $this, 'get_comment_author_filter_non_existent_id' ), 99 );
     60        }
     61}