diff --git a/includes/payment-methods/class-wc-stripe-express-checkout-helper.php b/includes/payment-methods/class-wc-stripe-express-checkout-helper.php index a94d35098..fe8aa1272 100644 --- a/includes/payment-methods/class-wc-stripe-express-checkout-helper.php +++ b/includes/payment-methods/class-wc-stripe-express-checkout-helper.php @@ -345,9 +345,9 @@ public function get_normalized_postal_code( $postcode, $country ) { if ( strlen( $spaceless_postcode ) < 5 ) { // Always reintroduce the space so that Shipping Zones regex like 'N1 *' work to match N1 postcodes like N1 1AA, but don't match N10 postcodes like N10 1AA return $spaceless_postcode . ' ***'; - } else { - return $postcode; // 5 or more chars means it probably wasn't redacted and will likely validate unchanged. } + + return $postcode; // 5 or more chars means it probably wasn't redacted and will likely validate unchanged. } if ( 'CA' === $country ) { diff --git a/includes/payment-methods/class-wc-stripe-payment-request.php b/includes/payment-methods/class-wc-stripe-payment-request.php index 2fc27b7ef..ca1372d62 100644 --- a/includes/payment-methods/class-wc-stripe-payment-request.php +++ b/includes/payment-methods/class-wc-stripe-payment-request.php @@ -536,9 +536,9 @@ public function get_normalized_postal_code( $postcode, $country ) { if ( strlen( $spaceless_postcode ) < 5 ) { // Always reintroduce the space so that Shipping Zones regex like 'N1 *' work to match N1 postcodes like N1 1AA, but don't match N10 postcodes like N10 1AA return $spaceless_postcode . ' ***'; - } else { - return $postcode; // 5 or more chars means it probably wasn't redacted and will likely validate unchanged. } + + return $postcode; // 5 or more chars means it probably wasn't redacted and will likely validate unchanged. } if ( 'CA' === $country ) { diff --git a/tests/phpunit/test-wc-stripe-express-checkout-helper.php b/tests/phpunit/test-wc-stripe-express-checkout-helper.php index ebe10a7d4..b618aaa6f 100644 --- a/tests/phpunit/test-wc-stripe-express-checkout-helper.php +++ b/tests/phpunit/test-wc-stripe-express-checkout-helper.php @@ -72,7 +72,7 @@ public function test_hides_ece_if_cannot_compute_taxes() { if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) { define( 'WOOCOMMERCE_CHECKOUT', true ); } - $original_gateways = WC()->payment_gateways()->payment_gateways; + $original_gateways = WC()->payment_gateways()->payment_gateways; WC()->payment_gateways()->payment_gateways = [ 'stripe' => new WC_Gateway_Stripe(), ]; @@ -267,4 +267,48 @@ public function test_is_account_creation_possible() { update_option( 'woocommerce_enable_signup_from_checkout_for_subscriptions', 'yes' ); $this->assertTrue( $wc_stripe_ece_helper_mock2->is_account_creation_possible() ); } + + /** + * Test for `get_normalized_postal_code`. + * + * @param string $postal_code Postal code. + * @param string $country Country code. + * @param string $expected Expected normalized postal code. + * @return void + * @dataProvider provide_test_get_normalized_postal_code + */ + public function test_get_normalized_postal_code( $postal_code, $country, $expected ) { + $wc_stripe_ece_helper = new WC_Stripe_Express_Checkout_Helper(); + $this->assertEquals( $expected, $wc_stripe_ece_helper->get_normalized_postal_code( $postal_code, $country ) ); + } + + /** + * Provider for `test_get_normalized_postal_code`. + * + * @return array + */ + public function provide_test_get_normalized_postal_code() { + return [ + 'GB country' => [ + 'postal code' => 'SW1A 1AA', + 'country' => 'GB', + 'expected' => 'SW1A 1AA', + ], + 'GB country, redacted' => [ + 'postal code' => 'SW1A', + 'country' => 'GB', + 'expected' => 'SW1A ***', + ], + 'CA country' => [ + 'postal code' => 'K1A ', + 'country' => 'CA', + 'expected' => 'K1A***', + ], + 'US country' => [ + 'postal code' => '12345', + 'country' => 'US', + 'expected' => '12345', + ], + ]; + } }