Comments: Return early from comment_form() if an invalid post ID is passed.

If an invalid post ID is passed to the function, `comments_open()` should return `false`, and no comment form be displayed. This commit restores the previous behavior that was unintentionally changed when standardizing on the `$post` parameter name.

Follow-up to [53715].

Props peterwilsoncc.
Fixes #56243.

git-svn-id: https://develop.svn.wordpress.org/trunk@54488 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2022-10-11 15:43:04 +00:00
parent 20bccea8a7
commit 645f474fc0
2 changed files with 46 additions and 4 deletions

View File

@@ -153,4 +153,44 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase {
$expected = '<a rel="nofollow" id="cancel-comment-reply-link" href="#respond" style="display:none;">Cancel reply</a>';
$this->assertStringNotContainsString( $expected, $form );
}
/**
* @ticket 56243
*/
public function test_comment_form_should_not_display_for_global_post_when_called_with_invalid_id() {
// Go to permalink to ensure global post ID is set.
$this->go_to( get_permalink( self::$post_id ) );
$impossibly_high_post_id = PHP_INT_MAX;
$form = get_echo( 'comment_form', array( array(), $impossibly_high_post_id ) );
$this->assertEmpty( $form );
}
/**
* @ticket 56243
*/
public function test_comment_form_should_display_for_global_post_with_falsey_post_id() {
$post_id = self::$post_id;
$this->go_to( get_permalink( $post_id ) );
$form = get_echo( 'comment_form', array( array(), false ) );
$this->assertNotEmpty( $form );
$post_hidden_field = "<input type='hidden' name='comment_post_ID' value='{$post_id}' id='comment_post_ID' />";
$this->assertStringContainsString( $post_hidden_field, $form );
}
/**
* @ticket 56243
*/
public function test_comment_form_should_display_for_specified_post_when_passed_a_valid_post_id() {
$post_id = self::$post_id;
$form = get_echo( 'comment_form', array( array(), $post_id ) );
$this->assertNotEmpty( $form );
$post_hidden_field = "<input type='hidden' name='comment_post_ID' value='{$post_id}' id='comment_post_ID' />";
$this->assertStringContainsString( $post_hidden_field, $form );
}
}