Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve slug generation & matching in request utils #52414

Merged
merged 6 commits into from
Jul 20, 2023
Merged
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
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
Loading