Skip to content

Commit c164ba2

Browse files
committed
Merge branch 'MC-17236' of https://github.com/magento-mpi/magento2ce into pr_2019_07_11
2 parents e40df5d + 2001743 commit c164ba2

File tree

7 files changed

+235
-10
lines changed

7 files changed

+235
-10
lines changed

app/code/Magento/Braintree/Model/LocaleResolver.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use Magento\Framework\Locale\ResolverInterface;
99
use Magento\Braintree\Gateway\Config\PayPal\Config;
1010

11+
/**
12+
* Resolves locale for PayPal Express.
13+
*/
1114
class LocaleResolver implements ResolverInterface
1215
{
1316
/**
@@ -20,6 +23,17 @@ class LocaleResolver implements ResolverInterface
2023
*/
2124
private $config;
2225

26+
/**
27+
* Mapping Magento locales on PayPal locales.
28+
*
29+
* @var array
30+
*/
31+
private $localeMap = [
32+
'zh_Hans_CN' => 'zh_CN',
33+
'zh_Hant_HK' => 'zh_HK',
34+
'zh_Hant_TW' => 'zh_TW'
35+
];
36+
2337
/**
2438
* @param ResolverInterface $resolver
2539
* @param Config $config
@@ -66,10 +80,11 @@ public function setLocale($locale = null)
6680
* Gets store's locale or the `en_US` locale if store's locale does not supported by PayPal.
6781
*
6882
* @return string
83+
* @see https://braintree.github.io/braintree-web/current/PayPalCheckout.html#createPayment
6984
*/
7085
public function getLocale()
7186
{
72-
$locale = $this->resolver->getLocale();
87+
$locale = $this->localeMap[$this->resolver->getLocale()] ?? $this->resolver->getLocale();
7388
$allowedLocales = $this->config->getValue('supported_locales');
7489

7590
return strpos($allowedLocales, $locale) !== false ? $locale : 'en_US';

app/code/Magento/Braintree/Test/Unit/Model/LocaleResolverTest.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,35 @@ public function testSetLocale()
9898
/**
9999
* Test getLocale method
100100
*
101-
* @return void
101+
* @param string $locale
102+
* @param string $expectedLocale
103+
* @dataProvider getLocaleDataProvider
102104
*/
103-
public function testGetLocale()
105+
public function testGetLocale(string $locale, string $expectedLocale)
104106
{
105-
$locale = 'en_TEST';
106-
$allowedLocales = 'en_US,en_GB,en_AU,da_DK,fr_FR,fr_CA,de_DE,zh_HK,it_IT,nl_NL';
107-
$this->resolverMock->expects($this->once())->method('getLocale')->willReturn($locale);
108-
$this->configMock->expects($this->once())->method('getValue')->with('supported_locales')
107+
$allowedLocales = 'en_US,en_GB,en_AU,da_DK,fr_FR,fr_CA,de_DE,zh_HK,it_IT,zh_CN,zh_TW,nl_NL';
108+
$this->resolverMock->method('getLocale')
109+
->willReturn($locale);
110+
$this->configMock->method('getValue')
111+
->with('supported_locales')
109112
->willReturn($allowedLocales);
110-
111-
$expected = 'en_US';
112113
$actual = $this->localeResolver->getLocale();
113-
self::assertEquals($expected, $actual);
114+
115+
self::assertEquals($expectedLocale, $actual);
116+
}
117+
118+
/**
119+
* @return array
120+
*/
121+
public function getLocaleDataProvider(): array
122+
{
123+
return [
124+
['locale' => 'zh_Hans_CN', 'expectedLocale' => 'zh_CN'],
125+
['locale' => 'zh_Hant_HK', 'expectedLocale' => 'zh_HK'],
126+
['locale' => 'zh_Hant_TW', 'expectedLocale' => 'zh_TW'],
127+
['locale' => 'fr_FR', 'expectedLocale' => 'fr_FR'],
128+
['locale' => 'unknown', 'expectedLocale' => 'en_US'],
129+
];
114130
}
115131

116132
/**

app/code/Magento/Paypal/Model/Config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,7 @@ protected function _mapExpressFieldset($fieldName)
15111511
case 'allow_ba_signup':
15121512
case 'in_context':
15131513
case 'merchant_id':
1514+
case 'supported_locales':
15141515
return "payment/{$this->_methodCode}/{$fieldName}";
15151516
default:
15161517
return $this->_mapMethodFieldset($fieldName);
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Paypal\Model\Express;
9+
10+
use Magento\Framework\Locale\ResolverInterface;
11+
use Magento\Paypal\Model\ConfigFactory;
12+
use Magento\Paypal\Model\Config;
13+
14+
/**
15+
* Resolves locale for PayPal Express.
16+
*/
17+
class LocaleResolver implements ResolverInterface
18+
{
19+
/**
20+
* @var ResolverInterface
21+
*/
22+
private $resolver;
23+
24+
/**
25+
* @var Config
26+
*/
27+
private $config;
28+
29+
/**
30+
* Mapping Magento locales on PayPal locales.
31+
*
32+
* @var array
33+
*/
34+
private $localeMap = [
35+
'zh_Hans_CN' => 'zh_CN',
36+
'zh_Hant_HK' => 'zh_HK',
37+
'zh_Hant_TW' => 'zh_TW'
38+
];
39+
40+
/**
41+
* @param ResolverInterface $resolver
42+
* @param ConfigFactory $configFactory
43+
*/
44+
public function __construct(ResolverInterface $resolver, ConfigFactory $configFactory)
45+
{
46+
$this->resolver = $resolver;
47+
$this->config = $configFactory->create();
48+
$this->config->setMethod(Config::METHOD_EXPRESS);
49+
}
50+
51+
/**
52+
* @inheritdoc
53+
*/
54+
public function getDefaultLocalePath()
55+
{
56+
return $this->resolver->getDefaultLocalePath();
57+
}
58+
59+
/**
60+
* @inheritdoc
61+
*/
62+
public function setDefaultLocale($locale)
63+
{
64+
return $this->resolver->setDefaultLocale($locale);
65+
}
66+
67+
/**
68+
* @inheritdoc
69+
*/
70+
public function getDefaultLocale()
71+
{
72+
return $this->resolver->getDefaultLocale();
73+
}
74+
75+
/**
76+
* @inheritdoc
77+
*/
78+
public function setLocale($locale = null)
79+
{
80+
return $this->resolver->setLocale($locale);
81+
}
82+
83+
/**
84+
* Gets store's locale or the `en_US` locale if store's locale does not supported by PayPal.
85+
*
86+
* @return string
87+
* @see https://developer.paypal.com/docs/api/reference/locale-codes/#supported-locale-codes
88+
*/
89+
public function getLocale(): string
90+
{
91+
$locale = $this->localeMap[$this->resolver->getLocale()] ?? $this->resolver->getLocale();
92+
$allowedLocales = $this->config->getValue('supported_locales');
93+
94+
return strpos($allowedLocales, $locale) !== false ? $locale : 'en_US';
95+
}
96+
97+
/**
98+
* @inheritdoc
99+
*/
100+
public function emulate($scopeId)
101+
{
102+
return $this->resolver->emulate($scopeId);
103+
}
104+
105+
/**
106+
* @inheritdoc
107+
*/
108+
public function revert()
109+
{
110+
return $this->resolver->revert();
111+
}
112+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Paypal\Test\Unit\Model\Express;
9+
10+
use Magento\Framework\Locale\ResolverInterface;
11+
use Magento\Paypal\Model\Config;
12+
use Magento\Paypal\Model\ConfigFactory;
13+
use Magento\Paypal\Model\Express\LocaleResolver as ExpressLocaleResolver;
14+
15+
/**
16+
* Class LocaleResolverTest
17+
*/
18+
class LocaleResolverTest extends \PHPUnit\Framework\TestCase
19+
{
20+
/**
21+
* @var \PHPUnit_Framework_MockObject_MockObject|ResolverInterface
22+
*/
23+
private $resolver;
24+
25+
/**
26+
* @var ExpressLocaleResolver
27+
*/
28+
private $model;
29+
30+
protected function setUp()
31+
{
32+
$this->resolver = $this->createMock(ResolverInterface::class);
33+
/** @var Config $config */
34+
$config = $this->createMock(Config::class);
35+
$config->method('getValue')
36+
->with('supported_locales')
37+
->willReturn('zh_CN,zh_HK,zh_TW,fr_FR');
38+
39+
/** @var ConfigFactory $configFactory */
40+
$configFactory = $this->createPartialMock(ConfigFactory::class, ['create']);
41+
$configFactory->method('create')->willReturn($config);
42+
43+
$this->model = new ExpressLocaleResolver($this->resolver, $configFactory);
44+
}
45+
46+
/**
47+
* Tests retrieving locales for PayPal Express.
48+
*
49+
* @param string $locale
50+
* @param string $expectedLocale
51+
* @dataProvider getLocaleDataProvider
52+
*/
53+
public function testGetLocale(string $locale, string $expectedLocale)
54+
{
55+
$this->resolver->method('getLocale')
56+
->willReturn($locale);
57+
58+
$this->assertEquals($expectedLocale, $this->model->getLocale());
59+
}
60+
61+
/**
62+
* @return array
63+
*/
64+
public function getLocaleDataProvider(): array
65+
{
66+
return [
67+
['locale' => 'zh_Hans_CN', 'expectedLocale' => 'zh_CN'],
68+
['locale' => 'zh_Hant_HK', 'expectedLocale' => 'zh_HK'],
69+
['locale' => 'zh_Hant_TW', 'expectedLocale' => 'zh_TW'],
70+
['locale' => 'fr_FR', 'expectedLocale' => 'fr_FR'],
71+
['locale' => 'unknown', 'expectedLocale' => 'en_US'],
72+
];
73+
}
74+
}

app/code/Magento/Paypal/etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<child_authorization_number>1</child_authorization_number>
6969
<verify_peer>1</verify_peer>
7070
<skip_order_review_step>1</skip_order_review_step>
71+
<supported_locales>ar_EG,cs_CZ,da_DK,de_DE,el_GR,en_AU,en_GB,en_IN,en_US,es_ES,es_XC,fi_FI,fr_CA,fr_FR,fr_XC,he_IL,hu_HU,id_ID,it_IT,ja_JP,ko_KR,nl_NL,no_NO,pl_PL,pt_BR,pt_PT,ru_RU,sk_SK,sv_SE,th_TH,zh_CN,zh_HK,zh_TW,zh_XC</supported_locales>
7172
</paypal_express>
7273
<paypal_express_bml>
7374
<model>Magento\Paypal\Model\Bml</model>

app/code/Magento/Paypal/etc/frontend/di.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@
174174
<item name="0" xsi:type="string">CREDIT</item>
175175
</item>
176176
</argument>
177+
<argument name="localeResolver" xsi:type="object">Magento\Paypal\Model\Express\LocaleResolver</argument>
178+
</arguments>
179+
</type>
180+
<type name="Magento\Paypal\Block\Express\InContext\Component">
181+
<arguments>
182+
<argument name="localeResolver" xsi:type="object">Magento\Paypal\Model\Express\LocaleResolver</argument>
177183
</arguments>
178184
</type>
179185
</config>

0 commit comments

Comments
 (0)