- Patch #602998 by grndlvl, asimmonds: Fixed polls being broken for anonymous users. With tests.

merge-requests/26/head
Dries Buytaert 2010-02-02 07:39:21 +00:00
parent cc4e41583d
commit d8eafc21ba
2 changed files with 110 additions and 1 deletions

View File

@ -720,7 +720,8 @@ function poll_vote($form, &$form_state) {
'nid' => $node->nid,
'chid' => $choice,
'uid' => $user->uid,
'hostname' => $user->uid ? ip_address() : '',
'hostname' => $user->uid ? '' : ip_address(),
'timestamp' => REQUEST_TIME,
))
->execute();

View File

@ -349,3 +349,111 @@ class PollJSAddChoice extends DrupalWebTestCase {
$this->assertFieldByName('choice[new:0][chtext]', '', t('Field !i found', array('!i' => 2)));
}
}
class PollVoteCheckHostname extends PollTestCase {
public static function getInfo() {
return array(
'name' => 'User poll vote capability.',
'description' => 'Check that users and anonymous users from specified ip-address can only vote once.',
'group' => 'Poll'
);
}
function setUp() {
parent::setUp('poll');
// Create and login user.
$this->admin_user = $this->drupalCreateUser(array('administer permissions', 'create poll content'));
$this->drupalLogin($this->admin_user);
// Allow anonymous users to vote on polls.
user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
'access content' => TRUE,
'vote on polls' => TRUE,
));
// Create poll.
$title = $this->randomName();
$choices = $this->_generateChoices(3);
$this->poll_nid = $this->pollCreate($title, $choices, FALSE);
$this->drupalLogout();
// Create web users.
$this->web_user1 = $this->drupalCreateUser(array('access content', 'vote on polls'));
$this->web_user2 = $this->drupalCreateUser(array('access content', 'vote on polls'));
}
/**
* Check that anonymous users with same ip cannot vote on poll more than once
* unless user is logged in.
*/
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.'), t('%user vote was recorded.', array('%user' => $this->web_user1->name)));
$this->assertText(t('Total votes: @votes', array('@votes' => 1)), t('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), t("%user is not able to vote again.", array('%user' => $this->web_user1->name)));
// Logout User1.
$this->drupalLogout();
// Anonymous user vote on Poll.
$this->drupalPost('node/' . $this->poll_nid, $edit, t('Vote'));
$this->assertText(t('Your vote was recorded.'), t('Anonymous vote was recorded.'));
$this->assertText(t('Total votes: @votes', array('@votes' => 2)), t('Vote count updated correctly.'));
// Check to make sure Anonymous user cannot vote again.
$this->drupalGet('node/' . $this->poll_nid);
$elements = $this->xpath('//input[@value="Vote"]');
$this->assertTrue(empty($elements), t("Anonymous is not able to vote again."));
// 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.'), t('%user vote was recorded.', array('%user' => $this->web_user2->name)));
$this->assertText(t('Total votes: @votes', array('@votes' => 3)), 'Vote count updated correctly.');
// Logout User2.
$this->drupalLogout();
// Change host name for anonymous users.
db_update('poll_vote')
->fields(array(
'hostname' => '123.456.789.20',
))
->condition('hostname', '', '!=')
->execute();
// Check to make sure Anonymous user can vote again after hostname change.
$this->drupalPost('node/' . $this->poll_nid, $edit, t('Vote'));
$this->assertText(t('Your vote was recorded.'), t('%user vote was recorded.', array('%user' => $this->web_user2->name)));
$this->assertText(t('Total votes: @votes', array('@votes' => 4)), 'Vote count updated correctly.');
// Check to make sure Anonymous user cannot vote again.
$this->drupalGet('node/' . $this->poll_nid);
$elements = $this->xpath('//input[@value="Vote"]');
$this->assertTrue(empty($elements), t("Anonymous is not able to vote again."));
// 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), t("%user is not able to vote again.", array('%user' => $this->web_user1->name)));
}
}