Skip to content

Commit 5abd330

Browse files
author
Julian Wundrak
committed
[#19908] locale in rest calls is always default locale
1 parent c307d25 commit 5abd330

File tree

3 files changed

+65
-16
lines changed

3 files changed

+65
-16
lines changed

app/code/Magento/Webapi/Controller/PathProcessor.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,21 @@ class PathProcessor
2121
*/
2222
private $storeManager;
2323

24+
/**
25+
* @var \Magento\Framework\Locale\ResolverInterface
26+
*/
27+
private $localeResolver;
28+
2429
/**
2530
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
31+
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
2632
*/
27-
public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
28-
{
33+
public function __construct(
34+
\Magento\Store\Model\StoreManagerInterface $storeManager,
35+
\Magento\Framework\Locale\ResolverInterface $localeResolver
36+
) {
2937
$this->storeManager = $storeManager;
38+
$this->localeResolver = $localeResolver;
3039
}
3140

3241
/**
@@ -57,9 +66,11 @@ public function process($pathInfo)
5766
$stores = $this->storeManager->getStores(false, true);
5867
if (isset($stores[$storeCode])) {
5968
$this->storeManager->setCurrentStore($storeCode);
69+
$this->localeResolver->emulate($this->storeManager->getStore()->getId());
6070
$path = '/' . (isset($pathParts[1]) ? $pathParts[1] : '');
6171
} elseif ($storeCode === self::ALL_STORE_CODE) {
6272
$this->storeManager->setCurrentStore(\Magento\Store\Model\Store::ADMIN_CODE);
73+
$this->localeResolver->emulate($this->storeManager->getStore()->getId());
6374
$path = '/' . (isset($pathParts[1]) ? $pathParts[1] : '');
6475
} else {
6576
$path = '/' . implode('/', $pathParts);

app/code/Magento/Webapi/Test/Unit/Controller/PathProcessorTest.php

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class PathProcessorTest extends \PHPUnit\Framework\TestCase
1313
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Store\Model\StoreManagerInterface */
1414
private $storeManagerMock;
1515

16+
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Locale\ResolverInterface */
17+
private $localeResolverMock;
18+
1619
/** @var \Magento\Webapi\Controller\PathProcessor */
1720
private $model;
1821

@@ -24,29 +27,42 @@ class PathProcessorTest extends \PHPUnit\Framework\TestCase
2427

2528
protected function setUp()
2629
{
27-
$this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
28-
->disableOriginalConstructor()
29-
->getMock();
30-
$this->storeManagerMock->expects($this->once())
31-
->method('getStores')
32-
->willReturn([$this->arbitraryStoreCode => 'store object', 'default' => 'default store object']);
33-
$this->model = new \Magento\Webapi\Controller\PathProcessor($this->storeManagerMock);
30+
$store = $this->createMock(\Magento\Store\Api\Data\StoreInterface::class);
31+
$store->method('getId')->willReturn(2);
32+
33+
$this->storeManagerMock = $this->createConfiguredMock(
34+
\Magento\Store\Model\StoreManagerInterface::class,
35+
[
36+
'getStores' => [$this->arbitraryStoreCode => 'store object', 'default' => 'default store object'],
37+
'getStore' => $store,
38+
]
39+
);
40+
$this->storeManagerMock->expects($this->once())->method('getStores');
41+
42+
$this->localeResolverMock = $this->createMock(\Magento\Framework\Locale\ResolverInterface::class);
43+
$this->localeResolverMock->method('emulate')->with(2);
44+
45+
$this->model = new \Magento\Webapi\Controller\PathProcessor($this->storeManagerMock, $this->localeResolverMock);
3446
}
3547

3648
/**
3749
* @dataProvider processPathDataProvider
3850
*
3951
* @param string $storeCodeInPath
4052
* @param string $storeCodeSet
41-
* @param int $setCurrentStoreCallCtr
53+
* @param int $setCurrentStoreCallCtr
4254
*/
4355
public function testAllStoreCode($storeCodeInPath, $storeCodeSet, $setCurrentStoreCallCtr = 1)
4456
{
45-
$storeCodeInPath = !$storeCodeInPath ?: '/' . $storeCodeInPath; // add leading slash if store code not empty
46-
$inPath = 'rest' . $storeCodeInPath . $this->endpointPath;
57+
$storeCodeInPath = !$storeCodeInPath ? : '/' . $storeCodeInPath; // add leading slash if store code not empty
58+
$inPath = 'rest' . $storeCodeInPath . $this->endpointPath;
4759
$this->storeManagerMock->expects($this->exactly($setCurrentStoreCallCtr))
4860
->method('setCurrentStore')
4961
->with($storeCodeSet);
62+
if($setCurrentStoreCallCtr > 0) {
63+
$this->localeResolverMock->expects($this->once())
64+
->method('emulate');
65+
}
5066
$result = $this->model->process($inPath);
5167
$this->assertSame($this->endpointPath, $result);
5268
}
@@ -57,10 +73,10 @@ public function testAllStoreCode($storeCodeInPath, $storeCodeSet, $setCurrentSto
5773
public function processPathDataProvider()
5874
{
5975
return [
60-
'All store code' => ['all', Store::ADMIN_CODE],
61-
'Default store code' => ['', 'default', 0],
62-
'Arbitrary store code' => [$this->arbitraryStoreCode, $this->arbitraryStoreCode],
63-
'Explicit default store code' => ['default', 'default']
76+
'All store code' => ['all', Store::ADMIN_CODE],
77+
'Default store code' => ['', 'default', 0],
78+
'Arbitrary store code' => [$this->arbitraryStoreCode, $this->arbitraryStoreCode],
79+
'Explicit default store code' => ['default', 'default'],
6480
];
6581
}
6682
}

dev/tests/integration/testsuite/Magento/Webapi/Controller/PathProcessorTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ class PathProcessorTest extends \PHPUnit\Framework\TestCase
1515
*/
1616
protected $storeManager;
1717

18+
/**
19+
* @var \Magento\Framework\Locale\ResolverInterface::class
20+
*/
21+
protected $localeResolver;
22+
1823
/**
1924
* @var \Magento\Webapi\Controller\PathProcessor
2025
*/
@@ -25,6 +30,7 @@ protected function setUp()
2530
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
2631
$this->storeManager = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
2732
$this->storeManager->reinitStores();
33+
$this->localeResolver = $objectManager->get(\Magento\Framework\Locale\ResolverInterface::class);
2834
$this->pathProcessor = $objectManager->get(\Magento\Webapi\Controller\PathProcessor::class);
2935
}
3036

@@ -59,4 +65,20 @@ public function testProcessWithoutStoreCode()
5965
$this->assertEquals($path, $result);
6066
$this->assertEquals('default', $this->storeManager->getStore()->getCode());
6167
}
68+
69+
/**
70+
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
71+
* @magentoConfigFixture default_store general/locale/code en_US
72+
* @magentoConfigFixture fixturestore_store general/locale/code de_DE
73+
*/
74+
public function testProcessWithValidStoreCodeApplyLocale()
75+
{
76+
$locale = 'de_DE';
77+
$storeCode = 'fixturestore';
78+
$basePath = "rest/{$storeCode}";
79+
$path = $basePath . '/V1/customerAccounts/createCustomer';
80+
$this->pathProcessor->process($path);
81+
$this->assertEquals($locale, $this->localeResolver->getLocale());
82+
$this->assertNotEquals('en_US', $this->localeResolver->getLocale());
83+
}
6284
}

0 commit comments

Comments
 (0)