You are here

public function QuizResultTestCase::testBuildOnLastAttempt in Quiz 7.5

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

Test the build on last attempt functionality.

File

tests/QuizResultTestCase.test, line 381

Class

QuizResultTestCase

Code

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

  // Prepopulate correct answers.
  $quiz_node = $this
    ->drupalCreateQuiz(array(
    'build_on_last' => 'correct',
  ));
  $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);

  // Take the quiz.
  $this
    ->drupalGet("node/{$quiz_node->nid}/take");

  // No build on last form here.
  $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]" => 1,
  ), t('Next'));
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node4->nid}][answer]" => 0,
  ), t('Finish'));

  // Take it again, ensure the correct answers are prefilled.
  $this
    ->drupalGet("node/{$quiz_node->nid}/take");
  $this
    ->drupalPost(NULL, array(
    'build_on_last' => 'correct',
  ), t('Start @quiz', array(
    '@quiz' => QUIZ_NAME,
  )));
  $this
    ->assertFieldChecked("edit-question-{$question_node1->nid}-answer-1");
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node1->nid}][answer]" => 1,
  ), t('Next'));
  $this
    ->assertNoFieldChecked("edit-question-{$question_node2->nid}-answer-1");
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node2->nid}][answer]" => 0,
  ), t('Next'));
  $this
    ->assertFieldChecked("edit-question-{$question_node3->nid}-answer-1");
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node3->nid}][answer]" => 1,
  ), t('Next'));
  $this
    ->assertNoFieldChecked("edit-question-{$question_node4->nid}-answer-1");
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node4->nid}][answer]" => 0,
  ), t('Finish'));

  // Switch the build option.
  $quiz_node->build_on_last = 'all';
  node_save($quiz_node);

  // Take the quiz with this revision.
  $this
    ->drupalGet("node/{$quiz_node->nid}/take");
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node1->nid}][answer]" => 1,
  ), t('Leave blank'));
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node2->nid}][answer]" => 0,
  ), 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'));

  // Take it again, ensure all the answers are prefilled.
  $this
    ->drupalGet("node/{$quiz_node->nid}/take");
  $this
    ->drupalPost(NULL, array(
    'build_on_last' => 'all',
  ), t('Start @quiz', array(
    '@quiz' => QUIZ_NAME,
  )));
  $this
    ->assertNoFieldChecked("edit-question-{$question_node1->nid}-answer-1");
  $this
    ->assertNoFieldChecked("edit-question-{$question_node1->nid}-answer-0");
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node1->nid}][answer]" => 1,
  ), t('Next'));
  $this
    ->assertFieldChecked("edit-question-{$question_node2->nid}-answer-0");
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node2->nid}][answer]" => 0,
  ), t('Next'));
  $this
    ->assertFieldChecked("edit-question-{$question_node3->nid}-answer-1");
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node3->nid}][answer]" => 1,
  ), t('Next'));
  $this
    ->assertFieldChecked("edit-question-{$question_node4->nid}-answer-0");
  $this
    ->drupalPost(NULL, array(
    "question[{$question_node4->nid}][answer]" => 0,
  ), t('Finish'));
}