From 66ec59e0f4179e2ab5630bb00bf20ba3fdab5ba6 Mon Sep 17 00:00:00 2001
From: Angie Byron <webchick@24967.no-reply.drupal.org>
Date: Fri, 15 Oct 2010 04:34:15 +0000
Subject: [PATCH] #664042 by coltrane, effulgentsia: Fixed TableSort order
 error when no sort set in header

---
 includes/tablesort.inc                        | 10 ++--
 modules/simpletest/tests/database_test.module | 51 ++++++++++++++++++-
 modules/simpletest/tests/database_test.test   |  9 ++++
 3 files changed, 65 insertions(+), 5 deletions(-)

diff --git a/includes/tablesort.inc b/includes/tablesort.inc
index cd0136bc19c8..960bd2f0d0b7 100644
--- a/includes/tablesort.inc
+++ b/includes/tablesort.inc
@@ -128,12 +128,14 @@ protected function order() {
     }
     else {
       // The first column specified is initial 'order by' field unless otherwise specified
-      if (is_array($this->header[0])) {
-        $this->header[0] += array('data' => NULL, 'field' => NULL);
-        return array('name' => $this->header[0]['data'], 'sql' => $this->header[0]['field']);
+      $headers = array_values($this->header);
+      $header = $headers[0];
+      if (is_array($header)) {
+        $header += array('data' => NULL, 'field' => NULL);
+        return array('name' => $header['data'], 'sql' => $header['field']);
       }
       else {
-        return array('name' => $this->header[0]);
+        return array('name' => $header);
       }
     }
   }
diff --git a/modules/simpletest/tests/database_test.module b/modules/simpletest/tests/database_test.module
index 18fc056aa39d..2b684409c58e 100644
--- a/modules/simpletest/tests/database_test.module
+++ b/modules/simpletest/tests/database_test.module
@@ -66,7 +66,11 @@ function database_test_menu() {
     'access callback' => TRUE,
     'page callback' => 'database_test_tablesort_first',
   );
-
+  $items['database_test/tablesort_default_sort'] = array(
+    'access callback' => TRUE,
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('database_test_theme_tablesort'),
+  );
   return $items;
 }
 
@@ -191,3 +195,48 @@ function database_test_tablesort_first() {
   ));
   exit;
 }
+
+/**
+ * Output a form without setting a header sort.
+ */
+function database_test_theme_tablesort($form, &$form_state) {
+  $header = array(
+    'username' => array('data' => t('Username'), 'field' => 'u.name'),
+    'status' => array('data' => t('Status'), 'field' => 'u.status'),
+  );
+
+  $query = db_select('users', 'u');
+  $query->condition('u.uid', 0, '<>');
+  user_build_filter_query($query);
+
+  $count_query = clone $query;
+  $count_query->addExpression('COUNT(u.uid)');
+
+  $query = $query->extend('PagerDefault')->extend('TableSort');
+  $query
+    ->fields('u', array('uid', 'name', 'status', 'created', 'access'))
+    ->limit(50)
+    ->orderByHeader($header)
+    ->setCountQuery($count_query);
+  $result = $query->execute();
+
+  $options = array();
+
+  $status = array(t('blocked'), t('active'));
+  $accounts = array();
+  foreach ($result as $account) {
+    $options[$account->uid] = array(
+      'username' => check_plain($account->name),
+      'status' =>  $status[$account->status],
+    );
+  }
+
+  $form['accounts'] = array(
+    '#type' => 'tableselect',
+    '#header' => $header,
+    '#options' => $options,
+    '#empty' => t('No people available.'),
+  );
+
+  return $form;
+}
diff --git a/modules/simpletest/tests/database_test.test b/modules/simpletest/tests/database_test.test
index 62ae06a40d3c..f34afafabcc7 100644
--- a/modules/simpletest/tests/database_test.test
+++ b/modules/simpletest/tests/database_test.test
@@ -2314,6 +2314,15 @@ class DatabaseSelectTableSortDefaultTestCase extends DatabaseTestCase {
       $this->assertEqual($last->task, $sort['last'], t('Items appear in the correct order sorting by @field @sort.', array('@field' => $sort['field'], '@sort' => $sort['sort'])));
     }
   }
+
+  /**
+   * Confirm that if a sort is not set in a tableselect form there is no error thrown when using the default.
+   */
+  function testTableSortDefaultSort() {
+    $this->drupalGet('database_test/tablesort_default_sort');
+    // Any PHP errors or notices thrown would trigger a simpletest exception, so
+    // no additional assertions are needed.
+  }
 }
 
 /**
-- 
GitLab