const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); const SETTINGS_PAGE_PATH = 'options-general.php'; const CONNECTORS_PAGE_QUERY = 'page=options-connectors-wp-admin'; const CONNECTORS = [ { slug: 'ai-provider-for-openai', name: 'OpenAI', description: 'Text and image generation with GPT and Dall-E.', }, { slug: 'ai-provider-for-anthropic', name: 'Anthropic', description: 'Text generation with Claude.', }, { slug: 'ai-provider-for-google', name: 'Google', description: 'Text and image generation with Gemini and Imagen.', }, ]; const TEST_PROVIDER_NAME = 'Test Provider'; const TEST_PROVIDER_DESCRIPTION = 'A test AI provider for E2E testing.'; const getConnectorCardByName = ( page, name ) => page .locator( '.components-item' ) .filter( { has: page.getByRole( 'heading', { name, level: 2 } ), } ) .first(); test.describe( 'Connectors', () => { test( 'should show a Connectors link in the Settings menu', async ( { page, admin, } ) => { await admin.visitAdminPage( SETTINGS_PAGE_PATH ); const settingsMenu = page.locator( '#menu-settings' ); const connectorsLink = settingsMenu.getByRole( 'link', { name: 'Connectors', } ); await expect( connectorsLink ).toBeVisible(); await expect( connectorsLink ).toHaveAttribute( 'href', `${ SETTINGS_PAGE_PATH }?${ CONNECTORS_PAGE_QUERY }` ); } ); test( 'should display default providers with install buttons', async ( { page, admin, } ) => { await admin.visitAdminPage( SETTINGS_PAGE_PATH, CONNECTORS_PAGE_QUERY ); // Verify the page title is an h1 heading. const pageTitle = page.getByRole( 'heading', { name: 'Connectors', level: 1, } ); await expect( pageTitle ).toBeVisible(); // Verify each connector card shows name as heading, description, and Install button. for ( const { slug, name, description } of CONNECTORS ) { const card = page.locator( `.connector-item--${ slug }` ); await expect( card ).toBeVisible(); // Connector name should be an h2 heading. const heading = card.getByRole( 'heading', { name, level: 2 } ); await expect( heading ).toBeVisible(); await expect( card.getByText( description ) ).toBeVisible(); // Connector should be wrapped in a group with the heading as label. const group = card.getByRole( 'group' ); await expect( group ).toBeVisible(); await expect( heading ).toHaveAttribute( 'id', /\S/ ); const headingId = await heading.getAttribute( 'id' ); await expect( group ).toHaveAttribute( 'aria-labelledby', headingId ); const button = card.getByRole( 'button', { name: 'Install' } ); await expect( button ).toBeVisible(); // Install button should not have aria-expanded until expanded. await expect( button ).not.toHaveAttribute( 'aria-expanded' ); } // Verify the plugin directory search link is present. await expect( page.getByRole( 'link', { name: 'search the plugin directory', } ) ).toHaveAttribute( 'href', 'plugin-install.php?s=connector&tab=search&type=tag' ); } ); test.describe( 'Test provider setup flow', () => { const PLUGIN_SLUG = 'gutenberg-test-connectors-provider'; const VALID_API_KEY = 'test-api-key-123'; test.beforeAll( async ( { requestUtils } ) => { await requestUtils.activatePlugin( PLUGIN_SLUG ); } ); test.afterEach( async ( { requestUtils } ) => { await requestUtils.rest( { path: '/wp/v2/settings', method: 'POST', data: { connectors_ai_test_provider_api_key: '', }, } ); } ); test.afterAll( async ( { requestUtils } ) => { await requestUtils.deactivatePlugin( PLUGIN_SLUG ); } ); test( 'should display the test provider with a "Set up" button', async ( { page, admin, } ) => { await admin.visitAdminPage( SETTINGS_PAGE_PATH, CONNECTORS_PAGE_QUERY ); const testProviderCard = getConnectorCardByName( page, TEST_PROVIDER_NAME ); await expect( testProviderCard ).toBeVisible(); await expect( testProviderCard.getByText( TEST_PROVIDER_DESCRIPTION ) ).toBeVisible(); // The test provider has no plugin dependency so it should show "Set up". await expect( testProviderCard.getByRole( 'button', { name: 'Set up' } ) ).toBeVisible(); } ); test( 'should expand the API key form when clicking "Set up"', async ( { page, admin, } ) => { await admin.visitAdminPage( SETTINGS_PAGE_PATH, CONNECTORS_PAGE_QUERY ); const testProviderCard = getConnectorCardByName( page, TEST_PROVIDER_NAME ); const setupButton = testProviderCard.getByRole( 'button', { name: 'Set up', } ); await setupButton.click(); // The form should now be visible with an API Key field and Save button. await expect( testProviderCard.getByPlaceholder( 'Enter your API key' ) ).toBeVisible(); await expect( testProviderCard.getByRole( 'button', { name: 'Save' } ) ).toBeVisible(); // The button label should change to "Cancel". await expect( testProviderCard.getByRole( 'button', { name: 'Cancel' } ) ).toBeVisible(); } ); test( 'should reject an invalid API key', async ( { page, admin } ) => { await admin.visitAdminPage( SETTINGS_PAGE_PATH, CONNECTORS_PAGE_QUERY ); const testProviderCard = getConnectorCardByName( page, TEST_PROVIDER_NAME ); await testProviderCard .getByRole( 'button', { name: 'Set up' } ) .click(); const apiKeyInput = testProviderCard.getByPlaceholder( 'Enter your API key' ); await apiKeyInput.fill( 'wrong-key' ); await testProviderCard .getByRole( 'button', { name: 'Save' } ) .click(); // Should show an error message with role="alert" for screen readers. const errorAlert = testProviderCard.getByRole( 'alert' ); await expect( errorAlert ).toBeVisible(); await expect( errorAlert ).toContainText( 'It was not possible to connect' ); } ); test( 'should accept a valid API key and show "Connected"', async ( { page, admin, } ) => { await admin.visitAdminPage( SETTINGS_PAGE_PATH, CONNECTORS_PAGE_QUERY ); const testProviderCard = getConnectorCardByName( page, TEST_PROVIDER_NAME ); await testProviderCard .getByRole( 'button', { name: 'Set up' } ) .click(); const apiKeyInput = testProviderCard.getByPlaceholder( 'Enter your API key' ); await apiKeyInput.fill( VALID_API_KEY ); await testProviderCard .getByRole( 'button', { name: 'Save' } ) .click(); // The form should close and show the "Connected" badge. await expect( testProviderCard.getByText( 'Connected', { exact: true } ) ).toBeVisible(); // The button should now show "Edit" instead of "Set up". const editButton = testProviderCard.getByRole( 'button', { name: 'Edit', } ); await expect( editButton ).toBeVisible(); // Focus should be on the Edit button after save. await expect( editButton ).toBeFocused(); } ); test( 'should keep focus on the action button when toggling "Set up" / "Cancel"', async ( { page, admin, } ) => { await admin.visitAdminPage( SETTINGS_PAGE_PATH, CONNECTORS_PAGE_QUERY ); const testProviderCard = getConnectorCardByName( page, TEST_PROVIDER_NAME ); // Focus the "Set up" button and activate with keyboard. const setupButton = testProviderCard.getByRole( 'button', { name: 'Set up', } ); await setupButton.focus(); await page.keyboard.press( 'Enter' ); // Focus should remain on the button, now labeled "Cancel". const cancelButton = testProviderCard.getByRole( 'button', { name: 'Cancel', } ); await expect( cancelButton ).toBeFocused(); // Press Enter again to collapse. await page.keyboard.press( 'Enter' ); // Focus should remain on the button, now labeled "Set up" again. const setupButtonAgain = testProviderCard.getByRole( 'button', { name: 'Set up', } ); await expect( setupButtonAgain ).toBeFocused(); } ); test( 'should complete the full setup flow using only the keyboard', async ( { page, admin, } ) => { await admin.visitAdminPage( SETTINGS_PAGE_PATH, CONNECTORS_PAGE_QUERY ); const testProviderCard = getConnectorCardByName( page, TEST_PROVIDER_NAME ); // Focus and activate the "Set up" button with keyboard. const setupButton = testProviderCard.getByRole( 'button', { name: 'Set up', } ); await setupButton.focus(); await page.keyboard.press( 'Enter' ); // Tab into the API key input. await page.keyboard.press( 'Tab' ); await expect( testProviderCard.getByPlaceholder( 'Enter your API key' ) ).toBeFocused(); // Type a valid API key. await page.keyboard.type( VALID_API_KEY ); // Tab to the Save button. await page.keyboard.press( 'Tab' ); await expect( testProviderCard.getByRole( 'button', { name: 'Save' } ) ).toBeFocused(); // Press Enter to save. await page.keyboard.press( 'Enter' ); // Wait for the connected state. await expect( testProviderCard.getByText( 'Connected', { exact: true } ) ).toBeVisible(); // Focus should be on the "Edit" button. await expect( testProviderCard.getByRole( 'button', { name: 'Edit' } ) ).toBeFocused(); } ); test( 'should keep focus in the form after a failed save', async ( { page, admin, } ) => { await admin.visitAdminPage( SETTINGS_PAGE_PATH, CONNECTORS_PAGE_QUERY ); const testProviderCard = getConnectorCardByName( page, TEST_PROVIDER_NAME ); await testProviderCard .getByRole( 'button', { name: 'Set up' } ) .click(); const apiKeyInput = testProviderCard.getByPlaceholder( 'Enter your API key' ); await apiKeyInput.fill( 'wrong-key' ); await testProviderCard .getByRole( 'button', { name: 'Save' } ) .click(); // Error alert should be visible. await expect( testProviderCard.getByRole( 'alert' ) ).toBeVisible(); // The panel should still be expanded (Cancel button visible). await expect( testProviderCard.getByRole( 'button', { name: 'Cancel' } ) ).toBeVisible(); // Focus should NOT be on the action button — the form stays // open so the user can correct their key. await expect( testProviderCard.getByRole( 'button', { name: 'Cancel' } ) ).not.toBeFocused(); } ); } ); test( 'should display the AI plugin callout banner with install button', async ( { page, admin, } ) => { await admin.visitAdminPage( SETTINGS_PAGE_PATH, CONNECTORS_PAGE_QUERY ); const banner = page.locator( '.ai-plugin-callout' ); await expect( banner ).toBeVisible(); // Verify the banner message mentions the AI plugin. await expect( banner.locator( 'p' ).getByText( 'AI plugin' ) ).toBeVisible(); // Verify the Install button is present. await expect( banner.getByRole( 'button', { name: 'Install the AI plugin' } ) ).toBeVisible(); // Verify the Learn more link is present. await expect( banner.getByRole( 'link', { name: 'Learn more' } ) ).toBeVisible(); } ); test.describe( 'Empty state', () => { const PLUGIN_SLUG = 'gutenberg-test-connectors-empty-state'; test.beforeAll( async ( { requestUtils } ) => { await requestUtils.activatePlugin( PLUGIN_SLUG ); } ); test.afterAll( async ( { requestUtils } ) => { await requestUtils.deactivatePlugin( PLUGIN_SLUG ); } ); test( 'should display an empty state when no connectors are registered', async ( { page, admin, } ) => { await admin.visitAdminPage( SETTINGS_PAGE_PATH, CONNECTORS_PAGE_QUERY ); // Verify the empty state heading is visible. await expect( page.getByRole( 'heading', { name: 'No connectors yet' } ) ).toBeVisible(); // Verify the explanatory description is visible. await expect( page.getByText( 'Connectors appear here when you install plugins that use external services.' ) ).toBeVisible(); // Verify the "Learn more" button links to plugin directory. const learnMoreButton = page.getByRole( 'link', { name: 'Learn more', } ); await expect( learnMoreButton ).toBeVisible(); await expect( learnMoreButton ).toHaveAttribute( 'href', 'plugin-install.php' ); // Verify the AI plugin callout banner is not shown. await expect( page.locator( '.ai-plugin-callout' ) ).toBeHidden(); // Verify none of the default connector cards are shown. for ( const { slug } of CONNECTORS ) { await expect( page.locator( `.connector-item--${ slug }` ) ).toBeHidden(); } } ); } ); test.describe( 'Connectors page capability checks', () => { const PLUGIN_SLUG = 'gutenberg-test-connectors-capability-restriction'; const installRequiredConnector = { slug: 'gutenberg-test-connectors-never-installed', name: 'Test Install Required Connector', action: 'Install', pluginSlug: 'gutenberg-test-connectors-never-installed', }; const activateRequiredConnector = { slug: 'hello', name: 'Test Activate Required Connector', action: 'Activate', pluginSlug: 'hello', }; const clearCapabilityRestriction = async ( requestUtils ) => { await requestUtils.rest( { path: '/wp/v2/settings', method: 'POST', data: { gutenberg_test_cap_restriction: '', }, } ); }; test.beforeAll( async ( { requestUtils } ) => { await requestUtils.activatePlugin( PLUGIN_SLUG ); } ); test.afterEach( async ( { requestUtils } ) => { await clearCapabilityRestriction( requestUtils ); } ); test.afterAll( async ( { requestUtils } ) => { await clearCapabilityRestriction( requestUtils ); await requestUtils.deactivatePlugin( PLUGIN_SLUG ); } ); const capabilities = [ [ 'no_install', 'user cannot install plugins' ], [ 'no_activate', 'user cannot activate plugins' ], [ 'no_install_activate', 'user cannot install or activate plugins', ], [ 'disallow_file_mods', 'DISALLOW_FILE_MODS is active' ], ]; capabilities.forEach( ( [ restriction, label ] ) => { test( `should show unavailable connector actions when ${ label }`, async ( { page, admin, requestUtils, } ) => { await requestUtils.rest( { path: '/wp/v2/settings', method: 'POST', data: { gutenberg_test_cap_restriction: restriction, }, } ); await admin.visitAdminPage( SETTINGS_PAGE_PATH, CONNECTORS_PAGE_QUERY ); for ( const { slug, name, action, pluginSlug } of [ installRequiredConnector, activateRequiredConnector, ] ) { const card = page.locator( `.connector-item--${ slug }` ); await expect( card ).toBeVisible(); await expect( card.getByRole( 'heading', { name, level: 2 } ) ).toBeVisible(); const learnMoreLink = card.getByRole( 'link', { name: 'Learn more', } ); await expect( learnMoreLink ).toBeVisible(); await expect( learnMoreLink ).toHaveAttribute( 'href', `https://wordpress.org/plugins/${ pluginSlug }/` ); await expect( card.getByRole( 'button', { name: action } ) ).toBeHidden(); } await expect( page.getByRole( 'link', { name: 'search the plugin directory', } ) ).toBeHidden(); } ); } ); } ); test.describe( 'Application password setup flow', () => { const PLUGIN_SLUG = 'gutenberg-test-application-password-connector'; const CREDENTIALS_SETTING = 'connectors_content_source_test_remote_wordpress_credentials'; const APPLICATION_PASSWORD = 'abcd efgh ijkl mnop 1234'; test.beforeAll( async ( { requestUtils } ) => { await requestUtils.activatePlugin( PLUGIN_SLUG ); const status = await requestUtils.rest( { path: '/gutenberg-test-connectors/v1/application-password', } ); // The companion Core change can land independently of this UI. // eslint-disable-next-line playwright/no-skipped-test test.skip( ! status.is_registered, 'Requires application_password support in WordPress Core.' ); } ); test.afterEach( async ( { requestUtils } ) => { await requestUtils.rest( { path: '/wp/v2/settings', method: 'POST', data: { [ CREDENTIALS_SETTING ]: { username: '', password: '', }, }, } ); } ); test.afterAll( async ( { requestUtils } ) => { await requestUtils.deactivatePlugin( PLUGIN_SLUG ); } ); test( 'should save, mask, reload, and remove both credentials', async ( { page, admin, requestUtils, } ) => { await admin.visitAdminPage( SETTINGS_PAGE_PATH, CONNECTORS_PAGE_QUERY ); const card = getConnectorCardByName( page, 'Test Remote WordPress' ); await card.getByRole( 'button', { name: 'Set up' } ).click(); const usernameInput = card.getByRole( 'textbox', { name: 'Username', } ); const passwordInput = card.getByLabel( 'Application password' ); const saveButton = card.getByRole( 'button', { name: 'Save' } ); await expect( saveButton ).toBeDisabled(); await usernameInput.fill( 'remote-user' ); await passwordInput.fill( APPLICATION_PASSWORD ); await expect( saveButton ).toBeEnabled(); await expect( card.getByRole( 'link', { name: 'example.com', } ) ).toHaveAttribute( 'href', 'https://example.com/wp-admin/profile.php' ); await saveButton.click(); await expect( card.getByText( 'Connected', { exact: true } ) ).toBeVisible(); const editButton = card.getByRole( 'button', { name: 'Edit' } ); await expect( editButton ).toBeFocused(); const settings = await requestUtils.rest( { path: '/wp/v2/settings', } ); expect( settings[ CREDENTIALS_SETTING ].username ).toBe( 'remote-user' ); expect( settings[ CREDENTIALS_SETTING ].password ).toBe( '\u2022'.repeat( 16 ) ); await admin.visitAdminPage( SETTINGS_PAGE_PATH, CONNECTORS_PAGE_QUERY ); const reloadedCard = getConnectorCardByName( page, 'Test Remote WordPress' ); await reloadedCard.getByRole( 'button', { name: 'Edit' } ).click(); await expect( reloadedCard.getByRole( 'textbox', { name: 'Username' } ) ).toHaveValue( 'remote-user' ); await expect( reloadedCard.getByLabel( 'Application password' ) ).toHaveValue( '•'.repeat( 16 ) ); await reloadedCard .getByRole( 'button', { name: 'Remove and replace' } ) .click(); await expect( reloadedCard.getByText( 'Connected', { exact: true } ) ).toBeHidden(); await expect( reloadedCard.getByRole( 'textbox', { name: 'Username' } ) ).toHaveValue( '' ); const clearedSettings = await requestUtils.rest( { path: '/wp/v2/settings', } ); expect( clearedSettings[ CREDENTIALS_SETTING ] ).toEqual( { username: '', password: '', } ); } ); test( 'should keep the stored password when a masked settings response is saved back', async ( { requestUtils, } ) => { await requestUtils.rest( { path: '/wp/v2/settings', method: 'POST', data: { [ CREDENTIALS_SETTING ]: { username: 'remote-user', password: APPLICATION_PASSWORD, }, }, } ); // Read the settings back; the REST API masks the password. const settings = await requestUtils.rest( { path: '/wp/v2/settings', } ); expect( settings[ CREDENTIALS_SETTING ].password ).toBe( '\u2022'.repeat( 16 ) ); // Save the masked response back, as a read-modify-write client // would, and confirm the stored password is not overwritten. await requestUtils.rest( { path: '/wp/v2/settings', method: 'POST', data: { [ CREDENTIALS_SETTING ]: settings[ CREDENTIALS_SETTING ], }, } ); const storedCredentials = await requestUtils.rest( { path: '/gutenberg-test-connectors/v1/application-password-credentials', } ); expect( storedCredentials ).toEqual( { username: 'remote-user', password: APPLICATION_PASSWORD, } ); } ); test( 'should show an environment-configured connector as connected and read-only', async ( { page, admin, } ) => { await admin.visitAdminPage( SETTINGS_PAGE_PATH, CONNECTORS_PAGE_QUERY ); const card = getConnectorCardByName( page, 'Test Env Configured WordPress' ); await expect( card.getByText( 'Connected', { exact: true } ) ).toBeVisible(); await card.getByRole( 'button', { name: 'Edit' } ).click(); const usernameInput = card.getByRole( 'textbox', { name: 'Username', } ); await expect( usernameInput ).toBeDisabled(); await expect( usernameInput ).toHaveValue( '•'.repeat( 16 ) ); await expect( card.getByLabel( 'Application password' ) ).toBeDisabled(); await expect( card.getByText( 'These credentials are configured using an environment variable.' ) ).toBeVisible(); await expect( card.getByRole( 'button', { name: 'Remove and replace' } ) ).toBeHidden(); } ); } ); test.describe( 'JS extensibility', () => { const PLUGIN_SLUG = 'gutenberg-test-connectors-js-extensibility'; test.beforeAll( async ( { requestUtils } ) => { await requestUtils.activatePlugin( PLUGIN_SLUG ); } ); test.afterAll( async ( { requestUtils } ) => { await requestUtils.deactivatePlugin( PLUGIN_SLUG ); } ); test( 'should not display a card for a server-only connector without a JS render function', async ( { page, admin, } ) => { await admin.visitAdminPage( SETTINGS_PAGE_PATH, CONNECTORS_PAGE_QUERY ); // The server registers test_server_only_service but no JS // registerConnector call provides a render function for it, // so no card should appear in the UI. await expect( page.getByRole( 'heading', { name: 'Test Server Only Service', level: 2, } ) ).toBeHidden(); } ); test( 'should display a custom connector registered via JS with merging strategy', async ( { page, admin, } ) => { await admin.visitAdminPage( SETTINGS_PAGE_PATH, CONNECTORS_PAGE_QUERY ); const card = page.locator( '.connector-item--test_custom_service' ); await expect( card ).toBeVisible(); // Verify the custom content from the render function is visible. await expect( card.getByText( 'Custom rendered content for testing.' ) ).toBeVisible(); // Verify label and description from the server-side PHP registration // are merged with the client-side JS render function. await expect( card.getByRole( 'heading', { name: 'Test Custom Service', level: 2, } ) ).toBeVisible(); await expect( card.getByText( 'A custom service for E2E testing.' ) ).toBeVisible(); } ); test( 'should preserve a custom render for an api_key connector registered before registerDefaultConnectors', async ( { page, admin, } ) => { await admin.visitAdminPage( SETTINGS_PAGE_PATH, CONNECTORS_PAGE_QUERY ); const card = page.locator( '.connector-item--test_api_key_with_custom_render' ); await expect( card ).toBeVisible(); // The JS-registered custom render must be visible inside the card. await expect( card.getByText( 'Custom render survived registerDefaultConnectors().' ) ).toBeVisible(); // The default API key input must not appear inside the card. await expect( card.getByRole( 'textbox', { name: 'API Key' } ) ).toHaveCount( 0 ); } ); } ); } );