-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: develop
Are you sure you want to change the base?
Conversation
@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 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? |
} | ||
|
||
// 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 |
There was a problem hiding this comment.
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
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. |
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
Issue #104 - Update phone validation regex to accept only numbers and correct amount
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
General
Phone number should not accept letters
Phone number should not accept letters - Bug is fixed
Test a valid phone number
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.
• 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 digitsChangelog Entry
Credits
Props @username, @username2, ...
Checklist: