Skip to content

Commit

Permalink
Add additional fields support to the BP Messages endpoint
Browse files Browse the repository at this point in the history
Add 3 unit tests including one testing a reply added to a thread
  • Loading branch information
imath committed Jul 4, 2019
1 parent cc2d9f5 commit 673252e
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 9 deletions.
48 changes: 40 additions & 8 deletions includes/bp-messages/classes/class-bp-rest-messages-endpoint.php
Expand Up @@ -69,7 +69,7 @@ public function register_routes() {
),
'recipients' => array(
'description' => __( 'Recipients of the message/reply.', 'buddypress' ),
'required' => true,
'required' => false,
'type' => 'array',
'items' => array( 'type' => 'integer' ),
'sanitize_callback' => 'wp_parse_id_list',
Expand Down Expand Up @@ -348,6 +348,16 @@ public function get_item_permissions_check( $request ) {
public function create_item( $request ) {
$prepared_thread = $this->prepare_item_for_database( $request );

if ( ! isset( $prepared_thread->recipients ) || ! $prepared_thread->recipients ) {
return new WP_Error(
'bp_rest_messages_missing_recipients',
__( 'Please provide some recipients for your message or reply.', 'buddypress' ),
array(
'status' => 400,
)
);
}

// Create message.
$thread_id = messages_new_message( $prepared_thread );

Expand All @@ -361,7 +371,15 @@ public function create_item( $request ) {
);
}

$thread = $this->get_thread_object( $thread_id );
// Make sure to get the newest message to update REST Additional fields.
$thread = $this->get_thread_object( $thread_id );
$last_message = wp_list_filter( $thread->messages, array( 'id' => $thread->last_message_id ) );
$last_message = reset( $last_message );
$fields_update = $this->update_additional_fields_for_object( $last_message, $request );

if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}

$retval = array(
$this->prepare_response_for_collection(
Expand Down Expand Up @@ -612,7 +630,15 @@ public function prepare_message_for_response( $message, $request ) {
// Add REST Fields (BP Messages meta) data.
$data = $this->add_additional_fields_to_object( $data, $request );

return apply_filters( 'bp_rest_messages_prepare_message_for_response', $data, $request );
/**
* Filter a message value returned from the API.
*
* @since 0.1.0
*
* @param array $data The message value for the REST response.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'bp_rest_messages_prepare_message_value', $data, $request );
}

/**
Expand Down Expand Up @@ -703,10 +729,12 @@ protected function prepare_item_for_database( $request ) {
}
}

if ( ! empty( $schema['properties']['last_sender_id'] ) && ! empty( $thread->sender_id ) ) {
// Defaults to current user.
$prepared_thread->sender_id = bp_loggedin_user_id();
if ( $request['sender_id'] ) {
$prepared_thread->sender_id = $request['sender_id'];
} elseif ( ! empty( $schema['properties']['last_sender_id'] ) && ! empty( $thread->sender_id ) ) {
$prepared_thread->sender_id = $thread->sender_id;
} else {
$prepared_thread->sender_id = bp_loggedin_user_id();
}

if ( ! empty( $schema['properties']['content'] ) && ! empty( $thread->last_message_content ) ) {
Expand All @@ -725,6 +753,8 @@ protected function prepare_item_for_database( $request ) {

if ( ! empty( $request['recipients'] ) ) {
$prepared_thread->recipients = $request['recipients'];
} elseif ( isset( $thread->recipients ) && $thread->recipients ) {
$prepared_thread->recipients = wp_parse_id_list( wp_list_pluck( $thread->recipients, 'user_id' ) );
}

/**
Expand Down Expand Up @@ -772,7 +802,7 @@ public function get_message_object( $message_id ) {
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => esc_html__( 'Thread', 'buddypress' ),
'title' => 'bp_messages',
'type' => 'object',
'properties' => array(
'id' => array(
Expand Down Expand Up @@ -898,9 +928,11 @@ public function get_item_schema() {
/**
* Filters the messages schema.
*
* @since 0.1.0
*
* @param array $schema The endpoint schema.
*/
return apply_filters( 'bp_rest_messages_schema', $schema );
return apply_filters( 'bp_rest_messages_schema', $this->add_additional_fields_schema( $schema ) );
}

/**
Expand Down
143 changes: 142 additions & 1 deletion tests/messages/test-controller.php
Expand Up @@ -280,7 +280,7 @@ public function test_create_item_with_no_receipts() {

$response = $this->server->dispatch( $request );

$this->assertErrorResponse( 'rest_missing_callback_param', $response, 400 );
$this->assertErrorResponse( 'bp_rest_messages_missing_recipients', $response, 400 );
}

/**
Expand Down Expand Up @@ -557,4 +557,145 @@ public function test_update_starred_remove_star() {

$this->assertFalse( $data['is_starred'] );
}

public function update_additional_field( $value, $data, $attribute ) {
return bp_messages_update_meta( $data->id, '_' . $attribute, $value );
}

public function get_additional_field( $data, $attribute ) {
return bp_messages_get_meta( $data['id'], '_' . $attribute );
}

/**
* @group additional_fields
*/
public function test_additional_fields_for_get_item() {
$registered_fields = $GLOBALS['wp_rest_additional_fields'];

bp_rest_register_field( 'messages', 'taz_field', array(
'get_callback' => array( $this, 'get_additional_field' ),
'update_callback' => array( $this, 'update_additional_field' ),
'schema' => array(
'description' => 'Message Meta Field',
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
) );

$u1 = $this->factory->user->create();
$u2 = $this->factory->user->create();

// Init a thread.
$m1 = $this->bp_factory->message->create_and_get( array(
'sender_id' => $u1,
'recipients' => array( $u2 ),
'subject' => 'Foo',
) );

$expected = 'boz_value';
bp_messages_update_meta( $m1->id, '_taz_field', $expected );
$this->bp->set_current_user( $u2 );

// GET
$request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m1->thread_id );
$request->set_param( 'context', 'view' );
$response = $this->server->dispatch( $request );

$get_data = $response->get_data();

$last_message = wp_list_filter( $get_data[0]['messages'], array( 'id' => $get_data[0]['message_id'] ) );
$last_message = reset( $last_message );
$this->assertTrue( $expected === $last_message['taz_field'] );

$GLOBALS['wp_rest_additional_fields'] = $registered_fields;
}

/**
* @group additional_fields
*/
public function test_additional_fields_for_created_thread() {
$registered_fields = $GLOBALS['wp_rest_additional_fields'];

bp_rest_register_field( 'messages', 'foo_field', array(
'get_callback' => array( $this, 'get_additional_field' ),
'update_callback' => array( $this, 'update_additional_field' ),
'schema' => array(
'description' => 'Message Meta Field',
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
) );

$u = $this->factory->user->create();
$this->bp->set_current_user( $this->user );
$expected = 'bar_value';

// POST
$request = new WP_REST_Request( 'POST', $this->endpoint_url );
$request->set_query_params(
array(
'sender_id' => $this->user,
'recipients' => [ $u ],
'subject' => 'Foo',
'content' => 'Bar',
'foo_field' => $expected,
)
);
$response = $this->server->dispatch( $request );

$create_data = $response->get_data();
$last_message = wp_list_filter( $create_data[0]['messages'], array( 'id' => $create_data[0]['message_id'] ) );
$last_message = reset( $last_message );
$this->assertTrue( $expected === $last_message['foo_field'] );

$GLOBALS['wp_rest_additional_fields'] = $registered_fields;
}

/**
* @group additional_fields
*/
public function test_additional_fields_for_created_reply() {
$registered_fields = $GLOBALS['wp_rest_additional_fields'];

bp_rest_register_field( 'messages', 'bar_field', array(
'get_callback' => array( $this, 'get_additional_field' ),
'update_callback' => array( $this, 'update_additional_field' ),
'schema' => array(
'description' => 'Message Meta Field',
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
) );

$u1 = $this->factory->user->create();
$u2 = $this->factory->user->create();

// Init a thread.
$m1 = $this->bp_factory->message->create_and_get( array(
'sender_id' => $u2,
'recipients' => array( $u1 ),
'subject' => 'Foo',
) );

$this->bp->set_current_user( $u1 );
$expected = 'foo_value';

// POST a reply.
$request = new WP_REST_Request( 'POST', $this->endpoint_url );
$request->set_query_params(
array(
'thread_id' => $m1->thread_id,
'content' => 'Taz',
'bar_field' => $expected,
)
);
$response = $this->server->dispatch( $request );

$create_data = $response->get_data();
$last_message = wp_list_filter( $create_data[0]['messages'], array( 'id' => $create_data[0]['message_id'] ) );
$last_message = reset( $last_message );
$this->assertTrue( $expected === $last_message['bar_field'] );

$GLOBALS['wp_rest_additional_fields'] = $registered_fields;
}
}

0 comments on commit 673252e

Please sign in to comment.