You are here

public function PollVoteMultilingualTest::testPollVoteMultilingual in Poll 8

Tests multilingual voting on a poll.

File

tests/src/Functional/PollVoteMultilingualTest.php, line 56

Class

PollVoteMultilingualTest
Tests multilingual voting on a poll.

Namespace

Drupal\Tests\poll\Functional

Code

public function testPollVoteMultilingual() {
  $this
    ->drupalLogin($this->admin_user);

  // Add another language.
  $language = ConfigurableLanguage::createFromLangcode('ca');
  $language
    ->save();

  // Make poll translatable.
  $this
    ->drupalGet('admin/config/regional/content-language');
  $edit = array(
    'entity_types[poll]' => TRUE,
    'entity_types[poll_choice]' => TRUE,
    'settings[poll][poll][translatable]' => TRUE,
    'settings[poll_choice][poll_choice][translatable]' => TRUE,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save configuration'));
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();

  // Translate a poll.
  $this
    ->drupalGet('poll/' . $this->poll
    ->id() . '/translations');
  $this
    ->clickLink(t('Add'));
  $edit = array(
    'question[0][value]' => 'ca question',
    'choice[0][choice]' => 'ca choice 1',
    'choice[1][choice]' => 'ca choice 2',
    'choice[2][choice]' => 'ca choice 3',
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->drupalGet('ca/poll/' . $this->poll
    ->id());
  $this
    ->assertText('ca choice 1');
  \Drupal::entityTypeManager()
    ->getStorage('poll')
    ->resetCache();
  \Drupal::entityTypeManager()
    ->getStorage('poll_choice')
    ->resetCache();
  $this->poll = Poll::load($this->poll
    ->id());

  // Login as web user.
  $this
    ->drupalLogin($this->web_user);

  // Record a vote.
  $edit = array(
    'choice' => $this
      ->getChoiceId($this->poll, 2),
  );
  $this
    ->drupalPostForm('poll/' . $this->poll
    ->id(), $edit, t('Vote'));
  $this
    ->assertText('Your vote has been recorded.');
  $this
    ->assertText('Total votes: 1');
  $this
    ->drupalGet('ca/poll/' . $this->poll
    ->id());
  $elements = $this
    ->xpath('//input[@value="Cancel vote"]');
  $this
    ->assertTrue(isset($elements[0]), "'Cancel vote' button appears.");

  // Cancel a vote.
  $this
    ->drupalPostForm('poll/' . $this->poll
    ->id(), array(), t('Cancel vote'));
  $this
    ->assertText('Your vote was cancelled.');
  $this
    ->assertNoText('Cancel your vote');

  // Vote again in reverse order.
  $edit = array(
    'choice' => $this
      ->getChoiceIdByLabel($this->poll
      ->getTranslation('ca'), 'ca choice 2'),
  );
  $this
    ->drupalPostForm('ca/poll/' . $this->poll
    ->id(), $edit, t('Vote'));
  $this
    ->assertText('Your vote has been recorded.');
  $this
    ->assertText('Total votes: 1');
  $this
    ->drupalGet('poll/' . $this->poll
    ->id());
  $elements = $this
    ->xpath('//input[@value="Cancel vote"]');
  $this
    ->assertTrue(isset($elements[0]), "'Cancel vote' button appears.");

  // Edit the original poll.
  $this
    ->drupalLogin($this->admin_user);
  $this
    ->drupalGet('poll/' . $this->poll
    ->id() . '/edit');
  $edit = array(
    'choice[0][choice]' => '',
    'choice[1][choice]' => 'choice 2',
    'choice[2][choice]' => 'choice 3',
    'choice[3][choice]' => 'choice 4',
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));

  // Translate the new label.
  $this
    ->drupalGet('ca/poll/' . $this->poll
    ->id() . '/edit');
  $edit = array(
    'choice[2][choice]' => 'ca choice 4',
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  \Drupal::entityTypeManager()
    ->getStorage('poll')
    ->resetCache();
  \Drupal::entityTypeManager()
    ->getStorage('poll_choice')
    ->resetCache();
  $this->poll = Poll::load($this->poll
    ->id());

  // Vote as anonymous user.
  $this
    ->drupalLogout();
  $edit = array(
    'choice' => $this
      ->getChoiceIdByLabel($this->poll
      ->getTranslation('ca'), 'ca choice 4'),
  );
  $this
    ->drupalPostForm('ca/poll/' . $this->poll
    ->id(), $edit, t('Vote'));
  $this
    ->assertText('Your vote has been recorded.');
  $this
    ->assertText('Total votes: 2');
  $this
    ->assertNoText('ca choice 1');
  $this
    ->assertText('ca choice 4');
  $elements = $this
    ->xpath('//*[@id="poll-view-form-2"]/div[1]/dl/dd[1]')[0];
  $this
    ->assertEqual($elements
    ->getText(), '50% (1 vote)');
  $elements = $this
    ->xpath('//*[@id="poll-view-form-2"]/div[1]/dl/dd[3]')[0];
  $this
    ->assertEqual($elements
    ->getText(), '50% (1 vote)');
  $this
    ->drupalGet('poll/' . $this->poll
    ->id());
  $elements = $this
    ->xpath('//input[@value="Cancel vote"]');
  $this
    ->assertTrue(isset($elements[0]), "'Cancel vote' button appears.");
  $this
    ->assertText('Total votes: 2');
  $elements = $this
    ->xpath('//*[@id="poll-view-form-2"]/div[1]/dl/dd[1]')[0];
  $this
    ->assertEqual($elements
    ->getText(), '50% (1 vote)');
  $elements = $this
    ->xpath('//*[@id="poll-view-form-2"]/div[1]/dl/dd[3]')[0];
  $this
    ->assertEqual($elements
    ->getText(), '50% (1 vote)');
}