function PollVoteCheckHostname::testHostnamePollVote in Drupal 7
Check that anonymous users with same ip cannot vote on poll more than once unless user is logged in.
File
- modules/
poll/ poll.test, line 522 - Tests for poll.module.
Class
Code
function testHostnamePollVote() {
// Login User1.
$this
->drupalLogin($this->web_user1);
$edit = array(
'choice' => '1',
);
// User1 vote on Poll.
$this
->drupalPost('node/' . $this->poll_nid, $edit, t('Vote'));
$this
->assertText(t('Your vote was recorded.'), format_string('%user vote was recorded.', array(
'%user' => $this->web_user1->name,
)));
$this
->assertText(t('Total votes: @votes', array(
'@votes' => 1,
)), 'Vote count updated correctly.');
// Check to make sure User1 cannot vote again.
$this
->drupalGet('node/' . $this->poll_nid);
$elements = $this
->xpath('//input[@value="Vote"]');
$this
->assertTrue(empty($elements), format_string("%user is not able to vote again.", array(
'%user' => $this->web_user1->name,
)));
$elements = $this
->xpath('//input[@value="Cancel your vote"]');
$this
->assertTrue(!empty($elements), "'Cancel your vote' button appears.");
// Logout User1.
$this
->drupalLogout();
// Fill the page cache by requesting the poll.
$this
->drupalGet('node/' . $this->poll_nid);
$this
->assertEqual($this
->drupalGetHeader('x-drupal-cache'), 'MISS', 'Page was cacheable but was not in the cache.');
$this
->drupalGet('node/' . $this->poll_nid);
$this
->assertEqual($this
->drupalGetHeader('x-drupal-cache'), 'HIT', 'Page was cached.');
// Anonymous user vote on Poll.
$this
->drupalPost(NULL, $edit, t('Vote'));
$this
->assertText(t('Your vote was recorded.'), 'Anonymous vote was recorded.');
$this
->assertText(t('Total votes: @votes', array(
'@votes' => 2,
)), 'Vote count updated correctly.');
$elements = $this
->xpath('//input[@value="Cancel your vote"]');
$this
->assertTrue(!empty($elements), "'Cancel your vote' button appears.");
// Check to make sure Anonymous user cannot vote again.
$this
->drupalGet('node/' . $this->poll_nid);
$this
->assertFalse($this
->drupalGetHeader('x-drupal-cache'), 'Page was not cacheable.');
$elements = $this
->xpath('//input[@value="Vote"]');
$this
->assertTrue(empty($elements), "Anonymous is not able to vote again.");
$elements = $this
->xpath('//input[@value="Cancel your vote"]');
$this
->assertTrue(!empty($elements), "'Cancel your vote' button appears.");
// Login User2.
$this
->drupalLogin($this->web_user2);
// User2 vote on poll.
$this
->drupalPost('node/' . $this->poll_nid, $edit, t('Vote'));
$this
->assertText(t('Your vote was recorded.'), format_string('%user vote was recorded.', array(
'%user' => $this->web_user2->name,
)));
$this
->assertText(t('Total votes: @votes', array(
'@votes' => 3,
)), 'Vote count updated correctly.');
$elements = $this
->xpath('//input[@value="Cancel your vote"]');
$this
->assertTrue(empty($elements), "'Cancel your vote' button does not appear.");
// Logout User2.
$this
->drupalLogout();
// Change host name for anonymous users.
db_update('poll_vote')
->fields(array(
'hostname' => '123.456.789.1',
))
->condition('hostname', '', '<>')
->execute();
// Check to make sure Anonymous user can vote again with a new session after
// a hostname change.
$this
->drupalGet('node/' . $this->poll_nid);
$this
->assertEqual($this
->drupalGetHeader('x-drupal-cache'), 'MISS', 'Page was cacheable but was not in the cache.');
$this
->drupalPost(NULL, $edit, t('Vote'));
$this
->assertText(t('Your vote was recorded.'), format_string('%user vote was recorded.', array(
'%user' => $this->web_user2->name,
)));
$this
->assertText(t('Total votes: @votes', array(
'@votes' => 4,
)), 'Vote count updated correctly.');
$elements = $this
->xpath('//input[@value="Cancel your vote"]');
$this
->assertTrue(!empty($elements), "'Cancel your vote' button appears.");
// Check to make sure Anonymous user cannot vote again with a new session,
// and that the vote from the previous session cannot be cancelledd.
$this
->curlClose();
$this
->drupalGet('node/' . $this->poll_nid);
$this
->assertEqual($this
->drupalGetHeader('x-drupal-cache'), 'MISS', 'Page was cacheable but was not in the cache.');
$elements = $this
->xpath('//input[@value="Vote"]');
$this
->assertTrue(empty($elements), 'Anonymous is not able to vote again.');
$elements = $this
->xpath('//input[@value="Cancel your vote"]');
$this
->assertTrue(empty($elements), "'Cancel your vote' button does not appear.");
// Login User1.
$this
->drupalLogin($this->web_user1);
// Check to make sure User1 still cannot vote even after hostname changed.
$this
->drupalGet('node/' . $this->poll_nid);
$elements = $this
->xpath('//input[@value="Vote"]');
$this
->assertTrue(empty($elements), format_string("%user is not able to vote again.", array(
'%user' => $this->web_user1->name,
)));
$elements = $this
->xpath('//input[@value="Cancel your vote"]');
$this
->assertTrue(!empty($elements), "'Cancel your vote' button appears.");
}