diff --git a/tests/e2e/assets/e2e-test-theme.md b/tests/e2e/assets/e2e-test-theme.md new file mode 100644 index 0000000000000..3c8dcc235b57d --- /dev/null +++ b/tests/e2e/assets/e2e-test-theme.md @@ -0,0 +1,23 @@ +# Theme fixture + +`e2e-test-theme.zip` contains a minimal classic theme with three files. Its template executes no code. The screenshot reuses the existing PHPUnit image fixture. The update URI prevents WordPress.org update matching. + +To reproduce the archive from the repository root with Python 3: + +```python +from pathlib import Path +from zipfile import ZipFile, ZipInfo + +files = { + 'style.css': '/*\nTheme Name: E2E Test Theme\nVersion: 1.0\nUpdate URI: false\n*/\n', + 'index.php': ' { + let previousTheme; + let ownsTheme; + + test.beforeEach( async ( { admin, page, requestUtils } ) => { + // The fixture slug requires an environment not shared with another test run. + ownsTheme = false; + const themes = await requestUtils.rest( { path: '/wp/v2/themes' } ); + expect( themes.map( ( theme ) => theme.stylesheet ) ).not.toContain( + themeSlug + ); + previousTheme = themes.find( + ( theme ) => theme.status === 'active' + ).stylesheet; + ownsTheme = true; + await admin.visitAdminPage( 'theme-install.php' ); + await page + .getByRole( 'button', { name: 'Upload Theme', exact: true } ) + .click(); + await page + .locator( '#themezip' ) + .setInputFiles( 'tests/e2e/assets/e2e-test-theme.zip' ); + await page + .getByRole( 'button', { name: 'Install Now', exact: true } ) + .click(); + await expect( + page.getByText( 'Theme installed successfully.', { exact: true } ) + ).toBeVisible(); + } ); + + test.afterEach( async ( { requestUtils } ) => { + if ( ! ownsTheme ) { + return; + } + await requestUtils.activateTheme( previousTheme ); + const themes = await requestUtils.rest( { path: '/wp/v2/themes' } ); + if ( themes.some( ( theme ) => theme.stylesheet === themeSlug ) ) { + const themesResponse = await requestUtils.request.get( + 'wp-admin/themes.php' + ); + const html = await themesResponse.text(); + await themesResponse.dispose(); + const deleteQuery = html.match( + `action=delete&stylesheet=${ themeSlug }&_wpnonce=[a-z0-9]+` + ); + expect( deleteQuery ).not.toBeNull(); + const response = await requestUtils.request.get( + `wp-admin/themes.php?${ deleteQuery[ 0 ].replace( /&/g, '&' ) }` + ); + expect( response.ok() ).toBe( true ); + await response.dispose(); + } + const remainingThemes = await requestUtils.rest( { + path: '/wp/v2/themes', + } ); + expect( + remainingThemes.map( ( theme ) => theme.stylesheet ) + ).not.toContain( themeSlug ); + expect( + remainingThemes.find( ( theme ) => theme.status === 'active' ) + .stylesheet + ).toBe( previousTheme ); + } ); + + test( 'activates an installed theme from its card', async ( { + admin, + page, + requestUtils, + } ) => { + await admin.visitAdminPage( 'themes.php' ); + const card = page.locator( `.theme[data-slug="${ themeSlug }"]` ); + await card.hover(); + await card + .getByRole( 'link', { + name: `Activate ${ themeName }`, + exact: true, + } ) + .click(); + await expect( + card.getByRole( 'heading', { + name: `Active: ${ themeName }`, + exact: true, + } ) + ).toBeVisible(); + await page.reload(); + await expect( + card.getByRole( 'heading', { + name: `Active: ${ themeName }`, + exact: true, + } ) + ).toBeVisible(); + const themes = await requestUtils.rest( { path: '/wp/v2/themes' } ); + expect( + themes.find( ( theme ) => theme.status === 'active' ).stylesheet + ).toBe( themeSlug ); + } ); + + test( 'deletes an inactive theme from its details', async ( { + admin, + page, + requestUtils, + } ) => { + await admin.visitAdminPage( 'themes.php' ); + const card = page.locator( `.theme[data-slug="${ themeSlug }"]` ); + await card.click(); + await Promise.all( [ + page.waitForEvent( 'dialog' ).then( async ( dialog ) => { + expect( dialog.type() ).toBe( 'confirm' ); + await dialog.accept(); + } ), + page + .getByRole( 'link', { + name: `Delete ${ themeName }`, + exact: true, + } ) + .click(), + ] ); + await expect( card ).toHaveCount( 0 ); + await page.reload(); + await expect( card ).toHaveCount( 0 ); + const themes = await requestUtils.rest( { path: '/wp/v2/themes' } ); + expect( themes.map( ( theme ) => theme.stylesheet ) ).not.toContain( + themeSlug + ); + expect( + themes.find( ( theme ) => theme.status === 'active' ).stylesheet + ).toBe( previousTheme ); + } ); +} );