You are here

public function QuizRandomTestCase::testRandomQuestions in Quiz 7.6

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

Test random plus required questions from a pool.

File

tests/QuizRandomTestCase.test, line 86

Class

QuizRandomTestCase
Tests for random questions.

Code

public function testRandomQuestions() {
  $this
    ->drupalLogin($this->admin);
  $quiz_node = $this
    ->drupalCreateQuiz(array(
    'randomization' => 2,
    'number_of_random_questions' => 2,
  ));
  $question_node1 = $this
    ->drupalCreateNode(array(
    'type' => 'truefalse',
    'correct_answer' => 1,
    'body' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => 'TF 1 body text',
        ),
      ),
    ),
  ));
  $question_node2 = $this
    ->drupalCreateNode(array(
    'type' => 'truefalse',
    'correct_answer' => 1,
    'body' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => 'TF 2 body text',
        ),
      ),
    ),
  ));
  $question_node3 = $this
    ->drupalCreateNode(array(
    'type' => 'truefalse',
    'correct_answer' => 1,
    'body' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => 'TF 3 body text',
        ),
      ),
    ),
  ));
  $question_node4 = $this
    ->drupalCreateNode(array(
    'type' => 'truefalse',
    'correct_answer' => 1,
    'body' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => 'TF 4 body text',
        ),
      ),
    ),
  ));
  $question_node5 = $this
    ->drupalCreateNode(array(
    'type' => 'truefalse',
    'correct_answer' => 1,
    'body' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => 'TF 5 body text',
        ),
      ),
    ),
  ));
  $this
    ->linkQuestionToQuiz($question_node1, $quiz_node);
  $this
    ->linkQuestionToQuiz($question_node2, $quiz_node);
  $this
    ->linkQuestionToQuiz($question_node3, $quiz_node);
  $this
    ->linkQuestionToQuiz($question_node4, $quiz_node);
  $this
    ->linkQuestionToQuiz($question_node5, $quiz_node);

  // Set up one required question.
  $this
    ->drupalGet("node/{$quiz_node->nid}/quiz/questions");
  $this
    ->drupalPost(NULL, array(
    "compulsories[{$question_node1->nid}-{$question_node1->vid}]" => TRUE,
  ), t('Submit'));
  for ($i = 1; $i <= 5; $i++) {
    $questions = quiz_build_question_list($quiz_node);
    $this
      ->assertEqual(count($questions), 3, t('Quiz has 2 questions.'));
    $out[$i] = '';
    foreach ($questions as $question) {
      $out[$i] .= $question['nid'];
    }
    $this
      ->assert(strpos($out[$i], $question_node1->nid) !== FALSE, t('Quiz always contains required question 1'));
  }

  // Also check that at least one of the orders is different.
  $this
    ->assertNotEqual(count(array_unique($out)), 1, t('At least one set of questions were different.'));

  // Start the quiz.
  $this
    ->drupalLogin($this->user);
  $this
    ->drupalGet("node/{$quiz_node->nid}");
}