Skip to content

Commit 031b784

Browse files
author
Anna Bukatar
committed
Merge branch 'ACP2E-508' of https://github.com/magento-l3/magento2ce into PR-2022-02-22
2 parents a9710ae + c9ac241 commit 031b784

File tree

2 files changed

+93
-43
lines changed

2 files changed

+93
-43
lines changed

app/code/Magento/Directory/Model/Currency.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ class Currency extends \Magento\Framework\Model\AbstractModel
2828
/**
2929
* CONFIG path constants
3030
*/
31-
const XML_PATH_CURRENCY_ALLOW = 'currency/options/allow';
31+
public const XML_PATH_CURRENCY_ALLOW = 'currency/options/allow';
3232

33-
const XML_PATH_CURRENCY_DEFAULT = 'currency/options/default';
33+
public const XML_PATH_CURRENCY_DEFAULT = 'currency/options/default';
3434

35-
const XML_PATH_CURRENCY_BASE = 'currency/options/base';
35+
public const XML_PATH_CURRENCY_BASE = 'currency/options/base';
3636

3737
/**
3838
* @var Filter
@@ -439,7 +439,7 @@ private function formatCurrency(string $price, array $options): string
439439

440440
if ((array_key_exists(LocaleCurrency::CURRENCY_OPTION_DISPLAY, $options)
441441
&& $options[LocaleCurrency::CURRENCY_OPTION_DISPLAY] === \Magento\Framework\Currency::NO_SYMBOL)) {
442-
$formattedCurrency = preg_replace(['/[^0-9.,۰٫]+/', '/ /'], '', $formattedCurrency);
442+
$formattedCurrency = str_replace(' ', '', $formattedCurrency);
443443
}
444444

445445
return preg_replace('/^\s+|\s+$/u', '', $formattedCurrency);
@@ -453,13 +453,11 @@ private function formatCurrency(string $price, array $options): string
453453
*/
454454
private function getNumberFormatter(array $options): \Magento\Framework\NumberFormatter
455455
{
456-
$key = 'currency_' . hash(
457-
'sha256',
458-
($this->localeResolver->getLocale() . $this->serializer->serialize($options))
459-
);
456+
$locale = $this->localeResolver->getLocale() . ($this->getCode() ? '@currency=' . $this->getCode() : '');
457+
$key = 'currency_' . hash('sha256', $locale . $this->serializer->serialize($options));
460458
if (!isset($this->numberFormatterCache[$key])) {
461459
$this->numberFormatter = $this->numberFormatterFactory->create(
462-
['locale' => $this->localeResolver->getLocale(), 'style' => \NumberFormatter::CURRENCY]
460+
['locale' => $locale, 'style' => \NumberFormatter::CURRENCY]
463461
);
464462

465463
$this->setOptions($options);

app/code/Magento/Directory/Test/Unit/Model/CurrencyTest.php

Lines changed: 86 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
namespace Magento\Directory\Test\Unit\Model;
99

10-
use Magento\Directory\Model\Currency;
10+
use Magento\Directory\Model\Currency as CurrencyModel;
11+
use Magento\Framework\Currency;
1112
use Magento\Framework\Locale\CurrencyInterface;
1213
use Magento\Framework\Locale\ResolverInterface as LocalResolverInterface;
1314
use Magento\Framework\NumberFormatterFactory;
@@ -19,10 +20,13 @@
1920
class CurrencyTest extends TestCase
2021
{
2122
/**
22-
* @var Currency
23+
* @var CurrencyModel
2324
*/
2425
protected $currency;
2526

27+
/**
28+
* @var string
29+
*/
2630
protected $currencyCode = 'USD';
2731

2832
/**
@@ -63,7 +67,7 @@ protected function setUp(): void
6367

6468
$objectManager = new ObjectManager($this);
6569
$this->currency = $objectManager->getObject(
66-
Currency::class,
70+
CurrencyModel::class,
6771
[
6872
'localeCurrency' => $this->localeCurrencyMock,
6973
'currencyFilterFactory' => $currencyFilterFactory,
@@ -81,7 +85,7 @@ public function testGetCurrencySymbol(): void
8185
{
8286
$currencySymbol = '$';
8387

84-
$currencyMock = $this->getMockBuilder(\Magento\Framework\Currency::class)
88+
$currencyMock = $this->getMockBuilder(Currency::class)
8589
->disableOriginalConstructor()
8690
->getMock();
8791
$currencyMock->expects(self::once())
@@ -97,22 +101,31 @@ public function testGetCurrencySymbol(): void
97101

98102
/**
99103
* @dataProvider getOutputFormatDataProvider
100-
* @param $expected
101-
* @param $locale
104+
* @param string $locale
105+
* @param string $currency
106+
* @param string $expected
102107
*/
103-
public function testGetOutputFormat($expected, $locale): void
108+
public function testGetOutputFormat(string $locale, string $currency, string $expected): void
104109
{
105-
$this->localeResolver->method('getLocale')->willReturn($locale);
110+
$this->localeResolver->expects(self::atLeastOnce())
111+
->method('getLocale')
112+
->willReturn($locale);
106113
$this->numberFormatterFactory
114+
->expects(self::exactly(2))
107115
->method('create')
108-
->with(['locale' => $locale, 'style' => 2])
109-
->willReturn(new \Magento\Framework\NumberFormatter($locale, 2));
116+
->willReturnCallback(
117+
function (array $args) {
118+
return new \Magento\Framework\NumberFormatter($args['locale'], $args['style']);
119+
}
120+
);
110121
$this->serializer->method('serialize')->willReturnMap(
111122
[
112123
[[], '[]'],
113124
[['display' => 1], '{"display":1}']
114125
]
115126
);
127+
128+
$this->currency->load($currency);
116129
self::assertEquals($expected, $this->currency->getOutputFormat());
117130
}
118131

@@ -124,42 +137,58 @@ public function testGetOutputFormat($expected, $locale): void
124137
public function getOutputFormatDataProvider(): array
125138
{
126139
return [
127-
'no_unicode' => [
128-
'expected' => '$%s',
129-
'locale' => 'en_US'
130-
],
131-
'arabic_unicode' => [
132-
'expected' => json_decode('"\u200E"') . '$%s',
133-
'locale' => 'fa_IR'
134-
]
140+
'en_US:USD' => ['en_US', 'USD', '$%s'],
141+
'en_US:PLN' => ['en_US', 'PLN', "PLN\u{00A0}%s"],
142+
'en_US:PKR' => ['en_US', 'PKR', "PKR\u{00A0}%s"],
143+
'af_ZA:VND' => ['af_ZA', 'VND', "\u{20AB}%s"],
144+
'ar_DZ:EGP' => ['ar_DZ', 'EGP', "\u{062C}.\u{0645}.\u{200F}\u{00A0}%s"],
145+
'ar_SA:USD' => ['ar_SA', 'USD', "%s\u{00A0}US$"],
146+
'ar_SA:LBP' => ['ar_SA', 'LBP', "%s\u{00A0}\u{0644}.\u{0644}.\u{200F}"],
147+
'fa_IR:USD' => ['fa_IR', 'USD', "\u{200E}$%s"],
148+
'ar_KW:USD' => ['ar_KW', 'USD', "%s\u{00A0}US$"],
149+
'bn_BD:IQD' => ['bn_BD', 'IQD', "%s\u{00A0}IQD"],
150+
'ca_ES:VND' => ['ca_ES', 'VND', "%s\u{00A0}\u{20AB}"],
151+
'de_DE:USD' => ['de_DE', 'USD', "%s\u{00A0}$"],
152+
'de_DE:AED' => ['de_DE', 'AED', "%s\u{00A0}AED"],
153+
'es_VE:VEF' => ['es_VE', 'VEF', "Bs.\u{00A0}%s"],
154+
'pl_PL:USD' => ['pl_PL', 'USD', "%s\u{00A0}USD"],
155+
'pl_PL:PLN' => ['pl_PL', 'PLN', "%s\u{00A0}z\u{0142}"],
135156
];
136157
}
137158

138159
/**
139160
* @dataProvider getFormatTxtNumberFormatterDataProvider
161+
* @param string $locale
162+
* @param string $currency
140163
* @param string $price
141164
* @param array $options
142-
* @param string $locale
143165
* @param string $expected
144166
*/
145167
public function testFormatTxtWithNumberFormatter(
168+
string $locale,
169+
string $currency,
146170
string $price,
147171
array $options,
148-
string $locale,
149172
string $expected
150173
): void {
151-
$this->localeResolver->expects(self::exactly(2))->method('getLocale')->willReturn($locale);
174+
$this->localeResolver->expects(self::once())
175+
->method('getLocale')
176+
->willReturn($locale);
152177
$this->numberFormatterFactory
153178
->expects(self::once())
154179
->method('create')
155-
->with(['locale' => $locale, 'style' => 2])
156-
->willReturn(new \Magento\Framework\NumberFormatter($locale, 2));
180+
->willReturnCallback(
181+
function (array $args) {
182+
return new \Magento\Framework\NumberFormatter($args['locale'], $args['style']);
183+
}
184+
);
157185
$this->serializer->method('serialize')->willReturnMap(
158186
[
159187
[[], '[]']
160188
]
161189
);
162190

191+
$this->currency->load($currency);
163192
self::assertEquals($expected, $this->currency->formatTxt($price, $options));
164193
}
165194

@@ -171,16 +200,39 @@ public function testFormatTxtWithNumberFormatter(
171200
public function getFormatTxtNumberFormatterDataProvider(): array
172201
{
173202
return [
174-
['9999', [], 'en_US', '$9,999.00'],
175-
['9999', ['display' => \Magento\Framework\Currency::NO_SYMBOL, 'precision' => 2], 'en_US', '9,999.00'],
176-
['9999', ['display' => \Magento\Framework\Currency::NO_SYMBOL], 'en_US', '9,999.00'],
177-
[' 9999', ['display' => \Magento\Framework\Currency::NO_SYMBOL], 'en_US', '9,999.00'],
178-
['9999', ['precision' => 1], 'en_US', '$9,999.0'],
179-
['9999', ['precision' => 2, 'symbol' => '#'], 'en_US', '#9,999.00'],
203+
['en_US', 'USD', '9999', [], '$9,999.00'],
204+
['en_US', 'EUR', '9999', [], '€9,999.00'],
205+
['en_US', 'LBP', '9999', [], "LBP\u{00A0}9,999"],
206+
['ar_AE', 'USD', '9', [], "\u{0669}\u{066B}\u{0660}\u{0660}\u{00A0}US$"],
207+
['ar_AE', 'AED', '9', [], "\u{0669}\u{066B}\u{0660}\u{0660}\u{00A0}\u{062F}.\u{0625}.\u{200F}"],
208+
['de_DE', 'USD', '9999', [], "9.999,00\u{00A0}$"],
209+
['de_DE', 'EUR', '9999', [], "9.999,00\u{00A0}"],
210+
['en_US', 'USD', '9999', ['display' => Currency::NO_SYMBOL, 'precision' => 2], '9,999.00'],
211+
['en_US', 'USD', '9999', ['display' => Currency::NO_SYMBOL], '9,999.00'],
212+
['en_US', 'PLN', '9999', ['display' => Currency::NO_SYMBOL], '9,999.00'],
213+
['en_US', 'LBP', '9999', ['display' => Currency::NO_SYMBOL], '9,999'],
214+
[
215+
'ar_AE',
216+
'USD',
217+
'9999',
218+
['display' => Currency::NO_SYMBOL],
219+
"\u{0669}\u{066C}\u{0669}\u{0669}\u{0669}\u{066B}\u{0660}\u{0660}"
220+
],
221+
[
222+
'ar_AE',
223+
'AED',
224+
'9999',
225+
['display' => Currency::NO_SYMBOL],
226+
"\u{0669}\u{066C}\u{0669}\u{0669}\u{0669}\u{066B}\u{0660}\u{0660}"
227+
],
228+
['en_US', 'USD', ' 9999', ['display' => Currency::NO_SYMBOL], '9,999.00'],
229+
['en_US', 'USD', '9999', ['precision' => 1], '$9,999.0'],
230+
['en_US', 'USD', '9999', ['precision' => 2, 'symbol' => '#'], '#9,999.00'],
180231
[
181-
'9999.99',
182-
['precision' => 2, 'symbol' => '#', 'display' => \Magento\Framework\Currency::NO_SYMBOL],
183232
'en_US',
233+
'USD',
234+
'9999.99',
235+
['precision' => 2, 'symbol' => '#', 'display' => Currency::NO_SYMBOL],
184236
'9,999.99'
185237
],
186238
];
@@ -217,11 +269,11 @@ public function testFormatTxtWithZendCurrency(string $price, array $options, str
217269
public function getFormatTxtZendCurrencyDataProvider(): array
218270
{
219271
return [
220-
['9999', ['display' => \Magento\Framework\Currency::USE_SYMBOL, 'foo' => 'bar'], '$9,999.00'],
221-
['9999', ['display' => \Magento\Framework\Currency::USE_SHORTNAME, 'foo' => 'bar'], 'USD9,999.00'],
272+
['9999', ['display' => Currency::USE_SYMBOL, 'foo' => 'bar'], '$9,999.00'],
273+
['9999', ['display' => Currency::USE_SHORTNAME, 'foo' => 'bar'], 'USD9,999.00'],
222274
['9999', ['currency' => 'USD'], '$9,999.00'],
223275
['9999', ['currency' => 'CNY'], 'CN¥9,999.00'],
224-
['9999', ['locale' => 'fr_FR'], '999,00 $']
276+
['9999', ['locale' => 'fr_FR'], "9\u{00A0}999,00\u{00A0}$"]
225277
];
226278
}
227279
}

0 commit comments

Comments
 (0)