View source
<?php
namespace Drupal\Tests\poll\Functional;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AnonymousUserSession;
class PollVoteCheckHostnameTest extends PollTestBase {
function setUp() {
parent::setUp();
user_role_change_permissions(AccountInterface::ANONYMOUS_ROLE, array(
'cancel own vote' => TRUE,
'access polls' => TRUE,
));
$this->poll
->setAnonymousVoteAllow(TRUE)
->save();
}
function testHostnamePollVote() {
$web_user2 = $this
->drupalCreateUser(array(
'access polls',
));
$this
->drupalLogin($this->web_user);
$edit = array(
'choice' => '1',
);
$this
->drupalPostForm('poll/' . $this->poll
->id(), $edit, t('Vote'));
$this
->assertText(t('Your vote has been recorded.'));
$this
->assertText(t('Total votes: @votes', array(
'@votes' => 1,
)));
$this
->drupalGet('poll/' . $this->poll
->id());
$elements = $this
->xpath('//input[@value="Vote"]');
$this
->assertTrue(empty($elements), $this->web_user
->getAccountName() . " is not able to vote again.");
$elements = $this
->xpath('//input[@value="Cancel vote"]');
$this
->assertTrue(!empty($elements), "'Cancel vote' button appears.");
$this
->drupalLogout();
$this
->drupalGet('poll/' . $this->poll
->id());
$this
->assertEqual($this
->drupalGetHeader('x-drupal-cache'), 'MISS', 'Page was cacheable but was not in the cache.');
$this
->drupalGet('poll/' . $this->poll
->id());
$this
->assertEqual($this
->drupalGetHeader('x-drupal-cache'), 'HIT', 'Page was cached.');
$this
->drupalPostForm(NULL, $edit, t('Vote'));
$this
->assertText(t('Your vote has been recorded.'));
$this
->assertText(t('Total votes: @votes', array(
'@votes' => 2,
)));
$elements = $this
->xpath('//input[@value="Cancel vote"]');
$this
->assertTrue(!empty($elements), "'Cancel vote' button appears.");
$this
->drupalGet('poll/' . $this->poll
->id());
$this
->assertNull($this
->drupalGetHeader('x-drupal-cache'), 'Page was not cacheable.');
$elements = $this
->xpath('//input[@value="Vote"]');
$this
->assertTrue(empty($elements), "Anonymous is not able to vote again.");
$elements = $this
->xpath('//input[@value="Cancel vote"]');
$this
->assertTrue(!empty($elements), "'Cancel vote' button appears.");
$this
->drupalLogin($web_user2);
$this
->drupalPostForm('poll/' . $this->poll
->id(), $edit, t('Vote'));
$this
->assertText(t('Your vote has been recorded.'));
$this
->assertText(t('Total votes: @votes', array(
'@votes' => 3,
)));
$elements = $this
->xpath('//input[@value="Cancel vote"]');
$this
->assertTrue(empty($elements), "'Cancel vote' button does not appear.");
$this
->drupalLogout();
\Drupal::database()
->update('poll_vote')
->fields(array(
'hostname' => '123.456.789.1',
))
->condition('hostname', '', '<>')
->execute();
$this
->drupalGet('poll/' . $this->poll
->id());
$this
->assertEqual($this
->drupalGetHeader('x-drupal-cache'), 'HIT', 'Cached page return.');
$this
->drupalPostForm(NULL, $edit, t('Vote'));
$this
->assertText(t('Your vote has been recorded.'));
$this
->assertText(t('Total votes: @votes', array(
'@votes' => 4,
)));
$elements = $this
->xpath('//input[@value="Cancel vote"]');
$this
->assertTrue(!empty($elements), "'Cancel vote' button appears.");
$this
->getSession()
->setCookie($this
->getSessionName());
$this
->drupalGet('poll/' . $this->poll
->id());
$this
->assertEqual($this
->drupalGetHeader('x-drupal-cache'), 'HIT', 'Page was cacheable but was not in the cache.');
$this
->drupalPostForm(NULL, $edit, t('Vote'));
$this
->assertText(t('Your vote for this poll has already been submitted.'));
$elements = $this
->xpath('//input[@value="Cancel vote"]');
$this
->assertTrue(empty($elements), "'Cancel vote' button does not appear.");
$this
->drupalLogin($this->web_user);
$this
->drupalGet('poll/' . $this->poll
->id());
$elements = $this
->xpath('//input[@value="Vote"]');
$this
->assertTrue(empty($elements), $this->web_user
->getAccountName() . " is not able to vote again.");
$elements = $this
->xpath('//input[@value="Cancel vote"]');
$this
->assertTrue(!empty($elements), "'Cancel vote' button appears.");
}
}