From 537c279861a72925054f3dfcfc205e7fb8213c18 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Sun, 15 May 2022 21:06:05 +0100 Subject: [PATCH] Issue #3276652 by andregp, eojthebrave, markie, catch, danflanagan8: AssertMenuActiveTrailTrait should not rely on classy/bartik (cherry picked from commit 3eedc9628e2cc7d14bd4b086c29a5a4d5c2711aa) --- .../Functional/Menu/AssertBreadcrumbTrait.php | 9 ++++++-- .../Menu/AssertMenuActiveTrailTrait.php | 23 +++++++++---------- .../src/Functional/Menu/BreadcrumbTest.php | 2 +- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/core/modules/system/tests/src/Functional/Menu/AssertBreadcrumbTrait.php b/core/modules/system/tests/src/Functional/Menu/AssertBreadcrumbTrait.php index 7be89a1b63a..fbf6485618b 100644 --- a/core/modules/system/tests/src/Functional/Menu/AssertBreadcrumbTrait.php +++ b/core/modules/system/tests/src/Functional/Menu/AssertBreadcrumbTrait.php @@ -32,8 +32,13 @@ trait AssertBreadcrumbTrait { * @param $last_active * (optional) Whether the last link in $tree is expected to be active (TRUE) * or just to be in the active trail (FALSE). + * @param string $active_trail_class + * (optional) The class of the active trail. Defaults to + * 'menu-item--active-trail'. + * @param string $active_class + * (optional) The class of the active element. Defaults to 'is-active'. */ - protected function assertBreadcrumb($goto, array $trail, $page_title = NULL, array $tree = [], $last_active = TRUE) { + protected function assertBreadcrumb($goto, array $trail, $page_title = NULL, array $tree = [], $last_active = TRUE, $active_trail_class = 'menu-item--active-trail', $active_class = 'is-active') { if (isset($goto)) { $this->drupalGet($goto); } @@ -46,7 +51,7 @@ trait AssertBreadcrumbTrait { // Additionally assert active trail in a menu tree output, if given. if ($tree) { - $this->assertMenuActiveTrail($tree, $last_active); + $this->assertMenuActiveTrail($tree, $last_active, $active_trail_class, $active_class); } } diff --git a/core/modules/system/tests/src/Functional/Menu/AssertMenuActiveTrailTrait.php b/core/modules/system/tests/src/Functional/Menu/AssertMenuActiveTrailTrait.php index 634507b0e65..ad39414c5e7 100644 --- a/core/modules/system/tests/src/Functional/Menu/AssertMenuActiveTrailTrait.php +++ b/core/modules/system/tests/src/Functional/Menu/AssertMenuActiveTrailTrait.php @@ -19,8 +19,13 @@ trait AssertMenuActiveTrailTrait { * @param bool $last_active * Whether the last link in $tree is expected to be active (TRUE) * or just to be in the active trail (FALSE). + * @param string $active_trail_class + * (optional) The class of the active trail. Defaults to + * 'menu-item--active-trail'. + * @param string $active_class + * (optional) The class of the active element. Defaults to 'is-active'. */ - protected function assertMenuActiveTrail($tree, $last_active) { + protected function assertMenuActiveTrail($tree, $last_active, $active_trail_class = 'menu-item--active-trail', $active_class = 'is-active') { end($tree); $active_link_path = key($tree); $active_link_title = array_pop($tree); @@ -29,14 +34,9 @@ trait AssertMenuActiveTrailTrait { $i = 0; foreach ($tree as $link_path => $link_title) { $part_xpath = (!$i ? '//' : '/following-sibling::ul/descendant::'); - $part_xpath .= 'li[contains(@class, :classy-class-trail) or contains(@class, :olivero-class-trail)]/a[contains(@href, :href) and contains(text(), :title)]'; - // These active trail classes are added by classy/olivero. This should - // be refactored to work with stark and not depend on any specific - // theme. - // https://www.drupal.org/project/drupal/issues/3276652 + $part_xpath .= 'li[contains(@class, :class-trail)]/a[contains(@href, :href) and contains(text(), :title)]'; $part_args = [ - ':classy-class-trail' => 'menu-item--active-trail', - ':olivero-class-trail' => 'menu__item--active-trail', + ':class-trail' => $active_trail_class, ':href' => Url::fromUri('base:' . $link_path)->toString(), ':title' => $link_title, ]; @@ -53,11 +53,10 @@ trait AssertMenuActiveTrailTrait { $xpath .= '//'; } $xpath_last_active = ($last_active ? 'and contains(@class, :class-active)' : ''); - $xpath .= 'li[contains(@class, :classy-class-trail) or contains(@class, :olivero-class-trail)]/a[contains(@href, :href) ' . $xpath_last_active . 'and contains(text(), :title)]'; + $xpath .= 'li[contains(@class, :class-trail)]/a[contains(@href, :href) ' . $xpath_last_active . 'and contains(text(), :title)]'; $args = [ - ':classy-class-trail' => 'menu-item--active-trail', - ':olivero-class-trail' => 'menu__item--active-trail', - ':class-active' => 'is-active', + ':class-trail' => $active_trail_class, + ':class-active' => $active_class, ':href' => Url::fromUri('base:' . $active_link_path)->toString(), ':title' => $active_link_title, ]; diff --git a/core/modules/system/tests/src/Functional/Menu/BreadcrumbTest.php b/core/modules/system/tests/src/Functional/Menu/BreadcrumbTest.php index 5533d4b8254..a02ae003aa6 100644 --- a/core/modules/system/tests/src/Functional/Menu/BreadcrumbTest.php +++ b/core/modules/system/tests/src/Functional/Menu/BreadcrumbTest.php @@ -294,7 +294,7 @@ class BreadcrumbTest extends BrowserTestBase { $tree += [ $link_path => $link->getTitle(), ]; - $this->assertBreadcrumb($link_path, $trail, $term->getName(), $tree); + $this->assertBreadcrumb($link_path, $trail, $term->getName(), $tree, TRUE, 'menu__item--active-trail'); // Ensure that the tagged node is found. $this->assertSession()->assertEscaped($parent->getTitle());