From bb253b053951049f05a6ab019bca726494e72b4c Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Mon, 13 Apr 2009 12:18:52 +0000
Subject: [PATCH] - Patch #423690 by Berdir: fixed bug in menu_link_maintain().
  With tests.

---
 includes/menu.inc                         |  7 ++--
 modules/simpletest/tests/menu.test        | 46 +++++++++++++++++++++++
 modules/simpletest/tests/menu_test.module |  6 +++
 3 files changed, 56 insertions(+), 3 deletions(-)

diff --git a/includes/menu.inc b/includes/menu.inc
index bb804fd74f9d..b9d0fba5ae93 100644
--- a/includes/menu.inc
+++ b/includes/menu.inc
@@ -2349,9 +2349,10 @@ function menu_link_maintain($module, $op, $link_path, $link_title) {
         ->condition('link_path', $link_path)
         ->condition('customized', 0)
         ->condition('module', $module)
-        ->execute();
-      foreach ($result as $item) {
-        menu_cache_clear($item['menu_name']);
+        ->groupBy('menu_name')
+        ->execute()->fetchCol();
+      foreach ($result as $menu_name) {
+        menu_cache_clear($menu_name);
       }
       break;
     case 'delete':
diff --git a/modules/simpletest/tests/menu.test b/modules/simpletest/tests/menu.test
index 882b3831f7bb..68257b3b1157 100644
--- a/modules/simpletest/tests/menu.test
+++ b/modules/simpletest/tests/menu.test
@@ -29,6 +29,52 @@ class MenuIncTestCase extends DrupalWebTestCase {
     $this->assertNoText(t('A title with @placeholder', array('@placeholder' => 'some other text')), t('Text with placeholder substitutions not found.'));
   }
 
+  /**
+   * Tests for menu_link_maintain().
+   */
+  function testMenuLinkMaintain() {
+    $admin_user = $this->drupalCreateUser(array('administer site configuration'));
+    $this->drupalLogin($admin_user);
+
+    // Create three menu items.
+    menu_link_maintain('menu_test', 'insert', 'menu_test_maintain/1', 'Menu link #1');
+    menu_link_maintain('menu_test', 'insert', 'menu_test_maintain/1', 'Menu link #1-1');
+    menu_link_maintain('menu_test', 'insert', 'menu_test_maintain/2', 'Menu link #2');
+
+    // Move second link to the main-menu, to test caching later on.
+    db_update('menu_links')
+      ->fields(array('menu_name' => 'main-menu'))
+      ->condition('link_title', 'Menu link #1-1')
+      ->condition('customized', 0)
+      ->condition('module', 'menu_test')
+      ->execute();
+    menu_cache_clear('main-menu');
+
+    // Load front page.
+    $this->drupalGet('node');
+    $this->assertLink(t('Menu link #1'), 0, 'Found menu link #1');
+    $this->assertLink(t('Menu link #1-1'), 0, 'Found menu link #1-1');
+    $this->assertLink(t('Menu link #2'), 0, 'Found menu link #2');
+
+    // Rename all links for the given path.
+    menu_link_maintain('menu_test', 'update', 'menu_test_maintain/1', 'Menu link updated');
+    // Load a different page to be sure that we have up to date information.
+    $this->drupalGet('menu_test_maintain/1');
+    $this->assertLink(t('Menu link updated'), 0, t('Found updated menu link'));
+    $this->assertNoLink(t('Menu link #1'), 0, t('Not found menu link #1'));
+    $this->assertNoLink(t('Menu link #1'), 0, t('Not found menu link #1-1'));
+    $this->assertLink(t('Menu link #2'), 0, t('Found menu link #2'));
+
+    // Delete all links for the given path.
+    menu_link_maintain('menu_test', 'delete', 'menu_test_maintain/1', '');
+    // Load a different page to be sure that we have up to date information.
+    $this->drupalGet('menu_test_maintain/2');
+    $this->assertNoLink(t('Menu link updated'), 0, t('Not found deleted menu link'));
+    $this->assertNoLink(t('Menu link #1'), 0, t('Not found menu link #1'));
+    $this->assertNoLink(t('Menu link #1'), 0, t('Not found menu link #1-1'));
+    $this->assertLink(t('Menu link #2'), 0, t('Found menu link #2'));
+  }
+
   /**
    * Tests for menu_name parameter for hook_menu().
    */
diff --git a/modules/simpletest/tests/menu_test.module b/modules/simpletest/tests/menu_test.module
index 7bb5af5e52c1..1b8daa4e876a 100644
--- a/modules/simpletest/tests/menu_test.module
+++ b/modules/simpletest/tests/menu_test.module
@@ -25,6 +25,12 @@ function menu_test_menu() {
     'access arguments' => array('access content'),
   );
 
+  // Hidden link for menu_link_maintain tests
+  $items['menu_test_maintain/%'] = array(
+    'title' => 'Menu maintain test',
+    'page callback' => 'node_page_default',
+    'access arguments' => array('access content'),
+   );
   // Hierarchical tests.
   $items['menu-test/hierarchy/parent'] = array(
     'title' => 'Parent menu router',
-- 
GitLab