Skip to content

Commit 3d4146d

Browse files
committed
Build/Test Tools: Run the trash end-to-end tests in parallel.
Give the Empty Trash test its own post type and restore only the post created by the current test. Developed in: #16175 Props adrianmoldovanwp. See #66392. git-svn-id: https://develop.svn.wordpress.org/trunk@65743 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 13c2209 commit 3d4146d

3 files changed

Lines changed: 110 additions & 45 deletions

File tree

‎tests/e2e/playwright.config.js‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ const SERIAL_SPECS = [
2020
'maintenance-mode.test.js',
2121
'gutenberg-plugin.test.js',
2222
'theme-activation.test.js',
23-
// This deletes all posts, including other workers' content.
24-
'empty-trash.test.js',
2523
];
2624

2725
const config = defineConfig( {

‎tests/e2e/specs/empty-trash-restore-trashed-posts.test.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,40 @@ test( 'bulk trashes only the selected posts', async ( {
108108
} );
109109
expect( control.status ).toBe( 'publish' );
110110
} );
111+
112+
test( 'Restore trash post', async ( {
113+
page,
114+
admin,
115+
postFactory,
116+
requestUtils,
117+
} ) => {
118+
const title = postFactory.uniqueTitle( 'Restore trash' );
119+
const post = await postFactory.create( { title, status: 'publish' } );
120+
await requestUtils.rest( {
121+
method: 'DELETE',
122+
path: `/wp/v2/posts/${ post.id }`,
123+
} );
124+
125+
await admin.visitAdminPage(
126+
'edit.php',
127+
new URLSearchParams( { post_status: 'trash', s: title } ).toString()
128+
);
129+
const row = postRow( page, post.id );
130+
await row.getByRole( 'cell' ).filter( { hasText: title } ).hover();
131+
await row
132+
.getByRole( 'link', {
133+
name: `Restore “${ title }” from the Trash`,
134+
exact: true,
135+
} )
136+
.click();
137+
138+
await expect( adminNotice( page ) ).toContainText(
139+
'1 post restored from the Trash.'
140+
);
141+
await expect( row ).toHaveCount( 0 );
142+
const restoredPost = await requestUtils.rest( {
143+
path: `/wp/v2/posts/${ post.id }`,
144+
params: { context: 'edit' },
145+
} );
146+
expect( restoredPost.status ).toBe( 'draft' );
147+
} );
Lines changed: 73 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,86 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { randomUUID } from 'node:crypto';
5+
16
/**
27
* Internal dependencies
38
*/
49
import { test, expect } from '../fixtures';
510
import { adminNotice } from '../utils/admin';
611

7-
const POST_TITLE = 'Test Title';
8-
912
test.describe( 'Empty Trash', () => {
10-
test.beforeEach( async ( { requestUtils } ) => {
11-
await requestUtils.deleteAllPosts();
12-
});
13-
14-
test('Empty Trash', async ({ admin, editor, page }) => {
15-
await admin.createNewPost( { title: POST_TITLE } );
16-
await editor.publishPost();
17-
18-
await admin.visitAdminPage( '/edit.php' );
19-
20-
const listTable = page.getByRole( 'table', { name: 'Table ordered by' } );
21-
await expect( listTable ).toBeVisible();
22-
23-
// Move post to trash
24-
await listTable.getByRole( 'link', { name: POST_TITLE, exact: true } ).hover();
25-
await listTable.getByRole( 'link', { name: `Move “${POST_TITLE}” to the Trash` } ).click();
26-
27-
// Empty trash
28-
await page.getByRole( 'link', { name: 'Trash' } ).click();
29-
await page.getByRole( 'button', { name: 'Empty Trash' } ).first().click();
30-
31-
await expect( adminNotice( page ) ).toContainText( '1 post permanently deleted.' );
32-
} );
33-
34-
test('Restore trash post', async ( { admin, editor, page }) => {
35-
await admin.createNewPost( { title: POST_TITLE } );
36-
await editor.publishPost();
37-
38-
await admin.visitAdminPage( '/edit.php' );
39-
40-
const listTable = page.getByRole( 'table', { name: 'Table ordered by' } );
41-
await expect( listTable ).toBeVisible();
13+
test( 'Empty Trash', async ( {
14+
admin,
15+
muPlugin,
16+
page,
17+
requestUtils,
18+
} ) => {
19+
// Empty Trash deletes every trashed post of one post type, so this test owns its type.
20+
// Post type names have at most 20 characters.
21+
const postType = `e2e_${ randomUUID().replaceAll( '-', '' ).slice( 0, 16 ) }`;
22+
// Other workers share the site, so only requests that name this type register it.
23+
await muPlugin.write(
24+
'empty-trash-post-type.php',
25+
`<?php
26+
if ( str_contains( $_SERVER['REQUEST_URI'] ?? '', '${ postType }' ) ) {
27+
add_action(
28+
'init',
29+
static function () {
30+
register_post_type(
31+
'${ postType }',
32+
array(
33+
'label' => 'Empty Trash test',
34+
'show_ui' => true,
35+
'show_in_menu' => false,
36+
'show_in_rest' => true,
37+
'rewrite' => false,
38+
)
39+
);
40+
}
41+
);
42+
}`
43+
);
4244

43-
// Move post to trash
44-
await listTable.getByRole( 'link', { name: POST_TITLE, exact: true } ).hover();
45-
await listTable.getByRole( 'link', { name: `Move “${POST_TITLE}” to the Trash` } ).click();
45+
const post = await requestUtils.rest( {
46+
method: 'POST',
47+
path: `/wp/v2/${ postType }`,
48+
data: { title: 'Empty Trash test', status: 'publish' },
49+
} );
4650

47-
await page.getByRole( 'link', { name: 'Trash' } ).click();
51+
try {
52+
await requestUtils.rest( {
53+
method: 'DELETE',
54+
path: `/wp/v2/${ postType }/${ post.id }`,
55+
} );
4856

49-
// Remove post from trash.
50-
await listTable.getByRole( 'cell' ).filter( { hasText: POST_TITLE } ).hover();
51-
await listTable.getByRole( 'link', { name: `Restore “${POST_TITLE}” from the Trash` } ).click();
57+
await admin.visitAdminPage(
58+
'edit.php',
59+
new URLSearchParams( {
60+
post_type: postType,
61+
post_status: 'trash',
62+
} ).toString()
63+
);
64+
await page
65+
.getByRole( 'button', { name: 'Empty Trash' } )
66+
.first()
67+
.click();
5268

53-
// Expect for success message for restored post.
54-
await expect( adminNotice( page ) ).toContainText( '1 post restored from the Trash.' );
69+
await expect( adminNotice( page ) ).toContainText(
70+
'1 post permanently deleted.'
71+
);
72+
} finally {
73+
await requestUtils
74+
.rest( {
75+
method: 'DELETE',
76+
path: `/wp/v2/${ postType }/${ post.id }`,
77+
params: { force: true },
78+
} )
79+
.catch( ( error ) => {
80+
if ( error.data?.status !== 404 ) {
81+
throw error;
82+
}
83+
} );
84+
}
5585
} );
5686
} );

0 commit comments

Comments
 (0)