diff options
Diffstat (limited to 'modules/poll')
-rw-r--r-- | modules/poll/poll.test | 89 | ||||
-rw-r--r-- | modules/poll/poll.tokens.inc | 17 |
2 files changed, 106 insertions, 0 deletions
diff --git a/modules/poll/poll.test b/modules/poll/poll.test index e4a7db848..35d5ed49e 100644 --- a/modules/poll/poll.test +++ b/modules/poll/poll.test @@ -457,3 +457,92 @@ class PollVoteCheckHostname extends PollTestCase { $this->assertTrue(empty($elements), t("%user is not able to vote again.", array('%user' => $this->web_user1->name))); } } + +/** + * Test poll token replacement in strings. + */ +class PollTokenReplaceTestCase extends PollTestCase { + public static function getInfo() { + return array( + 'name' => 'Poll token replacement', + 'description' => 'Generates text using placeholders for dummy content to check poll token replacement.', + 'group' => 'Poll', + ); + } + + function setUp() { + parent::setUp('poll'); + } + + /** + * Creates a poll, then tests the tokens generated from it. + */ + 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)), t('No empty tokens generated.')); + + foreach ($tests as $input => $expected) { + $output = token_replace($input, array('node' => $poll), array('language' => $language)); + $this->assertFalse(strcmp($output, $expected), t('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->assertFalse(strcmp($output, $expected), t('Unsanitized poll token %token replaced.', array('%token' => $input))); + } + } +} diff --git a/modules/poll/poll.tokens.inc b/modules/poll/poll.tokens.inc index 6affdb74a..6f931ac51 100644 --- a/modules/poll/poll.tokens.inc +++ b/modules/poll/poll.tokens.inc @@ -41,6 +41,14 @@ function poll_token_info() { */ function poll_tokens($type, $tokens, array $data = array(), array $options = array()) { $sanitize = !empty($options['sanitize']); + if (isset($options['language'])) { + $url_options['language'] = $options['language']; + $language_code = $options['language']->language; + } + else { + $language_code = NULL; + } + $replacements = array(); if ($type == 'node' && !empty($data['node']) && $data['node']->type == 'poll') { @@ -65,12 +73,18 @@ function poll_tokens($type, $tokens, array $data = array(), array $options = arr if (isset($winner)) { $replacements[$original] = $sanitize ? filter_xss($winner['chtext']) : $winner['chtext']; } + else { + $replacements[$original] = ''; + } break; case 'poll-winner-votes': if (isset($winner)) { $replacements[$original] = $winner['chvotes']; } + else { + $replacements[$original] = ''; + } break; case 'poll-winner-percent': @@ -78,6 +92,9 @@ function poll_tokens($type, $tokens, array $data = array(), array $options = arr $percent = ($winner['chvotes'] / $total_votes) * 100; $replacements[$original] = number_format($percent, 0); } + else { + $replacements[$original] = ''; + } break; case 'poll-duration': |