Skip to content

Commit 7832919

Browse files
authored
E2E: Add field text testing. (#103)
1 parent 90e9a3d commit 7832919

File tree

4 files changed

+169
-3
lines changed

4 files changed

+169
-3
lines changed

.wp-env.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
{
2-
"$schema": "./schemas/json/wp-env.json",
3-
"plugins": [ "." ]
2+
"$schema": "./schemas/json/wp-env.json",
3+
"plugins": ["."],
4+
"env": {
5+
"tests": {
6+
"mappings": {
7+
"wp-content/plugins/secure-custom-fields": ".",
8+
"wp-content/plugins/scf-test-plugins": "./tests/e2e/plugins"
9+
}
10+
}
11+
}
412
}

tests/e2e/field-type-text.spec.ts

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
}

tests/e2e/plugin-activation.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ test.describe('Plugin Activation', () => {
3636
const pluginName = page.locator(`tr[data-plugin="${PLUGIN_PATH}"] .plugin-title strong`);
3737
await expect(pluginName).toHaveText('Secure Custom Fields');
3838
});
39-
});
39+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Plugin Name: SCF Test Plugin, Get Field Movie Title
4+
* Plugin URI: https://github.com/WordPress/secure-custom-fields
5+
* Author: SCF Team
6+
*
7+
* @package scf-test-plugins
8+
*/
9+
10+
/**
11+
* Add post-formats support to pages
12+
*/
13+
function scf_add_get_field_at_the_end() {
14+
// Get the field object to validate it exists.
15+
$field_object = get_field_object( 'movie_title' );
16+
17+
// Only proceed if the field exists and is a valid type.
18+
if ( $field_object && isset( $field_object['type'] ) && 'text' === $field_object['type'] ) {
19+
$field = get_field( 'movie_title' );
20+
21+
// Ensure we have a string value and sanitize it.
22+
$field = is_string( $field ) ? sanitize_text_field( $field ) : '';
23+
24+
return '<p id="scf-test-movie-title">Movie title: ' . $field . '</p>';
25+
}
26+
27+
return '';
28+
}
29+
30+
add_filter( 'the_content', 'scf_add_get_field_at_the_end' );

0 commit comments

Comments
 (0)