From 63df811eea293cfc8632724c6fd257e313f86070 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Mon, 18 Sep 2023 20:55:25 +0100
Subject: [PATCH] Issue #3386682 by Spokje: [random test failure]
 MenuUiTest::testMenuAdministration

---
 .../tests/src/Functional/MenuUiTest.php       | 44 ++++++++++++-------
 1 file changed, 27 insertions(+), 17 deletions(-)

diff --git a/core/modules/menu_ui/tests/src/Functional/MenuUiTest.php b/core/modules/menu_ui/tests/src/Functional/MenuUiTest.php
index 0c6488b80c0c..4caa3c9c1e91 100644
--- a/core/modules/menu_ui/tests/src/Functional/MenuUiTest.php
+++ b/core/modules/menu_ui/tests/src/Functional/MenuUiTest.php
@@ -186,30 +186,40 @@ public function testMenuAdministration() {
     }
 
     // Test alphabetical order without pager.
-    $menus = [];
+    $menu_entities = [];
     for ($i = 1; $i < 6; $i++) {
-      $menus[] = strtolower($this->getRandomGenerator()->name());
-    }
-    sort($menus);
-    foreach ($menus as $menu) {
-      Menu::create(['id' => $menu, 'label' => $menu])->save();
+      $menu = strtolower($this->getRandomGenerator()->name());
+      $menu_entity = Menu::create(['id' => $menu, 'label' => $menu]);
+      $menu_entities[] = $menu_entity;
+      $menu_entity->save();
     }
+    uasort($menu_entities, [Menu::class, 'sort']);
+    $menu_entities = array_values($menu_entities);
     $this->drupalGet('/admin/structure/menu');
     $base_path = parse_url($this->baseUrl, PHP_URL_PATH) ?? '';
     $first_link = $this->assertSession()->elementExists('css', 'tbody tr:nth-of-type(1) a');
     $last_link = $this->assertSession()->elementExists('css', 'tbody tr:nth-of-type(5) a');
-    $this->assertEquals($first_link->getAttribute('href'), sprintf('%s/admin/structure/menu/manage/%s', $base_path, $menus[0]));
-    $this->assertEquals($last_link->getAttribute('href'), sprintf('%s/admin/structure/menu/manage/%s', $base_path, $menus[4]));
+    $this->assertEquals($first_link->getAttribute('href'), sprintf('%s/admin/structure/menu/manage/%s', $base_path, $menu_entities[0]->label()));
+    $this->assertEquals($last_link->getAttribute('href'), sprintf('%s/admin/structure/menu/manage/%s', $base_path, $menu_entities[4]->label()));
+
     // Test alphabetical order with pager.
-    $new_menus = [];
+    $new_menu_entities = [];
     for ($i = 1; $i < 61; $i++) {
-      $new_menus[] = strtolower($this->getRandomGenerator()->name());
-    }
-    foreach ($new_menus as $menu) {
-      Menu::create(['id' => $menu, 'label' => $menu])->save();
+      $new_menu = strtolower($this->getRandomGenerator()->name());
+      $new_menu_entity = Menu::create(['id' => $new_menu, 'label' => $new_menu]);
+      $new_menu_entities[] = $new_menu_entity;
+      $new_menu_entity->save();
     }
-    $menus = array_merge($menus, $new_menus);
-    sort($menus);
+    $menu_entities = array_merge($menu_entities, $new_menu_entities);
+
+    // To accommodate the current non-natural sorting of the pager, we have to
+    // first non-natural sort the array of menu entities, and then do a
+    // natural-sort on the ones that are on page 1.
+    sort($menu_entities);
+    $menu_entities_page_one = array_slice($menu_entities, 50, 64, TRUE);
+    uasort($menu_entities_page_one, [Menu::class, 'sort']);
+    $menu_entities_page_one = array_values($menu_entities_page_one);
+
     $this->drupalGet('/admin/structure/menu', [
       'query' => [
         'page' => 1,
@@ -217,8 +227,8 @@ public function testMenuAdministration() {
     ]);
     $first_link = $this->assertSession()->elementExists('css', 'tbody tr:nth-of-type(1) a');
     $last_link = $this->assertSession()->elementExists('css', 'tbody tr:nth-of-type(15) a');
-    $this->assertEquals($first_link->getAttribute('href'), sprintf('%s/admin/structure/menu/manage/%s', $base_path, $menus[50]));
-    $this->assertEquals($last_link->getAttribute('href'), sprintf('%s/admin/structure/menu/manage/%s', $base_path, $menus[64]));
+    $this->assertEquals($first_link->getAttribute('href'), sprintf('%s/admin/structure/menu/manage/%s', $base_path, $menu_entities_page_one[0]->label()));
+    $this->assertEquals($last_link->getAttribute('href'), sprintf('%s/admin/structure/menu/manage/%s', $base_path, $menu_entities_page_one[14]->label()));
   }
 
   /**
-- 
GitLab