From 07f3d9dee6b1091f38e776e32474d5a07967d768 Mon Sep 17 00:00:00 2001 From: Nestor Soriano Date: Mon, 8 Feb 2021 15:47:00 +0100 Subject: [PATCH] Add unit tests for the WC_Checkout class. --- includes/class-wc-checkout.php | 2 +- tests/legacy/mockable-functions.php | 2 +- tests/php/includes/class-wc-checkout-test.php | 144 ++++++++++++++++++ 3 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 tests/php/includes/class-wc-checkout-test.php diff --git a/includes/class-wc-checkout.php b/includes/class-wc-checkout.php index 07666c2f63f..6d8ed8e5723 100644 --- a/includes/class-wc-checkout.php +++ b/includes/class-wc-checkout.php @@ -881,7 +881,7 @@ class WC_Checkout { * Checks if a given country is one which exists but we don't ship to. * * @param string $country ISO 3166-1 alpha-2 country code to check. - * @return true if the country exists AND we don't ship to it (not that if country doesn't exist will return false). + * @return true if the country exists AND we don't ship to it (note that if country doesn't exist will return false). */ private function is_country_we_dont_ship_to( $country ) { return WC()->countries->country_exists( $country ) && diff --git a/tests/legacy/mockable-functions.php b/tests/legacy/mockable-functions.php index 6d01727f569..17e6030972c 100644 --- a/tests/legacy/mockable-functions.php +++ b/tests/legacy/mockable-functions.php @@ -8,5 +8,5 @@ */ return array( - // 'get_option' + 'wc_get_shipping_method_count', ); diff --git a/tests/php/includes/class-wc-checkout-test.php b/tests/php/includes/class-wc-checkout-test.php new file mode 100644 index 00000000000..3146b324a22 --- /dev/null +++ b/tests/php/includes/class-wc-checkout-test.php @@ -0,0 +1,144 @@ +sut = new class() extends WC_Checkout { + public function validate_posted_data( &$data, &$errors ) { + return parent::validate_posted_data( $data, $errors ); + } + + public function validate_checkout( &$data, &$errors ) { + return parent::validate_checkout( $data, $errors ); + } + }; + // phpcs:enable Generic.CodeAnalysis, Squiz.Commenting + } + + /** + * @testdox 'validate_posted_data' adds errors for non-existing billing/shipping countries. + * + * @testWith [true, true] + * [false, false] + * + * @param bool $ship_to_different_address True to simulate shipping to a different address than the billing address. + * @param bool $expect_error_message_for_shipping_country True to expect an error to be generated for the shipping country. + */ + public function test_validate_posted_data_adds_error_for_non_existing_country( $ship_to_different_address, $expect_error_message_for_shipping_country ) { + $_POST = array( + 'billing_country' => 'XX', + 'shipping_country' => 'YY', + 'ship_to_different_address' => $ship_to_different_address, + ); + $data = $_POST; // phpcs:ignore WordPress.Security.NonceVerification.Missing + + $errors = new WP_Error(); + + $this->sut->validate_posted_data( $data, $errors ); + + $this->assertEquals( "'XX' is not a valid country code.", $errors->get_error_message( 'billing_country_validation' ) ); + $this->assertEquals( + $expect_error_message_for_shipping_country ? "'YY' is not a valid country code." : '', + $errors->get_error_message( 'shipping_country_validation' ) + ); + } + + /** + * @testdox 'validate_posted_data' doesn't add errors for existing billing/shipping countries. + * + * @testWith [true] + * [false] + * + * @param bool $ship_to_different_address True to simulate shipping to a different address than the billing address. + */ + public function test_validate_posted_data_does_not_add_error_for_existing_country( $ship_to_different_address ) { + $_POST = array( + 'billing_country' => 'ES', + 'shipping_country' => 'ES', + 'ship_to_different_address' => $ship_to_different_address, + ); + $data = $_POST; // phpcs:ignore WordPress.Security.NonceVerification.Missing + + $errors = new WP_Error(); + + $this->sut->validate_posted_data( $data, $errors ); + + $this->assertEmpty( $errors->get_error_message( 'billing_country_validation' ) ); + $this->assertEmpty( $errors->get_error_message( 'shipping_country_validation' ) ); + } + + /** + * @testdox 'validate_checkout' adds a "We don't ship to country X" error but only if the country exists. + * + * @testWith [ "XX", false ] + * [ "JP", true ] + * + * @param string $country The billing/shipping country. + * @param bool $expect_we_dont_ship_error True to expect a "We don't ship to X" error. + */ + public function test_validate_checkout_adds_we_dont_ship_error_only_if_country_exists( $country, $expect_we_dont_ship_error ) { + add_filter( + 'woocommerce_countries_allowed_countries', + function() { + return array( 'ES' ); + } + ); + + add_filter( + 'woocommerce_cart_needs_shipping', + function() { + return true; + } + ); + + add_filter( + 'wc_shipping_enabled', + function() { + return true; + } + ); + + FunctionsMockerHack::add_function_mocks( + array( + 'wc_get_shipping_method_count' => function( $include_legacy = false, $enabled_only = false ) { + return 1; + }, + ) + ); + + $_POST = array( + 'billing_country' => $country, + 'shipping_country' => $country, + 'ship_to_different_address' => false, + ); + $data = $_POST; // phpcs:ignore WordPress.Security.NonceVerification.Missing + + $errors = new WP_Error(); + + $this->sut->validate_checkout( $data, $errors ); + + $this->assertEquals( + $expect_we_dont_ship_error ? 'Unfortunately we do not ship to the JP. Please enter an alternative shipping address.' : '', + $errors->get_error_message( 'shipping' ) + ); + } +}