|
| 1 | +/** |
| 2 | + * WordPress dependencies |
| 3 | + */ |
| 4 | +const { test, expect } = require('@wordpress/e2e-test-utils-playwright'); |
| 5 | + |
| 6 | +const PLUGIN_SLUG = 'secure-custom-fields'; |
| 7 | +const TEST_PLUGIN_SLUG = 'scf-test-plugin-get-field-movie-title'; |
| 8 | +const FIELD_GROUP_LABEL = 'Movie Details'; |
| 9 | +const FIELD_LABEL = 'Movie Title'; |
| 10 | + |
| 11 | +test.describe('Field Type > Text', () => { |
| 12 | + test.beforeAll(async ({ requestUtils }) => { |
| 13 | + await requestUtils.activatePlugin(PLUGIN_SLUG); |
| 14 | + await requestUtils.activatePlugin(TEST_PLUGIN_SLUG); |
| 15 | + |
| 16 | + }); |
| 17 | + |
| 18 | + test.afterAll(async ({ requestUtils }) => { |
| 19 | + await requestUtils.deactivatePlugin(PLUGIN_SLUG); |
| 20 | + await requestUtils.deactivatePlugin(TEST_PLUGIN_SLUG); |
| 21 | + await requestUtils.deleteAllPosts(); |
| 22 | + }); |
| 23 | + |
| 24 | + test.beforeEach(async ({ page, admin }) => { |
| 25 | + await deleteFieldGroups(page, admin); |
| 26 | + }); |
| 27 | + |
| 28 | + test('should create a text field and verify it in admin', async ({ page, admin, editor, requestUtils }) => { |
| 29 | + |
| 30 | + // Navigate to Field Groups and create new. |
| 31 | + await admin.visitAdminPage('edit.php', 'post_type=acf-field-group'); |
| 32 | + const addNewButton = page.locator('a.acf-btn:has-text("Add New")'); |
| 33 | + await addNewButton.click(); |
| 34 | + |
| 35 | + // Fill field group title. |
| 36 | + await page.waitForSelector('#title'); |
| 37 | + await page.fill('#title', FIELD_GROUP_LABEL); |
| 38 | + |
| 39 | + // Add text field. |
| 40 | + const fieldLabel = page.locator('input[id^="acf_fields-field_"][id$="-label"]'); |
| 41 | + await fieldLabel.fill(FIELD_LABEL); |
| 42 | + // The field name is generated automatically. |
| 43 | + |
| 44 | + // Select field type as text (it's default, but let's be explicit). |
| 45 | + const fieldType = page.locator('select[id^="acf_fields-field_"][id$="-type"]'); |
| 46 | + await fieldType.selectOption('text'); |
| 47 | + |
| 48 | + // Submit form. |
| 49 | + const publishButton = page.locator('button.acf-btn.acf-publish[type="submit"]'); |
| 50 | + await publishButton.click(); |
| 51 | + |
| 52 | + // Verify success message. |
| 53 | + const successNotice = page.locator('.updated.notice'); |
| 54 | + await expect(successNotice).toBeVisible(); |
| 55 | + await expect(successNotice).toContainText('Field group published'); |
| 56 | + |
| 57 | + // Verify field group appears in the list. |
| 58 | + await admin.visitAdminPage('edit.php', 'post_type=acf-field-group'); |
| 59 | + const fieldGroupRow = page.locator(`tr:has-text("${FIELD_GROUP_LABEL}")`); |
| 60 | + await expect(fieldGroupRow).toBeVisible(); |
| 61 | + |
| 62 | + // Create a new post |
| 63 | + const post = await requestUtils.createPost({ |
| 64 | + title: 'Movie 1', |
| 65 | + status: 'draft', |
| 66 | + showWelcomeGuide: false, |
| 67 | + }); |
| 68 | + |
| 69 | + // Navigate to edit post page |
| 70 | + await admin.editPost(post.id); |
| 71 | + |
| 72 | + // Fill in the movie title field using data-name attribute |
| 73 | + const movieTitleField = page.locator('.acf-field[data-name="movie_title"] input[type="text"]'); |
| 74 | + await movieTitleField.fill('The Shawshank Redemption'); |
| 75 | + |
| 76 | + // Verify the movie title is displayed |
| 77 | + const previewPage = await editor.openPreviewPage(); |
| 78 | + |
| 79 | + const movieTitleElement = previewPage.locator('#scf-test-movie-title'); |
| 80 | + await expect(movieTitleElement).toBeVisible(); |
| 81 | + await expect(movieTitleElement).toContainText('Movie title: The Shawshank Redemption'); |
| 82 | + |
| 83 | + // Close the preview tab |
| 84 | + await previewPage.close(); |
| 85 | + |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +/** |
| 90 | + * Helper function to delete the field group |
| 91 | + */ |
| 92 | +async function deleteFieldGroups(page, admin) { |
| 93 | + await admin.visitAdminPage('edit.php', 'post_type=acf-field-group'); |
| 94 | + |
| 95 | + // Find and select the field group row |
| 96 | + const allFieldGroupsCheckbox = page.locator('input#cb-select-all-1'); |
| 97 | + |
| 98 | + if (await allFieldGroupsCheckbox.isVisible()) { |
| 99 | + await allFieldGroupsCheckbox.check(); |
| 100 | + // Use bulk actions to trash the field group |
| 101 | + await page.selectOption('#bulk-action-selector-bottom', 'trash'); |
| 102 | + await page.click('#doaction2'); |
| 103 | + |
| 104 | + // Verify deletion success message |
| 105 | + const deleteMessage = page.locator('.updated.notice'); |
| 106 | + await expect(deleteMessage).toBeVisible({ timeout: 5000 }); |
| 107 | + await expect(deleteMessage).toContainText('moved to the Trash'); |
| 108 | + |
| 109 | + await emptyTrash(page, admin); |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Helper function to empty trash |
| 117 | + */ |
| 118 | +async function emptyTrash(page, admin) { |
| 119 | + await admin.visitAdminPage('edit.php', 'post_status=trash&post_type=acf-field-group'); |
| 120 | + const emptyTrashButton = page.locator('.tablenav.bottom input[name="delete_all"][value="Empty Trash"]'); |
| 121 | + await emptyTrashButton.waitFor({ state: 'visible' }); |
| 122 | + await emptyTrashButton.click(); |
| 123 | + |
| 124 | + // Verify success notice |
| 125 | + const successNotice = page.locator('.notice.updated p'); |
| 126 | + await expect(successNotice).toBeVisible(); |
| 127 | + await expect(successNotice).toHaveText(/permanently deleted/); |
| 128 | +} |
0 commit comments