Index: modules/poll/poll.test =================================================================== RCS file: /cvs/drupal/drupal/modules/poll/poll.test,v retrieving revision 1.5 diff -u -r1.5 poll.test --- modules/poll/poll.test 5 Aug 2008 18:07:14 -0000 1.5 +++ modules/poll/poll.test 14 Oct 2008 06:45:08 -0000 @@ -108,6 +108,15 @@ function tearDown() { parent::tearDown(); } + + /** + * Assert that the first vote count is $num. + * @todo generalize for all of the vote counts in the poll. + */ + function assertVoteCount($num) { + preg_match('@\(([0-9]*) '. t('votes') . '\)@' , $this->drupalGetContent(), $matchs); + $this->assertTrue(($num === (int)$matchs[1]), t('The results should have vote count of '. $num .', got '. $matchs[1] .'.')); + } function testPollVote() { $title = $this->randomName(); @@ -129,5 +138,86 @@ $this->assertText(t('This table lists all the recorded votes for this poll. If anonymous users are allowed to vote, they will be identified by the IP address of the computer they used when they voted.'), 'Vote table text.'); $this->assertText($choices[0], 'Vote recorded'); } + + /** + * Test anonymous user poll vote + * @see http://drupal.org/node/295994 + */ + function testAnonymousPollVote() { + // create a poll + $title = $this->randomName(); + $choices = $this->_generateChoices(7); + $poll_nid = $this->pollCreate($title, $choices, FALSE); + + $this->drupalLogout(); + // anonymous: check that we are not allowed to vote, but can view the node + $this->drupalGet('node/'.$poll_nid); + $this->assertResponse(200, t('Poll is accessible')); + $this->assertNoRaw(t('Vote'), t('We should NOT see the vote-button (by label)')); + $this->assertNoRaw('edit-vote', t('We should NOT see the vote-button (by id)')); + + // login in as admin user w/ permissions control + $poll_admin = $this->drupalCreateUser(array('create poll content', 'access content', 'administer permissions')); + $this->drupalLogin($poll_admin); + // and allow anonymous to post + $this->drupalGet('admin/user/permissions'); + $this->assertResponse(200, t('We should be able to view this')); + $post_values = array('1[cancel own vote]'=>'checked', '1[vote on polls]'=>'checked'); + $this->drupalPost('admin/user/permissions', $post_values, t('Save permissions')); + $this->drupalLogout(); + + // from now on as anonymous + $this->drupalGet('node/'.$poll_nid); + $this->assertResponse(200, t('We should be able to access a poll')); + $this->drupalGet('node/'.$poll_nid); + // @note: i assert both for label and id, which could be superfluous but i could not get assertFieldByName/assertFieldById to work ... + // there should be a Vote button + $this->assertRaw(t('Vote'), t('We should see the vote-button (by label)')); + // search the raw html for the id ... + $this->assertRaw('edit-vote', t('We should see the vote-button (by id)')); + // post a choice + $edit = array('choice'=>1); + $this->drupalPost('node/'. $poll_nid, $edit, t('Vote')); + $this->assertText(t('Your vote was recorded.'), t('Feedback text present')); + $this->assertRaw(t('Cancel your vote'), t('We should be able to see "cancel the vote"-button (by label)')); + $this->assertRaw('edit-poll-cancel-form', t('We should be able to see the "cancel the vote"-button (by id)')); + + // we check now that the vote button isnt there anymore und then cancel our vote and then the vote button is there again ... + $this->drupalGet('node/'.$poll_nid); + $this->assertNoRaw(t('Vote'), t('We should NOT see the vote-button (by label) anymore')); + // search the raw html for the id ... + $this->assertNoRaw('edit-vote', t('We should NOT see the vote-button (by id) anymore')); + $this->assertRaw('edit-poll-cancel-form', t('We should be able to see the "cancel the vote"-button (by id)')); + + // cancel the vote - and we should see the vote button + $this->drupalPost('node/'. $poll_nid, array(), t('Cancel your vote')); + $this->assertRaw(t('Vote'), t('We should see the vote-button (by label)')); + // search the raw html for the id ... + $this->assertRaw('edit-vote', t('We should see the vote-button (by id)')); + + // remove the 'cancel vote'-permissions and check again + $this->drupalLogin($poll_admin); + $this->drupalGet('admin/user/permissions'); + $this->assertResponse(200, t('We should be able to view this')); + $post_values = array('1[cancel own vote]'=>false, '1[vote on polls]'=>'checked'); + $this->drupalPost('admin/user/permissions', $post_values, t('Save permissions')); + $this->drupalLogout(); + + // now as anonymous + // post a choice + $edit = array('choice'=>1); + $this->drupalPost('node/'. $poll_nid, $edit, t('Vote')); + $this->assertText(t('Your vote was recorded.'), t('Feedback text present')); + // but no more cancelling available + $this->assertNoRaw(t('Cancel your vote'), t('We should be able to see "cancel the vote"-button (by label)')); + $this->assertNoRaw('edit-poll-cancel-form', t('We should be able to see the "cancel the vote"-button (by id)')); + + // Check the no. of votes. + // HTML per choice:
123
100% (13 votes)
+ $this->assertVoteCount(0); + + // @todo: try to force an invalid choice, and see how the poll.module deals with that - but drupalPost prevents that + // $this->drupalPost('node/'.$poll_nid, array('choice'=>-999999), t('Vote')) - thus fails before it "posts" (see drupal_web_test_case.php:920-922) + } }