You are here

public function QuizResultTestCase::testResultPruning in Quiz 7.6

Same name and namespace in other branches
  1. 7.5 tests/QuizResultTestCase.test \QuizResultTestCase::testResultPruning()

Test the all, best, and last quiz result pruning.

File

tests/QuizResultTestCase.test, line 216

Class

QuizResultTestCase

Code

public function testResultPruning() {
  $this
    ->drupalLogin($this->admin);
  $quiz_node = $this
    ->drupalCreateQuiz(array(
    'keep_results' => QUIZ_KEEP_ALL,
  ));
  $question_node1 = $this
    ->drupalCreateNode(array(
    'type' => 'truefalse',
    'correct_answer' => 1,
  ));
  $this
    ->linkQuestionToQuiz($question_node1, $quiz_node);
  $question_node2 = $this
    ->drupalCreateNode(array(
    'type' => 'truefalse',
    'correct_answer' => 1,
  ));
  $this
    ->linkQuestionToQuiz($question_node2, $quiz_node);
  $question_node3 = $this
    ->drupalCreateNode(array(
    'type' => 'truefalse',
    'correct_answer' => 1,
  ));
  $this
    ->linkQuestionToQuiz($question_node3, $quiz_node);
  $question_node4 = $this
    ->drupalCreateNode(array(
    'type' => 'truefalse',
    'correct_answer' => 1,
  ));
  $this
    ->linkQuestionToQuiz($question_node4, $quiz_node);
  $this
    ->drupalLogin($this->user);

  // Create 2 100% results.
  for ($i = 1; $i <= 2; $i++) {
    $this
      ->drupalGet("node/{$quiz_node->nid}/take");
    $this
      ->drupalPost(NULL, array(
      "question[{$question_node1->nid}][answer]" => 1,
    ), t('Next'));
    $this
      ->drupalPost(NULL, array(
      "question[{$question_node2->nid}][answer]" => 1,
    ), t('Next'));
    $this
      ->drupalPost(NULL, array(
      "question[{$question_node3->nid}][answer]" => 1,
    ), t('Next'));
    $this
      ->drupalPost(NULL, array(
      "question[{$question_node4->nid}][answer]" => 1,
    ), t('Finish'));
  }

  // Storing all results.
  $results = entity_load('quiz_result');
  $this
    ->assertEqual(count($results), 2, 'Found 2 quiz results.');
  $quiz_node->keep_results = QUIZ_KEEP_LATEST;
  node_save($quiz_node);

  // Create a 50% result.
  $this
    ->drupalGet("node/{$quiz_node->nid}/take");
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node1->nid}][answer]" => 1,
  ), t('Next'));
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node2->nid}][answer]" => 1,
  ), t('Next'));
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node3->nid}][answer]" => 0,
  ), t('Next'));
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node4->nid}][answer]" => 0,
  ), t('Finish'));

  // We should only have one 50% result.
  $results = entity_load('quiz_result', FALSE, array(), TRUE);
  $this
    ->assertEqual(count($results), 1, 'Found only one quiz result');
  $quiz_result = reset($results);
  $this
    ->assertEqual($quiz_result->score, 50, 'Quiz result was 50%');
  $quiz_node->keep_results = QUIZ_KEEP_BEST;
  node_save($quiz_node);
  $this
    ->drupalGet("node/{$quiz_node->nid}/take");
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node1->nid}][answer]" => 1,
  ), t('Next'));
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node2->nid}][answer]" => 0,
  ), t('Next'));
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node3->nid}][answer]" => 0,
  ), t('Next'));
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node4->nid}][answer]" => 0,
  ), t('Finish'));

  // We should still only have one 50% result, since we failed.
  $results = entity_load('quiz_result', FALSE, array(), TRUE);
  $this
    ->assertTrue(count($results) == 1, 'Found only one quiz result');
  $quiz_result = reset($results);
  $this
    ->assertEqual($quiz_result->score, 50, 'Quiz score was 50%');
  $this
    ->drupalGet("node/{$quiz_node->nid}/take");
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node1->nid}][answer]" => 1,
  ), t('Next'));
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node2->nid}][answer]" => 1,
  ), t('Next'));
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node3->nid}][answer]" => 1,
  ), t('Next'));
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node4->nid}][answer]" => 0,
  ), t('Finish'));

  // We should still only have one 75% result.
  $results = entity_load('quiz_result', FALSE, array(), TRUE);
  $this
    ->assertEqual(count($results), 1, 'Found only one quiz result');
  $quiz_result = reset($results);
  $this
    ->assertEqual($quiz_result->score, 75, 'Quiz score was 75%');
}