|
| 1 | +/** |
| 2 | + * Helpers for the Cloudinary wizard e2e spec. |
| 3 | + * |
| 4 | + * State changes (deleting the connection options) bypass the WP REST |
| 5 | + * API so they don't trigger `pre_update_option_cloudinary_connect`, |
| 6 | + * which would make a live Cloudinary API call. Direct DB access via |
| 7 | + * docker + wp-cli is the right tool here. |
| 8 | + * |
| 9 | + * We use `docker exec` rather than `npx wp-env run cli` because the |
| 10 | + * latter routes through `got` → `api.wordpress.org` at startup and |
| 11 | + * times out on macOS due to an IPv6 resolution issue. `docker exec` |
| 12 | + * goes straight to the running container. |
| 13 | + */ |
| 14 | + |
| 15 | +const { execSync } = require( 'child_process' ); |
| 16 | + |
| 17 | +const CONNECT_OPTION = 'cloudinary_connect'; |
| 18 | +const SIGNATURE_OPTION = 'cloudinary_connection_signature'; |
| 19 | +const STATUS_OPTION = 'cloudinary_status'; |
| 20 | + |
| 21 | +let cachedCliContainer = null; |
| 22 | + |
| 23 | +/** |
| 24 | + * Find the wp-env CLI container name dynamically. |
| 25 | + * |
| 26 | + * Playwright drives the `tests-wordpress` site (port 8889) by default, |
| 27 | + * so we target the matching `*-tests-cli-1` container. The container |
| 28 | + * name embeds a project hash that varies between machines; we discover |
| 29 | + * it by listing running containers and filtering for the suffix. |
| 30 | + * |
| 31 | + * @return {string} Container name. |
| 32 | + * @throws If no matching container is running. |
| 33 | + */ |
| 34 | +function getCliContainer() { |
| 35 | + if ( cachedCliContainer ) { |
| 36 | + return cachedCliContainer; |
| 37 | + } |
| 38 | + |
| 39 | + const out = execSync( "docker ps --format '{{.Names}}'", { |
| 40 | + encoding: 'utf8', |
| 41 | + } ); |
| 42 | + const lines = out.split( '\n' ).filter( Boolean ); |
| 43 | + |
| 44 | + const cli = lines.find( ( name ) => /-tests-cli-1$/.test( name ) ); |
| 45 | + |
| 46 | + if ( ! cli ) { |
| 47 | + throw new Error( |
| 48 | + 'Could not find a running wp-env tests-cli container. Run `docker ps` and confirm a `*-tests-cli-1` container is up.' |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + cachedCliContainer = cli; |
| 53 | + return cli; |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Run a WP-CLI command inside the wp-env cli container. |
| 58 | + * |
| 59 | + * @param {string[]} args wp-cli arguments after the leading `wp`. |
| 60 | + * @return {string} stdout, trimmed. |
| 61 | + */ |
| 62 | +function wpCli( args ) { |
| 63 | + const container = getCliContainer(); |
| 64 | + const cmd = [ |
| 65 | + 'docker', |
| 66 | + 'exec', |
| 67 | + container, |
| 68 | + 'wp', |
| 69 | + ...args, |
| 70 | + '--allow-root', |
| 71 | + ].join( ' ' ); |
| 72 | + |
| 73 | + return execSync( cmd, { |
| 74 | + encoding: 'utf8', |
| 75 | + stdio: [ 'ignore', 'pipe', 'pipe' ], |
| 76 | + } ).trim(); |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Wipe any existing Cloudinary connection so the wizard reappears. |
| 81 | + * |
| 82 | + * Each option may or may not exist. We attempt all three and |
| 83 | + * silently swallow "Could not get/delete option" errors. |
| 84 | + */ |
| 85 | +function resetCloudinaryConnection() { |
| 86 | + for ( const opt of [ CONNECT_OPTION, SIGNATURE_OPTION, STATUS_OPTION ] ) { |
| 87 | + try { |
| 88 | + wpCli( [ 'option', 'delete', opt ] ); |
| 89 | + } catch ( e ) { |
| 90 | + // Option not present; that's fine. |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Read the Cloudinary connection string from the environment. |
| 97 | + * |
| 98 | + * We use a dedicated `CLOUDINARY_E2E_URL` env var rather than the |
| 99 | + * Cloudinary SDK's conventional `CLOUDINARY_URL` to make it explicit |
| 100 | + * that this is test-only credentials and to avoid colliding with any |
| 101 | + * SDK auto-bootstrap behaviour developers may rely on locally. |
| 102 | + * |
| 103 | + * Throws if not set so the test fails loudly rather than silently |
| 104 | + * producing a meaningless pass/fail. |
| 105 | + * |
| 106 | + * @return {string} The cloudinary:// URL. |
| 107 | + */ |
| 108 | +function getCloudinaryUrlFromEnv() { |
| 109 | + const url = process.env.CLOUDINARY_E2E_URL; |
| 110 | + if ( ! url || ! url.startsWith( 'cloudinary://' ) ) { |
| 111 | + throw new Error( |
| 112 | + 'CLOUDINARY_E2E_URL env var must be set to a valid cloudinary:// connection string before running the wizard e2e spec.' |
| 113 | + ); |
| 114 | + } |
| 115 | + return url; |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Navigate the admin browser to the wizard screen. |
| 120 | + * |
| 121 | + * We hit the wizard URL directly so the test does not depend on the |
| 122 | + * "not connected → wizard" redirect. |
| 123 | + * |
| 124 | + * @param {Object} admin Admin fixture from @wordpress/e2e-test-utils-playwright. |
| 125 | + */ |
| 126 | +async function visitWizard( admin ) { |
| 127 | + await admin.visitAdminPage( 'admin.php', 'page=cloudinary§ion=wizard' ); |
| 128 | +} |
| 129 | + |
| 130 | +module.exports = { |
| 131 | + getCliContainer, |
| 132 | + getCloudinaryUrlFromEnv, |
| 133 | + resetCloudinaryConnection, |
| 134 | + visitWizard, |
| 135 | + wpCli, |
| 136 | +}; |
0 commit comments