api_fetch_inline_scripts = wp_scripts()->get_data( 'wp-api-fetch', 'after' ); } public function tear_down() { unset( $_GET['p'] ); wp_scripts()->add_data( 'wp-api-fetch', 'after', $this->api_fetch_inline_scripts ); parent::tear_down(); } /** * Returns the inline script the preload added to `wp-api-fetch`, empty * when it added none. * * @return string The inline script. */ private function get_preloaded_inline_script() { $before = false === $this->api_fetch_inline_scripts ? array() : (array) $this->api_fetch_inline_scripts; $after = wp_scripts()->get_data( 'wp-api-fetch', 'after' ); $after = false === $after ? array() : (array) $after; return implode( '', array_slice( $after, count( $before ) ) ); } /** * A route of the extensible site editor, or of the media editor page, * preloads the fields of the post types its screen shows. * * @dataProvider data_routes_and_their_post_types * * @param string $path The `p` query arg. * @param string[] $post_types The post types whose fields are preloaded. */ public function test_a_route_preloads_the_fields_of_its_post_types( $path, $post_types ) { $_GET['p'] = $path; $this->assertSame( $post_types, _gutenberg_get_route_post_types() ); } /** * Data provider for test_a_route_preloads_the_fields_of_its_post_types(). * * @return array[] */ public function data_routes_and_their_post_types() { return array( 'posts' => array( '/types/post', array( 'post' ) ), 'post list' => array( '/types/page/list/all', array( 'page' ) ), 'post editor' => array( '/types/page/edit/12', array( 'page' ) ), 'new post' => array( '/types/page/new', array( 'page' ) ), 'custom post type' => array( '/types/acme_book/list/all', array( 'acme_book' ) ), 'a list with a search' => array( '/types/page/list/all?page=2', array( 'page' ) ), 'templates' => array( '/templates', array( 'wp_template' ) ), 'template list' => array( '/templates/list/all', array( 'wp_template' ) ), 'template parts' => array( '/template-parts', array( 'wp_template_part' ) ), 'template part list' => array( '/template-parts/list/header', array( 'wp_template_part' ) ), 'patterns' => array( '/patterns', array( 'wp_block' ) ), 'pattern list' => array( '/patterns/list/user', array( 'wp_block' ) ), 'navigation' => array( '/navigation', array( 'wp_navigation' ) ), 'navigation list' => array( '/navigation/list', array( 'wp_navigation' ) ), 'media editor' => array( '/media-editor/12', array( 'attachment' ) ), 'home' => array( '/', array() ), 'styles' => array( '/styles', array() ), 'a post type-less type' => array( '/types', array() ), 'no route' => array( '', array() ), ); } /** * The fields of the post type reach the page as a preloading middleware of * `api-fetch`, next to the one the page prints itself. */ public function test_a_route_prints_the_preloaded_fields() { wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) ); $_GET['p'] = '/types/page/list/all'; _gutenberg_preload_route_entity_fields(); $script = $this->get_preloaded_inline_script(); $this->assertStringContainsString( 'wp.apiFetch.createPreloadingMiddleware(', $script ); // The path is a key of the JSON the middleware receives, so its // slashes are escaped. $this->assertStringContainsString( 'fields?kind=postType&name=page', $script ); $this->assertStringContainsString( '"body"', $script ); } /** * Only the media editor route preloads a view config: the form its loader * resolves before the route renders. */ public function test_the_media_editor_route_preloads_the_attachment_form() { $_GET['p'] = '/media-editor/12'; $this->assertSame( array( '/wp/v2/view-config?kind=postType&name=attachment&_fields=form' ), _gutenberg_get_route_view_config_preload_paths() ); $_GET['p'] = '/types/page/list/all'; $this->assertSame( array(), _gutenberg_get_route_view_config_preload_paths() ); } /** * The attachment form reaches the media editor route along with the * attachment fields. */ public function test_the_media_editor_route_prints_the_preloaded_form() { wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) ); $_GET['p'] = '/media-editor/12'; _gutenberg_preload_route_entity_fields(); $script = $this->get_preloaded_inline_script(); $this->assertStringContainsString( 'fields?kind=postType&name=attachment', $script ); $this->assertStringContainsString( 'view-config?kind=postType&name=attachment&_fields=form', $script ); $this->assertStringContainsString( '"form"', $script ); } /** * A route that shows no post fields prints nothing. */ public function test_a_route_without_post_fields_preloads_nothing() { wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) ); $_GET['p'] = '/styles'; _gutenberg_preload_route_entity_fields(); $this->assertSame( '', $this->get_preloaded_inline_script() ); } /** * A post type that is not registered preloads nothing, so a route naming * one prints no middleware at all. */ public function test_an_unregistered_post_type_preloads_nothing() { wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) ); $_GET['p'] = '/types/acme_not_registered/list/all'; $this->assertSame( array(), _gutenberg_get_post_type_fields_preload_paths( _gutenberg_get_route_post_types() ) ); _gutenberg_preload_route_entity_fields(); $this->assertSame( '', $this->get_preloaded_inline_script() ); } }