You are here

public function QuizRandomTestCase::testRandomOrderInPages in Quiz 7.5

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

Test that questions inside of pages are shuffled.

File

tests/QuizRandomTestCase.test, line 297

Class

QuizRandomTestCase
Tests for random questions.

Code

public function testRandomOrderInPages() {
  $this
    ->drupalLogin($this->admin);
  $quiz_node = $this
    ->drupalCreateQuiz(array(
    'randomization' => 1,
  ));

  // Create the questions.
  $question_node1 = $this
    ->drupalCreateNode(array(
    'type' => 'truefalse',
    'correct_answer' => 1,
    'body' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => 'TF 1 body text',
        ),
      ),
    ),
  ));
  $this
    ->linkQuestionToQuiz($question_node1, $quiz_node);
  $question_node2 = $this
    ->drupalCreateNode(array(
    'type' => 'truefalse',
    'correct_answer' => 1,
    'body' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => 'TF 2 body text',
        ),
      ),
    ),
  ));
  $this
    ->linkQuestionToQuiz($question_node2, $quiz_node);
  $question_node3 = $this
    ->drupalCreateNode(array(
    'type' => 'truefalse',
    'correct_answer' => 1,
    'body' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => 'TF 3 body text',
        ),
      ),
    ),
  ));
  $this
    ->linkQuestionToQuiz($question_node3, $quiz_node);

  // Create the pages.
  $page_node1 = $this
    ->drupalCreateNode(array(
    'type' => 'quiz_page',
  ));
  $this
    ->linkQuestionToQuiz($page_node1, $quiz_node);
  $page_node2 = $this
    ->drupalCreateNode(array(
    'type' => 'quiz_page',
  ));
  $this
    ->linkQuestionToQuiz($page_node2, $quiz_node);

  // Go to the manage questions form.
  $this
    ->drupalGet("node/{$quiz_node->nid}/quiz/questions");
  $post = array(
    // Make the questions have parents.
    "qnr_pids[{$question_node1->nid}-{$question_node1->vid}]" => 4,
    "qnr_pids[{$question_node2->nid}-{$question_node2->vid}]" => 4,
    "qnr_pids[{$question_node3->nid}-{$question_node3->vid}]" => 5,
    // Mirror what JS would have done by adjusting the weights.
    "weights[{$page_node1->nid}-{$page_node1->vid}]" => 2,
    "weights[{$question_node1->nid}-{$question_node1->vid}]" => 3,
    "weights[{$question_node2->nid}-{$question_node2->vid}]" => 4,
    "weights[{$page_node2->nid}-{$page_node2->vid}]" => 3,
    "weights[{$question_node3->nid}-{$question_node3->vid}]" => 4,
  );
  $this
    ->drupalPost(NULL, $post, t('Submit'));
  for ($i = 1; $i <= 10; $i++) {
    $questions = quiz_build_question_list($quiz_node);
    $out[$i] = '';
    foreach ($questions as $question) {
      $out[$i] .= $question['nid'];
    }
  }

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

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