Comments: Allow wp_update_comment() to return WP_Error().

The `wp_update_comment_data` filter introduced in 4.7 allows comment data to be filtered before it is updated in the database.

The patch aims to handle `WP_Error` as the filter above return value in a similar manner as is done for `wp_new_comment()`.


Fixes #39732.

Props: enricosorcinelli, swissspidy, gkloveweb, jnylen0, jbpaul17, afercia, SergeyBiryukov, audrasjb, imath, davidbaumwald.


git-svn-id: https://develop.svn.wordpress.org/trunk@48154 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jake Spurlock
2020-06-24 00:03:33 +00:00
parent 7a8d8a3e8a
commit ec062c08fe
9 changed files with 135 additions and 13 deletions

View File

@@ -32,6 +32,11 @@ class Tests_Ajax_EditComment extends WP_Ajax_UnitTestCase {
$this->_comment_post = get_post( $post_id );
}
public function tearDown() {
remove_filter( 'wp_update_comment_data', array( $this, '_wp_update_comment_data_filter' ), 10, 3 );
parent::tearDown();
}
/**
* Get comments as a privilged user (administrator)
* Expects test to pass
@@ -126,6 +131,41 @@ class Tests_Ajax_EditComment extends WP_Ajax_UnitTestCase {
$this->assertEmpty( (string) $xml->response[0]->edit_comment[0]->supplemental );
}
/**
* @ticket 39732
*/
public function test_wp_update_comment_data_is_wp_error() {
// Become an administrator
$this->_setRole( 'administrator' );
// Get a comment
$comments = get_comments(
array(
'post_id' => $this->_comment_post->ID,
)
);
$comment = array_pop( $comments );
// Set up a default request
$_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' );
$_POST['comment_ID'] = $comment->comment_ID;
$_POST['content'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
// Simulate filter check error
add_filter( 'wp_update_comment_data', array( $this, '_wp_update_comment_data_filter' ), 10, 3 );
// Make the request
$this->setExpectedException( 'WPAjaxDieStopException', 'wp_update_comment_data filter fails for this comment.' );
$this->_handleAjax( 'edit-comment' );
}
/**
* Block comments from being updated by returning WP_Error
*/
public function _wp_update_comment_data_filter( $data, $comment, $commentarr ) {
return new WP_Error( 'comment_wrong', __( 'wp_update_comment_data filter fails for this comment.' ), 500 );
}
/**
* Get comments as a non-privileged user (subscriber)
* Expects test to fail

View File

@@ -142,6 +142,31 @@ class Tests_Comment extends WP_UnitTestCase {
$this->assertEquals( $updated_comment_text, $comment->comment_content );
}
/**
* @ticket 39732
*/
public function test_wp_update_comment_is_wp_error() {
$comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
add_filter( 'wp_update_comment_data', array( $this, '_wp_update_comment_data_filter' ), 10, 3 );
$result = wp_update_comment(
array(
'comment_ID' => $comment_id,
'comment_type' => 'pingback',
),
true
);
$this->assertWPError( $result );
remove_filter( 'wp_update_comment_data', array( $this, '_wp_update_comment_data_filter' ), 10, 3 );
}
/**
* Block comments from being updated by returning WP_Error
*/
public function _wp_update_comment_data_filter( $data, $comment, $commentarr ) {
return new WP_Error( 'comment_wrong', __( 'wp_update_comment_data filter fails for this comment.' ), 500 );
}
public function test_get_approved_comments() {
$ca1 = self::factory()->comment->create(
array(

View File

@@ -2790,6 +2790,36 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$this->assertErrorResponse( 'comment_content_column_length', $response, 400 );
}
/**
* @ticket 39732
*/
public function test_update_comment_is_wp_error() {
wp_set_current_user( self::$admin_id );
$params = array(
'content' => 'This isn\'t a saxophone. It\'s an umbrella.',
);
add_filter( 'wp_update_comment_data', array( $this, '_wp_update_comment_data_filter' ), 10, 3 );
$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
$request->add_header( 'content-type', 'application/json' );
$request->set_body( wp_json_encode( $params ) );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_comment_failed_edit', $response, 500 );
remove_filter( 'wp_update_comment_data', array( $this, '_wp_update_comment_data_filter' ), 10, 3 );
}
/**
* Block comments from being updated by returning WP_Error
*/
public function _wp_update_comment_data_filter( $data, $comment, $commentarr ) {
return new WP_Error( 'comment_wrong', __( 'wp_update_comment_data filter fails for this comment.' ), array( 'status' => 500 ) );
}
public function verify_comment_roundtrip( $input = array(), $expected_output = array() ) {
// Create the comment.
$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );