diff --git a/tests/e2e/specs/install.test.js b/tests/e2e/specs/install.test.js index cc237b81452a2..9e996a28e212e 100644 --- a/tests/e2e/specs/install.test.js +++ b/tests/e2e/specs/install.test.js @@ -2,6 +2,7 @@ * External dependencies */ import { writeFileSync, readFileSync } from 'node:fs'; +import { execFileSync } from 'node:child_process'; import { join } from 'node:path'; /** @@ -11,6 +12,11 @@ import { test, expect } from '@wordpress/e2e-test-utils-playwright'; let wpConfigOriginal; +// The prefix used to trick WP into "not installed" mode. Kept as a single +// constant since it has to stay in sync between the config rewrite and the +// cleanup query below. +const TEST_TABLE_PREFIX = 'wp_e2e_'; + test.describe( 'WordPress installation process', () => { const wpConfig = join( process.cwd(), @@ -23,21 +29,48 @@ test.describe( 'WordPress installation process', () => { // Changing the table prefix tricks WP into new install mode. writeFileSync( wpConfig, - wpConfigOriginal.replace( `$table_prefix = 'wp_';`, `$table_prefix = 'wp_e2e_';` ) + wpConfigOriginal.replace( `$table_prefix = 'wp_';`, `$table_prefix = '${ TEST_TABLE_PREFIX }';` ) ); } ); test.afterEach( async () => { writeFileSync( wpConfig, wpConfigOriginal ); + + // The test completes a full install under the `TEST_TABLE_PREFIX`. Drop + // those tables, otherwise the next run finds a pre-existing install and + // never reaches the installation screen it's meant to be testing. + const tables = [ + 'commentmeta', 'comments', 'links', 'options', 'postmeta', 'posts', + 'term_relationships', 'term_taxonomy', 'termmeta', 'terms', 'usermeta', 'users', + ]; + const dropTablesPhp = tables + .map( ( table ) => `global $wpdb; $wpdb->query( "DROP TABLE IF EXISTS ${ TEST_TABLE_PREFIX }${ table }" );` ) + .join( ' ' ); + + execFileSync( + process.execPath, + [ + join( process.cwd(), 'tools/local-env/scripts/docker.js' ), + 'exec', + '--user', + 'wp_php', + 'cli', + 'wp', + 'eval', + dropTablesPhp, + ], + { stdio: 'inherit' } + ); } ); test( 'should install WordPress with pre-existing database credentials', async ( { page } ) => { - await page.goto( '/' ); - - await expect( - page, - 'should redirect to the installation page' - ).toHaveURL( /wp-admin\/install\.php$/ ); + // The config file was just rewritten on the host; retry the navigation + // (not just the URL check) since the container's view of the file can + // lag behind the write by a request or two. + await expect( async () => { + await page.goto( '/' ); + expect( page.url() ).toMatch( /wp-admin\/install\.php$/ ); + }, 'should redirect to the installation page' ).toPass( { timeout: 10_000 } ); await expect( page.getByText( /WordPress database error/ ),