Make WordPress Core


Ignore:
Timestamp:
03/19/2026 06:50:08 AM (8 months ago)
Author:
gziolo
Message:

Connectors: Allow hyphens in connector IDs

Expands the connector ID validation regex from /^[a-z0-9_]+$/ to /^[a-z0-9_-]+$/, aligning with the PHP AI Client library naming conventions. Hyphens are normalized to underscores when generating setting_name (e.g., azure-openai → connectors_ai_azure_openai_api_key).

Developed in ​https://github.com/WordPress/wordpress-develop/pull/11285.

Props pers, gziolo, jorgefilipecosta, westonruter, flixos90, mukesh27.
Fixes #64861.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/connectors/wpConnectorRegistry.php

    r61943 r62056  
    4747         */
    4848        public function test_register_returns_connector_data() {
    49                 $result = $this->registry->register( 'test_provider', self::$default_args );
     49                $result = $this->registry->register( 'test-provider', self::$default_args );
    5050
    5151                $this->assertIsArray( $result );
    … …  
    6262         */
    6363        public function test_register_generates_setting_name_for_api_key() {
    64                 $result = $this->registry->register( 'my_ai', self::$default_args );
     64                $result = $this->registry->register( 'myai', self::$default_args );
     65
     66                $this->assertSame( 'connectors_ai_myai_api_key', $result['authentication']['setting_name'] );
     67        }
     68
     69        /**
     70         * @ticket 64861
     71         */
     72        public function test_register_generates_setting_name_normalizes_hyphens() {
     73                $result = $this->registry->register( 'my-ai', self::$default_args );
    6574
    6675                $this->assertSame( 'connectors_ai_my_ai_api_key', $result['authentication']['setting_name'] );
    … …  
    7685                        'authentication' => array( 'method' => 'none' ),
    7786                );
    78                 $result = $this->registry->register( 'no_auth', $args );
     87                $result = $this->registry->register( 'no-auth', $args );
    7988
    8089                $this->assertIsArray( $result );
    … …  
    92101                );
    93102
    94                 $result = $this->registry->register( 'minimal', $args );
     103                $result = $this->registry->register( 'minimal-provider', $args );
    95104
    96105                $this->assertSame( '', $result['description'] );
    … …  
    104113                $args['logo_url'] = 'https://example.com/logo.png';
    105114
    106                 $result = $this->registry->register( 'with_logo', $args );
     115                $result = $this->registry->register( 'with-logo', $args );
    107116
    108117                $this->assertArrayHasKey( 'logo_url', $result );
    … …  
    114123         */
    115124        public function test_register_omits_logo_url_when_not_provided() {
    116                 $result = $this->registry->register( 'no_logo', self::$default_args );
     125                $result = $this->registry->register( 'no-logo', self::$default_args );
    117126
    118127                $this->assertArrayNotHasKey( 'logo_url', $result );
    … …  
    126135                $args['logo_url'] = '';
    127136
    128                 $result = $this->registry->register( 'empty_logo', $args );
     137                $result = $this->registry->register( 'empty-logo', $args );
    129138
    130139                $this->assertArrayNotHasKey( 'logo_url', $result );
    … …  
    138147                $args['plugin'] = array( 'slug' => 'my-plugin' );
    139148
    140                 $result = $this->registry->register( 'with_plugin', $args );
     149                $result = $this->registry->register( 'with-plugin', $args );
    141150
    142151                $this->assertArrayHasKey( 'plugin', $result );
    … …  
    148157         */
    149158        public function test_register_omits_plugin_when_not_provided() {
    150                 $result = $this->registry->register( 'no_plugin', self::$default_args );
     159                $result = $this->registry->register( 'no-plugin', self::$default_args );
    151160
    152161                $this->assertArrayNotHasKey( 'plugin', $result );
    … …  
    165174
    166175        /**
    167          * @ticket 64791
    168          */
    169         public function test_register_rejects_invalid_id_with_dashes() {
    170                 $this->setExpectedIncorrectUsage( 'WP_Connector_Registry::register' );
    171 
     176         * @ticket 64861
     177         */
     178        public function test_register_accepts_id_with_hyphens() {
    172179                $result = $this->registry->register( 'my-provider', self::$default_args );
    173180
    174                 $this->assertNull( $result );
     181                $this->assertIsArray( $result );
     182        }
     183
     184        /**
     185         * @ticket 64861
     186         */
     187        public function test_register_accepts_id_with_underscores() {
     188                $result = $this->registry->register( 'my_provider', self::$default_args );
     189
     190                $this->assertIsArray( $result );
    175191        }
    176192
    … …  
    192208                $this->setExpectedIncorrectUsage( 'WP_Connector_Registry::register' );
    193209
    194                 $this->registry->register( 'duplicate', self::$default_args );
    195                 $result = $this->registry->register( 'duplicate', self::$default_args );
     210                $this->registry->register( 'test-duplicate', self::$default_args );
     211                $result = $this->registry->register( 'test-duplicate', self::$default_args );
    196212
    197213                $this->assertNull( $result );
    … …  
    207223                unset( $args['name'] );
    208224
    209                 $result = $this->registry->register( 'no_name', $args );
     225                $result = $this->registry->register( 'no-name', $args );
    210226
    211227                $this->assertNull( $result );
    … …  
    221237                $args['name'] = '';
    222238
    223                 $result = $this->registry->register( 'empty_name', $args );
     239                $result = $this->registry->register( 'empty-name', $args );
    224240
    225241                $this->assertNull( $result );
    … …  
    235251                unset( $args['type'] );
    236252
    237                 $result = $this->registry->register( 'no_type', $args );
     253                $result = $this->registry->register( 'no-type', $args );
    238254
    239255                $this->assertNull( $result );
    … …  
    249265                unset( $args['authentication'] );
    250266
    251                 $result = $this->registry->register( 'no_auth', $args );
     267                $result = $this->registry->register( 'no-auth', $args );
    252268
    253269                $this->assertNull( $result );
    … …  
    263279                $args['authentication']['method'] = 'oauth';
    264280
    265                 $result = $this->registry->register( 'bad_auth', $args );
     281                $result = $this->registry->register( 'bad-auth', $args );
    266282
    267283                $this->assertNull( $result );
    … …  
    288304         */
    289305        public function test_get_registered_returns_connector_data() {
    290                 $this->registry->register( 'my_connector', self::$default_args );
    291 
    292                 $result = $this->registry->get_registered( 'my_connector' );
     306                $this->registry->register( 'my-connector', self::$default_args );
     307
     308                $result = $this->registry->get_registered( 'my-connector' );
    293309
    294310                $this->assertIsArray( $result );
    … …  
    335351         */
    336352        public function test_unregister_removes_connector() {
    337                 $this->registry->register( 'to_remove', self::$default_args );
    338 
    339                 $result = $this->registry->unregister( 'to_remove' );
     353                $this->registry->register( 'to-remove', self::$default_args );
     354
     355                $result = $this->registry->unregister( 'to-remove' );
    340356
    341357                $this->assertIsArray( $result );
    342358                $this->assertSame( 'Test Provider', $result['name'] );
    343                 $this->assertFalse( $this->registry->is_registered( 'to_remove' ) );
     359                $this->assertFalse( $this->registry->is_registered( 'to-remove' ) );
    344360        }
    345361
Note: See TracChangeset for help on using the changeset viewer.