diff --git a/app/code/Magento/GraphQl/Controller/GraphQl.php b/app/code/Magento/GraphQl/Controller/GraphQl.php index 29389030e7684..2eb5aa81c17b5 100644 --- a/app/code/Magento/GraphQl/Controller/GraphQl.php +++ b/app/code/Magento/GraphQl/Controller/GraphQl.php @@ -1,12 +1,16 @@ response = $response; $this->schemaGenerator = $schemaGenerator; @@ -145,6 +156,7 @@ public function __construct( $this->contextFactory = $contextFactory ?: ObjectManager::getInstance()->get(ContextFactoryInterface::class); $this->logDataHelper = $logDataHelper ?: ObjectManager::getInstance()->get(LogData::class); $this->loggerPool = $loggerPool ?: ObjectManager::getInstance()->get(LoggerPool::class); + $this->areaList = $areaList ?: ObjectManager::getInstance()->get(AreaList::class); } /** @@ -156,6 +168,8 @@ public function __construct( */ public function dispatch(RequestInterface $request): ResponseInterface { + $this->areaList->getArea(Area::AREA_GRAPHQL)->load(Area::PART_TRANSLATE); + $statusCode = 200; $jsonResult = $this->jsonFactory->create(); $data = $this->getDataFromRequest($request); diff --git a/app/code/Magento/StoreGraphQl/Controller/HttpHeaderProcessor/StoreProcessor.php b/app/code/Magento/StoreGraphQl/Controller/HttpHeaderProcessor/StoreProcessor.php index 7999a96917cde..b9cf231a58378 100644 --- a/app/code/Magento/StoreGraphQl/Controller/HttpHeaderProcessor/StoreProcessor.php +++ b/app/code/Magento/StoreGraphQl/Controller/HttpHeaderProcessor/StoreProcessor.php @@ -1,8 +1,10 @@ storeManager = $storeManager; $this->httpContext = $httpContext; $this->storeCookieManager = $storeCookieManager; + $this->localeResolver = $localeResolver ?: ObjectManager::getInstance()->get(ResolverInterface::class); + $this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class); } /** @@ -55,12 +76,19 @@ public function __construct( * @param string $headerValue * @return void */ - public function processHeaderValue(string $headerValue) : void + public function processHeaderValue(string $headerValue): void { if (!empty($headerValue)) { $storeCode = ltrim(rtrim($headerValue)); - $this->storeManager->setCurrentStore($storeCode); - $this->updateContext($storeCode); + try { + $this->localeResolver->emulate($this->storeManager->getStore($storeCode)->getId()); + // $this->storeManager->getStore($storeCode) throws error with non existing stores + // and logged in the catch + $this->storeManager->setCurrentStore($storeCode); + $this->updateContext($storeCode); + } catch (\Exception $e) { + $this->logger->error($e->getMessage()); + } } elseif (!$this->isAlreadySet()) { $storeCode = $this->storeCookieManager->getStoreCodeFromCookie() ?: $this->storeManager->getDefaultStoreView()->getCode(); @@ -75,7 +103,7 @@ public function processHeaderValue(string $headerValue) : void * @param string $storeCode * @return void */ - private function updateContext(string $storeCode) : void + private function updateContext(string $storeCode): void { $this->httpContext->setValue( StoreManagerInterface::CONTEXT_STORE, diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductInMultipleStoresTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductInMultipleStoresTest.php index 7c7212b9b9b26..3d0108bc97615 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductInMultipleStoresTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductInMultipleStoresTest.php @@ -25,31 +25,7 @@ class ProductInMultipleStoresTest extends GraphQlAbstract public function testProductFromSpecificAndDefaultStore() { $productSku = 'simple'; - - $query = <<getQuery($productSku); /** @var \Magento\Store\Model\Store $store */ $store = ObjectManager::getInstance()->get(\Magento\Store\Model\Store::class); @@ -89,12 +65,53 @@ public function testProductFromSpecificAndDefaultStore() $response['products']['items'][0]['name'], 'Product in the default store should be returned' ); + } - // use case for invalid storeCode + /** + * Test a product from a non existing store + * + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + */ + public function testProductFromNonExistingStore() + { $nonExistingStoreCode = "non_existent_store"; $headerMapInvalidStoreCode = ['Store' => $nonExistingStoreCode]; $this->expectException(\Exception::class); $this->expectExceptionMessage('Requested store is not found'); - $this->graphQlQuery($query, [], '', $headerMapInvalidStoreCode); + $this->graphQlQuery($this->getQuery('simple'), [], '', $headerMapInvalidStoreCode); + } + + /** + * Return GraphQL query string by productSku + * + * @param string $productSku + * @return string + */ + private function getQuery(string $productSku): string + { + return << $storeCode]; + $this->expectException(\Exception::class); + $this->expectExceptionMessage('currentPage-waarde moet groter zijn dan 0.'); + $this->graphQlQuery($this->getQuery(), [], '', $header); + } + + private function getQuery() + { + return <<create( + \Magento\Translation\Model\ResourceModel\StringUtils::class +); +$translateString->saveTranslate( + 'currentPage value must be greater than 0.', + 'currentPage-waarde moet groter zijn dan 0.', + "nl_NL", + 0 +); diff --git a/dev/tests/integration/testsuite/Magento/Translation/_files/catalog_message_translate_rollback.php b/dev/tests/integration/testsuite/Magento/Translation/_files/catalog_message_translate_rollback.php new file mode 100644 index 0000000000000..319ecbead796d --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Translation/_files/catalog_message_translate_rollback.php @@ -0,0 +1,13 @@ +create( + \Magento\Translation\Model\ResourceModel\StringUtils::class +); +$translateString->deleteTranslate('currentPage value must be greater than 0.', "nl_NL", 0);