You are here

public function QuizGradingTest::testWeightedScore in Quiz 8.6

Same name and namespace in other branches
  1. 8.5 tests/src/Functional/QuizGradingTest.php \Drupal\Tests\quiz\Functional\QuizGradingTest::testWeightedScore()
  2. 6.x tests/src/Functional/QuizGradingTest.php \Drupal\Tests\quiz\Functional\QuizGradingTest::testWeightedScore()

Test question weights.

File

tests/src/Functional/QuizGradingTest.php, line 20

Class

QuizGradingTest
Test quiz grading.

Namespace

Drupal\Tests\quiz\Functional

Code

public function testWeightedScore() {
  $this
    ->drupalLogin($this->admin);
  $question1 = $this
    ->createQuestion(array(
    'type' => 'truefalse',
    'truefalse_correct' => 1,
  ));
  $question2 = $this
    ->createQuestion(array(
    'type' => 'truefalse',
    'truefalse_correct' => 1,
  ));
  $question3 = $this
    ->createQuestion(array(
    'type' => 'truefalse',
    'truefalse_correct' => 1,
  ));

  // Link the questions. Make a 26 point quiz.
  $quiz_node = $this
    ->linkQuestionToQuiz($question1);
  $this
    ->linkQuestionToQuiz($question2, $quiz_node);
  $this
    ->linkQuestionToQuiz($question3, $quiz_node);
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/questions");

  // drupalPostForm cannot post disabled fields.
  $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'));

  // Login as non-admin.
  $this
    ->drupalLogin($this->user);

  // Test correct question.
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/take");
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question1->id()}][answer]" => 0,
  ), t('Next'));
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question2->id()}][answer]" => 0,
  ), t('Next'));
  $this
    ->drupalPostForm(NULL, array(
    "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();

  // Make sure the values in the database are correct.
  $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);

  // Total score is 20/26.
  $this
    ->assertEqual($quiz_result
    ->get('score')
    ->getString(), 77);
}