Skip to content

Commit

Permalink
Messages: the raw data in the messages field requires the edit
Browse files Browse the repository at this point in the history
…context
  • Loading branch information
renatonascalves committed Jun 6, 2024
1 parent 89ded5b commit 4551494
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -868,8 +868,9 @@ public function prepare_message_for_response( $message, $request ) {
$data['is_starred'] = bp_messages_is_message_starred( $data['id'], $user_id );
}

// Add REST Fields (BP Messages meta) data.
$data = $this->add_additional_fields_to_object( $data, $request );
$context = ! empty( $request->get_param( 'context' ) ) ? $request->get_param( 'context' ) : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );

/**
* Filter a message value returned from the API.
Expand Down
58 changes: 56 additions & 2 deletions tests/testcases/messages/test-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,64 @@ public function test_get_item_admin_access() {
$this->assertEquals( 200, $response->get_status() );

$all_data = $response->get_data();
$this->assertNotEmpty( $all_data );

$data = current( $all_data );
$this->check_thread_data( $this->endpoint->get_thread_object( $data['id'], $u2 ), $data );

$this->assertFalse( isset( $data['message']['raw'] ) );
$this->assertFalse( isset( $data['excerpt']['raw'] ) );
$this->assertFalse( isset( $data['subject']['raw'] ) );
$this->assertSame( 'Foo', $data['subject']['rendered'] );

$message = $data['messages'][0];

$this->assertSame( $m->id, $message['id'] );
$this->assertFalse( isset( $message['message']['raw'] ) );
$this->assertFalse( isset( $message['subject']['raw'] ) );

$this->assertTrue( isset( $message['message']['rendered'] ) );
$this->assertTrue( isset( $message['subject']['rendered'] ) );
}

/**
* @group get_item
*/
public function test_get_item_with_edit_context() {
$u1 = static::factory()->user->create();
$u2 = static::factory()->user->create();
$m = $this->bp::factory()->message->create_and_get( array(
'sender_id' => $u1,
'recipients' => array( $u2 ),
'subject' => 'Foo',
) );

$this->bp::set_current_user( $this->user );

$request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m->thread_id );

$request->set_param( 'context', 'edit' );
$request->set_param( 'user_id', $u2 );
$response = $this->server->dispatch( $request );

$this->assertEquals( 200, $response->get_status() );

$all_data = $response->get_data();

$data = current( $all_data );

$this->assertTrue( isset( $data['message']['raw'] ) );
$this->assertTrue( isset( $data['excerpt']['raw'] ) );
$this->assertTrue( isset( $data['subject']['raw'] ) );
$this->assertSame( 'Foo', $data['subject']['raw'] );

$message = $data['messages'][0];

$this->assertSame( $m->id, $message['id'] );
$this->assertTrue( isset( $message['message']['raw'] ) );
$this->assertTrue( isset( $message['subject']['raw'] ) );

$this->assertTrue( isset( $message['message']['rendered'] ) );
$this->assertTrue( isset( $message['subject']['rendered'] ) );
$this->assertSame( 'Foo', $message['subject']['raw'] );
}

/**
Expand Down

0 comments on commit 4551494

Please sign in to comment.