Skip to content

Commit 3e21f9e

Browse files
ENGCOM-4674: [#19908] locale in rest calls is always default locale #19913
- Merge Pull Request #19913 from jwundrak/magento2:issue/19908-rest-api-locale - Merged commits: 1. 5abd330 2. ef724a6 3. 29b4b39
2 parents 19c4a50 + 29b4b39 commit 3e21f9e

File tree

4 files changed

+81
-20
lines changed

4 files changed

+81
-20
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
namespace Magento\Webapi\Controller;
88

9+
use Magento\Framework\App\ObjectManager;
910
use Magento\Framework\Exception\NoSuchEntityException;
1011

1112
/**
@@ -21,12 +22,23 @@ class PathProcessor
2122
*/
2223
private $storeManager;
2324

25+
/**
26+
* @var \Magento\Framework\Locale\ResolverInterface
27+
*/
28+
private $localeResolver;
29+
2430
/**
2531
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
32+
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
2633
*/
27-
public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
28-
{
34+
public function __construct(
35+
\Magento\Store\Model\StoreManagerInterface $storeManager,
36+
\Magento\Framework\Locale\ResolverInterface $localeResolver = null
37+
) {
2938
$this->storeManager = $storeManager;
39+
$this->localeResolver = $localeResolver ?: ObjectManager::getInstance()->get(
40+
\Magento\Framework\Locale\ResolverInterface::class
41+
);
3042
}
3143

3244
/**
@@ -57,9 +69,11 @@ public function process($pathInfo)
5769
$stores = $this->storeManager->getStores(false, true);
5870
if (isset($stores[$storeCode])) {
5971
$this->storeManager->setCurrentStore($storeCode);
72+
$this->localeResolver->emulate($this->storeManager->getStore()->getId());
6073
$path = '/' . (isset($pathParts[1]) ? $pathParts[1] : '');
6174
} elseif ($storeCode === self::ALL_STORE_CODE) {
6275
$this->storeManager->setCurrentStore(\Magento\Store\Model\Store::ADMIN_CODE);
76+
$this->localeResolver->emulate($this->storeManager->getStore()->getId());
6377
$path = '/' . (isset($pathParts[1]) ? $pathParts[1] : '');
6478
} else {
6579
$path = '/' . implode('/', $pathParts);

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

Lines changed: 24 additions & 8 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,13 +27,22 @@ 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
/**
@@ -47,6 +59,10 @@ public function testAllStoreCode($storeCodeInPath, $storeCodeSet, $setCurrentSto
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
}
@@ -60,7 +76,7 @@ public function processPathDataProvider()
6076
'All store code' => ['all', Store::ADMIN_CODE],
6177
'Default store code' => ['', 'default', 0],
6278
'Arbitrary store code' => [$this->arbitraryStoreCode, $this->arbitraryStoreCode],
63-
'Explicit default store code' => ['default', 'default']
79+
'Explicit default store code' => ['default', 'default'],
6480
];
6581
}
6682
}

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class PathProcessorTest extends \PHPUnit\Framework\TestCase
1515
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Store\Model\StoreManagerInterface */
1616
private $storeManagerMock;
1717

18+
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Locale\ResolverInterface */
19+
private $localeResolverMock;
20+
1821
/** @var \Magento\Webapi\Controller\PathProcessor */
1922
private $model;
2023

@@ -26,16 +29,22 @@ class PathProcessorTest extends \PHPUnit\Framework\TestCase
2629

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

4150
/**

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+
private $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)