You are here

truefalse.test in Quiz 7.6

Unit tests for the truefalse Module.

File

question_types/truefalse/truefalse.test
View source
<?php

/**
 * @file
 * Unit tests for the truefalse Module.
 */

/**
 * Test class for true false questions.
 */
class TrueFalseTestCase extends QuizQuestionTestCase {
  var $question_node_type = 'truefalse';
  public static function getInfo() {
    return array(
      'name' => t('True/False'),
      'description' => t('Unit test for true or false question type.'),
      'group' => t('Quiz'),
    );
  }
  function setUp($modules = array(), $admin_permissions = array(), $user_permissions = array()) {
    parent::setUp(array(
      'truefalse',
    ));
  }

  /**
   * Test adding and taking a truefalse question.
   */
  function testCreateQuizQuestion() {

    // Login as our privileged user.
    $this
      ->drupalLogin($this->admin);
    $question_node = $this
      ->drupalCreateNode(array(
      'type' => 'truefalse',
      'title' => 'TF 1 title',
      'correct_answer' => 1,
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => 'TF 1 body text',
          ),
        ),
      ),
    ));
    return $question_node;
  }
  function testTakeQuestion() {
    $question_node = $this
      ->testCreateQuizQuestion();

    // Link the question.
    $quiz_node = $this
      ->linkQuestionToQuiz($question_node);

    // Test that question appears in lists.
    $this
      ->drupalGet("node/{$quiz_node->nid}/quiz/questions");
    $this
      ->assertText('TF 1 title');

    // Login as non-admin.
    $this
      ->drupalLogin($this->user);

    // Take the quiz.
    $this
      ->drupalGet("node/{$quiz_node->nid}/take");
    $this
      ->assertNoText('TF 1 title');
    $this
      ->assertText('TF 1 body text');
    $this
      ->assertText('True');
    $this
      ->assertText('False');

    // Test validation.
    $this
      ->drupalPost(NULL, array(), t('Finish'));
    $this
      ->assertText('You must provide an answer.');

    // Test correct question.
    $this
      ->drupalGet("node/{$quiz_node->nid}/take");
    $this
      ->drupalPost(NULL, array(
      "question[{$question_node->nid}][answer]" => 1,
    ), t('Finish'));
    $this
      ->assertText('You got 1 of 1 possible points.');

    // Test incorrect question.
    $this
      ->drupalGet("node/{$quiz_node->nid}/take");
    $this
      ->drupalPost(NULL, array(
      "question[{$question_node->nid}][answer]" => 0,
    ), t('Finish'));
    $this
      ->assertText('You got 0 of 1 possible points.');
  }
  function testQuestionFeedback() {

    // Login as our privileged user.
    $this
      ->drupalLogin($this->admin);

    // Create the quiz and question.
    $question_node = $this
      ->testCreateQuizQuestion();

    // Link the question.
    $quiz_node = $this
      ->linkQuestionToQuiz($question_node);

    // Test incorrect question with all feedbacks on.
    // Login as non-admin.
    $this
      ->drupalLogin($this->user);

    // Take the quiz.
    $this
      ->drupalGet("node/{$quiz_node->nid}/take");
    $this
      ->drupalPost(NULL, array(
      "question[{$question_node->nid}][answer]" => 1,
    ), t('Finish'));
    $this
      ->assertRaw('quiz-score-icon correct');
    $this
      ->assertRaw('quiz-score-icon should');

    // Take the quiz.
    $this
      ->drupalGet("node/{$quiz_node->nid}/take");
    $this
      ->drupalPost(NULL, array(
      "question[{$question_node->nid}][answer]" => 0,
    ), t('Finish'));
    $this
      ->assertRaw('quiz-score-icon incorrect');
    $this
      ->assertRaw('quiz-score-icon should');
  }

}

Classes

Namesort descending Description
TrueFalseTestCase Test class for true false questions.