Skip to content

Commit

Permalink
Fix URL in case it does not have the http protocol (#94208)
Browse files Browse the repository at this point in the history
  • Loading branch information
renatho authored Sep 16, 2024
1 parent a7abefb commit 78eef30
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { getPluginInstallationPage, getMigrateGuruPageURL } from '../utils';

describe( 'utils', () => {
describe( 'getPluginInstallationPage', () => {
it( 'should return WordPress.org plugin page if fromUrl is empty', () => {
expect( getPluginInstallationPage( '' ) ).toBe(
'https://wordpress.org/plugins/migrate-guru/'
);
} );

it( 'should return correct plugin installation page for given URL', () => {
expect( getPluginInstallationPage( 'example.com' ) ).toBe(
'https://example.com/wp-admin/plugin-install.php?s=%2522migrate%2520guru%2522&tab=search&type=term'
);
} );

it( 'should handle URLs with existing protocol', () => {
expect( getPluginInstallationPage( 'http://example.com' ) ).toBe(
'http://example.com/wp-admin/plugin-install.php?s=%2522migrate%2520guru%2522&tab=search&type=term'
);
} );

it( 'should remove duplicate slashes from the URL', () => {
expect( getPluginInstallationPage( 'https://example.com///' ) ).toBe(
'https://example.com/wp-admin/plugin-install.php?s=%2522migrate%2520guru%2522&tab=search&type=term'
);
} );
} );

describe( 'getMigrateGuruPageURL', () => {
it( 'should return correct Migrate Guru page URL', () => {
expect( getMigrateGuruPageURL( 'example.com' ) ).toBe(
'https://example.com/wp-admin/admin.php?page=migrateguru'
);
} );

it( 'should handle URLs with existing protocol', () => {
expect( getMigrateGuruPageURL( 'http://example.com' ) ).toBe(
'http://example.com/wp-admin/admin.php?page=migrateguru'
);
} );

it( 'should remove duplicate slashes from the URL', () => {
expect( getMigrateGuruPageURL( 'https://example.com///' ) ).toBe(
'https://example.com/wp-admin/admin.php?page=migrateguru'
);
} );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@ const isWhiteLabeledPluginEnabled = config.isEnabled(
'migration-flow/enable-white-labeled-plugin'
);

const ensureProtocol = ( url: string ) => {
if ( ! url.startsWith( 'http://' ) && ! url.startsWith( 'https://' ) ) {
return `https://${ url }`;
}
return url;
};

export const getPluginInstallationPage = ( fromUrl: string ) => {
if ( fromUrl !== '' ) {
const baseUrl = ensureProtocol( fromUrl );

if ( isWhiteLabeledPluginEnabled ) {
return removeDuplicatedSlashes(
`${ fromUrl }/wp-admin/plugin-install.php?s=%2522wpcom%2520migration%2522&tab=search&type=term`
`${ baseUrl }/wp-admin/plugin-install.php?s=%2522wpcom%2520migration%2522&tab=search&type=term`
);
}

return removeDuplicatedSlashes(
`${ fromUrl }/wp-admin/plugin-install.php?s=%2522migrate%2520guru%2522&tab=search&type=term`
`${ baseUrl }/wp-admin/plugin-install.php?s=%2522migrate%2520guru%2522&tab=search&type=term`
);
}

Expand All @@ -24,9 +33,11 @@ export const getPluginInstallationPage = ( fromUrl: string ) => {
};

export const getMigrateGuruPageURL = ( siteURL: string ) => {
const baseUrl = ensureProtocol( siteURL );

if ( isWhiteLabeledPluginEnabled ) {
return removeDuplicatedSlashes( `${ siteURL }/wp-admin/admin.php?page=wpcom-migration` );
return removeDuplicatedSlashes( `${ baseUrl }/wp-admin/admin.php?page=wpcom-migration` );
}

return removeDuplicatedSlashes( `${ siteURL }/wp-admin/admin.php?page=migrateguru` );
return removeDuplicatedSlashes( `${ baseUrl }/wp-admin/admin.php?page=migrateguru` );
};

0 comments on commit 78eef30

Please sign in to comment.