From 0ceb7db6971e70764cb1798b07edc4d80817585c Mon Sep 17 00:00:00 2001 From: Jan Goralski Date: Fri, 22 Dec 2023 11:26:31 +0100 Subject: [PATCH] [Behat][API] Allow setting no accept-language in header [Locale] Prepend default channel locale when resolving by header --- ..._with_the_locale_other_than_the_default.feature | 2 +- src/Sylius/Behat/Context/Api/Shop/CartContext.php | 4 +++- .../Context/RequestHeaderBasedLocaleContext.php | 14 +++++++++++--- .../RequestHeaderBasedLocaleContextSpec.php | 5 +++++ 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/features/checkout/picking_up_the_cart_with_the_locale_other_than_the_default.feature b/features/checkout/picking_up_the_cart_with_the_locale_other_than_the_default.feature index 860e44badd..a1dbe0b743 100644 --- a/features/checkout/picking_up_the_cart_with_the_locale_other_than_the_default.feature +++ b/features/checkout/picking_up_the_cart_with_the_locale_other_than_the_default.feature @@ -18,7 +18,7 @@ Feature: Picking up the cart with the locale other than the default @api Scenario: Picking up the cart without specified locale - When I pick up cart + When I pick up cart without specifying locale And I check details of my cart Then my cart's locale should be "French (France)" diff --git a/src/Sylius/Behat/Context/Api/Shop/CartContext.php b/src/Sylius/Behat/Context/Api/Shop/CartContext.php index 7e2ede3a96..cf4f9196ef 100644 --- a/src/Sylius/Behat/Context/Api/Shop/CartContext.php +++ b/src/Sylius/Behat/Context/Api/Shop/CartContext.php @@ -218,6 +218,7 @@ final class CartContext implements Context /** * @When I pick up (my )cart (again) * @When I pick up cart in the :localeCode locale + * @When I pick up cart without specifying locale * @When the visitor picks up the cart */ public function iPickUpMyCart(?string $localeCode = null): void @@ -776,8 +777,9 @@ final class CartContext implements Context $request = $this->requestFactory->custom( sprintf('%s/shop/orders', $this->apiUrlPrefix), HttpRequest::METHOD_POST, - $localeCode ? ['HTTP_ACCEPT_LANGUAGE' => $localeCode] : [], + ['HTTP_ACCEPT_LANGUAGE' => $localeCode ?? ''], ); + $this->shopClient->executeCustomRequest($request); $tokenValue = $this->responseChecker->getValue($this->shopClient->getLastResponse(), 'tokenValue'); diff --git a/src/Sylius/Bundle/LocaleBundle/Context/RequestHeaderBasedLocaleContext.php b/src/Sylius/Bundle/LocaleBundle/Context/RequestHeaderBasedLocaleContext.php index 80bf3aa759..3b4a7d8803 100644 --- a/src/Sylius/Bundle/LocaleBundle/Context/RequestHeaderBasedLocaleContext.php +++ b/src/Sylius/Bundle/LocaleBundle/Context/RequestHeaderBasedLocaleContext.php @@ -27,6 +27,9 @@ final class RequestHeaderBasedLocaleContext implements LocaleContextInterface { private const NO_CODE_VALID_STUB = 'NO_CODE_VALID_STUB'; + /** @var array $availableLocalesCodes */ + private array $availableLocalesCodes = []; + public function __construct(private RequestStack $requestStack, private LocaleProviderInterface $localeProvider) { } @@ -38,17 +41,22 @@ final class RequestHeaderBasedLocaleContext implements LocaleContextInterface throw new LocaleNotFoundException('No main request available.'); } - $availableLocalesCodes = $this->localeProvider->getAvailableLocalesCodes(); + if ([] === $this->availableLocalesCodes) { + $this->availableLocalesCodes = array_unique(array_merge( + [$this->localeProvider->getDefaultLocaleCode()], + $this->localeProvider->getAvailableLocalesCodes() + )); + } // Request::getPreferredLanguage() returns first available locale code if none matches. To allow detection of // this unwanted behavior, we will prepend special locale code to the list of available locale codes. - $prependedAvailableLocalesCodes = array_merge([self::NO_CODE_VALID_STUB], $availableLocalesCodes); + $prependedAvailableLocalesCodes = array_merge([self::NO_CODE_VALID_STUB], $this->availableLocalesCodes); $bestLocaleCode = $request->getPreferredLanguage($prependedAvailableLocalesCodes); if (self::NO_CODE_VALID_STUB === $bestLocaleCode) { throw new LocaleNotFoundException(sprintf( 'None of the available locales is acceptable: "%s".', - implode('", "', $availableLocalesCodes), + implode('", "', $this->availableLocalesCodes), )); } diff --git a/src/Sylius/Bundle/LocaleBundle/spec/Context/RequestHeaderBasedLocaleContextSpec.php b/src/Sylius/Bundle/LocaleBundle/spec/Context/RequestHeaderBasedLocaleContextSpec.php index e496416de0..9bfdbf3425 100644 --- a/src/Sylius/Bundle/LocaleBundle/spec/Context/RequestHeaderBasedLocaleContextSpec.php +++ b/src/Sylius/Bundle/LocaleBundle/spec/Context/RequestHeaderBasedLocaleContextSpec.php @@ -48,6 +48,7 @@ final class RequestHeaderBasedLocaleContextSpec extends ObjectBehavior $requestStack->getMainRequest()->willReturn($request); + $localeProvider->getDefaultLocaleCode()->willReturn('pl_PL'); $localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']); $this->shouldThrow(LocaleNotFoundException::class)->during('getLocaleCode'); @@ -62,6 +63,7 @@ final class RequestHeaderBasedLocaleContextSpec extends ObjectBehavior $requestStack->getMainRequest()->willReturn($request); + $localeProvider->getDefaultLocaleCode()->willReturn('pl_PL'); $localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']); $this->getLocaleCode()->shouldReturn('de_DE'); @@ -76,6 +78,7 @@ final class RequestHeaderBasedLocaleContextSpec extends ObjectBehavior $requestStack->getMainRequest()->willReturn($request); + $localeProvider->getDefaultLocaleCode()->willReturn('pl_PL'); $localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']); $this->getLocaleCode()->shouldReturn('de_DE'); @@ -90,6 +93,7 @@ final class RequestHeaderBasedLocaleContextSpec extends ObjectBehavior $requestStack->getMainRequest()->willReturn($request); + $localeProvider->getDefaultLocaleCode()->willReturn('pl_PL'); $localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']); $this->getLocaleCode()->shouldReturn('de_DE'); @@ -104,6 +108,7 @@ final class RequestHeaderBasedLocaleContextSpec extends ObjectBehavior $requestStack->getMainRequest()->willReturn($request); + $localeProvider->getDefaultLocaleCode()->willReturn('pl_PL'); $localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']); $this->getLocaleCode()->shouldReturn('de_DE');