PollKernelTestBase.php in Poll 8
File
tests/src/Kernel/PollKernelTestBase.php
View source
<?php
namespace Drupal\Tests\poll\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\poll\Entity\Poll;
use Drupal\poll\Entity\PollChoice;
use Drupal\poll\PollInterface;
abstract class PollKernelTestBase extends KernelTestBase {
public static $modules = [
'options',
'poll',
'user',
];
public function setUp() {
parent::setup();
$this
->installSchema('poll', 'poll_vote');
$this
->installEntitySchema('poll');
$this
->installEntitySchema('poll_choice');
}
public function createPoll($choice_count = 2) {
$poll = Poll::create([
'question' => $this
->randomMachineName(),
]);
$poll_choice_ids = [];
for ($i = 1; $i <= $choice_count; $i++) {
$poll_choice = PollChoice::create([
'choice' => $this
->randomMachineName(),
]);
$poll_choice
->save();
$poll_choice_ids[] = $poll_choice
->id();
}
$poll
->set('anonymous_vote_allow', TRUE)
->set('choice', $poll_choice_ids)
->save();
return $poll;
}
public function saveVote(PollInterface $poll, $choice_id) {
$options = [];
$options['chid'] = $choice_id;
$options['uid'] = \Drupal::currentUser()
->id();
$options['pid'] = $poll
->id();
$options['hostname'] = \Drupal::request()
->getClientIp();
$options['timestamp'] = \Drupal::time()
->getRequestTime();
$vote_storage = \Drupal::service('poll_vote.storage');
$vote_storage
->saveVote($options);
}
}