From 818d341f431f77acba27c4a339f49ae8f7a55420 Mon Sep 17 00:00:00 2001 From: Stefan Hagspiel Date: Fri, 6 Oct 2023 14:15:14 +0200 Subject: [PATCH 1/2] allow fetching active locales in any context --- src/Context/I18nContext.php | 6 ++- tests/FunctionalDefault/ActiveLocalesCest.php | 36 +++++++++++++ .../ActiveLocalesCest.php | 36 +++++++++++++ .../app/controller/DefaultController.php | 5 ++ .../app/templates/country-selector.html.twig | 53 +++++++++++++++++++ 5 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 tests/FunctionalDefault/ActiveLocalesCest.php create mode 100644 tests/FunctionalDefaultCountry/ActiveLocalesCest.php create mode 100644 tests/_etc/config/app/templates/country-selector.html.twig diff --git a/src/Context/I18nContext.php b/src/Context/I18nContext.php index 531a08d..00a2c83 100644 --- a/src/Context/I18nContext.php +++ b/src/Context/I18nContext.php @@ -145,9 +145,10 @@ public function getActiveLanguages(): array $languageData = $this->mapLanguageInfo($site->getLocale(), $site->getUrl()); $languageData['linkedHref'] = $site->getUrl(); - $languageData['active'] = $site->getLanguageIso() === $this->localeDefinition->getLanguageIso(); + $languageData['active'] = $site->getLocale() === $this->localeDefinition->getLocale(); + foreach ($linkedLanguages as $linkedLanguage) { - if ($linkedLanguage['languageIso'] === $site->getLanguageIso()) { + if ($linkedLanguage['locale'] === $site->getLocale()) { $languageData['linkedHref'] = $linkedLanguage['url']; break; @@ -192,6 +193,7 @@ public function getActiveCountries(): array $countryData = []; if (!empty($validCountries)) { foreach ($validCountries as $country) { + if (is_null($country['isoCode'])) { continue; } diff --git a/tests/FunctionalDefault/ActiveLocalesCest.php b/tests/FunctionalDefault/ActiveLocalesCest.php new file mode 100644 index 0000000..117bb73 --- /dev/null +++ b/tests/FunctionalDefault/ActiveLocalesCest.php @@ -0,0 +1,36 @@ +haveASite('test-domain1.test'); + + $document1 = $I->haveAPageDocumentForSite($site, 'de', ['action' => 'languageSelectorAction'], 'de'); + $document2 = $I->haveAPageDocumentForSite($site, 'en', ['action' => 'languageSelectorAction'], 'en'); + + $I->haveTwoConnectedDocuments($document1, $document2); + + $I->amOnPageWithLocale('http://test-domain1.test/en', 'en'); + + $I->seeElement('select option[selected][value="http://test-domain1.test/en"]'); + } + + public function testActiveCountriesSelector(FunctionalTester $I): void + { + $site = $I->haveASite('test-domain1.test'); + + $document1 = $I->haveAPageDocumentForSite($site, 'de', ['action' => 'languageCountrySelectorAction'], 'de'); + $document2 = $I->haveAPageDocumentForSite($site, 'en', ['action' => 'languageCountrySelectorAction'], 'en'); + + $I->haveTwoConnectedDocuments($document1, $document2); + + $I->amOnPageWithLocale('http://test-domain1.test/en', 'en'); + + $I->seeElement('li[data-country="GLOBAL"] li[data-language="en"]'); + } +} diff --git a/tests/FunctionalDefaultCountry/ActiveLocalesCest.php b/tests/FunctionalDefaultCountry/ActiveLocalesCest.php new file mode 100644 index 0000000..a1a0ec8 --- /dev/null +++ b/tests/FunctionalDefaultCountry/ActiveLocalesCest.php @@ -0,0 +1,36 @@ +haveASite('test-domain1.test'); + + $document1 = $I->haveAPageDocumentForSite($site, 'de-de', ['action' => 'languageSelectorAction'], 'de_DE'); + $document2 = $I->haveAPageDocumentForSite($site, 'de-ch', ['action' => 'languageSelectorAction'], 'de_CH'); + + $I->haveTwoConnectedDocuments($document1, $document2); + + $I->amOnPageWithLocale('http://test-domain1.test/de-de', 'de'); + + $I->seeElement('select option[selected][value="http://test-domain1.test/de-de"]'); + } + + public function testActiveCountriesSelector(FunctionalTester $I): void + { + $site = $I->haveASite('test-domain1.test'); + + $document1 = $I->haveAPageDocumentForSite($site, 'de-de', ['action' => 'languageCountrySelectorAction'], 'de_DE'); + $document2 = $I->haveAPageDocumentForSite($site, 'de-ch', ['action' => 'languageCountrySelectorAction'], 'de_CH'); + + $I->haveTwoConnectedDocuments($document1, $document2); + + $I->amOnPageWithLocale('http://test-domain1.test/de-ch', 'de'); + + $I->seeElement('li[data-country="CH"] li[data-language="de"]'); + } +} diff --git a/tests/_etc/config/app/controller/DefaultController.php b/tests/_etc/config/app/controller/DefaultController.php index aa3bbb7..776ca5c 100644 --- a/tests/_etc/config/app/controller/DefaultController.php +++ b/tests/_etc/config/app/controller/DefaultController.php @@ -25,4 +25,9 @@ public function languageSelectorAction(Request $request): Response { return $this->render('default/language-selector.html.twig'); } + + public function languageCountrySelectorAction(Request $request): Response + { + return $this->render('default/country-selector.html.twig'); + } } diff --git a/tests/_etc/config/app/templates/country-selector.html.twig b/tests/_etc/config/app/templates/country-selector.html.twig new file mode 100644 index 0000000..0ca3133 --- /dev/null +++ b/tests/_etc/config/app/templates/country-selector.html.twig @@ -0,0 +1,53 @@ + + + + + Test Page for I18n + {{ pimcore_head_meta() }} + {{ pimcore_head_link() }} + + +
+ {% set current_context = i18n_current_context() %} + {% if current_context is not null %} + {% set countries = current_context.activeCountries %} + {% if countries is iterable %} + + {% endif %} + {% endif %} + + +
+ {% if document is defined %} + {{ document.id }} + {% endif %} +
+ {% block content %} + {{ pimcore_areablock('dachcomBundleTest') }} + {% endblock %} +
+ + From 15a4eff51fd041c3d19cc0c5cf91bfc935396de5 Mon Sep 17 00:00:00 2001 From: Stefan Hagspiel Date: Sat, 7 Oct 2023 15:32:47 +0200 Subject: [PATCH 2/2] add test template to config --- tests/_etc/config.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/_etc/config.yaml b/tests/_etc/config.yaml index c88ae0e..b994bbc 100644 --- a/tests/_etc/config.yaml +++ b/tests/_etc/config.yaml @@ -10,6 +10,7 @@ setup_files: - { path: app/controller/DefaultController.php, dest: ./src/Controller/ } - { path: app/templates/default.html.twig, dest: ./templates/default/ } - { path: app/templates/language-selector.html.twig, dest: ./templates/default/ } + - { path: app/templates/country-selector.html.twig, dest: ./templates/default/ } preload_files: - { path: Services/TestAlternateListener.php } - { path: Services/TestLinkGenerator.php }