diff --git a/src/wp-includes/class-wp-entity-actions-registry.php b/src/wp-includes/class-wp-entity-actions-registry.php index 1ee5e722cf818..664819e078add 100644 --- a/src/wp-includes/class-wp-entity-actions-registry.php +++ b/src/wp-includes/class-wp-entity-actions-registry.php @@ -404,17 +404,31 @@ static function ( $entities ) { * * @param string $kind The entity kind (e.g. `postType`). * @param string $name The entity name (e.g. `page`). - * @return array The ids of the actions each module - * applies to, keyed by module id, in - * registration order. Empty when the - * entity is invalid. + * @return array[] { + * The script modules, in registration order. Empty when the entity + * is invalid. + * + * @type array ...$0 { + * @type string $id The id of the script module. + * @type string[] $actions The ids of the actions the module + * applies to, in registration order. + * } + * } */ public function get_registered_action_modules( $kind, $name ) { if ( ! $this->validate_entity( __METHOD__, $kind, $name ) ) { return array(); } $this->initialize(); - return $this->action_modules[ $kind ][ $name ] ?? array(); + $modules = array(); + foreach ( $this->action_modules[ $kind ][ $name ] ?? array() as $module => $action_ids ) { + $modules[] = array( + // A numeric module id is an integer as an array key. + 'id' => (string) $module, + 'actions' => $action_ids, + ); + } + return $modules; } /** diff --git a/src/wp-includes/class-wp-entity-fields-registry.php b/src/wp-includes/class-wp-entity-fields-registry.php index e25f2e026a0fe..864828ca1c243 100644 --- a/src/wp-includes/class-wp-entity-fields-registry.php +++ b/src/wp-includes/class-wp-entity-fields-registry.php @@ -477,13 +477,27 @@ static function ( $entities ) { * * @param string $kind The entity kind (e.g. `postType`). * @param string $name The entity name (e.g. `page`). - * @return array The ids of the fields each module - * applies to, keyed by module id, in - * registration order. + * @return array[] { + * The script modules, in registration order. + * + * @type array ...$0 { + * @type string $id The id of the script module. + * @type string[] $fields The ids of the fields the module applies + * to, in registration order. + * } + * } */ public function get_registered_field_modules( $kind, $name ) { $this->initialize(); - return $this->field_modules[ $kind ][ $name ] ?? array(); + $modules = array(); + foreach ( $this->field_modules[ $kind ][ $name ] ?? array() as $module => $field_ids ) { + $modules[] = array( + // A numeric module id is an integer as an array key. + 'id' => (string) $module, + 'fields' => $field_ids, + ); + } + return $modules; } /** @@ -497,9 +511,25 @@ public function get_registered_field_modules( $kind, $name ) { */ public function get_all_registered_field_modules() { $this->initialize(); + /* + * Keeps the module ids and drops the field ids they apply to, e.g.: + * + * array( + * 'postType' => array( + * 'page' => array( 'my-plugin/fields', '123' ), + * 'post' => array( 'my-plugin/fields' ), + * ), + * ) + */ return array_map( static function ( $entities ) { - return array_map( 'array_keys', $entities ); + return array_map( + static function ( $modules ) { + // A numeric module id is an integer as an array key. + return array_map( 'strval', array_keys( $modules ) ); + }, + $entities + ); }, $this->field_modules ); @@ -731,7 +761,7 @@ protected function skip_invalid_definitions( $method, $kind, $name, $fields ) { $valid = array(); $invalid = array(); foreach ( $fields as $position => $field ) { - if ( is_array( $field ) && ! empty( $field['id'] ) && is_string( $field['id'] ) ) { + if ( is_array( $field ) && isset( $field['id'] ) && is_string( $field['id'] ) && '' !== $field['id'] ) { $valid[] = $field; } else { $invalid[] = '#' . ( $position + 1 ); diff --git a/src/wp-includes/entity-actions.php b/src/wp-includes/entity-actions.php index 426fd1d30943a..80aced696b1c6 100644 --- a/src/wp-includes/entity-actions.php +++ b/src/wp-includes/entity-actions.php @@ -80,17 +80,22 @@ function wp_get_registered_entity_actions( $kind, $name ) { } /** - * Returns the ids of the script modules registered for the given entity. + * Returns the script modules registered for the given entity. * * @since 7.2.0 * * @param string $kind The entity kind (e.g. `postType`). * @param string $name The entity name (e.g. `page`). - * @return array The ids of the actions each module - * applies to, keyed by module id, in - * registration order. Empty until the - * `init` action has completed: a read - * before then is refused. + * @return array[] { + * The script modules, in registration order. Empty until the `init` + * action has completed: a read before then is refused. + * + * @type array ...$0 { + * @type string $id The id of the script module. + * @type string[] $actions The ids of the actions the module applies + * to, in registration order. + * } + * } */ function wp_get_registered_entity_action_modules( $kind, $name ) { return WP_Entity_Actions_Registry::get_instance()->get_registered_action_modules( $kind, $name ); @@ -114,78 +119,21 @@ function wp_get_all_registered_entity_action_modules() { /** * Declares the script modules of the entity actions as dependencies of the - * editor script. - * - * The editor imports the script modules of an entity on demand, with a dynamic - * `import()` the browser resolves through the import map of the page. Listing - * the modules as dynamic `module_dependencies` of the `wp-editor` script adds - * them to the import map of every page that loads the editor script: the post - * editor, the site editor, and the pages booted by `@wordpress/boot`, whose - * prerequisites depend on it. A dynamic dependency is only listed in the - * import map; the module is fetched when it is imported. - * - * Reading the registry fires `wp_entity_actions_api_init`, so the actions and their - * script modules are registered on demand. It only reads the registry on the - * pages that load the editor script, not on every admin request (heartbeat, - * Ajax, other screens). It runs on `admin_footer`, the last action before the - * import map is printed that fires both in the admin template and in the pages - * rendered outside it, which print the import map themselves. By then `init` - * has completed, so the post types and supports the actions derive from are - * final. - * The registry only accepts registrations while the action fires, so the - * modules declared here are the modules of every action the page can show. + * editor script, see _wp_add_entity_modules_to_editor_script(). + * + * The actions are loaded by `usePostActions()` of the editor script, so every + * screen showing them loads it. * * The `admin_footer` action passes it no argument, so the global registry is * used. In other scenarios (tests), the scripts are given directly. * * @since 7.2.0 + * @access private * * @param WP_Scripts|null $scripts The scripts registry. Defaults to the global one. */ function _wp_add_entity_action_modules_to_editor_script( $scripts = null ) { - if ( ! $scripts instanceof WP_Scripts ) { - $scripts = wp_scripts(); - } - - // The actions are loaded by `usePostActions()` of the editor script, so - // every screen showing them loads it. - if ( ! $scripts->query( 'wp-editor', 'enqueued' ) && ! $scripts->query( 'wp-editor', 'done' ) ) { - return; - } - - $kinds = wp_get_all_registered_entity_action_modules(); - if ( empty( $kinds ) ) { - return; - } - - $dependencies = $scripts->get_data( 'wp-editor', 'module_dependencies' ); - $dependencies = is_array( $dependencies ) ? $dependencies : array(); - - $declared = array(); - foreach ( $dependencies as $dependency ) { - $declared[] = is_array( $dependency ) ? $dependency['id'] : $dependency; - } - - $added = false; - foreach ( $kinds as $entities ) { - foreach ( $entities as $modules ) { - foreach ( $modules as $module ) { - if ( ! in_array( $module, $declared, true ) ) { - $dependencies[] = array( - 'id' => $module, - 'import' => 'dynamic', - ); - $declared[] = $module; - $added = true; - } - } - } - } - - if ( $added ) { - // `add_data()` replaces the value, hence the merge above. - $scripts->add_data( 'wp-editor', 'module_dependencies', $dependencies ); - } + _wp_add_entity_modules_to_editor_script( $scripts, 'wp_get_all_registered_entity_action_modules' ); } /** @@ -201,34 +149,12 @@ function _wp_add_entity_action_modules_to_editor_script( $scripts = null ) { * packages/core-data/src/resolvers.js in Gutenberg. * * @since 7.2.0 + * @access private * - * @param array $paths REST API paths to preload. - * @param WP_Block_Editor_Context $context Block editor context. - * @return array Filtered preload paths. + * @param mixed $paths REST API paths to preload. + * @param mixed $context Optional. Block editor context. Default null. + * @return mixed Filtered preload paths. See _wp_add_entity_preload_paths(). */ -function _wp_preload_entity_actions( $paths, $context ) { - $post_types = array(); - if ( 'core/edit-post' === $context->name && isset( $context->post ) ) { - $post_types[] = $context->post->post_type; - } elseif ( 'core/edit-site' === $context->name ) { - if ( isset( $context->post ) ) { - $post_types[] = $context->post->post_type; - } - $post_types = array_merge( $post_types, _wp_get_site_editor_screen_post_types() ); - } - - foreach ( array_unique( $post_types ) as $post_type ) { - if ( ! post_type_exists( $post_type ) ) { - continue; - } - $paths[] = add_query_arg( - array( - 'kind' => 'postType', - 'name' => $post_type, - ), - '/wp/v2/entity-actions' - ); - } - - return $paths; +function _wp_preload_entity_actions( $paths, $context = null ) { + return _wp_add_entity_preload_paths( $paths, $context, '/wp/v2/entity-actions' ); } diff --git a/src/wp-includes/entity-fields.php b/src/wp-includes/entity-fields.php index 779a32f7fe3f2..c08bf519fcaa4 100644 --- a/src/wp-includes/entity-fields.php +++ b/src/wp-includes/entity-fields.php @@ -111,16 +111,22 @@ function wp_get_registered_entity_fields( $kind, $name ) { } /** - * Returns the ids of the script modules registered for the given entity. + * Returns the script modules registered for the given entity. * * @since 7.2.0 * * @param string $kind The entity kind (e.g. `postType`). * @param string $name The entity name (e.g. `page`). - * @return array The ids of the fields each module applies - * to, keyed by module id, in registration - * order. Empty until the `init` action has - * completed: a read before then is refused. + * @return array[] { + * The script modules, in registration order. Empty until the `init` + * action has completed: a read before then is refused. + * + * @type array ...$0 { + * @type string $id The id of the script module. + * @type string[] $fields The ids of the fields the module applies to, + * in registration order. + * } + * } */ function wp_get_registered_entity_field_modules( $kind, $name ) { return WP_Entity_Fields_Registry::get_instance()->get_registered_field_modules( $kind, $name ); @@ -144,25 +150,12 @@ function wp_get_all_registered_entity_field_modules() { /** * Declares the script modules of the entity fields as dependencies of the - * editor script. - * - * The editor imports the script modules of an entity on demand, with a dynamic - * `import()` the browser resolves through the import map of the page. Listing - * the modules as dynamic `module_dependencies` of the `wp-editor` script adds - * them to the import map of every page that loads the editor script: the post - * editor, the site editor, and the pages booted by `@wordpress/boot`, whose - * prerequisites depend on it. A dynamic dependency is only listed in the - * import map; the module is fetched when it is imported. - * - * Reading the registry fires `wp_entity_fields_api_init`, so the fields and their - * script modules are registered on demand. It only reads the registry on the - * pages that load the editor script, not on every admin request (heartbeat, - * Ajax, other screens). It runs on `admin_footer`, the last action before the - * import map is printed that fires both in the admin template and in the pages - * rendered outside it, which print the import map themselves. By then `init` - * has completed, so the supports the default fields derive from are final. - * The registry only accepts registrations while the action fires, so the - * modules declared here are the modules of every field the page can show. + * editor script, see _wp_add_entity_modules_to_editor_script(). + * + * The fields are loaded by the editor package, see + * packages/editor/src/dataviews/store/private-resolvers.ts in Gutenberg, which + * means any screen that wants to use this mechanism needs to load the editor + * script. * * It is hooked to `admin_footer` without arguments, so the global scripts * registry is used. In other scenarios (tests), the scripts are given directly. @@ -174,53 +167,7 @@ function wp_get_all_registered_entity_field_modules() { * the global one. */ function _wp_add_entity_field_modules_to_editor_script( $scripts = null ) { - if ( ! $scripts instanceof WP_Scripts ) { - $scripts = wp_scripts(); - } - - /* - * The fields are loaded by the editor package, see - * packages/editor/src/dataviews/store/private-resolvers.ts in Gutenberg, - * which means any screen that wants to use this mechanism needs to load - * the editor script. - */ - if ( ! $scripts->query( 'wp-editor', 'enqueued' ) && ! $scripts->query( 'wp-editor', 'done' ) ) { - return; - } - - $kinds = wp_get_all_registered_entity_field_modules(); - if ( empty( $kinds ) ) { - return; - } - - $dependencies = $scripts->get_data( 'wp-editor', 'module_dependencies' ); - $dependencies = is_array( $dependencies ) ? $dependencies : array(); - - $declared = array(); - foreach ( $dependencies as $dependency ) { - $declared[] = is_array( $dependency ) ? $dependency['id'] : $dependency; - } - - $added = false; - foreach ( $kinds as $entities ) { - foreach ( $entities as $modules ) { - foreach ( $modules as $module ) { - if ( ! in_array( $module, $declared, true ) ) { - $dependencies[] = array( - 'id' => $module, - 'import' => 'dynamic', - ); - $declared[] = $module; - $added = true; - } - } - } - } - - if ( $added ) { - // `add_data()` replaces the value, hence the merge above. - $scripts->add_data( 'wp-editor', 'module_dependencies', $dependencies ); - } + _wp_add_entity_modules_to_editor_script( $scripts, 'wp_get_all_registered_entity_field_modules' ); } /** @@ -255,73 +202,6 @@ function wp_register_entity_field_collection( $directory ) { return WP_Entity_Fields_Registry::get_instance()->register_collection( $directory ); } -/** - * Returns the paths that serve the fields of the given post types, skipping - * the post types that are not registered. - * - * A path must match the request of the `getFieldsConfig` core data resolver, - * see packages/core-data/src/resolvers.js in Gutenberg. - * - * @since 7.2.0 - * @access private - * - * @param string[] $post_types The post types. - * @return string[] One path of the `/wp/v2/entity-fields` route per post type, - * in the order the post types are given, without duplicates. - */ -function _wp_get_post_type_fields_preload_paths( $post_types ) { - $paths = array(); - foreach ( array_unique( $post_types ) as $post_type ) { - if ( ! post_type_exists( $post_type ) ) { - continue; - } - $paths[] = add_query_arg( - array( - 'kind' => 'postType', - 'name' => $post_type, - ), - '/wp/v2/entity-fields' - ); - } - - return $paths; -} - -/** - * Returns the post types whose items the screen of the site editor being - * loaded lists or edits, from its `p` query arg. - * - * The screens are the routes in - * packages/edit-site/src/components/site-editor-routes in Gutenberg: the - * lists and the editors of pages, templates, patterns, and template parts. - * The patterns screen lists both patterns and template parts. - * - * @since 7.2.0 - * @access private - * - * @return string[] The post types, empty for any other screen. - */ -function _wp_get_site_editor_screen_post_types() { - // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only picks what to preload. - $path = isset( $_GET['p'] ) && is_string( $_GET['p'] ) ? sanitize_text_field( wp_unslash( $_GET['p'] ) ) : ''; - $segments = explode( '/', trim( $path, '/' ) ); - - switch ( $segments[0] ) { - case 'page': - return array( 'page' ); - case 'template': - case 'wp_template': - return array( 'wp_template' ); - case 'pattern': - return array( 'wp_block', 'wp_template_part' ); - case 'wp_block': - return array( 'wp_block' ); - case 'wp_template_part': - return array( 'wp_template_part' ); - } - return array(); -} - /** * Preloads the fields of the post types the editors list. * @@ -329,28 +209,17 @@ function _wp_get_site_editor_screen_post_types() { * in packages/editor/src/dataviews/fields-loader/index.ts in Gutenberg. The * post editor reads those of the edited post in the post summary of its * inspector. The site editor reads those of the edited post, if any, and of - * the post types of the screen it opens on. + * the post types of the screen it opens on. A path must match the request of + * the `getFieldsConfig` core data resolver, see + * packages/core-data/src/resolvers.js in Gutenberg. * * @since 7.2.0 * @access private * - * @param array $paths REST API paths to preload. - * @param WP_Block_Editor_Context $context Block editor context. - * @return array Filtered preload paths. + * @param mixed $paths REST API paths to preload. + * @param mixed $context Optional. Block editor context. Default null. + * @return mixed Filtered preload paths. See _wp_add_entity_preload_paths(). */ -function _wp_preload_entity_fields( $paths, $context ) { - $post_types = array(); - if ( 'core/edit-post' === $context->name && isset( $context->post ) ) { - $post_types[] = $context->post->post_type; - } elseif ( 'core/edit-site' === $context->name ) { - if ( isset( $context->post ) ) { - $post_types[] = $context->post->post_type; - } - $post_types = array_merge( $post_types, _wp_get_site_editor_screen_post_types() ); - } - - return array_merge( - $paths, - _wp_get_post_type_fields_preload_paths( $post_types ) - ); +function _wp_preload_entity_fields( $paths, $context = null ) { + return _wp_add_entity_preload_paths( $paths, $context, '/wp/v2/entity-fields' ); } diff --git a/src/wp-includes/entity-utils.php b/src/wp-includes/entity-utils.php new file mode 100644 index 0000000000000..9f2489886b18a --- /dev/null +++ b/src/wp-includes/entity-utils.php @@ -0,0 +1,156 @@ +name && isset( $context->post ) ) { + $post_types[] = $context->post->post_type; + } elseif ( 'core/edit-site' === $context->name ) { + if ( isset( $context->post ) ) { + $post_types[] = $context->post->post_type; + } + + /* + * Adds the post types the screen lists or edits, from the first + * segment of its `p` query arg. The screens are the routes in + * packages/edit-site/src/components/site-editor-routes in Gutenberg. + * The patterns screen lists both patterns and template parts. + */ + $screen_post_types = array( + 'page' => array( 'page' ), + 'template' => array( 'wp_template' ), + 'wp_template' => array( 'wp_template' ), + 'pattern' => array( 'wp_block', 'wp_template_part' ), + 'wp_block' => array( 'wp_block' ), + 'wp_template_part' => array( 'wp_template_part' ), + ); + + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only picks what to preload. + $screen = isset( $_GET['p'] ) && is_string( $_GET['p'] ) ? sanitize_text_field( wp_unslash( $_GET['p'] ) ) : ''; + $segments = explode( '/', trim( $screen, '/' ) ); + if ( isset( $screen_post_types[ $segments[0] ] ) ) { + $post_types = array_merge( $post_types, $screen_post_types[ $segments[0] ] ); + } + } + + foreach ( array_unique( $post_types ) as $post_type ) { + if ( ! post_type_exists( $post_type ) ) { + continue; + } + $paths[] = add_query_arg( + array( + 'kind' => 'postType', + 'name' => $post_type, + ), + $route + ); + } + + return $paths; +} + +/** + * Declares the script modules of an entity API as dependencies of the editor + * script. + * + * The editor imports the script modules of an entity on demand, with a dynamic + * `import()` the browser resolves through the import map of the page. Listing + * the modules as dynamic `module_dependencies` of the `wp-editor` script adds + * them to the import map of every page that loads the editor script: the post + * editor, the site editor, and the pages booted by `@wordpress/boot`, whose + * prerequisites depend on it. A dynamic dependency is only listed in the + * import map; the module is fetched when it is imported. + * + * The modules are read with `$get_modules`, which reads the registry of the + * API and so fires its init action, registering the entities and their script + * modules on demand. It is only called on the pages that load the editor + * script, not on every admin request (heartbeat, Ajax, other screens). The + * callers run on `admin_footer`, the last action before the import map is + * printed that fires both in the admin template and in the pages rendered + * outside it, which print the import map themselves. By then `init` has + * completed, so the post types and supports the registrations derive from are + * final. The registries only accept registrations while their action fires, + * so the modules declared here are those of everything the page can show. + * + * @since 7.2.0 + * @access private + * + * @param WP_Scripts|null $scripts The scripts registry. Null for the global one. + * @param callable $get_modules Returns the module ids of the API, keyed + * by kind, then by name. + */ +function _wp_add_entity_modules_to_editor_script( $scripts, $get_modules ) { + if ( ! $scripts instanceof WP_Scripts ) { + $scripts = wp_scripts(); + } + + if ( ! $scripts->query( 'wp-editor', 'enqueued' ) && ! $scripts->query( 'wp-editor', 'done' ) ) { + return; + } + + $kinds = call_user_func( $get_modules ); + if ( empty( $kinds ) ) { + return; + } + + $dependencies = $scripts->get_data( 'wp-editor', 'module_dependencies' ); + $dependencies = is_array( $dependencies ) ? $dependencies : array(); + + $declared = array(); + foreach ( $dependencies as $dependency ) { + $declared[] = is_array( $dependency ) ? $dependency['id'] : $dependency; + } + + $added = false; + foreach ( $kinds as $entities ) { + foreach ( $entities as $modules ) { + foreach ( $modules as $module ) { + if ( ! in_array( $module, $declared, true ) ) { + $dependencies[] = array( + 'id' => $module, + 'import' => 'dynamic', + ); + $declared[] = $module; + $added = true; + } + } + } + } + + if ( $added ) { + // `add_data()` replaces the value, hence the merge above. + $scripts->add_data( 'wp-editor', 'module_dependencies', $dependencies ); + } +} diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-actions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-actions-controller.php index d72cab8dc26c7..bf9ebcf895a16 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-actions-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-actions-controller.php @@ -197,20 +197,11 @@ public function get_items( $request ) { $kind = $request->get_param( 'kind' ); $name = $request->get_param( 'name' ); - $script_modules = array(); - foreach ( wp_get_registered_entity_action_modules( $kind, $name ) as $module => $action_ids ) { - $script_modules[] = array( - // A numeric module id is an integer as an array key. - 'id' => (string) $module, - 'actions' => array_values( $action_ids ), - ); - } - $response = array( 'kind' => $kind, 'name' => $name, 'actions' => wp_get_registered_entity_actions( $kind, $name ), - 'script_modules' => $script_modules, + 'script_modules' => wp_get_registered_entity_action_modules( $kind, $name ), ); return rest_ensure_response( $response ); diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php index e7674e2a80ba7..c9738f2a7bb6e 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php @@ -159,19 +159,11 @@ public function get_items( $request ) { $fields[] = wp_prepare_json_value_for_client( $field, $field_schema ); } - $script_modules = array(); - foreach ( wp_get_registered_entity_field_modules( $kind, $name ) as $module => $field_ids ) { - $script_modules[] = array( - 'id' => $module, - 'fields' => array_values( $field_ids ), - ); - } - $response = array( 'kind' => $kind, 'name' => $name, 'fields' => $fields, - 'script_modules' => $script_modules, + 'script_modules' => wp_get_registered_entity_field_modules( $kind, $name ), ); return rest_ensure_response( $response ); diff --git a/src/wp-settings.php b/src/wp-settings.php index 8bd37b43d635e..cf8fb4d090781 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -206,6 +206,7 @@ require ABSPATH . WPINC . '/template.php'; require ABSPATH . WPINC . '/class-wp-view-config-data.php'; require ABSPATH . WPINC . '/view-config.php'; +require ABSPATH . WPINC . '/entity-utils.php'; require ABSPATH . WPINC . '/class-wp-entity-actions-registry.php'; require ABSPATH . WPINC . '/entity-actions.php'; require ABSPATH . WPINC . '/class-wp-entity-fields-registry.php'; diff --git a/tests/phpunit/tests/entity-actions.php b/tests/phpunit/tests/entity-actions.php index 2928351e5df10..3951c15ee2d50 100644 --- a/tests/phpunit/tests/entity-actions.php +++ b/tests/phpunit/tests/entity-actions.php @@ -8,6 +8,8 @@ * @group entity-actions * * @covers ::_wp_add_entity_action_modules_to_editor_script + * @covers ::_wp_add_entity_modules_to_editor_script + * @covers ::_wp_add_entity_preload_paths * @covers ::_wp_preload_entity_actions * @covers ::wp_get_all_registered_entity_action_modules * @covers ::wp_register_entity_actions @@ -671,7 +673,16 @@ public function test_a_callback_at_the_default_priority_alters_the_defaults() { $actions, 'The removed action is gone, and the replaced one has the new definition and origin.' ); - $this->assertSame( array( 'core/defaults' => array( 'move-to-trash' ) ), $modules, 'The replaced action keeps the script module of the default.' ); + $this->assertSame( + array( + array( + 'id' => 'core/defaults', + 'actions' => array( 'move-to-trash' ), + ), + ), + $modules, + 'The replaced action keeps the script module of the default.' + ); } /** @@ -717,7 +728,7 @@ public function test_registering_a_registered_action_is_skipped() { $this->assertArrayHasKey( 'feature', $actions, 'The rest of the actions of the call are registered.' ); $this->assertSame( 'Rename…', $actions['rename-post']['label'], 'The registered action is left untouched.' ); $this->assertSame( 'core', $actions['rename-post']['origin']['registeredBy'] ); - $this->assertSame( array( 'feature' ), wp_get_registered_entity_action_modules( 'postType', 'page' )['plugin/actions'], 'The script module only applies to the actions registered.' ); + $this->assertSame( array( 'feature' ), array_column( wp_get_registered_entity_action_modules( 'postType', 'page' ), 'actions', 'id' )['plugin/actions'], 'The script module only applies to the actions registered.' ); $this->assertCount( 1, $reported ); $this->assertStringContainsString( 'postType "page"', $reported[0], 'The notice names the entity.' ); $this->assertStringContainsString( 'already registered: rename-post', $reported[0], 'The notice names the action that is already registered, not the others of the call.' ); @@ -813,9 +824,18 @@ public function test_actions_keep_their_registration_order() { ); $this->assertSame( array( - 'core/defaults' => array( 'rename-post', 'move-to-trash' ), - 'plugin/actions' => array( 'feature', 'archive' ), - 'other-plugin/actions' => array( 'share' ), + array( + 'id' => 'core/defaults', + 'actions' => array( 'rename-post', 'move-to-trash' ), + ), + array( + 'id' => 'plugin/actions', + 'actions' => array( 'feature', 'archive' ), + ), + array( + 'id' => 'other-plugin/actions', + 'actions' => array( 'share' ), + ), ), wp_get_registered_entity_action_modules( 'postType', 'page' ), 'Each module applies to the actions it was registered with, in registration order.' @@ -844,7 +864,16 @@ function () use ( &$result ) { $this->assertSame( array( 'feature' ), array_column( $result['unregistered'], 'id' ), 'Unregistering the first entity only removes its own action.' ); $this->assertSame( array(), wp_get_registered_entity_actions( 'a/b', 'c' ) ); $this->assertSame( array( 'feature', 'archive' ), array_column( wp_get_registered_entity_actions( 'a', 'b/c' ), 'id' ), 'The second entity keeps its actions.' ); - $this->assertSame( array( 'plugin/second' => array( 'feature', 'archive' ) ), wp_get_registered_entity_action_modules( 'a', 'b/c' ), 'The second entity keeps only its own module.' ); + $this->assertSame( + array( + array( + 'id' => 'plugin/second', + 'actions' => array( 'feature', 'archive' ), + ), + ), + wp_get_registered_entity_action_modules( 'a', 'b/c' ), + 'The second entity keeps only its own module.' + ); $this->assertSame( array( 'b/c' => array( 'plugin/second' ) ), wp_get_all_registered_entity_action_modules()['a'], 'Only the second entity has modules under its kind.' ); $this->assertArrayNotHasKey( 'a/b', wp_get_all_registered_entity_action_modules(), 'The kind of the first entity, emptied, is forgotten.' ); } @@ -881,7 +910,16 @@ function () use ( &$unregistered, &$modules ) { $unregistered['by_id'], 'The actions unregistered by id are returned in registration order.' ); - $this->assertSame( array( 'plugin/first' => array( 'b' ) ), $modules['by_id'], 'The unregistered actions leave their modules, and an emptied module is forgotten.' ); + $this->assertSame( + array( + array( + 'id' => 'plugin/first', + 'actions' => array( 'b' ), + ), + ), + $modules['by_id'], + 'The unregistered actions leave their modules, and an emptied module is forgotten.' + ); $this->assertSame( array(), $unregistered['missing'], 'Nothing is returned when no action is registered.' ); $this->assertSame( array( array_merge( $this->action( 'b' ), array( 'origin' => $origin ) ) ), @@ -988,7 +1026,7 @@ static function () use ( &$results ) { ); $this->assertCount( 3, $reported ); $this->assertNotContains( 'feature', array_column( wp_get_registered_entity_actions( 'postType', 'page' ), 'id' ) ); - $this->assertArrayNotHasKey( 'plugin/actions', wp_get_registered_entity_action_modules( 'postType', 'page' ) ); + $this->assertNotContains( 'plugin/actions', array_column( wp_get_registered_entity_action_modules( 'postType', 'page' ), 'id' ) ); } /** @@ -1084,6 +1122,7 @@ public function test_a_numeric_module_id_is_read_as_a_string() { $this->register_actions( 'postType', 'page', array( $this->action( 'feature' ) ), '123' ); $this->assertSame( array( 'core/defaults', '123' ), wp_get_all_registered_entity_action_modules()['postType']['page'] ); + $this->assertSame( array( 'core/defaults', '123' ), array_column( wp_get_registered_entity_action_modules( 'postType', 'page' ), 'id' ) ); } /** @@ -1102,7 +1141,7 @@ public function test_registering_outside_the_action_is_refused() { $this->assertSame( array(), wp_register_entity_actions( 'test-plugin', 'postType', 'page', array( $this->action( 'feature' ) ), 'plugin/feature' ) ); $this->assertSame( $fired, did_action( 'wp_entity_actions_api_init' ), 'Refusing does not fire the action.' ); $this->assertNotContains( 'feature', array_column( wp_get_registered_entity_actions( 'postType', 'page' ), 'id' ) ); - $this->assertArrayNotHasKey( 'plugin/feature', wp_get_registered_entity_action_modules( 'postType', 'page' ) ); + $this->assertNotContains( 'plugin/feature', array_column( wp_get_registered_entity_action_modules( 'postType', 'page' ), 'id' ) ); } /** diff --git a/tests/phpunit/tests/entity-actions/registerCoreEntityActions.php b/tests/phpunit/tests/entity-actions/registerCoreEntityActions.php index c7b960d2349fe..23c0a88d7b2c0 100644 --- a/tests/phpunit/tests/entity-actions/registerCoreEntityActions.php +++ b/tests/phpunit/tests/entity-actions/registerCoreEntityActions.php @@ -164,7 +164,7 @@ public function test_the_actions_of_pages() { self::POST_TYPE_SUPPORTS => $ids, self::PAGE => self::PAGE_ACTIONS, ), - wp_get_registered_entity_action_modules( 'postType', 'page' ), + array_column( wp_get_registered_entity_action_modules( 'postType', 'page' ), 'actions', 'id' ), 'Each action of pages is registered with the module of its folder.' ); $this->assertSame( array( 'core' ), array_unique( array_column( array_column( wp_get_registered_entity_actions( 'postType', 'page' ), 'origin' ), 'registeredBy' ) ), 'Every action is registered by core.' ); @@ -205,7 +205,7 @@ public function test_the_actions_of_patterns() { self::POST_TYPE_SUPPORTS => array_merge( array( 'view-post-revisions', 'rename-post' ), self::EVERY_POST_TYPE ), self::WP_BLOCK => array( 'duplicate-pattern', 'export-pattern' ), ), - wp_get_registered_entity_action_modules( 'postType', 'wp_block' ), + array_column( wp_get_registered_entity_action_modules( 'postType', 'wp_block' ), 'actions', 'id' ), 'The actions of patterns come from the `wp_block` module.' ); } @@ -243,7 +243,7 @@ public function test_template_parts_are_only_duplicated_with_a_block_theme() { ); $this->assertSame( array( 'duplicate-template-part' ), - wp_get_registered_entity_action_modules( 'postType', 'wp_template_part' )[ self::WP_TEMPLATE_PART ], + array_column( wp_get_registered_entity_action_modules( 'postType', 'wp_template_part' ), 'actions', 'id' )[ self::WP_TEMPLATE_PART ], 'The action comes from the `wp_template_part` module.' ); @@ -522,7 +522,7 @@ public function test_a_plugin_can_remove_and_replace_core_actions() { self::POST_TYPE_SUPPORTS => array_merge( $ids, array( 'move-to-trash' ) ), self::PAGE => self::PAGE_ACTIONS, ), - wp_get_registered_entity_action_modules( 'postType', 'page' ), + array_column( wp_get_registered_entity_action_modules( 'postType', 'page' ), 'actions', 'id' ), 'The replaced action keeps the module of the core action.' ); diff --git a/tests/phpunit/tests/entity-fields/wpAddEntityFieldModulesToEditorScript.php b/tests/phpunit/tests/entity-fields/wpAddEntityFieldModulesToEditorScript.php index f345156089e5d..aa7e4929c5360 100644 --- a/tests/phpunit/tests/entity-fields/wpAddEntityFieldModulesToEditorScript.php +++ b/tests/phpunit/tests/entity-fields/wpAddEntityFieldModulesToEditorScript.php @@ -12,6 +12,7 @@ * @group entity-fields * * @covers ::_wp_add_entity_field_modules_to_editor_script + * @covers ::_wp_add_entity_modules_to_editor_script */ class Tests_Entity_Fields_WpAddEntityFieldModulesToEditorScript extends WP_Entity_Fields_UnitTestCase { diff --git a/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php b/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php index e91c910b81c2d..6a035503d1f1f 100644 --- a/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php +++ b/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php @@ -129,7 +129,7 @@ public function test_registering_a_registered_field_is_skipped() { $this->assertArrayHasKey( 'color', $fields, 'The rest of the fields of the call are registered.' ); $this->assertSame( 'Author', $fields['author']['label'], 'The registered field is left untouched.' ); $this->assertSame( 'core', $fields['author']['origin']['registeredBy'] ); - $this->assertSame( array( 'color' ), wp_get_registered_entity_field_modules( 'postType', 'page' )['plugin/fields'], 'The script module only applies to the fields registered.' ); + $this->assertSame( array( 'color' ), array_column( wp_get_registered_entity_field_modules( 'postType', 'page' ), 'fields', 'id' )['plugin/fields'], 'The script module only applies to the fields registered.' ); $this->assertCount( 1, $reported ); $this->assertStringContainsString( 'postType "page"', $reported[0], 'The notice names the entity.' ); $this->assertStringContainsString( 'already registered: author', $reported[0], 'The notice names the field that is already registered, not the others of the call.' ); @@ -217,11 +217,33 @@ function () use ( &$result ) { $this->assertSame( array( 'color' ), array_column( $result['unregistered'], 'id' ), 'Unregistering the first entity only removes its own field.' ); $this->assertSame( array(), wp_get_registered_entity_fields( 'a/b', 'c' ) ); $this->assertSame( array( 'color', 'size' ), array_column( wp_get_registered_entity_fields( 'a', 'b/c' ), 'id' ), 'The second entity keeps its fields.' ); - $this->assertSame( array( 'plugin/second' => array( 'color', 'size' ) ), wp_get_registered_entity_field_modules( 'a', 'b/c' ), 'The second entity keeps only its own module.' ); + $this->assertSame( + array( + array( + 'id' => 'plugin/second', + 'fields' => array( 'color', 'size' ), + ), + ), + wp_get_registered_entity_field_modules( 'a', 'b/c' ), + 'The second entity keeps only its own module.' + ); $this->assertSame( array( 'b/c' => array( 'plugin/second' ) ), wp_get_all_registered_entity_field_modules()['a'], 'Only the second entity has modules under its kind.' ); $this->assertArrayNotHasKey( 'a/b', wp_get_all_registered_entity_field_modules(), 'The kind of the first entity, emptied, is forgotten.' ); } + /** + * A numeric module id is listed as a string, as it was registered, not + * as the integer its array key becomes. + * + * @ticket 66229 + */ + public function test_all_registered_field_modules_lists_a_numeric_module_id_as_a_string() { + $this->register_fields( 'postType', 'post', array( $this->field( 'color' ) ), '42' ); + + $this->assertSame( array( '42' ), wp_get_all_registered_entity_field_modules()['postType']['post'] ); + $this->assertSame( array( '42' ), array_column( wp_get_registered_entity_field_modules( 'postType', 'post' ), 'id' ) ); + } + /** * Unregistering returns the definitions it removes, in registration * order and with their origin, whatever the order of the ids asked for; @@ -322,7 +344,7 @@ static function () use ( &$updated ) { ), $fields[0]['origin'] ); - $modules = wp_get_registered_entity_field_modules( 'postType', 'page' ); + $modules = array_column( wp_get_registered_entity_field_modules( 'postType', 'page' ), 'fields', 'id' ); $this->assertContains( 'author', $modules['test/defaults'], 'The field keeps its modules.' ); $this->assertSame( array( 'author' ), $modules['plugin/author'], 'The module applies to the field.' ); } @@ -396,7 +418,7 @@ static function () use ( &$updated ) { $this->assertArrayNotHasKey( 'color', $fields, 'The field is not registered.' ); $this->assertSame( 'Writer', $fields['author']['label'], 'The rest of the fields of the call are updated.' ); $this->assertSame( array( 'test-plugin' ), $fields['author']['origin']['updatedBy'] ); - $this->assertSame( array( 'author' ), wp_get_registered_entity_field_modules( 'postType', 'page' )['plugin/fields'], 'The script module only applies to the fields updated.' ); + $this->assertSame( array( 'author' ), array_column( wp_get_registered_entity_field_modules( 'postType', 'page' ), 'fields', 'id' )['plugin/fields'], 'The script module only applies to the fields updated.' ); $this->assertCount( 1, $reported ); $this->assertStringContainsString( 'postType "page"', $reported[0], 'The notice names the entity.' ); $this->assertStringContainsString( 'not registered: color', $reported[0], 'The notice names only the field that is not registered.' ); @@ -430,6 +452,7 @@ function () use ( &$registered, &$updated ) { 'label' => 'Numeric id', ), $this->field( 'weight' ), + $this->field( '0' ), ) ); $updated = wp_update_entity_fields( @@ -442,15 +465,20 @@ function () use ( &$registered, &$updated ) { 'id' => 'color', 'label' => 'Colour', ), + array( + 'id' => '0', + 'label' => 'Zero', + ), ) ); } ); - $this->assertSame( array( 'color', 'weight' ), $registered, 'The valid definitions are registered.' ); - $this->assertSame( array( 'color' ), $updated, 'The valid definitions are updated.' ); + $this->assertSame( array( 'color', 'weight', '0' ), $registered, 'The valid definitions are registered, `0` included.' ); + $this->assertSame( array( 'color', '0' ), $updated, 'The valid definitions are updated, `0` included.' ); $fields = array_column( wp_get_registered_entity_fields( 'postType', 'page' ), null, 'id' ); $this->assertSame( 'Colour', $fields['color']['label'] ); + $this->assertSame( 'Zero', $fields['0']['label'] ); $this->assertArrayHasKey( 'weight', $fields ); $this->assertCount( 1, $reported ); $this->assertStringContainsString( 'postType "page"', $reported[0], 'The notice names the entity.' ); @@ -557,7 +585,7 @@ public function test_registering_outside_the_action_is_refused() { $this->assertSame( array(), wp_register_entity_fields( 'test-plugin', 'postType', 'page', array( $this->field( 'color' ) ), 'plugin/color' ) ); $this->assertSame( $fired, did_action( 'wp_entity_fields_api_init' ), 'Refusing does not fire the action.' ); $this->assertNotContains( 'color', array_column( wp_get_registered_entity_fields( 'postType', 'page' ), 'id' ) ); - $this->assertArrayNotHasKey( 'plugin/color', wp_get_registered_entity_field_modules( 'postType', 'page' ) ); + $this->assertNotContains( 'plugin/color', array_column( wp_get_registered_entity_field_modules( 'postType', 'page' ), 'id' ) ); } /** diff --git a/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php b/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php index 58593c5727c36..0c3141326a82b 100644 --- a/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php +++ b/tests/phpunit/tests/entity-fields/wpPreloadEntityFields.php @@ -11,9 +11,8 @@ * * @group entity-fields * + * @covers ::_wp_add_entity_preload_paths * @covers ::_wp_preload_entity_fields - * @covers ::_wp_get_post_type_fields_preload_paths - * @covers ::_wp_get_site_editor_screen_post_types */ class Tests_Entity_Fields_WpPreloadEntityFields extends WP_Entity_Fields_UnitTestCase { @@ -140,10 +139,14 @@ public function test_the_site_editor_preloads_the_fields_of_the_edited_post() { * @ticket 66229 */ public function test_unregistered_post_types_are_not_preloaded() { - $this->assertSame( - array( '/wp/v2/entity-fields?kind=postType&name=page' ), - _wp_get_post_type_fields_preload_paths( array( 'page', 'not_a_post_type', 'page' ) ) + $context = new WP_Block_Editor_Context( + array( + 'name' => 'core/edit-post', + 'post' => new WP_Post( (object) array( 'post_type' => 'not_a_post_type' ) ), + ) ); + + $this->assertSame( array(), _wp_preload_entity_fields( array(), $context ) ); } /** @@ -161,4 +164,60 @@ public function test_other_editors_do_not_preload_the_fields() { $this->assertSame( array(), _wp_preload_entity_fields( array(), $no_post ) ); $this->assertSame( array(), _wp_preload_entity_fields( array(), $widgets ) ); } + + /** + * Empty paths that are not an array become an array the fields are added + * to. Other paths that are not an array are returned unchanged. + * + * @ticket 66229 + * + * @dataProvider data_non_array_paths + * + * @param mixed $paths The paths passed to the filter. + * @param mixed $expected The filtered paths, or null for the fields path + * only. + */ + public function test_non_array_paths( $paths, $expected ) { + $post = self::factory()->post->create_and_get( array( 'post_type' => 'page' ) ); + $context = new WP_Block_Editor_Context( + array( + 'name' => 'core/edit-post', + 'post' => $post, + ) + ); + + if ( null === $expected ) { + $expected = array( '/wp/v2/entity-fields?kind=postType&name=page' ); + } + + $this->assertSame( $expected, _wp_preload_entity_fields( $paths, $context ) ); + } + + /** + * Data provider for test_non_array_paths(). + * + * @return array[] + */ + public function data_non_array_paths() { + $object = new stdClass(); + + return array( + 'null' => array( null, null ), + 'false' => array( false, null ), + 'empty string' => array( '', null ), + 'string' => array( '/wp/v2/types', '/wp/v2/types' ), + 'object' => array( $object, $object ), + ); + } + + /** + * Called with the paths only, as by a filter added with one accepted + * argument, the paths are returned unchanged. + * + * @ticket 66229 + */ + public function test_the_paths_are_unchanged_without_a_context() { + $this->assertSame( array( '/wp/v2/types' ), _wp_preload_entity_fields( array( '/wp/v2/types' ) ) ); + $this->assertSame( array(), _wp_preload_entity_fields( null ) ); + } } diff --git a/tests/phpunit/tests/entity-fields/wpRegisterCoreFieldCollections.php b/tests/phpunit/tests/entity-fields/wpRegisterCoreFieldCollections.php index 7749ccc0a5ec9..7470ec30b4127 100644 --- a/tests/phpunit/tests/entity-fields/wpRegisterCoreFieldCollections.php +++ b/tests/phpunit/tests/entity-fields/wpRegisterCoreFieldCollections.php @@ -209,7 +209,7 @@ public function test_a_custom_post_type_gets_the_default_fields_of_its_supports( $this->assertSame( array( 'core' ), array_unique( array_column( array_column( $fields, 'origin' ), 'registeredBy' ) ), 'The fields carry the origin of WordPress core.' ); $this->assertSame( array( self::POST_TYPE_SUPPORTS => array_column( $fields, 'id' ) ), - wp_get_registered_entity_field_modules( 'postType', 'test_book' ), + array_column( wp_get_registered_entity_field_modules( 'postType', 'test_book' ), 'fields', 'id' ), 'Every default field is registered with the module of its folder.' ); } @@ -363,7 +363,7 @@ public function test_the_defaults_make_no_exception() { */ public function test_pages_get_their_own_title_field() { $fields = self::get_fields( 'postType', 'page' ); - $modules = wp_get_registered_entity_field_modules( 'postType', 'page' ); + $modules = array_column( wp_get_registered_entity_field_modules( 'postType', 'page' ), 'fields', 'id' ); $this->assertFalse( $fields['title']['enableHiding'], 'The page title is the one of the collection.' ); $this->assertSame( 'core', $fields['title']['origin']['registeredBy'], 'The page title is registered by WordPress core.' ); @@ -390,7 +390,7 @@ public function test_templates_get_their_own_fields() { $this->assertSame( 'core', $fields['author']['origin']['registeredBy'], 'The template author is registered by WordPress core.' ); $this->assertSame( $ids, - wp_get_registered_entity_field_modules( 'postType', 'wp_template' )['@wordpress/core-fields/wp_template'], + array_column( wp_get_registered_entity_field_modules( 'postType', 'wp_template' ), 'fields', 'id' )['@wordpress/core-fields/wp_template'], 'The fields of templates ship their JavaScript parts in the module of the collection.' ); } @@ -412,7 +412,7 @@ public function test_template_parts_get_their_own_fields() { $this->assertSame( 'Title', $fields['title']['label'], 'The title is the one of the collection.' ); $this->assertSame( array( 'author', 'title' ), - wp_get_registered_entity_field_modules( 'postType', 'wp_template_part' )['@wordpress/core-fields/wp_template_part'], + array_column( wp_get_registered_entity_field_modules( 'postType', 'wp_template_part' ), 'fields', 'id' )['@wordpress/core-fields/wp_template_part'], 'The fields of template parts ship their JavaScript parts in the module of the collection.' ); } @@ -425,7 +425,7 @@ public function test_template_parts_get_their_own_fields() { */ public function test_patterns_get_their_own_fields() { $fields = self::get_fields( 'postType', 'wp_block' ); - $modules = wp_get_registered_entity_field_modules( 'postType', 'wp_block' ); + $modules = array_column( wp_get_registered_entity_field_modules( 'postType', 'wp_block' ), 'fields', 'id' ); $this->assertTrue( post_type_supports( 'wp_block', 'excerpt' ), 'The post type supports excerpts.' ); $this->assertSame( 'Description', $fields['excerpt']['label'], 'The excerpt is the description of the collection.' ); @@ -486,7 +486,7 @@ public function test_attachments_get_the_media_fields_instead_of_the_defaults() $this->assertSame( 'core', $fields['date']['origin']['registeredBy'], 'The media fields are registered by WordPress core.' ); $this->assertSame( array( '@wordpress/core-fields/attachment' => $ids ), - wp_get_registered_entity_field_modules( 'postType', 'attachment' ), + array_column( wp_get_registered_entity_field_modules( 'postType', 'attachment' ), 'fields', 'id' ), 'Every media field is registered with the module of the collection.' ); } @@ -508,7 +508,7 @@ public function test_the_site_gets_the_fields_of_its_collection() { $this->assertSame( 'core', $fields['title']['origin']['registeredBy'], 'The fields of the site are registered by WordPress core.' ); $this->assertSame( array( '@wordpress/core-fields/root_site' => $ids ), - wp_get_registered_entity_field_modules( 'root', 'site' ), + array_column( wp_get_registered_entity_field_modules( 'root', 'site' ), 'fields', 'id' ), 'Every field of the site is registered with the module of the collection.' ); } @@ -713,7 +713,7 @@ static function () { } ); - $modules = wp_get_registered_entity_field_modules( 'postType', 'wp_template' ); + $modules = array_column( wp_get_registered_entity_field_modules( 'postType', 'wp_template' ), 'fields', 'id' ); $this->assertSame( 'my-plugin', self::get_fields( 'postType', 'wp_template' )['author']['origin']['registeredBy'], 'The author field is the one of the plugin.' ); $this->assertNotContains( 'author', $modules['@wordpress/core-fields/wp_template'], 'The module of the replaced field no longer applies to it.' ); diff --git a/tests/phpunit/tests/entity-fields/wpRegisterEntityFieldCollection.php b/tests/phpunit/tests/entity-fields/wpRegisterEntityFieldCollection.php index cc7bc78f6357f..52e8c5abfc874 100644 --- a/tests/phpunit/tests/entity-fields/wpRegisterEntityFieldCollection.php +++ b/tests/phpunit/tests/entity-fields/wpRegisterEntityFieldCollection.php @@ -100,7 +100,16 @@ public function test_a_collection_places_its_fields() { $book = wp_get_registered_entity_fields( 'postType', 'test_book' ); $this->assertSame( array( 'subtitle', 'isbn' ), array_column( $book, 'id' ), 'The fields registered first come first: the fixture collections run at priority 0.' ); $this->assertSame( 'fixture', $book[0]['origin']['registeredBy'], 'The fields carry the origin of their collection.' ); - $this->assertSame( array( 'fixture/book' => array( 'subtitle' ) ), wp_get_registered_entity_field_modules( 'postType', 'test_book' ), 'Each field is registered with the module of its collection.' ); + $this->assertSame( + array( + array( + 'id' => 'fixture/book', + 'fields' => array( 'subtitle' ), + ), + ), + wp_get_registered_entity_field_modules( 'postType', 'test_book' ), + 'Each field is registered with the module of its collection.' + ); $this->assertSame( array( 'issue' ), array_column( wp_get_registered_entity_fields( 'postType', 'test_magazine' ), 'id' ) ); $this->assertSame( array(), wp_get_registered_entity_field_modules( 'postType', 'test_magazine' ), 'A collection without module registers none.' ); diff --git a/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php b/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php index f6d424817d7df..f5ac37779f8b3 100644 --- a/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php +++ b/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php @@ -593,6 +593,41 @@ public function test_get_items_exposes_registered_fields_and_their_script_module ); } + /** + * A numeric script module id is returned as a string, like any other. + * + * @covers ::get_items + * + * @ticket 66229 + */ + public function test_get_items_returns_a_numeric_module_id_as_a_string() { + $this->register_fields( + 'customKind', + 'customName', + array( + array( + 'id' => 'color', + 'type' => 'text', + 'label' => 'Color', + ), + ), + '123' + ); + + wp_set_current_user( self::$editor_id ); + $data = $this->dispatch_request( 'customKind', 'customName' )->get_data(); + + $this->assertSame( + array( + array( + 'id' => '123', + 'fields' => array( 'color' ), + ), + ), + $data['script_modules'] + ); + } + /** * Fields registered without a script module have none listed. *