Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to send "0" in messages through REST API #511

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions includes/bp-messages/classes/class-bp-rest-messages-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API consumer should be providing this.

$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 )
);
}

Expand Down Expand Up @@ -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' ) ) ) {
Expand Down Expand Up @@ -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' );
Expand Down
85 changes: 82 additions & 3 deletions tests/testcases/messages/test-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,17 +416,44 @@ 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 );
$request->set_query_params(
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,
)
);

Expand All @@ -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 );
Expand Down
Loading