diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index d125f0c2dd..8c56097333 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -24,7 +24,7 @@ function get_comment_author( $comment_id = 0 ) { $comment = get_comment( $comment_id ); - $comment_id = ! empty( $comment->comment_ID ) ? $comment->comment_ID : $comment_id; + $comment_id = ! empty( $comment->comment_ID ) ? $comment->comment_ID : (string) $comment_id; if ( empty( $comment->comment_author ) ) { $user = ! empty( $comment->user_id ) ? get_userdata( $comment->user_id ) : false; diff --git a/tests/phpunit/tests/comment/getCommentAuthor.php b/tests/phpunit/tests/comment/getCommentAuthor.php new file mode 100644 index 0000000000..31d5a57bdb --- /dev/null +++ b/tests/phpunit/tests/comment/getCommentAuthor.php @@ -0,0 +1,59 @@ +<?php + +/** + * @group comment + * + * @covers ::get_comment_author + */ +class Tests_Comment_GetCommentAuthor extends WP_UnitTestCase { + + private static $comment; + private static $non_existent_comment_id; + + public static function set_up_before_class() { + parent::set_up_before_class(); + + self::$comment = self::factory()->comment->create_and_get( + array( + 'comment_post_ID' => 0, + ) + ); + } + + public function get_comment_author_filter( $comment_author, $comment_id, $comment ) { + $this->assertSame( $comment_id, self::$comment->comment_ID, 'Comment IDs do not match.' ); + $this->assertTrue( is_string( $comment_id ), '$comment_id parameter is not a string.' ); + + return $comment_author; + } + + public function test_comment_author_passes_correct_comment_id_for_comment_object() { + add_filter( 'get_comment_author', array( $this, 'get_comment_author_filter' ), 99, 3 ); + + get_comment_author( self::$comment ); + } + + public function test_comment_author_passes_correct_comment_id_for_int() { + add_filter( 'get_comment_author', array( $this, 'get_comment_author_filter' ), 99, 3 ); + + get_comment_author( (int) self::$comment->comment_ID ); + } + + public function get_comment_author_filter_non_existent_id( $comment_author, $comment_id, $comment ) { + $this->assertSame( $comment_id, (string) self::$non_existent_comment_id, 'Comment IDs do not match.' ); + $this->assertTrue( is_string( $comment_id ), '$comment_id parameter is not a string.' ); + + return $comment_author; + } + + /** + * @ticket 60475 + */ + public function test_comment_author_passes_correct_comment_id_for_non_existent_comment() { + add_filter( 'get_comment_author', array( $this, 'get_comment_author_filter_non_existent_id' ), 99, 3 ); + + self::$non_existent_comment_id = self::$comment->comment_ID + 1; + + get_comment_author( self::$non_existent_comment_id ); // Non-existent comment ID. + } +} diff --git a/tests/phpunit/tests/comment/getCommentAuthorEmailLink.php b/tests/phpunit/tests/comment/getCommentAuthorEmailLink.php index a203d38122..c505c9750f 100644 --- a/tests/phpunit/tests/comment/getCommentAuthorEmailLink.php +++ b/tests/phpunit/tests/comment/getCommentAuthorEmailLink.php @@ -5,6 +5,7 @@ * @covers ::get_comment_author_email_link */ class Tests_Comment_GetCommentAuthorEmailLink extends WP_UnitTestCase { + public static $comment; public function set_up() { diff --git a/tests/phpunit/tests/comment/getCommentAuthorUrl.php b/tests/phpunit/tests/comment/getCommentAuthorUrl.php index bc008b19fc..e54e043b50 100644 --- a/tests/phpunit/tests/comment/getCommentAuthorUrl.php +++ b/tests/phpunit/tests/comment/getCommentAuthorUrl.php @@ -6,26 +6,31 @@ * @covers ::get_comment_author_url */ class Tests_Comment_GetCommentAuthorUrl extends WP_UnitTestCase { - public function get_comment_author_url_filter( $url, $id, $comment ) { - $this->assertSame( $id, $comment->comment_ID ); - return $url; + private static $comment; + + public static function set_up_before_class() { + parent::set_up_before_class(); + + self::$comment = self::factory()->comment->create_and_get( + array( + 'comment_post_ID' => 0, + ) + ); + } + + public function get_comment_author_url_filter( $comment_author_url, $comment_id, $comment ) { + $this->assertSame( $comment_id, $comment->comment_ID ); + + return $comment_author_url; } /** * @ticket 41334 */ public function test_comment_author_url_passes_correct_comment_id() { - $comment = self::factory()->comment->create_and_get( - array( - 'comment_post_ID' => 0, - ) - ); - add_filter( 'get_comment_author_url', array( $this, 'get_comment_author_url_filter' ), 99, 3 ); - get_comment_author_url( $comment ); - - remove_filter( 'get_comment_author_url', array( $this, 'get_comment_author_url_filter' ), 99 ); + get_comment_author_url( self::$comment ); } } diff --git a/tests/phpunit/tests/comment/getCommentAuthorUrlLink.php b/tests/phpunit/tests/comment/getCommentAuthorUrlLink.php index 7e60b9cc1b..ee964d96d9 100644 --- a/tests/phpunit/tests/comment/getCommentAuthorUrlLink.php +++ b/tests/phpunit/tests/comment/getCommentAuthorUrlLink.php @@ -6,6 +6,7 @@ * @covers ::get_comment_author_url_link */ class Tests_Comment_GetCommentAuthorUrlLink extends WP_UnitTestCase { + protected static $comments = array(); public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {