Skip to content

Commit 99fc84b

Browse files
authored
Merge pull request #124 from wmde/fix-postcode-validation
Always validate postcode length
2 parents 25ce5f7 + 7136e23 commit 99fc84b

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/Validators/AddressValidator.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,12 @@ public function validatePostalAddress( string $streetAddress, string $postalCode
6666
$violations[] = $this->validateFieldLength( $streetAddress, self::SOURCE_STREET_ADDRESS );
6767
}
6868

69-
if ( isset( $this->countriesPostcodePatterns[$countryCode] ) ) {
70-
$violations[] = $this->validatePostalCode( $this->countriesPostcodePatterns[$countryCode], $postalCode );
71-
} else {
72-
$postalCodeLengthViolation = $this->validateFieldLength( $postalCode, self::SOURCE_POSTAL_CODE );
73-
if ( $postalCodeLengthViolation === null ) {
74-
$violations[] = $this->validatePostalCode( $this->addressPatterns['postcode'], $postalCode );
69+
$violations[] = $postalCodeLengthViolation = $this->validateFieldLength( $postalCode, self::SOURCE_POSTAL_CODE );
70+
if ( $postalCodeLengthViolation === null ) {
71+
if ( isset( $this->countriesPostcodePatterns[$countryCode] ) ) {
72+
$violations[] = $this->validatePostalCode( $this->countriesPostcodePatterns[$countryCode], $postalCode );
7573
} else {
76-
$violations[] = $postalCodeLengthViolation;
74+
$violations[] = $this->validatePostalCode( $this->addressPatterns['postcode'], $postalCode );
7775
}
7876
}
7977

tests/Unit/Validators/AddressValidatorTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,41 @@ public function testGivenBadPostcodeForCountry_correctViolationsAreReturned(): v
135135
$this->assertSame( 'postcode', $validationResult->getViolations()[0]->getSource() );
136136
}
137137

138+
public function testGivenBadPostcodeForCountryWithoutPatterns_addressPatternIsUsedViolationsAreReturned(): void {
139+
$addressPatterns = [
140+
'firstName' => "/.+$/",
141+
'lastName' => "/.+$/",
142+
// Weird pattern to make numbers fail
143+
'postcode' => '/^[bao]{5}$/',
144+
];
145+
$validator = new AddressValidator( [], $addressPatterns );
146+
$validationResult = $validator->validatePostalAddress( 'Test 1234', '123', 'Test City', 'US' );
147+
$this->assertSame( 'postcode', $validationResult->getViolations()[0]->getSource() );
148+
}
149+
150+
public function testGivenLengthValidationFailsForPostCode_violationsContainOnlyLengthViolationsAndNoPatternViolations(): void {
151+
$addressPatterns = [
152+
'firstName' => "/.+$/",
153+
'lastName' => "/.+$/",
154+
'postcode' => '/^[0-9]{10}$/',
155+
];
156+
$countryPatterns = [
157+
'DE' => '/^[0-9]{5}$/',
158+
];
159+
// has to be longer than maximum field length in AddressValidator
160+
$longPostalCode = '1234567890123456789';
161+
$validator = new AddressValidator( $countryPatterns, $addressPatterns );
162+
$validationResultForUnknownCountry = $validator->validatePostalAddress( 'Test 1234', $longPostalCode, 'Test City', 'US' );
163+
$validationResultForKnownCountry = $validator->validatePostalAddress( 'Test 1234', $longPostalCode, 'Test City', 'DE' );
164+
165+
$this->assertCount( 1, $validationResultForUnknownCountry->getViolations() );
166+
$this->assertSame( 'postcode', $validationResultForUnknownCountry->getViolations()[0]->getSource() );
167+
$this->assertSame( 'wrong-length', $validationResultForUnknownCountry->getViolations()[0]->getMessageIdentifier() );
168+
$this->assertCount( 1, $validationResultForKnownCountry->getViolations() );
169+
$this->assertSame( 'postcode', $validationResultForKnownCountry->getViolations()[0]->getSource() );
170+
$this->assertSame( 'wrong-length', $validationResultForKnownCountry->getViolations()[0]->getMessageIdentifier() );
171+
}
172+
138173
public function testGivenBadFirstAndLastName_correctViolationsAreReturned(): void {
139174
$validator = new AddressValidator( self::COUNTRY_POSTCODE_PATTERNS, self::ADDRESS_PATTERNS );
140175
$validationResult = $validator->validatePersonName( 'Herr', '', '£$%^&*()', '£$%^&*()' );

0 commit comments

Comments
 (0)