image_file = BP_TESTS_DIR . 'assets/test-image-large.jpg'; } public function test_register_routes() { $routes = $this->server->get_routes(); $endpoint = $this->endpoint_url . '/(?P[\d]+)/avatar'; // Single. $this->assertArrayHasKey( $endpoint, $routes ); $this->assertCount( 3, $routes[ $endpoint ] ); } /** * @group get_items */ public function test_get_items() { $this->markTestSkipped( 'This endpoint has no collection route or get_items() method.' ); } /** * @group get_item */ public function test_get_item() { $u1 = $this->bp::factory()->user->create(); wp_set_current_user( $u1 ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d/avatar', $u1 ) ); $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->assertTrue( isset( $all_data['full'] ) && isset( $all_data['thumb'] ) ); } /** * @group get_item */ public function test_get_item_with_support_for_the_community_visibility() { toggle_component_visibility(); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d/avatar', $this->user ) ); $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() { $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d/avatar', $this->user ) ); $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->assertTrue( isset( $all_data['full'] ) && isset( $all_data['thumb'] ) ); } /** * @group get_item */ public function test_get_item_invalid_member_id() { $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d/avatar', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_member_invalid_id', $response, 404 ); } /** * @group create_item */ public function test_create_item() { $reset_files = $_FILES; $reset_post = $_POST; wp_set_current_user( $this->user ); add_filter( 'pre_move_uploaded_file', array( $this, 'copy_file' ), 10, 3 ); add_filter( 'bp_core_avatar_dimension', array( $this, 'return_100' ) ); $_FILES['file'] = array( 'tmp_name' => $this->image_file, 'name' => 'test-image-large.jpg', 'type' => 'image/jpg', 'error' => 0, 'size' => filesize( $this->image_file ), ); $_POST['action'] = 'bp_avatar_upload'; $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '/%d/avatar', $this->user ) ); $request->set_param( 'context', 'edit' ); $request->set_file_params( $_FILES ); $response = $this->server->dispatch( $request ); remove_filter( 'pre_move_uploaded_file', array( $this, 'copy_file' ) ); remove_filter( 'bp_core_avatar_dimension', array( $this, 'return_100' ) ); $avatar = $response->get_data(); $this->assertSame( $avatar, array( 'full' => bp_core_fetch_avatar( array( 'object' => 'user', 'type' => 'full', 'item_id' => $this->user, 'html' => false, ) ), 'thumb' => bp_core_fetch_avatar( array( 'object' => 'user', 'type' => 'thumb', 'item_id' => $this->user, 'html' => false, ) ), ) ); $_FILES = $reset_files; $_POST = $reset_post; } public function copy_file( $return, $file, $new_file ) { return @copy( $file['tmp_name'], $new_file ); } public function return_100() { return 100; } /** * @group create_item */ public function test_create_item_with_upload_disabled() { wp_set_current_user( $this->user ); // Disabling member avatar upload. add_filter( 'bp_disable_avatar_uploads', '__return_true' ); $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '/%d/avatar', $this->user ) ); $request->set_param( 'context', 'edit' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_attachments_member_avatar_disabled', $response, 500 ); } /** * @group create_item */ public function test_create_item_empty_image() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '/%d/avatar', $this->user ) ); $request->set_param( 'context', 'edit' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_attachments_member_avatar_no_image_file', $response, 500 ); } /** * @group create_item */ public function test_create_item_user_not_logged_in() { $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '/%d/avatar', $this->user ) ); $request->set_param( 'context', 'edit' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() ); } /** * @group create_item */ public function test_create_item_invalid_member_id() { $u1 = $this->bp::factory()->user->create(); wp_set_current_user( $u1 ); $request = new WP_REST_Request( 'POST', sprintf( $this->endpoint_url . '/%d/avatar', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $request->set_param( 'context', 'edit' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_member_invalid_id', $response, 404 ); } /** * @group update_item */ public function test_update_item() { $this->markTestSkipped( 'This endpoint does not support updating member avatars.' ); } /** * @group delete_item */ public function test_delete_item() { $user_id = $this->bp::factory()->user->create(); $upload_dir = get_temp_dir() . 'bp-rest-member-avatar-' . wp_generate_uuid4(); $upload_url = set_url_scheme( 'http://example.org/bp-test-avatars' ); $avatar_dir = $upload_dir . '/avatars/' . $user_id; $full = $avatar_dir . '/test-bpfull.jpg'; $thumb = $avatar_dir . '/test-bpthumb.jpg'; $upload_path_filter = static function () use ( $upload_dir ) { return $upload_dir; }; $upload_url_filter = static function () use ( $upload_url ) { return $upload_url; }; add_filter( 'bp_core_avatar_upload_path', $upload_path_filter ); add_filter( 'bp_core_avatar_url', $upload_url_filter ); try { // Seed stored avatars without repeating upload and cropping coverage. $this->assertTrue( wp_mkdir_p( $avatar_dir ) ); $this->assertTrue( copy( $this->image_file, $full ) ); $this->assertTrue( copy( $this->image_file, $thumb ) ); $this->assertTrue( bp_get_user_has_avatar( $user_id ) ); wp_set_current_user( $user_id ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d/avatar', $user_id ) ); $request->set_param( 'context', 'edit' ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertTrue( $data['deleted'] ); $this->assertInstanceOf( 'stdClass', $data['previous'] ); $this->assertSame( array( 'full' => $upload_url . '/avatars/' . $user_id . '/test-bpfull.jpg', 'thumb' => $upload_url . '/avatars/' . $user_id . '/test-bpthumb.jpg', ), (array) $data['previous'] ); $this->assertFalse( file_exists( $full ) ); $this->assertFalse( file_exists( $thumb ) ); $this->assertDirectoryDoesNotExist( $avatar_dir ); $this->assertFalse( bp_get_user_has_avatar( $user_id ) ); } finally { $this->bp->rrmdir( $upload_dir ); remove_filter( 'bp_core_avatar_upload_path', $upload_path_filter ); remove_filter( 'bp_core_avatar_url', $upload_url_filter ); } } /** * @group delete_item */ public function test_delete_item_failed() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d/avatar', $this->user ) ); $request->set_param( 'context', 'edit' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_attachments_member_avatar_no_uploaded_avatar', $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/avatar', $this->user ) ); $request->set_param( 'context', 'edit' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_authorization_required', $response, rest_authorization_required_code() ); } /** * @group delete_item */ public function test_delete_item_invalid_member_id() { $request = new WP_REST_Request( 'DELETE', sprintf( $this->endpoint_url . '/%d/avatar', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $request->set_param( 'context', 'edit' ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_member_invalid_id', $response, 404 ); } /** * @group prepare_item */ public function test_prepare_item() { global $wp_rest_additional_fields; $original_fields = $wp_rest_additional_fields; $avatar = (object) array( 'full' => 'https://example.org/avatar-full.jpg', 'thumb' => 'https://example.org/avatar-thumb.jpg', ); register_rest_field( 'bp_attachments_member_avatar', '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', sprintf( $this->endpoint_url . '/%d/avatar', $this->user ) ); if ( null !== $context ) { $request->set_param( 'context', $context ); } $response = $this->endpoint->prepare_item_for_response( $avatar, $request ); $expected = (array) $avatar; if ( 'edit' === $context ) { $expected['test_edit_field'] = 'Additional value'; } $this->assertInstanceOf( 'WP_REST_Response', $response ); $this->assertSame( 200, $response->get_status() ); $this->assertSame( $expected, $response->get_data() ); } } finally { $wp_rest_additional_fields = $original_fields; } } public function test_get_item_schema() { $request = new WP_REST_Request( 'OPTIONS', sprintf( $this->endpoint_url . '/%d/avatar', $this->user ) ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); $properties = $data['schema']['properties']; $this->assertCount( 2, $properties ); $this->assertArrayHasKey( 'full', $properties ); $this->assertArrayHasKey( 'thumb', $properties ); } public function test_context_param() { // Single. $request = new WP_REST_Request( 'OPTIONS', sprintf( $this->endpoint_url . '/%d/avatar', $this->user ) ); $response = $this->server->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $this->assertNotEmpty( $data ); } }