You are here

function VotingAPITestCase::testAddMultipleVotes in Voting API 7.3

Same name and namespace in other branches
  1. 6.2 tests/votingapi.test \VotingAPITestCase::testAddMultipleVotes()
  2. 7.2 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] = new VotingApi_Vote(array(
        'entity_type' => 'node',
        'entity_id' => $nid,
        'uid' => $users[$index]->uid,
        'value' => $values[$index],
      ));
    }
    $result = VotingApi_Vote::saveMultiple($votes);

    // Test that the result has its fields set appropriately.
    $this
      ->validateVote("testAddMultipleVotes()", $result, $nid, $values);
  } catch (Exception $e) {
    $this
      ->fail('The VotingApi_Vote::saveMultiple threw an error.');
    return;
  }

  // Load the vote back in and verify it matches.
  $vote_results = votingapi_recalculate_results('node', $nid);
  $this
    ->validateVoteCounts('testAddVote()', $vote_results, $nid, $values);
}