Index: modules/taxonomy/taxonomy.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/taxonomy/taxonomy.module,v
retrieving revision 1.442
diff -u -p -r1.442 taxonomy.module
--- modules/taxonomy/taxonomy.module	13 Nov 2008 08:13:56 -0000	1.442
+++ modules/taxonomy/taxonomy.module	28 Nov 2008 15:22:57 -0000
@@ -928,25 +928,25 @@ function taxonomy_get_synonym_root($syno
  */
 function taxonomy_term_count_nodes($tid, $type = 0) {
   static $count;
-
-  if (!isset($count[$type])) {
-    // $type == 0 always evaluates TRUE if $type is a string
-    if (is_numeric($type)) {
-      $result = db_query(db_rewrite_sql('SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t INNER JOIN {node} n ON t.vid = n.vid WHERE n.status = 1 GROUP BY t.tid'));
-    }
-    else {
-      $result = db_query(db_rewrite_sql("SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t INNER JOIN {node} n ON t.vid = n.vid WHERE n.status = 1 AND n.type = '%s' GROUP BY t.tid"), $type);
+  $term = taxonomy_term_load($tid);
+  $tree = taxonomy_get_tree($term->vid, $tid);
+  $tids = array($tid);
+  foreach ($tree as $descendent) {
+    $tids[] =  $descendent->tid;
+  }
+
+  if (!isset($count[$type][$tid])) {
+    $query = db_select('term_node', 't');
+    $query->addExpression('COUNT(DISTINCT(t.nid))', 'nid_count');
+    $query->join('node', 'n', 't.vid = n.vid');
+    $query->condition('t.tid', $tids, 'IN');
+    $query->condition('n.status', 1);
+    if (!is_numeric($type)) {
+      $query->condition('n.type', $type);
     }
-    $count[$type] = array();
-    while ($term = db_fetch_object($result)) {
-      $count[$type][$term->tid] = $term->c;
-    }
-  }
-  $children_count = 0;
-  foreach (_taxonomy_term_children($tid) as $c) {
-    $children_count += taxonomy_term_count_nodes($c, $type);
+    $count[$type][$tid] = $query->execute()->fetchField();
   }
-  return $children_count + (isset($count[$type][$tid]) ? $count[$type][$tid] : 0);
+  return $count[$type][$tid];
 }
 
 /**
Index: modules/taxonomy/taxonomy.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/taxonomy/taxonomy.test,v
retrieving revision 1.15
diff -u -p -r1.15 taxonomy.test
--- modules/taxonomy/taxonomy.test	22 Nov 2008 13:43:13 -0000	1.15
+++ modules/taxonomy/taxonomy.test	28 Nov 2008 15:22:57 -0000
@@ -237,6 +237,58 @@ function getInfo() {
 }
 
 /**
+ * Unit tests for taxonomy term functions.
+ */
+class TaxonomyTermUnitTest extends TaxonomyWebTestCase {
+
+  function getInfo() {
+    return array(
+      'name' => t('Taxonomy term unit tests'),
+      'description' => t('Unit tests for taxonomy term functions.'),
+      'group' => t('Taxonomy'),
+    );
+  }
+
+  /**
+   * Tests for taxonomy_term_count_nodes().
+   */
+  function testTaxonomyTermCountNodes() {
+    // Create a vocabulary with three terms.
+    $vocabulary = $this->createVocabulary();
+    $term1 = $this->createTerm($vocabulary->vid);
+    $term2 = $this->createTerm($vocabulary->vid);
+    $term3 = $this->createTerm($vocabulary->vid);
+
+    // Attach term1 to a node, and confirm it is counted correctly.
+    $node1 = $this->drupalCreateNode();
+    $node1->taxonomy = array($term1->tid);
+    node_save($node1);
+    $this->assertEqual(taxonomy_term_count_nodes($term1->tid), 1, t('Term has one valid node association.'));
+
+    // Attach term2 to a node.
+    $node2 = $this->drupalCreateNode();
+    $node2->taxonomy = array($term2->tid);
+    node_save($node2);
+    $this->assertEqual(taxonomy_term_count_nodes($term2->tid), 1, t('Term has one valid node association.'));
+
+    // Confirm that term3 is not associated with any nodes.
+    //$this->assertEqual(taxonomy_term_count_nodes($term3->tid), 0, t('Term is not associated with any nodes'));
+
+    // Set term3 as the parent of term 1.
+    $term1->parent = array($term3->tid);
+    taxonomy_term_save($term1);
+    $children = taxonomy_get_children($term3->tid);
+    $this->assertTrue(isset($children[$term1->tid]), t('Term 3 saved as parent of term 1'));
+    $this->assertEqual($term1->vid, $term2->vid);
+    $this->assertEqual(count(taxonomy_get_tree($term3->vid, $term3->tid)), 2, t('Term 3 has one child term'));
+    $this->assertEqual(taxonomy_term_count_nodes($term1->tid), 1, t('Term has one valid node association.'));
+    $this->assertEqual(taxonomy_term_count_nodes($term3->tid), 1, t('Term has one valid node association due to child term.'));
+    $node->taxonomy = array($term1->tid, $term2->tid);
+    node_save($node);
+  }
+}
+
+/**
  * Tests for taxonomy term functions.
  */
 class TaxonomyTermTestCase extends TaxonomyWebTestCase {
