From 49ffcb8d33d68821a6e28ef9d20fdc116834e77b Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Fri, 17 Feb 2023 08:24:51 +0000
Subject: [PATCH] =?UTF-8?q?Issue=20#3030403=20by=20Krzysztof=20Doma=C5=84s?=
 =?UTF-8?q?ki,=20ranjith=5Fkumar=5Fk=5Fu,=20larowlan:=20Improve=20the=20re?=
 =?UTF-8?q?adability=20of=20the=20code=20in=20assertBreadcrumbParts?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../Functional/Menu/AssertBreadcrumbTrait.php | 21 ++++++++++++-------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/core/modules/system/tests/src/Functional/Menu/AssertBreadcrumbTrait.php b/core/modules/system/tests/src/Functional/Menu/AssertBreadcrumbTrait.php
index fbf6485618b6..ee8db8a93a11 100644
--- a/core/modules/system/tests/src/Functional/Menu/AssertBreadcrumbTrait.php
+++ b/core/modules/system/tests/src/Functional/Menu/AssertBreadcrumbTrait.php
@@ -65,14 +65,10 @@ protected function assertBreadcrumb($goto, array $trail, $page_title = NULL, arr
   protected function assertBreadcrumbParts($trail) {
     // Compare paths with actual breadcrumb.
     $parts = $this->getBreadcrumbParts();
+    $found = $parts;
     $pass = TRUE;
-    // Fail if there is no breadcrumb and we have a trail.
-    if (!empty($trail) && empty($parts)) {
-      $pass = FALSE;
-    }
-    // There may be more than one breadcrumb on the page. If $trail is empty
-    // this test would go into an infinite loop, so we need to check that too.
-    while ($trail && !empty($parts)) {
+
+    if (!empty($trail) && !empty($parts)) {
       foreach ($trail as $path => $title) {
         // If the path is empty, generate the path from the <front> route.  If
         // the path does not start with a leading slash, then run it through
@@ -91,11 +87,20 @@ protected function assertBreadcrumbParts($trail) {
         $pass = ($pass && $part['href'] === $url && $part['text'] === Html::escape($title));
       }
     }
+    elseif (!empty($trail) && empty($parts) || empty($trail) && !empty($parts)) {
+      // Fail if there is no breadcrumb and we have a trail or breadcrumb
+      // exists but trail is empty.
+      $pass = FALSE;
+    }
+
     // No parts must be left, or an expected "Home" will always pass.
     $pass = ($pass && empty($parts));
 
-    $this->assertTrue($pass, new FormattableMarkup('Breadcrumb %parts found on @path.', [
+    $this->assertTrue($pass, new FormattableMarkup('Expected breadcrumb %parts on @path but found %found.', [
       '%parts' => implode(' » ', $trail),
+      '%found' => implode(' » ', array_map(function (array $item) {
+        return $item['text'];
+      }, $found)),
       '@path' => $this->getUrl(),
     ]));
   }
-- 
GitLab