You are here

function PollTokenReplaceTestCase::testPollTokenReplacement in Drupal 7

Creates a poll, then tests the tokens generated from it.

File

modules/poll/poll.test, line 638
Tests for poll.module.

Class

PollTokenReplaceTestCase
Test poll token replacement in strings.

Code

function testPollTokenReplacement() {
  global $language;

  // Craete a poll with three choices.
  $title = $this
    ->randomName();
  $choices = $this
    ->_generateChoices(3);
  $poll_nid = $this
    ->pollCreate($title, $choices, FALSE);
  $this
    ->drupalLogout();

  // Create four users and have each of them vote.
  $vote_user1 = $this
    ->drupalCreateUser(array(
    'vote on polls',
    'access content',
  ));
  $this
    ->drupalLogin($vote_user1);
  $edit = array(
    'choice' => '1',
  );
  $this
    ->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
  $this
    ->drupalLogout();
  $vote_user2 = $this
    ->drupalCreateUser(array(
    'vote on polls',
    'access content',
  ));
  $this
    ->drupalLogin($vote_user2);
  $edit = array(
    'choice' => '1',
  );
  $this
    ->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
  $this
    ->drupalLogout();
  $vote_user3 = $this
    ->drupalCreateUser(array(
    'vote on polls',
    'access content',
  ));
  $this
    ->drupalLogin($vote_user3);
  $edit = array(
    'choice' => '2',
  );
  $this
    ->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
  $this
    ->drupalLogout();
  $vote_user4 = $this
    ->drupalCreateUser(array(
    'vote on polls',
    'access content',
  ));
  $this
    ->drupalLogin($vote_user4);
  $edit = array(
    'choice' => '3',
  );
  $this
    ->drupalPost('node/' . $poll_nid, $edit, t('Vote'));
  $this
    ->drupalLogout();
  $poll = node_load($poll_nid, NULL, TRUE);

  // Generate and test sanitized tokens.
  $tests = array();
  $tests['[node:poll-votes]'] = 4;
  $tests['[node:poll-winner]'] = filter_xss($poll->choice[1]['chtext']);
  $tests['[node:poll-winner-votes]'] = 2;
  $tests['[node:poll-winner-percent]'] = 50;
  $tests['[node:poll-duration]'] = format_interval($poll->runtime, 1, $language->language);

  // Test to make sure that we generated something for each token.
  $this
    ->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
  foreach ($tests as $input => $expected) {
    $output = token_replace($input, array(
      'node' => $poll,
    ), array(
      'language' => $language,
    ));
    $this
      ->assertEqual($output, $expected, format_string('Sanitized poll token %token replaced.', array(
      '%token' => $input,
    )));
  }

  // Generate and test unsanitized tokens.
  $tests['[node:poll-winner]'] = $poll->choice[1]['chtext'];
  foreach ($tests as $input => $expected) {
    $output = token_replace($input, array(
      'node' => $poll,
    ), array(
      'language' => $language,
      'sanitize' => FALSE,
    ));
    $this
      ->assertEqual($output, $expected, format_string('Unsanitized poll token %token replaced.', array(
      '%token' => $input,
    )));
  }
}