You are here

public function QuizGradingTest::testManualWeightedScore in Quiz 8.5

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

Test question weights.

File

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

Class

QuizGradingTest
Test quiz grading.

Namespace

Drupal\Tests\quiz\Functional

Code

public function testManualWeightedScore() {
  $question1 = $this
    ->createQuestion(array(
    '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(array(
    'type' => 'truefalse',
    'truefalse_correct' => 1,
  ));
  $this
    ->linkQuestionToQuiz($question2, $quiz_node);

  // Link the question. Make a 10 point quiz with a 7 point SA and 3 point TF.
  $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'));

  // 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]" => 'blah blah',
  ), t('Next'));
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question2->id()}][answer]" => 1,
  ), t('Finish'));
  $this
    ->assertText('You got 3 of 10 possible points.');

  // Test grading the question.
  $this
    ->drupalLogin($this->admin);
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/result/1/edit");
  $this
    ->drupalPostForm(NULL, array(
    "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);

  // We got 3 + 3 points out of 10.
  // Unweighted we would have received 2.14 + 1 point.
  $this
    ->assertEqual($quiz_result
    ->get('score')
    ->getString(), 60);
}