Index: modules/system/system.admin.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.admin.inc,v
retrieving revision 1.313
diff -u -p -r1.313 system.admin.inc
--- modules/system/system.admin.inc	3 Oct 2010 00:47:51 -0000	1.313
+++ modules/system/system.admin.inc	4 Oct 2010 21:12:02 -0000
@@ -1186,7 +1186,7 @@ function system_modules_submit($form, &$
   unset($form_state['storage']);
 
   // Reverse the 'enable' list, to order dependencies before dependents.
-  rsort($actions['enable']);
+  krsort($actions['enable']);
 
   // Installs, enables, and disables modules.
   module_enable($actions['enable'], FALSE);
Index: modules/system/system.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.test,v
retrieving revision 1.145
diff -u -p -r1.145 system.test
--- modules/system/system.test	1 Oct 2010 18:37:23 -0000	1.145
+++ modules/system/system.test	4 Oct 2010 21:12:03 -0000
@@ -318,6 +318,47 @@ class ModuleDependencyTestCase extends M
     $this->assertText(t('The configuration options have been saved.'), t('Modules status has been updated.'));
     $this->assertModules(array('taxonomy', 'forum'), TRUE);
   }
+
+  /**
+   * Tests that module dependencies are enabled in the correct order via the
+   * UI. Dependencies should be enabled before their dependents.
+   */
+  function testModuleEnableOrder() {
+    module_enable(array('module_test'), FALSE);
+    drupal_flush_all_caches();
+    $this->assertModules(array('module_test'), TRUE);
+    variable_set('dependency_test', 'dependency');
+    // module_test has creates a dependency chain: forum depends on poll,
+    // which depends on php. The correct enable order is, php, poll, forum.
+    $expected_order = array('php', 'poll', 'forum');
+
+    // Enable the modules through the UI, verifying that the dependency chain
+    // is correct.
+    $edit = array();
+    $edit['modules[Core][forum][enable]'] = 'forum';
+    $this->drupalPost('admin/modules', $edit, t('Save configuration'));
+    $this->assertModules(array('forum'), FALSE);
+    $this->assertText(t('You must enable the Poll, PHP filter modules to install Forum.'), t('Dependency chain created.'));
+    $edit['modules[Core][poll][enable]'] = 'poll';
+    $edit['modules[Core][php][enable]'] = 'php';
+    $this->drupalPost('admin/modules', $edit, t('Save configuration'));
+    $this->assertModules(array('forum', 'poll', 'php'), TRUE);
+
+    // Check the actual order by examining the watchdog table.
+    $query = db_select('watchdog', 'w')
+      ->fields('w')
+      ->orderBy('timestamp');
+    $query->condition('w.type', 'system');
+    $actual_order = array();
+    foreach ($query->execute() as $dblog) {
+      $variables = unserialize($dblog->variables);
+      if ($dblog->message != '%module module enabled.' || !in_array($variables['%module'], $expected_order)) {
+        continue;
+      }
+      $actual_order[] = $variables['%module'];
+    }
+    $this->assertIdentical($actual_order, $expected_order, t('Modules enabled in the correct order.'));
+  }
 }
 
 /**
