View source
<?php
namespace Drupal\Tests\quiz_truefalse\Functional;
use Drupal\quiz\Entity\QuizQuestion;
use Drupal\Tests\quiz\Functional\QuizQuestionTestBase;
class QuizTrueFalseTestCase extends QuizQuestionTestBase {
public function getQuestionType() {
return 'truefalse';
}
public static $modules = [
'quiz_truefalse',
];
public function testCreateQuizQuestion($settings = []) {
$this
->drupalLogin($this->admin);
$question = QuizQuestion::create([
'type' => 'truefalse',
'title' => 'TF 1 title',
'truefalse_correct' => [
'value' => 1,
],
'body' => 'TF 1 body text',
] + $settings);
$question
->save();
return $question;
}
public function testTakeQuestion() {
$question = $this
->testCreateQuizQuestion();
$quiz = $this
->linkQuestionToQuiz($question);
$this
->drupalGet("quiz/{$quiz->id()}/questions");
$this
->assertText('TF 1 title');
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->assertNoText('TF 1 title');
$this
->assertText('TF 1 body text');
$this
->assertText('True');
$this
->assertText('False');
$this
->drupalPostForm(NULL, [], t('Finish'));
$this
->assertText('You must provide an answer.');
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalPostForm(NULL, [
"question[{$question->id()}][answer]" => 1,
], t('Finish'));
$this
->assertText('You got 1 of 1 possible points.');
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalPostForm(NULL, [
"question[{$question->id()}][answer]" => 0,
], t('Finish'));
$this
->assertText('You got 0 of 1 possible points.');
}
public function testQuestionFeedback() {
$this
->drupalLogin($this->admin);
$question = $this
->testCreateQuizQuestion();
$quiz = $this
->linkQuestionToQuiz($question);
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalPostForm(NULL, [
"question[{$question->id()}][answer]" => 1,
], t('Finish'));
$this
->assertRaw('quiz-score-icon correct');
$this
->assertRaw('quiz-score-icon should');
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalPostForm(NULL, [
"question[{$question->id()}][answer]" => 0,
], t('Finish'));
$this
->assertRaw('quiz-score-icon incorrect');
$this
->assertRaw('quiz-score-icon should');
}
public function testEditQuestionResponse() {
$question = $this
->testCreateQuizQuestion();
$quiz = $this
->linkQuestionToQuiz($question);
$quiz
->set('backwards_navigation', 1);
$quiz
->set('allow_change', 1);
$quiz
->save();
$question2 = $this
->testCreateQuizQuestion();
$this
->linkQuestionToQuiz($question2, $quiz);
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalGet("quiz/{$quiz->id()}/take/1");
$this
->drupalPostForm(NULL, [
"question[{$question->id()}][answer]" => 0,
], t('Next'));
$this
->drupalGet("quiz/{$quiz->id()}/take/1");
$this
->drupalPostForm(NULL, [
"question[{$question->id()}][answer]" => 1,
], t('Next'));
}
}