Index: tests/votingapi.test
===================================================================
RCS file: tests/votingapi.test
diff -N tests/votingapi.test
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/votingapi.test	17 Jul 2008 17:37:54 -0000
@@ -0,0 +1,216 @@
+<?php
+
+class ErrorDetector {
+  static private $errorCount = 0;
+  public $handler;
+  public function __construct() {
+    $this->reset();    
+  }
+  
+  static public function errorHandler($errno, $errstr) {
+    ErrorDetector::$errorCount++;
+  }
+  
+  public function reset() {
+    self::$errorCount = 0;
+  }
+  
+  public function installHandler() {
+    $this->handler = set_error_handler('ErrorDetector::errorHandler');
+  }
+  
+  public function removeHandler() {
+    set_error_handler($this->handler);
+  }
+  
+  public function checkErrors() {
+    $this->removeHandler();
+    if (ErrorDetector::$errorCount > 0) {
+      throw new Exception(t('Unexpected errors encountered'));
+    }
+  }
+}
+
+class VotingapiTestCase extends DrupalWebTestCase {
+  /**
+   * Implementation of getInfo().
+   */
+  function getInfo() {
+    return array(
+      'name' => t('Voting API'),
+      'desc' => t('Voting API'),
+      'group' => t('Voting API Tests'));
+  }
+  
+  function setUp() {
+    parent::setUp('votingapi');
+    // Create a new page
+    $node = new stdClass();
+    $node->title = '';
+    $node->teaser = t('Teaser text');
+    $node->body = t('Here is the body of the page');
+    $node->uid = 1;
+    $node->type = 'page';
+    $node->status = 1;
+    $node->promote = 0;
+    node_save($node);
+    variable_set('votingapi_nid1', $node->nid);
+    $node->title = t('Node @id', array('@id' => $node->nid));
+    node_save($node);
+  }
+  
+  function tearDown() {
+    $nid = variable_get('votingapi_nid1', null);
+    if ($nid) {
+      node_delete($nid);
+      variable_del('votingapi_nid1');
+    }
+    parent::tearDown();
+  }
+  
+  /**
+   * Ensure that the optional fields are truely optional.
+   */
+  function testMinimalAdd() {
+    $nid = variable_get('votingapi_nid1', NULL);
+    $value = '85';
+    // The minimum required fields according to the documentation are
+    // content_id and value.
+    $vote = array(
+      'content_id' => $nid,
+      'value' => $value
+    );
+    $error_detector = new ErrorDetector();
+    $error_detector->installHandler();
+    try {
+      $result = votingapi_add_votes($vote);
+      $error_detector->checkErrors();
+      // Test that the result has its fields set appropriately.
+      $this->validateVote('testMinimalAdd()', $result, $nid, array($value));
+      $this->assertTrue(time() - $result[0]['timestamp'] < 2, t('The timestamp should be less than 2 seconds ago.'));
+    }
+    catch (Exception $e) {
+      $this->fail(t('Could not add a vote with only content_id and value.'));
+      return;
+    }
+  }
+  
+  /**
+   * Add a vote and ensure that the vote was stored properly.
+   */
+  function testAddVote() {
+    global $user;
+    $value = '7';
+    $nid = variable_get('votingapi_nid1', null);
+    $vote = array('content_id' => $nid,
+      'value' => $value,
+      'content_type' => 'node');
+    try {
+      $result = votingapi_add_votes($vote);
+      // Test that the result has its fields set appropriately.
+      $this->validateVote("testAddVote()", $result, $nid, $value, 'node', $user->uid);
+      $this->assertTrue(time() - $result[0]['timestamp'] < 2, t('The timestamp should be less than 2 seconds ago.'));
+    }
+    catch (Exception $e) {
+      $this->fail('The votingapi_add_votes threw an error during the "votingapi_add_votes" call.');
+      return;
+    }
+    // Load the vote back in and verify it matches.
+    $vote_results = votingapi_recalculate_results('node', $nid);
+    $this->validateVoteCounts('testAddVote()', $vote_results, $nid, array($value));
+  }
+  
+  /**
+   * Add multiple votes and ensure that the vote calculations are working.
+   */
+  function testAddMultipleVotes() {
+    $permissions = array();
+    $users = array();
+    $users[] = $this->drupalCreateUser($permissions);
+    $users[] = $this->drupalCreateUser($permissions);
+    $users[] = $this->drupalCreateUser($permissions);
+    $nid = variable_get('votingapi_nid1', null);
+    $values = array(72, 13, 27);
+    $votes = array();
+    try {
+      for ($index = 0; $index < count($values); $index++) {
+        $votes[$index] = array();
+        $votes[$index]['content_type'] = 'node';
+        $votes[$index]['content_id'] = $nid;
+        $votes[$index]['uid'] = $users[$index]->uid;
+        $votes[$index]['value'] = $values[$index];
+      }
+      $result = votingapi_add_votes($votes);
+      // Test that the result has its fields set appropriately.
+      $this->validateVote("testAddMultipleVotes()", $result, $nid, $values);
+    }
+    catch (Exception $e) {
+      $this->fail('The votingapi_add_votes threw an error during the "votingapi_add_votes" call.');
+       return;
+    }
+    // Load the vote back in and verify it matches.
+    $vote_results = votingapi_recalculate_results('node', $nid);
+    $this->validateVoteCounts('testAddVote()', $vote_results, $nid, $values);
+  }
+
+  function validateVote($prefix, $vote, $content_id, $value, $content_type = 'node',
+    $uid = NULL, $value_type = 'percent', $tag = 'vote', $vote_source = NULL) {
+    global $user;
+    if ($vote_source == NULL) {
+      $vote_source = ip_address();
+    }
+    $prefix_array = array('@prefix' => $prefix);
+    for ($index = 0; $index < count($vote); $index++) {
+      $this->assertTrue($vote[$index]['content_id'] == $content_id, t('@prefix: content_id should match.', $prefix_array));
+      $this->assertTrue($vote[$index]['value'] == $value[$index], t('@prefix: value should match.', $prefix_array));
+      $this->assertTrue($vote[$index]['content_type'] == $content_type, t('@prefix: content_type should match, default = "node".', $prefix_array));
+      $this->assertTrue($vote[$index]['value_type'] == $value_type, t('@prefix: value_type should match, default= "percent".', $prefix_array));
+      $this->assertTrue($vote[$index]['tag'] == $tag, t('@prefix: tag should match, default =  "vote".', $prefix_array));
+      $this->assertTrue($vote[$index]['vote_source'] == $vote_source, t('@prefix: vote_source should match, default = ip address.', $prefix_array));
+      if ($uid != NULL) {
+        $this->assertTrue($vote[0]['uid'] == $uid, t('@prefix: uid should match.', $prefix_array));
+      }
+    }
+  }
+  
+  function validateVoteCounts($prefix, $votes, $content_id, $values, $content_type = 'node',
+    $value_type = 'percent', $tag = 'vote') {
+      $count_summary = 0;
+      $average_summary = 1;
+      $count = 0.0;
+      $sum = 0.0;
+      foreach ($values as $value) {
+        $sum += $value;
+        $count++;
+      }
+      $average = $sum / $count;
+      $prefix_array = array('@prefix' => $prefix);
+      foreach ($votes as $summary) {
+        if ($summary['function'] == 'count') {
+          $summary_desc = 'count summary';
+          $prefix_array['@summary_desc'] = $summary_desc;
+          $this->assertTrue($summary['value'] == $count,
+            t('@prefix: (@summary_desc) value should match the number of values.', $prefix_array));
+        }
+        elseif ($summary['function'] == 'average') {
+          $summary_desc = 'average summary';
+          $prefix_array['@summary_desc'] = $summary_desc;
+          $this->assertTrue($summary['value'] == $average,
+            t('@prefix: (@summary_desc) value should match the average of values', $prefix_array));
+        }
+        else {
+          $prefix_array['@summary'] = $summary['function'];
+          $prefix_array['@summary_desc'] = $summary['function'] . ' summary';
+          $this->assertFalse(true, t('@prefix: Unknown summary type @summary.', $prefix_array));
+        }
+        $this->assertTrue($summary['content_type'] == $content_type,
+          t('@prefix: (@summary_desc) content_type should match, default = "node"', $prefix_array));
+      $this->assertTrue($summary['content_id'] == $content_id,
+        t('@prefix: (@summary_desc) content_id should match', $prefix_array));
+      $this->assertTrue($summary['value_type'] == $value_type,
+        t('@prefix: (@summary_desc) value_type should match.', $prefix_array));
+      $this->assertTrue($summary['tag'] == $tag,
+        t('@prefix: (@summary_desc) tag should match', $prefix_array));
+    }
+  }
+}
