Skip to content

E2E: Add field text testing. #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 4, 2025
Merged

E2E: Add field text testing. #103

merged 11 commits into from
Apr 4, 2025

Conversation

cbravobernal
Copy link
Contributor

What

Includes a test and the boilerplate for a text field creation, cleaning, and frontend rendering check.

Comment on lines 17 to 33
$field_object = get_field_object( 'movie_title' );

// Only proceed if the field exists and is a valid type.
if ( $field_object && isset( $field_object['type'] ) && 'text' === $field_object['type'] ) {
$field = get_field( 'movie_title' );

// Ensure we have a string value and sanitize it.
$field = is_string( $field ) ? $field : '';

// Sanitize the field value using WordPress sanitization functions.
$field = sanitize_text_field( $field );

// Escape the output for HTML context.
$escaped_field = esc_html( $field );

// Use wp_kses_post to allow safe HTML if needed, but escape by default.
$output = wp_kses_post( '<br><p id="scf-test-movie-title">Movie title: ' . $escaped_field . '</p>' );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pkevan Should SCF escape automatically within the get_field function?

Docs says that you need to do this $escaped_wysiwyg = get_field('wysiwyg', false, true, true);, but also recommends using wp_kses_post.

Am I being redundant here? I have that feeling.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wp_kses_post filters out unallowed tags and attributes. In effect, if you would not escape the field and the HTML tags are allowed they would get printed inside the paragraph. So it sounds like a decision of whether you allow any HTML for the field here.

Copy link
Contributor

@pkevan pkevan Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pkevan Should SCF escape automatically within the get_field function?

Generally this isn't expected, and would more than likely get flagged when using phpcs anyway.

Escaping should be in the realm of whatever is outputting the data, so in this case it's not needed, but generally any outputting functions within the plugin which requires no user input should be escaping it after using get_field i.e. the shortcodes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one does not require user input cause the input will be only filled in an automated test. So I guess we are fine with removing it then.

@cbravobernal
Copy link
Contributor Author

Let's add a cleaning step before every test to avoid the errors we have right now.

@cbravobernal cbravobernal mentioned this pull request Apr 4, 2025
33 tasks
@cbravobernal cbravobernal marked this pull request as draft April 4, 2025 13:22
@cbravobernal cbravobernal marked this pull request as ready for review April 4, 2025 15:30
@cbravobernal cbravobernal requested a review from kraftbj April 4, 2025 15:31
@cbravobernal cbravobernal assigned cbravobernal and unassigned kraftbj Apr 4, 2025
@cbravobernal cbravobernal merged commit 7832919 into trunk Apr 4, 2025
5 checks passed
@kraftbj kraftbj deleted the add/text-field-e2e-test branch April 7, 2025 13:34
@kraftbj kraftbj added this to the 6.5.0 milestone Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants