#41846 closed defect (bug) (fixed)
Avoid PHP notice in get_comment_reply_link() for null comment
Reported by: | birgire | Owned by: | whyisjake |
---|---|---|---|
Milestone: | 5.3 | Priority: | normal |
Severity: | normal | Version: | 2.7 |
Component: | Comments | Keywords: | has-patch has-unit-tests commit |
Focuses: | Cc: |
Description
When I was working on tests for get_comment_reply_link()
I got a PHP notice:
Notice Trying to get property of non-object ...
with inputs like:
get_comment_reply_link( array( 'depth' => 1, 'max_depth' => 2 ) );
when there's no current global comment available and the input comment is null.
That's because of this part:
$comment = get_comment( $comment ); if ( empty( $post ) ) { $post = $comment->comment_post_ID; }
where there's no validation for $comment
.
Attachments (3)
Change History (15)
#3
@
6 years ago
- Milestone changed from Awaiting Review to 5.3
Thanks @birgire! This looks good.
In 41846.diff I just made a few small whitespace CS fixes to the unit test and added the ticket number to the @ticket annotation in the docblock.
I'm going to go ahead and milestone this for the next release.
#5
@
5 years ago
When I run phpunit --group comment
I am getting the following error.
1) Tests_Comment_GetCommentReplyLink::test_should_return_null_when_depth_less_than_max_depth_and_comment_null_and_no_current_global_comment TypeError: Argument 2 passed to PHPUnit\Framework\Assert::assertNull() must be of the type string, null given, called in /tests/phpunit/tests/comment/getCommentReplyLink.php on line 89 /tests/phpunit/tests/comment/getCommentReplyLink.php:89
#7
@
5 years ago
@whyisjake thanks for testing
41846-2.diff is a refreshed patch that runs successfully through the comment group tests.
#9
@
5 years ago
- Owner set to whyisjake
- Resolution set to fixed
- Status changed from new to closed
In 46335:
#11
@
5 years ago
@birgire @whyisjake What bug does this fix? Returning early when there's no global comment silences the error which, in my eyes, seems to be a legitimate one. Calling this function in a context where there's no global comment is incorrect.
#12
@
5 years ago
@johnbillion good question
At the time I think I might have looked at the validation on some of the other input arguments, that also returns null when invalid, correpsonding to the existing void part of the docs:
* @return void|false|string Link to show comment form, if successful. False, if comments are closed.
I might have also looked into get_post()
as a reference point too, for a typical WordPress practice, as it returns null on failure of finding a global post object:
https://core.trac.wordpress.org/browser/tags/5.2/src/wp-includes/post.php#L726
But I'm all in for helping devs to better debug their code.
In 41846.patch we added a comment validation, similar to the existing one for the post.
Included is a test.