QuizAnonymousTest.php in Quiz 6.x
File
tests/src/Functional/QuizAnonymousTest.php
View source
<?php
namespace Drupal\Tests\quiz\Functional;
use Drupal\Core\Session\AccountInterface;
use Drupal\quiz\Entity\QuizQuestion;
use function quiz_get_feedback_options;
use function user_role_grant_permissions;
class QuizAnonymousTest extends QuizTestBase {
protected static $modules = [
'quiz_truefalse',
];
public function testAnonymousQuizTake() {
$this
->drupalLogin($this->admin);
$question1 = QuizQuestion::create([
'type' => 'truefalse',
'title' => 'TF 1 title',
'truefalse_correct' => [
'value' => 1,
],
'body' => 'TF 1 body text',
]);
$question1
->save();
$question2 = QuizQuestion::create([
'type' => 'truefalse',
'title' => 'TF 2 title',
'truefalse_correct' => [
'value' => 0,
],
'body' => 'TF 2 body text',
]);
$question2
->save();
$quiz = $this
->linkQuestionToQuiz($question1);
$quiz
->addQuestion($question2)
->set('weight', 10)
->save();
$quiz
->set('review_options', [
'end' => array_combine(array_keys(quiz_get_feedback_options()), array_keys(quiz_get_feedback_options())),
]);
$quiz
->save();
user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, [
'view own quiz_result',
'view any quiz',
'access quiz',
]);
$this
->drupalLogout();
$this
->drupalGet("quiz/{$quiz->id()}");
$this
->clickLink('Start Quiz');
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 1,
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question2->id()}][answer]" => 1,
], t('Finish'));
$this
->assertText('You got 1 of 2 possible points.');
$this
->assertText('Your score: 50%');
}
}