From 474494dcab55ff9baa77bc6be1bee4b92bae4132 Mon Sep 17 00:00:00 2001 From: Saggre Date: Tue, 1 Feb 2022 14:42:38 +0200 Subject: [PATCH 01/10] Add LV postcode formatting and tests --- plugins/woocommerce/includes/wc-formatting-functions.php | 5 +++++ .../tests/legacy/unit-tests/formatting/functions.php | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/plugins/woocommerce/includes/wc-formatting-functions.php b/plugins/woocommerce/includes/wc-formatting-functions.php index 2bb63a68b5d..ef0fbff2267 100644 --- a/plugins/woocommerce/includes/wc-formatting-functions.php +++ b/plugins/woocommerce/includes/wc-formatting-functions.php @@ -993,6 +993,11 @@ function wc_format_postcode( $postcode, $country ) { case 'NL': $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; + } + break; } return apply_filters( 'woocommerce_format_postcode', trim( $postcode ), $country ); diff --git a/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php b/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php index e613d9341ed..5c918148eea 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php @@ -801,6 +801,12 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case { // JP postcode. $this->assertEquals( '999-9999', wc_format_postcode( '9999999', 'JP' ) ); + // Test LV postcode without mandatory country code. + $this->assertEquals( 'LV-1337', wc_format_postcode( '1337', 'LV' ) ); + + // Test LV postcode with incorrect format (no dash). + $this->assertEquals( 'LV-1337', wc_format_postcode( 'lv1337', 'LV' ) ); + // Test empty NL postcode. $this->assertEquals( '', wc_format_postcode( '', 'NL' ) ); } From 5991311a33e47b52ec69135d05f87eb601966ad4 Mon Sep 17 00:00:00 2001 From: Saggre Date: Tue, 1 Feb 2022 15:14:02 +0200 Subject: [PATCH 02/10] Add wc_format_postcode tests for IE and PT postcodes --- .../tests/legacy/unit-tests/formatting/functions.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php b/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php index 5c918148eea..c6466f18f80 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php @@ -795,6 +795,12 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case { // UK postcode. $this->assertEquals( 'PCRN 1ZZ', wc_format_postcode( 'pcrn1zz', 'GB' ) ); + // IE postcode. + $this->assertEquals( 'D02 AF30', wc_format_postcode( 'D02AF30', 'IE' ) ); + + // PT postcode. + $this->assertEquals( '1000-205', wc_format_postcode( '1000205', 'PT' ) ); + // BR/PL postcode. $this->assertEquals( '99999-999', wc_format_postcode( '99999999', 'BR' ) ); From 3081fa593f45fa0b8dd3a24e41f193b7e8fe736c Mon Sep 17 00:00:00 2001 From: Saggre Date: Tue, 1 Feb 2022 15:44:48 +0200 Subject: [PATCH 03/10] Add wc_format_postcode tests to new test directory --- .../includes/wc-formatting-functions-test.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/plugins/woocommerce/tests/php/includes/wc-formatting-functions-test.php b/plugins/woocommerce/tests/php/includes/wc-formatting-functions-test.php index 93dfda3b314..9252e6e3d55 100644 --- a/plugins/woocommerce/tests/php/includes/wc-formatting-functions-test.php +++ b/plugins/woocommerce/tests/php/includes/wc-formatting-functions-test.php @@ -17,4 +17,33 @@ class WC_Formatting_Functions_Test extends \WC_Unit_Test_Case { $this->assertEquals( 'DUMMYCOUPON', wc_sanitize_coupon_code( 'DUMMYCOUPON' ) ); $this->assertEquals( 'a&a', wc_sanitize_coupon_code( 'a&a' ) ); } + + /** + * Test wc_format_postcode() function. + */ + public function test_wc_format_postcode() { + // Generic postcode. + $this->assertEquals( '02111', wc_format_postcode( ' 02111 ', 'US' ) ); + + // US 9-digit postcode. + $this->assertEquals( '02111-9999', wc_format_postcode( ' 021119999 ', 'US' ) ); + + // UK postcode. + $this->assertEquals( 'PCRN 1ZZ', wc_format_postcode( 'pcrn1zz', 'GB' ) ); + + // IE postcode. + $this->assertEquals( 'D02 AF30', wc_format_postcode( 'D02AF30', 'IE' ) ); + + // PT postcode. + $this->assertEquals( '1000-205', wc_format_postcode( '1000205', 'PT' ) ); + + // BR/PL postcode. + $this->assertEquals( '99999-999', wc_format_postcode( '99999999', 'BR' ) ); + + // JP postcode. + $this->assertEquals( '999-9999', wc_format_postcode( '9999999', 'JP' ) ); + + // Test empty NL postcode. + $this->assertEquals( '', wc_format_postcode( '', 'NL' ) ); + } } From ec82f8865cd74e68059b8a848ca2ffff690cbace Mon Sep 17 00:00:00 2001 From: Saggre Date: Tue, 1 Feb 2022 15:46:01 +0200 Subject: [PATCH 04/10] Restore legacy tests --- .../tests/legacy/unit-tests/formatting/functions.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php b/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php index c6466f18f80..5c918148eea 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php @@ -795,12 +795,6 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case { // UK postcode. $this->assertEquals( 'PCRN 1ZZ', wc_format_postcode( 'pcrn1zz', 'GB' ) ); - // IE postcode. - $this->assertEquals( 'D02 AF30', wc_format_postcode( 'D02AF30', 'IE' ) ); - - // PT postcode. - $this->assertEquals( '1000-205', wc_format_postcode( '1000205', 'PT' ) ); - // BR/PL postcode. $this->assertEquals( '99999-999', wc_format_postcode( '99999999', 'BR' ) ); From 80e8157c1205ddc5be609bb4a467ee14d0cc1779 Mon Sep 17 00:00:00 2001 From: Saggre Date: Tue, 1 Feb 2022 15:52:46 +0200 Subject: [PATCH 05/10] Move tests to new tests dir --- .../tests/legacy/unit-tests/formatting/functions.php | 6 ------ .../tests/php/includes/wc-formatting-functions-test.php | 6 ++++++ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php b/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php index 5c918148eea..e613d9341ed 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php @@ -801,12 +801,6 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case { // JP postcode. $this->assertEquals( '999-9999', wc_format_postcode( '9999999', 'JP' ) ); - // Test LV postcode without mandatory country code. - $this->assertEquals( 'LV-1337', wc_format_postcode( '1337', 'LV' ) ); - - // Test LV postcode with incorrect format (no dash). - $this->assertEquals( 'LV-1337', wc_format_postcode( 'lv1337', 'LV' ) ); - // Test empty NL postcode. $this->assertEquals( '', wc_format_postcode( '', 'NL' ) ); } diff --git a/plugins/woocommerce/tests/php/includes/wc-formatting-functions-test.php b/plugins/woocommerce/tests/php/includes/wc-formatting-functions-test.php index 9252e6e3d55..6670e65de0f 100644 --- a/plugins/woocommerce/tests/php/includes/wc-formatting-functions-test.php +++ b/plugins/woocommerce/tests/php/includes/wc-formatting-functions-test.php @@ -43,6 +43,12 @@ class WC_Formatting_Functions_Test extends \WC_Unit_Test_Case { // JP postcode. $this->assertEquals( '999-9999', wc_format_postcode( '9999999', 'JP' ) ); + // Test LV postcode without mandatory country code. + $this->assertEquals( 'LV-1337', wc_format_postcode( '1337', 'LV' ) ); + + // Test LV postcode with incorrect format (no dash). + $this->assertEquals( 'LV-1337', wc_format_postcode( 'lv1337', 'LV' ) ); + // Test empty NL postcode. $this->assertEquals( '', wc_format_postcode( '', 'NL' ) ); } From fab094dcbe0355167c1efdf3e8a45c7cc8e7b51d Mon Sep 17 00:00:00 2001 From: Saggre Date: Tue, 1 Feb 2022 15:14:02 +0200 Subject: [PATCH 06/10] Add wc_format_postcode tests for IE and PT postcodes --- .../tests/legacy/unit-tests/formatting/functions.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php b/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php index e613d9341ed..ce0cc6ccd74 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php @@ -795,6 +795,12 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case { // UK postcode. $this->assertEquals( 'PCRN 1ZZ', wc_format_postcode( 'pcrn1zz', 'GB' ) ); + // IE postcode. + $this->assertEquals( 'D02 AF30', wc_format_postcode( 'D02AF30', 'IE' ) ); + + // PT postcode. + $this->assertEquals( '1000-205', wc_format_postcode( '1000205', 'PT' ) ); + // BR/PL postcode. $this->assertEquals( '99999-999', wc_format_postcode( '99999999', 'BR' ) ); From 004bfeabf50c7278e225e0199e977c63fe698fbe Mon Sep 17 00:00:00 2001 From: Saggre Date: Tue, 1 Feb 2022 15:46:01 +0200 Subject: [PATCH 07/10] Restore legacy tests --- .../tests/legacy/unit-tests/formatting/functions.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php b/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php index ce0cc6ccd74..e613d9341ed 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php @@ -795,12 +795,6 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case { // UK postcode. $this->assertEquals( 'PCRN 1ZZ', wc_format_postcode( 'pcrn1zz', 'GB' ) ); - // IE postcode. - $this->assertEquals( 'D02 AF30', wc_format_postcode( 'D02AF30', 'IE' ) ); - - // PT postcode. - $this->assertEquals( '1000-205', wc_format_postcode( '1000205', 'PT' ) ); - // BR/PL postcode. $this->assertEquals( '99999-999', wc_format_postcode( '99999999', 'BR' ) ); From 46ae32161c9933fedb9c1d8054438a78ec59aac0 Mon Sep 17 00:00:00 2001 From: Saggre Date: Wed, 23 Mar 2022 10:41:13 +0200 Subject: [PATCH 08/10] Remove duplicate tests --- .../php/includes/wc-formatting-functions-test.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/plugins/woocommerce/tests/php/includes/wc-formatting-functions-test.php b/plugins/woocommerce/tests/php/includes/wc-formatting-functions-test.php index 6670e65de0f..545f30e0a35 100644 --- a/plugins/woocommerce/tests/php/includes/wc-formatting-functions-test.php +++ b/plugins/woocommerce/tests/php/includes/wc-formatting-functions-test.php @@ -22,15 +22,6 @@ class WC_Formatting_Functions_Test extends \WC_Unit_Test_Case { * Test wc_format_postcode() function. */ public function test_wc_format_postcode() { - // Generic postcode. - $this->assertEquals( '02111', wc_format_postcode( ' 02111 ', 'US' ) ); - - // US 9-digit postcode. - $this->assertEquals( '02111-9999', wc_format_postcode( ' 021119999 ', 'US' ) ); - - // UK postcode. - $this->assertEquals( 'PCRN 1ZZ', wc_format_postcode( 'pcrn1zz', 'GB' ) ); - // IE postcode. $this->assertEquals( 'D02 AF30', wc_format_postcode( 'D02AF30', 'IE' ) ); @@ -51,5 +42,6 @@ class WC_Formatting_Functions_Test extends \WC_Unit_Test_Case { // Test empty NL postcode. $this->assertEquals( '', wc_format_postcode( '', 'NL' ) ); + } } From 19d230af6336bb8582fd123d2ced7250c92c4ebd Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Wed, 27 Apr 2022 11:40:51 +0530 Subject: [PATCH 09/10] Merge latvian formatting test with other tests for DRY. --- .../unit-tests/formatting/functions.php | 6 +++++ .../includes/wc-formatting-functions-test.php | 27 ------------------- 2 files changed, 6 insertions(+), 27 deletions(-) diff --git a/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php b/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php index e613d9341ed..6496a0f903c 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/formatting/functions.php @@ -803,6 +803,12 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case { // Test empty NL postcode. $this->assertEquals( '', wc_format_postcode( '', 'NL' ) ); + + // Test LV postcode without mandatory country code. + $this->assertEquals( 'LV-1337', wc_format_postcode( '1337', 'LV' ) ); + + // Test LV postcode with incorrect format (no dash). + $this->assertEquals( 'LV-1337', wc_format_postcode( 'lv1337', 'LV' ) ); } /** diff --git a/plugins/woocommerce/tests/php/includes/wc-formatting-functions-test.php b/plugins/woocommerce/tests/php/includes/wc-formatting-functions-test.php index 545f30e0a35..93dfda3b314 100644 --- a/plugins/woocommerce/tests/php/includes/wc-formatting-functions-test.php +++ b/plugins/woocommerce/tests/php/includes/wc-formatting-functions-test.php @@ -17,31 +17,4 @@ class WC_Formatting_Functions_Test extends \WC_Unit_Test_Case { $this->assertEquals( 'DUMMYCOUPON', wc_sanitize_coupon_code( 'DUMMYCOUPON' ) ); $this->assertEquals( 'a&a', wc_sanitize_coupon_code( 'a&a' ) ); } - - /** - * Test wc_format_postcode() function. - */ - public function test_wc_format_postcode() { - // IE postcode. - $this->assertEquals( 'D02 AF30', wc_format_postcode( 'D02AF30', 'IE' ) ); - - // PT postcode. - $this->assertEquals( '1000-205', wc_format_postcode( '1000205', 'PT' ) ); - - // BR/PL postcode. - $this->assertEquals( '99999-999', wc_format_postcode( '99999999', 'BR' ) ); - - // JP postcode. - $this->assertEquals( '999-9999', wc_format_postcode( '9999999', 'JP' ) ); - - // Test LV postcode without mandatory country code. - $this->assertEquals( 'LV-1337', wc_format_postcode( '1337', 'LV' ) ); - - // Test LV postcode with incorrect format (no dash). - $this->assertEquals( 'LV-1337', wc_format_postcode( 'lv1337', 'LV' ) ); - - // Test empty NL postcode. - $this->assertEquals( '', wc_format_postcode( '', 'NL' ) ); - - } } From b0748973a498cfe1f72629ef6138ecdb25cc8034 Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Wed, 27 Apr 2022 11:47:25 +0530 Subject: [PATCH 10/10] Add changelog. --- plugins/woocommerce/changelog/pr-31788 | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 plugins/woocommerce/changelog/pr-31788 diff --git a/plugins/woocommerce/changelog/pr-31788 b/plugins/woocommerce/changelog/pr-31788 new file mode 100644 index 00000000000..1b9686bc79d --- /dev/null +++ b/plugins/woocommerce/changelog/pr-31788 @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Add formatting rules for Latvian postcodes.