diff options
author | Dries Buytaert <dries@buytaert.net> | 2010-02-02 07:39:21 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2010-02-02 07:39:21 +0000 |
commit | d8eafc21bada749ebac32ec4b42f12c98006b119 (patch) | |
tree | 8bfd5d2779bafe21de734258762e07c46b0fe8b2 /modules/poll | |
parent | cc4e41583d60d71b54c4896be5eeff7be13a65a5 (diff) | |
download | brdo-d8eafc21bada749ebac32ec4b42f12c98006b119.tar.gz brdo-d8eafc21bada749ebac32ec4b42f12c98006b119.tar.bz2 |
- Patch #602998 by grndlvl, asimmonds: Fixed polls being broken for anonymous users. With tests.
Diffstat (limited to 'modules/poll')
-rw-r--r-- | modules/poll/poll.module | 3 | ||||
-rw-r--r-- | modules/poll/poll.test | 108 |
2 files changed, 110 insertions, 1 deletions
diff --git a/modules/poll/poll.module b/modules/poll/poll.module index 841510c59..88564424c 100644 --- a/modules/poll/poll.module +++ b/modules/poll/poll.module @@ -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(); diff --git a/modules/poll/poll.test b/modules/poll/poll.test index f3bf4f378..80af43ebf 100644 --- a/modules/poll/poll.test +++ b/modules/poll/poll.test @@ -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))); + } +} |