View source
<?php
namespace Drupal\Tests\poll\FunctionalJavascript;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\poll\PollInterface;
use Symfony\Component\CssSelector\CssSelectorConverter;
class PollVoteJavascriptTest extends WebDriverTestBase {
protected $adminUser;
protected $webUser;
protected $poll;
protected $adminPermissions = [];
protected $webUserPermissions = [];
public static $modules = [
'poll',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser(array_merge([
'administer polls',
'access polls',
], $this->adminPermissions));
$this->webUser = $this
->drupalCreateUser(array_merge([
'access polls',
'cancel own vote',
], $this->webUserPermissions));
$this->poll = $this
->pollCreate();
}
protected function pollCreate($choice_count = 5) {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('poll/add');
$question = $this
->randomMachineName();
$choices = $this
->generateChoices($choice_count);
list($edit, $index) = $this
->pollGenerateEdit($question, $choices);
$session = $this
->getSession();
if (count($choices) > 0) {
for ($delta = 0; $delta <= count($choices); $delta++) {
$this
->submitForm($edit, t('Add another item'));
list($edit, $index) = $this
->pollGenerateEdit($question, $choices, $index);
$session
->wait(1000, 'jQuery("[id^=edit-choice-' . $delta . '-choice]").length > 0');
}
}
$this
->submitForm($edit, t('Save'));
$polls = \Drupal::entityTypeManager()
->getStorage('poll')
->loadByProperties([
'question' => $question,
]);
$page = $session
->getPage();
$this
->assertTrue($page
->hasContent(new FormattableMarkup('The poll @question has been added.', [
'@question' => $question,
])), 'Poll has been created.');
$this
->assertFalse(empty($polls), 'Poll has been found in the database.');
$poll = reset($polls);
return $poll instanceof PollInterface ? $poll : FALSE;
}
private function pollGenerateEdit($question, array $choices, $index = 0) {
$max_new_choices = 1;
$already_submitted_choices = array_slice($choices, 0, $index);
$new_choices = array_values(array_slice($choices, $index, $max_new_choices));
$edit = [
'question[0][value]' => $question,
];
foreach ($already_submitted_choices as $k => $text) {
$edit['choice[' . $k . '][choice]'] = $text;
}
foreach ($new_choices as $k => $text) {
$edit['choice[' . $k . '][choice]'] = $text;
}
return [
$edit,
count($already_submitted_choices) + count($new_choices),
];
}
private function generateChoices($count = 7) {
$choices = [];
for ($i = 1; $i <= $count; $i++) {
$choices[] = $this
->randomMachineName();
}
return $choices;
}
public function testAjaxPollVote() {
$this
->drupalLogin($this->webUser);
$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.');
$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.");
$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.");
}
}