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

Tests: Use REST API to delete templates and template parts #38524

Merged
merged 6 commits into from
Feb 11, 2022
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
8 changes: 8 additions & 0 deletions packages/e2e-test-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ _Parameters_

Delete all menus using the REST API

### deleteAllTemplates

Delete all the templates of given type.

_Parameters_

- _type_ `('wp_template'|'wp_template_part')`: - Template type to delete.

### deleteAllWidgets

Delete all the widgets in the widgets screen.
Expand Down
1 change: 1 addition & 0 deletions packages/e2e-test-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export { showBlockToolbar } from './show-block-toolbar';
export { openPreviewPage } from './preview';
export { wpDataSelect } from './wp-data-select';
export { deleteAllWidgets } from './widgets';
export { deleteAllTemplates } from './templates';
export {
rest as __experimentalRest,
batch as __experimentalBatch,
Expand Down
33 changes: 33 additions & 0 deletions packages/e2e-test-utils/src/templates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Internal dependencies
*/
import { rest, batch } from './rest-api';

const PATH_MAPPING = {
wp_template: '/wp/v2/templates',
wp_template_part: '/wp/v2/template-parts',
};

/**
* Delete all the templates of given type.
*
* @param {('wp_template'|'wp_template_part')} type - Template type to delete.
*/
export async function deleteAllTemplates( type ) {
const path = PATH_MAPPING[ type ];

if ( ! path ) {
throw new Error( `Unsupported template type: ${ type }.` );
}

const templates = await rest( { path } );

await batch(
templates
.filter( ( template ) => !! template.wp_id )
.map( ( template ) => ( {
method: 'DELETE',
path: `/wp/v2/posts/${ template.wp_id }?force=true`,
Comment on lines +29 to +30
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be faster than using the templates endpoint.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not working properly in my tests

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I'll have a look tomorrow or Monday.

Are you testing this util separately, or e2e tests are failing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was debugging the template-part test in my PR and noticed that the template parts were not removed between runs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One limitation of this util (and in some parts of templates endpoint) is that it can only delete templates for the current theme.

But I will see if there are any other issues. If util isn't reliable, we can always revert to my original PR - #38486.

} ) )
);
}
24 changes: 0 additions & 24 deletions packages/e2e-tests/mu-plugins/enable-templates-ui.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
createNewPost,
insertBlock,
saveDraft,
trashAllPosts,
openPreviewPage,
openDocumentSettingsSidebar,
activatePlugin,
deactivatePlugin,
deleteAllTemplates,
} from '@wordpress/e2e-test-utils';

const openSidebarPanelWithTitle = async ( title ) => {
Expand Down Expand Up @@ -92,8 +92,8 @@ const createNewTemplate = async ( templateName ) => {
describe( 'Post Editor Template mode', () => {
beforeAll( async () => {
await activatePlugin( 'gutenberg-test-block-templates' );
await trashAllPosts( 'wp_template' );
await trashAllPosts( 'wp_template_part' );
await deleteAllTemplates( 'wp_template' );
await deleteAllTemplates( 'wp_template_part' );
} );

afterAll( async () => {
Expand Down
11 changes: 5 additions & 6 deletions packages/e2e-tests/specs/performance/site-editor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { writeFileSync } from 'fs';
* WordPress dependencies
*/
import {
trashAllPosts,
activateTheme,
canvas,
createNewPost,
visitSiteEditor,
saveDraft,
insertBlock,
deleteAllTemplates,
} from '@wordpress/e2e-test-utils';

/**
Expand All @@ -32,13 +32,12 @@ jest.setTimeout( 1000000 );
describe( 'Site Editor Performance', () => {
beforeAll( async () => {
await activateTheme( 'emptytheme' );
await trashAllPosts( 'wp_template' );
await trashAllPosts( 'wp_template', 'auto-draft' );
await trashAllPosts( 'wp_template_part' );
await deleteAllTemplates( 'wp_template' );
await deleteAllTemplates( 'wp_template_part' );
} );
afterAll( async () => {
await trashAllPosts( 'wp_template' );
await trashAllPosts( 'wp_template_part' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we switch trashAllPosts to REST API too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can. I don't see a reason for visiting post list table before every test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not saying we should do so, just replacing the implementation of the current util.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ICYMI, there's an ongoing project to migrate e2e tests to Playwright, which follows the best practices of using requestUtils to set/clear states between tests.

await deleteAllTemplates( 'wp_template' );
await deleteAllTemplates( 'wp_template_part' );
await activateTheme( 'twentytwentyone' );
} );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import {
trashAllPosts,
deleteAllTemplates,
activateTheme,
visitSiteEditor,
} from '@wordpress/e2e-test-utils';
Expand All @@ -26,8 +26,8 @@ async function getDocumentSettingsSecondaryTitle() {
describe( 'Document Settings', () => {
beforeAll( async () => {
await activateTheme( 'emptytheme' );
await trashAllPosts( 'wp_template' );
await trashAllPosts( 'wp_template_part' );
await deleteAllTemplates( 'wp_template' );
await deleteAllTemplates( 'wp_template_part' );
} );
afterAll( async () => {
await activateTheme( 'twentytwentyone' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
insertBlock,
createNewPost,
publishPost,
trashAllPosts,
deleteAllTemplates,
activateTheme,
canvas,
openDocumentSettingsSidebar,
Expand Down Expand Up @@ -135,8 +135,8 @@ const removeErrorMocks = () => {
describe( 'Multi-entity editor states', () => {
beforeAll( async () => {
await activateTheme( 'emptytheme' );
await trashAllPosts( 'wp_template' );
await trashAllPosts( 'wp_template_part' );
await deleteAllTemplates( 'wp_template' );
await deleteAllTemplates( 'wp_template_part' );
} );

afterAll( async () => {
Expand Down Expand Up @@ -185,8 +185,8 @@ describe( 'Multi-entity editor states', () => {
const templateName = 'Custom Template';

beforeAll( async () => {
await trashAllPosts( 'wp_template' );
await trashAllPosts( 'wp_template_part' );
await deleteAllTemplates( 'wp_template' );
await deleteAllTemplates( 'wp_template_part' );
await createNewPost( {
postType: 'wp_template',
title: templateName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
clickButton,
createReusableBlock,
visitSiteEditor,
deleteAllTemplates,
} from '@wordpress/e2e-test-utils';

describe( 'Multi-entity save flow', () => {
Expand Down Expand Up @@ -43,8 +44,8 @@ describe( 'Multi-entity save flow', () => {

beforeAll( async () => {
await activateTheme( 'emptytheme' );
await trashAllPosts( 'wp_template' );
await trashAllPosts( 'wp_template_part' );
await deleteAllTemplates( 'wp_template' );
await deleteAllTemplates( 'wp_template_part' );
await trashAllPosts( 'wp_block' );

// Get the current Site Title and Site Tagline, so that we can reset
Expand Down
10 changes: 5 additions & 5 deletions packages/e2e-tests/specs/site-editor/settings-sidebar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import {
trashAllPosts,
deleteAllTemplates,
activateTheme,
getAllBlocks,
selectBlockByClientId,
Expand Down Expand Up @@ -39,12 +39,12 @@ async function getTemplateCard() {
describe( 'Settings sidebar', () => {
beforeAll( async () => {
await activateTheme( 'emptytheme' );
await trashAllPosts( 'wp_template' );
await trashAllPosts( 'wp_template_part' );
await deleteAllTemplates( 'wp_template' );
await deleteAllTemplates( 'wp_template_part' );
} );
afterAll( async () => {
await trashAllPosts( 'wp_template' );
await trashAllPosts( 'wp_template_part' );
await deleteAllTemplates( 'wp_template' );
await deleteAllTemplates( 'wp_template_part' );
await activateTheme( 'twentytwentyone' );
} );
beforeEach( async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import os from 'os';
* WordPress dependencies
*/
import {
trashAllPosts,
deleteAllTemplates,
activateTheme,
visitSiteEditor,
clickOnMoreMenuItem,
Expand All @@ -31,8 +31,8 @@ async function waitForFileExists( filePath, timeout = 10000 ) {
describe( 'Site Editor Templates Export', () => {
beforeAll( async () => {
await activateTheme( 'emptytheme' );
await trashAllPosts( 'wp_template' );
await trashAllPosts( 'wp_template_part' );
await deleteAllTemplates( 'wp_template' );
await deleteAllTemplates( 'wp_template_part' );
} );

afterAll( async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
* WordPress dependencies
*/
import {
trashAllPosts,
deleteAllTemplates,
activateTheme,
visitSiteEditor,
} from '@wordpress/e2e-test-utils';

describe( 'Site Editor Inserter', () => {
beforeAll( async () => {
await activateTheme( 'emptytheme' );
await trashAllPosts( 'wp_template' );
await trashAllPosts( 'wp_template_part' );
await deleteAllTemplates( 'wp_template' );
await deleteAllTemplates( 'wp_template_part' );
} );
afterAll( async () => {
await activateTheme( 'twentytwentyone' );
Expand Down
10 changes: 5 additions & 5 deletions packages/e2e-tests/specs/site-editor/template-part.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import {
insertBlock,
trashAllPosts,
deleteAllTemplates,
activateTheme,
getAllBlocks,
selectBlockByClientId,
Expand All @@ -18,12 +18,12 @@ const templatePartNameInput =
describe( 'Template Part', () => {
beforeAll( async () => {
await activateTheme( 'emptytheme' );
await trashAllPosts( 'wp_template' );
await trashAllPosts( 'wp_template_part' );
await deleteAllTemplates( 'wp_template' );
await deleteAllTemplates( 'wp_template_part' );
} );
afterAll( async () => {
await trashAllPosts( 'wp_template' );
await trashAllPosts( 'wp_template_part' );
await deleteAllTemplates( 'wp_template' );
await deleteAllTemplates( 'wp_template_part' );
await activateTheme( 'twentytwentyone' );
} );

Expand Down
34 changes: 12 additions & 22 deletions packages/e2e-tests/specs/site-editor/template-revert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
*/
import {
insertBlock,
trashAllPosts,
activateTheme,
switchUserToAdmin,
switchUserToTest,
visitAdminPage,
deleteAllTemplates,
visitSiteEditor,
getCurrentSiteEditorContent,
} from '@wordpress/e2e-test-utils';
import { addQueryArgs } from '@wordpress/url';

const assertSaveButtonIsDisabled = async () =>
page.waitForSelector(
Expand Down Expand Up @@ -71,30 +67,19 @@ const revertTemplate = async () => {
await assertSaveButtonIsEnabled();
};

const assertTemplatesAreDeleted = async () => {
await switchUserToAdmin();
const query = addQueryArgs( '', {
post_type: 'wp_template',
} ).slice( 1 );
await visitAdminPage( 'edit.php', query );
const element = await page.waitForSelector( '#the-list .no-items' );
expect( element ).toBeTruthy();
await switchUserToTest();
};

describe( 'Template Revert', () => {
beforeAll( async () => {
await activateTheme( 'emptytheme' );
await trashAllPosts( 'wp_template' );
await trashAllPosts( 'wp_template_part' );
await deleteAllTemplates( 'wp_template' );
await deleteAllTemplates( 'wp_template_part' );
} );
afterAll( async () => {
await trashAllPosts( 'wp_template' );
await trashAllPosts( 'wp_template_part' );
await deleteAllTemplates( 'wp_template' );
await deleteAllTemplates( 'wp_template_part' );
await activateTheme( 'twentytwentyone' );
} );
beforeEach( async () => {
await trashAllPosts( 'wp_template' );
await deleteAllTemplates( 'wp_template' );
await visitSiteEditor();
} );

Expand All @@ -104,7 +89,12 @@ describe( 'Template Revert', () => {
await revertTemplate();
await save();

await assertTemplatesAreDeleted();
await page.click( '.edit-site-document-actions__get-info' );

// The revert button isn't visible anymore.
expect(
await page.$( '.edit-site-template-details__revert-button' )
).toBeNull();
} );

it( 'should show the original content after revert', async () => {
Expand Down