From ebfea7276060c5a6a9ab45dfb95a4422724f5227 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Fri, 8 Sep 2023 14:44:34 +0300 Subject: [PATCH 1/5] UHF-8738: prevent showing active trail on menu item in subdomain --- src/ExternalMenuTreeBuilder.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/ExternalMenuTreeBuilder.php b/src/ExternalMenuTreeBuilder.php index 070ed9d..3c26321 100644 --- a/src/ExternalMenuTreeBuilder.php +++ b/src/ExternalMenuTreeBuilder.php @@ -192,6 +192,13 @@ private function inActiveTrail(object $item): bool { $currentPath = parse_url($request->getUri(), PHP_URL_PATH); $linkPath = parse_url($item->url->getUri(), PHP_URL_PATH); + // Subdomain cannot be in active trail. + $host = parse_url($item->url->getUri(), PHP_URL_HOST); + $host = str_replace('www.', '', $host); + if (explode('.',$host)[0] != 'hel') { + return FALSE; + } + // We don't care about the domain when comparing URLs because the // site might be served from multiple different domains. if ($linkPath === $currentPath) { From c10aeabb9ba576579f6e27a5322fefdbfa8a39ca Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 11 Sep 2023 08:32:59 +0300 Subject: [PATCH 2/5] UHF-8738: code fixes --- src/ExternalMenuTreeBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ExternalMenuTreeBuilder.php b/src/ExternalMenuTreeBuilder.php index 3c26321..5dd0ee1 100644 --- a/src/ExternalMenuTreeBuilder.php +++ b/src/ExternalMenuTreeBuilder.php @@ -195,7 +195,7 @@ private function inActiveTrail(object $item): bool { // Subdomain cannot be in active trail. $host = parse_url($item->url->getUri(), PHP_URL_HOST); $host = str_replace('www.', '', $host); - if (explode('.',$host)[0] != 'hel') { + if (explode('.', $host)[0] != 'hel') { return FALSE; } From 3790fd33fa9df05aa91b1df97e8ea1def08d26d1 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 11 Sep 2023 08:57:50 +0300 Subject: [PATCH 3/5] UHF-8738: easier way to check if external link --- src/ExternalMenuTreeBuilder.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/ExternalMenuTreeBuilder.php b/src/ExternalMenuTreeBuilder.php index 5dd0ee1..d48952c 100644 --- a/src/ExternalMenuTreeBuilder.php +++ b/src/ExternalMenuTreeBuilder.php @@ -183,7 +183,11 @@ private function createLink( * Returns true or false. */ private function inActiveTrail(object $item): bool { - if ($item->url->isRouted() && $item->url->getRouteName() === '') { + if ( + $item->url->isRouted() && + $item->url->getRouteName() === '' || + $item->external + ) { return FALSE; } if (!$request = $this->requestStack->getCurrentRequest()) { @@ -192,13 +196,6 @@ private function inActiveTrail(object $item): bool { $currentPath = parse_url($request->getUri(), PHP_URL_PATH); $linkPath = parse_url($item->url->getUri(), PHP_URL_PATH); - // Subdomain cannot be in active trail. - $host = parse_url($item->url->getUri(), PHP_URL_HOST); - $host = str_replace('www.', '', $host); - if (explode('.', $host)[0] != 'hel') { - return FALSE; - } - // We don't care about the domain when comparing URLs because the // site might be served from multiple different domains. if ($linkPath === $currentPath) { From 8ca50a8b99b90684a3a0fa45abccb77cc8a9fb8e Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Tue, 12 Sep 2023 08:55:16 +0300 Subject: [PATCH 4/5] UHF-8738: added subdomain to test fixtures --- fixtures/api-v1-global-menu-en.json | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/fixtures/api-v1-global-menu-en.json b/fixtures/api-v1-global-menu-en.json index bb20a0c..6323200 100644 --- a/fixtures/api-v1-global-menu-en.json +++ b/fixtures/api-v1-global-menu-en.json @@ -34,6 +34,22 @@ "attributes": {}, "weight": 0, "sub_tree": [ + { + "url": "https://paatokset.hel.fi/en/urban-environment-and-traffic/cycling", + "id": "menu_link_content:df660188-d56f-4cb4-9539-32945792da18", + "name": "Decisions", + "parentId": "base:health_and_social_services", + "attributes": {}, + "external": true, + "hasItems": false, + "expanded": false, + "parents": [ + "menu_link_content:df660188-d56f-4cb4-9539-32945792da18", + "base:health_and_social_services" + ], + "weight": -49, + "sub_tree": [] + }, { "url": "https://helfi-kymp.docker.so/en/urban-environment-and-traffic/parking", "id": "menu_link_content:7c9ddcc2-4d07-4785-8940-046b4cb85fb4", From c97664ee3eb778c0e8352a12e428692e8a58d614 Mon Sep 17 00:00:00 2001 From: tuutti Date: Tue, 12 Sep 2023 09:10:17 +0300 Subject: [PATCH 5/5] UHF-8738: Fixed internal domain --- tests/src/Kernel/ExternalMenuTreeBuilderTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/src/Kernel/ExternalMenuTreeBuilderTest.php b/tests/src/Kernel/ExternalMenuTreeBuilderTest.php index bc6f6db..1d035ad 100644 --- a/tests/src/Kernel/ExternalMenuTreeBuilderTest.php +++ b/tests/src/Kernel/ExternalMenuTreeBuilderTest.php @@ -4,6 +4,7 @@ namespace Drupal\Tests\helfi_navigation\Kernel; +use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\helfi_navigation\ExternalMenuTreeBuilder; use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; @@ -20,6 +21,16 @@ class ExternalMenuTreeBuilderTest extends MenuTreeBuilderTestBase { use ProphecyTrait; + /** + * {@inheritdoc} + */ + public function register(ContainerBuilder $container) : void { + $container->setParameter('helfi_api_base.internal_domains', [ + 'localhost', + ]); + parent::register($container); + } + /** * Constructs a new external menu tree. *