diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 9868d62c23..22543a998a 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -2422,6 +2422,8 @@ function wp_set_comment_status( $comment_id, $comment_status, $wp_error = false * @since 2.0.0 * @since 4.9.0 Add updating comment meta during comment update. * @since 5.5.0 The `$wp_error` parameter was added. + * @since 5.5.0 The return values for an invalid comment or post ID + * were changed to false instead of 0. * * @global wpdb $wpdb WordPress database abstraction object. * @@ -2439,7 +2441,7 @@ function wp_update_comment( $commentarr, $wp_error = false ) { if ( $wp_error ) { return new WP_Error( 'invalid_comment_id', __( 'Invalid comment ID.' ) ); } else { - return 0; + return false; } } @@ -2448,7 +2450,7 @@ function wp_update_comment( $commentarr, $wp_error = false ) { if ( $wp_error ) { return new WP_Error( 'invalid_post_id', __( 'Invalid post ID.' ) ); } else { - return 0; + return false; } } diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index 5d4fa6e4f0..24cbeca00c 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -31,28 +31,32 @@ class Tests_Comment extends WP_UnitTestCase { } public function test_wp_update_comment() { - $post = self::factory()->post->create_and_get( + $post = self::factory()->post->create_and_get( array( 'post_title' => 'some-post', 'post_type' => 'post', ) ); - $post2 = self::factory()->post->create_and_get( + $post2 = self::factory()->post->create_and_get( array( 'post_title' => 'some-post-2', 'post_type' => 'post', ) ); + $comments = self::factory()->comment->create_post_comments( $post->ID, 5 ); - $result = wp_update_comment( + + $result = wp_update_comment( array( 'comment_ID' => $comments[0], 'comment_parent' => $comments[1], ) ); $this->assertEquals( 1, $result ); + $comment = get_comment( $comments[0] ); $this->assertEquals( $comments[1], $comment->comment_parent ); + $result = wp_update_comment( array( 'comment_ID' => $comments[0], @@ -60,12 +64,14 @@ class Tests_Comment extends WP_UnitTestCase { ) ); $this->assertEquals( 0, $result ); - $result = wp_update_comment( + + $result = wp_update_comment( array( 'comment_ID' => $comments[0], 'comment_post_ID' => $post2->ID, ) ); + $comment = get_comment( $comments[0] ); $this->assertEquals( $post2->ID, $comment->comment_post_ID ); } @@ -92,6 +98,7 @@ class Tests_Comment extends WP_UnitTestCase { */ public function test_wp_update_comment_updates_comment_meta() { $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) ); + wp_update_comment( array( 'comment_ID' => $comment_id, @@ -101,6 +108,7 @@ class Tests_Comment extends WP_UnitTestCase { ), ) ); + $this->assertEquals( 'fire', get_comment_meta( $comment_id, 'sauce', true ) ); } @@ -142,6 +150,29 @@ class Tests_Comment extends WP_UnitTestCase { $this->assertEquals( $updated_comment_text, $comment->comment_content ); } + /** + * @ticket 39732 + */ + public function test_wp_update_comment_returns_false_for_invalid_comment_or_post_id() { + $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) ); + + $update = wp_update_comment( + array( + 'comment_ID' => -1, + 'comment_post_ID' => self::$post_id, + ) + ); + $this->assertSame( false, $update ); + + $update = wp_update_comment( + array( + 'comment_ID' => $comment_id, + 'comment_post_ID' => -1, + ) + ); + $this->assertSame( false, $update ); + } + /** * @ticket 39732 */