Comments: Ensure that inserting a comment with an empty type results in correct `comment` type.

Add unit tests for `wp_handle_comment_submission()` and `wp_insert_comment()` receiving an empty type.

Follow-up to [47597].

Props ocean90, imath.
Fixes #49236.

git-svn-id: https://develop.svn.wordpress.org/trunk@47626 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2020-04-26 13:42:03 +00:00
parent aff2844d75
commit 3a229d0b47
2 changed files with 40 additions and 1 deletions

View File

@ -1912,7 +1912,7 @@ function wp_insert_comment( $commentdata ) {
$comment_karma = ! isset( $data['comment_karma'] ) ? 0 : $data['comment_karma']; $comment_karma = ! isset( $data['comment_karma'] ) ? 0 : $data['comment_karma'];
$comment_approved = ! isset( $data['comment_approved'] ) ? 1 : $data['comment_approved']; $comment_approved = ! isset( $data['comment_approved'] ) ? 1 : $data['comment_approved'];
$comment_agent = ! isset( $data['comment_agent'] ) ? '' : $data['comment_agent']; $comment_agent = ! isset( $data['comment_agent'] ) ? '' : $data['comment_agent'];
$comment_type = ! isset( $data['comment_type'] ) ? 'comment' : $data['comment_type']; $comment_type = empty( $data['comment_type'] ) ? 'comment' : $data['comment_type'];
$comment_parent = ! isset( $data['comment_parent'] ) ? 0 : $data['comment_parent']; $comment_parent = ! isset( $data['comment_parent'] ) ? 0 : $data['comment_parent'];
$user_id = ! isset( $data['user_id'] ) ? 0 : $data['user_id']; $user_id = ! isset( $data['user_id'] ) ? 0 : $data['user_id'];

View File

@ -715,6 +715,45 @@ class Tests_Comment_Submission extends WP_UnitTestCase {
$this->assertSame( $error, $comment->get_error_code() ); $this->assertSame( $error, $comment->get_error_code() );
} }
/**
* @ticket 49236
*/
public function test_submitting_comment_with_empty_type_results_in_correct_type() {
$data = array(
'comment_post_ID' => self::$post->ID,
'comment' => 'Comment',
'author' => 'Comment Author',
'email' => 'comment@example.org',
'comment_type' => '',
);
$comment = wp_handle_comment_submission( $data );
$this->assertNotWPError( $comment );
$this->assertInstanceOf( 'WP_Comment', $comment );
$this->assertSame( 'comment', $comment->comment_type );
}
/**
* @ticket 49236
*/
public function test_inserting_comment_with_empty_type_results_in_correct_type() {
$data = array(
'comment_post_ID' => self::$post->ID,
'comment' => 'Comment',
'author' => 'Comment Author',
'email' => 'comment@example.org',
'comment_type' => '',
);
$comment_id = wp_insert_comment( $data );
$comment = get_comment( $comment_id );
$this->assertNotWPError( $comment );
$this->assertInstanceOf( 'WP_Comment', $comment );
$this->assertSame( 'comment', $comment->comment_type );
}
/** /**
* @ticket 34997 * @ticket 34997
*/ */