You are here

public function QuizGradingTestCase::testWeightedScore in Quiz 7.5

Test question weights.

File

tests/QuizGradingTestCase.test, line 30
Unit tests for the quiz question Module.

Class

QuizGradingTestCase
Base test class for Quiz questions.

Code

public function testWeightedScore() {
  $question_node1 = $this
    ->drupalCreateNode(array(
    'type' => 'truefalse',
    'correct_answer' => 1,
  ));
  $question_node2 = $this
    ->drupalCreateNode(array(
    'type' => 'truefalse',
    'correct_answer' => 1,
  ));
  $question_node3 = $this
    ->drupalCreateNode(array(
    'type' => 'truefalse',
    'correct_answer' => 1,
  ));

  // Link the questions. Make a 26 point quiz.
  $quiz_node = $this
    ->linkQuestionToQuiz($question_node1);
  $this
    ->linkQuestionToQuiz($question_node2, $quiz_node);
  $this
    ->linkQuestionToQuiz($question_node3, $quiz_node);
  db_update('quiz_node_relationship')
    ->condition('child_nid', $question_node1->nid)
    ->fields(array(
    'max_score' => 1,
  ))
    ->execute();
  db_update('quiz_node_relationship')
    ->condition('child_nid', $question_node2->nid)
    ->fields(array(
    'max_score' => 5,
  ))
    ->execute();
  db_update('quiz_node_relationship')
    ->condition('child_nid', $question_node3->nid)
    ->fields(array(
    'max_score' => 20,
  ))
    ->execute();

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

  // Test correct question.
  $this
    ->drupalGet("node/{$quiz_node->nid}/take");
  $result_id = _quiz_active_result_id($this->user->uid, $quiz_node->nid, $quiz_node->vid);
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node1->nid}][answer]" => 0,
  ), t('Next'));
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node2->nid}][answer]" => 0,
  ), t('Next'));
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node3->nid}][answer]" => 1,
  ), t('Finish'));
  $this
    ->assertText('You got 20 of 26 possible points.');

  // Make sure the values in the database are correct.
  $qra1 = _quiz_question_response_get_instance($result_id, $question_node1);
  $this
    ->assertEqual($qra1->points_awarded, 0);
  $qra2 = _quiz_question_response_get_instance($result_id, $question_node2);
  $this
    ->assertEqual($qra2->points_awarded, 0);
  $qra3 = _quiz_question_response_get_instance($result_id, $question_node3);
  $this
    ->assertEqual($qra3->points_awarded, 20);
  $quiz_result = quiz_result_load($result_id);
  $this
    ->assertEqual($quiz_result->score, 77);
}