Problem/Motivation
TckkValidator::validate() (src/TckkValidator.php) has three bugs that make it both accept invalid numbers and reject valid ones:
- Leading zero check never matches.
$tc_kimlik[0] === 0 compares a string with an integer, so it is always FALSE. Numbers starting with 0 are not rejected; 00000000000 passes.
- Wrong modulo for the 10th digit.
abs($even - $odd) % 10 gives the wrong result when 7 * (d1+d3+d5+d7+d9) - (d2+d4+d6+d8) is negative. The check digit must be the mathematical (non-negative) modulo.
- Non-digits are stripped before validation.
preg_replace('/[^0-9]/', '', ...) runs before the checks, so input such as 1000000014a6 validates, while the raw value (with the letter) is what gets stored.
Steps to reproduce
drush php:eval '$v = \Drupal::service("tckk_field.validator");
foreach (["10000000146", "00000000000", "19090909018", "19090909096", "1000000014a6"] as $n) {
echo $n, " ", var_export($v->validate($n), TRUE), "\n";
}'
| Input |
Current result |
Expected |
| 10000000146 |
valid |
valid |
| 00000000000 |
valid |
invalid (leading zero) |
| 19090909018 |
invalid |
valid (negative difference case) |
| 19090909096 |
valid |
invalid (negative difference case) |
| 1000000014a6 |
valid |
invalid (non-digit) |
Proposed resolution
- Reject anything that does not match
/^[1-9][0-9]{10}$/ instead of stripping characters.
- Compute the 10th digit as
((7 * $odd - $even) % 10 + 10) % 10.
- Add a unit test (
tests/src/Unit/TckkValidatorTest.php) covering the cases above.
Remaining tasks
- Patch / merge request
- Unit tests
- Review
User interface changes
None.
API changes
None. validate() keeps its signature; it only returns correct results.
Data model changes
None. Previously stored values containing non-digits or a leading zero would fail validation on the next save.
Comments
Comment #4
orkutmuratyilmaz