bug: #3630848: TckkValidator accepts invalid and rejects valid T.C. Kimlik numbers

Closes #3630848

What this changes

TckkValidator::validate():

  • Requires the exact format /^[1-9][0-9]{10}$/D instead of stripping non-digits first. This rejects leading zeros (the old $tc_kimlik[0] === 0 check compared a string to an int and never fired) and any non-digit or whitespace characters, so the value that passes validation is the value that gets stored.
  • The D modifier is an addition to the proposed resolution: without it, $ also matches before a trailing newline, so "10000000146\n" would pass.

TckkValidator::sum():

  • Computes the 10th digit as ((7 * odd - even) % 10 + 10) % 10 instead of abs(7 * odd - even) % 10, which returned the wrong digit whenever the weighted difference was negative.

No API changes: validate(), sum() and on() keep their signatures and return values.

Tests

Adds tests/src/Unit/TckkValidatorTest.php with the five cases from the issue summary plus whitespace, trailing newline, wrong 11th digit, too short, too long and empty input. The test uses both docblock annotations and PHPUnit attributes so it runs on PHPUnit 9 through 11, matching core_version_requirement: ^8 || ^9 || ^10 || ^11.

The project has no .gitlab-ci.yml, so no pipeline runs on this MR. Verified locally on Drupal 11.4 / PHP 8.4 / PHPUnit 11.5:

vendor/bin/phpunit -c web/core web/modules/contrib/tckk_field
OK (11 tests, 11 assertions)

phpcs --standard=Drupal,DrupalPractice reports no issues on the changed files.

Steps to verify

drush php:eval '$v = \Drupal::service("tckk_field.validator"); foreach (["10000000146","00000000000","19090909018","19090909096","1000000014a6"] as $n) { echo $n, " => ", $v->validate($n) ? "valid" : "invalid", PHP_EOL; }'

Expected: 10000000146 and 19090909018 valid, the other three invalid.

Behavior change

Form input is not trimmed, so values entered with spaces or dashes are now rejected. Existing stored values with non-digits or a leading zero will fail validation the next time the entity is saved through the form, as noted in the issue summary.

Merge request reports

Loading
Loading