diff --git a/includes/bp-messages/classes/class-bp-rest-messages-endpoint.php b/includes/bp-messages/classes/class-bp-rest-messages-endpoint.php index cc49f823..1a225e42 100644 --- a/includes/bp-messages/classes/class-bp-rest-messages-endpoint.php +++ b/includes/bp-messages/classes/class-bp-rest-messages-endpoint.php @@ -345,20 +345,20 @@ public function get_item_permissions_check( $request ) { * @return WP_REST_Response|WP_Error */ public function create_item( $request ) { - // Setting context. - $request->set_param( 'context', 'edit' ); + $create_args = $this->prepare_item_for_database( $request ); + + // Let's return the original error if possible. + $create_args->error_type = 'wp_error'; // Create the message or the reply. - $thread_id = messages_new_message( $this->prepare_item_for_database( $request ) ); + $thread_id = messages_new_message( $create_args ); // Validate it created a Thread or was added to it. - if ( false === $thread_id ) { + if ( $thread_id instanceof WP_Error ) { return new WP_Error( 'bp_rest_messages_create_failed', - __( 'There was an error trying to create the message.', 'buddypress' ), - array( - 'status' => 500, - ) + $thread_id->get_error_message(), + array( 'status' => 500 ) ); } @@ -804,10 +804,10 @@ protected function prepare_item_for_database( $request ) { $prepared_thread->sender_id = bp_loggedin_user_id(); } - if ( ! empty( $schema['properties']['message'] ) && ! empty( $request->get_param( 'message' ) ) ) { - $prepared_thread->content = $request->get_param( 'message' ); - } elseif ( ! empty( $thread->message ) ) { + if ( ! empty( $thread->message ) ) { $prepared_thread->message = $thread->message; + } elseif ( ! empty( $schema['properties']['message'] ) ) { + $prepared_thread->content = $request->get_param('message'); } if ( ! empty( $schema['properties']['subject'] ) && ! empty( $request->get_param( 'subject' ) ) ) { @@ -1133,6 +1133,7 @@ public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CRE $args['message']['type'] = 'string'; $args['message']['description'] = __( 'Content of the Message to add to the Thread.', 'buddypress' ); + // Edit recipients properties. $args['recipients']['required'] = true; $args['recipients']['items'] = array( 'type' => 'integer' ); diff --git a/tests/testcases/messages/test-controller.php b/tests/testcases/messages/test-controller.php index c658df71..300cc4fb 100644 --- a/tests/testcases/messages/test-controller.php +++ b/tests/testcases/messages/test-controller.php @@ -416,9 +416,12 @@ public function test_create_item_user_is_not_logged_in() { } /** + * @dataProvider provider_create_item_with_empty_content_options + * + * @ticket BP9175 * @group create_item */ - public function test_create_item_with_no_content() { + public function test_create_item_with_empty_content_options( $content ) { $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); @@ -426,7 +429,31 @@ public function test_create_item_with_no_content() { array( 'sender_id' => $this->user, 'recipients' => [ static::factory()->user->create() ], - 'subject' => 'Foo', + 'subject' => 'A new message', + 'message' => $content, + ) + ); + + $response = $this->server->dispatch( $request ); + + $this->assertErrorResponse( 'bp_rest_messages_create_failed', $response, 500 ); + $this->assertSame( 'Your message was not sent. Please enter some content.', $response->get_data()['message'] ); + } + + /** + * @ticket BP9175 + * @group create_item + */ + public function test_create_item_with_null_content() { + $this->bp::set_current_user( $this->user ); + + $request = new WP_REST_Request( 'POST', $this->endpoint_url ); + $request->set_query_params( + array( + 'sender_id' => $this->user, + 'recipients' => [ static::factory()->user->create() ], + 'subject' => 'A new message', + 'message' => null, ) ); @@ -437,10 +464,62 @@ public function test_create_item_with_no_content() { ); } + /** + * @dataProvider provider_create_item_irregular_content_options + * + * @ticket BP9175 + * @group create_item + */ + public function test_create_item_with_irregular_content_options( $content ) { + $this->bp::set_current_user( $this->user ); + + $request = new WP_REST_Request( 'POST', $this->endpoint_url ); + $request->set_query_params( + array( + 'sender_id' => $this->user, + 'recipients' => [ static::factory()->user->create() ], + 'subject' => 'A new message', + 'message' => $content, + ) + ); + + $response = $this->server->dispatch( $request ); + + $this->assertEquals( 200, $response->get_status() ); + } + + /** + * Provider for the test_create_item_with_empty_content_options() test. + * + * @return array + */ + public function provider_create_item_with_empty_content_options() { + return array( + array( '' ), + array( "" ), + array( false ), + array( 0 ), // '0' is a valid message content. + array( array() ), + ); + } + + /** + * Provider for the test_create_item_with_irregular_content_options() test. + * + * @return array + */ + public function provider_create_item_irregular_content_options() { + return array( + array( '0' ), + array( '00' ), + array( 'false' ), + ); + } + /** * @group create_item */ - public function test_create_item_with_no_receipts() { + public function test_create_item_with_no_recipients() { $this->bp::set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url );