You are here

protected function PollVoteJavascriptTest::pollCreate in Poll 8

Creates a poll.

Parameters

int $choice_count: (optional) The number of choices to generate. Defaults to 7.

Return value

mixed The node id of the created poll, or FALSE on error.

1 call to PollVoteJavascriptTest::pollCreate()
PollVoteJavascriptTest::setUp in tests/src/FunctionalJavascript/PollVoteJavascriptTest.php

File

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

Class

PollVoteJavascriptTest
Tests voting on a poll using Javascript.

Namespace

Drupal\Tests\poll\FunctionalJavascript

Code

protected function pollCreate($choice_count = 5) {
  $this
    ->drupalLogin($this->adminUser);

  // Get the form first to initialize the state of the internal browser.
  $this
    ->drupalGet('poll/add');
  $question = $this
    ->randomMachineName();
  $choices = $this
    ->generateChoices($choice_count);
  list($edit, $index) = $this
    ->pollGenerateEdit($question, $choices);
  $session = $this
    ->getSession();

  // Re-submit the form until all choices are filled in.
  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'));

  // Load the poll.
  $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.');

  /** @var \Drupal\poll\PollInterface $poll */
  $poll = reset($polls);
  return $poll instanceof PollInterface ? $poll : FALSE;
}