wordpress-develop/tests/phpunit/tests/comment
Tonya Mork 3442f776f3 Comments: Fix PHP Notice "trying to get property of non-object" in comments_open() and pings_open().
The post for the comments or pings is retrieved by `get_post()`. If the post exists, `get_post()` returns an instance of `WP_Post`; else, it returns `null`.

In both `comments_open()` and `pings_open()`, the returned value from `get_post()` is used without checking if the object returned, if the post exists. When the post does not exist, the following notices occur:

{{{
PHP Notice:  Trying to get property 'comment_status' of non-object in .../src/wp-includes/comment-template.php on line 1244
}}}

and

{{{
PHP Notice:  Trying to get property 'pings_open' of non-object in ../src/wp-includes/comment-template.php on line 1274
}}}

This commit fixes these notices by checking if the post has a non-falsey value before using it as an object to set the `$open` state. As the return from `get_post()` will only be an object or `null`, the truthy check is appropriate and slightly more performant.

Tests added to validate the fix.

Follow-up to [1964], [40666].

Props dd32, audrasjb, costdev, hellofromTonya, sergeybiryukov.
Fixes #54159.

git-svn-id: https://develop.svn.wordpress.org/trunk@52223 602fd350-edb4-49c9-b593-d223f7449a82
2021-11-19 20:22:43 +00:00
..
checkComment.php General: Remove “whitelist” and “blacklist” in favor of more clear and inclusive language. 2020-06-22 17:24:34 +00:00
commentForm.php Comments: Don't output "cancel comment reply link" if comments aren't threaded. 2021-11-16 02:13:25 +00:00
commentsOpen.php Comments: Fix PHP Notice "trying to get property of non-object" in comments_open() and pings_open(). 2021-11-19 20:22:43 +00:00
commentsTemplate.php Coding Standards: Use static closures when not using $this. 2021-08-26 12:57:08 +00:00
dateQuery.php Build/Test Tools: Implement use of the void solution. 2021-08-07 10:29:41 +00:00
getCommentAuthorEmailLink.php Build/Test Tools: Implement use of the void solution. 2021-08-07 10:29:41 +00:00
getCommentAuthorUrl.php Code is Poetry. 2017-11-30 23:09:33 +00:00
getCommentAuthorUrlLink.php Build/Test Tools: Standardise the name, signature, and usage of the wpSetUpBeforeClass() method that's used in test classes. 2020-11-15 13:59:32 +00:00
getCommentClass.php Unit Tests: after [35225], make factory a method/getter on WP_UnitTestCase and add magic methods for BC for every plugin that is extending WP_UnitTestCase and accessing the $factory instance prop. 2015-10-17 18:02:16 +00:00
getCommentCount.php Comments: Ensure all elements in the array returned by get_comment_count() are integers. 2020-03-28 20:42:31 +00:00
getCommentExcerpt.php Tests: Use more appropriate assertions in various tests. 2021-07-07 10:32:56 +00:00
getCommentLink.php Tests: Replace assertContains() with assertStringContainsString() when used with strings. 2021-07-19 14:00:11 +00:00
getCommentReplyLink.php Tests: Replace assertContains() with assertStringContainsString() when used with strings. 2021-07-19 14:00:11 +00:00
getCommentsPagesCount.php Coding Standards: Add visibility to methods in tests/phpunit/tests/. 2021-11-04 15:22:47 +00:00
getPageOfComment.php Tests: Fix tests failing due to assertContains() using strict checking. 2021-08-07 10:51:59 +00:00
isAvatarCommentType.php Tests: Correct a number of malformed @covers tags. 2020-08-23 17:23:52 +00:00
lastCommentModified.php Tests: Use @ticket annotation for ticket numbers, for clarity. 2021-02-13 12:17:36 +00:00
metaCache.php Build/Test Tools: Replace assertInternalType() usage in unit tests. 2021-07-05 17:21:53 +00:00
pingsOpen.php Comments: Fix PHP Notice "trying to get property of non-object" in comments_open() and pings_open(). 2021-11-19 20:22:43 +00:00
query.php Coding Standards: Add visibility to methods in tests/phpunit/tests/. 2021-11-04 15:22:47 +00:00
slashes.php Coding Standards: Add visibility to methods in tests/phpunit/tests/. 2021-11-04 15:22:47 +00:00
template.php Coding Standards: Add visibility to methods in tests/phpunit/tests/. 2021-11-04 15:22:47 +00:00
walker.php Coding Standards: Add visibility to methods in tests/phpunit/tests/. 2021-11-04 15:22:47 +00:00
wpAllowComment.php Build/Test Tools: Standardise the name, signature, and usage of the wpSetUpBeforeClass() method that's used in test classes. 2020-11-15 13:59:32 +00:00
wpBatchUpdateCommentType.php Tests: First pass at using assertSame() instead of assertEquals() in most of the unit tests. 2020-09-02 00:35:36 +00:00
wpBlacklistCheck.php General: Update code for readability and inclusion 2020-07-23 03:12:49 +00:00
wpComment.php Build/Test Tools: Standardise the name, signature, and usage of the wpSetUpBeforeClass() method that's used in test classes. 2020-11-15 13:59:32 +00:00
wpCountComments.php Tests: First pass at using assertSame() instead of assertEquals() in most of the unit tests. 2020-09-02 00:35:36 +00:00
wpListComments.php Docs: Improve inline comments per the documentation standards. 2020-01-29 00:43:23 +00:00
wpUpdateCommentCountNow.php Correct usage of Covers for Tests_Update_Comment_Count_Now 2016-05-17 20:19:50 +00:00