You are here

function VotingAPITestCase::testAddMultipleVotes in Voting API 7.2

Same name and namespace in other branches
  1. 6.2 tests/votingapi.test \VotingAPITestCase::testAddMultipleVotes()
  2. 7.3 tests/votingapi.test \VotingAPITestCase::testAddMultipleVotes()

Add multiple votes and ensure that the vote calculations are working.

File

tests/votingapi.test, line 96
Test file for VotingAPI module.

Class

VotingAPITestCase
@file Test file for VotingAPI module.

Code

function testAddMultipleVotes() {
  $users = array();
  $users[] = $this
    ->drupalCreateUser();
  $users[] = $this
    ->drupalCreateUser();
  $users[] = $this
    ->drupalCreateUser();
  $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]['entity_type'] = 'node';
      $votes[$index]['entity_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);
}