You are here

public function PollVoteJavascriptTest::testAjaxPollVote in Poll 8

Tests voting on a poll using AJAX.

File

tests/src/FunctionalJavascript/PollVoteJavascriptTest.php, line 182

Class

PollVoteJavascriptTest
Tests voting on a poll using Javascript.

Namespace

Drupal\Tests\poll\FunctionalJavascript

Code

public function testAjaxPollVote() {
  $this
    ->drupalLogin($this->webUser);

  // Empty vote on a poll.
  $this
    ->drupalGet('poll/' . $this->poll
    ->id());
  $this
    ->submitForm([], 'Vote', 'poll-view-form-1');
  $session = $this
    ->getSession();
  $session
    ->wait(1000, 'jQuery(".messages--error").length > 0');
  $page = $session
    ->getPage();
  $this
    ->assertTrue($page
    ->hasContent('Your vote could not be recorded because you did not select any of the choices.'), 'Vote can not be empty.');
  $this
    ->assertTrue($page
    ->hasButton('Vote'), "'Vote' button appears.");
  $converter = new CssSelectorConverter();
  $xpath = $converter
    ->toXPath('.poll-view-form-' . $this->poll
    ->id());
  $this
    ->assertTrue($session
    ->getDriver()
    ->isVisible($xpath), 'The vote form is visible.');

  // Record a vote for the first choice.
  $edit = [
    'choice' => '1',
  ];
  $this
    ->drupalGet('poll/' . $this->poll
    ->id());
  $this
    ->submitForm($edit, 'Vote', 'poll-view-form-1');
  $session
    ->wait(1000, 'jQuery(".messages--status").length > 0');
  $page = $session
    ->getPage();
  $this
    ->assertTrue($page
    ->hasContent('Your vote has been recorded.'), 'Your vote was recorded.');
  $this
    ->assertTrue($page
    ->hasContent('Total votes: 1'), 'Vote count updated correctly.');
  $this
    ->assertCount(1, $this
    ->cssSelect('.choice-title.is-current-selection'));
  $this
    ->assertCount(4, $this
    ->cssSelect('.choice-title.not-current-selection'));
  $this
    ->assertTrue($page
    ->hasButton('Cancel vote'), "'Cancel your vote' button appears.");

  // Reload the page so that the messages are reset.
  $this
    ->drupalGet('poll/' . $this->poll
    ->id());
  $page
    ->pressButton('Cancel vote');
  $session
    ->wait(1000, 'jQuery(".messages--status").length > 0');
  $this
    ->assertTrue($page
    ->hasContent('Your vote was cancelled.'));
  $this
    ->assertTrue($page
    ->hasButton('Vote'), "Vote button appears.");
}