=== modified file 'includes/menu.inc'
--- includes/menu.inc	2010-08-05 07:56:50 +0000
+++ includes/menu.inc	2010-08-22 14:06:50 +0000
@@ -3004,25 +3004,31 @@ function _menu_link_move_children($item,
   $query->fields(array('menu_name' => $item['menu_name']));
 
   $p = 'p1';
+  $expressions = array();
   for ($i = 1; $i <= $item['depth']; $p = 'p' . ++$i) {
-    $query->fields(array($p => $item[$p]));
+    $expressions[] = array($p, ":p_$i", array(":p_$i" => $item[$p]));
   }
   $j = $existing_item['depth'] + 1;
   while ($i <= MENU_MAX_DEPTH && $j <= MENU_MAX_DEPTH) {
-    $query->expression('p' . $i++, 'p' . $j++);
+    $expressions[] = array('p' . $i++, 'p' . $j++, array());
   }
   while ($i <= MENU_MAX_DEPTH) {
-    $query->fields(array('p' . $i++ => 0));
+    $expressions[] = array('p' . $i++, 0, array());
   }
 
   $shift = $item['depth'] - $existing_item['depth'];
-  if ($shift < 0) {
-    $query->expression('depth', 'depth - :depth', array(':depth' => -$shift));
+  if ($shift > 0) {
+    // The order of expressions must be reversed so the new values don't
+    // overwrite the old ones before they can be used because "Single-table
+    // UPDATE assignments are generally evaluated from left to right"
+    // see: http://dev.mysql.com/doc/refman/5.0/en/update.html
+    $expressions = array_reverse($expressions);
   }
-  elseif ($shift > 0) {
-    $query->expression('depth', 'depth + :depth', array(':depth' => $shift));
+  foreach ($expressions as $expression) {
+    $query->expression($expression[0], $expression[1], $expression[2]);
   }
 
+  $query->expression('depth', 'depth + :depth', array(':depth' => $shift));
   $query->condition('menu_name', $existing_item['menu_name']);
   $p = 'p1';
   for ($i = 1; $i <= MENU_MAX_DEPTH && $existing_item[$p]; $p = 'p' . ++$i) {

=== modified file 'modules/menu/menu.test'
--- modules/menu/menu.test	2010-08-05 23:53:37 +0000
+++ modules/menu/menu.test	2010-08-22 14:01:59 +0000
@@ -192,14 +192,28 @@ class MenuTestCase extends DrupalWebTest
     // Add nodes to use as links for menu links.
     $node1 = $this->drupalCreateNode(array('type' => 'article'));
     $node2 = $this->drupalCreateNode(array('type' => 'article'));
+    $node3 = $this->drupalCreateNode(array('type' => 'article'));
+    $node4 = $this->drupalCreateNode(array('type' => 'article'));
+    $node5 = $this->drupalCreateNode(array('type' => 'article'));
 
     // Add menu links.
     $item1 = $this->addMenuLink(0, 'node/' . $node1->nid, $menu_name);
     $item2 = $this->addMenuLink($item1['mlid'], 'node/' . $node2->nid, $menu_name);
+    $item3 = $this->addMenuLink($item2['mlid'], 'node/' . $node3->nid, $menu_name);
+    $this->assertMenuLink($item1['mlid'], array('depth' => 1, 'has_children' => 1, 'p1' => $item1['mlid'], 'p2' => 0));
+    $this->assertMenuLink($item2['mlid'], array('depth' => 2, 'has_children' => 1, 'p1' => $item1['mlid'], 'p2' => $item2['mlid'], 'p3' => 0));
+    $this->assertMenuLink($item3['mlid'], array('depth' => 3, 'has_children' => 0, 'p1' => $item1['mlid'], 'p2' => $item2['mlid'], 'p3' => $item3['mlid'], 'p4' => 0));
 
     // Verify menu links.
     $this->verifyMenuLink($item1, $node1);
     $this->verifyMenuLink($item2, $node2, $item1, $node1);
+    $this->verifyMenuLink($item3, $node3, $item2, $node2);
+
+    // Add more menu links.
+    $item4 = $this->addMenuLink(0, 'node/' . $node4->nid, $menu_name);
+    $item5 = $this->addMenuLink($item4['mlid'], 'node/' . $node5->nid, $menu_name);
+    $this->assertMenuLink($item4['mlid'], array('depth' => 1, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => 0));
+    $this->assertMenuLink($item5['mlid'], array('depth' => 2, 'has_children' => 0, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => 0));
 
     // Modify menu links.
     $this->modifyMenuLink($item1);
@@ -209,6 +223,14 @@ class MenuTestCase extends DrupalWebTest
     $this->toggleMenuLink($item1);
     $this->toggleMenuLink($item2);
 
+    // Move link and verify that descendants are updated.
+    $this->moveMenuLink($item2, $item5['mlid'], $menu_name);
+    $this->assertMenuLink($item1['mlid'], array('depth' => 1, 'has_children' => 0, 'p1' => $item1['mlid'], 'p2' => 0));
+    $this->assertMenuLink($item4['mlid'], array('depth' => 1, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => 0));
+    $this->assertMenuLink($item5['mlid'], array('depth' => 2, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => 0));
+    $this->assertMenuLink($item2['mlid'], array('depth' => 3, 'has_children' => 1, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => $item2['mlid'], 'p4' => 0));
+    $this->assertMenuLink($item3['mlid'], array('depth' => 4, 'has_children' => 0, 'p1' => $item4['mlid'], 'p2' => $item5['mlid'], 'p3' => $item2['mlid'], 'p4' => $item3['mlid'], 'p5' => 0));
+
     // Enable a link via the overview form.
     $this->disableMenuLink($item1);
     $edit = array();
@@ -219,8 +241,7 @@ class MenuTestCase extends DrupalWebTest
     $this->drupalPost('admin/structure/menu/manage/' . $item1['menu_name'], $edit, t('Save configuration'));
 
     // Verify in the database.
-    $hidden = db_query("SELECT hidden FROM {menu_links} WHERE mlid = :mlid", array(':mlid' => $item1['mlid']))->fetchField();
-    $this->assertEqual($hidden, 0, t('Link is not hidden in the database table when enabled via the overview form'));
+    $this->assertMenuLink($item1['mlid'], array('hidden' => 0));
 
     // Save menu links for later tests.
     $this->items[] = $item1;
@@ -277,32 +298,9 @@ class MenuTestCase extends DrupalWebTest
     // Unlike most other modules, there is no confirmation message displayed.
     $this->assertText($title, 'Menu link was added');
 
-    // Retrieve menu link.
-    $item = db_query("SELECT * FROM {menu_links} WHERE link_title = :title", array(':title' => $title))->fetchAssoc();
-
-    // Check the structure in the DB of the two menu links.
-    // In general, if $n = $item['depth'] then $item['p'. $n] == $item['mlid'] and $item['p' . ($n - 1)] == $item['plid'] (unless depth == 0).
-    // All $item['p' . $n] for $n > depth must be 0.
-    // We know link1 is at the top level, so $item1['deptj'] == 1 and $item1['plid'] == 0.
-    // We know that the parent of link2 is link1, so $item2['plid'] == $item1['mlid'].
-    // Both menu links were created in the navigation menu.
-    $this->assertEqual($item['menu_name'], $menu_name);
-    $this->assertEqual($item['plid'], $plid);
-    $options = unserialize($item['options']);
-    if (!empty($options['query'])) {
-      $item['link_path'] .= '?' . drupal_http_build_query($options['query']);
-    }
-    if (!empty($options['fragment'])) {
-      $item['link_path'] .= '#' . $options['fragment'];
-    }
-    $this->assertEqual($item['link_path'], $link);
-    $this->assertEqual($item['link_title'], $title);
-    if ($plid == 0) {
-      $this->assertTrue($item['depth'] == 1 && !$item['has_children'] && $item['p1'] == $item['mlid'] && $item['p2'] == 0, 'Menu link has correct data');
-    }
-    else {
-      $this->assertTrue($item['depth'] == 2 && !$item['has_children'] && $item['p1'] == $plid && $item['p2'] == $item['mlid'], 'Menu link has correct data');
-    }
+    $item = db_query('SELECT * FROM {menu_links} WHERE link_title = :title', array(':title' => $title))->fetchAssoc();
+    $this->assertTrue(t('Menu link was found in database.'));
+    $this->assertMenuLink($item['mlid'], array('menu_name' => $menu_name, 'link_path' => $link, 'has_children' => 0, 'plid' => $plid));
 
     return $item;
   }
@@ -359,6 +357,19 @@ class MenuTestCase extends DrupalWebTest
   }
 
   /**
+   * Change the parent of a menu link using the menu module UI.
+   */
+  function moveMenuLink($item, $plid, $menu_name) {
+    $mlid = $item['mlid'];
+
+    $edit = array(
+      'parent' => $menu_name . ':' . $plid,
+    );
+    $this->drupalPost("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
+    $this->assertResponse(200);
+  }
+
+  /**
    * Modify a menu link using the menu module UI.
    *
    * @param array &$item Menu link passed by reference.
@@ -453,8 +464,7 @@ class MenuTestCase extends DrupalWebTest
 
     // Unlike most other modules, there is no confirmation message displayed.
     // Verify in the database.
-    $hidden = db_query("SELECT hidden FROM {menu_links} WHERE mlid = :mlid", array(':mlid' => $mlid))->fetchField();
-    $this->assertEqual($hidden, 1, t('Link is hidden in the database table'));
+    $this->assertMenuLink($mlid, array('hidden' => 1));
   }
 
   /**
@@ -469,8 +479,31 @@ class MenuTestCase extends DrupalWebTest
     $this->drupalPost("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));
 
     // Verify in the database.
-    $hidden = db_query("SELECT hidden FROM {menu_links} WHERE mlid = :mlid", array(':mlid' => $mlid))->fetchField();
-    $this->assertEqual($hidden, 0, t('Link is not hidden in the database table'));
+    $this->assertMenuLink($mlid, array('hidden' => 0));
+  }
+
+  /**
+   * Fetch the menu item from the database and compare it to the specified
+   * array.
+   *
+   * @param $mlid
+   *   Menu item id.
+   * @param $item
+   *   Array containing properties to verify.
+   */
+  function assertMenuLink($mlid, array $expected_item) {
+    // Retrieve menu link.
+    $item = db_query('SELECT * FROM {menu_links} WHERE mlid = :mlid', array(':mlid' => $mlid))->fetchAssoc();
+    $options = unserialize($item['options']);
+    if (!empty($options['query'])) {
+      $item['link_path'] .= '?' . drupal_http_build_query($options['query']);
+    }
+    if (!empty($options['fragment'])) {
+      $item['link_path'] .= '#' . $options['fragment'];
+    }
+    foreach ($expected_item as $key => $value) {
+      $this->assertEqual($item[$key], $value, t('Parameter %key had expected value.', array('%key' => $key)));
+    }
   }
 
   /**

