From 3a229d0b477a47edf5235f6ad49f5e132ac37049 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 26 Apr 2020 13:42:03 +0000 Subject: [PATCH] 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 --- src/wp-includes/comment.php | 2 +- tests/phpunit/tests/comment-submission.php | 39 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 87ed753c17..8648a89a0f 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -1912,7 +1912,7 @@ function wp_insert_comment( $commentdata ) { $comment_karma = ! isset( $data['comment_karma'] ) ? 0 : $data['comment_karma']; $comment_approved = ! isset( $data['comment_approved'] ) ? 1 : $data['comment_approved']; $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']; $user_id = ! isset( $data['user_id'] ) ? 0 : $data['user_id']; diff --git a/tests/phpunit/tests/comment-submission.php b/tests/phpunit/tests/comment-submission.php index 6a5b845bb0..e2534ed2e8 100644 --- a/tests/phpunit/tests/comment-submission.php +++ b/tests/phpunit/tests/comment-submission.php @@ -715,6 +715,45 @@ class Tests_Comment_Submission extends WP_UnitTestCase { $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 */