Skip to content

Commit

Permalink
Comments: Only type cast a scalar $comment_id in `get_comment_autho…
Browse files Browse the repository at this point in the history
…r_link()`.

This aims to resolve a fatal error when the incoming `$comment_id` is an instance of `WP_Comment` (or any object) without a `comment_ID` property defined, or if it's empty:
{{{
Object of class WP_Comment could not be converted to string
}}}

This commit mirrors the changes previously made for a similar code fragment in `get_comment_author()`.

Includes:
* Unit tests to demonstrate the fatal error and validate the fix.
* Changing the default value for a non-existent comment ID in `get_comment_author()` from an empty string to zero as a numeric string, for consistency with `get_comment_ID()`.

Follow-up to [52818], [55289], [58335], [58755].

Props narenin, mukesh27, iflairwebtechnologies, umeshsinghin, SergeyBiryukov.
Fixes #61715.

git-svn-id: https://develop.svn.wordpress.org/trunk@58809 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Jul 25, 2024
1 parent 4acefac commit 5ea8ba0
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/wp-includes/comment-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function get_comment_author( $comment_id = 0 ) {
} elseif ( is_scalar( $comment_id ) ) {
$comment_id = (string) $comment_id;
} else {
$comment_id = '';
$comment_id = '0';
}

if ( empty( $comment->comment_author ) ) {
Expand Down Expand Up @@ -233,7 +233,13 @@ function get_comment_author_email_link( $link_text = '', $before = '', $after =
function get_comment_author_link( $comment_id = 0 ) {
$comment = get_comment( $comment_id );

$comment_id = ! empty( $comment->comment_ID ) ? $comment->comment_ID : (string) $comment_id;
if ( ! empty( $comment->comment_ID ) ) {
$comment_id = $comment->comment_ID;
} elseif ( is_scalar( $comment_id ) ) {
$comment_id = (string) $comment_id;
} else {
$comment_id = '0';
}

$comment_author_url = get_comment_author_url( $comment );
$comment_author = get_comment_author( $comment );
Expand Down
116 changes: 116 additions & 0 deletions tests/phpunit/tests/comment/getCommentAuthorLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

/**
* @group comment
*
* @covers ::get_comment_author_link
*/
class Tests_Comment_GetCommentAuthorLink 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_link_filter( $comment_author_link, $comment_author, $comment_id ) {
$this->assertSame( $comment_id, self::$comment->comment_ID, 'Comment IDs do not match.' );
$this->assertIsString( $comment_id, '$comment_id parameter is not a string.' );

return $comment_author_link;
}

public function test_comment_author_link_passes_correct_comment_id_for_comment_object() {
add_filter( 'get_comment_author_link', array( $this, 'get_comment_author_link_filter' ), 99, 3 );

get_comment_author_link( self::$comment );
}

public function test_comment_author_link_passes_correct_comment_id_for_int() {
add_filter( 'get_comment_author_link', array( $this, 'get_comment_author_link_filter' ), 99, 3 );

get_comment_author_link( (int) self::$comment->comment_ID );
}

public function get_comment_author_link_filter_non_existent_id( $comment_author_link, $comment_author, $comment_id ) {
$this->assertSame( $comment_id, (string) self::$non_existent_comment_id, 'Comment IDs do not match.' );
$this->assertIsString( $comment_id, '$comment_id parameter is not a string.' );

return $comment_author_link;
}

/**
* @ticket 60475
*/
public function test_comment_author_link_passes_correct_comment_id_for_non_existent_comment() {
add_filter( 'get_comment_author_link', array( $this, 'get_comment_author_link_filter_non_existent_id' ), 99, 3 );

self::$non_existent_comment_id = self::$comment->comment_ID + 1;

get_comment_author_link( self::$non_existent_comment_id ); // Non-existent comment ID.
}

/**
* @ticket 61681
* @ticket 61715
*
* @dataProvider data_should_return_author_when_given_object_without_comment_id
*
* @param stdClass $comment_props Comment properties test data.
* @param string $expected The expected result.
* @param array $user_data Optional. User data for creating an author. Default empty array.
*/
public function test_should_return_author_when_given_object_without_comment_id( $comment_props, $expected, $user_data = array() ) {
if ( ! empty( $comment_props->user_id ) ) {
$user = self::factory()->user->create_and_get( $user_data );
$comment_props->user_id = $user->ID;
}

$comment = new WP_Comment( $comment_props );

$this->assertSame( $expected, get_comment_author_link( $comment ) );
}

/**
* Data provider.
*
* @return array
*/
public function data_should_return_author_when_given_object_without_comment_id() {
return array(
'with no author' => array(
'comment_props' => new stdClass(),
'expected' => 'Anonymous',
),
'with author name' => array(
'comment_props' => (object) array(
'comment_author' => 'tester1',
),
'expected' => 'tester1',
),
'with author name, empty ID' => array(
'comment_props' => (object) array(
'comment_author' => 'tester2',
'comment_ID' => '',
),
'expected' => 'tester2',
),
'with author ID' => array(
'comment_props' => (object) array(
'user_id' => 1, // Populates in the test with an actual user ID.
),
'expected' => 'Tester3',
'user_data' => array(
'display_name' => 'Tester3',
),
),
);
}
}

0 comments on commit 5ea8ba0

Please sign in to comment.