From e159faf54d0994102c2b0b3cf2e189c0c43c561d Mon Sep 17 00:00:00 2001 From: Daniel Sloof Date: Mon, 21 Oct 2019 12:24:31 +0200 Subject: [PATCH] also thrown an exception when provided store is not enabled Closes #871 --- .../HttpRequestValidator/StoreValidator.php | 14 +++- .../Test/Integration/StoreValidatorTest.php | 74 +++++++++++++++++++ 2 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 app/code/Magento/StoreGraphQl/Test/Integration/StoreValidatorTest.php diff --git a/app/code/Magento/StoreGraphQl/Controller/HttpRequestValidator/StoreValidator.php b/app/code/Magento/StoreGraphQl/Controller/HttpRequestValidator/StoreValidator.php index 144905d728141..1823a8020f082 100644 --- a/app/code/Magento/StoreGraphQl/Controller/HttpRequestValidator/StoreValidator.php +++ b/app/code/Magento/StoreGraphQl/Controller/HttpRequestValidator/StoreValidator.php @@ -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; } } } diff --git a/app/code/Magento/StoreGraphQl/Test/Integration/StoreValidatorTest.php b/app/code/Magento/StoreGraphQl/Test/Integration/StoreValidatorTest.php new file mode 100644 index 0000000000000..f1a83610ce288 --- /dev/null +++ b/app/code/Magento/StoreGraphQl/Test/Integration/StoreValidatorTest.php @@ -0,0 +1,74 @@ +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); + } +}