? 396284_tablesort_extender.patch
Index: includes/tablesort.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/tablesort.inc,v
retrieving revision 1.50
diff -u -p -r1.50 tablesort.inc
--- includes/tablesort.inc	22 Feb 2009 16:53:41 -0000	1.50
+++ includes/tablesort.inc	15 Mar 2009 02:37:44 -0000
@@ -23,6 +23,11 @@ class TableSort extends SelectQueryExten
   protected $header = array();
 
   public function execute() {
+    return $this->query->execute();
+  }
+
+  public function setHeader(Array $header) {
+    $this->header = $header;
     $ts = $this->init();
     if ($ts['sql']) {
       // Based on code from db_escape_table(), but this can also contain a dot.
@@ -33,11 +38,6 @@ class TableSort extends SelectQueryExten
       $sort = in_array($sort, array('ASC', 'DESC')) ? $sort : '';
       $this->orderBy($field, $sort);
     }
-    return $this->query->execute();
-  }
-
-  public function setHeader(Array $header) {
-    $this->header = $header;
     return $this;
   }
 
Index: modules/simpletest/tests/database_test.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/database_test.module,v
retrieving revision 1.7
diff -u -p -r1.7 database_test.module
--- modules/simpletest/tests/database_test.module	22 Feb 2009 16:53:41 -0000	1.7
+++ modules/simpletest/tests/database_test.module	15 Mar 2009 02:37:44 -0000
@@ -60,6 +60,10 @@ function database_test_menu() {
     'access callback' => TRUE,
     'page callback' => 'database_test_tablesort',
   );
+  $items['database_test/tablesort_first'] = array(
+    'access callback' => TRUE,
+    'page callback' => 'database_test_tablesort_first',
+  );
 
   return $items;
 }
@@ -155,4 +159,33 @@ function database_test_tablesort() {
     'tasks' => $tasks,
   ));
   exit;
+}
+
+/**
+ * Run a tablesort query with a second order_by after and return the results.
+ *
+ * This function does care about the page GET parameter, as set by the
+ * simpletest HTTP call.
+ */
+function database_test_tablesort_first() {
+  $header = array(
+    'tid' => array('data' => t('Task ID'), 'field' => 'tid', 'sort' => 'desc'),
+    'pid' => array('data' => t('Person ID'), 'field' => 'pid'),
+    'task' => array('data' => t('Task'), 'field' => 'task'),
+    'priority' => array('data' => t('Priority'), 'field' => 'priority', ),
+  );
+
+  $query = db_select('test_task', 't');
+  $query
+    ->fields('t', array('tid', 'pid', 'task', 'priority'));
+  
+  $query = $query->extend('TableSort')->setHeader($header)->orderBy('priority');
+
+  // We need all the results at once to check the sort.
+  $tasks = $query->execute()->fetchAll();
+
+  drupal_json(array(
+    'tasks' => $tasks,
+  ));
+  exit;
 }
\ No newline at end of file
Index: modules/simpletest/tests/database_test.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/database_test.test,v
retrieving revision 1.46
diff -u -p -r1.46 database_test.test
--- modules/simpletest/tests/database_test.test	14 Mar 2009 17:45:55 -0000	1.46
+++ modules/simpletest/tests/database_test.test	15 Mar 2009 02:37:45 -0000
@@ -1713,6 +1713,32 @@ class DatabaseSelectTableSortDefaultTest
       $this->assertEqual($last->task, $sort['last'], t('Items appear in the correct order.'));
     }
   }
+  
+  /**
+   * Confirm that if a tablesort's setHeader is called before another orderBy, that the header happens first.
+   *
+   */
+  function testTableSortQueryFirst() {
+    $sorts = array(
+      array('field' => t('Task ID'), 'sort' => 'desc', 'first' => 'perform at superbowl', 'last' => 'eat'),
+      array('field' => t('Task ID'), 'sort' => 'asc', 'first' => 'eat', 'last' => 'perform at superbowl'),
+      array('field' => t('Task'), 'sort' => 'asc', 'first' => 'code', 'last' => 'sleep'),
+      array('field' => t('Task'), 'sort' => 'desc', 'first' => 'sleep', 'last' => 'code'),
+      // more elements here
+
+    );
+
+    foreach ($sorts as $sort) {
+      $this->drupalGet('database_test/tablesort_first/', array('query' => array('order' => $sort['field'], 'sort' => $sort['sort'])));
+      $data = json_decode($this->drupalGetContent());
+
+      $first = array_shift($data->tasks);
+      $last = array_pop($data->tasks);
+
+      $this->assertEqual($first->task, $sort['first'], t('Items appear in the correct order sorting by @field @sort.', array('@field' => $sort['field'], '@sort' => $sort['sort'])));
+      $this->assertEqual($last->task, $sort['last'], t('Items appear in the correct order sorting by @field @sort.', array('@field' => $sort['field'], '@sort' => $sort['sort'])));
+    }
+  }
 }
 
 /**
