Skip to content
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

Update phone validation regex to accept only numbers and correct amount - Fix "must consist of only numbers" validation error message to be specific #112

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from

Conversation

MaxwellGarceau
Copy link
Collaborator

@MaxwellGarceau MaxwellGarceau commented Jan 3, 2025

Description of the Change

Update phone validation to fix several bugs and give detailed error messages.

Issue #101 - Update phone validation regex to accept only numbers and correct amount

  • Fix a bug that would allow a user to enter phone numbers that contained letters.

Issue #104 - Update phone validation regex to accept only numbers and correct amount

  • Fix a bug that would allow a user to enter whitespace in the middle of the last group of numbers
  • If too few digits are entered, the form displays an appropriate error message letting the user know that too few digits were entered

Closes #101
Closes #104
Both these issues required work in the same function that would have created ugly merge conflicts.

How to test the Change

Setup

  1. Change the phone format in the test user Mailchimp account to US format
  2. Click the "Update List" button in the Mailchimp admin
  3. Set the Phone number merge field to be included
  4. Navigate to a FE form for submission testing

General

  • Form submission with empty phone number and only email should succeed

Phone number should not accept letters

Phone number should not accept letters - Bug is fixed

  • Submit a form with a phone number that contains a letter. The submission should fail. The error message should notify the user that the phone number must only contain numbers.

Test a valid phone number

  • Submit a valid US phone number and the submission should succeed

Test that other invalid characters are not accepted

The phone number field should only accept numbers. The error message should notify the user that the phone number must only contain numbers.

  • The phone field should not accept any of these characters
    • Letters (A–Z, a–z).
    • Special characters like @, #, $, %, *, &, !, etc.
    • Whitespace, both at the ends and also in the middle.
    • Multiple or misplaced delimiters, such as --, (123)), or leading/trailing symbols.
    • Any character outside the ASCII range (e.g., emojis, non-standard symbols). - too broad to really test comprehensively

Phone number does not contain enough digits

Whitespace testing

All whitespace should be stripped and should return an error notifying the user that the phone number does not have enough digits.

  • 1 3 - 12 - 234 should fail validation with an error messaging stating that there are not enough digits

Changelog Entry

Added - New feature
Changed - Existing functionality
Deprecated - Soon-to-be removed feature
Removed - Feature
Fixed - Bug fix
Security - Vulnerability

Credits

Props @username, @username2, ...

Checklist:

  • I agree to follow this project's Code of Conduct.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests pass.

@github-actions github-actions bot added this to the 1.7.0 milestone Jan 3, 2025
@MaxwellGarceau
Copy link
Collaborator Author

MaxwellGarceau commented Jan 4, 2025

@dkotter I think this ticket would be a good candidate for some PHP unit tests around the address validation. The changes are small, the only WP dependency is WP_Error, and some easy to run tests would help me feel better that we're covering the right validation conditions. What do you think?

Assuming that you think it's worth adding the tests, I wanted to ask if 10up had a preferred testing library or "10up way" of unit testing? There's currently no PHP tests in this project.

My preference would be to use Pest with WP_Mock or Brain Monkey, but I'm open to anything.

@MaxwellGarceau MaxwellGarceau marked this pull request as ready for review January 4, 2025 00:40
@MaxwellGarceau MaxwellGarceau requested a review from dkotter January 4, 2025 00:40
@github-actions github-actions bot added the needs:feedback This requires reporter feedback to better understand the request. label Jan 4, 2025
Copy link

github-actions bot commented Jan 4, 2025

@MaxwellGarceau thanks for the PR! Could you please fill out the PR template with description, changelog, and credits information so that we can properly review and merge this?

@github-actions github-actions bot added needs:code-review This requires code review. and removed needs:feedback This requires reporter feedback to better understand the request. labels Jan 4, 2025
}

// Trim whitespace
$opt_val = array_map( 'trim', $opt_val ); // Beginning and end
$opt_val = array_map( fn( $s ) => preg_replace( '/\s/', '', $s ), $opt_val ); // Middle
Copy link
Collaborator

Choose a reason for hiding this comment

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

Our minimum PHP version is still 7.0 and I believe arrow functions weren't introduced until 7.4, so we'll need to change this to use an anonymous function I believe

@dkotter
Copy link
Collaborator

dkotter commented Jan 6, 2025

Assuming that you think it's worth adding the tests, I wanted to ask if 10up had a preferred testing library or "10up way" of unit testing? There's currently no PHP tests in this project.

I'm fine with spending the time to add unit tests here. As far as a preferred way, we don't have a well documented approach, though we typically use PHPUnit (as this is what WordPress core itself uses) and either use WP_Mock or use the testing framework that WordPress provides, for more complicated setups.

I think PHPUnit + WP_Mock would probably work fine here. Can see how we do things on Safe SVG for one example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs:code-review This requires code review.
Projects
None yet
2 participants