Skip to content

Commit

Permalink
Merge pull request #124 from wmde/fix-postcode-validation
Browse files Browse the repository at this point in the history
Always validate postcode length
  • Loading branch information
gbirke authored Nov 28, 2023
2 parents 25ce5f7 + 7136e23 commit 99fc84b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/Validators/AddressValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,12 @@ public function validatePostalAddress( string $streetAddress, string $postalCode
$violations[] = $this->validateFieldLength( $streetAddress, self::SOURCE_STREET_ADDRESS );
}

if ( isset( $this->countriesPostcodePatterns[$countryCode] ) ) {
$violations[] = $this->validatePostalCode( $this->countriesPostcodePatterns[$countryCode], $postalCode );
} else {
$postalCodeLengthViolation = $this->validateFieldLength( $postalCode, self::SOURCE_POSTAL_CODE );
if ( $postalCodeLengthViolation === null ) {
$violations[] = $this->validatePostalCode( $this->addressPatterns['postcode'], $postalCode );
$violations[] = $postalCodeLengthViolation = $this->validateFieldLength( $postalCode, self::SOURCE_POSTAL_CODE );
if ( $postalCodeLengthViolation === null ) {
if ( isset( $this->countriesPostcodePatterns[$countryCode] ) ) {
$violations[] = $this->validatePostalCode( $this->countriesPostcodePatterns[$countryCode], $postalCode );
} else {
$violations[] = $postalCodeLengthViolation;
$violations[] = $this->validatePostalCode( $this->addressPatterns['postcode'], $postalCode );
}
}

Expand Down
35 changes: 35 additions & 0 deletions tests/Unit/Validators/AddressValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,41 @@ public function testGivenBadPostcodeForCountry_correctViolationsAreReturned(): v
$this->assertSame( 'postcode', $validationResult->getViolations()[0]->getSource() );
}

public function testGivenBadPostcodeForCountryWithoutPatterns_addressPatternIsUsedViolationsAreReturned(): void {
$addressPatterns = [
'firstName' => "/.+$/",
'lastName' => "/.+$/",
// Weird pattern to make numbers fail
'postcode' => '/^[bao]{5}$/',
];
$validator = new AddressValidator( [], $addressPatterns );
$validationResult = $validator->validatePostalAddress( 'Test 1234', '123', 'Test City', 'US' );
$this->assertSame( 'postcode', $validationResult->getViolations()[0]->getSource() );
}

public function testGivenLengthValidationFailsForPostCode_violationsContainOnlyLengthViolationsAndNoPatternViolations(): void {
$addressPatterns = [
'firstName' => "/.+$/",
'lastName' => "/.+$/",
'postcode' => '/^[0-9]{10}$/',
];
$countryPatterns = [
'DE' => '/^[0-9]{5}$/',
];
// has to be longer than maximum field length in AddressValidator
$longPostalCode = '1234567890123456789';
$validator = new AddressValidator( $countryPatterns, $addressPatterns );
$validationResultForUnknownCountry = $validator->validatePostalAddress( 'Test 1234', $longPostalCode, 'Test City', 'US' );
$validationResultForKnownCountry = $validator->validatePostalAddress( 'Test 1234', $longPostalCode, 'Test City', 'DE' );

$this->assertCount( 1, $validationResultForUnknownCountry->getViolations() );
$this->assertSame( 'postcode', $validationResultForUnknownCountry->getViolations()[0]->getSource() );
$this->assertSame( 'wrong-length', $validationResultForUnknownCountry->getViolations()[0]->getMessageIdentifier() );
$this->assertCount( 1, $validationResultForKnownCountry->getViolations() );
$this->assertSame( 'postcode', $validationResultForKnownCountry->getViolations()[0]->getSource() );
$this->assertSame( 'wrong-length', $validationResultForKnownCountry->getViolations()[0]->getMessageIdentifier() );
}

public function testGivenBadFirstAndLastName_correctViolationsAreReturned(): void {
$validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS, self::ADDRESS_PATTERNS );
$validationResult = $validator->validatePersonName( 'Herr', '', '£$%^&*()', '£$%^&*()' );
Expand Down

0 comments on commit 99fc84b

Please sign in to comment.