server->get_routes(); $endpoint = $this->endpoint_url . '/(?P[\d]+)/avatar'; // Single. $this->assertArrayHasKey( $endpoint, $routes ); $this->assertCount( 1, $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() { $this->skipWithoutMultisite(); $u = $this->bp::factory()->user->create(); $expected = array( 'full' => get_avatar_url( $u, array( 'size' => 150 ) ), 'thumb' => get_avatar_url( $u, array( 'size' => 50 ) ), ); wp_set_current_user( $u ); $blog_id = self::factory()->blog->create(); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d/avatar', $blog_id ) ); $request->set_param( 'context', 'view' ); $response = $this->server->dispatch( $request ); $all_data = $response->get_data(); $this->assertSame( $all_data, $expected ); } /** * @group get_item */ public function test_get_item_with_support_for_the_community_visibility() { $this->skipWithoutMultisite(); toggle_component_visibility(); $blog_id = self::factory()->blog->create(); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d/avatar', $blog_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_site_icon() { $this->skipWithoutMultisite(); $expected = array( 'full' => bp_blogs_default_avatar( '', array( 'object' => 'blog', 'type' => 'full', ) ), 'thumb' => bp_blogs_default_avatar( '', array( 'object' => 'blog', 'type' => 'thumb', ) ), ); $u = $this->bp::factory()->user->create(); wp_set_current_user( $u ); $blog_id = self::factory()->blog->create(); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d/avatar', $blog_id ) ); $request->set_param( 'context', 'view' ); $request->set_param( 'no_user_gravatar', true ); $response = $this->server->dispatch( $request ); $all_data = $response->get_data(); $this->assertSame( $all_data, $expected ); } /** * @group get_item */ public function test_get_item_no_grav() { $this->skipWithoutMultisite(); $u = $this->bp::factory()->user->create(); wp_set_current_user( $u ); $blog_id = self::factory()->blog->create(); $expected = array( 'full' => bp_get_blog_avatar( array( 'blog_id' => $blog_id, 'html' => false, 'type' => 'full', ) ), 'thumb' => bp_get_blog_avatar( array( 'blog_id' => $blog_id, 'html' => false, 'type' => 'thumb', ) ), ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d/avatar', $blog_id ) ); $request->set_param( 'context', 'view' ); $request->set_param( 'no_user_gravatar', true ); $response = $this->server->dispatch( $request ); $all_data = $response->get_data(); $this->assertSame( $all_data, $expected ); } /** * @group get_item */ public function test_get_item_invalid_user_id() { $this->skipWithoutMultisite(); $current_user = get_current_user_id(); $u = $this->bp::factory()->user->create(); wp_set_current_user( $u ); $blog_id = self::factory()->blog->create( array( 'meta' => array( 'public' => 1 ) ) ); wp_set_current_user( $current_user ); // Remove admins. add_filter( 'bp_blogs_get_blogs', array( $this, 'filter_admin_user_id' ) ); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d/avatar', $blog_id ) ); $response = $this->server->dispatch( $request ); remove_filter( 'bp_blogs_get_blogs', array( $this, 'filter_admin_user_id' ) ); $this->assertErrorResponse( 'bp_rest_blog_avatar_get_item_user_failed', $response, 500 ); } public function filter_admin_user_id( $blog_results ) { unset( $blog_results['blogs'][0]->admin_user_id ); return $blog_results; } /** * @group get_item */ public function test_get_item_invalid_blog_id() { $this->skipWithoutMultisite(); $request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d/avatar', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $response = $this->server->dispatch( $request ); $this->assertErrorResponse( 'bp_rest_blog_invalid_id', $response, 404 ); } /** * @group create_item */ public function test_create_item() { $this->markTestSkipped( 'This endpoint does not support creating blog avatars.' ); } /** * @group update_item */ public function test_update_item() { $this->markTestSkipped( 'This endpoint does not support updating blog avatars.' ); } /** * @group delete_item */ public function test_delete_item() { $this->markTestSkipped( 'This endpoint does not support deleting blog avatars.' ); } /** * @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_blog_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', $this->endpoint_url ); 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() { $this->skipWithoutMultisite(); $blog_id = self::factory()->blog->create(); // Single. $request = new WP_REST_Request( 'OPTIONS', sprintf( $this->endpoint_url . '/%d/avatar', $blog_id ) ); $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() { $this->skipWithoutMultisite(); $blog_id = self::factory()->blog->create(); // Single. $request = new WP_REST_Request( 'OPTIONS', sprintf( $this->endpoint_url . '/%d/avatar', $blog_id ) ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); $this->assertNotEmpty( $data ); } }