Improve formatting for SE, LV, CZ, SK postcodes + Update tests (#45478)
* Add data providers for WC_Formatting_Functions_Test tests * Add tests for Denmark postcodes * Add formatting for Sweden postcodes * Add formatting for CZ, SK postcodes. Cleanup formatting for LV * Group wc_format_postcode tests by country * Add changefile * Match digits only in LV, CZ, SK postcode formatting * Update postcode validation for CZ, SK * Minor adjustment to regex --------- Co-authored-by: Jorge Torres <jorge.torres@automattic.com>
This commit is contained in:
parent
0d6b9770f4
commit
c0da722a93
|
@ -0,0 +1,4 @@
|
|||
Significance: minor
|
||||
Type: enhancement
|
||||
|
||||
Improve formatting for SE, LV, CZ, SK postcodes
|
|
@ -102,7 +102,7 @@ class WC_Validation {
|
|||
break;
|
||||
case 'CZ':
|
||||
case 'SK':
|
||||
$valid = (bool) preg_match( '/^([0-9]{3})(\s?)([0-9]{2})$/', $postcode );
|
||||
$valid = (bool) preg_match( "/^($country-)?([0-9]{3})(\s?)([0-9]{2})$/", $postcode );
|
||||
break;
|
||||
case 'NL':
|
||||
$valid = (bool) preg_match( '/^([1-9][0-9]{3})(\s?)(?!SA|SD|SS)[A-Z]{2}$/i', $postcode );
|
||||
|
|
|
@ -979,6 +979,9 @@ function wc_format_postcode( $postcode, $country ) {
|
|||
$postcode = wc_normalize_postcode( $postcode ?? '' );
|
||||
|
||||
switch ( $country ) {
|
||||
case 'SE':
|
||||
$postcode = substr_replace( $postcode, ' ', -2, 0 );
|
||||
break;
|
||||
case 'CA':
|
||||
case 'GB':
|
||||
$postcode = substr_replace( $postcode, ' ', -3, 0 );
|
||||
|
@ -1004,9 +1007,12 @@ function wc_format_postcode( $postcode, $country ) {
|
|||
$postcode = substr_replace( $postcode, ' ', 4, 0 );
|
||||
break;
|
||||
case 'LV':
|
||||
if ( preg_match( '/(?:LV)?-?(\d+)/i', $postcode, $matches ) ) {
|
||||
$postcode = count( $matches ) >= 2 ? "LV-$matches[1]" : $postcode;
|
||||
}
|
||||
$postcode = preg_replace( '/^(LV)?-?(\d+)$/', 'LV-${2}', $postcode );
|
||||
break;
|
||||
case 'CZ':
|
||||
case 'SK':
|
||||
$postcode = preg_replace( "/^({$country})-?(\d+)$/", '${1}-${2}', $postcode );
|
||||
$postcode = substr_replace( $postcode, ' ', -2, 0 );
|
||||
break;
|
||||
case 'DK':
|
||||
$postcode = preg_replace( '/^(DK)(.+)$/', '${1}-${2}', $postcode );
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
/**
|
||||
* Validation functions tests
|
||||
*
|
||||
* @package WooCommerce\Tests\Validation.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class WC_Validation_Test.
|
||||
*/
|
||||
class WC_Validation_Test extends \WC_Unit_Test_Case {
|
||||
/**
|
||||
* Data provider for test_is_postcode().
|
||||
*/
|
||||
public function data_provider_test_is_postcode(): array {
|
||||
$cz = array(
|
||||
array( true, '115 03', 'CZ' ),
|
||||
array( true, 'CZ-115 03', 'CZ' ),
|
||||
);
|
||||
|
||||
return array_merge( $cz );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test postcode validation.
|
||||
*
|
||||
* @dataProvider data_provider_test_is_postcode
|
||||
*
|
||||
* @param bool $expected Expected result.
|
||||
* @param string $postcode Postcode param for is_postcode.
|
||||
* @param string $country Country param for is_postcode.
|
||||
*/
|
||||
public function test_is_postcode( bool $expected, string $postcode, string $country ): void {
|
||||
$this->assertSame( $expected, WC_Validation::is_postcode( $postcode, $country ) );
|
||||
}
|
||||
}
|
|
@ -11,19 +11,80 @@
|
|||
class WC_Formatting_Functions_Test extends \WC_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Test wc_sanitize_coupon_code() function.
|
||||
* Data provider for test_wc_sanitize_coupon_code.
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public function test_wc_sanitize_coupon_code() {
|
||||
$this->assertEquals( 'DUMMYCOUPON', wc_sanitize_coupon_code( 'DUMMYCOUPON' ) );
|
||||
$this->assertEquals( 'a&a', wc_sanitize_coupon_code( 'a&a' ) );
|
||||
$this->assertEquals( "test's", wc_sanitize_coupon_code( "test's" ) );
|
||||
public function data_provider_test_wc_sanitize_coupon_code(): array {
|
||||
return array(
|
||||
array( 'DUMMYCOUPON', 'DUMMYCOUPON' ),
|
||||
array( 'a&a', 'a&a' ),
|
||||
array( "test's", "test's" ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_sanitize_coupon_code() function.
|
||||
*
|
||||
* @dataProvider data_provider_test_wc_sanitize_coupon_code
|
||||
*
|
||||
* @param string $assert Expected result.
|
||||
* @param string $input Input for wc_sanitize_coupon_code().
|
||||
*/
|
||||
public function test_wc_sanitize_coupon_code( string $assert, string $input ) {
|
||||
$this->assertSame( $assert, wc_sanitize_coupon_code( $input ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for test_wc_format_postcode.
|
||||
*
|
||||
* @return array[]
|
||||
* @see WC_Tests_Formatting_Functions::test_wc_format_postcode for US, GB, BR, JP, NL, LV
|
||||
*/
|
||||
public function data_provider_test_wc_format_postcode(): array {
|
||||
$ie = array(
|
||||
array( 'D02 AF30', 'D02AF30', 'IE' ),
|
||||
);
|
||||
|
||||
$pt = array(
|
||||
array( '1000-205', '1000205', 'PT' ),
|
||||
);
|
||||
|
||||
$dk = array(
|
||||
array( '1234', '1234', 'DK' ),
|
||||
array( 'DK-1234', 'DK-1234', 'DK' ),
|
||||
array( 'DK-1234', 'dk-1234', 'DK' ),
|
||||
);
|
||||
|
||||
$se = array(
|
||||
array( '113 52', '11352', 'SE' ),
|
||||
);
|
||||
|
||||
$sk = array(
|
||||
array( '811 02', '81102', 'SK' ),
|
||||
array( 'SK-811 02', 'SK-81102', 'SK' ),
|
||||
array( 'SK-811 02', 'sk-81102', 'SK' ),
|
||||
);
|
||||
|
||||
$cz = array(
|
||||
array( '115 03', '11503', 'CZ' ),
|
||||
array( 'CZ-115 03', 'CZ-11503', 'CZ' ),
|
||||
array( 'CZ-115 03', 'cz-11503', 'CZ' ),
|
||||
);
|
||||
|
||||
return array_merge( $ie, $pt, $dk, $se, $sk, $cz );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_format_postcode() function.
|
||||
*
|
||||
* @dataProvider data_provider_test_wc_format_postcode
|
||||
*
|
||||
* @param string $assert Expected result.
|
||||
* @param string $postcode Postcode input for wc_format_postcode().
|
||||
* @param string $country Country input for wc_format_postcode().
|
||||
*/
|
||||
public function test_wc_format_postcode() {
|
||||
$this->assertEquals( 'D02 AF30', wc_format_postcode( 'D02AF30', 'IE' ), 'Test formatting of IE postcodes.' );
|
||||
$this->assertEquals( '1000-205', wc_format_postcode( '1000205', 'PT' ), 'Test formatting of PT postcodes.' );
|
||||
public function test_wc_format_postcode( string $assert, string $postcode, string $country ) {
|
||||
$this->assertSame( $assert, wc_format_postcode( $postcode, $country ), "Test formatting of $postcode postcodes." );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue