Skip to content

Commit

Permalink
Merge pull request #1535 from Automattic/forno-1677/fix/dev-env-multi…
Browse files Browse the repository at this point in the history
…site-slug

FORNO-1677: Fix dev env network site domains
  • Loading branch information
hanifn authored Oct 30, 2023
2 parents 02fb7fe + be09ed2 commit 66c4607
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
24 changes: 24 additions & 0 deletions __tests__/commands/dev-env-sync-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ jest.spyOn( console, 'log' ).mockImplementation( () => {} );
describe( 'commands/DevEnvSyncSQLCommand', () => {
const app = { id: 123, name: 'test-app' };
const env = { id: 456, name: 'test-env', wpSitesSDS: {} };
const msEnv = {
id: 456,
name: 'test-env',
wpSitesSDS: {
nodes: [
{
blogId: 2,
homeUrl: 'https://subsite.com',
},
],
},
};

describe( '.generateExport', () => {
it( 'should create an instance of ExportSQLCommand and run', async () => {
Expand All @@ -93,6 +105,18 @@ describe( 'commands/DevEnvSyncSQLCommand', () => {

expect( cmd.searchReplaceMap ).toEqual( { 'test.go-vip.com': 'test-slug.vipdev.lndo.site' } );
} );

it( 'should return a map of search-replace values for multisite', () => {
const cmd = new DevEnvSyncSQLCommand( app, msEnv, 'test-slug' );
cmd.slug = 'test-slug';
cmd.siteUrls = [ 'test.go-vip.com', 'subsite.com' ];
cmd.generateSearchReplaceMap();

expect( cmd.searchReplaceMap ).toEqual( {
'test.go-vip.com': 'test-slug.vipdev.lndo.site',
'subsite.com': 'subsite-com-2.test-slug.vipdev.lndo.site',
} );
} );
} );

describe( '.runSearchReplace', () => {
Expand Down
15 changes: 14 additions & 1 deletion src/commands/dev-env-sync-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,23 @@ export class DevEnvSyncSQLCommand {
const url = site.homeUrl.replace( /https?:\/\//, '' );
if ( ! this.searchReplaceMap[ url ] ) continue;

this.searchReplaceMap[ url ] = `${ site.blogId }.${ this.landoDomain }`;
this.searchReplaceMap[ url ] = `${ this.slugifyDomain( url ) }-${ site.blogId }.${
this.landoDomain
}`;
}
}

slugifyDomain( domain ) {
return String( domain )
.normalize( 'NFKD' ) // split accented characters into their base characters and diacritical marks
.replace( /[\u0300-\u036f]/g, '' ) // remove all the accents, which happen to be all in the \u03xx UNICODE block.
.trim() // trim leading or trailing whitespace
.toLowerCase() // convert to lowercase
.replace( /[^a-z0-9 .-]/g, '' ) // remove non-alphanumeric characters except for spaces, dots, and hyphens
.replace( /[.\s]+/g, '-' ) // replace dots and spaces with hyphens
.replace( /-+/g, '-' ); // remove consecutive hyphens
}

/**
* Runs the SQL import command to import the SQL file
*
Expand Down

0 comments on commit 66c4607

Please sign in to comment.