From 455149485ada49cf4796856041a84c6ec590e2e1 Mon Sep 17 00:00:00 2001 From: Renato Alves <19148962+renatonascalves@users.noreply.github.com> Date: Wed, 5 Jun 2024 23:43:33 -0300 Subject: [PATCH] Messages: the `raw` data in the `messages` field requires the `edit` context --- .../class-bp-rest-messages-endpoint.php | 5 +- tests/testcases/messages/test-controller.php | 58 ++++++++++++++++++- 2 files changed, 59 insertions(+), 4 deletions(-) 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 e20d2560..cc49f823 100644 --- a/includes/bp-messages/classes/class-bp-rest-messages-endpoint.php +++ b/includes/bp-messages/classes/class-bp-rest-messages-endpoint.php @@ -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. diff --git a/tests/testcases/messages/test-controller.php b/tests/testcases/messages/test-controller.php index f884c201..c658df71 100644 --- a/tests/testcases/messages/test-controller.php +++ b/tests/testcases/messages/test-controller.php @@ -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'] ); } /**