View source
<?php
namespace Drupal\Tests\poll\Functional;
use Drupal\Core\Database\Database;
use Drupal\poll\PollInterface;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
class PollVoteTest extends PollTestBase {
function testPollVote() {
$this
->drupalLogin($this->web_user);
$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
->drupalPostForm('poll/' . $this->poll
->id(), array(), t('Cancel vote'));
$this
->assertText('Your vote was cancelled.');
$this
->assertNoText('Cancel your vote');
$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.");
$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();
$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');
$this
->clickLink($this->poll
->label());
$this
->clickLink('Edit');
$edit = array(
'runtime' => 172800,
'anonymous_vote_allow[value]' => TRUE,
);
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertText('The poll ' . $this->poll
->label() . ' has been updated.');
$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);
$assert_session
->elementContains('css', 'tbody tr:nth-child(1) td:nth-child(3)', 'On');
$assert_session
->elementContains('css', 'tbody tr:nth-child(1) td:nth-child(5)', '1');
$anonymous_role = Role::load(RoleInterface::ANONYMOUS_ID);
$anonymous_role
->grantPermission('access polls');
$anonymous_role
->save();
$this
->drupalLogout();
$edit = [
'choice' => '1',
];
$this
->drupalPostForm('poll/' . $this->poll
->id(), $edit, t('Vote'));
$this
->drupalLogin($this->admin_user);
$this
->drupalGet('admin/content/poll');
$xpath = "//tr[1]/td[@class='views-field views-field-votes']";
$this
->assertFieldByXPath($xpath, 2);
$this
->drupalLogin($this->web_user);
$this
->drupalPostForm('poll/' . $this->poll
->id(), [], t('Cancel vote'));
$this
->assertText(t('Your vote was cancelled.'));
$this
->drupalLogin($this->admin_user);
$this
->drupalGet('admin/content/poll');
$xpath = "//tr[1]/td[@class='views-field views-field-votes']";
$this
->assertFieldByXPath($xpath, 1);
$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.");
}
public function testClosedPollVoteCancel() {
$poll = $this
->pollCreate();
$this
->drupalLogin($this->web_user);
$choices = $poll->choice
->getValue();
$this
->drupalGet('poll/' . $poll
->id());
$edit = array(
'choice' => $choices[0]['target_id'],
);
$this
->drupalPostForm(NULL, $edit, t('Vote'));
$elements = $this
->xpath('//input[@value="Cancel vote"]');
$this
->assertTrue(isset($elements[0]), "'Cancel your vote' button appears.");
$this
->drupalLogin($this->admin_user);
$this
->drupalGet('poll/' . $poll
->id() . '/edit');
$edit = [
'status[value]' => FALSE,
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->drupalLogin($this->web_user);
$this
->drupalGet('poll/' . $poll
->id());
$elements = $this
->xpath('//input[@value="Cancel vote"]');
$this
->assertFalse(isset($elements[0]), "'Cancel your vote' button not appears.");
}
public function testAnonymousCancelVote() {
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, array(
'cancel own vote',
'access polls',
));
$this->poll
->setAnonymousVoteAllow(TRUE)
->save();
$this
->drupalLogout();
$edit = array(
'choice' => '1',
);
$this
->drupalPostForm('poll/' . $this->poll
->id(), $edit, t('Vote'));
Database::getConnection()
->update('poll_vote')
->fields(array(
'hostname' => '240.0.0.1',
))
->condition('uid', \Drupal::currentUser()
->id())
->execute();
$this
->drupalLogin($this->web_user);
$this
->drupalPostForm('poll/' . $this->poll
->id(), $edit, t('Vote'));
$this
->assertText(t('Total votes: @votes', array(
'@votes' => 2,
)));
$this
->drupalLogout();
$this
->drupalPostForm('poll/' . $this->poll
->id(), $edit, t('Vote'));
$this
->assertText(t('Total votes: @votes', array(
'@votes' => 3,
)));
$this
->drupalPostForm(NULL, array(), t('Cancel vote'));
$this
->drupalPostForm('poll/' . $this->poll
->id(), $edit, t('Vote'));
$this
->assertText(t('Total votes: @votes', array(
'@votes' => 3,
)));
}
}