Skip to content

Commit

Permalink
E2E theme switch: match incoming theme slug, then optional folder (Wo…
Browse files Browse the repository at this point in the history
…rdPress#59851)

* Test for incoming theme slug first.
The `optionalFolder` regex part matches paths with a folder,
so it will return the first match, which might contain a folder.
First try to honor the including theme slug, that is, without a folder, the look for the optional folder path, if any.

Co-authored-by: ramonjd <[email protected]>
Co-authored-by: andrewserong <[email protected]>
  • Loading branch information
3 people authored and carstingaxion committed Mar 27, 2024
1 parent d59c351 commit 5cc9191
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions packages/e2e-test-utils-playwright/src/request-utils/themes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,25 @@ async function activateTheme(
let response = await this.request.get( THEMES_URL );
const html = await response.text();
const optionalFolder = '([a-z0-9-]+%2F)?';
const matchGroup = html.match(
`action=activate&amp;stylesheet=${ optionalFolder }${ encodeURIComponent(

// The `optionalFolder` regex part matches paths with a folder,
// so it will return the first match, which might contain a folder.
// First try to honor the included theme slug, that is, without a folder.
let matchGroup = html.match(
`action=activate&amp;stylesheet=${ encodeURIComponent(
themeSlug
) }&amp;_wpnonce=[a-z0-9]+`
);

// If the theme is not found, try to match the theme slug with a folder.
if ( ! matchGroup ) {
matchGroup = html.match(
`action=activate&amp;stylesheet=${ optionalFolder }${ encodeURIComponent(
themeSlug
) }&amp;_wpnonce=[a-z0-9]+`
);
}

if ( ! matchGroup ) {
if ( html.includes( `data-slug="${ themeSlug }"` ) ) {
// The theme is already activated.
Expand Down

0 comments on commit 5cc9191

Please sign in to comment.