From 5cc91910b8349668013798a9097ae8b3758f7442 Mon Sep 17 00:00:00 2001 From: Ramon Date: Thu, 14 Mar 2024 13:49:17 +1100 Subject: [PATCH] E2E theme switch: match incoming theme slug, then optional folder (#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 Co-authored-by: andrewserong --- .../src/request-utils/themes.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/e2e-test-utils-playwright/src/request-utils/themes.ts b/packages/e2e-test-utils-playwright/src/request-utils/themes.ts index 6b4fd175887371..1f56814346d6ed 100644 --- a/packages/e2e-test-utils-playwright/src/request-utils/themes.ts +++ b/packages/e2e-test-utils-playwright/src/request-utils/themes.ts @@ -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&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&stylesheet=${ encodeURIComponent( themeSlug ) }&_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&stylesheet=${ optionalFolder }${ encodeURIComponent( + themeSlug + ) }&_wpnonce=[a-z0-9]+` + ); + } + if ( ! matchGroup ) { if ( html.includes( `data-slug="${ themeSlug }"` ) ) { // The theme is already activated.