From 017dfdfea5d3580d27b0c72a3abfb6dabbc29e0c Mon Sep 17 00:00:00 2001 From: Olayinka Vaughan Date: Sun, 22 Nov 2026 17:42:28 +0000 Subject: [PATCH] feat: add navigation menus query --- src/Categories.php | 19 +- src/Manifest.php | 47 +- .../Navigation_Menus_Query.php | 532 ++++++++++++++++++ tests/Abilities/NavigationMenusQueryTest.php | 361 ++++++++++++ 4 files changed, 933 insertions(+), 26 deletions(-) create mode 100644 src/Navigation_Menus/Navigation_Menus_Query.php create mode 100644 tests/Abilities/NavigationMenusQueryTest.php diff --git a/src/Categories.php b/src/Categories.php index 89b79c2..8d4b6a3 100644 --- a/src/Categories.php +++ b/src/Categories.php @@ -19,10 +19,9 @@ * Definitions mirror core's default ability category registration * (wordpress-develop trunk, wp_register_core_ability_categories()) verbatim, * including being passed through the `default` text domain at declaration - * time, so the package's declarations are indistinguishable from core's own - * on 7.2+ hosts — with one exception: the `media` category is package-authored - * (core trunk registers no media category; nothing to copy verbatim from - * trunk). This is not a general category registry: keep an entry in + * time, so the package's declarations match core's own on 7.2+ hosts. + * The `media` and `site-editing` categories are package-authored because + * core does not register them. This is not a general category registry: keep an entry in * this table only for as long as the manifest declares a category that * needs it, and keep the strings in sync when a trunk refresh changes them. * @@ -40,15 +39,19 @@ final class Categories { * @var array */ private const DEFINITIONS = array( - 'site' => array( + 'site-editing' => array( + 'label' => 'Site Editing', + 'description' => 'Abilities that retrieve or modify site structure and presentation.', + ), + 'site' => array( 'label' => 'Site', 'description' => 'Abilities that retrieve or modify site information and settings.', ), - 'user' => array( + 'user' => array( 'label' => 'User', 'description' => 'Abilities that retrieve or modify user information and settings.', ), - 'content' => array( + 'content' => array( 'label' => 'Content', 'description' => 'Abilities that retrieve or manage posts and other content.', ), @@ -60,7 +63,7 @@ final class Categories { * with the same trunk refresh cadence as the entries above. If core lands * a media category, replace this definition with core's verbatim strings. */ - 'media' => array( + 'media' => array( 'label' => 'Media', 'description' => 'Abilities that retrieve or manage media items and attachments.', ), diff --git a/src/Manifest.php b/src/Manifest.php index 2c263d1..9a3626e 100644 --- a/src/Manifest.php +++ b/src/Manifest.php @@ -42,7 +42,18 @@ private function __construct() {} */ public static function all(): array { return array( - 'core/content-query' => array( + 'wp/navigation-menus-query' => array( + 'name' => 'wp/navigation-menus-query', + 'class' => Navigation_Menus\Navigation_Menus_Query::class, + 'category' => 'site-editing', + 'tier' => self::TIER_EXPERIMENTAL, + 'min_wp_version' => '6.9', + 'core_since' => null, + 'requires' => array(), + 'renamed_from' => null, + 'proposal' => 'https://github.com/WordPress/ai/pull/858', + ), + 'core/content-query' => array( 'name' => 'core/content-query', /* * `::class` resolves to the FQCN without loading the class, @@ -60,7 +71,7 @@ public static function all(): array { // the landed core work (wordpress-develop PR 12195) before release. 'proposal' => 'https://github.com/WordPress/wordpress-develop/pull/12195', ), - 'core/settings-get' => array( + 'core/settings-get' => array( 'name' => 'core/settings-get', 'class' => Settings\Settings_Get::class, 'category' => 'site', @@ -72,7 +83,7 @@ public static function all(): array { // Landed in trunk as b6512b1e8c (Trac #64605), developed in this PR. 'proposal' => 'https://github.com/WordPress/wordpress-develop/pull/12141', ), - 'core/users-query' => array( + 'core/users-query' => array( 'name' => 'core/users-query', 'class' => Users\Users_Query::class, 'category' => 'user', @@ -93,7 +104,7 @@ public static function all(): array { * filing). The entries flip to core with `core_since` set once * the abilities land in an actual WordPress release. */ - 'core/content-create' => array( + 'core/content-create' => array( 'name' => 'core/content-create', 'class' => Content\Content_Create::class, 'category' => 'content', @@ -104,7 +115,7 @@ public static function all(): array { 'renamed_from' => null, 'proposal' => 'https://github.com/WordPress/ai/pull/1025', ), - 'core/content-update' => array( + 'core/content-update' => array( 'name' => 'core/content-update', 'class' => Content\Content_Update::class, 'category' => 'content', @@ -115,7 +126,7 @@ public static function all(): array { 'renamed_from' => null, 'proposal' => 'https://github.com/WordPress/ai/pull/1025', ), - 'core/content-delete' => array( + 'core/content-delete' => array( 'name' => 'core/content-delete', 'class' => Content\Content_Delete::class, 'category' => 'content', @@ -137,7 +148,7 @@ public static function all(): array { * the package-authored `media` ability category (core trunk has no * media category; see Categories). */ - 'wp/media-query' => array( + 'wp/media-query' => array( 'name' => 'wp/media-query', 'class' => Media\Media_Query::class, 'category' => 'media', @@ -161,7 +172,7 @@ public static function all(): array { * They use the package-authored `media` ability category (see * Categories). */ - 'wp/media-upload' => array( + 'wp/media-upload' => array( 'name' => 'wp/media-upload', 'class' => Media\Media_Upload::class, 'category' => 'media', @@ -172,7 +183,7 @@ public static function all(): array { 'renamed_from' => null, 'proposal' => 'https://github.com/WordPress/ai/pull/1104', ), - 'wp/media-update' => array( + 'wp/media-update' => array( 'name' => 'wp/media-update', 'class' => Media\Media_Update::class, 'category' => 'media', @@ -183,7 +194,7 @@ public static function all(): array { 'renamed_from' => null, 'proposal' => 'https://github.com/WordPress/ai/pull/1104', ), - 'wp/media-delete' => array( + 'wp/media-delete' => array( 'name' => 'wp/media-delete', 'class' => Media\Media_Delete::class, 'category' => 'media', @@ -208,7 +219,7 @@ public static function all(): array { * core (`core_since` stays null and flips when core lands it), so its * name and schema carry no promises yet (NAME-LIFECYCLE.md). */ - 'wp/settings-update' => array( + 'wp/settings-update' => array( 'name' => 'wp/settings-update', 'class' => Settings\Settings_Update::class, 'category' => 'site', @@ -233,7 +244,7 @@ public static function all(): array { * core (`core_since` stays null and flips when core lands them), so * their names and schemas carry no promises yet (NAME-LIFECYCLE.md). */ - 'wp/user-create' => array( + 'wp/user-create' => array( 'name' => 'wp/user-create', 'class' => Users\User_Create::class, 'category' => 'user', @@ -244,7 +255,7 @@ public static function all(): array { 'renamed_from' => null, 'proposal' => 'https://github.com/WordPress/ai/pull/1105', ), - 'wp/user-update' => array( + 'wp/user-update' => array( 'name' => 'wp/user-update', 'class' => Users\User_Update::class, 'category' => 'user', @@ -255,7 +266,7 @@ public static function all(): array { 'renamed_from' => null, 'proposal' => 'https://github.com/WordPress/ai/pull/1105', ), - 'wp/user-delete' => array( + 'wp/user-delete' => array( 'name' => 'wp/user-delete', 'class' => Users\User_Delete::class, 'category' => 'user', @@ -278,7 +289,7 @@ public static function all(): array { * pre-7.2 hosts), and the taxonomy opt-in `show_in_abilities` is * native on 7.2+ and polyfilled on 6.9-7.1. */ - 'wp/terms-query' => array( + 'wp/terms-query' => array( 'name' => 'wp/terms-query', 'class' => Terms\Terms_Query::class, 'category' => 'content', @@ -312,7 +323,7 @@ public static function all(): array { * it on pre-7.2 hosts), and the taxonomy opt-in they ride is the * `taxonomy_show_in_abilities` support feature, as for the query. */ - 'wp/term-create' => array( + 'wp/term-create' => array( 'name' => 'wp/term-create', 'class' => Terms\Term_Create::class, 'category' => 'content', @@ -323,7 +334,7 @@ public static function all(): array { 'renamed_from' => null, 'proposal' => 'https://github.com/WordPress/ai/pull/1103', ), - 'wp/term-update' => array( + 'wp/term-update' => array( 'name' => 'wp/term-update', 'class' => Terms\Term_Update::class, 'category' => 'content', @@ -334,7 +345,7 @@ public static function all(): array { 'renamed_from' => null, 'proposal' => 'https://github.com/WordPress/ai/pull/1103', ), - 'wp/term-delete' => array( + 'wp/term-delete' => array( 'name' => 'wp/term-delete', 'class' => Terms\Term_Delete::class, 'category' => 'content', diff --git a/src/Navigation_Menus/Navigation_Menus_Query.php b/src/Navigation_Menus/Navigation_Menus_Query.php new file mode 100644 index 0000000..380d533 --- /dev/null +++ b/src/Navigation_Menus/Navigation_Menus_Query.php @@ -0,0 +1,532 @@ +get_nav_menus_input_schema(); + } + + protected function output_schema(): array { + return $this->get_nav_menus_output_schema(); + } + + protected function make_execute_callback(): callable { + return function ( $input = array() ) { + return $this->execute_get_nav_menus( $input ); + }; + } + + protected function make_permission_callback(): callable { + return function (): bool { + return $this->check_permission(); + }; + } + + protected function required_capabilities(): array { + return array(); + } + + protected function ability_meta(): array { + return array( + 'annotations' => array( + 'readonly' => true, + 'destructive' => false, + 'idempotent' => true, + ), + 'show_in_rest' => true, + ); + } + + /** + * Permission callback for the `wp/navigation-menus-query` ability. + * + * Nav menus are ordinarily rendered to every site visitor, but a menu not currently + * assigned to any location, or the full location-assignment map, exposes more of the + * site's structure than the front end does. The ability is gated behind the same + * capability WordPress requires to manage menus in wp-admin. + * + * @since x.x.x + * + * @return bool True if the current user can manage nav menus. + */ + public function check_permission(): bool { + return current_user_can( 'edit_theme_options' ); + } + + /** + * Executes the `wp/navigation-menus-query` ability. + * + * @since x.x.x + * + * @param mixed $input Optional. The ability input. Default empty array. + * @return array|\WP_Error Menu data, collection data, or a WP_Error on failure. + */ + public function execute_get_nav_menus( $input = array() ) { + $input = $this->to_input_array( $input ); + + if ( array_key_exists( 'id', $input ) ) { + return $this->get_single_menu_response( $this->input_int( $input['id'] ) ); + } + + if ( array_key_exists( 'slug', $input ) ) { + return $this->get_single_menu_response( is_string( $input['slug'] ) ? $input['slug'] : '' ); + } + + if ( array_key_exists( 'location', $input ) ) { + return $this->get_menu_by_location_response( is_string( $input['location'] ) ? $input['location'] : '' ); + } + + $search = isset( $input['search'] ) && is_string( $input['search'] ) ? $input['search'] : ''; + + return $this->get_collection_response( $search ); + } + + /** + * Casts raw ability input to an array. + * + * Schema validation accepts object input, so it must be treated as equivalent to its + * array form rather than discarded. Any other non-array input is replaced with an + * empty array. + * + * @since x.x.x + * + * @param mixed $input The raw ability input. + * @return array The input as an array. + */ + private function to_input_array( $input ): array { + if ( $input instanceof \stdClass ) { + $input = (array) $input; + } + + return is_array( $input ) ? $input : array(); + } + + /** + * Casts a raw input value to a non-negative integer. + * + * @since x.x.x + * + * @param mixed $value The raw input value. + * @return int The value as a non-negative integer, or 0 when not scalar. + */ + private function input_int( $value ): int { + return is_scalar( $value ) ? absint( $value ) : 0; + } + + /** + * Resolves and formats a single menu by ID or slug. + * + * @since x.x.x + * + * @param int|string $menu Menu ID or slug. + * @return array|\WP_Error The formatted menu, or a WP_Error when not found. + */ + private function get_single_menu_response( $menu ) { + $term = '' !== $menu && 0 !== $menu ? wp_get_nav_menu_object( $menu ) : false; + + if ( ! $term instanceof WP_Term ) { + return new WP_Error( + 'ability_invalid_input', + __( 'The requested nav menu was not found.' ) + ); + } + + return $this->format_menu( $term, true ); + } + + /** + * Resolves and formats the menu currently assigned to a registered theme location. + * + * @since x.x.x + * + * @param string $location Registered theme location slug. + * @return array|\WP_Error The formatted menu, or a WP_Error when the + * location has no assigned menu. + */ + private function get_menu_by_location_response( string $location ) { + $locations = get_nav_menu_locations(); + $menu_id = isset( $locations[ $location ] ) ? (int) $locations[ $location ] : 0; + + if ( 0 === $menu_id ) { + return new WP_Error( + 'ability_invalid_input', + __( 'No nav menu is assigned to that location.' ) + ); + } + + return $this->get_single_menu_response( $menu_id ); + } + + /** + * Builds the collection response: every nav menu, alongside registered theme locations + * and their current menu assignments. + * + * @since x.x.x + * + * @param string $search Optional. Limit menus to this name or slug search term. Default empty string. + * @return array The collection response. + */ + private function get_collection_response( string $search ): array { + $args = array(); + if ( '' !== $search ) { + $args['search'] = $search; + } + + $menus = array(); + /** @var mixed $term */ + foreach ( wp_get_nav_menus( $args ) as $term ) { + if ( ! $term instanceof WP_Term ) { + continue; + } + $menus[] = $this->format_menu( $term, false ); + } + + $registered_locations = array(); + foreach ( get_registered_nav_menus() as $location => $label ) { + $registered_locations[ $location ] = (string) $label; + } + + $location_assignments = array(); + foreach ( get_nav_menu_locations() as $location => $menu_id ) { + if ( 0 === (int) $menu_id ) { + continue; + } + $location_assignments[ $location ] = (int) $menu_id; + } + + return array( + 'menus' => $menus, + // Cast to object so an empty map serializes as {}, not [], consistent with the + // output schema's `object` type for these two properties. + 'registered_locations' => (object) $registered_locations, + 'location_assignments' => (object) $location_assignments, + ); + } + + /** + * Formats a nav menu term into the ability output shape. + * + * @since x.x.x + * + * @param \WP_Term $term The nav menu term. + * @param bool $with_items Whether to include the menu's items. + * @return array The formatted menu data. + */ + private function format_menu( WP_Term $term, bool $with_items ): array { + $menu_id = (int) $term->term_id; + + $data = array( + 'id' => $menu_id, + 'name' => (string) $term->name, + 'slug' => (string) $term->slug, + 'count' => (int) $term->count, + 'locations' => $this->get_menu_locations( $menu_id ), + ); + + if ( $with_items ) { + $items = wp_get_nav_menu_items( $term ); + $data['items'] = is_array( $items ) ? array_map( array( $this, 'format_menu_item' ), $items ) : array(); + } + + return $data; + } + + /** + * Finds the registered theme locations currently assigned to a menu. + * + * A menu can be assigned to more than one location at once, so this walks every + * assignment rather than doing a single reverse lookup. + * + * @since x.x.x + * + * @param int $menu_id The nav menu term ID. + * @return string[] The location slugs this menu is assigned to. + */ + private function get_menu_locations( int $menu_id ): array { + $locations = array(); + foreach ( get_nav_menu_locations() as $location => $assigned_menu_id ) { + if ( (int) $assigned_menu_id !== $menu_id ) { + continue; + } + $locations[] = $location; + } + + return $locations; + } + + /** + * Formats a single nav menu item into the ability output shape. + * + * Nav menu items are `WP_Post` objects, but the fields below (other than `ID` and + * `menu_order`, which are native post fields) are dynamic properties that + * wp_setup_nav_menu_item() adds at runtime and are not part of WP_Post's declared + * property list. They are read via get_object_vars() rather than direct property + * access so static analysis does not treat them as undefined. + * + * @since x.x.x + * + * @param \WP_Post $item A nav menu item, as returned by wp_get_nav_menu_items(). + * @return array The formatted menu item data. + */ + private function format_menu_item( WP_Post $item ): array { + $fields = get_object_vars( $item ); + $classes = isset( $fields['classes'] ) && is_array( $fields['classes'] ) ? $fields['classes'] : array(); + + return array( + 'id' => (int) $item->ID, + 'parent' => isset( $fields['menu_item_parent'] ) ? (int) $fields['menu_item_parent'] : 0, + 'title' => isset( $fields['title'] ) ? (string) $fields['title'] : '', + 'url' => isset( $fields['url'] ) ? (string) $fields['url'] : '', + 'target' => isset( $fields['target'] ) ? (string) $fields['target'] : '', + 'classes' => array_values( array_filter( $classes, static fn( $menu_item_class ) => '' !== $menu_item_class ) ), + 'description' => isset( $fields['description'] ) ? (string) $fields['description'] : '', + 'menu_order' => (int) $item->menu_order, + 'type' => isset( $fields['type'] ) ? (string) $fields['type'] : '', + 'object' => isset( $fields['object'] ) ? (string) $fields['object'] : '', + 'object_id' => isset( $fields['object_id'] ) ? (int) $fields['object_id'] : 0, + ); + } + + /** + * Builds the input schema for the `wp/navigation-menus-query` ability. + * + * The ability has four mutually exclusive modes, modeled as a `oneOf` so invalid + * combinations are rejected rather than silently ignored: + * + * - Get a single menu, with its items, by `id`. + * - Get a single menu, with its items, by `slug`. + * - Get the menu, with its items, currently assigned to a registered theme `location`. + * - Query the full collection of menus, optionally filtered by `search`. + * + * @since x.x.x + * + * @return array The input JSON Schema. + */ + private function get_nav_menus_input_schema(): array { + // Input enum intentionally reflects registered theme locations at ability + // registration time. This makes the schema a stable contract that themes and + // plugins can filter when registering the ability, matching how the `roles` enum + // is resolved in the plugin's Users ability. + $locations = array_keys( get_registered_nav_menus() ); + + return array( + 'type' => 'object', + 'default' => (object) array(), + 'oneOf' => array( + array( + 'title' => __( 'Get a single nav menu, with its items, by ID' ), + 'required' => array( 'id' ), + 'additionalProperties' => false, + 'properties' => array( + 'id' => array( + 'type' => 'integer', + 'minimum' => 1, + 'description' => __( 'Retrieve a single nav menu, with its items, by its term ID.' ), + ), + ), + ), + array( + 'title' => __( 'Get a single nav menu, with its items, by slug' ), + 'required' => array( 'slug' ), + 'additionalProperties' => false, + 'properties' => array( + 'slug' => array( + 'type' => 'string', + 'description' => __( 'Retrieve a single nav menu, with its items, by its slug.' ), + ), + ), + ), + array( + 'title' => __( 'Get the nav menu assigned to a registered theme location' ), + 'required' => array( 'location' ), + 'additionalProperties' => false, + 'properties' => array( + 'location' => array( + 'type' => 'string', + 'enum' => $locations, + 'description' => __( 'Retrieve the nav menu, with its items, currently assigned to this registered theme location.' ), + ), + ), + ), + array( + 'title' => __( 'Query all nav menus' ), + 'additionalProperties' => false, + 'properties' => array( + 'search' => array( + 'type' => 'string', + 'description' => __( 'Limit the returned menus to those whose name or slug matches this search term.' ), + ), + ), + ), + ), + ); + } + + /** + * Builds the output schema for the `wp/navigation-menus-query` ability. + * + * Single-menu modes (by ID, slug, or location) return the menu object directly, with its + * items. Collection mode returns every menu without items, alongside the registered + * theme locations and their current menu assignments. + * + * @since x.x.x + * + * @return array The output JSON Schema. + */ + private function get_nav_menus_output_schema(): array { + $menu_item_schema = array( + 'type' => 'object', + 'additionalProperties' => false, + 'properties' => array( + 'id' => array( + 'type' => 'integer', + 'description' => __( 'The menu item ID.' ), + ), + 'parent' => array( + 'type' => 'integer', + 'description' => __( 'The ID of the parent menu item, or 0 for a top-level item.' ), + ), + 'title' => array( + 'type' => 'string', + 'description' => __( 'The menu item title.' ), + ), + 'url' => array( + 'type' => 'string', + 'description' => __( 'The URL the menu item links to.' ), + ), + 'target' => array( + 'type' => 'string', + 'description' => __( 'The link target attribute (for example _blank), or an empty string.' ), + ), + 'classes' => array( + 'type' => 'array', + 'items' => array( 'type' => 'string' ), + 'description' => __( 'CSS classes applied to the menu item.' ), + ), + 'description' => array( + 'type' => 'string', + 'description' => __( 'The menu item description.' ), + ), + 'menu_order' => array( + 'type' => 'integer', + 'description' => __( 'The menu item order among its siblings.' ), + ), + 'type' => array( + 'type' => 'string', + 'description' => __( 'The menu item type: custom, post_type, or taxonomy.' ), + ), + 'object' => array( + 'type' => 'string', + 'description' => __( 'The underlying object type for post_type and taxonomy items (for example page or category). Empty for custom links.' ), + ), + 'object_id' => array( + 'type' => 'integer', + 'description' => __( 'The ID of the linked post or term, or 0 for a custom link.' ), + ), + ), + ); + + $menu_properties = array( + 'id' => array( + 'type' => 'integer', + 'description' => __( 'The nav menu term ID.' ), + ), + 'name' => array( + 'type' => 'string', + 'description' => __( 'The nav menu name.' ), + ), + 'slug' => array( + 'type' => 'string', + 'description' => __( 'The nav menu slug.' ), + ), + 'count' => array( + 'type' => 'integer', + 'description' => __( 'Number of items in the menu.' ), + ), + 'locations' => array( + 'type' => 'array', + 'items' => array( 'type' => 'string' ), + 'description' => __( 'Registered theme locations this menu is currently assigned to.' ), + ), + ); + + $menu_schema = array( + 'type' => 'object', + 'additionalProperties' => false, + 'properties' => array_merge( + $menu_properties, + array( + 'items' => array( + 'type' => 'array', + 'items' => $menu_item_schema, + 'description' => __( 'The menu items, in menu order.' ), + ), + ) + ), + ); + + $menu_summary_schema = array( + 'type' => 'object', + 'additionalProperties' => false, + 'properties' => $menu_properties, + ); + + $collection_schema = array( + 'type' => 'object', + 'additionalProperties' => false, + 'required' => array( 'menus', 'registered_locations', 'location_assignments' ), + 'properties' => array( + 'menus' => array( + 'type' => 'array', + 'items' => $menu_summary_schema, + 'description' => __( 'All nav menus on the site.' ), + ), + 'registered_locations' => array( + 'type' => 'object', + 'description' => __( 'Registered theme locations, keyed by location slug, with a human-readable label.' ), + 'additionalProperties' => array( 'type' => 'string' ), + ), + 'location_assignments' => array( + 'type' => 'object', + 'description' => __( 'The nav menu term ID currently assigned to each registered theme location, keyed by location slug. A location with no assigned menu is omitted.' ), + 'additionalProperties' => array( 'type' => 'integer' ), + ), + ), + ); + + return array( + 'oneOf' => array( + $menu_schema, + $collection_schema, + ), + ); + } +} diff --git a/tests/Abilities/NavigationMenusQueryTest.php b/tests/Abilities/NavigationMenusQueryTest.php new file mode 100644 index 0000000..fcbc4d0 --- /dev/null +++ b/tests/Abilities/NavigationMenusQueryTest.php @@ -0,0 +1,361 @@ +menu_id = (int) wp_create_nav_menu( 'Test Menu' ); + + wp_update_nav_menu_item( + $this->menu_id, + 0, + array( + 'menu-item-title' => 'Home', + 'menu-item-url' => home_url( '/' ), + 'menu-item-status' => 'publish', + 'menu-item-type' => 'custom', + ) + ); + + set_theme_mod( 'nav_menu_locations', array( 'primary' => $this->menu_id ) ); + } + + /** Registers the package before the abilities registry boots. */ + public static function set_up_before_class(): void { + register_nav_menu( 'primary', 'Primary Menu' ); + register_nav_menu( 'footer', 'Footer Menu' ); + Registrar::register_all(); + WP_Abilities_Registry::get_instance(); + } + + /** + * Tear down test case. + * + * @since x.x.x + */ + public function tear_down(): void { + wp_delete_nav_menu( $this->menu_id ); + remove_theme_mod( 'nav_menu_locations' ); + $user_id = get_current_user_id(); + wp_set_current_user( 0 ); + if ( $user_id ) { + if ( ! function_exists( 'wp_delete_user' ) ) { + require_once ABSPATH . 'wp-admin/includes/user.php'; + } + wp_delete_user( $user_id ); + } + + parent::tear_down(); + } + + /** + * Logs in as an administrator so the ability's permission check passes. + * + * @since x.x.x + */ + private function become_admin(): void { + wp_set_current_user( + wp_insert_user( + array( + 'user_login' => 'nav-test-admin', + 'user_pass' => 'unused', + 'role' => 'administrator', + ) + ) + ); + } + + /** + * The ability is registered in the `site-editing` category and flagged read-only. + * + * @since x.x.x + */ + public function test_navigation_menus_query_ability_is_registered(): void { + $ability = wp_get_ability( 'wp/navigation-menus-query' ); + + $this->assertInstanceOf( WP_Ability::class, $ability ); + $this->assertSame( 'wp/navigation-menus-query', $ability->get_name() ); + $this->assertSame( 'site-editing', $ability->get_category() ); + $this->assertSame( 'experimental', $ability->get_meta_item( 'stability', '' ) ); + $this->assertSame( 'object', $ability->get_input_schema()['type'] ); + $this->assertCount( 2, $ability->get_output_schema()['oneOf'] ); + $this->assertTrue( $ability->get_meta_item( 'show_in_rest', false ) ); + + $annotations = $ability->get_meta_item( 'annotations', array() ); + $this->assertTrue( $annotations['readonly'] ); + $this->assertFalse( $annotations['destructive'] ); + $this->assertTrue( $annotations['idempotent'] ); + } + + /** + * A single menu, with its items, is returned by ID. + * + * @since x.x.x + */ + public function test_navigation_menus_query_returns_a_single_menu_by_id(): void { + $this->become_admin(); + + $result = wp_get_ability( 'wp/navigation-menus-query' )->execute( array( 'id' => $this->menu_id ) ); + + $this->assertIsArray( $result ); + $this->assertSame( $this->menu_id, $result['id'] ); + $this->assertSame( 'Test Menu', $result['name'] ); + $this->assertSame( array( 'primary' ), $result['locations'] ); + $this->assertCount( 1, $result['items'] ); + $this->assertSame( 'Home', $result['items'][0]['title'] ); + $this->assertSame( home_url( '/' ), $result['items'][0]['url'] ); + } + + /** + * A single menu, with its items, is returned by slug. + * + * @since x.x.x + */ + public function test_navigation_menus_query_returns_a_single_menu_by_slug(): void { + $this->become_admin(); + + $result = wp_get_ability( 'wp/navigation-menus-query' )->execute( array( 'slug' => 'test-menu' ) ); + + $this->assertSame( $this->menu_id, $result['id'] ); + $this->assertCount( 1, $result['items'] ); + } + + /** + * The menu assigned to a registered theme location is returned. + * + * @since x.x.x + */ + public function test_navigation_menus_query_returns_the_menu_assigned_to_a_location(): void { + $this->become_admin(); + + $result = wp_get_ability( 'wp/navigation-menus-query' )->execute( array( 'location' => 'primary' ) ); + + $this->assertSame( $this->menu_id, $result['id'] ); + } + + /** + * A location with no assigned menu errors instead of returning an empty result. + * + * @since x.x.x + */ + public function test_navigation_menus_query_errors_when_location_has_no_assigned_menu(): void { + register_nav_menu( 'footer', 'Footer Menu' ); + + $this->become_admin(); + + $result = wp_get_ability( 'wp/navigation-menus-query' )->execute( array( 'location' => 'footer' ) ); + + $this->assertInstanceOf( \WP_Error::class, $result ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code() ); + } + + /** + * An unknown menu ID errors instead of returning an empty result. + * + * @since x.x.x + */ + public function test_navigation_menus_query_errors_when_menu_not_found(): void { + $this->become_admin(); + + $result = wp_get_ability( 'wp/navigation-menus-query' )->execute( array( 'id' => 999999 ) ); + + $this->assertInstanceOf( \WP_Error::class, $result ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code() ); + } + + /** + * Without input, the ability lists every menu alongside registered locations and their + * current assignments. + * + * @since x.x.x + */ + public function test_navigation_menus_query_lists_all_menus_with_registered_locations_and_assignments(): void { + $this->become_admin(); + + $result = wp_get_ability( 'wp/navigation-menus-query' )->execute( array() ); + + $this->assertCount( 1, $result['menus'] ); + $this->assertSame( $this->menu_id, $result['menus'][0]['id'] ); + $this->assertArrayNotHasKey( 'items', $result['menus'][0] ); + + $this->assertSame( 'Primary Menu', $result['registered_locations']->primary ); + $this->assertSame( $this->menu_id, $result['location_assignments']->primary ); + } + + /** + * The `search` filter narrows the collection to menus matching the term. + * + * @since x.x.x + */ + public function test_navigation_menus_query_filters_collection_by_search(): void { + $this->become_admin(); + + $result = wp_get_ability( 'wp/navigation-menus-query' )->execute( array( 'search' => 'Nonexistent' ) ); + + $this->assertSame( array(), $result['menus'] ); + } + + /** + * Users without `edit_theme_options` cannot run the ability. + * + * @since x.x.x + */ + public function test_navigation_menus_query_requires_edit_theme_options(): void { + wp_set_current_user( + wp_insert_user( + array( + 'user_login' => 'nav-test-subscriber', + 'user_pass' => 'unused', + 'role' => 'subscriber', + ) + ) + ); + + $result = wp_get_ability( 'wp/navigation-menus-query' )->execute( array( 'id' => $this->menu_id ) ); + + $this->assertInstanceOf( \WP_Error::class, $result ); + $this->assertSame( 'ability_invalid_permissions', $result->get_error_code() ); + } + + /** + * Schema-valid object input behaves like its array form. + * + * @since x.x.x + */ + public function test_navigation_menus_query_accepts_object_input(): void { + $this->become_admin(); + + $result = wp_get_ability( 'wp/navigation-menus-query' )->execute( + (object) array( + 'id' => $this->menu_id, + ) + ); + + $this->assertIsArray( $result ); + $this->assertSame( $this->menu_id, $result['id'] ); + } + + /** + * Non-array and non-object inputs fall back to default collection queries. + * + * @since x.x.x + */ + public function test_navigation_menus_query_handles_non_array_input(): void { + $this->become_admin(); + + $nav_menus = new Navigation_Menus_Query(); + $result = $nav_menus->execute_get_nav_menus( null ); + + $this->assertIsArray( $result ); + $this->assertArrayHasKey( 'menus', $result ); + } + + /** + * Non-WP_Term elements returned by wp_get_nav_menus are skipped in collection queries. + * + * @since x.x.x + */ + public function test_navigation_menus_query_skips_non_term_elements_in_collection(): void { + $this->become_admin(); + + $filter = static function ( $menus ) { + return array_merge( (array) $menus, array( false ) ); + }; + add_filter( 'wp_get_nav_menus', $filter ); + + try { + $result = wp_get_ability( 'wp/navigation-menus-query' )->execute( array() ); + } finally { + remove_filter( 'wp_get_nav_menus', $filter ); + } + + $this->assertCount( 1, $result['menus'] ); + $this->assertSame( $this->menu_id, $result['menus'][0]['id'] ); + } + + /** + * Registered theme locations with no assigned menu (ID 0) are omitted from assignments. + * + * @since x.x.x + */ + public function test_navigation_menus_query_omits_unassigned_locations_from_assignments(): void { + register_nav_menu( 'footer', 'Footer Menu' ); + set_theme_mod( + 'nav_menu_locations', + array( + 'primary' => $this->menu_id, + 'footer' => 0, + ) + ); + + $this->become_admin(); + + $result = wp_get_ability( 'wp/navigation-menus-query' )->execute( array() ); + + $this->assertTrue( property_exists( $result['location_assignments'], 'primary' ) ); + $this->assertSame( $this->menu_id, $result['location_assignments']->primary ); + $this->assertFalse( property_exists( $result['location_assignments'], 'footer' ) ); + } + + /** + * Menu locations assigned to a different menu are skipped when resolving menu locations. + * + * @since x.x.x + */ + public function test_navigation_menus_query_filters_out_other_menu_locations(): void { + register_nav_menu( 'footer', 'Footer Menu' ); + $second_menu_id = (int) wp_create_nav_menu( 'Second Menu' ); + + set_theme_mod( + 'nav_menu_locations', + array( + 'primary' => $this->menu_id, + 'footer' => $second_menu_id, + ) + ); + + $this->become_admin(); + + try { + $result = wp_get_ability( 'wp/navigation-menus-query' )->execute( array( 'id' => $this->menu_id ) ); + + $this->assertSame( array( 'primary' ), $result['locations'] ); + } finally { + wp_delete_nav_menu( $second_menu_id ); + } + } +}