From 8c6b569920638c4431dbfbe38f0af7f67d71b9e7 Mon Sep 17 00:00:00 2001 From: Naman Malhotra Date: Tue, 17 Aug 2021 21:24:33 +0530 Subject: [PATCH] Merge release/2.8.4 into trunk (#2696) (#2747) * Moved Stripe connection tests to methods that query currencies, making MC work, but only with the default currency until Stripe is connected. * Update changelogs, add tests for 2696. * Fix logic error in get_cached_currencies. * Additional test for Multi-Currency. --- changelog.txt | 5 ++++ includes/multi-currency/MultiCurrency.php | 29 +++++-------------- readme.txt | 5 ++++ .../test-class-multi-currency.php | 22 +++++++++++++- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/changelog.txt b/changelog.txt index d6c0d9c9bd2..86c962fb46a 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,5 +1,10 @@ *** WooCommerce Payments Changelog *** += 2.8.4 - 2021-xx-xx = +* Fix - Fix database connection error on account cache clear. +* Fix - Fix fatal error logged when updating analytics data when account is not connected to Stripe. +* Fix - Multi-Currency Compatibility fatal error with Subscriptions when account is not connected to Stripe. + = 2.8.3 - 2021-08-10 = * Fix - Fix for payment request buttons when the new payment methods gateway is enabled. diff --git a/includes/multi-currency/MultiCurrency.php b/includes/multi-currency/MultiCurrency.php index e8f6e6ec924..05304575362 100644 --- a/includes/multi-currency/MultiCurrency.php +++ b/includes/multi-currency/MultiCurrency.php @@ -201,10 +201,6 @@ public function __construct( WC_Payments_API_Client $payments_api_client, WC_Pay * @return void */ public function init() { - if ( ! $this->payments_account->is_stripe_connected() ) { - return; - } - $store_currency_updated = $this->check_store_currency_for_change(); // If the store currency has been updated, clear the cache to make sure we fetch fresh rates from the server. @@ -248,10 +244,6 @@ public function init() { * @return void */ public function init_rest_api() { - if ( ! $this->payments_account->is_stripe_connected() ) { - return; - } - $api_controller = new RestController( \WC_Payments::create_api_client() ); $api_controller->register_routes(); } @@ -262,10 +254,6 @@ public function init_rest_api() { * @return void */ public function init_widgets() { - if ( ! $this->payments_account->is_stripe_connected() ) { - return; - } - $this->currency_switcher_widget = new CurrencySwitcherWidget( $this, $this->compatibility ); register_widget( $this->currency_switcher_widget ); } @@ -334,8 +322,8 @@ public function get_cached_currencies() { return $cache_data; } - // If connection to server cannot be established, return expired data or null. - if ( ! $this->payments_api_client->is_server_connected() ) { + // If connection to server cannot be established, or if Stripe is not connected, return expired data or null. + if ( ! $this->payments_api_client->is_server_connected() || ! $this->payments_account->is_stripe_connected() ) { return $cache_data ?? null; } @@ -594,10 +582,6 @@ public function update_selected_currency( string $currency_code ) { * @return void */ public function update_selected_currency_by_url() { - if ( ! $this->payments_account->is_stripe_connected() ) { - return; - } - if ( ! isset( $_GET['currency'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification return; } @@ -611,10 +595,6 @@ public function update_selected_currency_by_url() { * @return void */ public function update_selected_currency_by_geolocation() { - if ( ! $this->payments_account->is_stripe_connected() ) { - return; - } - // We only want to automatically set the currency if it's already not set. if ( $this->is_using_auto_currency_switching() && ! $this->get_stored_currency_code() ) { $currency = $this->geolocation->get_currency_by_customer_location(); @@ -964,6 +944,11 @@ private function remove_currency_settings( $currency ) { * @return array Array with the available currencies' codes. */ private function get_account_available_currencies(): array { + // If Stripe is not connected, return an empty array. This prevents using MC without being connected to Stripe. + if ( ! $this->payments_account->is_stripe_connected() ) { + return []; + } + $wc_currencies = array_keys( get_woocommerce_currencies() ); $account_currencies = $wc_currencies; diff --git a/readme.txt b/readme.txt index 957b32c38e9..1abffdc376d 100644 --- a/readme.txt +++ b/readme.txt @@ -98,6 +98,11 @@ Please note that our support for the checkout block is still experimental and th == Changelog == += 2.8.4 - 2021-xx-xx = +* Fix - Align table items according to design correctly. +* Fix - Fix database connection error on account cache clear. +* Fix - Fix fatal error logged when updating analytics data when account is not connected to Stripe. + = 2.8.3 - 2021-08-10 = * Fix - Fix for payment request buttons when the new payment methods gateway is enabled. diff --git a/tests/unit/multi-currency/test-class-multi-currency.php b/tests/unit/multi-currency/test-class-multi-currency.php index 2f96f8b5521..0d2a8c65d30 100644 --- a/tests/unit/multi-currency/test-class-multi-currency.php +++ b/tests/unit/multi-currency/test-class-multi-currency.php @@ -309,6 +309,11 @@ public function test_get_selected_currency_returns_currency_from_user() { $this->assertSame( 'GBP', $this->multi_currency->get_selected_currency()->get_code() ); } + public function test_get_selected_currency_returns_default_currency_with_no_stripe_account() { + $this->init_multi_currency( null, false ); + $this->assertSame( get_woocommerce_currency(), $this->multi_currency->get_selected_currency()->get_code() ); + } + public function test_update_selected_currency_does_not_set_invalid_session_currency() { $this->multi_currency->update_selected_currency( 'UNSUPPORTED_CURRENCY' ); @@ -620,7 +625,6 @@ public function test_get_cached_currencies_handles_api_exception() { // Assert that the cache was correctly set with the error expiration time. $this->assertEquals( time() + MINUTE_IN_SECONDS, get_option( self::CURRENCY_RETRIEVAL_ERROR_OPTION ) ); - } public function test_storefront_integration_init_with_compatible_themes() { @@ -702,6 +706,22 @@ public function test_add_order_meta_on_refund() { $this->assertEquals( '0.724', $refund->get_meta( '_wcpay_multi_currency_stripe_exchange_rate', true ) ); } + public function test_get_cached_currencies_with_no_stripe_connection() { + $this->init_multi_currency( null, false ); + $this->assertEquals( + $this->mock_cached_currencies, + $this->multi_currency->get_cached_currencies() + ); + } + + public function test_get_available_currencies_returns_store_currency_with_no_stripe_connection() { + $expected = [ + 'USD' => new WCPay\MultiCurrency\Currency( 'USD', 1 ), + ]; + $this->init_multi_currency( null, false ); + $this->assertEquals( $expected, $this->multi_currency->get_available_currencies() ); + } + public function get_price_provider() { return [ [ '5.2499', '0.00', 5.2499 ],