View source
<?php
namespace Drupal\Tests\quiz\Functional;
use Drupal\quiz\Entity\QuizResult;
use Drupal\quiz_short_answer\Plugin\quiz\QuizQuestion\ShortAnswerQuestion;
class QuizGradingTest extends QuizTestBase {
protected static $modules = [
'quiz_truefalse',
'quiz_short_answer',
];
public function testWeightedScore() {
$this
->drupalLogin($this->admin);
$question1 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$question2 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$question3 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$quiz_node = $this
->linkQuestionToQuiz($question1);
$this
->linkQuestionToQuiz($question2, $quiz_node);
$this
->linkQuestionToQuiz($question3, $quiz_node);
$this
->drupalGet("quiz/{$quiz_node->id()}/questions");
$this
->drupalPostForm(NULL, [
'question_list[1][auto_update_max_score]' => FALSE,
'question_list[2][auto_update_max_score]' => FALSE,
'question_list[3][auto_update_max_score]' => FALSE,
], t('Submit'));
$this
->drupalPostForm(NULL, [
'question_list[1][max_score]' => 1,
'question_list[2][max_score]' => 5,
'question_list[3][max_score]' => 20,
], t('Submit'));
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 0,
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question2->id()}][answer]" => 0,
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question3->id()}][answer]" => 1,
], t('Finish'));
$this
->assertText('You got 20 of 26 possible points.');
$quiz_result = QuizResult::load(1);
$layout = $quiz_result
->getLayout();
$this
->assertEqual($layout[1]
->get('points_awarded')
->getString(), 0);
$this
->assertEqual($layout[2]
->get('points_awarded')
->getString(), 0);
$this
->assertEqual($layout[3]
->get('points_awarded')
->getString(), 20);
$this
->assertEqual($layout[1]
->get('is_correct')
->getString(), 0);
$this
->assertEqual($layout[2]
->get('is_correct')
->getString(), 0);
$this
->assertEqual($layout[3]
->get('is_correct')
->getString(), 1);
$this
->assertEqual($quiz_result
->get('score')
->getString(), 77);
}
public function testManualWeightedScore() {
$question1 = $this
->createQuestion([
'type' => 'short_answer',
'short_answer_evaluation' => ShortAnswerQuestion::ANSWER_MANUAL,
'short_answer_correct' => 'the Zero One Infinity rule',
]);
$quiz_node = $this
->linkQuestionToQuiz($question1);
$question2 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question2, $quiz_node);
$this
->drupalLogin($this->admin);
$this
->drupalGet("quiz/{$quiz_node->id()}/questions");
$this
->drupalPostForm(NULL, [
'question_list[1][auto_update_max_score]' => FALSE,
'question_list[2][auto_update_max_score]' => FALSE,
], t('Submit'));
$this
->drupalPostForm(NULL, [
'question_list[1][max_score]' => 7,
'question_list[2][max_score]' => 3,
], t('Submit'));
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 'blah blah',
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question2->id()}][answer]" => 1,
], t('Finish'));
$this
->assertText('You got 3 of 10 possible points.');
$this
->drupalLogin($this->admin);
$this
->drupalGet("quiz/{$quiz_node->id()}/result/1/edit");
$this
->drupalPostForm(NULL, [
"question[1][score]" => 3,
], t('Save score'));
$quiz_result = QuizResult::load(1);
$layout = $quiz_result
->getLayout();
$this
->assertEqual($layout[1]
->get('points_awarded')
->getString(), 3);
$this
->assertEqual($layout[2]
->get('points_awarded')
->getString(), 3);
$this
->assertEqual($quiz_result
->get('score')
->getString(), 60);
}
}