Skip to content

Commit

Permalink
Merge pull request #7383 from magento-gl/GL_Mainline_PR_07012022
Browse files Browse the repository at this point in the history
Gl mainline pr 07012022
  • Loading branch information
cpartica authored Jan 13, 2022
2 parents 7658eb2 + 733ae99 commit 50554e7
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 36 deletions.
22 changes: 18 additions & 4 deletions app/code/Magento/GraphQl/Controller/GraphQl.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\GraphQl\Controller;

use Magento\Framework\App\Area;
use Magento\Framework\App\AreaList;
use Magento\Framework\App\FrontControllerInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Request\Http;
Expand Down Expand Up @@ -101,6 +105,11 @@ class GraphQl implements FrontControllerInterface
*/
private $loggerPool;

/**
* @var AreaList
*/
private $areaList;

/**
* @param Response $response
* @param SchemaGeneratorInterface $schemaGenerator
Expand All @@ -112,9 +121,10 @@ class GraphQl implements FrontControllerInterface
* @param QueryFields $queryFields
* @param JsonFactory|null $jsonFactory
* @param HttpResponse|null $httpResponse
* @param ContextFactoryInterface $contextFactory
* @param LogData $logDataHelper
* @param LoggerPool $loggerPool
* @param ContextFactoryInterface|null $contextFactory
* @param LogData|null $logDataHelper
* @param LoggerPool|null $loggerPool
* @param AreaList|null $areaList
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -130,7 +140,8 @@ public function __construct(
HttpResponse $httpResponse = null,
ContextFactoryInterface $contextFactory = null,
LogData $logDataHelper = null,
LoggerPool $loggerPool = null
LoggerPool $loggerPool = null,
AreaList $areaList = null
) {
$this->response = $response;
$this->schemaGenerator = $schemaGenerator;
Expand All @@ -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);
}

/**
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\StoreGraphQl\Controller\HttpHeaderProcessor;
Expand All @@ -11,6 +13,9 @@
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Store\Api\StoreCookieManagerInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Locale\ResolverInterface;
use Psr\Log\LoggerInterface;

/**
* Process the "Store" header entry
Expand All @@ -32,19 +37,35 @@ class StoreProcessor implements HttpHeaderProcessorInterface
*/
private $storeCookieManager;

/**
* @var ResolverInterface
*/
private $localeResolver;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @param StoreManagerInterface $storeManager
* @param HttpContext $httpContext
* @param StoreCookieManagerInterface $storeCookieManager
* @param ResolverInterface $localeResolver
* @param LoggerInterface $logger
*/
public function __construct(
StoreManagerInterface $storeManager,
HttpContext $httpContext,
StoreCookieManagerInterface $storeCookieManager
StoreCookieManagerInterface $storeCookieManager,
ResolverInterface $localeResolver = null,
LoggerInterface $logger = null
) {
$this->storeManager = $storeManager;
$this->httpContext = $httpContext;
$this->storeCookieManager = $storeCookieManager;
$this->localeResolver = $localeResolver ?: ObjectManager::getInstance()->get(ResolverInterface::class);
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
}

/**
Expand All @@ -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();
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,7 @@ class ProductInMultipleStoresTest extends GraphQlAbstract
public function testProductFromSpecificAndDefaultStore()
{
$productSku = 'simple';

$query = <<<QUERY
{
products(filter: {sku: {eq: "{$productSku}"}})
{
items {
id
name
price {
minimalPrice {
amount {
value
currency
}
}
}
sku
type_id
... on PhysicalProductInterface {
weight
}
}
}
}
QUERY;
$query = $this->getQuery($productSku);

/** @var \Magento\Store\Model\Store $store */
$store = ObjectManager::getInstance()->get(\Magento\Store\Model\Store::class);
Expand Down Expand Up @@ -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 <<<QUERY
{
products(filter: {sku: {eq: "{$productSku}"}})
{
items {
id
name
price {
minimalPrice {
amount {
value
currency
}
}
}
sku
type_id
... on PhysicalProductInterface {
weight
}
}
}
}
QUERY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\GraphQl\Catalog;

use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\TestFramework\Helper\CacheCleaner;

/**
* The GraphQl test for product in non default store with different locale
*/
class ProductSearchWithTranslatedMessageTest extends GraphQlAbstract
{
/**
* Test translated error message in non default store
*
* @magentoApiDataFixture Magento/Store/_files/second_store.php
* @magentoApiDataFixture Magento/Translation/_files/catalog_message_translate.php
* @magentoConfigFixture fixture_second_store_store general/locale/code nl_NL
*/
public function testErrorMessageTranslationInNonDefaultLocale()
{
CacheCleaner::clean(['translate', 'config']);
$storeCode = "fixture_second_store";
$header = ['Store' => $storeCode];
$this->expectException(\Exception::class);
$this->expectExceptionMessage('currentPage-waarde moet groter zijn dan 0.');
$this->graphQlQuery($this->getQuery(), [], '', $header);
}

private function getQuery()
{
return <<<QUERY
{
products( currentPage: 0) {
items {
id
name
}
}
}
QUERY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

/** @var \Magento\Translation\Model\ResourceModel\StringUtils $translateString */
$translateString = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->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
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

/** @var \Magento\Translation\Model\ResourceModel\StringUtils $translateString */
$translateString = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Translation\Model\ResourceModel\StringUtils::class
);
$translateString->deleteTranslate('currentPage value must be greater than 0.', "nl_NL", 0);

0 comments on commit 50554e7

Please sign in to comment.