 modules/forum/forum.test |  129 +++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 128 insertions(+), 1 deletions(-)

diff --git modules/forum/forum.test modules/forum/forum.test
index 7860937..7d6a87b 100644
--- modules/forum/forum.test
+++ modules/forum/forum.test
@@ -10,6 +10,7 @@ class ForumTestCase extends DrupalWebTestCase {
   protected $forum;
   protected $root_forum;
   protected $nids;
+  protected $post_count;
 
   public static function getInfo() {
     return array(
@@ -45,6 +46,12 @@ class ForumTestCase extends DrupalWebTestCase {
     // Generate topics to populate the active forum block.
     $this->generateForumTopics($this->forum);
 
+    // Generate topics posts to populate the active forum block.
+    foreach ($this->nids as $nid) {
+      $node = node_load($nid);
+      $this->generateForumPosts($node);
+    }
+
     // Login an unprivileged user to view the forum topics and generate an
     // active forum topics list.
     $this->drupalLogin($this->web_user);
@@ -72,7 +79,8 @@ class ForumTestCase extends DrupalWebTestCase {
     // Verify the topic and post counts on the forum page.
     $this->drupalGet('forum');
     $this->assertRaw("<td class=\"topics\">\n          6                  </td>");
-    $this->assertRaw('<td class="posts">6</td>');
+    $this->post_count += 4;
+    $this->assertRaw('<td class="posts">'. $this->post_count .'</td>');
 
     // Test loading multiple forum nodes on the front page.
     $this->drupalLogin($this->drupalCreateUser(array('administer content types', 'create forum content')));
@@ -286,6 +294,8 @@ class ForumTestCase extends DrupalWebTestCase {
     $this->createForumTopic($this->container, TRUE);
     // Create forum node.
     $node = $this->createForumTopic($this->forum, FALSE);
+    // Create response.
+    $this->createForumPost($node);
     // Verify the user has access to all the forum nodes.
     $this->verifyForums($user, $node, $admin);
   }
@@ -341,6 +351,79 @@ class ForumTestCase extends DrupalWebTestCase {
   }
 
   /**
+   * Create Forum Post (comment).
+   *
+   * @param $node
+   *   Node to post comment on.
+   * @param $contact
+   *   Set to NULL for no contact info, TRUE to ignore success checking, and
+   *   array of values to set contact info.
+   */
+  function createForumPost($node, $contact = NULL) {
+    $subject = $this->randomName(20);
+    $comment = $this->randomName(200);
+    $langcode = LANGUAGE_NONE;
+    $edit = array();
+    $edit['comment_body[' . $langcode . '][0][value]'] = $comment;
+
+    $preview_mode = variable_get('comment_preview_article', DRUPAL_OPTIONAL);
+    $subject_mode = variable_get('comment_subject_field_article', 1);
+
+    // Must get the page before we test for fields.
+    if ($node !== NULL) {
+      $this->drupalGet('comment/reply/' . $node->nid);
+    }
+
+    if ($subject_mode == TRUE) {
+      $edit['subject'] = $subject;
+    }
+    else {
+      $this->assertNoFieldByName('subject', '', t('Subject field not found.'));
+    }
+
+    if ($contact !== NULL && is_array($contact)) {
+      $edit += $contact;
+    }
+    switch ($preview_mode) {
+      case DRUPAL_REQUIRED:
+        // Preview required so no save button should be found.
+        $this->assertNoFieldByName('op', t('Save'), t('Save button not found.'));
+        $this->drupalPost(NULL, $edit, t('Preview'));
+        // Don't break here so that we can test post-preview field presence and
+        // function below.
+      case DRUPAL_OPTIONAL:
+        $this->assertFieldByName('op', t('Preview'), t('Preview button found.'));
+        $this->assertFieldByName('op', t('Save'), t('Save button found.'));
+        $this->drupalPost(NULL, $edit, t('Save'));
+        break;
+
+      case DRUPAL_DISABLED:
+        $this->assertNoFieldByName('op', t('Preview'), t('Preview button not found.'));
+        $this->assertFieldByName('op', t('Save'), t('Save button found.'));
+        $this->drupalPost(NULL, $edit, t('Save'));
+        break;
+    }
+    $this->post_count++;
+    $match = array();
+    // Get comment ID
+    preg_match('/#comment-([0-9]+)/', $this->getURL(), $match);
+
+    // Get comment.
+    if ($contact !== TRUE) {
+      // Attempt to find error message.
+      if ($subject) {
+        $this->assertText($subject, 'Comment subject posted.');
+      }
+      $this->assertText($comment, 'Comment body posted.');
+      $this->assertTrue((!empty($match) && !empty($match[1])), t('Comment id found.'));
+    }
+
+    if (isset($match[1])) {
+      return (object) array('id' => $match[1], 'subject' => $subject, 'comment' => $comment);
+    }
+  }
+
+  /**
    * Verify the logged in user has access to a forum nodes.
    *
    * @param $node_user
@@ -367,9 +450,41 @@ class ForumTestCase extends DrupalWebTestCase {
     }
 
     // Verify the forum blocks were displayed.
+    drupal_flush_all_caches();
     $this->drupalGet('');
     $this->assertResponse(200);
     $this->assertText(t('New forum topics'), t('[New forum topics] Forum block was displayed'));
+    $this->assertText(t('Active forum topics'), t('[Active forum topics] Forum block was displayed'));
+    
+    // Verify top topics are listed on blocks first
+    $result = db_query("SELECT nid FROM {node} WHERE type = 'forum'");
+    $topics = node_load_multiple($result->fetchCol());
+    $newest_topic = array('nid' => 0, 'title' => '', 'created' => 0, 'comment_count' => 0);
+    $active_topic = array('nid' => 0, 'title' => '', 'created' => 0, 'comment_count' => 0);
+    foreach ($topics as $topic) {
+      $comment_count = db_query("SELECT COUNT(cid) count FROM {comment} WHERE nid = :nid", array(':nid' => $topic->nid))->fetchField();
+      $newest_topic = $topic->created > $newest_topic['created'] ? array('nid' => $topic->nid, 'title' => $topic->title, 'created' => $topic->created, 'comment_count' => $comment_count) : $newest_topic;
+      $active_topic = $topic->created > $active_topic['created'] ? array('nid' => $topic->nid, 'title' => $topic->title, 'created' => $topic->created, 'comment_count' => $comment_count) : $active_topic;
+      $active_topic = $topic->last_comment_timestamp > $active_topic['created'] ? array('nid' => $topic->nid, 'title' => $topic->title, 'created' => $topic->last_comment_timestamp, 'comment_count' => $comment_count) : $active_topic;
+    }
+    if ($newest_topic['nid']) {
+      $s = $newest_topic['comment_count'] > 1 ? 's' : '';
+      $title = $title_xpath = '';
+      if ($newest_topic['comment_count']) {
+        $title = $newest_topic['comment_count'] . ' comment' . $s;
+        $title_xpath = ' and @title=:title';
+      }
+      $this->assertFieldByXPath($this->buildXPathQuery('//li[@class=:class]/a[contains(@href, :nid)' . $title_xpath . ']', array(':class' => 'first', ':nid' => 'node/' . $newest_topic['nid'], ':title' => $title)), $newest_topic['title']);
+    }
+    if ($active_topic['nid']) {
+      $s = $active_topic['comment_count'] > 1 ? 's' : '';
+      $title = $title_xpath = '';
+      if ($active_topic['comment_count']) {
+        $title = $active_topic['comment_count'] . ' comment' . $s;
+        $title_xpath = ' and @title=:title';
+      }
+      $this->assertFieldByXPath($this->buildXPathQuery('//li[@class=:class]/a[contains(@href, :nid)' . $title_xpath . ']', array(':class' => 'first', ':nid' => 'node/' . $active_topic['nid'], ':title' => $title)), $newest_topic['title']);
+    }
 
     // View forum container page.
     $this->verifyForumView($this->container);
@@ -452,6 +567,18 @@ class ForumTestCase extends DrupalWebTestCase {
   }
 
   /**
+   * Generate forum posts to test display of active forum block.
+   *
+   * @param array $node Forum array (a row from taxonomy_term_data table).
+   */
+  private function generateForumPosts($node) {
+    $max = rand(0,20);
+    for ($i = 0; $i < $max; $i++) {
+      $this->createForumPost($node);
+    }
+  }
+
+  /**
    * View forum topics to test display of active forum block.
    *
    * @todo The logic here is completely incorrect, since the active
