Comments: Ensure the correct comment ID type is passed to get_comment_author.

The `$comment_id` parameter of the `get_comment_author` filter is documented as a numeric string, however in case a non-existing comment ID is passed to the `get_comment_author()` function, it could be an integer instead.

This commit resolves the issue and adds a PHPUnit test demonstrating the behavior.

Includes updating `get_comment_author_url()` unit tests for consistency.

Follow-up to [41127], [52818].

Props david.binda.
Fixes #60475.

git-svn-id: https://develop.svn.wordpress.org/trunk@58335 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2024-06-04 15:27:57 +00:00
parent 447f6d7bc9
commit 1e208fc053
5 changed files with 79 additions and 13 deletions

View File

@ -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;

View File

@ -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.
}
}

View File

@ -5,6 +5,7 @@
* @covers ::get_comment_author_email_link
*/
class Tests_Comment_GetCommentAuthorEmailLink extends WP_UnitTestCase {
public static $comment;
public function set_up() {

View File

@ -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 );
}
}

View File

@ -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 ) {