View source
<?php
namespace Drupal\Tests\poll\Functional;
use Drupal\poll\Entity\Poll;
class PollTokenReplaceTest extends PollTestBase {
public function testPollTokenReplacement() {
$poll = $this
->pollCreate(3);
$poll
->save();
$poll_nid = $poll
->id();
$vote_user1 = $this
->drupalCreateUser(array(
'access polls',
'access content',
));
$this
->drupalLogin($vote_user1);
$edit = array(
'choice' => $this
->getChoiceId($poll, 1),
);
$this
->drupalPostForm('poll/' . $poll_nid, $edit, t('Vote'));
$this
->drupalLogout();
$vote_user2 = $this
->drupalCreateUser(array(
'access polls',
'access content',
));
$this
->drupalLogin($vote_user2);
$edit = array(
'choice' => $this
->getChoiceId($poll, 1),
);
$this
->drupalPostForm('poll/' . $poll_nid, $edit, t('Vote'));
$this
->drupalLogout();
$vote_user3 = $this
->drupalCreateUser(array(
'access polls',
'access content',
));
$this
->drupalLogin($vote_user3);
$edit = array(
'choice' => $this
->getChoiceId($poll, 2),
);
$this
->drupalPostForm('poll/' . $poll_nid, $edit, t('Vote'));
$this
->drupalLogout();
$vote_user4 = $this
->drupalCreateUser(array(
'access polls',
'access content',
));
$this
->drupalLogin($vote_user4);
$edit = array(
'choice' => $this
->getChoiceId($poll, 3),
);
$this
->drupalPostForm('poll/' . $poll_nid, $edit, t('Vote'));
$this
->drupalLogout();
$poll = Poll::load($poll_nid);
$tests = array();
$tests['[poll:votes]'] = 4;
$tests['[poll:winner]'] = $poll
->getOptions()[$this
->getChoiceId($poll, 1)];
$tests['[poll:winner-votes]'] = 2;
$tests['[poll:winner-percent]'] = 50;
$tests['[poll:duration]'] = \Drupal::service('date.formatter')
->formatInterval($poll
->getRuntime());
$this
->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
$token = \Drupal::service('token');
foreach ($tests as $input => $expected) {
$output = $token
->replace($input, array(
'poll' => $poll,
));
$this
->assertEqual($output, $expected, "Sanitized poll token {$input} replaced.");
}
$tests['[poll:winner]'] = $poll
->getOptions()[$this
->getChoiceId($poll, 1)];
foreach ($tests as $input => $expected) {
$output = $token
->replace($input, array(
'poll' => $poll,
), array(
'sanitize' => FALSE,
));
$this
->assertEqual($output, $expected, "Unsanitized poll token {$input} replaced.");
}
}
}