mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-03-30 18:24:31 +00:00
REST API: Allow shortcircuiting rest_pre_insert_comment
rest_pre_insert_{post_type} allows returning a WP_Error from the filter to shortcircuit actually creating the object, so it makes sense to do so for comments too.
Props dspilka.
Fixes #39578.
git-svn-id: https://develop.svn.wordpress.org/trunk@39922 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -985,6 +985,32 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
|
||||
$this->assertEquals( $params['content']['raw'], $new_comment->comment_content );
|
||||
}
|
||||
|
||||
public function test_create_item_error_from_filter() {
|
||||
add_filter( 'rest_pre_insert_comment', array( $this, 'return_premade_error' ) );
|
||||
wp_set_current_user( self::$admin_id );
|
||||
|
||||
$params = array(
|
||||
'post' => self::$post_id,
|
||||
'author_name' => 'Homer Jay Simpson',
|
||||
'author_email' => 'homer@example.org',
|
||||
'content' => array(
|
||||
'raw' => 'Aw, he loves beer. Here, little fella.'
|
||||
),
|
||||
);
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
|
||||
$request->add_header( 'content-type', 'application/json' );
|
||||
$request->set_body( wp_json_encode( $params ) );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertErrorResponse( 'test_rest_premade_error', $response, 418 );
|
||||
}
|
||||
|
||||
public function return_premade_error() {
|
||||
return new WP_Error( 'test_rest_premade_error', "I'm sorry, I thought he was a party robot.", array( 'status' => 418 ) );
|
||||
}
|
||||
|
||||
public function test_create_comment_missing_required_author_name() {
|
||||
add_filter( 'rest_allow_anonymous_comments', '__return_true' );
|
||||
update_option( 'require_name_email', 1 );
|
||||
|
||||
Reference in New Issue
Block a user