Skip to content

Commit

Permalink
Merge branch 'develop' into delete/4710-remove-deposit_status
Browse files Browse the repository at this point in the history
  • Loading branch information
jessy-p committed May 15, 2024
2 parents 37bc90b + 038fd72 commit 0082367
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 52 deletions.
4 changes: 4 additions & 0 deletions changelog/add-phpstan-qit-tests-command
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: dev

Add command to run QIT PHPStan tests.
4 changes: 4 additions & 0 deletions changelog/fix-5150-xpf-currency
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: update

Update XPF currency formatting.
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,11 @@ const CurrencyPreview = ( {
? charmed
: charmed * 100,
targetCurrency.code,
storeCurrency.code
null,
true
);
},
[
charmValue,
currencyRate,
roundingValue,
targetCurrency,
storeCurrency,
]
[ charmValue, currencyRate, roundingValue, targetCurrency ]
);

useEffect( () => {
Expand Down
52 changes: 50 additions & 2 deletions client/utils/currency/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,48 @@ export const getCurrency = ( currencyCode, baseCurrencyCode = null ) => {
};
/* eslint-enable valid-jsdoc */

/**
* Gets wc-admin currency for the given currency code. Unlike `getCurrency`, this function
* will return the currency based only on the currency itself and locale. This means that the
* currency object will not be modified based on the store's country settings,
* or on another given currency.
*
* @param {string} currencyCode Currency code
* @return {Object} Currency object.
*/
export const getCurrencyByLocale = ( currencyCode ) => {
const code = currencyCode.toUpperCase();
const {
currencyData,
connect: { country = 'US' },
} = wcpaySettings;

// If store's country currency matches the provided currency code, return store's country currency.
if ( currencyData[ country ]?.code === code ) {
return Currency( currencyData[ country ] );
}

const currency = find( currencyData, { code } );

if ( currency ) {
const { defaultLocale = {} } = currency;

if (
defaultLocale.hasOwnProperty( 'decimalSeparator' ) &&
defaultLocale.hasOwnProperty( 'thousandSeparator' ) &&
defaultLocale.hasOwnProperty( 'symbolPosition' )
) {
currency.decimalSeparator = defaultLocale.decimalSeparator;
currency.thousandSeparator = defaultLocale.thousandSeparator;
currency.symbolPosition = defaultLocale.symbolPosition;
}

return Currency( currency );
}

return null;
};

/**
* Determines if the given currency is zero decimal.
*
Expand Down Expand Up @@ -105,13 +147,17 @@ export const formatExportAmount = ( amount, currencyCode ) => {
* @param {number} amount Amount
* @param {string} currencyCode Currency code
* @param {string} baseCurrencyCode Base Currency code to override decimal and thousand separators
* @param {boolean} useLocaleFormatting If `baseCurrencyCode` isn't provided, the currency will be
* formatted based on the store's country currency. This parameter allows to override this behavior
* and get the default formatting.
*
* @return {string} formatted currency representation
*/
export const formatCurrency = (
amount,
currencyCode = 'USD',
baseCurrencyCode = null
baseCurrencyCode = null,
useLocaleFormatting = false
) => {
// Normalize amount with respect to zer decimal currencies and provided data formats
const isZeroDecimal = isZeroDecimalCurrency( currencyCode );
Expand All @@ -122,7 +168,9 @@ export const formatCurrency = (
const isNegative = amount < 0;
const positiveAmount = isNegative ? -1 * amount : amount;
const prefix = isNegative ? '-' : '';
const currency = getCurrency( currencyCode, baseCurrencyCode );
const currency = useLocaleFormatting
? getCurrencyByLocale( currencyCode )
: getCurrency( currencyCode, baseCurrencyCode );

if ( currency === null ) {
return (
Expand Down
66 changes: 65 additions & 1 deletion client/utils/currency/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe( 'Currency utilities', () => {
jest.clearAllMocks();
global.wcpaySettings = {
shouldUseExplicitPrice: true,
zeroDecimalCurrencies: [ 'vnd', 'jpy' ],
zeroDecimalCurrencies: [ 'vnd', 'jpy', 'xpf' ],
connect: {
country: 'US',
},
Expand All @@ -25,6 +25,11 @@ describe( 'Currency utilities', () => {
thousandSeparator: ',',
decimalSeparator: '.',
precision: 2,
defaultLocale: {
symbolPosition: 'left',
thousandSeparator: ',',
decimalSeparator: '.',
},
},
JP: {
code: 'JPY',
Expand All @@ -33,6 +38,11 @@ describe( 'Currency utilities', () => {
thousandSeparator: ',',
decimalSeparator: '.',
precision: 0,
defaultLocale: {
symbolPosition: 'left',
thousandSeparator: ',',
decimalSeparator: '.',
},
},
FR: {
code: 'EUR',
Expand All @@ -41,6 +51,11 @@ describe( 'Currency utilities', () => {
thousandSeparator: ' ',
decimalSeparator: ',',
precision: 2,
defaultLocale: {
symbolPosition: 'right_space',
thousandSeparator: ',',
decimalSeparator: '.',
},
},
GB: {
code: 'GBP',
Expand All @@ -49,6 +64,11 @@ describe( 'Currency utilities', () => {
thousandSeparator: ',',
decimalSeparator: '.',
precision: 2,
defaultLocale: {
symbolPosition: 'left',
thousandSeparator: ',',
decimalSeparator: '.',
},
},
IN: {
code: 'INR',
Expand All @@ -57,6 +77,24 @@ describe( 'Currency utilities', () => {
thousandSeparator: ',',
decimalSeparator: '.',
precision: 2,
defaultLocale: {
symbolPosition: 'left',
thousandSeparator: ',',
decimalSeparator: '.',
},
},
NC: {
code: 'XPF',
symbol: 'XPF',
symbolPosition: 'right_space',
thousandSeparator: ' ',
decimalSeparator: '.',
precision: 0,
defaultLocale: {
symbolPosition: 'left_space',
thousandSeparator: ',',
decimalSeparator: '.',
},
},
RU: {
code: 'RUB',
Expand All @@ -65,6 +103,11 @@ describe( 'Currency utilities', () => {
thousandSeparator: ' ',
decimalSeparator: ',',
precision: 2,
defaultLocale: {
symbolPosition: 'right_space',
thousandSeparator: ' ',
decimalSeparator: ',',
},
},
},
};
Expand Down Expand Up @@ -110,6 +153,27 @@ describe( 'Currency utilities', () => {
expect( utils.formatCurrency( 100000, 'EUR' ) ).toEqual( '€1,000.00' );
} );

test( 'getCurrencyLocale should use store country currency when it matches', () => {
expect( utils.formatCurrency( 100000, 'USD', null, true ) ).toEqual(
'$1,000.00'
);

global.wcpaySettings.connect.country = 'NC';

expect( utils.formatCurrency( 100000, 'XPF', null, true ) ).toEqual(
'100 000 XPF'
);
} );

test( 'getCurrencyLocale should use default locale formatting', () => {
expect( utils.formatCurrency( 100000, 'EUR', null, true ) ).toEqual(
'1,000.00 €'
);
expect( utils.formatCurrency( 100000, 'XPF', null, true ) ).toEqual(
'XPF 100,000'
);
} );

test( 'format export amounts', () => {
expect( utils.formatExportAmount( 1000, 'USD' ) ).toEqual( 10 );
expect( utils.formatExportAmount( 1250, 'USD' ) ).toEqual( 12.5 );
Expand Down
2 changes: 1 addition & 1 deletion i18n/currency-info.php
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@
'fr_NC' => $global_formats['rs_comma_space_ltr'],
'fr_PF' => $global_formats['rs_comma_space_ltr'],
'fr_WF' => $global_formats['rs_comma_space_ltr'],
'default' => $global_formats['rs_comma_space_ltr'],
'default' => $global_formats['ls_dot_comma_ltr'],
],
'YER' => [
'ar_YE' => $global_formats['rs_comma_dot_rtl'],
Expand Down
6 changes: 3 additions & 3 deletions i18n/locale-info.php
Original file line number Diff line number Diff line change
Expand Up @@ -2559,7 +2559,7 @@
'currency_code' => 'XPF',
'currency_pos' => 'right_space',
'thousand_sep' => ' ',
'decimal_sep' => ',',
'decimal_sep' => '.',
'num_decimals' => 0,
'weight_unit' => 'kg',
'dimension_unit' => 'cm',
Expand Down Expand Up @@ -2783,7 +2783,7 @@
'currency_code' => 'XPF',
'currency_pos' => 'right_space',
'thousand_sep' => ' ',
'decimal_sep' => ',',
'decimal_sep' => '.',
'num_decimals' => 0,
'weight_unit' => 'kg',
'dimension_unit' => 'cm',
Expand Down Expand Up @@ -3855,7 +3855,7 @@
'currency_code' => 'XPF',
'currency_pos' => 'right_space',
'thousand_sep' => ' ',
'decimal_sep' => ',',
'decimal_sep' => '.',
'num_decimals' => 0,
'weight_unit' => 'kg',
'dimension_unit' => 'cm',
Expand Down
10 changes: 8 additions & 2 deletions includes/admin/class-wc-payments-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,14 +756,20 @@ private function get_js_settings(): array {
$currency_data = [];

foreach ( $locale_info as $key => $value ) {
$currency_code = $value['currency_code'] ?? '';
$currency_data[ $key ] = [
$currency_code = $value['currency_code'] ?? '';
$default_locale_formatting = $value['locales']['default'] ?? [];
$currency_data[ $key ] = [
'code' => $currency_code,
'symbol' => $value['short_symbol'] ?? $symbols[ $currency_code ] ?? '',
'symbolPosition' => $value['currency_pos'] ?? '',
'thousandSeparator' => $value['thousand_sep'] ?? '',
'decimalSeparator' => $value['decimal_sep'] ?? '',
'precision' => $value['num_decimals'],
'defaultLocale' => [
'symbolPosition' => $default_locale_formatting['currency_pos'] ?? '',
'thousandSeparator' => $default_locale_formatting['thousand_sep'] ?? '',
'decimalSeparator' => $default_locale_formatting['decimal_sep'] ?? '',
],
];
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"test:php-coverage": "./bin/check-test-coverage.sh",
"test:php-coverage-src": "./bin/check-test-coverage.sh src",
"test:php-watch": "npm run test:php -- -w",
"test:qit": "npm run build:release && ./tests/qit/security.sh",
"test:qit-security": "npm run build:release && ./tests/qit/security.sh",
"test:qit-phpstan": "npm run build:release && ./tests/qit/phpstan.sh",
"watch": "webpack --watch",
"hmr": "webpack server",
"start": "npm run watch",
Expand Down
34 changes: 34 additions & 0 deletions tests/qit/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash

set -e

cwd=$(pwd)
WCP_ROOT=$cwd
QIT_ROOT="$cwd/tests/qit"
EXTENSION_NAME="woocommerce-payments"

#Load local env variables if present.
if [[ -f "$QIT_ROOT/config/local.env" ]]; then
. "$QIT_ROOT/config/local.env"
fi

# Check if QIT_USER and QIT_APP_PASSWORD are set and not empty
if [[ -z $QIT_USER ]] || [[ -z $QIT_PASSWORD ]]; then
echo "QIT_USER or QIT_APP_PASSWORD environment variables are not set or empty. Please set them in the local env file before running the script."
exit 1
fi

export QIT_DISABLE_ONBOARDING=yes

# If QIT_BINARY is not set, default to ./vendor/bin/qit
QIT_BINARY=${QIT_BINARY:-./vendor/bin/qit}

# Add the partner by validating credentials.
if ! $QIT_BINARY list | grep -q 'partner:remove'; then
echo "Adding partner with QIT credentials..."
$QIT_BINARY partner:add --user=$QIT_USER --application_password=$QIT_PASSWORD
if [ $? -ne 0 ]; then
echo "Failed to add partner. Exiting with status 1."
exit 1
fi
fi
14 changes: 14 additions & 0 deletions tests/qit/phpstan.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

# Get the directory of the current script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Source common.sh using the relative path
source "$DIR/common.sh"

echo "Running PHPStan tests..."
$QIT_BINARY run:phpstan woocommerce-payments --wait
if [ $? -ne 0 ]; then
echo "Failed to run PHPStan command. Exiting with status 1."
exit 1
fi
40 changes: 6 additions & 34 deletions tests/qit/security.sh
Original file line number Diff line number Diff line change
@@ -1,42 +1,14 @@
#!/usr/bin/env bash

set -e
# Get the directory of the current script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

cwd=$(pwd)
WCP_ROOT=$cwd
QIT_ROOT="$cwd/tests/qit"
EXTENSION_NAME="woocommerce-payments"
# Source common.sh using the relative path
source "$DIR/common.sh"

#Load local env variables if present.
if [[ -f "$QIT_ROOT/config/local.env" ]]; then
. "$QIT_ROOT/config/local.env"
fi

# Check if QIT_USER and QIT_APP_PASSWORD are set and not empty
if [[ -z $QIT_USER ]] || [[ -z $QIT_PASSWORD ]]; then
echo "QIT_USER or QIT_APP_PASSWORD environment variables are not set or empty. Please set them in the local env file before running the script."
exit 1
fi

export QIT_DISABLE_ONBOARDING=yes

# If QIT_BINARY is not set, default to ./vendor/bin/qit
QIT_BINARY=${QIT_BINARY:-./vendor/bin/qit}

# Add the partner by validating credentials.
if ! $QIT_BINARY list | grep -q 'partner:remove'; then
echo "Adding partner with QIT credentials..."
$QIT_BINARY partner:add --user=$QIT_USER --application_password=$QIT_PASSWORD
if [ $? -ne 0 ]; then
echo "Failed to add partner. Exiting with status 1."
exit 1
fi
fi

# Run the security command
echo "Running security tests..."
$QIT_BINARY run:security woocommerce-payments --zip=woocommerce-payments.zip --wait
if [ $? -ne 0 ]; then
echo "Failed to run security command. Exiting with status 1."
exit 1
echo "Failed to run security command. Exiting with status 1."
exit 1
fi

0 comments on commit 0082367

Please sign in to comment.