|
| 1 | +/** |
| 2 | + * External dependencies |
| 3 | + */ |
| 4 | +import { randomUUID } from 'node:crypto'; |
| 5 | + |
1 | 6 | /** |
2 | 7 | * Internal dependencies |
3 | 8 | */ |
4 | 9 | import { test, expect } from '../fixtures'; |
5 | 10 | import { adminNotice } from '../utils/admin'; |
6 | 11 |
|
7 | | -const POST_TITLE = 'Test Title'; |
8 | | - |
9 | 12 | 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 | + ); |
42 | 44 |
|
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 | + } ); |
46 | 50 |
|
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 | + } ); |
48 | 56 |
|
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(); |
52 | 68 |
|
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 | + } |
55 | 85 | } ); |
56 | 86 | } ); |
0 commit comments