server->get_routes(); // Main. $this->assertArrayHasKey( $this->endpoint_url, $routes ); $this->assertCount( 2, $routes[ $this->endpoint_url ] ); } /** * @group get_items */ public function test_get_items() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $all_data = $response->get_data(); $this->assertNotEmpty( $all_data ); $this->assertCount( 10, $all_data ); $component = $this->endpoint->get_component_info( $all_data[0]['name'] ); $this->check_component_data( $component, $all_data[0] ); } /** * @group get_items */ public function test_get_items_paginated() { 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( 'per_page' => 5 ) ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $headers = $response->get_headers(); $this->assertSame( 10, $headers['X-WP-Total'] ); $this->assertSame( 2, $headers['X-WP-TotalPages'] ); $all_data = $response->get_data(); $this->assertNotEmpty( $all_data ); $this->assertCount( 10, $all_data ); } /** * @group get_items */ public function test_get_items_invalid_status() { 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( 'status' => 'another', ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); } /** * @group get_items */ public function test_get_items_user_is_not_logged_in() { $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, 401 ); } /** * @group get_items */ public function test_get_items_without_permission() { $u = static::factory()->user->create(); wp_set_current_user( $u ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, 403 ); } /** * @group get_items */ public function test_get_items_active_permission() { $u = static::factory()->user->create( array( 'role' => 'author', ) ); wp_set_current_user( $u ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); $request->set_param( 'status', 'active' ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $all_data = $response->get_data(); $this->assertNotEmpty( $all_data ); $all_features = wp_list_pluck( $all_data, 'features' ); $this->assertNotEmpty( array_filter( $all_features ) ); } /** * @group get_items */ public function test_get_items_active_component_features() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $all_data = $response->get_data(); $this->assertNotEmpty( $all_data ); $members_info = wp_list_filter( $all_data, array( 'name' => 'members' ) ); $members_info = reset( $members_info ); $this->assertTrue( $members_info['features']['avatar'] ); } /** * @group get_items */ public function test_get_items_inactive_component_features() { wp_set_current_user( $this->user ); add_filter( 'bp_is_messages_star_active', '__return_false' ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); remove_filter( 'bp_is_messages_star_active', '__return_false' ); $this->assertSame( 200, $response->get_status() ); $all_data = $response->get_data(); $this->assertNotEmpty( $all_data ); $messages_info = wp_list_filter( $all_data, array( 'name' => 'messages' ) ); $messages_info = reset( $messages_info ); $this->assertFalse( $messages_info['features']['star'] ); } public function deactivate_activity_component( $retval, $component ) { if ( 'activity' === $component ) { $retval = false; } return $retval; } /** * @group get_items */ public function test_get_items_inactive_component() { wp_set_current_user( $this->user ); add_filter( 'bp_is_active', array( $this, 'deactivate_activity_component' ), 10, 2 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); remove_filter( 'bp_is_active', array( $this, 'deactivate_activity_component' ) ); $this->assertSame( 200, $response->get_status() ); $all_data = $response->get_data(); $this->assertNotEmpty( $all_data ); $components_info = wp_list_filter( $all_data, array( 'name' => 'messages', 'title' => 'Activity Streams', ), 'or' ); $features = wp_list_pluck( $components_info, 'features', 'name' ); $this->assertTrue( $features['messages']['star'] ); $this->assertEmpty( $features['activity'] ); } /** * @group get_item */ public function test_get_item() { $this->markTestSkipped( 'This endpoint has no single-item get_item() route.' ); } /** * @group create_item */ public function test_create_item() { $this->markTestSkipped( 'This endpoint does not support creating components.' ); } /** * @group update_item */ public function test_update_item() { wp_set_current_user( $this->user ); $active_components = buddypress()->active_components; try { $request = new WP_REST_Request( 'PUT', $this->endpoint_url ); $request->set_query_params( array( 'name' => 'blogs', 'action' => 'deactivate', ) ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $all_data = $response->get_data(); $this->assertNotEmpty( $all_data ); $this->assertSame( 'inactive', $all_data['status'] ); } finally { buddypress()->active_components = $active_components; } } /** * @group update_item */ public function test_update_item_nonexistent_component() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url ); $request->set_query_params( array( 'name' => 'blogssss', 'action' => 'deactivate', ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_component_nonexistent', $response, 404 ); } /** * @group update_item */ public function test_update_item_empty_action() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url ); $request->set_query_params( array( 'name' => 'blogs', 'action' => '', ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); } /** * @group update_item */ public function test_update_item_invalid_action() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url ); $request->set_query_params( array( 'name' => 'blogs', 'action' => 'update', ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); } /** * @group update_item */ public function test_update_item_user_is_not_logged_in() { $request = new WP_REST_Request( 'PUT', $this->endpoint_url ); $request->set_query_params( array( 'name' => 'core', 'action' => 'activate', ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, 401 ); } /** * @group update_item */ public function test_update_item_without_permission() { $u = static::factory()->user->create(); wp_set_current_user( $u ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url ); $request->set_query_params( array( 'name' => 'core', 'action' => 'activate', ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, 403 ); } /** * @group update_item */ public function test_update_item_site_admin_only() { $u = static::factory()->user->create( array( 'role' => 'author', ) ); wp_set_current_user( $u ); // Snapshot so we can prove no component was toggled. $before = bp_get_option( 'bp-active-components' ); $request = new WP_REST_Request( 'PUT', $this->endpoint_url ); $request->set_query_params( array( 'name' => 'friends', 'action' => 'deactivate', ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, 403 ); // The component toggle must not have executed. $this->assertSame( $before, bp_get_option( 'bp-active-components' ) ); } /** * @group delete_item */ public function test_delete_item() { $this->markTestSkipped( 'This endpoint does not support deleting components.' ); } public function test_prepare_item() { global $wp_rest_additional_fields; $original_fields = $wp_rest_additional_fields; $component = array( 'name' => 'activity', 'status' => 'active', 'is_active' => true, 'title' => 'Activity', 'description' => 'Activity description', 'features' => null, ); register_rest_field( 'bp_components', '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 ); if ( null !== $context ) { $request->set_param( 'context', $context ); } $response = $this->endpoint->prepare_item_for_response( $component, $request ); $this->assertInstanceOf( 'WP_REST_Response', $response ); $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $expected = $component; if ( 'edit' === $context ) { $expected['test_edit_field'] = 'Additional value'; } $this->assertSame( $expected, $data ); 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_component_data( $component, $data ) { $this->assertSame( $component['name'], $data['name'] ); $this->assertSame( $component['status'], $data['status'] ); $this->assertSame( $component['title'], $data['title'] ); $this->assertSame( $component['description'], $data['description'] ); } 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( 6, $properties ); $this->assertArrayHasKey( 'name', $properties ); $this->assertArrayHasKey( 'status', $properties ); $this->assertArrayHasKey( 'title', $properties ); $this->assertArrayHasKey( 'description', $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'] ); } }