Changeset 62056
- Timestamp:
- 03/19/2026 06:50:08 AM (2 weeks ago)
- Location:
- trunk
- Files:
-
- 5 edited
-
src/wp-includes/class-wp-connector-registry.php (modified) (4 diffs)
-
tests/phpunit/includes/wp-ai-client-mock-provider-trait.php (modified) (3 diffs)
-
tests/phpunit/tests/connectors/wpConnectorRegistry.php (modified) (18 diffs)
-
tests/phpunit/tests/connectors/wpConnectorsGetConnectorSettings.php (modified) (3 diffs)
-
tests/phpunit/tests/connectors/wpConnectorsIsApiKeyValid.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-connector-registry.php
r62032 r62056 68 68 * Validates the provided arguments and stores the connector in the registry. 69 69 * For connectors with `api_key` authentication, a `setting_name` is automatically 70 * generated using the pattern `connectors_ai_{$id}_api_key` (e.g., connector ID 71 * `openai` produces `connectors_ai_openai_api_key`). This setting name is used 72 * for the Settings API registration and REST API exposure. 70 * generated using the pattern `connectors_ai_{$id}_api_key`, with hyphens in the ID 71 * normalized to underscores (e.g., connector ID `openai` produces 72 * `connectors_ai_openai_api_key`, and `azure-openai` produces 73 * `connectors_ai_azure_openai_api_key`). This setting name is used for the Settings 74 * API registration and REST API exposure. 73 75 * 74 76 * Registering a connector with an ID that is already registered will trigger a … … 81 83 * 82 84 * @param string $id The unique connector identifier. Must match the pattern 83 * `/^[a-z0-9_ ]+$/` (lowercase alphanumericand underscores only).85 * `/^[a-z0-9_-]+$/` (lowercase alphanumeric, hyphens, and underscores only). 84 86 * @param array $args { 85 87 * An associative array of arguments for the connector. … … 107 109 */ 108 110 public function register( string $id, array $args ): ?array { 109 if ( ! preg_match( '/^[a-z0-9_ ]+$/', $id ) ) {111 if ( ! preg_match( '/^[a-z0-9_-]+$/', $id ) ) { 110 112 _doing_it_wrong( 111 113 __METHOD__, 112 114 __( 113 'Connector ID must contain only lowercase alphanumeric characters and underscores.'115 'Connector ID must contain only lowercase alphanumeric characters, hyphens, and underscores.' 114 116 ), 115 117 '7.0.0' … … 186 188 $connector['authentication']['credentials_url'] = $args['authentication']['credentials_url']; 187 189 } 188 $connector['authentication']['setting_name'] = "connectors_ai_{$id}_api_key";190 $connector['authentication']['setting_name'] = 'connectors_ai_' . str_replace( '-', '_', $id ) . '_api_key'; 189 191 } 190 192 -
trunk/tests/phpunit/includes/wp-ai-client-mock-provider-trait.php
r61943 r62056 97 97 protected static function createProviderMetadata(): ProviderMetadata { 98 98 return new ProviderMetadata( 99 'mock _connectors_test',99 'mock-connectors-test', 100 100 'Mock Connectors Test', 101 101 ProviderTypeEnum::cloud(), … … 157 157 private static function register_mock_connectors_provider(): void { 158 158 $ai_registry = AiClient::defaultRegistry(); 159 if ( ! $ai_registry->hasProvider( 'mock _connectors_test' ) ) {159 if ( ! $ai_registry->hasProvider( 'mock-connectors-test' ) ) { 160 160 $ai_registry->registerProvider( Mock_Connectors_Test_Provider::class ); 161 161 } … … 163 163 // Also register in the WP connector registry if not already present. 164 164 $connector_registry = WP_Connector_Registry::get_instance(); 165 if ( null !== $connector_registry && ! $connector_registry->is_registered( 'mock _connectors_test' ) ) {165 if ( null !== $connector_registry && ! $connector_registry->is_registered( 'mock-connectors-test' ) ) { 166 166 $connector_registry->register( 167 'mock _connectors_test',167 'mock-connectors-test', 168 168 array( 169 169 'name' => 'Mock Connectors Test', -
trunk/tests/phpunit/tests/connectors/wpConnectorRegistry.php
r61943 r62056 47 47 */ 48 48 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 ); 50 50 51 51 $this->assertIsArray( $result ); … … 62 62 */ 63 63 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 ); 65 74 66 75 $this->assertSame( 'connectors_ai_my_ai_api_key', $result['authentication']['setting_name'] ); … … 76 85 'authentication' => array( 'method' => 'none' ), 77 86 ); 78 $result = $this->registry->register( 'no _auth', $args );87 $result = $this->registry->register( 'no-auth', $args ); 79 88 80 89 $this->assertIsArray( $result ); … … 92 101 ); 93 102 94 $result = $this->registry->register( 'minimal ', $args );103 $result = $this->registry->register( 'minimal-provider', $args ); 95 104 96 105 $this->assertSame( '', $result['description'] ); … … 104 113 $args['logo_url'] = 'https://example.com/logo.png'; 105 114 106 $result = $this->registry->register( 'with _logo', $args );115 $result = $this->registry->register( 'with-logo', $args ); 107 116 108 117 $this->assertArrayHasKey( 'logo_url', $result ); … … 114 123 */ 115 124 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 ); 117 126 118 127 $this->assertArrayNotHasKey( 'logo_url', $result ); … … 126 135 $args['logo_url'] = ''; 127 136 128 $result = $this->registry->register( 'empty _logo', $args );137 $result = $this->registry->register( 'empty-logo', $args ); 129 138 130 139 $this->assertArrayNotHasKey( 'logo_url', $result ); … … 138 147 $args['plugin'] = array( 'slug' => 'my-plugin' ); 139 148 140 $result = $this->registry->register( 'with _plugin', $args );149 $result = $this->registry->register( 'with-plugin', $args ); 141 150 142 151 $this->assertArrayHasKey( 'plugin', $result ); … … 148 157 */ 149 158 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 ); 151 160 152 161 $this->assertArrayNotHasKey( 'plugin', $result ); … … 165 174 166 175 /** 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() { 172 179 $result = $this->registry->register( 'my-provider', self::$default_args ); 173 180 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 ); 175 191 } 176 192 … … 192 208 $this->setExpectedIncorrectUsage( 'WP_Connector_Registry::register' ); 193 209 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 ); 196 212 197 213 $this->assertNull( $result ); … … 207 223 unset( $args['name'] ); 208 224 209 $result = $this->registry->register( 'no _name', $args );225 $result = $this->registry->register( 'no-name', $args ); 210 226 211 227 $this->assertNull( $result ); … … 221 237 $args['name'] = ''; 222 238 223 $result = $this->registry->register( 'empty _name', $args );239 $result = $this->registry->register( 'empty-name', $args ); 224 240 225 241 $this->assertNull( $result ); … … 235 251 unset( $args['type'] ); 236 252 237 $result = $this->registry->register( 'no _type', $args );253 $result = $this->registry->register( 'no-type', $args ); 238 254 239 255 $this->assertNull( $result ); … … 249 265 unset( $args['authentication'] ); 250 266 251 $result = $this->registry->register( 'no _auth', $args );267 $result = $this->registry->register( 'no-auth', $args ); 252 268 253 269 $this->assertNull( $result ); … … 263 279 $args['authentication']['method'] = 'oauth'; 264 280 265 $result = $this->registry->register( 'bad _auth', $args );281 $result = $this->registry->register( 'bad-auth', $args ); 266 282 267 283 $this->assertNull( $result ); … … 288 304 */ 289 305 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' ); 293 309 294 310 $this->assertIsArray( $result ); … … 335 351 */ 336 352 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' ); 340 356 341 357 $this->assertIsArray( $result ); 342 358 $this->assertSame( 'Test Provider', $result['name'] ); 343 $this->assertFalse( $this->registry->is_registered( 'to _remove' ) );359 $this->assertFalse( $this->registry->is_registered( 'to-remove' ) ); 344 360 } 345 361 -
trunk/tests/phpunit/tests/connectors/wpConnectorsGetConnectorSettings.php
r61983 r62056 38 38 $this->assertArrayHasKey( 'openai', $connectors ); 39 39 $this->assertArrayHasKey( 'anthropic', $connectors ); 40 $this->assertArrayHasKey( 'mock _connectors_test', $connectors );40 $this->assertArrayHasKey( 'mock-connectors-test', $connectors ); 41 41 $this->assertCount( 4, $connectors ); 42 42 } … … 81 81 $this->assertArrayHasKey( 'setting_name', $connector_data['authentication'], "Connector '{$connector_id}' authentication is missing 'setting_name'." ); 82 82 $this->assertSame( 83 "connectors_ai_{$connector_id}_api_key",83 'connectors_ai_' . str_replace( '-', '_', $connector_id ) . '_api_key', 84 84 $connector_data['authentication']['setting_name'] ?? null, 85 85 "Connector '{$connector_id}' setting_name does not match expected format." … … 106 106 public function test_includes_registered_provider_from_registry(): void { 107 107 $connectors = wp_get_connectors(); 108 $mock = $connectors['mock _connectors_test'];108 $mock = $connectors['mock-connectors-test']; 109 109 110 110 $this->assertSame( 'Mock Connectors Test', $mock['name'] ); -
trunk/tests/phpunit/tests/connectors/wpConnectorsIsApiKeyValid.php
r61824 r62056 50 50 self::set_mock_provider_configured( true ); 51 51 52 $result = _wp_connectors_is_ai_api_key_valid( 'test-key', 'mock _connectors_test' );52 $result = _wp_connectors_is_ai_api_key_valid( 'test-key', 'mock-connectors-test' ); 53 53 54 54 $this->assertTrue( $result ); … … 63 63 self::set_mock_provider_configured( false ); 64 64 65 $result = _wp_connectors_is_ai_api_key_valid( 'test-key', 'mock _connectors_test' );65 $result = _wp_connectors_is_ai_api_key_valid( 'test-key', 'mock-connectors-test' ); 66 66 67 67 $this->assertFalse( $result );
Note: See TracChangeset
for help on using the changeset viewer.