Skip to content

Commit

Permalink
also thrown an exception when provided store is not enabled
Browse files Browse the repository at this point in the history
Closes #871
  • Loading branch information
danslo committed Oct 21, 2019
1 parent abbfa03 commit e159faf
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,19 @@ public function validate(HttpRequestInterface $request): void
if (!empty($headerValue)) {
$storeCode = ltrim(rtrim($headerValue));
$stores = $this->storeManager->getStores(false, true);
if (!isset($stores[$storeCode])) {
if (strtolower($storeCode) !== 'default') {
$this->storeManager->setCurrentStore(null);
try {
if (!isset($stores[$storeCode]) && strtolower($storeCode) !== 'default') {
throw new GraphQlInputException(
__("Requested store is not found")
__("Requested store is not found.")
);
} elseif (!$stores[$storeCode]->getIsActive()) {
throw new GraphQlInputException(
__("Requested store is not enabled.")
);
}
} catch (GraphQlInputException $e) {
$this->storeManager->setCurrentStore(null);
throw $e;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\StoreGraphQl\Test\Integration;

use Magento\Framework\App\Request\Http;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\Registry;
use Magento\Store\Model\Store;
use Magento\StoreGraphQl\Controller\HttpRequestValidator\StoreValidator;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
use Zend\Http\Headers;

class StoreValidatorTest extends TestCase
{
/**
* @var Store
*/
private $disabledStore;

/**
* @var StoreValidator
*/
private $storeValidator;

/**
* @var Http
*/
private $httpRequest;

public function __construct()
{
parent::__construct();
$this->storeValidator = Bootstrap::getObjectManager()->create(StoreValidator::class);
$this->httpRequest = Bootstrap::getObjectManager()->create(Http::class);
}

protected function setUp()
{
Bootstrap::getObjectManager()->get(Registry::class)->register('isSecureArea', true);

$this->disabledStore = Bootstrap::getObjectManager()->create(Store::class)
->setCode('inactive_store')
->setWebsiteId(1)
->setGroupId(1)
->setName('Inactive Store')
->setIsActive(false)
->save();
}

protected function tearDown()
{
$this->disabledStore->delete();
Bootstrap::getObjectManager()->get(Registry::class)->unregister('isSecureArea');
}

public function testExceptionIsThrownWhenStoreIsNotValid()
{
$this->expectException(GraphQlInputException::class);
$this->httpRequest->setHeaders(Headers::fromString('Store: inactive_store'));
$this->storeValidator->validate($this->httpRequest);
}

public function testExceptionIsThrownWhenStoreDoesNotExist()
{
$this->expectException(GraphQlInputException::class);
$this->httpRequest->setHeaders(Headers::fromString('Store: nonexistent_store'));
$this->storeValidator->validate($this->httpRequest);
}
}

0 comments on commit e159faf

Please sign in to comment.