group_id = $this->bp::factory()->xprofile_group->create(); } public function test_register_routes() { $routes = $this->server->get_routes(); // Main. $this->assertArrayHasKey( $this->endpoint_url, $routes ); $this->assertCount( 2, $routes[ $this->endpoint_url ] ); // Single. $this->assertArrayHasKey( $this->endpoint_url . '/(?P[\d]+)', $routes ); $this->assertCount( 3, $routes[ $this->endpoint_url . '/(?P[\d]+)' ] ); } /** * @group get_items */ public function test_get_items() { wp_set_current_user( $this->user ); $this->bp::factory()->xprofile_group->create_many( 5 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); $this->assertNotInstanceOf( 'WP_Error', $response ); $this->assertSame( 200, $response->get_status() ); $all_data = $response->get_data(); $this->assertNotEmpty( $all_data ); foreach ( $all_data as $data ) { $field_group = $this->endpoint->get_xprofile_field_group_object( $data['id'] ); $this->check_group_data( $field_group, $data, 'view', $response->get_links() ); } } /** * @group get_items */ public function test_get_items_with_support_for_the_community_visibility() { toggle_component_visibility(); $this->bp::factory()->xprofile_group->create_many( 5 ); $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, rest_authorization_required_code() ); } /** * @group get_items */ public function test_get_items_include_groups() { wp_set_current_user( $this->user ); $g1 = $this->bp::factory()->xprofile_group->create(); $g2 = $this->bp::factory()->xprofile_group->create(); $this->bp::factory()->xprofile_group->create_many( 3 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); $request->set_param( 'include_groups', array( $g1, $g2 ) ); $response = $this->server->dispatch( $request ); $this->assertNotInstanceOf( 'WP_Error', $response ); $this->assertSame( 200, $response->get_status() ); $all_data = $response->get_data(); $this->assertNotEmpty( $all_data ); foreach ( $all_data as $data ) { $field_group = $this->endpoint->get_xprofile_field_group_object( $data['id'] ); $this->check_group_data( $field_group, $data, 'view', $response->get_links() ); } $group_ids = wp_list_pluck( $all_data, 'id' ); sort( $group_ids ); $this->assertSame( $group_ids, array( $g1, $g2 ) ); } /** * @group get_items */ public function test_get_items_publicly() { $this->bp::factory()->xprofile_group->create_many( 5 ); $request = new WP_REST_Request( 'GET', $this->endpoint_url ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); $this->assertNotInstanceOf( 'WP_Error', $response ); $this->assertSame( 200, $response->get_status() ); $all_data = $response->get_data(); $this->assertNotEmpty( $all_data ); foreach ( $all_data as $data ) { $field_group = $this->endpoint->get_xprofile_field_group_object( $data['id'] ); $this->check_group_data( $field_group, $data, 'view', $response->get_links() ); } } /** * @group get_item */ public function test_get_item() { wp_set_current_user( $this->user ); $field_group = $this->endpoint->get_xprofile_field_group_object( $this->group_id ); $this->assertSame( $this->group_id, $field_group->id ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $field_group->id ) ); $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->check_group_data( $field_group, $all_data, 'view', $response->get_links() ); } /** * @group get_item */ public function test_get_item_publicly_with_support_for_the_community_visibility() { toggle_component_visibility(); $field_group = $this->endpoint->get_xprofile_field_group_object( $this->group_id ); $this->assertSame( $this->group_id, $field_group->id ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $field_group->id ) ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() ); } /** * @group get_item */ public function test_get_item_publicly() { $field_group = $this->endpoint->get_xprofile_field_group_object( $this->group_id ); $this->assertSame( $this->group_id, $field_group->id ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $field_group->id ) ); $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->check_group_data( $field_group, $all_data, 'view', $response->get_links() ); } /** * @group get_item */ public function test_get_item_invalid_id() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', 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() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); $params = $this->set_field_group_data(); $request->set_body_params( $params ); $request->set_param( 'context', 'edit' ); $response = $this->server->dispatch( $request ); $this->check_create_field_group_response( $response ); } /** * @group create_item */ public function test_rest_create_item() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); $params = $this->set_field_group_data(); $request->set_body( wp_json_encode( $params ) ); $response = $this->server->dispatch( $request ); $this->check_create_field_group_response( $response ); } /** * @group create_item */ public function test_create_item_user_not_logged_in() { $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); $params = $this->set_field_group_data(); $request->set_body( wp_json_encode( $params ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() ); } /** * @group create_item */ public function test_create_item_user_without_permission() { $u = static::factory()->user->create(); wp_set_current_user( $u ); $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->add_header( 'content-type', 'application/json' ); $params = $this->set_field_group_data(); $request->set_body( wp_json_encode( $params ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() ); } /** * @group update_item */ public function test_update_item() { $new_name = 'Updated name'; wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $this->group_id ) ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( array( 'name' => $new_name ) ) ); $response = $this->server->dispatch( $request ); $this->assertNotInstanceOf( 'WP_Error', $response ); $this->assertSame( 200, $response->get_status() ); $object = $response->get_data(); $updated = $this->endpoint->get_xprofile_field_group_object( $object['id'] ); $this->assertSame( $new_name, $updated->name ); } /** * @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 ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_invalid_id', $response, 404 ); } /** * @group update_item */ public function test_update_item_user_not_logged_in() { $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $this->group_id ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() ); } /** * @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', sprintf( $this->endpoint_url . '/%d', $this->group_id ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() ); } /** * @group delete_item */ public function test_delete_item() { wp_set_current_user( $this->user ); $field_group = $this->endpoint->get_xprofile_field_group_object( $this->group_id ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $field_group->id ) ); $request->set_param( 'context', 'edit' ); $response = $this->server->dispatch( $request ); $this->assertNotInstanceOf( 'WP_Error', $response ); $this->assertSame( 200, $response->get_status() ); $all_data = $response->get_data(); $this->assertNotEmpty( $all_data ); $this->check_group_data( $field_group, $all_data['previous'], 'view', $response->get_links() ); } /** * @group delete_item */ public function test_delete_item_invalid_id() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_invalid_id', $response, 404 ); } /** * @group delete_item */ public function test_delete_item_user_not_logged_in() { $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $this->group_id ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() ); } /** * @group delete_item */ public function test_delete_item_without_permission() { $u = static::factory()->user->create(); wp_set_current_user( $u ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d', $this->group_id ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() ); } /** * @group prepare_item */ public function test_prepare_item() { wp_set_current_user( $this->user ); $group = $this->endpoint->get_xprofile_field_group_object( $this->group_id ); $this->assertSame( $this->group_id, $group->id ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $group->id ) ); $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->check_group_data( $group, $all_data, 'view', $response->get_links() ); } protected function check_create_field_group_response( $response ) { $this->assertNotInstanceOf( 'WP_Error', $response ); $response = rest_ensure_response( $response ); $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertNotEmpty( $data ); $field_group = $this->endpoint->get_xprofile_field_group_object( $data['id'] ); $this->check_group_data( $field_group, $data, 'view', $response->get_links() ); } protected function check_group_data( $group, $data, $context, $links ) { $this->assertSame( $group->id, $data['id'] ); $this->assertSame( $group->name, $data['name'] ); $this->assertSame( $group->group_order, $data['group_order'] ); $this->assertSame( (bool) $group->can_delete, $data['can_delete'] ); if ( 'view' === $context ) { $this->assertSame( $group->description, $data['description']['rendered'] ); } else { $this->assertSame( $group->description, $data['description']['raw'] ); } } protected function set_field_group_data( $args = array() ) { return wp_parse_args( $args, array( 'description' => 'Field Group Description', 'name' => 'Test Field Name', 'can_delete' => true, ) ); } public function test_get_item_schema() { $request = new WP_REST_Request( 'OPTIONS', sprintf( $this->endpoint_url . '/%d', $this->group_id ) ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); $properties = $data['schema']['properties']; $this->assertCount( 6, $properties ); $this->assertArrayHasKey( 'id', $properties ); $this->assertArrayHasKey( 'name', $properties ); $this->assertArrayHasKey( 'description', $properties ); $this->assertArrayHasKey( 'group_order', $properties ); $this->assertArrayHasKey( 'can_delete', $properties ); $this->assertArrayHasKey( 'fields', $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'] ); } public function update_additional_field( $value, $data, $attribute ) { return bp_xprofile_update_meta( $data->id, 'group', '_' . $attribute, $value ); } public function get_additional_field( $data, $attribute ) { return bp_xprofile_get_meta( $data['id'], 'group', '_' . $attribute ); } /** * @group additional_fields */ public function test_additional_fields() { $registered_fields = $GLOBALS['wp_rest_additional_fields']; bp_rest_register_field( 'xprofile', 'foo_group_key', array( 'get_callback' => array( $this, 'get_additional_field' ), 'update_callback' => array( $this, 'update_additional_field' ), 'schema' => array( 'description' => 'xProfile Group Meta Field', 'type' => 'string', 'context' => array( 'view', 'edit' ), ), ), 'group' ); wp_set_current_user( $this->user ); $expected = 'bar_group_value'; // POST $request = new WP_REST_Request( 'POST', $this->endpoint_url ); $request->set_param( 'context', 'edit' ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); $params = $this->set_field_group_data( array( 'foo_group_key' => $expected ) ); $request->set_body_params( $params ); $response = $this->server->dispatch( $request ); $create_data = $response->get_data(); $this->assertSame( $expected, $create_data['foo_group_key'] ); // GET $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $create_data['id'] ) ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); $get_data = $response->get_data(); $this->assertSame( $expected, $get_data['foo_group_key'] ); $GLOBALS['wp_rest_additional_fields'] = $registered_fields; } /** * @group additional_fields */ public function test_update_additional_fields() { $registered_fields = $GLOBALS['wp_rest_additional_fields']; bp_rest_register_field( 'xprofile', 'bar_group_key', array( 'get_callback' => array( $this, 'get_additional_field' ), 'update_callback' => array( $this, 'update_additional_field' ), 'schema' => array( 'description' => 'xProfile Group Meta Field', 'type' => 'string', 'context' => array( 'view', 'edit' ), ), ), 'group' ); wp_set_current_user( $this->user ); $expected = 'foo_group_value'; // PUT $request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $this->group_id ) ); $request->set_param( 'context', 'edit' ); $request->add_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( array( 'name' => $this->endpoint->get_xprofile_field_group_object( $this->group_id )->name, 'bar_group_key' => $expected, ) ) ); $response = $this->server->dispatch( $request ); $update_data = $response->get_data(); $this->assertSame( $expected, $update_data['bar_group_key'] ); $GLOBALS['wp_rest_additional_fields'] = $registered_fields; } }