Skip to content

Commit 6bd2d4a

Browse files
committed
Merge remote-tracking branch 'community_person_rikwillems/31351-translations-graphql' into AC-1946-latest
2 parents e8c31f5 + 4635cf1 commit 6bd2d4a

File tree

6 files changed

+177
-36
lines changed

6 files changed

+177
-36
lines changed

app/code/Magento/GraphQl/Controller/GraphQl.php

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<?php
2+
23
/**
34
* Copyright © Magento, Inc. All rights reserved.
45
* See COPYING.txt for license details.
56
*/
7+
68
declare(strict_types=1);
79

810
namespace Magento\GraphQl\Controller;
911

12+
use Magento\Framework\App\Area;
13+
use Magento\Framework\App\AreaList;
1014
use Magento\Framework\App\FrontControllerInterface;
1115
use Magento\Framework\App\ObjectManager;
1216
use Magento\Framework\App\Request\Http;
@@ -101,6 +105,11 @@ class GraphQl implements FrontControllerInterface
101105
*/
102106
private $loggerPool;
103107

108+
/**
109+
* @var AreaList
110+
*/
111+
private $areaList;
112+
104113
/**
105114
* @param Response $response
106115
* @param SchemaGeneratorInterface $schemaGenerator
@@ -112,9 +121,10 @@ class GraphQl implements FrontControllerInterface
112121
* @param QueryFields $queryFields
113122
* @param JsonFactory|null $jsonFactory
114123
* @param HttpResponse|null $httpResponse
115-
* @param ContextFactoryInterface $contextFactory
116-
* @param LogData $logDataHelper
117-
* @param LoggerPool $loggerPool
124+
* @param ContextFactoryInterface|null $contextFactory
125+
* @param LogData|null $logDataHelper
126+
* @param LoggerPool|null $loggerPool
127+
* @param AreaList|null $areaList
118128
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
119129
*/
120130
public function __construct(
@@ -130,7 +140,8 @@ public function __construct(
130140
HttpResponse $httpResponse = null,
131141
ContextFactoryInterface $contextFactory = null,
132142
LogData $logDataHelper = null,
133-
LoggerPool $loggerPool = null
143+
LoggerPool $loggerPool = null,
144+
AreaList $areaList = null
134145
) {
135146
$this->response = $response;
136147
$this->schemaGenerator = $schemaGenerator;
@@ -145,6 +156,7 @@ public function __construct(
145156
$this->contextFactory = $contextFactory ?: ObjectManager::getInstance()->get(ContextFactoryInterface::class);
146157
$this->logDataHelper = $logDataHelper ?: ObjectManager::getInstance()->get(LogData::class);
147158
$this->loggerPool = $loggerPool ?: ObjectManager::getInstance()->get(LoggerPool::class);
159+
$this->areaList = $areaList ?: ObjectManager::getInstance()->get(AreaList::class);
148160
}
149161

150162
/**
@@ -156,6 +168,8 @@ public function __construct(
156168
*/
157169
public function dispatch(RequestInterface $request): ResponseInterface
158170
{
171+
$this->areaList->getArea(Area::AREA_GRAPHQL)->load(Area::PART_TRANSLATE);
172+
159173
$statusCode = 200;
160174
$jsonResult = $this->jsonFactory->create();
161175
$data = $this->getDataFromRequest($request);

app/code/Magento/StoreGraphQl/Controller/HttpHeaderProcessor/StoreProcessor.php

+33-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
2+
23
/**
34
* Copyright © Magento, Inc. All rights reserved.
45
* See COPYING.txt for license details.
56
*/
7+
68
declare(strict_types=1);
79

810
namespace Magento\StoreGraphQl\Controller\HttpHeaderProcessor;
@@ -11,6 +13,9 @@
1113
use Magento\Store\Model\StoreManagerInterface;
1214
use Magento\Framework\App\Http\Context as HttpContext;
1315
use Magento\Store\Api\StoreCookieManagerInterface;
16+
use Magento\Framework\App\ObjectManager;
17+
use Magento\Framework\Locale\ResolverInterface;
18+
use Psr\Log\LoggerInterface;
1419

1520
/**
1621
* Process the "Store" header entry
@@ -32,19 +37,35 @@ class StoreProcessor implements HttpHeaderProcessorInterface
3237
*/
3338
private $storeCookieManager;
3439

40+
/**
41+
* @var ResolverInterface
42+
*/
43+
private $localeResolver;
44+
45+
/**
46+
* @var LoggerInterface
47+
*/
48+
private $logger;
49+
3550
/**
3651
* @param StoreManagerInterface $storeManager
3752
* @param HttpContext $httpContext
3853
* @param StoreCookieManagerInterface $storeCookieManager
54+
* @param ResolverInterface $localeResolver
55+
* @param LoggerInterface $logger
3956
*/
4057
public function __construct(
4158
StoreManagerInterface $storeManager,
4259
HttpContext $httpContext,
43-
StoreCookieManagerInterface $storeCookieManager
60+
StoreCookieManagerInterface $storeCookieManager,
61+
ResolverInterface $localeResolver = null,
62+
LoggerInterface $logger = null
4463
) {
4564
$this->storeManager = $storeManager;
4665
$this->httpContext = $httpContext;
4766
$this->storeCookieManager = $storeCookieManager;
67+
$this->localeResolver = $localeResolver ?: ObjectManager::getInstance()->get(ResolverInterface::class);
68+
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
4869
}
4970

5071
/**
@@ -55,12 +76,19 @@ public function __construct(
5576
* @param string $headerValue
5677
* @return void
5778
*/
58-
public function processHeaderValue(string $headerValue) : void
79+
public function processHeaderValue(string $headerValue): void
5980
{
6081
if (!empty($headerValue)) {
6182
$storeCode = ltrim(rtrim($headerValue));
62-
$this->storeManager->setCurrentStore($storeCode);
63-
$this->updateContext($storeCode);
83+
try {
84+
$this->localeResolver->emulate($this->storeManager->getStore($storeCode)->getId());
85+
// $this->storeManager->getStore($storeCode) throws error with non existing stores
86+
// and logged in the catch
87+
$this->storeManager->setCurrentStore($storeCode);
88+
$this->updateContext($storeCode);
89+
} catch (\Exception $e) {
90+
$this->logger->error($e->getMessage());
91+
}
6492
} elseif (!$this->isAlreadySet()) {
6593
$storeCode = $this->storeCookieManager->getStoreCodeFromCookie()
6694
?: $this->storeManager->getDefaultStoreView()->getCode();
@@ -75,7 +103,7 @@ public function processHeaderValue(string $headerValue) : void
75103
* @param string $storeCode
76104
* @return void
77105
*/
78-
private function updateContext(string $storeCode) : void
106+
private function updateContext(string $storeCode): void
79107
{
80108
$this->httpContext->setValue(
81109
StoreManagerInterface::CONTEXT_STORE,

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductInMultipleStoresTest.php

+44-27
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,7 @@ class ProductInMultipleStoresTest extends GraphQlAbstract
2525
public function testProductFromSpecificAndDefaultStore()
2626
{
2727
$productSku = 'simple';
28-
29-
$query = <<<QUERY
30-
{
31-
products(filter: {sku: {eq: "{$productSku}"}})
32-
{
33-
items {
34-
id
35-
name
36-
price {
37-
minimalPrice {
38-
amount {
39-
value
40-
currency
41-
}
42-
}
43-
}
44-
sku
45-
type_id
46-
... on PhysicalProductInterface {
47-
weight
48-
}
49-
}
50-
}
51-
}
52-
QUERY;
28+
$query = $this->getQuery($productSku);
5329

5430
/** @var \Magento\Store\Model\Store $store */
5531
$store = ObjectManager::getInstance()->get(\Magento\Store\Model\Store::class);
@@ -89,12 +65,53 @@ public function testProductFromSpecificAndDefaultStore()
8965
$response['products']['items'][0]['name'],
9066
'Product in the default store should be returned'
9167
);
68+
}
9269

93-
// use case for invalid storeCode
70+
/**
71+
* Test a product from a non existing store
72+
*
73+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
74+
*/
75+
public function testProductFromNonExistingStore()
76+
{
9477
$nonExistingStoreCode = "non_existent_store";
9578
$headerMapInvalidStoreCode = ['Store' => $nonExistingStoreCode];
9679
$this->expectException(\Exception::class);
9780
$this->expectExceptionMessage('Requested store is not found');
98-
$this->graphQlQuery($query, [], '', $headerMapInvalidStoreCode);
81+
$this->graphQlQuery($this->getQuery('simple'), [], '', $headerMapInvalidStoreCode);
82+
}
83+
84+
/**
85+
* Return GraphQL query string by productSku
86+
*
87+
* @param string $productSku
88+
* @return string
89+
*/
90+
private function getQuery(string $productSku): string
91+
{
92+
return <<<QUERY
93+
{
94+
products(filter: {sku: {eq: "{$productSku}"}})
95+
{
96+
items {
97+
id
98+
name
99+
price {
100+
minimalPrice {
101+
amount {
102+
value
103+
currency
104+
}
105+
}
106+
}
107+
sku
108+
type_id
109+
... on PhysicalProductInterface {
110+
weight
111+
}
112+
}
113+
}
114+
}
115+
QUERY;
99116
}
100117
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Magento\GraphQl\Catalog;
11+
12+
use Magento\TestFramework\TestCase\GraphQlAbstract;
13+
use Magento\TestFramework\Helper\CacheCleaner;
14+
15+
/**
16+
* The GraphQl test for product in non default store with different locale
17+
*/
18+
class ProductSearchWithTranslatedMessageTest extends GraphQlAbstract
19+
{
20+
/**
21+
* Test translated error message in non default store
22+
*
23+
* @magentoApiDataFixture Magento/Store/_files/second_store.php
24+
* @magentoApiDataFixture Magento/Translation/_files/catalog_message_translate.php
25+
* @magentoConfigFixture fixture_second_store_store general/locale/code nl_NL
26+
*/
27+
public function testErrorMessageTranslationInNonDefaultLocale()
28+
{
29+
CacheCleaner::clean(['translate', 'config']);
30+
$storeCode = "fixture_second_store";
31+
$header = ['Store' => $storeCode];
32+
$this->expectException(\Exception::class);
33+
$this->expectExceptionMessage('currentPage-waarde moet groter zijn dan 0.');
34+
$this->graphQlQuery($this->getQuery(), [], '', $header);
35+
}
36+
37+
private function getQuery()
38+
{
39+
return <<<QUERY
40+
{
41+
products( currentPage: 0) {
42+
items {
43+
id
44+
name
45+
}
46+
}
47+
}
48+
QUERY;
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
/** @var \Magento\Translation\Model\ResourceModel\StringUtils $translateString */
11+
$translateString = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
12+
\Magento\Translation\Model\ResourceModel\StringUtils::class
13+
);
14+
$translateString->saveTranslate(
15+
'currentPage value must be greater than 0.',
16+
'currentPage-waarde moet groter zijn dan 0.',
17+
"nl_NL",
18+
0
19+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
declare(strict_types=1);
8+
9+
/** @var \Magento\Translation\Model\ResourceModel\StringUtils $translateString */
10+
$translateString = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
11+
\Magento\Translation\Model\ResourceModel\StringUtils::class
12+
);
13+
$translateString->deleteTranslate('currentPage value must be greater than 0.', "nl_NL", 0);

0 commit comments

Comments
 (0)