Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 33 additions & 76 deletions app/code/Magento/Directory/Test/Unit/Model/CurrencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,83 +138,25 @@ function (array $args) {
*/
public static function getOutputFormatDataProvider(): array
{
// Use dynamic detection for problematic locale/currency combinations!
$ar_DZ_EGP = self::getExpectedFormatForLocale('ar_DZ', 'EGP');

return [
'en_US:USD' => ['en_US', 'USD', '$%s'],
'en_US:PLN' => ['en_US', 'PLN', "PLN\u{00A0}%s"],
'en_US:PKR' => ['en_US', 'PKR', "PKR\u{00A0}%s"],
'af_ZA:VND' => ['af_ZA', 'VND', "\u{20AB}%s"],
'ar_DZ:EGP' => ['ar_DZ', 'EGP', $ar_DZ_EGP],
'ar_SA:USD' => ['ar_SA', 'USD', "%s\u{00A0}US$"],
'ar_SA:LBP' => ['ar_SA', 'LBP', "%s\u{00A0}\u{0644}.\u{0644}.\u{200F}"],
'fa_IR:USD' => ['fa_IR', 'USD', "\u{200E}$%s"],
'ar_KW:USD' => ['ar_KW', 'USD', "%s\u{00A0}US$"],
'bn_BD:IQD' => ['bn_BD', 'IQD', "%s\u{00A0}IQD"],
'ca_ES:VND' => ['ca_ES', 'VND', "%s\u{00A0}\u{20AB}"],
'de_DE:USD' => ['de_DE', 'USD', "%s\u{00A0}$"],
'de_DE:AED' => ['de_DE', 'AED', "%s\u{00A0}AED"],
'es_VE:VEF' => ['es_VE', 'VEF', "Bs.\u{00A0}%s"],
'pl_PL:USD' => ['pl_PL', 'USD', "%s\u{00A0}USD"],
'pl_PL:PLN' => ['pl_PL', 'PLN', "%s\u{00A0}z\u{0142}"],
];
}

/**
* Get expected format for a specific locale/currency combination
* This handles cases where intl extension version affects formatting
*
* @param string $locale
* @param string $currency
* @return string
*/
private static function getExpectedFormatForLocale(string $locale, string $currency): string
{
// Define known problematic combinations and their expected formats
$problematicFormats = [
'ar_DZ:EGP' => [
'old' => "\u{062C}.\u{0645}.\u{200F}\u{00A0}%s",
'new' => "%s\u{00A0}\u{062C}.\u{0645}.\u{200F}"
]
'en_US:USD' => ['en_US', 'USD', '$%s'],
'en_US:PLN' => ['en_US', 'PLN', "PLN\u{00A0}%s"],
'en_US:PKR' => ['en_US', 'PKR', "PKR\u{00A0}%s"],
'af_ZA:VND' => ['af_ZA', 'VND', "\u{20AB}%s"],
'ar_MAD:MAD' => ['ar_MAD', 'MAD', "%s\u{00A0}\u{062F}.\u{0645}.\u{200F}"],
'ar_EG:EGP' => ['ar_EG', 'EGP', "%s\u{00A0}\u{062C}.\u{0645}.\u{200F}"],
'ar_SA:USD' => ['ar_SA', 'USD', "%s\u{00A0}US$"],
'ar_SA:LBP' => ['ar_SA', 'LBP', "%s\u{00A0}\u{0644}.\u{0644}.\u{200F}"],
'fa_IR:USD' => ['fa_IR', 'USD', "\u{200E}$%s"],
'ar_KW:USD' => ['ar_KW', 'USD', "%s\u{00A0}US$"],
'bn_BD:IQD' => ['bn_BD', 'IQD', "%s\u{00A0}IQD"],
'ca_ES:VND' => ['ca_ES', 'VND', "%s\u{00A0}\u{20AB}"],
'de_DE:USD' => ['de_DE', 'USD', "%s\u{00A0}$"],
'de_DE:AED' => ['de_DE', 'AED', "%s\u{00A0}AED"],
'es_VE:VEF' => ['es_VE', 'VEF', "Bs.\u{00A0}%s"],
'pl_PL:USD' => ['pl_PL', 'USD', "%s\u{00A0}USD"],
'pl_PL:PLN' => ['pl_PL', 'PLN', "%s\u{00A0}z\u{0142}"],
];

$key = $locale . ':' . $currency;

if (isset($problematicFormats[$key])) {
// Check if we're using a newer intl version that changes formatting
if (self::isNewerIntlVersion()) {
return $problematicFormats[$key]['new'];
}
return $problematicFormats[$key]['old'];
}

// For non-problematic combinations, return a default format
// This could be enhanced with more specific formats as needed
return "%s";
}

/**
* Check if the current intl extension version uses newer formatting rules
*
* @return bool
*/
private static function isNewerIntlVersion(): bool
{
// Check intl extension version
if (extension_loaded('intl')) {
$intlVersion = INTL_ICU_VERSION ?? '0.0.0';

// ICU 72+ (released around 2022) introduced changes to RTL formatting
// This is a more reliable indicator than PHP version
if (version_compare($intlVersion, '72.0', '>=')) {
return true;
}
}

// Fallback: Check PHP version as a rough indicator
// This is less reliable but provides some backward compatibility
return version_compare(PHP_VERSION, '8.3', '>=');
}

/**
Expand Down Expand Up @@ -296,11 +238,26 @@ public static function getFormatTxtNumberFormatterDataProvider(): array
['precision' => 2, 'symbol' => '#', 'display' => CurrencyData::NO_SYMBOL],
'9,999.99'
],
['he_IL', 'USD', '9999', [], '9,999.00 ‏$'],
['he_IL', 'USD', '9999', [], self::expectedHeIlUsd()],
['he_IL', 'USD', '9999', ['display' => CurrencyData::NO_SYMBOL], '9,999.00'],
];
}

private static function expectedHeIlUsd(): string
{
if (!extension_loaded('intl')) {
return "9,999.00\u{00A0}$";
}

$icu = INTL_ICU_VERSION ?? '0.0.0';
if (version_compare($icu, '72.0', '>=')) {
return "9,999.00\u{00A0}\u{200F}$";
}

return "9,999.00\u{00A0}$";
}


/**
* @dataProvider getFormatTxtZendCurrencyDataProvider
* @param string $price
Expand Down
8 changes: 4 additions & 4 deletions lib/internal/Magento/Framework/Currency/Data/Currency.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2022 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

Expand Down Expand Up @@ -50,7 +50,7 @@ class Currency
/**
* @var array
*/
private $displayLocale = [
private array $displayLocale = [
'AED' => 'ar_AE', 'AFN' => 'fa_AF', 'ALL' => 'sq_AL', 'AMD' => 'hy_AM',
'AOA' => 'pt_AO', 'ARS' => 'es_AR', 'AUD' => 'en_AU', 'AWG' => 'nl_AW',
'AZN' => 'az_Latn_AZ', 'BAM' => 'bs_BA', 'BBD' => 'en_BB', 'BDT' => 'bn_BD',
Expand All @@ -70,7 +70,7 @@ class Currency
'KHR' => 'km_KH', 'KMF' => 'ar_KM', 'KPW' => 'ko_KP', 'KRW' => 'ko_KR',
'KWD' => 'ar_KW', 'KYD' => 'en_KY', 'KZT' => 'ru_KZ', 'LAK' => 'lo_LA',
'LBP' => 'ar_LB', 'LKR' => 'si_LK', 'LRD' => 'en_LR', 'LYD' => 'ar_LY',
'MAD' => 'ar_EH', 'MDL' => 'ro_MD', 'MGA' => 'mg_MG', 'MKD' => 'mk_MK',
'MAD' => 'ar_MA', 'MDL' => 'ro_MD', 'MGA' => 'mg_MG', 'MKD' => 'mk_MK',
'MMK' => 'my_MM', 'MNT' => 'mn_Cyrl_MN', 'MOP' => 'zh_Hant_MO', 'MRU' => 'ar_MR',
'MUR' => 'mfe_MU', 'MVR' => 'dv_MV', 'MWK' => 'ny_MW', 'MXN' => 'es_MX',
'MYR' => 'ms_MY', 'MZN' => 'pt_MZ', 'NAD' => 'kj_NA', 'NGN' => 'en_NG',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2023 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\Framework\Test\Unit\Currency\Data;

use Magento\Framework\Currency\Data\Currency;
use Magento\Framework\Currency\Exception\CurrencyException;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -21,7 +21,7 @@ class CurrencyTest extends TestCase
* @param array $options
* @param string $expectedResult
* @return void
* @throws \Magento\Framework\Currency\Exception\CurrencyException
* @throws CurrencyException
*
* @dataProvider optionsDataProvider
*/
Expand Down Expand Up @@ -120,6 +120,43 @@ public static function optionsDataProvider(): array
],
'expectedResult' => '$12',
],
'format_ar_MA_MAD' => [
'value' => 3,
'options' => [
'locale' => 'ar_MA',
'currency' => 'MAD',
],
'expectedResult' => self::expectedMadFormat(),
],
];
}

/**
* Check if the current intl extension version uses newer formatting rules
*
* @return bool
*/
private static function isNewerIntlVersion(): bool
{
// Check intl extension version
if (extension_loaded('intl')) {
$intlVersion = INTL_ICU_VERSION ?? '0.0.0';

// ICU 72+ (released around 2022) introduced changes to RTL formatting
// This is a more reliable indicator than PHP version
return version_compare($intlVersion, '72.0', '>=');
}

// Fallback: Check PHP version as a rough indicator
// This is less reliable but provides some backward compatibility
return version_compare(PHP_VERSION, '8.3', '>=');
}

private static function expectedMadFormat(): string
{
if (self::isNewerIntlVersion()) {
return "\u{200F}3,00\u{00A0}\u{062F}.\u{0645}.\u{200F}";
}
return "\u{062F}.\u{0645}.\u{200F}\u{00A0}3,00";
}
}