Skip to content

Commit

Permalink
Improve slug generation & matching in request utils (#52414)
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy authored Jul 20, 2023
1 parent 8aa016e commit 4bfc190
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
48 changes: 37 additions & 11 deletions packages/e2e-test-utils-playwright/src/request-utils/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,55 @@ async function getPluginsMap( this: RequestUtils, forceRefetch = false ) {
for ( const plugin of plugins ) {
// Ideally, we should be using sanitize_title() in PHP rather than kebabCase(),
// but we don't have the exact port of it in JS.
this.pluginsMap[ kebabCase( plugin.name ) ] = plugin.plugin;
// This is a good approximation though.
const slug = kebabCase( plugin.name.toLowerCase() );
this.pluginsMap[ slug ] = plugin.plugin;
}
return this.pluginsMap;
}

/**
* Activates an installed plugin.
* Finds a plugin in the plugin map.
*
* @param this RequestUtils.
* @param slug Plugin slug.
* Attempts to provide a helpful error message if not found.
*
* @param slug Plugin slug.
* @param pluginsMap Plugins map.
*/
async function activatePlugin( this: RequestUtils, slug: string ) {
const pluginsMap = await this.getPluginsMap();
function getPluginFromMap(
slug: string,
pluginsMap: Record< string, string >
) {
const plugin = pluginsMap[ slug ];

if ( ! plugin ) {
for ( const key of Object.keys( pluginsMap ) ) {
if (
key.toLowerCase().replace( /-/g, '' ) ===
slug.toLowerCase().replace( /-/g, '' )
) {
throw new Error(
`The plugin "${ slug }" isn't installed. Did you perhaps mean "${ key }"?`
);
}
}

throw new Error( `The plugin "${ slug }" isn't installed` );
}

return plugin;
}

/**
* Activates an installed plugin.
*
* @param this RequestUtils.
* @param slug Plugin slug.
*/
async function activatePlugin( this: RequestUtils, slug: string ) {
const pluginsMap = await this.getPluginsMap();
const plugin = getPluginFromMap( slug, pluginsMap );

await this.rest( {
method: 'PUT',
path: `/wp/v2/plugins/${ plugin }`,
Expand All @@ -61,11 +91,7 @@ async function activatePlugin( this: RequestUtils, slug: string ) {
*/
async function deactivatePlugin( this: RequestUtils, slug: string ) {
const pluginsMap = await this.getPluginsMap();
const plugin = pluginsMap[ slug ];

if ( ! plugin ) {
throw new Error( `The plugin "${ slug }" isn't installed` );
}
const plugin = getPluginFromMap( slug, pluginsMap );

await this.rest( {
method: 'PUT',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );
test.describe( 'Allowed Blocks Setting on InnerBlocks', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.activatePlugin(
'gutenberg-test-inner-blocks-allowed-blocks'
'gutenberg-test-innerblocks-allowed-blocks'
);
} );

test.afterAll( async ( { requestUtils } ) => {
await requestUtils.deactivatePlugin(
'gutenberg-test-inner-blocks-allowed-blocks'
'gutenberg-test-innerblocks-allowed-blocks'
);
} );

Expand Down
4 changes: 2 additions & 2 deletions test/e2e/specs/editor/various/inner-blocks-templates.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );
test.describe( 'Inner blocks templates', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.activatePlugin(
'gutenberg-test-inner-blocks-templates'
'gutenberg-test-innerblocks-templates'
);
} );

Expand All @@ -16,7 +16,7 @@ test.describe( 'Inner blocks templates', () => {

test.afterAll( async ( { requestUtils } ) => {
await requestUtils.deactivatePlugin(
'gutenberg-test-inner-blocks-templates'
'gutenberg-test-innerblocks-templates'
);
} );

Expand Down

0 comments on commit 4bfc190

Please sign in to comment.