function PollVoteTest::testPollVote in Poll 8
Tests voting on a poll.
File
- tests/
src/ Functional/ PollVoteTest.php, line 20
Class
- PollVoteTest
- Tests voting on a poll.
Namespace
Drupal\Tests\poll\FunctionalCode
function testPollVote() {
$this
->drupalLogin($this->web_user);
// Record a vote for the first choice.
$edit = array(
'choice' => '1',
);
$this
->drupalPostForm('poll/' . $this->poll
->id(), $edit, t('Vote'));
$this
->assertText('Your vote has been recorded.');
$this
->assertText('Total votes: 1');
$elements = $this
->xpath('//input[@value="Cancel vote"]');
$this
->assertTrue(isset($elements[0]), "'Cancel your vote' button appears.");
// $this->drupalGet('poll/' . $this->poll->id() . '/votes');
// $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.');
// $options = $this->poll->getOptions();
// debug($options);
// $this->assertText($this->poll->getOptions()[0], 'Vote recorded');
// Ensure poll listing page has correct number of votes.
// $this->drupalGet('poll');
// $this->assertText($this->poll->label(), 'Poll appears in poll list.');
// $this->assertText('1 vote', 'Poll has 1 vote.');
// Cancel a vote.
$this
->drupalPostForm('poll/' . $this->poll
->id(), array(), t('Cancel vote'));
$this
->assertText('Your vote was cancelled.');
$this
->assertNoText('Cancel your vote');
// $this->drupalGet('poll/' . $this->poll->id() . '/votes');
// $this->assertNoText($choices[0], 'Vote cancelled');
// Ensure poll listing page has correct number of votes.
// $this->drupalGet('poll');
// $this->assertText($title, 'Poll appears in poll list.');
// $this->assertText('0 votes', 'Poll has 0 votes.');
// Log in as a user who can only vote on polls.
// $this->drupalLogout();
// $this->drupalLogin($restricted_vote_user);
// Empty vote on a poll.
$this
->drupalPostForm('poll/' . $this->poll
->id(), [], t('Vote'));
$this
->assertText('Your vote could not be recorded because you did not select any of the choices.');
$elements = $this
->xpath('//input[@value="Vote"]');
$this
->assertTrue(isset($elements[0]), "'Vote' button appears.");
// Vote on a poll.
$edit = array(
'choice' => '1',
);
$this
->drupalPostForm('poll/' . $this->poll
->id(), $edit, t('Vote'));
$this
->assertText('Your vote has been recorded.');
$this
->assertText('Total votes: 1');
$elements = $this
->xpath('//input[@value="Cancel your vote"]');
$this
->assertTrue(empty($elements), "'Cancel your vote' button does not appear.");
$this
->drupalLogin($this->admin_user);
$this
->drupalGet('admin/content/poll');
$this
->assertText($this->poll
->label());
$assert_session = $this
->assertSession();
// Test for the overview page.
$assert_session
->elementContains('css', 'tbody tr:nth-child(1) td:nth-child(2)', 'Yes');
$assert_session
->elementContains('css', 'tbody tr:nth-child(1) td:nth-child(3)', 'Off');
// Edit the poll.
$this
->clickLink($this->poll
->label());
$this
->clickLink('Edit');
// Add the runtime date and allow anonymous to vote.
$edit = array(
'runtime' => 172800,
'anonymous_vote_allow[value]' => TRUE,
);
$this
->drupalPostForm(NULL, $edit, t('Save'));
// Assert that editing was successful.
$this
->assertText('The poll ' . $this->poll
->label() . ' has been updated.');
// Check if the active label is correct.
$date = \Drupal::service('date.formatter')
->format($this->poll
->getCreated() + 172800, 'short');
$output = 'Yes (until ' . rtrim(strstr($date, '-', TRUE)) . ')';
$assert_session
->elementContains('css', 'tbody tr:nth-child(1) td:nth-child(2)', $output);
// Check if allow anonymous voting is on.
$assert_session
->elementContains('css', 'tbody tr:nth-child(1) td:nth-child(3)', 'On');
// Check the number of total votes.
$assert_session
->elementContains('css', 'tbody tr:nth-child(1) td:nth-child(5)', '1');
// Add permissions to anonymous user to view polls.
/** @var \Drupal\user\RoleInterface $anonymous_role */
$anonymous_role = Role::load(RoleInterface::ANONYMOUS_ID);
$anonymous_role
->grantPermission('access polls');
$anonymous_role
->save();
// Let the anonymous user to vote.
$this
->drupalLogout();
$edit = [
'choice' => '1',
];
$this
->drupalPostForm('poll/' . $this->poll
->id(), $edit, t('Vote'));
// Login as admin and check the number of total votes on the overview page.
$this
->drupalLogin($this->admin_user);
$this
->drupalGet('admin/content/poll');
$xpath = "//tr[1]/td[@class='views-field views-field-votes']";
$this
->assertFieldByXPath($xpath, 2);
// Cancel the vote from the user, ensure that backend updates.
$this
->drupalLogin($this->web_user);
$this
->drupalPostForm('poll/' . $this->poll
->id(), [], t('Cancel vote'));
$this
->assertText(t('Your vote was cancelled.'));
// Login as admin and check the number of total votes on the overview page.
$this
->drupalLogin($this->admin_user);
$this
->drupalGet('admin/content/poll');
$xpath = "//tr[1]/td[@class='views-field views-field-votes']";
$this
->assertFieldByXPath($xpath, 1);
// Test for the 'View results' button.
$this
->drupalLogin($this->admin_user);
$this
->drupalGet('poll/' . $this->poll
->id());
$elements = $this
->xpath('//input[@value="View results"]');
$this
->assertTrue(!empty($elements), "'View results' button appears.");
$this
->drupalLogin($this->web_user);
$this
->drupalGet('poll/' . $this->poll
->id());
$elements = $this
->xpath('//input[@value="View results"]');
$this
->assertTrue(empty($elements), "'View results' button doesn't appear.");
}