Make WordPress Core


Ignore:
Timestamp:
05/28/2026 07:46:15 AM (2 months ago)
Author:
gziolo
Message:

Abilities API: Unify schema conventions across core abilities

Align core/get-site-info, core/get-user-info, and core/get-environment-info on a shared blueprint: every output property carries a Title Case title and a description, core/get-environment-info gains the optional fields input parameter the other two already accept, and core/get-user-info is exposed via REST. Descriptions are also tightened for programmatic consumers, and registration tests now lock the exact ordered set of property keys.

Props gziolo, westonruter.
Fixes #65355.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/abilities-api/wpRegisterCoreAbilities.php

    r62419 r62426  
    7171                $this->assertSame( array(), $input_schema['default'] );
    7272
    73                 // Input schema should have optional fields array.
    7473                $this->assertArrayHasKey( 'fields', $input_schema['properties'] );
    7574                $this->assertSame( 'array', $input_schema['properties']['fields']['type'] );
    76                 $this->assertContains( 'name', $input_schema['properties']['fields']['items']['enum'] );
    77 
    78                 // Output schema should have all fields documented.
    79                 $this->assertArrayHasKey( 'name', $output_schema['properties'] );
    80                 $this->assertArrayHasKey( 'url', $output_schema['properties'] );
    81                 $this->assertArrayHasKey( 'version', $output_schema['properties'] );
     75
     76                $expected_fields = array( 'name', 'description', 'url', 'wpurl', 'admin_email', 'charset', 'language', 'version' );
     77
     78                $this->assertSame( $expected_fields, $input_schema['properties']['fields']['items']['enum'] );
     79                $this->assertSame( $expected_fields, array_keys( $output_schema['properties'] ) );
     80
     81                foreach ( $expected_fields as $field ) {
     82                        $this->assertArrayHasKey( 'title', $output_schema['properties'][ $field ] );
     83                        $this->assertArrayHasKey( 'description', $output_schema['properties'][ $field ] );
     84                }
    8285        }
    8386
     
    201204
    202205                $this->assertInstanceOf( WP_Ability::class, $ability );
     206                $this->assertTrue( $ability->get_meta_item( 'show_in_rest', false ) );
    203207
    204208                $input_schema  = $ability->get_input_schema();
    205209                $output_schema = $ability->get_output_schema();
    206210
    207                 // Input schema should expose an optional `fields` array with an enum of valid field names.
    208211                $this->assertSame( 'object', $input_schema['type'] );
    209212                $this->assertArrayHasKey( 'default', $input_schema );
     
    212215                $this->assertSame( 'array', $input_schema['properties']['fields']['type'] );
    213216
    214                 $enum = $input_schema['properties']['fields']['items']['enum'];
    215                 foreach ( array( 'id', 'display_name', 'first_name', 'last_name', 'nickname', 'description', 'user_url' ) as $field ) {
    216                         $this->assertContains( $field, $enum );
    217                 }
    218 
    219                 // Output schema should document the original and new profile fields with title + description.
    220                 foreach ( array( 'id', 'display_name', 'first_name', 'last_name', 'nickname', 'description', 'user_url' ) as $field ) {
    221                         $this->assertArrayHasKey( $field, $output_schema['properties'] );
     217                $expected_fields = array( 'id', 'display_name', 'user_nicename', 'user_login', 'roles', 'locale', 'first_name', 'last_name', 'nickname', 'description', 'user_url' );
     218
     219                $this->assertSame( $expected_fields, $input_schema['properties']['fields']['items']['enum'] );
     220                $this->assertSame( $expected_fields, array_keys( $output_schema['properties'] ) );
     221
     222                foreach ( $expected_fields as $field ) {
    222223                        $this->assertArrayHasKey( 'title', $output_schema['properties'][ $field ] );
    223224                        $this->assertArrayHasKey( 'description', $output_schema['properties'][ $field ] );
     
    297298                $this->assertArrayHasKey( 'wp_version', $ability_data );
    298299                $this->assertSame( $environment, $ability_data['environment'] );
     300        }
     301
     302        /**
     303         * Tests that the `core/get-environment-info` ability is registered with the expected schema.
     304         *
     305         * @ticket 65355
     306         */
     307        public function test_core_get_environment_info_ability_is_registered(): void {
     308                $ability = wp_get_ability( 'core/get-environment-info' );
     309
     310                $this->assertInstanceOf( WP_Ability::class, $ability );
     311                $this->assertTrue( $ability->get_meta_item( 'show_in_rest', false ) );
     312
     313                $input_schema  = $ability->get_input_schema();
     314                $output_schema = $ability->get_output_schema();
     315
     316                $this->assertSame( 'object', $input_schema['type'] );
     317                $this->assertArrayHasKey( 'default', $input_schema );
     318                $this->assertSame( array(), $input_schema['default'] );
     319                $this->assertArrayHasKey( 'fields', $input_schema['properties'] );
     320                $this->assertSame( 'array', $input_schema['properties']['fields']['type'] );
     321
     322                $expected_fields = array( 'environment', 'php_version', 'db_server_info', 'wp_version' );
     323
     324                $this->assertSame( $expected_fields, $input_schema['properties']['fields']['items']['enum'] );
     325                $this->assertSame( $expected_fields, array_keys( $output_schema['properties'] ) );
     326
     327                foreach ( $expected_fields as $field ) {
     328                        $this->assertArrayHasKey( 'title', $output_schema['properties'][ $field ] );
     329                        $this->assertArrayHasKey( 'description', $output_schema['properties'][ $field ] );
     330                }
     331        }
     332
     333        /**
     334         * Tests that the `core/get-environment-info` ability filters its output by the `fields` input parameter.
     335         *
     336         * @ticket 65355
     337         */
     338        public function test_core_get_environment_info_filters_fields(): void {
     339                $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
     340                wp_set_current_user( $admin_id );
     341
     342                $ability = wp_get_ability( 'core/get-environment-info' );
     343
     344                $result = $ability->execute(
     345                        array(
     346                                'fields' => array( 'environment', 'wp_version' ),
     347                        )
     348                );
     349
     350                $this->assertIsArray( $result );
     351                $this->assertCount( 2, $result );
     352                $this->assertArrayHasKey( 'environment', $result );
     353                $this->assertArrayHasKey( 'wp_version', $result );
     354                $this->assertArrayNotHasKey( 'php_version', $result );
     355                $this->assertArrayNotHasKey( 'db_server_info', $result );
     356        }
     357
     358        /**
     359         * Tests that the `core/get-environment-info` ability rejects unknown field names via schema validation.
     360         *
     361         * @ticket 65355
     362         */
     363        public function test_core_get_environment_info_rejects_invalid_fields(): void {
     364                $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
     365                wp_set_current_user( $admin_id );
     366
     367                $ability = wp_get_ability( 'core/get-environment-info' );
     368
     369                $result = $ability->execute(
     370                        array(
     371                                'fields' => array( 'environment', 'not_a_real_field' ),
     372                        )
     373                );
     374
     375                $this->assertWPError( $result );
     376                $this->assertSame( 'ability_invalid_input', $result->get_error_code() );
    299377        }
    300378
Note: See TracChangeset for help on using the changeset viewer.