diff --git a/changelog/fix-backwards-compat-mccy-instance b/changelog/fix-backwards-compat-mccy-instance
new file mode 100644
index 00000000000..95a0701ac1c
--- /dev/null
+++ b/changelog/fix-backwards-compat-mccy-instance
@@ -0,0 +1,4 @@
+Significance: minor
+Type: fix
+
+Provide backwards-compatible method for retrieving the multi-currency instance.
diff --git a/changelog/multi-currency-v2 b/changelog/multi-currency-v2
new file mode 100644
index 00000000000..463f61c882e
--- /dev/null
+++ b/changelog/multi-currency-v2
@@ -0,0 +1,4 @@
+Significance: minor
+Type: update
+
+Decoupled Multi-currency module from gateway dependencies.
diff --git a/client/additional-methods-setup/upe-preview-methods-selector/add-payment-methods-task.js b/client/additional-methods-setup/upe-preview-methods-selector/add-payment-methods-task.js
index 76a2f3baa71..fbc23b63b67 100644
--- a/client/additional-methods-setup/upe-preview-methods-selector/add-payment-methods-task.js
+++ b/client/additional-methods-setup/upe-preview-methods-selector/add-payment-methods-task.js
@@ -36,7 +36,7 @@ import PaymentMethodCheckbox from '../../components/payment-methods-checkboxes/p
import { LoadableBlock } from '../../components/loadable';
import LoadableSettingsSection from '../../settings/loadable-settings-section';
import CurrencyInformationForMethods from './currency-information-for-methods';
-import { getMissingCurrenciesTooltipMessage } from 'wcpay/multi-currency/missing-currencies-message';
+import { getMissingCurrenciesTooltipMessage } from 'multi-currency/interface/functions';
import { upeCapabilityStatuses, upeMethods } from '../constants';
import paymentMethodsMap from '../../payment-methods-map';
import ConfirmPaymentMethodActivationModal from 'wcpay/settings/payment-methods-list/activation-modal';
diff --git a/client/additional-methods-setup/upe-preview-methods-selector/currency-information-for-methods.js b/client/additional-methods-setup/upe-preview-methods-selector/currency-information-for-methods.js
index f4e1fc02cbc..9639a729c8b 100644
--- a/client/additional-methods-setup/upe-preview-methods-selector/currency-information-for-methods.js
+++ b/client/additional-methods-setup/upe-preview-methods-selector/currency-information-for-methods.js
@@ -9,11 +9,11 @@ import interpolateComponents from '@automattic/interpolate-components';
/**
* Internal dependencies
*/
+import { useAccountDomesticCurrency } from '../../data';
import {
- useAccountDomesticCurrency,
useCurrencies,
useEnabledCurrencies,
-} from '../../data';
+} from 'multi-currency/interface/data';
import WCPaySettingsContext from '../../settings/wcpay-settings-context';
import InlineNotice from 'components/inline-notice';
import PaymentMethodsMap from '../../payment-methods-map';
diff --git a/client/additional-methods-setup/upe-preview-methods-selector/test/add-payment-methods-task.test.js b/client/additional-methods-setup/upe-preview-methods-selector/test/add-payment-methods-task.test.js
index 3339e812975..3a12347327f 100644
--- a/client/additional-methods-setup/upe-preview-methods-selector/test/add-payment-methods-task.test.js
+++ b/client/additional-methods-setup/upe-preview-methods-selector/test/add-payment-methods-task.test.js
@@ -22,11 +22,14 @@ import {
useEnabledPaymentMethodIds,
useGetPaymentMethodStatuses,
useSettings,
- useCurrencies,
- useEnabledCurrencies,
useManualCapture,
useAccountDomesticCurrency,
} from '../../../data';
+import {
+ useCurrencies,
+ useEnabledCurrencies,
+} from 'multi-currency/interface/data';
+
import WCPaySettingsContext from '../../../settings/wcpay-settings-context';
import { upeCapabilityStatuses } from 'wcpay/additional-methods-setup/constants';
@@ -34,13 +37,16 @@ jest.mock( '../../../data', () => ( {
useGetAvailablePaymentMethodIds: jest.fn(),
useEnabledPaymentMethodIds: jest.fn(),
useSettings: jest.fn(),
- useCurrencies: jest.fn(),
- useEnabledCurrencies: jest.fn(),
useGetPaymentMethodStatuses: jest.fn(),
useManualCapture: jest.fn(),
useAccountDomesticCurrency: jest.fn(),
} ) );
+jest.mock( 'multi-currency/interface/data', () => ( {
+ useCurrencies: jest.fn(),
+ useEnabledCurrencies: jest.fn(),
+} ) );
+
jest.mock( '@wordpress/a11y', () => ( {
...jest.requireActual( '@wordpress/a11y' ),
speak: jest.fn(),
diff --git a/client/additional-methods-setup/upe-preview-methods-selector/test/currency-information-for-methods.test.js b/client/additional-methods-setup/upe-preview-methods-selector/test/currency-information-for-methods.test.js
index fd473249eb7..cc2abefe96d 100644
--- a/client/additional-methods-setup/upe-preview-methods-selector/test/currency-information-for-methods.test.js
+++ b/client/additional-methods-setup/upe-preview-methods-selector/test/currency-information-for-methods.test.js
@@ -7,18 +7,21 @@ import { render, screen } from '@testing-library/react';
/**
* Internal dependencies
*/
+import { useAccountDomesticCurrency } from '../../../data';
import {
useCurrencies,
useEnabledCurrencies,
- useAccountDomesticCurrency,
-} from 'wcpay/data';
+} from 'multi-currency/interface/data';
import CurrencyInformationForMethods from '../currency-information-for-methods';
import WCPaySettingsContext from '../../../settings/wcpay-settings-context';
-jest.mock( 'wcpay/data', () => ( {
+jest.mock( '../../../data', () => ( {
+ useAccountDomesticCurrency: jest.fn(),
+} ) );
+
+jest.mock( 'multi-currency/interface/data', () => ( {
useCurrencies: jest.fn(),
useEnabledCurrencies: jest.fn(),
- useAccountDomesticCurrency: jest.fn(),
} ) );
jest.mock( '@wordpress/a11y', () => ( {
diff --git a/client/additional-methods-setup/wizard/task-item.scss b/client/additional-methods-setup/wizard/task-item.scss
index c2b7dbb5ec6..ae24c06ac08 100644
--- a/client/additional-methods-setup/wizard/task-item.scss
+++ b/client/additional-methods-setup/wizard/task-item.scss
@@ -117,6 +117,22 @@
}
}
+ &__visible-description-element {
+ position: absolute;
+ margin-left: 40px;
+ margin-top: 0;
+ margin-bottom: 1em;
+
+ &.is-muted-color {
+ color: $gray-700;
+ }
+
+ .components-external-link svg {
+ width: 1em;
+ height: 1em;
+ }
+ }
+
.add-payment-methods-task {
&__payment-selector {
&-wrapper {
diff --git a/client/additional-methods-setup/wizard/task-item.tsx b/client/additional-methods-setup/wizard/task-item.tsx
index f1c7ba4177e..a0ec6169bae 100644
--- a/client/additional-methods-setup/wizard/task-item.tsx
+++ b/client/additional-methods-setup/wizard/task-item.tsx
@@ -16,6 +16,7 @@ interface WizardTaskItemProps {
title: string;
index: number;
className?: string;
+ visibleDescription?: string;
}
const WizardTaskItem: React.FC< WizardTaskItemProps > = ( {
@@ -23,6 +24,7 @@ const WizardTaskItem: React.FC< WizardTaskItemProps > = ( {
title,
index,
className,
+ visibleDescription,
} ) => {
const { isCompleted, isActive } = useContext( WizardTaskContext );
@@ -51,6 +53,16 @@ const WizardTaskItem: React.FC< WizardTaskItemProps > = ( {
{ title }
+ { visibleDescription && ! isActive && (
+
+ { visibleDescription }
+
+ ) }
';
- // No need to escape here as the function called handles it.
+ // No need to escape here as the contents of $message is already escaped.
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
- echo \WC_Payments_Utils::esc_interpolated_html(
- $message,
- [
- 'a' => '',
- ]
- );
+ echo $message;
echo ' ' . esc_html__( 'Dismiss', 'woocommerce-payments' ) . '
';
}
@@ -1098,14 +991,14 @@ public function set_new_customer_currency_meta( $customer_id ) {
*
* @return void
*/
- public static function add_woo_admin_notes() {
+ public function add_woo_admin_notes() {
// Do not try to add notes on ajax requests to improve their performance.
if ( wp_doing_ajax() ) {
return;
}
if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '4.4.0', '>=' ) ) {
- NoteMultiCurrencyAvailable::set_account( WC_Payments::get_account_service() );
+ NoteMultiCurrencyAvailable::set_account( $this->payments_account );
NoteMultiCurrencyAvailable::possibly_add_note();
}
}
@@ -1122,284 +1015,130 @@ public static function remove_woo_admin_notes() {
}
/**
- * Gets the price after adjusting it with the rounding and charm settings.
- *
- * @param float $price The price to be adjusted.
- * @param bool $apply_charm_pricing Whether charm pricing should be applied.
- * @param Currency $currency The currency to be used when adjusting.
+ * Checks if the merchant has enabled automatic currency switching and geolocation.
*
- * @return float The adjusted price.
+ * @return bool
*/
- protected function get_adjusted_price( $price, $apply_charm_pricing, $currency ): float {
- $price = $this->ceil_price( $price, (float) $currency->get_rounding() );
-
- if ( $apply_charm_pricing ) {
- $price += (float) $currency->get_charm();
- }
-
- // Do not return negative prices (possible because of $currency->get_charm()).
- return max( 0, $price );
+ public function is_using_auto_currency_switching(): bool {
+ return 'yes' === get_option( $this->id . '_enable_auto_currency', 'no' );
}
/**
- * Ceils the price to the next number based on the rounding value.
- *
- * @param float $price The price to be ceiled.
- * @param float $rounding The rounding option.
+ * Checks if the merchant has enabled the currency switcher widget.
*
- * @return float The ceiled price.
+ * @return bool
*/
- protected function ceil_price( float $price, float $rounding ): float {
- if ( 0.00 === $rounding ) {
- return $price;
- }
- return ceil( $price / $rounding ) * $rounding;
+ public function is_using_storefront_switcher(): bool {
+ return 'yes' === get_option( $this->id . '_enable_storefront_switcher', 'no' );
}
/**
- * Returns the currency code stored for the user or in the session.
+ * Gets the store settings.
*
- * @return string|null Currency code.
+ * @return array The store settings.
*/
- private function get_stored_currency_code() {
- $user_id = get_current_user_id();
-
- if ( $user_id ) {
- return get_user_meta( $user_id, self::CURRENCY_META_KEY, true );
- }
-
- WC()->initialize_session();
- $currency_code = WC()->session->get( self::CURRENCY_SESSION_KEY );
-
- return is_string( $currency_code ) ? $currency_code : null;
+ public function get_settings() {
+ return [
+ $this->id . '_enable_auto_currency' => $this->is_using_auto_currency_switching(),
+ $this->id . '_enable_storefront_switcher' => $this->is_using_storefront_switcher(),
+ 'site_theme' => wp_get_theme()->get( 'Name' ),
+ 'date_format' => esc_attr( get_option( 'date_format', 'F j, Y' ) ),
+ 'time_format' => esc_attr( get_option( 'time_format', 'g:i a' ) ),
+ 'store_url' => esc_attr( get_page_uri( wc_get_page_id( 'shop' ) ) ),
+ ];
}
/**
- * Checks to see if the store currency has changed. If it has, this will
- * also update the option containing the store currency.
+ * Updates the store settings
*
- * @return bool
+ * @param array $params Update requested values.
+ *
+ * @return void
*/
- private function check_store_currency_for_change(): bool {
- $last_known_currency = get_option( $this->id . '_store_currency', false );
- $woocommerce_currency = get_woocommerce_currency();
-
- // If the last known currency was not set, update the option to set it and return false.
- if ( ! $last_known_currency ) {
- update_option( $this->id . '_store_currency', $woocommerce_currency );
- return false;
- }
+ public function update_settings( $params ) {
+ $updateable_options = [
+ 'wcpay_multi_currency_enable_auto_currency',
+ 'wcpay_multi_currency_enable_storefront_switcher',
+ ];
- if ( $last_known_currency !== $woocommerce_currency ) {
- update_option( $this->id . '_store_currency', $woocommerce_currency );
- return true;
+ foreach ( $updateable_options as $key ) {
+ if ( isset( $params[ $key ] ) ) {
+ update_option( $key, sanitize_text_field( $params[ $key ] ) );
+ }
}
-
- return false;
}
/**
- * Called when the store currency has changed. Puts any manual rate currencies into an option for a notice to display.
+ * Apply client order currency format and reduces the rounding precision to 2.
*
- * @return void
+ * @return void
*/
- private function update_manual_rate_currencies_notice_option() {
- $enabled_currencies = $this->get_enabled_currencies();
- $manual_currencies = [];
-
- // Check enabled currencies for manual rates.
- foreach ( $enabled_currencies as $currency ) {
- $rate_type = get_option( $this->id . '_exchange_rate_' . $currency->get_id(), false );
- if ( 'manual' === $rate_type ) {
- $manual_currencies[] = $currency->get_name();
+ public function set_client_format_and_rounding_precision() {
+ $screen = get_current_screen();
+ if ( in_array( $screen->id, [ 'shop_order', 'woocommerce_page_wc-orders' ], true ) ) :
+ $order = wc_get_order();
+ if ( ! $order ) {
+ return;
}
- }
+ $currency = $order->get_currency();
+ $currency_format_num_decimals = $this->backend_currencies->get_price_decimals( $currency );
+ $currency_format_decimal_sep = $this->backend_currencies->get_price_decimal_separator( $currency );
+ $currency_format_thousand_sep = $this->backend_currencies->get_price_thousand_separator( $currency );
+ $currency_format = str_replace( [ '%1$s', '%2$s', ' ' ], [ '%s', '%v', ' ' ], $this->backend_currencies->get_woocommerce_price_format( $currency ) );
- if ( 0 < count( $manual_currencies ) ) {
- update_option( $this->id . '_show_store_currency_changed_notice', $manual_currencies );
- }
+ $rounding_precision = wc_get_price_decimals() ?? wc_get_rounding_precision();
+ ?>
+
+ remove_currency_settings( $currency );
- }
+ public function register_script_with_dependencies( string $handler, string $script, array $additional_dependencies = [] ) {
+ $script_file = $script . '.js';
+ $script_src_url = plugins_url( $script_file, $this->settings_service->get_plugin_file_path() );
+ $script_asset_path = plugin_dir_path( $this->settings_service->get_plugin_file_path() ) . $script . '.asset.php';
+ $script_asset = file_exists( $script_asset_path ) ? require $script_asset_path : [ 'dependencies' => [] ]; // nosemgrep: audit.php.lang.security.file.inclusion-arg -- server generated path is used.
+ $all_dependencies = array_merge( $script_asset['dependencies'], $additional_dependencies );
+
+ wp_register_script(
+ $handler,
+ $script_src_url,
+ $all_dependencies,
+ $this->get_file_version( $script_file ),
+ true
+ );
}
/**
- * Will remove a currency's settings if it is not enabled.
+ * Get the file modified time as a cache buster if we're in dev mode.
*
- * @param mixed $currency Currency object or 3 letter currency code.
+ * @param string $file Local path to the file.
*
- * @return void
+ * @return string
*/
- private function remove_currency_settings( $currency ) {
- $code = is_a( $currency, Currency::class ) ? $currency->get_code() : strtoupper( $currency );
+ public function get_file_version( $file ) {
+ $plugin_path = plugin_dir_path( $this->settings_service->get_plugin_file_path() );
- // Bail if the currency code passed is not 3 characters, or if the currency is presently enabled.
- if ( 3 !== strlen( $code ) || isset( $this->get_enabled_currencies()[ $code ] ) ) {
- return;
- }
-
- $settings = [
- 'price_charm',
- 'price_rounding',
- 'manual_rate',
- 'exchange_rate',
- ];
-
- // Go through each setting and remove them.
- foreach ( $settings as $setting ) {
- delete_option( $this->id . '_' . $setting . '_' . strtolower( $code ) );
- }
- }
-
- /**
- * Returns the currencies enabled for the Stripe account that are
- * also available in WC.
- *
- * Can be filtered with the 'wcpay_multi_currency_available_currencies' hook.
- *
- * @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;
-
- $account = $this->payments_account->get_cached_account_data();
- $supported_currencies = $this->payments_account->get_account_customer_supported_currencies();
- if ( $account && ! empty( $supported_currencies ) ) {
- $account_currencies = array_map( 'strtoupper', $supported_currencies );
- }
-
- /**
- * Filter the available currencies for WooCommerce Multi-Currency.
- *
- * This filter can be used to modify the currencies available for WC Pay
- * Multi-Currency. Currencies have to be added in uppercase and should
- * also be available in `get_woocommerce_currencies` for them to work.
- *
- * @since 2.8.0
- *
- * @param array $available_currencies Current available currencies. Calculated based on
- * WC Pay's account currencies and WC currencies.
- */
- return apply_filters( self::FILTER_PREFIX . 'available_currencies', array_intersect( $account_currencies, $wc_currencies ) );
- }
-
- /**
- * Checks if the merchant has enabled automatic currency switching and geolocation.
- *
- * @return bool
- */
- public function is_using_auto_currency_switching(): bool {
- return 'yes' === get_option( $this->id . '_enable_auto_currency', 'no' );
- }
-
- /**
- * Checks if the merchant has enabled the currency switcher widget.
- *
- * @return bool
- */
- public function is_using_storefront_switcher(): bool {
- return 'yes' === get_option( $this->id . '_enable_storefront_switcher', 'no' );
- }
-
- /**
- * Gets the store settings.
- *
- * @return array The store settings.
- */
- public function get_settings() {
- return [
- $this->id . '_enable_auto_currency' => $this->is_using_auto_currency_switching(),
- $this->id . '_enable_storefront_switcher' => $this->is_using_storefront_switcher(),
- 'site_theme' => wp_get_theme()->get( 'Name' ),
- 'date_format' => esc_attr( get_option( 'date_format', 'F j, Y' ) ),
- 'time_format' => esc_attr( get_option( 'time_format', 'g:i a' ) ),
- 'store_url' => esc_attr( get_page_uri( wc_get_page_id( 'shop' ) ) ),
- ];
- }
-
- /**
- * Updates the store settings
- *
- * @param array $params Update requested values.
- *
- * @return void
- */
- public function update_settings( $params ) {
- $updateable_options = [
- 'wcpay_multi_currency_enable_auto_currency',
- 'wcpay_multi_currency_enable_storefront_switcher',
- ];
-
- foreach ( $updateable_options as $key ) {
- if ( isset( $params[ $key ] ) ) {
- update_option( $key, sanitize_text_field( $params[ $key ] ) );
- }
+ if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $plugin_path . $file ) ) {
+ return (string) filemtime( $plugin_path . trim( $file, '/' ) );
}
- }
-
- /**
- * Apply client order currency format and reduces the rounding precision to 2.
- *
- * @return void
- */
- public function set_client_format_and_rounding_precision() {
- $screen = get_current_screen();
- if ( in_array( $screen->id, [ 'shop_order', 'woocommerce_page_wc-orders' ], true ) ) :
- $order = wc_get_order();
- if ( ! $order ) {
- return;
- }
- $currency = $order->get_currency();
- $currency_format_num_decimals = $this->backend_currencies->get_price_decimals( $currency );
- $currency_format_decimal_sep = $this->backend_currencies->get_price_decimal_separator( $currency );
- $currency_format_thousand_sep = $this->backend_currencies->get_price_thousand_separator( $currency );
- $currency_format = str_replace( [ '%1$s', '%2$s', ' ' ], [ '%s', '%v', ' ' ], $this->backend_currencies->get_woocommerce_price_format( $currency ) );
-
- $rounding_precision = wc_get_price_decimals() ?? wc_get_rounding_precision();
- ?>
-
- settings_service->get_plugin_version();
}
/**
@@ -1431,52 +1170,6 @@ public function possible_simulation_activation() {
$this->simulate_client_currency();
}
- /**
- * Enables simulation of client browser currency.
- *
- * @return void
- */
- private function simulate_client_currency() {
- if ( ! $this->simulation_params['enable_auto_currency'] ) {
- return;
- }
-
- $countries = WC_Payments_Utils::supported_countries();
-
- $predefined_simulation_currencies = [
- Currency_Code::UNITED_STATES_DOLLAR => $countries[ Country_Code::UNITED_STATES ],
- Currency_Code::POUND_STERLING => $countries[ Country_Code::UNITED_KINGDOM ],
- ];
-
- $simulation_currency = Currency_Code::UNITED_STATES_DOLLAR === get_option( 'woocommerce_currency', Currency_Code::UNITED_STATES_DOLLAR ) ? Currency_Code::POUND_STERLING : Currency_Code::UNITED_STATES_DOLLAR;
- $simulation_currency_name = $this->available_currencies[ $simulation_currency ]->get_name();
- $simulation_country = $predefined_simulation_currencies[ $simulation_currency ];
-
- // Simulate client currency from geolocation.
- add_filter(
- 'wcpay_multi_currency_override_notice_currency_name',
- function ( $selected_currency_name ) use ( $simulation_currency_name ) {
- return $simulation_currency_name;
- }
- );
-
- // Simulate client country from geolocation.
- add_filter(
- 'wcpay_multi_currency_override_notice_country',
- function ( $selected_country ) use ( $simulation_country ) {
- return $simulation_country;
- }
- );
-
- // Always display the notice on simulation screen, prevent duplicate hooks.
- if ( ! has_action( 'wp_footer', [ $this, 'display_geolocation_currency_update_notice' ] ) ) {
- add_action( 'wp_footer', [ $this, 'display_geolocation_currency_update_notice' ] );
- }
-
- // Skip recalculating the cart to prevent infinite loop in simulation.
- remove_action( 'wp_loaded', [ $this, 'recalculate_cart' ] );
- }
-
/**
* Returns whether the simulation querystring param is set and active
*
@@ -1538,45 +1231,6 @@ public function get_multi_currency_onboarding_simulation_variables() {
return $values;
}
- /**
- * Adds the required querystring parameters to all urls in preview pages.
- *
- * @return void
- */
- private function add_simulation_params_to_preview_urls() {
- $params = $this->simulation_params;
- add_filter(
- 'wp_footer',
- function () use ( $params ) {
- ?>
-
- ceil_price( $price, (float) $currency->get_rounding() );
+
+ if ( $apply_charm_pricing ) {
+ $price += (float) $currency->get_charm();
+ }
+
+ // Do not return negative prices (possible because of $currency->get_charm()).
+ return max( 0, $price );
+ }
+
+ /**
+ * Ceils the price to the next number based on the rounding value.
+ *
+ * @param float $price The price to be ceiled.
+ * @param float $rounding The rounding option.
+ *
+ * @return float The ceiled price.
+ */
+ protected function ceil_price( float $price, float $rounding ): float {
+ if ( 0.00 === $rounding ) {
+ return $price;
+ }
+ return ceil( $price / $rounding ) * $rounding;
+ }
+
+ /**
+ * Sets up the available currencies, which are alphabetical by name.
+ *
+ * @return void
+ */
+ private function initialize_available_currencies() {
+ // Add default store currency with a rate of 1.0.
+ $woocommerce_currency = get_woocommerce_currency();
+ $this->available_currencies[ $woocommerce_currency ] = new Currency( $this->localization_service, $woocommerce_currency, 1.0 );
+
+ $available_currencies = [];
+
+ $currencies = $this->get_account_available_currencies();
+ $cache_data = $this->get_cached_currencies();
+
+ foreach ( $currencies as $currency_code ) {
+ $currency_rate = $cache_data['currencies'][ $currency_code ] ?? 1.0;
+ $update_time = $cache_data['updated'] ?? null;
+ $new_currency = new Currency( $this->localization_service, $currency_code, $currency_rate, $update_time );
+
+ // Add this to our list of available currencies.
+ $available_currencies[ $new_currency->get_name() ] = $new_currency;
+ }
+
+ ksort( $available_currencies );
+
+ foreach ( $available_currencies as $currency ) {
+ $this->available_currencies[ $currency->get_code() ] = $currency;
+ }
+ }
+
+ /**
+ * Sets up the enabled currencies.
+ *
+ * @return void
+ */
+ private function initialize_enabled_currencies() {
+ $available_currencies = $this->get_available_currencies();
+ $enabled_currency_codes = get_option( $this->id . '_enabled_currencies', [] );
+ $enabled_currency_codes = is_array( $enabled_currency_codes ) ? $enabled_currency_codes : [];
+ $default_code = $this->get_default_currency()->get_code();
+ $default = [];
+ $enabled_currency_codes[] = $default_code;
+
+ // This allows to keep the alphabetical sorting by name.
+ $enabled_currencies = array_filter(
+ $available_currencies,
+ function ( $currency ) use ( $enabled_currency_codes ) {
+ return in_array( $currency->get_code(), $enabled_currency_codes, true );
+ }
+ );
+
+ $this->enabled_currencies = [];
+
+ foreach ( $enabled_currencies as $enabled_currency ) {
+ // Get the charm and rounding for each enabled currency and add the currencies to the object property.
+ $currency = clone $enabled_currency;
+ $charm = get_option( $this->id . '_price_charm_' . $currency->get_id(), 0.00 );
+ $rounding = get_option( $this->id . '_price_rounding_' . $currency->get_id(), $currency->get_is_zero_decimal() ? '100' : '1.00' );
+ $currency->set_charm( $charm );
+ $currency->set_rounding( $rounding );
+
+ // If the currency is set to be manual, set the rate to the stored manual rate.
+ $type = get_option( $this->id . '_exchange_rate_' . $currency->get_id(), 'automatic' );
+ if ( 'manual' === $type ) {
+ $manual_rate = get_option( $this->id . '_manual_rate_' . $currency->get_id(), $currency->get_rate() );
+ $currency->set_rate( $manual_rate );
+ }
+
+ $this->enabled_currencies[ $currency->get_code() ] = $currency;
+ }
+
+ // Set default currency to the top of the list.
+ $default[ $default_code ] = $this->enabled_currencies[ $default_code ];
+ unset( $this->enabled_currencies[ $default_code ] );
+ $this->enabled_currencies = array_merge( $default, $this->enabled_currencies );
+ }
+
+ /**
+ * Sets the default currency.
+ *
+ * @return void
+ */
+ private function set_default_currency() {
+ $available_currencies = $this->get_available_currencies();
+ $this->default_currency = $available_currencies[ get_woocommerce_currency() ] ?? null;
+ }
+
+ /**
+ * Returns the currency code stored for the user or in the session.
+ *
+ * @return string|null Currency code.
+ */
+ private function get_stored_currency_code() {
+ $user_id = get_current_user_id();
+
+ if ( $user_id ) {
+ return get_user_meta( $user_id, self::CURRENCY_META_KEY, true );
+ }
+
+ WC()->initialize_session();
+ $currency_code = WC()->session->get( self::CURRENCY_SESSION_KEY );
+
+ return is_string( $currency_code ) ? $currency_code : null;
+ }
+
+ /**
+ * Checks to see if the store currency has changed. If it has, this will
+ * also update the option containing the store currency.
+ *
+ * @return bool
+ */
+ private function check_store_currency_for_change(): bool {
+ $last_known_currency = get_option( $this->id . '_store_currency', false );
+ $woocommerce_currency = get_woocommerce_currency();
+
+ // If the last known currency was not set, update the option to set it and return false.
+ if ( ! $last_known_currency ) {
+ update_option( $this->id . '_store_currency', $woocommerce_currency );
+ return false;
+ }
+
+ if ( $last_known_currency !== $woocommerce_currency ) {
+ update_option( $this->id . '_store_currency', $woocommerce_currency );
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Called when the store currency has changed. Puts any manual rate currencies into an option for a notice to display.
+ *
+ * @return void
+ */
+ private function update_manual_rate_currencies_notice_option() {
+ $enabled_currencies = $this->get_enabled_currencies();
+ $manual_currencies = [];
+
+ // Check enabled currencies for manual rates.
+ foreach ( $enabled_currencies as $currency ) {
+ $rate_type = get_option( $this->id . '_exchange_rate_' . $currency->get_id(), false );
+ if ( 'manual' === $rate_type ) {
+ $manual_currencies[] = $currency->get_name();
+ }
+ }
+
+ if ( 0 < count( $manual_currencies ) ) {
+ update_option( $this->id . '_show_store_currency_changed_notice', $manual_currencies );
+ }
+ }
+
+ /**
+ * Accepts an array of currencies that should have their settings removed.
+ *
+ * @param array $currencies Array of Currency objects or 3 letter currency codes.
+ *
+ * @return void
+ */
+ private function remove_currencies_settings( array $currencies ) {
+
+ foreach ( $currencies as $currency ) {
+ $this->remove_currency_settings( $currency );
+ }
+ }
+
+ /**
+ * Will remove a currency's settings if it is not enabled.
+ *
+ * @param mixed $currency Currency object or 3 letter currency code.
+ *
+ * @return void
+ */
+ private function remove_currency_settings( $currency ) {
+ $code = is_a( $currency, Currency::class ) ? $currency->get_code() : strtoupper( $currency );
+
+ // Bail if the currency code passed is not 3 characters, or if the currency is presently enabled.
+ if ( 3 !== strlen( $code ) || isset( $this->get_enabled_currencies()[ $code ] ) ) {
+ return;
+ }
+
+ $settings = [
+ 'price_charm',
+ 'price_rounding',
+ 'manual_rate',
+ 'exchange_rate',
+ ];
+
+ // Go through each setting and remove them.
+ foreach ( $settings as $setting ) {
+ delete_option( $this->id . '_' . $setting . '_' . strtolower( $code ) );
+ }
+ }
+
+ /**
+ * Returns the currencies enabled for the payment provider account that are
+ * also available in WC.
+ *
+ * Can be filtered with the 'wcpay_multi_currency_available_currencies' hook.
+ *
+ * @return array Array with the available currencies' codes.
+ */
+ private function get_account_available_currencies(): array {
+ // If the payment provider is not connected, return an empty array. This prevents using MC without being connected to the payment provider.
+ if ( ! $this->payments_account->is_provider_connected() ) {
+ return [];
+ }
+
+ $wc_currencies = array_keys( get_woocommerce_currencies() );
+ $account_currencies = $wc_currencies;
+
+ $account = $this->payments_account->get_cached_account_data();
+ $supported_currencies = $this->payments_account->get_account_customer_supported_currencies();
+ if ( $account && ! empty( $supported_currencies ) ) {
+ $account_currencies = array_map( 'strtoupper', $supported_currencies );
+ }
+
+ /**
+ * Filter the available currencies for WooCommerce Multi-Currency.
+ *
+ * This filter can be used to modify the currencies available for WC Pay
+ * Multi-Currency. Currencies have to be added in uppercase and should
+ * also be available in `get_woocommerce_currencies` for them to work.
+ *
+ * @since 2.8.0
+ *
+ * @param array $available_currencies Current available currencies. Calculated based on
+ * WC Pay's account currencies and WC currencies.
+ */
+ return apply_filters( self::FILTER_PREFIX . 'available_currencies', array_intersect( $account_currencies, $wc_currencies ) );
+ }
+
+ /**
+ * Register the CSS and JS admin scripts.
+ *
+ * @return void
+ */
+ private function register_admin_scripts() {
+ $this->register_script_with_dependencies( 'WCPAY_MULTI_CURRENCY_SETTINGS', 'dist/multi-currency', [ 'WCPAY_ADMIN_SETTINGS' ] );
+
+ wp_register_style(
+ 'WCPAY_MULTI_CURRENCY_SETTINGS',
+ plugins_url( 'dist/multi-currency.css', $this->settings_service->get_plugin_file_path() ),
+ [ 'wc-components', 'WCPAY_ADMIN_SETTINGS' ],
+ $this->get_file_version( 'dist/multi-currency.css' ),
+ 'all'
+ );
+ }
+
+ /**
+ * Enables simulation of client browser currency.
+ *
+ * @return void
+ */
+ private function simulate_client_currency() {
+ if ( ! $this->simulation_params['enable_auto_currency'] ) {
+ return;
+ }
+
+ $countries = $this->payments_account->get_supported_countries();
+
+ $predefined_simulation_currencies = [
+ 'USD' => $countries['US'],
+ 'GBP' => $countries['GB'],
+ ];
+
+ $simulation_currency = 'USD' === get_option( 'woocommerce_currency', 'USD' ) ? 'GBP' : 'USD';
+ $simulation_currency_name = $this->available_currencies[ $simulation_currency ]->get_name();
+ $simulation_country = $predefined_simulation_currencies[ $simulation_currency ];
+
+ // Simulate client currency from geolocation.
+ add_filter(
+ 'wcpay_multi_currency_override_notice_currency_name',
+ function ( $selected_currency_name ) use ( $simulation_currency_name ) {
+ return $simulation_currency_name;
+ }
+ );
+
+ // Simulate client country from geolocation.
+ add_filter(
+ 'wcpay_multi_currency_override_notice_country',
+ function ( $selected_country ) use ( $simulation_country ) {
+ return $simulation_country;
+ }
+ );
+
+ // Always display the notice on simulation screen, prevent duplicate hooks.
+ if ( ! has_action( 'wp_footer', [ $this, 'display_geolocation_currency_update_notice' ] ) ) {
+ add_action( 'wp_footer', [ $this, 'display_geolocation_currency_update_notice' ] );
+ }
+
+ // Skip recalculating the cart to prevent infinite loop in simulation.
+ remove_action( 'wp_loaded', [ $this, 'recalculate_cart' ] );
+ }
+
+ /**
+ * Adds the required querystring parameters to all urls in preview pages.
+ *
+ * @return void
+ */
+ private function add_simulation_params_to_preview_urls() {
+ $params = $this->simulation_params;
+ add_filter(
+ 'wp_footer',
+ function () use ( $params ) {
+ ?>
+
+ is_stripe_connected() ) {
+ if ( is_null( self::$account ) || ! self::$account->is_provider_connected() ) {
return;
}
@@ -80,9 +80,9 @@ public static function possibly_add_note() {
/**
* Sets the account service instance reference on the class.
*
- * @param WC_Payments_Account $account account service instance.
+ * @param MultiCurrencyAccountInterface $account account service instance.
*/
- public static function set_account( WC_Payments_Account $account ) {
+ public static function set_account( MultiCurrencyAccountInterface $account ) {
self::$account = $account;
}
}
diff --git a/includes/multi-currency/RestController.php b/multi-currency/src/RestController.php
similarity index 78%
rename from includes/multi-currency/RestController.php
rename to multi-currency/src/RestController.php
index 0266b5f4e76..df36b1186b4 100644
--- a/includes/multi-currency/RestController.php
+++ b/multi-currency/src/RestController.php
@@ -1,21 +1,29 @@
multi_currency = $multi_currency;
+ }
+
+
/**
* Configure REST API routes.
*/
@@ -144,7 +169,7 @@ public function register_routes() {
* @return \WP_REST_Response|\WP_Error Array of the store currencies structure.
*/
public function get_store_currencies() {
- return rest_ensure_response( WC_Payments_Multi_Currency()->get_store_currencies() );
+ return rest_ensure_response( $this->multi_currency->get_store_currencies() );
}
/**
@@ -157,10 +182,10 @@ public function get_store_currencies() {
public function update_enabled_currencies( $request ) {
$enabled = $request->get_param( 'enabled' );
try {
- WC_Payments_Multi_Currency()->set_enabled_currencies( $enabled );
+ $this->multi_currency->set_enabled_currencies( $enabled );
$response = $this->get_store_currencies();
} catch ( InvalidCurrencyException $e ) {
- $response = new \WP_Error( $e->get_error_code(), $e->getMessage() );
+ $response = new \WP_Error( $e->getCode(), $e->getMessage() );
}
return rest_ensure_response( $response );
}
@@ -176,9 +201,9 @@ public function get_single_currency_settings( $request ) {
$currency_code = $request->get_param( 'currency_code' );
try {
- $response = WC_Payments_Multi_Currency()->get_single_currency_settings( $currency_code );
+ $response = $this->multi_currency->get_single_currency_settings( $currency_code );
} catch ( InvalidCurrencyException $e ) {
- $response = new \WP_Error( $e->get_error_code(), $e->getMessage() );
+ $response = new \WP_Error( $e->getCode(), $e->getMessage() );
}
return rest_ensure_response( $response );
@@ -199,10 +224,10 @@ public function update_single_currency_settings( $request ) {
$manual_rate = $request->get_param( 'manual_rate' ) ?? null;
try {
- WC_Payments_Multi_Currency()->update_single_currency_settings( $currency_code, $exchange_rate_type, $price_rounding, $price_charm, $manual_rate );
- $response = WC_Payments_Multi_Currency()->get_single_currency_settings( $currency_code );
- } catch ( Base_Exception $e ) {
- $response = new \WP_Error( $e->get_error_code(), $e->getMessage() );
+ $this->multi_currency->update_single_currency_settings( $currency_code, $exchange_rate_type, $price_rounding, $price_charm, $manual_rate );
+ $response = $this->multi_currency->get_single_currency_settings( $currency_code );
+ } catch ( Exception $e ) {
+ $response = new \WP_Error( $e->getCode(), $e->getMessage() );
}
return rest_ensure_response( $response );
@@ -214,7 +239,7 @@ public function update_single_currency_settings( $request ) {
* @return \WP_REST_Response|\WP_Error The store settings as an array.
*/
public function get_settings() {
- return rest_ensure_response( WC_Payments_Multi_Currency()->get_settings() );
+ return rest_ensure_response( $this->multi_currency->get_settings() );
}
/**
@@ -226,7 +251,14 @@ public function get_settings() {
*/
public function update_settings( $request ) {
$params = $request->get_params();
- WC_Payments_Multi_Currency()->update_settings( $params );
- return rest_ensure_response( WC_Payments_Multi_Currency()->get_settings() );
+ $this->multi_currency->update_settings( $params );
+ return rest_ensure_response( $this->multi_currency->get_settings() );
+ }
+
+ /**
+ * Verify access.
+ */
+ public function check_permission() {
+ return current_user_can( 'manage_woocommerce' );
}
}
diff --git a/includes/multi-currency/Settings.php b/multi-currency/src/Settings.php
similarity index 96%
rename from includes/multi-currency/Settings.php
rename to multi-currency/src/Settings.php
index 11c74d56619..8fe0a4aafcd 100644
--- a/includes/multi-currency/Settings.php
+++ b/multi-currency/src/Settings.php
@@ -88,7 +88,7 @@ public function wcpay_multi_currency_settings_page() {
* Load inline Emoji detection script on multi-currency settings page
*/
public function maybe_add_print_emoji_detection_script() {
- if ( WC_Payments_Multi_Currency()->is_multi_currency_settings_page() ) {
+ if ( $this->multi_currency->is_multi_currency_settings_page() ) {
print_emoji_detection_script();
}
}
diff --git a/includes/multi-currency/SettingsOnboardCta.php b/multi-currency/src/SettingsOnboardCta.php
similarity index 74%
rename from includes/multi-currency/SettingsOnboardCta.php
rename to multi-currency/src/SettingsOnboardCta.php
index ee89ea5638c..59a2eee286f 100644
--- a/includes/multi-currency/SettingsOnboardCta.php
+++ b/multi-currency/src/SettingsOnboardCta.php
@@ -7,6 +7,8 @@
namespace WCPay\MultiCurrency;
+use WCPay\MultiCurrency\Interfaces\MultiCurrencyAccountInterface;
+
defined( 'ABSPATH' ) || exit;
/**
@@ -27,15 +29,24 @@ class SettingsOnboardCta extends \WC_Settings_Page {
*/
private $multi_currency;
+ /**
+ * Instance of MultiCurrencyAccountInterface.
+ *
+ * @var MultiCurrencyAccountInterface
+ */
+ private $payments_account;
+
/**
* Constructor.
*
- * @param MultiCurrency $multi_currency The MultiCurrency instance.
+ * @param MultiCurrency $multi_currency The MultiCurrency instance.
+ * @param MultiCurrencyAccountInterface $payments_account Payments Account instance.
*/
- public function __construct( MultiCurrency $multi_currency ) {
- $this->multi_currency = $multi_currency;
- $this->id = $this->multi_currency->id;
- $this->label = _x( 'Multi-currency', 'Settings tab label', 'woocommerce-payments' );
+ public function __construct( MultiCurrency $multi_currency, MultiCurrencyAccountInterface $payments_account ) {
+ $this->multi_currency = $multi_currency;
+ $this->payments_account = $payments_account;
+ $this->id = $this->multi_currency->id;
+ $this->label = _x( 'Multi-currency', 'Settings tab label', 'woocommerce-payments' );
parent::__construct();
}
@@ -53,7 +64,7 @@ public function init_hooks() {
* Output the call to action button if needing to onboard.
*/
public function currencies_settings_onboarding_cta() {
- $href = \WC_Payments_Account::get_connect_page_url();
+ $href = $this->payments_account->get_provider_onboarding_page_url();
?>
diff --git a/includes/multi-currency/StorefrontIntegration.php b/multi-currency/src/StorefrontIntegration.php
similarity index 100%
rename from includes/multi-currency/StorefrontIntegration.php
rename to multi-currency/src/StorefrontIntegration.php
diff --git a/includes/multi-currency/Tracking.php b/multi-currency/src/Tracking.php
similarity index 100%
rename from includes/multi-currency/Tracking.php
rename to multi-currency/src/Tracking.php
diff --git a/includes/multi-currency/UserSettings.php b/multi-currency/src/UserSettings.php
similarity index 100%
rename from includes/multi-currency/UserSettings.php
rename to multi-currency/src/UserSettings.php
diff --git a/includes/multi-currency/Utils.php b/multi-currency/src/Utils.php
similarity index 51%
rename from includes/multi-currency/Utils.php
rename to multi-currency/src/Utils.php
index 64e5356a77a..b0c1fba84ea 100644
--- a/includes/multi-currency/Utils.php
+++ b/multi-currency/src/Utils.php
@@ -57,7 +57,7 @@ public function is_page_with_vars( array $pages, array $vars ): bool {
* @return boolean
*/
public static function is_admin_api_request(): bool {
- return 0 === stripos( wp_get_referer(), admin_url() ) && WC()->is_rest_api_request() && ! \WC_Payments_Utils::is_store_api_request();
+ return 0 === stripos( wp_get_referer(), admin_url() ) && WC()->is_rest_api_request() && ! self::is_store_api_request();
}
@@ -71,4 +71,39 @@ public static function is_admin_api_request(): bool {
public static function set_customer_session_cookie( bool $set ) {
WC()->session->set_customer_session_cookie( $set );
}
+
+ /**
+ * Returns true if the request is a store REST API request.
+ *
+ * @return bool
+ */
+ public static function is_store_api_request() {
+ if ( function_exists( 'WC' ) && method_exists( WC(), 'is_store_api_request' ) ) {
+ return WC()->is_store_api_request();
+ }
+ // The logic below is sourced from `WC()->is_store_api_request()`.
+ if ( empty( $_SERVER['REQUEST_URI'] ) ) {
+ return false;
+ }
+ // phpcs:disable WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
+ return false !== strpos( $_SERVER['REQUEST_URI'], trailingslashit( rest_get_url_prefix() ) . 'wc/store/' );
+ }
+
+ /**
+ * Returns true if the request that's currently being processed is a Store API batch request, false
+ * otherwise.
+ *
+ * @return bool True if the request is a Store API batch request, false otherwise.
+ */
+ public static function is_store_batch_request(): bool {
+ if ( isset( $_REQUEST['rest_route'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
+ $rest_route = sanitize_text_field( $_REQUEST['rest_route'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.NonceVerification
+ } else {
+ $url_parts = wp_parse_url( esc_url_raw( $_SERVER['REQUEST_URI'] ?? '' ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash
+ $request_path = $url_parts ? rtrim( $url_parts['path'], '/' ) : '';
+ $rest_route = str_replace( trailingslashit( rest_get_url_prefix() ), '', $request_path );
+ }
+
+ return 1 === preg_match( '@^\/wc\/store(\/v[\d]+)?\/batch@', $rest_route );
+ }
}
diff --git a/phpcs.xml.dist b/phpcs.xml.dist
index 6e21606d2bb..a0bf916f7a9 100644
--- a/phpcs.xml.dist
+++ b/phpcs.xml.dist
@@ -102,7 +102,7 @@
tests/*
- includes/multi-currency
+ multi-currency/src
src
diff --git a/psalm-baseline.xml b/psalm-baseline.xml
index 2a5164e8af3..1eb5fab3f04 100644
--- a/psalm-baseline.xml
+++ b/psalm-baseline.xml
@@ -46,13 +46,13 @@
WC_Subscriptions_Product
-
+
\WC_Product_Addons_Helper
\WC_Product_Addons_Helper
-
+
\WC_Name_Your_Price_Helpers
\WC_Name_Your_Price_Helpers
diff --git a/tasks/release.js b/tasks/release.js
index 92559af4ffb..72c3eccdef7 100644
--- a/tasks/release.js
+++ b/tasks/release.js
@@ -14,11 +14,11 @@ const targetFolder = 'release/' + pluginSlug;
const filesToCopy = [
'assets',
'dist',
- 'includes',
'i18n',
+ 'includes',
'languages',
- 'src',
'lib',
+ 'src',
'templates',
'vendor',
'woocommerce-payments.php',
@@ -43,6 +43,10 @@ rm( 'dist/*.map' );
// copy the directories to the release folder
cp( '-Rf', filesToCopy, targetFolder );
+// copy the multi-currency files
+mkdir( '-p', targetFolder + '/multi-currency' );
+cp( '-R', 'multi-currency/src', targetFolder + '/multi-currency/src' );
+
const output = fs.createWriteStream(
releaseFolder + '/' + pluginSlug + '.zip'
);
diff --git a/tests/js/jest.config.js b/tests/js/jest.config.js
index 18c11bc9553..03060196d4c 100644
--- a/tests/js/jest.config.js
+++ b/tests/js/jest.config.js
@@ -2,13 +2,18 @@ const { jsWithBabel: tsjPreset } = require( 'ts-jest/presets' );
module.exports = {
rootDir: '../../',
- moduleDirectories: [ 'node_modules', '/client' ],
+ moduleDirectories: [
+ 'node_modules',
+ '/client',
+ '/multi-currency/client',
+ ],
moduleNameMapper: {
'^react$': '/node_modules/react',
'^react-dom$': '/node_modules/react-dom',
'^moment$': '/node_modules/moment',
'^moment-timezone$': '/node_modules/moment-timezone',
'^wcpay(.*)$': '/client$1',
+ '^multi-currency(.*)$': '/multi-currency/client$1',
'^iti/utils$': '/node_modules/intl-tel-input/build/js/utils',
'^assets(.*?)(\\?.*)?$': '/assets$1',
'^@woocommerce/blocks-registry$':
diff --git a/tests/unit/multi-currency/compatibility/test-class-woocommerce-subscriptions.php b/tests/unit/multi-currency/compatibility/test-class-woocommerce-subscriptions.php
index 5b71b8f8e7f..c2085b1fe5d 100644
--- a/tests/unit/multi-currency/compatibility/test-class-woocommerce-subscriptions.php
+++ b/tests/unit/multi-currency/compatibility/test-class-woocommerce-subscriptions.php
@@ -6,6 +6,11 @@
*/
use WCPay\MultiCurrency\Compatibility\WooCommerceSubscriptions;
+use WCPay\MultiCurrency\Interfaces\MultiCurrencyAccountInterface;
+use WCPay\MultiCurrency\Interfaces\MultiCurrencyApiClientInterface;
+use WCPay\MultiCurrency\Interfaces\MultiCurrencyCacheInterface;
+use WCPay\MultiCurrency\Interfaces\MultiCurrencyLocalizationInterface;
+use WCPay\MultiCurrency\Interfaces\MultiCurrencySettingsInterface;
use WCPay\MultiCurrency\MultiCurrency;
use WCPay\MultiCurrency\Utils;
@@ -62,7 +67,16 @@ class WCPay_Multi_Currency_WooCommerceSubscriptions_Tests extends WCPAY_UnitTest
public function set_up() {
parent::set_up();
- $this->mock_multi_currency = $this->createMock( MultiCurrency::class );
+ $mock_api_client = $this->createMock( MultiCurrencyApiClientInterface::class );
+ $mock_account = $this->createMock( MultiCurrencyAccountInterface::class );
+ $mock_localization = $this->createMock( MultiCurrencyLocalizationInterface::class );
+ $mock_cache = $this->createMock( MultiCurrencyCacheInterface::class );
+ $mock_settings = $this->createMock( MultiCurrencySettingsInterface::class );
+
+ $this->mock_multi_currency = $this->getMockBuilder( MultiCurrency::class )
+ ->setConstructorArgs( [ $mock_settings, $mock_api_client, $mock_account, $mock_localization, $mock_cache ] )
+ ->getMock();
+
$this->mock_utils = $this->createMock( Utils::class );
$this->woocommerce_subscriptions = new WooCommerceSubscriptions( $this->mock_multi_currency, $this->mock_utils );
@@ -828,18 +842,11 @@ public function test_maybe_get_explicit_format_for_subscription_total() {
->willReturn( true );
// Arrange: Set expectation and return for is_initialized and has_additional_currencies_enabled.
- $this->mock_multi_currency
- ->expects( $this->once() )
- ->method( 'is_initialized' )
- ->willReturn( true );
$this->mock_multi_currency
->expects( $this->once() )
->method( 'has_additional_currencies_enabled' )
->willReturn( true );
- // Arrange: Make sure to set our Multi-Currency instance as our mock instance.
- WC_Payments_Explicit_Price_Formatter::set_multi_currency_instance( $this->mock_multi_currency );
-
// Arrange/Assert: Apply the woocommerce_subscription_price_string_details filter and confirm the filter does not change the passed array.
$this->assertSame( [ 1, 2, 3 ], apply_filters( 'woocommerce_subscription_price_string_details', [ 1, 2, 3 ], $mock_subscription ) );
diff --git a/tests/unit/multi-currency/notes/test-class-note-multi-currency-available-test.php b/tests/unit/multi-currency/notes/test-class-note-multi-currency-available-test.php
index 5a985dd0511..e434ae9f661 100644
--- a/tests/unit/multi-currency/notes/test-class-note-multi-currency-available-test.php
+++ b/tests/unit/multi-currency/notes/test-class-note-multi-currency-available-test.php
@@ -6,6 +6,7 @@
*/
use WCPay\MultiCurrency\Notes\NoteMultiCurrencyAvailable;
+use WCPay\MultiCurrency\Interfaces\MultiCurrencyAccountInterface;
/**
* Class Note_Multi_Currency_Available_Test tests.
@@ -55,8 +56,8 @@ public function test_possibly_add_note_without_account() {
}
public function test_possibly_add_note_with_account_not_connected() {
- $account_mock = $this->createMock( WC_Payments_Account::class );
- $account_mock->method( 'is_stripe_connected' )->willReturn( false );
+ $account_mock = $this->createMock( MultiCurrencyAccountInterface::class );
+ $account_mock->method( 'is_provider_connected' )->willReturn( false );
NoteMultiCurrencyAvailable::set_account( $account_mock );
NoteMultiCurrencyAvailable::possibly_add_note();
@@ -65,8 +66,8 @@ public function test_possibly_add_note_with_account_not_connected() {
}
public function test_possibly_add_note_with_connected_account() {
- $account_mock = $this->createMock( WC_Payments_Account::class );
- $account_mock->method( 'is_stripe_connected' )->willReturn( true );
+ $account_mock = $this->createMock( MultiCurrencyAccountInterface::class );
+ $account_mock->method( 'is_provider_connected' )->willReturn( true );
NoteMultiCurrencyAvailable::set_account( $account_mock );
NoteMultiCurrencyAvailable::possibly_add_note();
diff --git a/tests/unit/multi-currency/test-class-analytics.php b/tests/unit/multi-currency/test-class-analytics.php
index 00b46e2d7ef..178db790140 100644
--- a/tests/unit/multi-currency/test-class-analytics.php
+++ b/tests/unit/multi-currency/test-class-analytics.php
@@ -1,6 +1,6 @@
create_can_manage_woocommerce_cap_override( true );
add_filter( 'user_has_cap', $cb );
- $this->mock_multi_currency = $this->createMock( MultiCurrency::class );
+ $mock_api_client = $this->createMock( MultiCurrencyApiClientInterface::class );
+ $mock_account = $this->createMock( MultiCurrencyAccountInterface::class );
+ $mock_localization = $this->createMock( MultiCurrencyLocalizationInterface::class );
+ $mock_cache = $this->createMock( MultiCurrencyCacheInterface::class );
+ $mock_settings = $this->createMock( MultiCurrencySettingsInterface::class );
+
+ $this->mock_multi_currency = $this->getMockBuilder( MultiCurrency::class )
+ ->setConstructorArgs( [ $mock_settings, $mock_api_client, $mock_account, $mock_localization, $mock_cache ] )
+ ->getMock();
$this->mock_multi_currency->expects( $this->any() )
->method( 'get_all_customer_currencies' )
@@ -82,9 +95,12 @@ public function set_up() {
->method( 'get_available_currencies' )
->willReturn( $this->get_mock_available_currencies() );
- $this->analytics = new Analytics( $this->mock_multi_currency );
+ $this->analytics = new Analytics( $this->mock_multi_currency, $mock_settings );
- $this->localization_service = new WC_Payments_Localization_Service();
+ $this->mock_localization_service = $this->createMock( MultiCurrencyLocalizationInterface::class );
+ $this->mock_localization_service->expects( $this->any() )
+ ->method( 'get_currency_format' )
+ ->willReturn( [ 'num_decimals' => 2 ] );
remove_filter( 'user_has_cap', $cb );
}
@@ -164,7 +180,7 @@ public function test_update_order_stats_data_with_multi_currency_order_without_m
public function test_update_order_stats_data_with_multi_currency_order() {
$this->mock_multi_currency->expects( $this->once() )
->method( 'get_default_currency' )
- ->willReturn( new Currency( $this->localization_service, 'USD', 1.0 ) );
+ ->willReturn( new Currency( $this->mock_localization_service, 'USD', 1.0 ) );
$args = $this->order_args_provider( 123, 0, 1, 15.50, 1.50, 0, 14.00 );
$order = wc_create_order();
@@ -179,7 +195,7 @@ public function test_update_order_stats_data_with_multi_currency_order() {
public function test_update_order_stats_data_with_large_order() {
$this->mock_multi_currency->expects( $this->once() )
->method( 'get_default_currency' )
- ->willReturn( new Currency( $this->localization_service, 'USD', 1.0 ) );
+ ->willReturn( new Currency( $this->mock_localization_service, 'USD', 1.0 ) );
$args = $this->order_args_provider( 123, 0, 1, 130500.75, 20000, 10000, 100500.75 );
$order = wc_create_order();
@@ -194,7 +210,7 @@ public function test_update_order_stats_data_with_large_order() {
public function test_update_order_stats_data_with_stripe_exchange_rate() {
$this->mock_multi_currency->expects( $this->once() )
->method( 'get_default_currency' )
- ->willReturn( new Currency( $this->localization_service, 'USD', 1.0 ) );
+ ->willReturn( new Currency( $this->mock_localization_service, 'USD', 1.0 ) );
$args = $this->order_args_provider( 123, 0, 1, 15.50, 1.50, 0, 15.00 );
$order = wc_create_order();
@@ -587,14 +603,19 @@ private function create_can_manage_woocommerce_cap_override( bool $can_manage_wo
}
private function get_mock_available_currencies() {
- $this->localization_service = new WC_Payments_Localization_Service();
+ $this->mock_localization_service = $this->createMock( MultiCurrencyLocalizationInterface::class );
if ( empty( $this->mock_available_currencies ) ) {
+ $this->mock_localization_service
+ ->expects( $this->any() )
+ ->method( 'get_currency_format' )
+ ->willReturn( [ 'num_decimals' => 2 ] );
+
$this->mock_available_currencies = [
- 'GBP' => new Currency( $this->localization_service, 'GBP', 1.2 ),
- 'USD' => new Currency( $this->localization_service, 'USD', 1 ),
- 'EUR' => new Currency( $this->localization_service, 'EUR', 0.9 ),
- 'ISK' => new Currency( $this->localization_service, 'ISK', 30.52 ),
- 'NZD' => new Currency( $this->localization_service, 'NZD', 1.4 ),
+ 'GBP' => new Currency( $this->mock_localization_service, 'GBP', 1.2 ),
+ 'USD' => new Currency( $this->mock_localization_service, 'USD', 1 ),
+ 'EUR' => new Currency( $this->mock_localization_service, 'EUR', 0.9 ),
+ 'ISK' => new Currency( $this->mock_localization_service, 'ISK', 30.52 ),
+ 'NZD' => new Currency( $this->mock_localization_service, 'NZD', 1.4 ),
];
}
diff --git a/tests/unit/multi-currency/test-class-backend-currencies.php b/tests/unit/multi-currency/test-class-backend-currencies.php
index 530efc71b7d..8b333c6f189 100644
--- a/tests/unit/multi-currency/test-class-backend-currencies.php
+++ b/tests/unit/multi-currency/test-class-backend-currencies.php
@@ -6,6 +6,7 @@
*/
use WCPay\MultiCurrency\BackendCurrencies;
+use WCPay\MultiCurrency\Interfaces\MultiCurrencyLocalizationInterface;
use WCPay\MultiCurrency\MultiCurrency;
/**
@@ -13,9 +14,9 @@
*/
class WCPay_Multi_Currency_Backend_Currencies_Tests extends WCPAY_UnitTestCase {
/**
- * Mock WC_Payments_Localization_Service.
+ * Mock MultiCurrencyLocalizationInterface.
*
- * @var WC_Payments_Localization_Service|PHPUnit_Framework_MockObject_MockObject
+ * @var MultiCurrencyLocalizationInterface|PHPUnit_Framework_MockObject_MockObject
*/
private $mock_localization_service;
@@ -36,7 +37,7 @@ class WCPay_Multi_Currency_Backend_Currencies_Tests extends WCPAY_UnitTestCase {
public function set_up() {
parent::set_up();
- $this->mock_localization_service = $this->createMock( WC_Payments_Localization_Service::class );
+ $this->mock_localization_service = $this->createMock( MultiCurrencyLocalizationInterface::class );
$this->mock_multi_currency = $this->createMock( MultiCurrency::class );
// Mock admin part.
diff --git a/tests/unit/multi-currency/test-class-compatibility.php b/tests/unit/multi-currency/test-class-compatibility.php
index cefb478ca50..ae4cc75dc31 100644
--- a/tests/unit/multi-currency/test-class-compatibility.php
+++ b/tests/unit/multi-currency/test-class-compatibility.php
@@ -7,6 +7,7 @@
use WCPay\MultiCurrency\Compatibility;
use WCPay\MultiCurrency\Currency;
+use WCPay\MultiCurrency\Interfaces\MultiCurrencyLocalizationInterface;
use WCPay\MultiCurrency\MultiCurrency;
use WCPay\MultiCurrency\Utils;
@@ -36,11 +37,11 @@ class WCPay_Multi_Currency_Compatibility_Tests extends WCPAY_UnitTestCase {
private $mock_utils;
/**
- * WC_Payments_Localization_Service.
+ * MultiCurrencyLocalizationInterface.
*
- * @var WC_Payments_Localization_Service
+ * @var MultiCurrencyLocalizationInterface
*/
- private $localization_service;
+ private $mock_localization_service;
/**
* Pre-test setup
@@ -48,10 +49,15 @@ class WCPay_Multi_Currency_Compatibility_Tests extends WCPAY_UnitTestCase {
public function set_up() {
parent::set_up();
- $this->mock_multi_currency = $this->createMock( MultiCurrency::class );
- $this->mock_utils = $this->createMock( Utils::class );
- $this->compatibility = new Compatibility( $this->mock_multi_currency, $this->mock_utils );
- $this->localization_service = new WC_Payments_Localization_Service();
+ $this->mock_multi_currency = $this->createMock( MultiCurrency::class );
+ $this->mock_utils = $this->createMock( Utils::class );
+ $this->mock_localization_service = $this->createMock( MultiCurrencyLocalizationInterface::class );
+ $this->mock_localization_service
+ ->method( 'get_currency_format' )
+ ->with( 'USD' )
+ ->willReturn( [ 'num_decimals' => 2 ] );
+
+ $this->compatibility = new Compatibility( $this->mock_multi_currency, $this->mock_utils );
}
public function test_init_compatibility_classes_does_not_add_classes_if_one_enabled_currencies() {
@@ -108,7 +114,7 @@ public function test_filter_woocommerce_order_query_with_order_in_default_curren
$this->mock_multi_currency->expects( $this->once() )
->method( 'get_default_currency' )
- ->willReturn( new Currency( $this->localization_service, 'USD', 1.0 ) );
+ ->willReturn( new Currency( $this->mock_localization_service, 'USD', 1.0 ) );
$this->mock_utils->expects( $this->once() )
->method( 'is_call_in_backtrace' )
@@ -132,7 +138,7 @@ public function test_filter_woocommerce_order_query_with_order_with_no_exchange_
$this->mock_multi_currency->expects( $this->once() )
->method( 'get_default_currency' )
- ->willReturn( new Currency( $this->localization_service, 'USD', 1.0 ) );
+ ->willReturn( new Currency( $this->mock_localization_service, 'USD', 1.0 ) );
$this->mock_utils->expects( $this->once() )
->method( 'is_call_in_backtrace' )
@@ -153,7 +159,7 @@ public function test_filter_woocommerce_order_query_with_no_meta() {
$this->mock_multi_currency->expects( $this->once() )
->method( 'get_default_currency' )
- ->willReturn( new Currency( $this->localization_service, 'USD', 1.0 ) );
+ ->willReturn( new Currency( $this->mock_localization_service, 'USD', 1.0 ) );
$this->mock_utils->expects( $this->once() )
->method( 'is_call_in_backtrace' )
@@ -177,7 +183,7 @@ public function test_filter_woocommerce_order_query() {
$this->mock_multi_currency->expects( $this->once() )
->method( 'get_default_currency' )
- ->willReturn( new Currency( $this->localization_service, 'USD', 1.0 ) );
+ ->willReturn( new Currency( $this->mock_localization_service, 'USD', 1.0 ) );
$this->mock_utils->expects( $this->once() )
->method( 'is_call_in_backtrace' )
diff --git a/tests/unit/multi-currency/test-class-country-flags.php b/tests/unit/multi-currency/test-class-country-flags.php
index 0ebe63fb8ef..cd3adff8feb 100644
--- a/tests/unit/multi-currency/test-class-country-flags.php
+++ b/tests/unit/multi-currency/test-class-country-flags.php
@@ -5,7 +5,6 @@
* @package WooCommerce\Payments\Tests
*/
-use WCPay\Constants\Country_Code;
use WCPay\MultiCurrency\CountryFlags;
/**
@@ -13,7 +12,7 @@
*/
class Country_Flags_Test extends WCPAY_UnitTestCase {
public function test_get_by_country_returns_emoji_flag() {
- $this->assertEquals( CountryFlags::get_by_country( Country_Code::UNITED_STATES ), '🇺🇸' );
+ $this->assertEquals( CountryFlags::get_by_country( 'US' ), '🇺🇸' );
}
public function test_get_by_country_returns_empty_string() {
diff --git a/tests/unit/multi-currency/test-class-currency-switcher-block.php b/tests/unit/multi-currency/test-class-currency-switcher-block.php
index f716f6dbe75..2ac0a69fa11 100644
--- a/tests/unit/multi-currency/test-class-currency-switcher-block.php
+++ b/tests/unit/multi-currency/test-class-currency-switcher-block.php
@@ -10,6 +10,7 @@
use WCPay\MultiCurrency\Currency;
use WCPay\MultiCurrency\CurrencySwitcherBlock;
use WCPay\MultiCurrency\MultiCurrency;
+use WCPay\MultiCurrency\Interfaces\MultiCurrencyLocalizationInterface;
/**
* CurrencySwitcherBlock unit tests.
@@ -37,23 +38,25 @@ class WCPay_Multi_Currency_Currency_Switcher_Block_Tests extends WCPAY_UnitTestC
protected $mock_currencies;
/**
- * WC_Payments_Localization_Service.
- *
- * @var WC_Payments_Localization_Service
+ * @var MockObject\MultiCurrencyLocalizationInterface
*/
- private $localization_service;
+ private $mock_localization_service;
public function set_up() {
parent::set_up();
- $this->mock_multi_currency = $this->createMock( MultiCurrency::class );
- $this->mock_compatibility = $this->createMock( Compatibility::class );
- $this->localization_service = new WC_Payments_Localization_Service();
- $this->mock_currencies = [
- new Currency( $this->localization_service, 'USD', 1 ),
- new Currency( $this->localization_service, 'CAD', 1.206823 ),
- new Currency( $this->localization_service, 'GBP', 0.708099 ),
- new Currency( $this->localization_service, 'EUR', 0.826381 ),
+ $this->mock_multi_currency = $this->createMock( MultiCurrency::class );
+ $this->mock_compatibility = $this->createMock( Compatibility::class );
+ $this->mock_localization_service = $this->createMock( MultiCurrencyLocalizationInterface::class );
+ $this->mock_localization_service
+ ->method( 'get_currency_format' )
+ ->willReturn( [ 'num_decimals' => 2 ] );
+
+ $this->mock_currencies = [
+ new Currency( $this->mock_localization_service, 'USD', 1 ),
+ new Currency( $this->mock_localization_service, 'CAD', 1.206823 ),
+ new Currency( $this->mock_localization_service, 'GBP', 0.708099 ),
+ new Currency( $this->mock_localization_service, 'EUR', 0.826381 ),
];
$this->currency_switcher_block = new CurrencySwitcherBlock(
@@ -219,8 +222,8 @@ public function test_render_currency_option_will_escape_output() {
->method( 'get_enabled_currencies' )
->willReturn(
[
- new Currency( $this->localization_service, 'USD' ),
- new Currency( $this->localization_service, $currency_code, 1 ),
+ new Currency( $this->mock_localization_service, 'USD' ),
+ new Currency( $this->mock_localization_service, $currency_code, 1 ),
]
);
@@ -275,7 +278,7 @@ public function test_widget_does_not_render_on_single_currency() {
$this->mock_multi_currency
->expects( $this->once() )
->method( 'get_enabled_currencies' )
- ->willReturn( [ new Currency( $this->localization_service, 'USD' ) ] );
+ ->willReturn( [ new Currency( $this->mock_localization_service, 'USD' ) ] );
// Act/Assert: Confirm that when calling the renger method nothing is returned.
$this->assertSame( '', $this->currency_switcher_block->render_block_widget( [], '' ) );
diff --git a/tests/unit/multi-currency/test-class-frontend-currencies.php b/tests/unit/multi-currency/test-class-frontend-currencies.php
index bb560d8af9c..d63147de225 100644
--- a/tests/unit/multi-currency/test-class-frontend-currencies.php
+++ b/tests/unit/multi-currency/test-class-frontend-currencies.php
@@ -410,6 +410,7 @@ public function test_rest_api_ensure_should_return_store_currency_and_should_con
$_SERVER['REQUEST_URI'] = $request_uri;
$mccy = new WCPay\MultiCurrency\MultiCurrency(
+ WC_Payments::get_settings_service(),
WC_Payments::get_payments_api_client(),
WC_Payments::get_account_service(),
WC_Payments::get_localization_service(),
diff --git a/tests/unit/multi-currency/test-class-frontend-prices.php b/tests/unit/multi-currency/test-class-frontend-prices.php
index c38d343dcc9..13d6b4bb34d 100644
--- a/tests/unit/multi-currency/test-class-frontend-prices.php
+++ b/tests/unit/multi-currency/test-class-frontend-prices.php
@@ -5,8 +5,6 @@
* @package WooCommerce\Payments\Tests
*/
-use WCPay\Constants\Country_Code;
-
/**
* WCPay\MultiCurrency\FrontendPrices unit tests.
*/
@@ -216,7 +214,7 @@ function () {
);
WC()->session->init();
- WC()->customer->set_location( Country_Code::UNITED_STATES, 'CA' );
+ WC()->customer->set_location( 'US', 'CA' );
$shipping_method = new \WC_Shipping_Flat_Rate();
$shipping_method->tax_status = 'taxable';
@@ -260,7 +258,7 @@ function () {
);
WC()->session->init();
- WC()->customer->set_location( Country_Code::UNITED_STATES, 'CA' );
+ WC()->customer->set_location( 'US', 'CA' );
$shipping_method = new \WC_Shipping_Flat_Rate();
$shipping_method->tax_status = 'taxable';
diff --git a/tests/unit/multi-currency/test-class-geolocation.php b/tests/unit/multi-currency/test-class-geolocation.php
index b66543dbf67..92b0b50b166 100644
--- a/tests/unit/multi-currency/test-class-geolocation.php
+++ b/tests/unit/multi-currency/test-class-geolocation.php
@@ -5,8 +5,6 @@
* @package WooCommerce\Payments\Tests
*/
-use WCPay\Constants\Country_Code;
-
/**
* WCPay\MultiCurrency\Geolocation unit tests.
*/
@@ -51,10 +49,10 @@ public function test_get_country_by_customer_location_returns_geolocation_countr
add_filter(
'woocommerce_geolocate_ip',
function () {
- return Country_Code::CANADA;
+ return 'CA';
}
);
- $this->assertSame( Country_Code::CANADA, $this->geolocation->get_country_by_customer_location() );
+ $this->assertSame( 'CA', $this->geolocation->get_country_by_customer_location() );
}
public function test_get_country_by_customer_location_returns_default_country_when_no_geolocation() {
@@ -68,20 +66,20 @@ function () {
add_filter(
'woocommerce_customer_default_location',
function () {
- return Country_Code::BRAZIL;
+ return 'BR';
}
);
- $this->assertSame( Country_Code::BRAZIL, $this->geolocation->get_country_by_customer_location() );
+ $this->assertSame( 'BR', $this->geolocation->get_country_by_customer_location() );
}
public function test_get_currency_by_customer_location_returns_geolocation_currency_code() {
- $this->mock_localization_service->method( 'get_country_locale_data' )->with( Country_Code::CANADA )->willReturn( [ 'currency_code' => 'CAD' ] );
+ $this->mock_localization_service->method( 'get_country_locale_data' )->with( 'CA' )->willReturn( [ 'currency_code' => 'CAD' ] );
add_filter(
'woocommerce_geolocate_ip',
function () {
- return Country_Code::CANADA;
+ return 'CA';
}
);
@@ -89,7 +87,7 @@ function () {
}
public function test_get_currency_by_customer_location_returns_default_currency_code() {
- $this->mock_localization_service->method( 'get_country_locale_data' )->with( Country_Code::BRAZIL )->willReturn( [ 'currency_code' => 'BRL' ] );
+ $this->mock_localization_service->method( 'get_country_locale_data' )->with( 'BR' )->willReturn( [ 'currency_code' => 'BRL' ] );
add_filter(
'woocommerce_geolocate_ip',
@@ -100,7 +98,7 @@ function () {
add_filter(
'woocommerce_customer_default_location',
function () {
- return Country_Code::BRAZIL;
+ return 'BR';
}
);
diff --git a/tests/unit/multi-currency/test-class-multi-currency.php b/tests/unit/multi-currency/test-class-multi-currency.php
index 023fd92868c..6d4eddaab84 100644
--- a/tests/unit/multi-currency/test-class-multi-currency.php
+++ b/tests/unit/multi-currency/test-class-multi-currency.php
@@ -5,11 +5,13 @@
* @package WooCommerce\Payments\Tests
*/
-use WCPay\Constants\Country_Code;
use WCPay\MultiCurrency\Utils;
-use WCPay\Database_Cache;
use WCPay\MultiCurrency\Exceptions\InvalidCurrencyException;
use WCPay\MultiCurrency\Exceptions\InvalidCurrencyRateException;
+use WCPay\MultiCurrency\Interfaces\MultiCurrencyAccountInterface;
+use WCPay\MultiCurrency\Interfaces\MultiCurrencyApiClientInterface;
+use WCPay\MultiCurrency\Interfaces\MultiCurrencyCacheInterface;
+use WCPay\MultiCurrency\Interfaces\MultiCurrencySettingsInterface;
use WCPay\MultiCurrency\MultiCurrency;
use WCPay\MultiCurrency\Settings;
use WCPay\MultiCurrency\SettingsOnboardCta;
@@ -65,14 +67,14 @@ class WCPay_Multi_Currency_Tests extends WCPAY_UnitTestCase {
/**
* Mock of the API client.
*
- * @var WC_Payments_API_Client
+ * @var MultiCurrencyApiClientInterface
*/
private $mock_api_client;
/**
- * Mock of the WC_Payments_Account.
+ * Mock of the MultiCurrencyAccountInterface.
*
- * @var WC_Payments_Account
+ * @var MultiCurrencyAccountInterface
*/
private $mock_account;
@@ -84,11 +86,18 @@ class WCPay_Multi_Currency_Tests extends WCPAY_UnitTestCase {
private $localization_service;
/**
- * Mock of Database_Cache.
+ * Mock of MultiCurrencyCacheInterface.
*
- * @var Database_Cache;
+ * @var MultiCurrencyCacheInterface;
*/
- private $mock_database_cache;
+ private $mock_cache;
+
+ /**
+ * Mock of MultiCurrencySettingsInterface.
+ *
+ * @var MultiCurrencySettingsInterface;
+ */
+ private $mock_settings;
/**
* Mock of Utils.
@@ -456,7 +465,7 @@ public function test_update_selected_currency_by_geolocation_does_not_set_sessio
add_filter(
'woocommerce_geolocate_ip',
function () {
- return Country_Code::CANADA;
+ return 'CA';
}
);
@@ -473,7 +482,7 @@ public function test_update_selected_currency_by_geolocation_updates_session_whe
add_filter(
'woocommerce_geolocate_ip',
function () {
- return Country_Code::CANADA;
+ return 'CA';
}
);
@@ -488,7 +497,7 @@ public function test_update_selected_currency_by_geolocation_displays_notice() {
add_filter(
'woocommerce_geolocate_ip',
function () {
- return Country_Code::CANADA;
+ return 'CA';
}
);
@@ -509,7 +518,7 @@ public function test_update_selected_currency_by_geolocation_does_not_update_if_
add_filter(
'woocommerce_geolocate_ip',
function () {
- return Country_Code::CANADA;
+ return 'CA';
}
);
@@ -534,7 +543,7 @@ public function test_display_geolocation_currency_update_notice() {
add_filter(
'woocommerce_geolocate_ip',
function () {
- return Country_Code::CANADA;
+ return 'CA';
}
);
@@ -544,11 +553,11 @@ function () {
}
public function test_display_geolocation_currency_update_notice_does_not_display_if_using_default_currency() {
- WC()->session->set( WCPay\MultiCurrency\MultiCurrency::CURRENCY_SESSION_KEY, Country_Code::UNITED_STATES );
+ WC()->session->set( WCPay\MultiCurrency\MultiCurrency::CURRENCY_SESSION_KEY, 'US' );
add_filter(
'woocommerce_geolocate_ip',
function () {
- return Country_Code::UNITED_STATES;
+ return 'US';
}
);
@@ -562,7 +571,7 @@ public function test_display_geolocation_currency_update_notice_does_not_display
add_filter(
'woocommerce_geolocate_ip',
function () {
- return Country_Code::UNITED_STATES;
+ return 'US';
}
);
@@ -721,13 +730,13 @@ public function test_get_raw_conversion_throws_exception_on_invalid_from_rate()
public function test_get_cached_currencies_with_no_server_connection() {
// Need to create a new instance of MultiCurrency with a different $mock_api_client
// Because the mock return value of 'is_server_connected' cannot be overridden.
- $mock_api_client = $this->createMock( WC_Payments_API_Client::class );
+ $mock_api_client = $this->createMock( MultiCurrencyApiClientInterface::class );
$mock_api_client->method( 'is_server_connected' )->willReturn( false );
$this->init_multi_currency( $mock_api_client );
- $this->mock_database_cache->method( 'get' )->willReturn( $this->mock_cached_currencies );
+ $this->mock_cache->method( 'get' )->willReturn( $this->mock_cached_currencies );
$this->assertEquals(
$this->mock_cached_currencies,
@@ -736,7 +745,7 @@ public function test_get_cached_currencies_with_no_server_connection() {
}
public function test_get_cached_currencies_with_account_rejected() {
- $this->mock_database_cache
+ $this->mock_cache
->expects( $this->once() )
->method( 'get' )
->willReturn( null );
@@ -746,7 +755,7 @@ public function test_get_cached_currencies_with_account_rejected() {
->method( 'is_account_rejected' )
->willReturn( true );
- $this->mock_database_cache
+ $this->mock_cache
->expects( $this->never() )
->method( 'get_or_add' );
@@ -758,11 +767,11 @@ public function test_get_cached_currencies_with_account_rejected() {
public function test_get_cached_currencies_fetches_from_server() {
$get_or_add_call_count = 1;
- $mock_database_cache = $this->createMock( Database_Cache::class );
- $mock_database_cache
+ $mock_cache = $this->createMock( MultiCurrencyCacheInterface::class );
+ $mock_cache
->expects( $this->exactly( 2 ) )
->method( 'get_or_add' )
- ->with( Database_Cache::CURRENCIES_KEY, $this->anything(), $this->anything() )
+ ->with( MultiCurrencyCacheInterface::CURRENCIES_KEY, $this->anything(), $this->anything() )
->willReturnCallback(
function ( $key, $generator, $validator ) use ( &$get_or_add_call_count ) {
if ( 1 === $get_or_add_call_count ) {
@@ -776,7 +785,7 @@ function ( $key, $generator, $validator ) use ( &$get_or_add_call_count ) {
}
);
- $this->init_multi_currency( null, true, null, $mock_database_cache );
+ $this->init_multi_currency( null, true, null, $mock_cache );
$currency_from = strtolower( get_woocommerce_currency() );
$currencies_to = get_woocommerce_currencies();
@@ -886,7 +895,7 @@ public function test_enabled_currencies_option_as_string_does_not_fatal() {
public function test_get_cached_currencies_with_no_stripe_connection() {
$this->init_multi_currency( null, false );
- $this->mock_database_cache->method( 'get' )->willReturn( $this->mock_cached_currencies );
+ $this->mock_cache->method( 'get' )->willReturn( $this->mock_cached_currencies );
$this->assertEquals(
$this->mock_cached_currencies,
$this->multi_currency->get_cached_currencies()
@@ -1051,14 +1060,14 @@ public function test_get_all_customer_currencies() {
$mock_orders[] = $this->add_mock_order_with_currency_meta( 'EUR' );
$mock_orders[] = $this->add_mock_order_with_currency_meta( 'USD' );
- $mock_database_cache = $this->createMock( Database_Cache::class );
- $mock_database_cache
+ $mock_cache = $this->createMock( MultiCurrencyCacheInterface::class );
+ $mock_cache
->expects( $this->once() )
->method( 'get_or_add' )
- ->with( Database_Cache::CURRENCIES_KEY, $this->anything(), $this->anything() )
+ ->with( MultiCurrencyCacheInterface::CURRENCIES_KEY, $this->anything(), $this->anything() )
->willReturn( $this->mock_cached_currencies );
- $this->init_multi_currency( null, true, null, $mock_database_cache );
+ $this->init_multi_currency( null, true, null, $mock_cache );
$result = $this->multi_currency->get_all_customer_currencies();
@@ -1073,14 +1082,14 @@ public function test_get_all_customer_currencies_with_option_data() {
$mock_option_data = [ 'GBP', 'EUR', 'USD' ];
update_option( MultiCurrency::CUSTOMER_CURRENCIES_KEY, $mock_option_data );
- $mock_database_cache = $this->createMock( Database_Cache::class );
- $mock_database_cache
+ $mock_cache = $this->createMock( MultiCurrencyCacheInterface::class );
+ $mock_cache
->expects( $this->once() )
->method( 'get_or_add' )
- ->with( Database_Cache::CURRENCIES_KEY, $this->anything(), $this->anything() )
+ ->with( MultiCurrencyCacheInterface::CURRENCIES_KEY, $this->anything(), $this->anything() )
->willReturn( $this->mock_cached_currencies );
- $this->init_multi_currency( null, true, null, $mock_database_cache );
+ $this->init_multi_currency( null, true, null, $mock_cache );
$result = $this->multi_currency->get_all_customer_currencies();
@@ -1103,14 +1112,14 @@ public function test_get_all_customer_currencies_with_invalid_option_data( $opti
$mock_orders[] = $this->add_mock_order_with_currency_meta( 'EUR' );
$mock_orders[] = $this->add_mock_order_with_currency_meta( 'USD' );
- $mock_database_cache = $this->createMock( Database_Cache::class );
- $mock_database_cache
+ $mock_cache = $this->createMock( MultiCurrencyCacheInterface::class );
+ $mock_cache
->expects( $this->once() )
->method( 'get_or_add' )
- ->with( Database_Cache::CURRENCIES_KEY, $this->anything(), $this->anything() )
+ ->with( MultiCurrencyCacheInterface::CURRENCIES_KEY, $this->anything(), $this->anything() )
->willReturn( $this->mock_cached_currencies );
- $this->init_multi_currency( null, true, null, $mock_database_cache );
+ $this->init_multi_currency( null, true, null, $mock_cache );
$result = $this->multi_currency->get_all_customer_currencies();
@@ -1414,31 +1423,31 @@ private function remove_currency_settings_mock( $currency_code, $settings ) {
}
}
- private function init_multi_currency( $mock_api_client = null, $wcpay_account_connected = true, $mock_account = null, $mock_database_cache = null ) {
- $this->mock_api_client = $this->createMock( WC_Payments_API_Client::class );
+ private function init_multi_currency( $mock_api_client = null, $wcpay_account_connected = true, $mock_account = null, $mock_cache = null ) {
+ $this->mock_api_client = $this->createMock( MultiCurrencyApiClientInterface::class );
- $this->mock_account = $mock_account ?? $this->createMock( WC_Payments_Account::class );
- $this->mock_account->method( 'is_stripe_connected' )->willReturn( $wcpay_account_connected );
+ $this->mock_account = $mock_account ?? $this->createMock( MultiCurrencyAccountInterface::class );
+ $this->mock_account->method( 'is_provider_connected' )->willReturn( $wcpay_account_connected );
$this->mock_api_client->method( 'is_server_connected' )->willReturn( true );
- $this->mock_database_cache = $this->createMock( Database_Cache::class );
- $this->mock_database_cache->method( 'get_or_add' )->willReturn( $this->mock_cached_currencies );
+ $this->mock_cache = $this->createMock( MultiCurrencyCacheInterface::class );
+ $this->mock_cache->method( 'get_or_add' )->willReturn( $this->mock_cached_currencies );
$this->mock_utils = $this->createMock( Utils::class );
+ $this->mock_settings = $this->createMock( MultiCurrencySettingsInterface::class );
+
$this->multi_currency = new MultiCurrency(
+ $this->mock_settings,
$mock_api_client ?? $this->mock_api_client,
$this->mock_account,
$this->localization_service,
- $mock_database_cache ?? $this->mock_database_cache,
+ $mock_cache ?? $this->mock_cache,
$this->mock_utils
);
$this->multi_currency->init_widgets();
$this->multi_currency->init();
-
- // Fix an issue in WPCOM tests.
- WC_Payments_Explicit_Price_Formatter::set_multi_currency_instance( $this->multi_currency );
}
private function add_mock_order_with_currency_meta( $currency ) {
diff --git a/tests/unit/multi-currency/test-class-rest-controller.php b/tests/unit/multi-currency/test-class-rest-controller.php
index 49ac60ad1be..661b991df52 100644
--- a/tests/unit/multi-currency/test-class-rest-controller.php
+++ b/tests/unit/multi-currency/test-class-rest-controller.php
@@ -5,6 +5,13 @@
* @package WooCommerce\Payments\Tests
*/
+use PHPUnit\Framework\MockObject\MockObject;
+use WCPay\MultiCurrency\Currency;
+use WCPay\MultiCurrency\Interfaces\MultiCurrencyAccountInterface;
+use WCPay\MultiCurrency\Interfaces\MultiCurrencyApiClientInterface;
+use WCPay\MultiCurrency\Interfaces\MultiCurrencyCacheInterface;
+use WCPay\MultiCurrency\Interfaces\MultiCurrencyLocalizationInterface;
+use WCPay\MultiCurrency\MultiCurrency;
use WCPay\MultiCurrency\RestController;
/**
@@ -24,6 +31,27 @@ class WCPay_Multi_Currency_Rest_Controller_Tests extends WCPAY_UnitTestCase {
*/
private $controller;
+ /**
+ * Mock MultiCurrency.
+ *
+ * @var MultiCurrency|MockObject
+ */
+ private $mock_multi_currency;
+
+ /**
+ * The localization service.
+ *
+ * @var MultiCurrencyLocalizationInterface
+ */
+ private $mock_localization_service;
+
+ /**
+ * Mock available currencies.
+ *
+ * @var array An array of available currencies.
+ */
+ private $mock_available_currencies = [];
+
/**
* Pre-test setup
*/
@@ -33,13 +61,48 @@ public function set_up() {
// Set the user so that we can pass the authentication.
wp_set_current_user( 1 );
- $mock_api_client = $this->getMockBuilder( WC_Payments_API_Client::class )->disableOriginalConstructor()->getMock();
- $this->controller = new RestController( $mock_api_client );
+ $mock_api_client = $this->createMock( MultiCurrencyApiClientInterface::class );
+ $mock_account = $this->createMock( MultiCurrencyAccountInterface::class );
+ $mock_localization = $this->createMock( MultiCurrencyLocalizationInterface::class );
+ $mock_cache = $this->createMock( MultiCurrencyCacheInterface::class );
+ $mock_settings = $this->createMock( WC_Payments_Settings_Service::class );
+
+ $mock_account->method( 'is_provider_connected' )->willReturn( true );
+ $mock_api_client->method( 'is_server_connected' )->willReturn( true );
+
+ $mock_localization
+ ->method( 'get_currency_format' )
+ ->willReturn(
+ [
+ 'currency_pos' => 'right_space',
+ 'num_decimals' => 2,
+ ]
+ );
+
+ $this->mock_multi_currency = $this->getMockBuilder( MultiCurrency::class )
+ ->setConstructorArgs( [ $mock_settings, $mock_api_client, $mock_account, $mock_localization, $mock_cache ] )
+ ->enableOriginalConstructor()
+ ->onlyMethods( [ 'get_available_currencies' ] )
+ ->getMock();
+
+ $this->mock_multi_currency->expects( $this->any() )
+ ->method( 'get_available_currencies' )
+ ->willReturn( $this->get_mock_available_currencies() );
+
+ $this->controller = new RestController( $this->mock_multi_currency );
+ }
+
+ /**
+ * Post-test teardown
+ */
+ public function tear_down() {
+ remove_all_filters( 'wcpay_multi_currency_available_currencies' );
+ parent::tear_down();
}
public function test_get_store_currencies_gets_expected_response() {
// Arrange: Create expected response.
- $expected = rest_ensure_response( WC_Payments_Multi_Currency()->get_store_currencies() );
+ $expected = rest_ensure_response( $this->mock_multi_currency->get_store_currencies() );
// Act: Get the store currencies.
$response = $this->controller->get_store_currencies();
@@ -56,7 +119,7 @@ public function test_get_store_currencies_gets_expected_response() {
*/
public function test_update_enabled_currencies_updates_currencies() {
// Arrange: Create expected response.
- $expected = rest_ensure_response( WC_Payments_Multi_Currency()->get_store_currencies() );
+ $expected = rest_ensure_response( $this->mock_multi_currency->get_store_currencies() );
// Arrange: Delete the enabled currencies option.
delete_option( 'wcpay_multi_currency_enabled_currencies' );
@@ -82,9 +145,8 @@ public function test_update_enabled_currencies_throws_exception_on_unavailable_c
$error_currencies = [ 'EUR', 'GBP', 'banana' ];
// Arrange: Set expected result.
- $error_code = 'wcpay_multi_currency_invalid_currency';
$error_message = 'Invalid currency passed to set_enabled_currencies: ' . implode( ', ', $error_currencies );
- $expected = rest_ensure_response( new WP_Error( $error_code, $error_message ) );
+ $expected = rest_ensure_response( new WP_Error( 500, $error_message ) );
// Arrange: Create the new REST request.
$request = new WP_REST_Request( 'POST', self::ROUTE . '/update-enabled-currencies' );
@@ -110,7 +172,7 @@ public function test_get_single_currency_settings() {
update_option( 'wcpay_multi_currency_price_charm_usd', 0 );
// Arrange: Create expected response.
- $expected = rest_ensure_response( WC_Payments_Multi_Currency()->get_single_currency_settings( 'USD' ) );
+ $expected = rest_ensure_response( $this->mock_multi_currency->get_single_currency_settings( 'USD' ) );
// Arrange: Create the new REST request.
$request = new WP_REST_Request( 'GET', self::ROUTE . '/currencies/USD' );
@@ -129,9 +191,8 @@ public function test_get_single_currency_settings() {
public function test_get_single_currency_settings_throws_exception_on_unavailable_currency() {
// Arrange: Set expected result.
- $error_code = 'wcpay_multi_currency_invalid_currency';
$error_message = 'Invalid currency passed to get_single_currency_settings: AAA';
- $expected = rest_ensure_response( new WP_Error( $error_code, $error_message ) );
+ $expected = rest_ensure_response( new WP_Error( 500, $error_message ) );
// Arrange: Create the new REST request.
$request = new WP_REST_Request( 'GET', self::ROUTE . '/currencies/AAA' );
@@ -162,7 +223,7 @@ public function test_update_single_currency_settings() {
update_option( 'wcpay_multi_currency_price_charm_usd', 0 );
// Arrange: Create expected response.
- $expected = rest_ensure_response( WC_Payments_Multi_Currency()->get_single_currency_settings( 'USD' ) );
+ $expected = rest_ensure_response( $this->mock_multi_currency->get_single_currency_settings( 'USD' ) );
// Arrange: Now remove all the options.
delete_option( 'wcpay_multi_currency_exchange_rate_usd' );
@@ -191,9 +252,8 @@ public function test_update_single_currency_settings() {
public function test_update_single_currency_settings_throws_exception_on_unavailable_currency() {
// Arrange: Set expected result.
- $error_code = 'wcpay_multi_currency_invalid_currency';
$error_message = 'Invalid currency passed to update_single_currency_settings: AAA';
- $expected = rest_ensure_response( new WP_Error( $error_code, $error_message ) );
+ $expected = rest_ensure_response( new WP_Error( 500, $error_message ) );
// Arrange: Create the new REST request.
$request = new WP_REST_Request( 'POST', self::ROUTE . '/currencies/AAA' );
@@ -220,9 +280,8 @@ public function test_update_single_currency_settings_throws_exception_on_unavail
*/
public function test_update_single_currency_settings_throws_exception_on_invalid_currency_rate( $manual_rate ) {
// Arrange: Set expected result.
- $error_code = 'wcpay_multi_currency_invalid_currency_rate';
$error_message = 'Invalid manual currency rate passed to update_single_currency_settings: ' . $manual_rate;
- $expected = rest_ensure_response( new WP_Error( $error_code, $error_message ) );
+ $expected = rest_ensure_response( new WP_Error( 500, $error_message ) );
// Arrange: Create the new REST request.
$request = new WP_REST_Request( 'POST', self::ROUTE . '/currencies/USD' );
@@ -260,7 +319,7 @@ public function update_single_currency_settings_throws_exception_on_invalid_curr
public function test_get_settings_gets_expected_response() {
// Arrange: Create expected response.
- $expected = rest_ensure_response( WC_Payments_Multi_Currency()->get_settings() );
+ $expected = rest_ensure_response( $this->mock_multi_currency->get_settings() );
// Act: Get the settings.
$response = $this->controller->get_settings();
@@ -275,7 +334,7 @@ public function test_update_multi_currency_settings() {
update_option( 'wcpay_multi_currency_enable_storefront_switcher', 'yes' );
// Arrange: Create expected response.
- $expected = rest_ensure_response( WC_Payments_Multi_Currency()->get_settings() );
+ $expected = rest_ensure_response( $this->mock_multi_currency->get_settings() );
// Arrange: Now remove all the options.
delete_option( 'wcpay_multi_currency_enable_auto_currency' );
@@ -296,4 +355,20 @@ public function test_update_multi_currency_settings() {
// Assert: Confirm the response is what we expected.
$this->assertEquals( $expected, $response );
}
+
+ private function get_mock_available_currencies() {
+ $this->mock_localization_service = $this->createMock( MultiCurrencyLocalizationInterface::class );
+ if ( empty( $this->mock_available_currencies ) ) {
+ $this->mock_localization_service
+ ->expects( $this->any() )
+ ->method( 'get_currency_format' )
+ ->willReturn( [ 'num_decimals' => 2 ] );
+
+ $this->mock_available_currencies = [
+ 'USD' => new Currency( $this->mock_localization_service, 'USD', 1 ),
+ ];
+ }
+
+ return $this->mock_available_currencies;
+ }
}
diff --git a/tests/unit/multi-currency/test-class-settings.php b/tests/unit/multi-currency/test-class-settings.php
index 1ccbd373a0c..272a691910b 100644
--- a/tests/unit/multi-currency/test-class-settings.php
+++ b/tests/unit/multi-currency/test-class-settings.php
@@ -5,8 +5,6 @@
* @package WooCommerce\Payments\Tests
*/
-use WCPay\MultiCurrency\Currency;
-
/**
* WCPay\MultiCurrency\Settings unit tests.
*/
diff --git a/tests/unit/multi-currency/test-class-utils.php b/tests/unit/multi-currency/test-class-utils.php
index 1f3c9cce762..553df2a0cc1 100644
--- a/tests/unit/multi-currency/test-class-utils.php
+++ b/tests/unit/multi-currency/test-class-utils.php
@@ -60,8 +60,7 @@ public function test_is_admin_api_request_returns_true() {
public function test_is_admin_api_request_returns_false_with_store_api() {
$_SERVER['HTTP_REFERER'] = 'http://example.org/wp-admin/';
- $_REQUEST['rest_route'] = '/wc/store/v1/checkout';
- $_SERVER['REQUEST_URI'] = trailingslashit( rest_get_url_prefix() );
+ $_SERVER['REQUEST_URI'] = trailingslashit( rest_get_url_prefix() ) . 'wc/store/v1/checkout';
$this->assertFalse( $this->utils->is_admin_api_request() );
diff --git a/tests/unit/multi-currency/test-class-payment-methods-compatibility.php b/tests/unit/test-class-wc-payments-currency-manager.php
similarity index 84%
rename from tests/unit/multi-currency/test-class-payment-methods-compatibility.php
rename to tests/unit/test-class-wc-payments-currency-manager.php
index bdbbb767cde..6f13664ec75 100644
--- a/tests/unit/multi-currency/test-class-payment-methods-compatibility.php
+++ b/tests/unit/test-class-wc-payments-currency-manager.php
@@ -1,14 +1,14 @@
multi_currency_mock = $this
- ->getMockBuilder( WCPay\MultiCurrency\MultiCurrency::class )
+ $this->multi_currency_mock = $this->getMockBuilder( WCPay\MultiCurrency\MultiCurrency::class )
->disableOriginalConstructor()
->setMethods(
[
@@ -68,8 +67,16 @@ public function set_up() {
->getMock();
$this->gateway_mock->method( 'get_account_country' )->willReturn( 'US' );
- $this->payment_methods_compatibility = new \WCPay\MultiCurrency\PaymentMethodsCompatibility( $this->multi_currency_mock, $this->gateway_mock );
- $this->payment_methods_compatibility->init_hooks();
+ $this->currency_manager = $this->getMockBuilder( \WCPay\WC_Payments_Currency_Manager::class )
+ ->setConstructorArgs( [ $this->gateway_mock ] )
+ ->setMethods( [ 'get_multi_currency_instance' ] )
+ ->getMock();
+
+ // Mocking get_multi_currency_instance to return the multi_currency_mock.
+ $this->currency_manager->method( 'get_multi_currency_instance' )
+ ->willReturn( $this->multi_currency_mock );
+
+ $this->currency_manager->init_hooks();
$this->localization_service = new WC_Payments_Localization_Service();
}
@@ -79,7 +86,7 @@ public function test_it_should_not_update_available_currencies_when_enabled_paym
$this->gateway_mock->expects( $this->atLeastOnce() )->method( 'get_upe_enabled_payment_method_ids' )->willReturn( [ 'card' ] );
$this->gateway_mock->expects( $this->atLeastOnce() )->method( 'get_account_domestic_currency' )->willReturn( 'USD' );
- $this->payment_methods_compatibility->add_missing_currencies();
+ $this->currency_manager->maybe_add_missing_currencies();
}
public function test_it_should_not_update_available_currencies_when_not_needed() {
@@ -107,7 +114,7 @@ public function test_it_should_not_update_available_currencies_when_not_needed()
);
$this->multi_currency_mock->expects( $this->never() )->method( 'set_enabled_currencies' );
- $this->payment_methods_compatibility->add_missing_currencies();
+ $this->currency_manager->maybe_add_missing_currencies();
}
public function test_it_should_update_available_currencies_when_needed() {
@@ -147,7 +154,7 @@ public function test_it_should_update_available_currencies_when_needed() {
)
);
- $this->payment_methods_compatibility->add_missing_currencies();
+ $this->currency_manager->maybe_add_missing_currencies();
}
public function test_it_should_not_update_available_currencies_with_bnpl_methods() {
@@ -173,7 +180,7 @@ public function test_it_should_not_update_available_currencies_with_bnpl_methods
);
$this->multi_currency_mock->expects( $this->never() )->method( 'set_enabled_currencies' );
- $this->payment_methods_compatibility->add_missing_currencies();
+ $this->currency_manager->maybe_add_missing_currencies();
}
public function test_it_should_update_available_currencies_with_bnpl_methods() {
@@ -207,6 +214,6 @@ public function test_it_should_update_available_currencies_with_bnpl_methods() {
)
);
- $this->payment_methods_compatibility->add_missing_currencies();
+ $this->currency_manager->maybe_add_missing_currencies();
}
}
diff --git a/tests/unit/test-class-wc-payments-explicit-price-formatter.php b/tests/unit/test-class-wc-payments-explicit-price-formatter.php
index 9c32f3afdb5..728099e4473 100644
--- a/tests/unit/test-class-wc-payments-explicit-price-formatter.php
+++ b/tests/unit/test-class-wc-payments-explicit-price-formatter.php
@@ -78,6 +78,13 @@ class WC_Payments_Explicit_Price_Formatter_Test extends WCPAY_UnitTestCase {
*/
private $mock_localization_service;
+ /**
+ * Mock of the WC_Payments_Settings_Service.
+ *
+ * @var WC_Payments_Settings_Service
+ */
+ private $mock_settings;
+
/**
* Mock of Database_Cache.
*
@@ -230,7 +237,7 @@ private function init_multi_currency( $mock_api_client = null, $wcpay_account_co
$this->mock_api_client = $this->createMock( WC_Payments_API_Client::class );
$this->mock_account = $this->createMock( WC_Payments_Account::class );
- $this->mock_account->method( 'is_stripe_connected' )->willReturn( $wcpay_account_connected );
+ $this->mock_account->method( 'is_provider_connected' )->willReturn( $wcpay_account_connected );
$this->mock_localization_service = $this->createMock( WC_Payments_Localization_Service::class );
@@ -247,8 +254,9 @@ private function init_multi_currency( $mock_api_client = null, $wcpay_account_co
$this->mock_database_cache = $this->createMock( Database_Cache::class );
$this->mock_database_cache->method( 'get_or_add' )->willReturn( $this->mock_cached_currencies );
+ $this->mock_settings = $this->createMock( WC_Payments_Settings_Service::class );
- $this->multi_currency = new MultiCurrency( $mock_api_client ?? $this->mock_api_client, $this->mock_account, $this->mock_localization_service, $this->mock_database_cache );
+ $this->multi_currency = new MultiCurrency( $this->mock_settings, $mock_api_client ?? $this->mock_api_client, $this->mock_account, $this->mock_localization_service, $this->mock_database_cache );
$this->multi_currency->init();
WC_Payments_Explicit_Price_Formatter::set_multi_currency_instance( $this->multi_currency );
diff --git a/tsconfig.json b/tsconfig.json
index 3b9efabf45e..f7f56cddd52 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -15,8 +15,9 @@
"paths": {
"assets/*": [ "../assets/*" ],
"wcpay/*": [ "./*" ],
+ "multi-currency/*": [ "../multi-currency/client/*" ],
"iti/utils": [ "../node_modules/intl-tel-input/build/js/utils" ],
- "react": ["../node_modules/@types/react"]
+ "react": [ "../node_modules/@types/react" ]
},
"types": [
"node",
diff --git a/webpack/shared.js b/webpack/shared.js
index 7ef039967cd..55c249694ea 100644
--- a/webpack/shared.js
+++ b/webpack/shared.js
@@ -26,11 +26,11 @@ module.exports = {
'subscription-edit-page': './client/subscription-edit-page.js',
tos: './client/tos/index.js',
'payment-gateways': './client/payment-gateways/index.js',
- 'multi-currency': './client/multi-currency/index.js',
+ 'multi-currency': './multi-currency/client/index.js',
'multi-currency-switcher-block':
- './client/multi-currency/blocks/currency-switcher.js',
+ './multi-currency/client/blocks/currency-switcher.js',
'multi-currency-analytics':
- './client/multi-currency-analytics/index.js',
+ './multi-currency/client/analytics/index.js',
order: './client/order/index.js',
'subscriptions-empty-state':
'./client/subscriptions-empty-state/index.js',
@@ -113,9 +113,18 @@ module.exports = {
},
resolve: {
extensions: [ '.ts', '.tsx', '.json', '.js', '.jsx' ],
- modules: [ path.join( process.cwd(), 'client' ), 'node_modules' ],
+ modules: [
+ path.join( process.cwd(), 'client' ),
+ path.join( process.cwd(), 'multi-currency', 'client' ),
+ 'node_modules',
+ ],
alias: {
assets: path.resolve( process.cwd(), 'assets' ),
+ 'multi-currency': path.resolve(
+ process.cwd(),
+ 'multi-currency',
+ 'client'
+ ),
wcpay: path.resolve( process.cwd(), 'client' ),
iti: path.resolve(
process.cwd(),