Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions tools/common/lib/remove-default-db-constants.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
const DB_SETTINGS_BLOCK =
normalizeLineEndings( `// ** Database settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/**
* Default values for database constants in wp-config-sample.php.
* Only these exact default values will be matched and removed.
*/
const DB_DEFAULTS: Array< [ string, string ] > = [
[ 'DB_NAME', 'database_name_here' ],
[ 'DB_USER', 'username_here' ],
[ 'DB_PASSWORD', 'password_here' ],
[ 'DB_HOST', 'localhost' ],
[ 'DB_CHARSET', 'utf8mb4' ],
[ 'DB_COLLATE', '' ],
];

/** Database username */
define( 'DB_USER', 'username_here' );
/**
* Builds a regex that matches the default DB settings block in wp-config.php.
*
* The regex is flexible with comments (they may be in any language) but strict
* with PHP code (only matches exact default values from wp-config-sample.php).
*
* Structure matched per constant:
* [optional comment lines] <- flexible (any language)
* define( 'DB_X', 'default_value' ); <- strict (exact match)
* [optional blank lines]
*/
function buildDefaultDbBlockRegex(): RegExp {
// Matches a single-line comment: // ... or a docblock comment: /** ... */
const commentLine =
'[ \\t]*(?:\\/\\/[^\\r\\n]*|\\/\\*\\*[^*]*(?:\\*(?!\\/)[^*]*)*\\*\\/)[ \\t]*\\r?\\n';
// Zero or more comment lines before a define
const optionalComments = `(?:${ commentLine })*`;
// Optional blank lines between entries
const blankLines = '(?:[ \\t]*\\r?\\n)*';

/** Database password */
define( 'DB_PASSWORD', 'password_here' );
const definePatterns = DB_DEFAULTS.map( ( [ name, value ] ) => {
const escapedValue = value.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' );
return `${ optionalComments }[ \\t]*define\\([ \\t]*'${ name }'[ \\t]*,[ \\t]*'${ escapedValue }'[ \\t]*\\)[ \\t]*;[ \\t]*\\r?\\n`;
} );

/** Database hostname */
define( 'DB_HOST', 'localhost' );

/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8mb4' );
return new RegExp( definePatterns.join( blankLines ) );
}

/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
` );
const DB_SETTINGS_BLOCK_REGEX = buildDefaultDbBlockRegex();

const REPLACEMENT_COMMENT = normalizeLineEndings( `/**
* Database connection information is automatically provided.
Expand All @@ -36,9 +58,9 @@ export function normalizeLineEndings( content: string ): string {
}

export function hasDefaultDbBlock( content: string ): boolean {
return content.includes( DB_SETTINGS_BLOCK );
return DB_SETTINGS_BLOCK_REGEX.test( content );
}

export function removeDbConstants( content: string ): string {
return content.replace( DB_SETTINGS_BLOCK, REPLACEMENT_COMMENT + '\n' );
return content.replace( DB_SETTINGS_BLOCK_REGEX, REPLACEMENT_COMMENT + '\n' );
}
124 changes: 123 additions & 1 deletion tools/common/lib/tests/remove-default-db-constants.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,75 @@ define( 'WP_DEBUG', false );
/* That's all, stop editing! Happy publishing. */
` );

const WP_CONFIG_WITH_POLISH_COMMENTS = normalizeLineEndings( `<?php
/**
* Podstawowa konfiguracja WordPressa
*/

// ** Ustawienia bazy danych - Możesz uzyskać te informacje od swojego hostingodawcy ** //
/** Nazwa bazy danych WordPressa */
define( 'DB_NAME', 'database_name_here' );

/** Nazwa użytkownika bazy danych */
define( 'DB_USER', 'username_here' );

/** Hasło do bazy danych */
define( 'DB_PASSWORD', 'password_here' );

/** Nazwa serwera bazy danych */
define( 'DB_HOST', 'localhost' );

/** Kodowanie znaków bazy danych */
define( 'DB_CHARSET', 'utf8mb4' );

/** Typ porównywania bazy danych. Nie zmieniaj tego, jeśli nie masz pewności. */
define( 'DB_COLLATE', '' );

$table_prefix = 'wp_';

define( 'WP_DEBUG', false );
` );

const WP_CONFIG_WITH_FRENCH_COMMENTS = normalizeLineEndings( `<?php
/**
* La configuration de base de WordPress
*/

// ** Réglages de la base de données - Téléchargez les infos chez votre hébergeur ** //
/** Le nom de la base de données de WordPress */
define( 'DB_NAME', 'database_name_here' );

/** Utilisateur de la base de données */
define( 'DB_USER', 'username_here' );

/** Mot de passe de la base de données */
define( 'DB_PASSWORD', 'password_here' );

/** Adresse de l'hébergement */
define( 'DB_HOST', 'localhost' );

/** Jeu de caractères à utiliser par la base de données lors de la création des tables. */
define( 'DB_CHARSET', 'utf8mb4' );

/** Type d'interclassement de la base de données. */
define( 'DB_COLLATE', '' );

$table_prefix = 'wp_';

define( 'WP_DEBUG', false );
` );

const WP_CONFIG_WITH_NO_COMMENTS = normalizeLineEndings( `<?php
define( 'DB_NAME', 'database_name_here' );
define( 'DB_USER', 'username_here' );
define( 'DB_PASSWORD', 'password_here' );
define( 'DB_HOST', 'localhost' );
define( 'DB_CHARSET', 'utf8mb4' );
define( 'DB_COLLATE', '' );

$table_prefix = 'wp_';
` );

const WP_CONFIG_WITH_CUSTOM_VALUES = normalizeLineEndings( `<?php
/**
* The base configuration for WordPress
Expand Down Expand Up @@ -112,6 +181,18 @@ describe( 'hasDefaultDbBlock', () => {
expect( hasDefaultDbBlock( WP_CONFIG_WITH_DEFAULTS ) ).toBe( true );
} );

it( 'should return true when comments are in Polish', () => {
expect( hasDefaultDbBlock( WP_CONFIG_WITH_POLISH_COMMENTS ) ).toBe( true );
} );

it( 'should return true when comments are in French', () => {
expect( hasDefaultDbBlock( WP_CONFIG_WITH_FRENCH_COMMENTS ) ).toBe( true );
} );

it( 'should return true when there are no comments at all', () => {
expect( hasDefaultDbBlock( WP_CONFIG_WITH_NO_COMMENTS ) ).toBe( true );
} );

it( 'should return false when DB values are custom', () => {
expect( hasDefaultDbBlock( WP_CONFIG_WITH_CUSTOM_VALUES ) ).toBe( false );
} );
Expand All @@ -133,19 +214,52 @@ describe( 'removeDbConstants', () => {
it( 'should remove the entire DB settings block', () => {
const result = removeDbConstants( WP_CONFIG_WITH_DEFAULTS );

expect( result ).not.toContain( '// ** Database settings' );
expect( result ).not.toContain( "define( 'DB_NAME'" );
expect( result ).not.toContain( "define( 'DB_USER'" );
expect( result ).not.toContain( "define( 'DB_PASSWORD'" );
expect( result ).not.toContain( "define( 'DB_HOST'" );
expect( result ).not.toContain( "define( 'DB_CHARSET'" );
expect( result ).not.toContain( "define( 'DB_COLLATE'" );
} );

it( 'should remove comments associated with the DB block', () => {
const result = removeDbConstants( WP_CONFIG_WITH_DEFAULTS );

expect( result ).not.toContain( '// ** Database settings' );
expect( result ).not.toContain( 'The name of the database for WordPress' );
expect( result ).not.toContain( 'Database username' );
expect( result ).not.toContain( 'Database password' );
expect( result ).not.toContain( 'Database hostname' );
} );

it( 'should remove translated comments (Polish)', () => {
const result = removeDbConstants( WP_CONFIG_WITH_POLISH_COMMENTS );

expect( result ).not.toContain( "define( 'DB_NAME'" );
expect( result ).not.toContain( 'Nazwa bazy danych' );
expect( result ).not.toContain( 'Ustawienia bazy danych' );
expect( result ).toContain( 'Database connection information is automatically provided' );
expect( result ).toContain( "$table_prefix = 'wp_'" );
} );

it( 'should remove translated comments (French)', () => {
const result = removeDbConstants( WP_CONFIG_WITH_FRENCH_COMMENTS );

expect( result ).not.toContain( "define( 'DB_NAME'" );
expect( result ).not.toContain( 'Le nom de la base' );
expect( result ).toContain( 'Database connection information is automatically provided' );
expect( result ).toContain( "$table_prefix = 'wp_'" );
} );

it( 'should remove defines even without comments', () => {
const result = removeDbConstants( WP_CONFIG_WITH_NO_COMMENTS );

expect( result ).not.toContain( "define( 'DB_NAME'" );
expect( result ).not.toContain( "define( 'DB_USER'" );
expect( result ).toContain( 'Database connection information is automatically provided' );
expect( result ).toContain( "$table_prefix = 'wp_'" );
} );

it( 'should add the replacement comment block', () => {
const result = removeDbConstants( WP_CONFIG_WITH_DEFAULTS );

Expand Down Expand Up @@ -179,6 +293,14 @@ describe( 'integration: full migration flow', () => {
expect( result ).toContain( 'Database connection information is automatically provided' );
} );

it( 'should correctly process wp-config.php with translated comments', () => {
expect( hasDefaultDbBlock( WP_CONFIG_WITH_POLISH_COMMENTS ) ).toBe( true );

const result = removeDbConstants( WP_CONFIG_WITH_POLISH_COMMENTS );
expect( result ).toContain( 'Database connection information is automatically provided' );
expect( hasDefaultDbBlock( result ) ).toBe( false );
} );

it( 'should not modify wp-config.php with custom values', () => {
expect( hasDefaultDbBlock( WP_CONFIG_WITH_CUSTOM_VALUES ) ).toBe( false );
} );
Expand Down
Loading