Comments: Make wp_update_comment() return false instead of 0 for an invalid comment or post ID.

This addresses an inconsistency where 0 could mean one of the three scenarios:

* Invalid comment ID.
* Invalid comment post ID.
* No DB rows updated. This is not an error and should not be treated as one.

With this change, `wp_update_comment()` always returns either `false` or a `WP_Error` object on failure, depending on the value of the `$wp_error` parameter.

Follow-up to [48154], [48215], [48216], [48218], [48230].

Props dd32, jnylen0, enrico.sorcinelli.
Fixes #39732. See #38700, #39735.

git-svn-id: https://develop.svn.wordpress.org/trunk@48235 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2020-06-30 14:11:00 +00:00
parent 0445f4b95c
commit 90e840f9db
2 changed files with 39 additions and 6 deletions

View File

@@ -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
*/