View source
<?php
namespace Drupal\Tests\quiz\Functional;
use Drupal\quiz\Entity\QuizFeedbackType;
class QuizFeedbackTest extends QuizTestBase {
protected static $modules = [
'quiz_truefalse',
];
public function testAnswerFeedback() {
$this
->drupalLogin($this->admin);
$quiz = $this
->createQuiz();
$question1 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question1, $quiz);
$question2 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question2, $quiz);
$review_options = [
'attempt' => t('Your answer'),
'correct' => t('Correct?'),
'score' => t('Score'),
'answer_feedback' => t('Feedback'),
'solution' => t('Correct answer'),
];
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 1,
], t('Next'));
foreach ($review_options as $option => $text) {
$quiz->review_options = [
'question' => [
$option => $option,
],
];
$quiz
->save();
$this
->drupalGet("quiz/{$quiz->id()}/take/1/feedback");
$this
->assertText('Question 1');
$this
->assertNoText('Question 2');
$this
->assertRaw('<th>' . $text . '</th>');
foreach ($review_options as $option2 => $text2) {
if ($option != $option2) {
$this
->assertNoRaw('<th>' . $text2 . '</th>');
}
}
}
$this
->clickLink(t('Next question'));
$this
->drupalPostForm(NULL, [
"question[{$question2->id()}][answer]" => 1,
], t('Finish'));
$this
->assertNoText('Question 1');
$this
->assertText('Question 2');
$this
->drupalPostForm(NULL, [], t('Finish'));
foreach ($review_options as $option => $text) {
$quiz->review_options = [
'end' => [
$option => $option,
],
];
$quiz
->save();
$this
->drupalGet("quiz/{$quiz->id()}/result/1");
$this
->assertText('Question 1');
$this
->assertText('Question 2');
$this
->assertRaw('<th>' . $text . '</th>');
foreach ($review_options as $option2 => $text2) {
if ($option != $option2) {
$this
->assertNoRaw('<th>' . $text2 . '</th>');
}
}
}
}
public function testQuestionFeedback() {
$this
->drupalLogin($this->admin);
$quiz = $this
->createQuiz([
'review_options' => [
'end' => [
'question_feedback' => 'question_feedback',
],
],
]);
$question1 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
'feedback' => 'Feedback for TF test.',
]);
$this
->linkQuestionToQuiz($question1, $quiz);
$question2 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
'feedback' => 'Feedback for TF test.',
]);
$this
->linkQuestionToQuiz($question2, $quiz);
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 1,
], t('Next'));
$this
->assertNoText('Feedback for TF test.');
$this
->drupalPostForm(NULL, [
"question[{$question2->id()}][answer]" => 1,
], t('Finish'));
$this
->assertText('Feedback for TF test.');
}
public function testNoFeedback() {
$this
->drupalLogin($this->admin);
$quiz = $this
->createQuiz([
'review_options' => [],
]);
$question1 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
'feedback' => 'Feedback for TF test.',
]);
$this
->linkQuestionToQuiz($question1, $quiz);
$question2 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
'feedback' => 'Feedback for TF test.',
]);
$this
->linkQuestionToQuiz($question2, $quiz);
$this
->drupalLogin($this->user);
$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 have finished this Quiz');
}
public function testQuestionBodyFeedback() {
$this
->drupalLogin($this->admin);
$quiz = $this
->createQuiz([
'review_options' => [],
]);
$question1 = $this
->createQuestion([
'type' => 'truefalse',
'body' => 'TF 1 body text',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question1, $quiz);
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 1,
], t('Finish'));
$this
->assertNoText('TF 1 body text');
$quiz->review_options = [
'end' => [
'quiz_question_view_full' => 'quiz_question_view_full',
],
];
$quiz
->save();
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 1,
], t('Finish'));
$this
->assertText('TF 1 body text');
}
public function testFeedbackTimes() {
$this
->drupalLogin($this->admin);
$component = [
'expression' => [
'id' => 'rules_and',
'conditions' => [
[
'id' => 'rules_condition',
'uuid' => 'ca2a6b2f-3b17-449e-b913-d64b52c17203',
'weight' => 2,
'context_values' => [
'operation' => '==',
'value' => '2',
],
'context_mapping' => [
'data' => 'quiz_result.attempt.value',
],
'condition_id' => 'rules_data_comparison',
'negate' => 0,
],
],
],
'context_definitions' => [
'quiz_result' => [
'type' => 'entity:quiz_result',
'label' => 'Quiz result',
'description' => 'Quiz result to evaluate feedback',
],
],
];
QuizFeedbackType::create([
'label' => 'After two attempts',
'id' => 'after2attempts',
'component' => $component,
])
->save();
$quiz = $this
->createQuiz([
'review_options' => [
'after2attempts' => [
'solution' => 'solution',
],
],
]);
$question1 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question1, $quiz);
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 1,
], t('Finish'));
$this
->assertNoText('Correct answer');
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 1,
], t('Finish'));
$this
->assertText('Correct answer');
}
}