server->get_routes(); $endpoint = $this->endpoint_url; // Main. $this->assertArrayHasKey( $endpoint, $routes ); $this->assertCount( 2, $routes[ $endpoint ] ); // Single. $single_endpoint = $endpoint . '/(?P[\d]+)'; $this->assertArrayHasKey( $single_endpoint, $routes ); $this->assertCount( 3, $routes[ $single_endpoint ] ); // Starred. $starred_endpoint = $endpoint . '/' . bp_get_messages_starred_slug() . '/(?P[\d]+)'; $this->assertArrayHasKey( $starred_endpoint, $routes ); $this->assertCount( 1, $routes[ $starred_endpoint ] ); } /** * @group get_items */ public function test_get_items() { $u1 = static::factory()->user->create(); $u2 = static::factory()->user->create(); $this->bp::factory()->message->create( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); $this->bp::factory()->message->create( array( 'sender_id' => $u2, 'recipients' => array( $u1 ), 'subject' => 'Fooo', ) ); wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); $request->set_param( 'user_id', $u1 ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertNotEmpty( $data ); $a_ids = wp_list_pluck( $data, 'id' ); $this->assertCount( 1, $a_ids ); $this->assertCount( 1, $data[0]['messages'] ); $this->check_thread_data( $this->endpoint->get_thread_object( $data[0]['id'], $u1 ), $data[0] ); } /** * @group get_items */ public function test_get_items_and_paginate_messages_recipients() { $u1 = static::factory()->user->create(); $u2 = static::factory()->user->create(); $u3 = static::factory()->user->create(); $thread = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); $this->bp::factory()->message->create( array( 'thread_id' => $thread->thread_id, 'sender_id' => $u2, 'recipients' => array( $u1 ), 'subject' => 'Fooo', ) ); $thread2 = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u3 ), 'subject' => 'Foo', ) ); $this->bp::factory()->message->create( array( 'thread_id' => $thread2->thread_id, 'sender_id' => $u3, 'recipients' => array( $u1 ), 'subject' => 'Fooo', ) ); wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); $request->set_query_params( array( 'user_id' => $u1, 'messages_per_page' => 1, 'recipients_per_page' => 1, ) ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $all_data = $response->get_data(); $this->assertNotEmpty( $all_data ); // Messages. $this->assertCount( 1, $all_data[0]['messages'] ); $this->assertCount( 1, $all_data[1]['messages'] ); // Recipients. $this->assertCount( 1, $all_data[0]['recipients'] ); $this->assertCount( 1, $all_data[1]['recipients'] ); } /** * @BP9157 * @group get_items */ public function test_filter_threads_by_recipients() { $u1 = static::factory()->user->create(); $u2 = static::factory()->user->create(); $u3 = static::factory()->user->create(); $thread = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'First Thread', ) ); $this->bp::factory()->message->create( array( 'thread_id' => $thread->thread_id, 'sender_id' => $u2, 'recipients' => array( $u1 ), ) ); $thread2 = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u3 ), 'subject' => 'Second Thread', ) ); $this->bp::factory()->message->create( array( 'thread_id' => $thread2->thread_id, 'sender_id' => $u3, 'recipients' => array( $u1 ), ) ); wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); $request->set_param( 'user_id', $u1 ); $request->set_param( 'includes', array( $u3 ) ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertNotEmpty( $data ); $a_ids = wp_list_pluck( $data, 'id' ); $this->assertCount( 1, $a_ids ); $this->assertSame( $thread2->thread_id, $a_ids[0] ); } /** * @group get_item */ public function test_get_item() { $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', 'content' => 'Content', ) ); wp_set_current_user( $u2 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m->thread_id ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertNotEmpty( $data ); $this->check_thread_data( $this->endpoint->get_thread_object( $data['id'], $u2 ), $data ); } /** * @group get_item */ public function test_get_thread_messages_paginated() { $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', 'content' => 'Content', ) ); // create several messages. $this->bp::factory()->message->create_many( 10, array( 'thread_id' => $m->thread_id, 'sender_id' => $u2, 'recipients' => array( $u1 ), 'content' => 'Bar', ) ); // create a reply. $message_id = $this->bp::factory()->message->create( array( 'sender_id' => $u2, 'thread_id' => $m->thread_id, 'recipients' => array( $u1 ), 'content' => 'Bar', ) ); wp_set_current_user( $u2 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m->thread_id ); $request->set_param( 'context', 'view' ); $request->set_param( 'messages_per_page', 1 ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $headers = $response->get_headers(); $this->assertSame( 12, $headers['X-WP-Total'] ); $this->assertSame( 12, $headers['X-WP-TotalPages'] ); $thread = $response->get_data(); $this->assertCount( 1, $thread['messages'] ); $this->assertSame( $m->thread_id, $thread['messages'][0]['thread_id'] ); $this->assertSame( $message_id, $thread['messages'][0]['id'] ); } /** * @group get_item */ public function test_get_item_admin_access() { $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', ) ); wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m->thread_id ); $request->set_param( 'context', 'view' ); $request->set_param( 'user_id', $u2 ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $data = $response->get_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_prevent_counterfeit_user_id() { $u1 = static::factory()->user->create(); $u2 = static::factory()->user->create(); $u3 = static::factory()->user->create(); $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); wp_set_current_user( $u3 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m->thread_id ); $request->set_param( 'context', 'view' ); $request->set_param( 'user_id', $u2 ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() ); $this->assertSame( 403, $response->get_status() ); } /** * @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', ) ); wp_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->assertSame( 200, $response->get_status() ); $data = $response->get_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'] ); } /** * @group get_item */ public function test_get_item_user_with_no_access() { $u1 = static::factory()->user->create(); $u2 = static::factory()->user->create(); $u3 = static::factory()->user->create(); $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); wp_set_current_user( $u3 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m->thread_id ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() ); $this->assertSame( 403, $response->get_status() ); } /** * @ticket BP9160 * @group get_item */ public function test_get_thread_deleted_messages() { $u1 = static::factory()->user->create(); $deleted_user = static::factory()->user->create(); $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $deleted_user, 'recipients' => array( $u1 ), 'subject' => 'Foo', 'content' => 'Content', ) ); $this->bp::factory()->message->create( array( 'thread_id' => $m->thread_id, 'sender_id' => $u1, 'recipients' => array( $deleted_user ), 'content' => 'Bar', ) ); // Delete user. if ( is_multisite() ) { wpmu_delete_user( $deleted_user ); } else { wp_delete_user( $deleted_user ); } wp_set_current_user( $u1 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m->thread_id ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $all_data = $response->get_data(); $deleted_recipient = array_values( wp_filter_object_list( $all_data['recipients'], array( 'user_id' => $deleted_user ), 'AND', 'is_deleted' ) ); $this->assertSame( $deleted_user, $all_data['last_sender_id'] ); $this->assertContains( $deleted_user, $all_data['sender_ids'] ); $this->assertStringContainsString( '

[deleted]

', $all_data['message']['rendered'] ); $this->assertStringContainsString( '[deleted]', $all_data['excerpt']['rendered'] ); $this->assertTrue( $deleted_recipient[0] ); $this->assertCount( 2, $all_data['recipients'] ); } /** * @group get_item */ public function test_get_item_user_is_not_logged_in() { $u1 = static::factory()->user->create(); $u2 = static::factory()->user->create(); static::factory()->user->create(); $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m->thread_id ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() ); $this->assertSame( 401, $response->get_status() ); } /** * @group get_item */ public function test_get_item_invalid_id() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_invalid_id', $response, 404 ); } /** * @group create_item */ public function test_create_item() { $u = static::factory()->user->create(); wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_param( 'context', 'edit' ); $request->set_query_params( array( 'sender_id' => $this->user, 'recipients' => array( $u ), 'subject' => 'Foo', 'message' => 'Content', ) ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertNotEmpty( $data ); $this->check_thread_data( $this->endpoint->get_thread_object( $data['id'] ), $data ); } /** * @group create_item */ public function test_create_item_user_is_not_logged_in() { $u = static::factory()->user->create(); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_param( 'context', 'edit' ); $request->set_query_params( array( 'sender_id' => $this->user, 'recipients' => array( $u ), 'subject' => 'Foo', 'message' => 'Content', ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() ); } /** * @dataProvider provider_create_item_with_empty_content_options * * @ticket BP9175 * @group create_item * * @param mixed $content Empty message content to test. */ public function test_create_item_with_empty_content_options( $content ) { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_param( 'context', 'edit' ); $request->set_query_params( array( 'sender_id' => $this->user, 'recipients' => array( static::factory()->user->create() ), '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() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_param( 'context', 'edit' ); $request->set_query_params( array( 'sender_id' => $this->user, 'recipients' => array( static::factory()->user->create() ), 'subject' => 'A new message', 'message' => null, ) ); $this->assertErrorResponse( 'rest_missing_callback_param', $this->server->dispatch( $request ), 400 ); } /** * @dataProvider provider_create_item_irregular_content_options * * @ticket BP9175 * @group create_item * * @param string $content Irregular message content to test. */ public function test_create_item_with_irregular_content_options( $content ) { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_param( 'context', 'edit' ); $request->set_query_params( array( 'sender_id' => $this->user, 'recipients' => array( static::factory()->user->create() ), 'subject' => 'A new message', 'message' => $content, ) ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); } /** * Provider for the test_create_item_with_empty_content_options() test. * * @return array */ public static 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 static 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_recipients() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_param( 'context', 'edit' ); $request->set_query_params( array( 'sender_id' => $this->user, 'subject' => 'Foo', 'message' => 'Content', ) ); $this->assertErrorResponse( 'rest_missing_callback_param', $this->server->dispatch( $request ), 400 ); } /** * @group update_item */ public function test_update_item() { $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', 'content' => 'Content', ) ); wp_set_current_user( $u2 ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $m->thread_id ) ); $request->set_param( 'context', 'edit' ); $request->set_param( 'read', true ); $request->set_param( 'user_id', $u2 ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertNotEmpty( $data ); $this->assertFalse( (bool) $data['unread_count'] ); $this->assertFalse( (bool) $data['recipients'][0]['unread_count'] ); $this->assertFalse( (bool) $data['recipients'][1]['unread_count'] ); } /** * @group update_item */ public function test_update_thread_to_unread() { $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', 'content' => 'Content', ) ); // Update to read. messages_mark_thread_read( $m->thread_id, $u2 ); wp_set_current_user( $u2 ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $m->thread_id ) ); $request->set_param( 'context', 'edit' ); $request->set_param( 'unread', true ); $request->set_param( 'user_id', $u2 ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertNotEmpty( $data ); $this->assertTrue( (bool) $data['unread_count'] ); foreach ( $data['recipients'] as $recipient ) { if ( $recipient['user_id'] === $u1 ) { $this->assertFalse( (bool) $recipient['unread_count'] ); } else { $this->assertTrue( (bool) $recipient['unread_count'] ); } } } /** * @group update_item */ public function test_update_item_user_is_not_logged_in() { $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', 'content' => 'Content', ) ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $m->thread_id ) ); $request->set_param( 'context', 'edit' ); $this->assertErrorResponse( 'bp_rest_authorization_required', $this->server->dispatch( $request ), rest_authorization_required_code() ); } /** * @group update_item */ public function test_update_item_invalid_id() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $request->set_param( 'context', 'edit' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_invalid_id', $response, 404 ); } /** * @group update_item */ public function test_update_item_with_user_with_no_access() { $u1 = static::factory()->user->create(); $u2 = static::factory()->user->create(); $u3 = static::factory()->user->create(); $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', 'content' => 'Content', ) ); wp_set_current_user( $u3 ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $m->thread_id ) ); $request->set_param( 'context', 'edit' ); $this->assertErrorResponse( 'bp_rest_authorization_required', $this->server->dispatch( $request ), rest_authorization_required_code() ); } /** * @group update_item */ public function test_update_item_prevent_counterfeit_user_id() { $u1 = static::factory()->user->create(); $u2 = static::factory()->user->create(); $u3 = static::factory()->user->create(); $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); wp_set_current_user( $u3 ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $m->thread_id ) ); $request->set_param( 'user_id', $u2 ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() ); $this->assertSame( 403, $response->get_status() ); } /** * @group delete_item */ public function test_delete_item() { $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', 'content' => 'Content', ) ); wp_set_current_user( $u2 ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $m->thread_id ) ); $request->set_param( 'context', 'edit' ); $request->set_query_params( array( 'user_id' => $u2 ) ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertNotEmpty( $data ); $this->assertTrue( $data['deleted'] ); $this->assertSame( 'Foo', $data['previous']['subject']['rendered'] ); } /** * @group delete_item */ public function test_delete_item_admin_access() { $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', ) ); wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . '/' . $m->thread_id ); $request->set_param( 'context', 'edit' ); $request->set_query_params( array( 'user_id' => $u2 ) ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertNotEmpty( $data ); $this->assertTrue( $data['deleted'] ); $this->assertSame( 'Foo', $data['previous']['subject']['rendered'] ); } /** * @group delete_item */ public function test_delete_item_with_user_with_no_access() { $u1 = static::factory()->user->create(); $u2 = static::factory()->user->create(); $u3 = static::factory()->user->create(); $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); wp_set_current_user( $u3 ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . '/' . $m->thread_id ); $request->set_param( 'context', 'edit' ); $this->assertErrorResponse( 'bp_rest_authorization_required', $this->server->dispatch( $request ), rest_authorization_required_code() ); } /** * @group delete_item */ public function test_delete_item_user_is_not_logged_in() { $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', ) ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . '/' . $m->thread_id ); $request->set_param( 'context', 'edit' ); $this->assertErrorResponse( 'bp_rest_authorization_required', $this->server->dispatch( $request ), rest_authorization_required_code() ); } /** * @group delete_item */ public function test_delete_item_prevent_counterfeit_user_id() { $u1 = static::factory()->user->create(); $u2 = static::factory()->user->create(); $u3 = static::factory()->user->create(); $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); wp_set_current_user( $u3 ); $request = new WP_REST_Request( 'DELETE', $this->endpoint_url . '/' . $m->thread_id ); $request->set_param( 'user_id', $u2 ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() ); $this->assertSame( 403, $response->get_status() ); } /** * @group starred */ public function test_get_starred_items() { $u1 = static::factory()->user->create(); $u2 = static::factory()->user->create(); // Init a thread. $m1 = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); // Init another thread. $m2_id = $this->bp::factory()->message->create( array( 'sender_id' => $u2, 'recipients' => array( $u1 ), 'subject' => 'Taz', ) ); // Create a reply. $r1 = $this->bp::factory()->message->create_and_get( array( 'thread_id' => $m1->thread_id, 'sender_id' => $u2, 'recipients' => array( $u1 ), 'content' => 'Bar', ) ); wp_set_current_user( $u1 ); bp_messages_star_set_action( array( 'user_id' => $u1, 'message_id' => $r1->id, ) ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); $request->set_query_params( array( 'user_id' => $u1, 'box' => 'starred', ) ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); $threads = wp_list_pluck( $data, 'id' ); $this->assertNotContains( $m2_id, $threads ); $this->assertContains( $m1->thread_id, $threads ); $result = reset( $data ); $this->assertNotEmpty( $result['starred_message_ids'] ); $this->assertContains( $r1->id, $result['starred_message_ids'] ); } /** * @group starred */ public function test_update_starred_add_star() { $u1 = static::factory()->user->create(); $u2 = static::factory()->user->create(); // Init a thread. $m1 = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); // Create a reply. $r1 = $this->bp::factory()->message->create_and_get( array( 'thread_id' => $m1->thread_id, 'sender_id' => $u2, 'recipients' => array( $u1 ), 'content' => 'Bar', ) ); wp_set_current_user( $u2 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . bp_get_messages_starred_slug() . '/' . $r1->id ); $request->set_param( 'context', 'edit' ); $request->add_header( 'content-type', 'application/json' ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertNotEmpty( $data ); $this->assertTrue( $data['is_starred'] ); } /** * @group starred */ public function test_update_starred_remove_star() { $u1 = static::factory()->user->create(); $u2 = static::factory()->user->create(); // Init a thread. $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); bp_messages_star_set_action( array( 'user_id' => $u2, 'message_id' => $m->id, ) ); wp_set_current_user( $u2 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . bp_get_messages_starred_slug() . '/' . $m->id ); $request->set_param( 'context', 'edit' ); $request->add_header( 'content-type', 'application/json' ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertNotEmpty( $data ); $this->assertFalse( $data['is_starred'] ); } /** * @group starred */ public function test_update_starred_user_is_not_logged_in() { $u1 = static::factory()->user->create(); $u2 = static::factory()->user->create(); // Init a thread. $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . bp_get_messages_starred_slug() . '/' . $m->id ); $request->set_param( 'context', 'edit' ); $this->assertErrorResponse( 'bp_rest_authorization_required', $this->server->dispatch( $request ), rest_authorization_required_code() ); } /** * @group starred */ public function test_update_starred_user_with_no_access() { $u1 = static::factory()->user->create(); $u2 = static::factory()->user->create(); $u3 = static::factory()->user->create(); // Init a thread. $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Foo', ) ); wp_set_current_user( $u3 ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . bp_get_messages_starred_slug() . '/' . $m->id ); $request->set_param( 'context', 'edit' ); $this->assertErrorResponse( 'bp_rest_authorization_required', $this->server->dispatch( $request ), rest_authorization_required_code() ); } /** * @group starred */ public function test_update_starred_using_invalid_id() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . bp_get_messages_starred_slug() . '/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $request->set_param( 'context', 'edit' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_invalid_id', $response, 404 ); } 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 = static::factory()->user->create(); $u2 = static::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 ); wp_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(); $this->assertNotEmpty( $get_data ); $last_message = wp_list_filter( $get_data['messages'], array( 'id' => $get_data['message_id'] ) ); $last_message = reset( $last_message ); $this->assertSame( $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 = static::factory()->user->create(); wp_set_current_user( $this->user ); $expected = 'bar_value'; // POST $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_param( 'context', 'edit' ); $request->set_query_params( array( 'sender_id' => $this->user, 'recipients' => array( $u ), 'subject' => 'Foo', 'message' => 'Bar', 'foo_field' => $expected, ) ); $response = $this->server->dispatch( $request ); $create_data = $response->get_data(); $this->assertNotEmpty( $create_data ); $last_message = wp_list_filter( $create_data['messages'], array( 'id' => $create_data['message_id'] ) ); $last_message = reset( $last_message ); $this->assertSame( $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 = static::factory()->user->create(); $u2 = static::factory()->user->create(); // Init a thread. $m1 = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u2, 'recipients' => array( $u1 ), 'subject' => 'Foo', ) ); wp_set_current_user( $u1 ); $expected = 'foo_value'; // POST a reply. $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_param( 'context', 'edit' ); $request->set_query_params( array( 'id' => $m1->thread_id, 'sender_id' => $u1, 'recipients' => array( $u2 ), 'message' => 'Taz', 'bar_field' => $expected, ) ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $create_data = $response->get_data(); $this->assertNotEmpty( $create_data ); $last_message = wp_list_filter( $create_data['messages'], array( 'id' => $create_data['message_id'] ) ); $last_message = reset( $last_message ); $this->assertSame( $expected, $last_message['bar_field'] ); $GLOBALS['wp_rest_additional_fields'] = $registered_fields; } /** * @group additional_fields */ public function test_additional_fields_for_last_message_updated() { $registered_fields = $GLOBALS['wp_rest_additional_fields']; bp_rest_register_field( 'messages', 'boz_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 = static::factory()->user->create(); $u2 = static::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::factory()->message->create_and_get( array( 'thread_id' => $m1->thread_id, 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Bar', ) ); $this->bp::factory()->message->create_and_get( array( 'thread_id' => $m1->thread_id, 'sender_id' => $u2, 'recipients' => array( $u1 ), 'subject' => 'Taz', ) ); wp_set_current_user( $u2 ); $expected = 'taz_value'; // Update the last message. $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . $m1->thread_id ); $request->set_param( 'context', 'edit' ); $request->set_query_params( array( 'boz_field' => $expected ) ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $update_data = $response->get_data(); $this->assertNotEmpty( $update_data ); $last_message = wp_list_filter( $update_data['messages'], array( 'id' => $update_data['message_id'] ) ); $last_message = reset( $last_message ); $this->assertSame( $expected, $last_message['boz_field'] ); $GLOBALS['wp_rest_additional_fields'] = $registered_fields; } /** * @group additional_fields */ public function test_additional_fields_for_specific_message_updated() { $registered_fields = $GLOBALS['wp_rest_additional_fields']; bp_rest_register_field( 'messages', 'top_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 = static::factory()->user->create(); $u2 = static::factory()->user->create(); // Init a thread. $m1 = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u2, 'recipients' => array( $u1 ), 'subject' => 'Top', ) ); $r1 = $this->bp::factory()->message->create_and_get( array( 'thread_id' => $m1->thread_id, 'sender_id' => $u1, 'recipients' => array( $u2 ), 'subject' => 'Up', ) ); $r1 = $this->bp::factory()->message->create_and_get( array( 'thread_id' => $m1->thread_id, 'sender_id' => $u2, 'recipients' => array( $u1 ), 'subject' => 'Upper', ) ); wp_set_current_user( $u2 ); $expected = 'up_value'; // Update the last message. $request = new WP_REST_Request( 'PUT', $this->endpoint_url . '/' . $m1->thread_id ); $request->set_param( 'context', 'edit' ); $request->set_query_params( array( 'message_id' => $r1->id, 'top_field' => $expected, ) ); $response = $this->server->dispatch( $request ); $update_data = $response->get_data(); $this->assertNotEmpty( $update_data ); $specific_message = wp_list_filter( $update_data['messages'], array( 'id' => $r1->id ) ); $specific_message = reset( $specific_message ); $this->assertSame( $expected, $specific_message['top_field'] ); $GLOBALS['wp_rest_additional_fields'] = $registered_fields; } /** * @group prepare_recipient_for_response */ public function test_prepare_prepare_recipient_for_response() { $u1 = static::factory()->user->create(); $u2 = static::factory()->user->create(); $u3 = static::factory()->user->create(); $m = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $u1, 'recipients' => array( $u2, $u3 ), 'subject' => 'Foo', 'content' => 'Content', ) ); wp_set_current_user( $u2 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m->thread_id ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $get_data = $response->get_data(); $this->assertNotEmpty( $get_data ); $recipients = $get_data['recipients']; foreach ( $recipients as $recipient ) { $user_id = $recipient['user_id']; $this->assertSame( esc_url( bp_members_get_user_url( $user_id ) ), $recipient['user_link'] ); foreach ( array( 'full', 'thumb' ) as $type ) { $expected['user_avatars'][ $type ] = bp_core_fetch_avatar( array( 'item_id' => $user_id, 'html' => false, 'type' => $type, ) ); $this->assertSame( $expected['user_avatars'][ $type ], $recipient['user_avatars'][ $type ] ); } } } /** * @group get_item */ public function test_prepare_item() { global $wp_rest_additional_fields; $original_fields = $wp_rest_additional_fields; wp_set_current_user( $this->user ); $recipient = $this->bp::factory()->user->create(); $message = $this->bp::factory()->message->create_and_get( array( 'sender_id' => $this->user, 'recipients' => array( $recipient ), 'subject' => 'Thread subject', 'content' => 'Thread message', 'date_sent' => '2020-01-02 03:04:05', ) ); $thread = $this->endpoint->get_thread_object( $message->thread_id, $this->user ); register_rest_field( 'bp_messages', 'test_edit_field', array( 'get_callback' => static function () { return 'Additional value'; }, 'schema' => array( 'type' => 'string', 'context' => array( 'edit' ), ), ) ); try { foreach ( array( null, 'view', 'edit' ) as $context ) { $request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $thread->thread_id ); if ( null !== $context ) { $request->set_param( 'context', $context ); } $response = $this->endpoint->prepare_item_for_response( $thread, $request ); $this->assertInstanceOf( 'WP_REST_Response', $response ); $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->check_thread_data( $thread, $data ); $this->assertSame( '2020-01-02T03:04:05', $data['date_gmt'] ); $this->assertCount( 1, $data['messages'] ); $this->assertSame( $thread->last_message_id, $data['messages'][0]['id'] ); $this->assertEqualSets( array( $this->user, $recipient ), wp_list_pluck( $data['recipients'], 'user_id' ) ); $this->assertArrayHasKey( 'rendered', $data['messages'][0]['message'] ); if ( 'edit' === $context ) { $this->assertSame( 'Thread subject', $data['subject']['raw'] ); $this->assertSame( 'Thread message', $data['message']['raw'] ); $this->assertSame( 'Thread message', $data['messages'][0]['message']['raw'] ); } else { $this->assertArrayNotHasKey( 'raw', $data['subject'] ); $this->assertArrayNotHasKey( 'raw', $data['message'] ); $this->assertArrayNotHasKey( 'raw', $data['messages'][0]['message'] ); } $links = $response->get_links(); $this->assertSame( rest_url( $this->endpoint_url . '/' . $thread->thread_id ), $links['self'][0]['href'] ); $this->assertSame( rest_url( $this->endpoint_url . '/' ), $links['collection'][0]['href'] ); if ( 'edit' === $context ) { $this->assertSame( 'Additional value', $data['test_edit_field'] ); } else { $this->assertArrayNotHasKey( 'test_edit_field', $data ); } } } finally { $wp_rest_additional_fields = $original_fields; } } protected function check_thread_data( $thread, $data ) { $this->assertSame( $thread->thread_id, $data['id'] ); $this->assertSame( $thread->last_message_id, $data['message_id'] ); $this->assertSame( $thread->last_sender_id, $data['last_sender_id'] ); $this->assertSame( apply_filters( 'bp_get_message_thread_subject', $thread->last_message_subject ), $data['subject']['rendered'] ); $this->assertSame( apply_filters( 'bp_get_message_thread_content', $thread->last_message_content ), $data['message']['rendered'] ); $this->assertSame( bp_rest_prepare_date_response( $thread->last_message_date, get_date_from_gmt( $thread->last_message_date ) ), $data['date'] ); $this->assertSame( bp_rest_prepare_date_response( $thread->last_message_date ), $data['date_gmt'] ); $this->assertSame( $thread->unread_count, $data['unread_count'] ); $this->assertSame( array_values( $thread->sender_ids ), $data['sender_ids'] ); } public function test_get_item_schema() { $request = new WP_REST_Request( 'OPTIONS', $this->endpoint_url ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); $properties = $data['schema']['properties']; $this->assertCount( 13, $properties ); $this->assertArrayHasKey( 'id', $properties ); $this->assertArrayHasKey( 'message_id', $properties ); $this->assertArrayHasKey( 'last_sender_id', $properties ); $this->assertArrayHasKey( 'subject', $properties ); $this->assertArrayHasKey( 'date', $properties ); $this->assertArrayHasKey( 'date_gmt', $properties ); $this->assertArrayHasKey( 'unread_count', $properties ); $this->assertArrayHasKey( 'sender_ids', $properties ); $this->assertArrayHasKey( 'messages', $properties ); $this->assertArrayHasKey( 'starred_message_ids', $properties ); } public function test_context_param() { // Collection. $request = new WP_REST_Request( 'OPTIONS', $this->endpoint_url ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] ); $this->assertSame( array( 'view', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); } }