You are here

public function QuizPageTestCase::testQuizPageParentage in Quiz 7.5

Same name and namespace in other branches
  1. 8.6 question_types/quiz_page/tests/src/Functional/QuizPageTestCase.php \QuizPageTestCase::testQuizPageParentage()
  2. 8.6 question_types/quiz_page/tests/src/Functional/QuizPageTestCase.test \QuizPageTestCase::testQuizPageParentage()
  3. 8.5 question_types/quiz_page/tests/src/Functional/QuizPageTestCase.php \QuizPageTestCase::testQuizPageParentage()
  4. 8.5 question_types/quiz_page/tests/src/Functional/QuizPageTestCase.test \QuizPageTestCase::testQuizPageParentage()
  5. 7.6 question_types/quiz_page/QuizPageTestCase.test \QuizPageTestCase::testQuizPageParentage()

Test that question parentage saves.

File

question_types/quiz_page/QuizPageTestCase.test, line 24

Class

QuizPageTestCase

Code

public function testQuizPageParentage() {
  $this
    ->drupalLogin($this->admin);

  // Create Quiz with review of score.
  $quiz_node = $this
    ->drupalCreateQuiz();

  // 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);

  // QNR ID 1
  $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);

  // QNR ID 2
  $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);

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

  // QNR ID 4
  $page_node2 = $this
    ->drupalCreateNode(array(
    'type' => 'quiz_page',
  ));
  $this
    ->linkQuestionToQuiz($page_node2, $quiz_node);

  // QNR ID 5
  // 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'));
  $sql = "SELECT * FROM {quiz_node_relationship}";
  $data = db_query($sql)
    ->fetchAllAssoc('qnr_id');

  // Check the relationships properly saved.
  foreach ($data as $qnr_id => $rel) {
    switch ($qnr_id) {
      case 1:
      case 2:
        $this
          ->assertEqual($rel->qnr_pid, 4);
        break;
      case 3:
        $this
          ->assertEqual($rel->qnr_pid, 5);
        break;
      case 4:
      case 5:
        $this
          ->assertNull($rel->qnr_pid);
        break;
    }
  }

  // Take the quiz. Ensure the pages are correct.
  $this
    ->drupalLogin($this->user);
  $this
    ->drupalGet("node/{$quiz_node->nid}/take");

  // Questions 1 and 2 are present. Question 3 is hidden.
  $this
    ->assertFieldByName("question[{$question_node1->nid}][answer]");
  $this
    ->assertFieldByName("question[{$question_node2->nid}][answer]");
  $this
    ->assertNoFieldByName("question[{$question_node3->nid}][answer]");
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node1->nid}][answer]" => 1,
    "question[{$question_node2->nid}][answer]" => 1,
  ), t('Next'));

  // Questions 1 and 2 are gone. Question 3 is present.
  $this
    ->assertNoFieldByName("question[{$question_node1->nid}][answer]");
  $this
    ->assertNoFieldByName("question[{$question_node2->nid}][answer]");
  $this
    ->assertFieldByName("question[{$question_node3->nid}][answer]");
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node3->nid}][answer]" => 1,
  ), t('Finish'));

  // Check that the results page contains all the questions.
  $this
    ->assertText('You got 3 of 3 possible points.');
  $this
    ->assertText('TF 1 body text');
  $this
    ->assertText('TF 2 body text');
  $this
    ->assertText('TF 3 body text');

  // Check to make sure that saving a new revision of the Quiz does not affect
  // the parentage.
  $this
    ->drupalLogin($this->admin);

  // 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}]" => 9,
    "qnr_pids[{$question_node2->nid}-{$question_node2->vid}]" => 9,
    "qnr_pids[{$question_node3->nid}-{$question_node3->vid}]" => 10,
    // 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'));

  // Take the quiz. Ensure the pages are correct.
  $this
    ->drupalLogin($this->user);
  $this
    ->drupalGet("node/{$quiz_node->nid}/take");

  // Questions 1 and 2 are present. Question 3 is hidden.
  $this
    ->assertText("Page 1 of 2");
  $this
    ->assertFieldByName("question[{$question_node1->nid}][answer]");
  $this
    ->assertFieldByName("question[{$question_node2->nid}][answer]");
  $this
    ->assertNoFieldByName("question[{$question_node3->nid}][answer]");
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node1->nid}][answer]" => 1,
    "question[{$question_node2->nid}][answer]" => 1,
  ), t('Next'));

  // Questions 1 and 2 are gone. Question 3 is present.
  $this
    ->assertText("Page 2 of 2");
  $this
    ->assertNoFieldByName("question[{$question_node1->nid}][answer]");
  $this
    ->assertNoFieldByName("question[{$question_node2->nid}][answer]");
  $this
    ->assertFieldByName("question[{$question_node3->nid}][answer]");
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node3->nid}][answer]" => 1,
  ), t('Finish'));
}