diff --git a/tests/e2e/playwright.config.js b/tests/e2e/playwright.config.js index abcf308472365..43c8bd10c3ffc 100644 --- a/tests/e2e/playwright.config.js +++ b/tests/e2e/playwright.config.js @@ -23,11 +23,8 @@ const SERIAL_SPECS = [ 'gutenberg-plugin.test.js', 'plugins.test.js', 'themes.test.js', - // These delete all posts or media, including other workers' content. - 'cache-control-headers-directives.test.js', - 'dashboard.test.js', - 'edit-posts.test.js', - 'empty-trash-restore-trashed-posts.test.js', + // This deletes all posts, including other workers' content. + 'empty-trash.test.js', ]; const config = defineConfig( { diff --git a/tests/e2e/specs/cache-control-headers-directives.test.js b/tests/e2e/specs/cache-control-headers-directives.test.js index e2f4ffdc3e566..192fe49f5162a 100644 --- a/tests/e2e/specs/cache-control-headers-directives.test.js +++ b/tests/e2e/specs/cache-control-headers-directives.test.js @@ -4,20 +4,17 @@ import { test, expect } from '../fixtures'; test.describe( 'Cache Control header directives', () => { - test.beforeAll( async ( { requestUtils } ) => { - await requestUtils.deleteAllPosts(); - }); - test( 'No private directive present in cache control when user not logged in.', - async ( { loggedOutPage, admin, editor} + async ( { loggedOutPage, postFactory } ) => { - await admin.createNewPost( { title: 'Hello World' } ); - await editor.publishPost(); - - await admin.visitAdminPage( '/' ); + const post = await postFactory.create( { + title: postFactory.uniqueTitle( 'Cache control' ), + status: 'publish', + } ); + const { pathname, search } = new URL( post.link ); - const response = await loggedOutPage.goto( '/hello-world/' ); + const response = await loggedOutPage.goto( pathname + search ); const responseHeaders = response.headers(); const cacheControl = responseHeaders[ 'cache-control' ] ?? ''; diff --git a/tests/e2e/specs/dashboard.test.js b/tests/e2e/specs/dashboard.test.js index ac664fa01fb28..2fdb4383a6caa 100644 --- a/tests/e2e/specs/dashboard.test.js +++ b/tests/e2e/specs/dashboard.test.js @@ -5,8 +5,39 @@ import { test, expect } from '../fixtures'; import { signIn } from '../fixtures/user'; test.describe( 'Quick Draft', () => { - test.beforeEach( async ({ requestUtils }) => { - await requestUtils.deleteAllPosts(); + // Recent Drafts and the author-filtered post list show only this user's posts. + test.use( { storageState: { cookies: [], origins: [] } } ); + let userId; + + test.beforeEach( async ( { page, userFactory } ) => { + userId = undefined; + const { user, username, password } = await userFactory.create( { + roles: [ 'administrator' ], + } ); + userId = user.id; + await signIn( page, username, password ); + await expect( + page.getByRole( 'heading', { name: 'Dashboard', exact: true } ) + ).toBeVisible(); + } ); + + // Deleting the user would move its saved drafts to the trash. + test.afterEach( async ( { requestUtils } ) => { + // Without an author, the query would match other workers' drafts. + if ( ! userId ) { + return; + } + const drafts = await requestUtils.rest( { + path: '/wp/v2/posts', + params: { author: userId, status: 'draft', per_page: 100 }, + } ); + for ( const draft of drafts ) { + await requestUtils.rest( { + method: 'DELETE', + path: `/wp/v2/posts/${ draft.id }`, + params: { force: true }, + } ); + } } ); test( 'should allow Quick Draft to be created with Title and Content', async ( { @@ -57,7 +88,7 @@ test.describe( 'Quick Draft', () => { ).toBeVisible(); // Check that the new draft appears in the Posts page. - await admin.visitAdminPage( '/edit.php' ); + await admin.visitAdminPage( '/edit.php', `author=${ userId }` ); await expect( page @@ -92,7 +123,7 @@ test.describe( 'Quick Draft', () => { ).toHaveText( 'Cannot create a draft post with empty title and content.' ); // Check that no new draft appears in the Posts page. - await admin.visitAdminPage( '/edit.php' ); + await admin.visitAdminPage( '/edit.php', `author=${ userId }` ); await expect( page.getByRole( 'cell', { name: 'No posts found.' } ) @@ -132,7 +163,7 @@ test.describe( 'Quick Draft', () => { ).toHaveText( 'Quick Draft test title' ); // Check that the new draft appears in the Posts page. - await admin.visitAdminPage( '/edit.php' ); + await admin.visitAdminPage( '/edit.php', `author=${ userId }` ); await expect( page @@ -181,7 +212,7 @@ test.describe( 'Quick Draft', () => { ).toBeVisible(); // Check that the new draft appears in the Posts page. - await admin.visitAdminPage( '/edit.php' ); + await admin.visitAdminPage( '/edit.php', `author=${ userId }` ); await expect( page diff --git a/tests/e2e/specs/edit-posts.test.js b/tests/e2e/specs/edit-posts.test.js index 534895ba6069f..792deca7a1f2d 100644 --- a/tests/e2e/specs/edit-posts.test.js +++ b/tests/e2e/specs/edit-posts.test.js @@ -1,18 +1,26 @@ +/** + * External dependencies + */ +import { randomUUID } from 'node:crypto'; + /** * Internal dependencies */ import { test, expect } from '../fixtures'; test.describe( 'Edit Posts', () => { - test.beforeEach( async ( { requestUtils }) => { - await requestUtils.deleteAllPosts(); - } ); + async function visitPostList( admin, title ) { + await admin.visitAdminPage( + 'edit.php', + new URLSearchParams( { s: title } ).toString() + ); + } - test( 'displays a message in the posts table when no posts are present',async ( { + test( 'displays a message in the posts table when a search finds no posts',async ( { admin, page, } ) => { - await admin.visitAdminPage( '/edit.php' ); + await visitPostList( admin, randomUUID() ); await expect( page.getByRole( 'cell', { name: 'No posts found.' } ) ).toBeVisible(); @@ -20,14 +28,13 @@ test.describe( 'Edit Posts', () => { test( 'lists a published post and renders its title and content on the front end', async ( { admin, - editor, page, + postFactory, } ) => { - const title = 'Test Title'; + const title = postFactory.uniqueTitle( 'Edit Posts list' ); const content = 'This content should be visible on the published post.'; - await admin.createNewPost( { title, content } ); - await editor.publishPost(); - await admin.visitAdminPage( '/edit.php' ); + const post = await postFactory.create( { title, content, status: 'publish' } ); + await visitPostList( admin, title ); const listTable = page.getByRole( 'table', { name: 'Table ordered by' } ); await expect( listTable ).toBeVisible(); @@ -36,16 +43,17 @@ test.describe( 'Edit Posts', () => { await expect( listTable.locator( '#the-list' ).getByRole( 'row' ) ).toHaveCount( 1 ); + const row = listTable.locator( `#post-${ post.id }` ); // Expect the title of the post to be correct. - const postLink = listTable.getByRole( 'link', { + const postLink = row.getByRole( 'link', { name: title, exact: true, } ); await expect( postLink ).toBeVisible(); await postLink.hover(); - await listTable.getByRole( 'link', { name: `View “${ title }”`, exact: true } ).click(); + await row.getByRole( 'link', { name: `View “${ title }”`, exact: true } ).click(); await expect( page.getByRole( 'heading', { name: title, exact: true } ) ).toBeVisible(); await expect( page.getByText( content, { exact: true } ) ).toBeVisible(); } ); @@ -54,17 +62,20 @@ test.describe( 'Edit Posts', () => { admin, editor, page, + postFactory, } ) => { - const title = 'Test Title'; - await admin.createNewPost( { title } ); - await editor.publishPost(); - await admin.visitAdminPage( '/edit.php' ); + const title = postFactory.uniqueTitle( 'Edit Posts edit' ); + const post = await postFactory.create( { title, status: 'publish' } ); + await visitPostList( admin, title ); const listTable = page.getByRole( 'table', { name: 'Table ordered by' } ); await expect( listTable ).toBeVisible(); // Click the post title (edit) link - await listTable.getByRole( 'link', { name: title, exact: true } ).click(); + await listTable + .locator( `#post-${ post.id }` ) + .getByRole( 'link', { name: title, exact: true } ) + .click(); const editorPostTitle = editor.canvas.getByRole( 'textbox', { name: 'Add title' } ); @@ -76,20 +87,20 @@ test.describe( 'Edit Posts', () => { test( 'allows an existing post to be quick edited using the Quick Edit button', async ( { admin, - editor, page, - pageUtils + pageUtils, + postFactory, } ) => { - const title = 'Test Title'; - await admin.createNewPost( { title } ); - await editor.publishPost(); - await admin.visitAdminPage( '/edit.php' ); + const title = postFactory.uniqueTitle( 'Edit Posts quick edit' ); + const post = await postFactory.create( { title, status: 'publish' } ); + await visitPostList( admin, title ); const listTable = page.getByRole( 'table', { name: 'Table ordered by' } ); await expect( listTable ).toBeVisible(); + const row = listTable.locator( `#post-${ post.id }` ); // // Focus on the post title link. - await listTable.getByRole( 'link', { name: title, exact: true } ).focus(); + await row.getByRole( 'link', { name: title, exact: true } ).focus(); // Tab to the Quick Edit button and press Enter to quick edit. await pageUtils.pressKeys( 'Tab', { times: 2 } ); @@ -108,7 +119,7 @@ test.describe( 'Edit Posts', () => { await page.getByRole( 'button', { name: 'Update' } ).click(); // Wait for the quick edit button to reappear. - await expect( page.getByRole( 'button', { name: 'Quick Edit' } ) ).toBeVisible(); + await expect( row.getByRole( 'button', { name: 'Quick Edit' } ) ).toBeVisible(); // Expect there to be one row in the post list. await expect( @@ -117,7 +128,7 @@ test.describe( 'Edit Posts', () => { // Expect the title of the post to be correct. await expect( - listTable.getByRole( 'link', { + row.getByRole( 'link', { name: `${ title } Edited`, exact: true, } ) @@ -126,19 +137,19 @@ test.describe( 'Edit Posts', () => { test( 'allows an existing post to be deleted using the Trash button', async ( { admin, - editor, page, + postFactory, } ) => { - const title = 'Test Title'; - await admin.createNewPost( { title } ); - await editor.publishPost(); - await admin.visitAdminPage( '/edit.php' ); + const title = postFactory.uniqueTitle( 'Edit Posts trash' ); + const post = await postFactory.create( { title, status: 'publish' } ); + await visitPostList( admin, title ); const listTable = page.getByRole( 'table', { name: 'Table ordered by' } ); await expect( listTable ).toBeVisible(); + const row = listTable.locator( `#post-${ post.id }` ); - await listTable.getByRole( 'link', { name: title, exact: true } ).hover(); - await listTable + await row.getByRole( 'link', { name: title, exact: true } ).hover(); + await row .getByRole( 'link', { name: `Move “${ title }” to the Trash`, exact: true, diff --git a/tests/e2e/specs/empty-trash-restore-trashed-posts.test.js b/tests/e2e/specs/empty-trash-restore-trashed-posts.test.js index 6940c10837cd7..2703b525b6dcc 100644 --- a/tests/e2e/specs/empty-trash-restore-trashed-posts.test.js +++ b/tests/e2e/specs/empty-trash-restore-trashed-posts.test.js @@ -3,8 +3,6 @@ */ import { test, expect } from '../fixtures'; -const POST_TITLE = 'Test Title'; - test( 'Undo restores a trashed post to its published status', async ( { page, admin, @@ -58,55 +56,6 @@ test( 'Undo restores a trashed post to its published status', async ( { expect( restoredPost.status ).toBe( 'publish' ); } ); -test.describe( 'Empty Trash', () => { - test.beforeEach( async ( { requestUtils } ) => { - await requestUtils.deleteAllPosts(); - }); - - test('Empty Trash', async ({ admin, editor, page }) => { - await admin.createNewPost( { title: POST_TITLE } ); - await editor.publishPost(); - - await admin.visitAdminPage( '/edit.php' ); - - const listTable = page.getByRole( 'table', { name: 'Table ordered by' } ); - await expect( listTable ).toBeVisible(); - - // Move post to trash - await listTable.getByRole( 'link', { name: POST_TITLE, exact: true } ).hover(); - await listTable.getByRole( 'link', { name: `Move “${POST_TITLE}” to the Trash` } ).click(); - - // Empty trash - await page.getByRole( 'link', { name: 'Trash' } ).click(); - await page.getByRole( 'button', { name: 'Empty Trash' } ).first().click(); - - await expect( page.locator( '#message' ) ).toContainText( '1 post permanently deleted.' ); - } ); - - test('Restore trash post', async ( { admin, editor, page }) => { - await admin.createNewPost( { title: POST_TITLE } ); - await editor.publishPost(); - - await admin.visitAdminPage( '/edit.php' ); - - const listTable = page.getByRole( 'table', { name: 'Table ordered by' } ); - await expect( listTable ).toBeVisible(); - - // Move post to trash - await listTable.getByRole( 'link', { name: POST_TITLE, exact: true } ).hover(); - await listTable.getByRole( 'link', { name: `Move “${POST_TITLE}” to the Trash` } ).click(); - - await page.getByRole( 'link', { name: 'Trash' } ).click(); - - // Remove post from trash. - await listTable.getByRole( 'cell' ).filter( { hasText: POST_TITLE } ).hover(); - await listTable.getByRole( 'link', { name: `Restore “${POST_TITLE}” from the Trash` } ).click(); - - // Expect for success message for restored post. - await expect( page.locator( '#message' ) ).toContainText( '1 post restored from the Trash.' ); - } ); -} ); - test( 'bulk trashes only the selected posts', async ( { page, admin, diff --git a/tests/e2e/specs/empty-trash.test.js b/tests/e2e/specs/empty-trash.test.js new file mode 100644 index 0000000000000..d0fee6a7ad919 --- /dev/null +++ b/tests/e2e/specs/empty-trash.test.js @@ -0,0 +1,55 @@ +/** + * Internal dependencies + */ +import { test, expect } from '../fixtures'; + +const POST_TITLE = 'Test Title'; + +test.describe( 'Empty Trash', () => { + test.beforeEach( async ( { requestUtils } ) => { + await requestUtils.deleteAllPosts(); + }); + + test('Empty Trash', async ({ admin, editor, page }) => { + await admin.createNewPost( { title: POST_TITLE } ); + await editor.publishPost(); + + await admin.visitAdminPage( '/edit.php' ); + + const listTable = page.getByRole( 'table', { name: 'Table ordered by' } ); + await expect( listTable ).toBeVisible(); + + // Move post to trash + await listTable.getByRole( 'link', { name: POST_TITLE, exact: true } ).hover(); + await listTable.getByRole( 'link', { name: `Move “${POST_TITLE}” to the Trash` } ).click(); + + // Empty trash + await page.getByRole( 'link', { name: 'Trash' } ).click(); + await page.getByRole( 'button', { name: 'Empty Trash' } ).first().click(); + + await expect( page.locator( '#message' ) ).toContainText( '1 post permanently deleted.' ); + } ); + + test('Restore trash post', async ( { admin, editor, page }) => { + await admin.createNewPost( { title: POST_TITLE } ); + await editor.publishPost(); + + await admin.visitAdminPage( '/edit.php' ); + + const listTable = page.getByRole( 'table', { name: 'Table ordered by' } ); + await expect( listTable ).toBeVisible(); + + // Move post to trash + await listTable.getByRole( 'link', { name: POST_TITLE, exact: true } ).hover(); + await listTable.getByRole( 'link', { name: `Move “${POST_TITLE}” to the Trash` } ).click(); + + await page.getByRole( 'link', { name: 'Trash' } ).click(); + + // Remove post from trash. + await listTable.getByRole( 'cell' ).filter( { hasText: POST_TITLE } ).hover(); + await listTable.getByRole( 'link', { name: `Restore “${POST_TITLE}” from the Trash` } ).click(); + + // Expect for success message for restored post. + await expect( page.locator( '#message' ) ).toContainText( '1 post restored from the Trash.' ); + } ); +} );