You are here

public function PollCreateTest::testPollCreate in Poll 8

Tests creating and editing a poll.

File

tests/src/Functional/PollCreateTest.php, line 15

Class

PollCreateTest
Tests creating a poll.

Namespace

Drupal\Tests\poll\Functional

Code

public function testPollCreate() {
  $poll = $this->poll;

  // Check we loaded the right poll.
  $this
    ->drupalLogin($this->admin_user);
  $this
    ->drupalGet('poll/' . $poll
    ->id() . '/edit');
  $this
    ->assertText($poll
    ->label());

  // Verify applying condition for non-active polls.
  $this
    ->drupalGet('admin/content/poll', [
    'query' => [
      'status' => '2',
    ],
  ]);
  $this
    ->assertNoText($poll
    ->label());

  // Verify poll appears on 'poll' page.
  $this
    ->drupalGet('admin/content/poll');
  $this
    ->assertText($poll
    ->label());
  $this
    ->assertText('Y');

  // Click on the poll question to go to poll page.
  $this
    ->clickLink($poll
    ->label());

  // Alter the question and ensure it gets saved correctly.
  $new_question = $this
    ->randomMachineName();
  $poll
    ->setQuestion($new_question);
  $poll
    ->save();

  // Check the new question has taken effect.
  $this
    ->drupalGet('poll/' . $poll
    ->id() . '/edit');
  $this
    ->assertText($new_question);

  // Now add a new option to make sure that when we update the poll, the
  // option is displayed.
  $vote_choice = $this
    ->randomMachineName();
  $poll->choice[0]->entity
    ->setChoice($vote_choice);
  $poll->choice[0]->entity
    ->save();

  // Check the new choice has taken effect.
  $this
    ->drupalGet('poll/' . $poll
    ->id() . '/edit');
  $this
    ->assertFieldByXPath("//input[@name='choice[0][choice]']", $vote_choice, 'Choice successfully changed.');
}