You are here

matching.test in Quiz 7.5

Unit tests for the matching Module.

File

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

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

/**
 * Test class for matching questions.
 */
class MatchingTestCase extends QuizQuestionTestCase {

  // Initializing attributes.
  var $question_node_type = 'matching';
  var $title = '';
  var $body = '';
  public static function getInfo() {
    return array(
      'name' => t('Matching'),
      'description' => t('Unit test for matching question type.'),
      'group' => t('Quiz'),
    );
  }
  public function setUp($modules = array(), $admin_permissions = array(), $user_permissions = array()) {
    parent::setUp(array(
      'matching',
    ));
  }

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

    // Login as our privileged user.
    $this
      ->drupalLogin($this->admin);
    $match = array();
    for ($i = 0; $i <= 2; $i++) {
      $match[$i]['question'] = "MAQ " . ($i + 1);
      $match[$i]['answer'] = "MAA " . ($i + 1);
      $match[$i]['feedback'] = "MAF " . ($i + 1);
    }
    $question_node = $this
      ->drupalCreateNode(array(
      'type' => $this->question_node_type,
      'title' => 'MA 1 title',
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => 'MA 1 body text',
          ),
        ),
      ),
      'match' => $match,
      'choice_penalty' => 0,
    ));
    return $question_node;
  }

  /**
   * Test using a matching question inside a quiz.
   */
  public function testTakeQuestion() {
    $quiz_node = $this
      ->drupalCreateQuiz(array(
      'review_options' => array(
        'end' => drupal_map_assoc(array(
          'answer_feedback',
          'score',
        )),
      ),
    ));
    $question_node = $this
      ->testCreateQuizQuestion();

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

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

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

    // Take the quiz.
    $this
      ->drupalGet("node/{$quiz_node->nid}/take");
    $this
      ->assertNoText('MA 1 title');
    $this
      ->assertText('MA 1 body text');
    $this
      ->assertText('MAQ 1');
    $this
      ->assertText('MAQ 2');
    $this
      ->assertText('MAQ 3');
    $this
      ->assertText('MAA 1');
    $this
      ->assertText('MAA 2');
    $this
      ->assertText('MAA 3');

    // Test validation.
    $this
      ->drupalPost(NULL, array(), t('Finish'));
    $this
      ->assertText('You need to match at least one of the items.');

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

    // Test incorrect question.
    $this
      ->drupalGet("node/{$quiz_node->nid}/take");
    $this
      ->drupalPost(NULL, array(
      "question[2][answer][1]" => 1,
      "question[2][answer][2]" => 2,
      "question[2][answer][3]" => 2,
    ), t('Finish'));
    $this
      ->assertText('MAF 1');
    $this
      ->assertText('MAF 2');

    // The behavior right now is that all the feedback shows.
    $this
      ->assertText('MAF 3');
    $this
      ->assertText('You got 2 of 3 possible points.');
  }

  /**
   * Test if the penalty system for guessing wrong work.
   */
  public function testChoicePenalty() {
    $quiz_node = $this
      ->drupalCreateQuiz(array(
      'review_options' => array(
        'end' => drupal_map_assoc(array(
          'answer_feedback',
          'score',
        )),
      ),
    ));
    $question_node = $this
      ->testCreateQuizQuestion();
    $question_node->choice_penalty = 1;
    node_save($question_node);

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

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

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

  /**
   * Test that the question response can be edited.
   */
  public function testEditQuestionResponse() {

    // Create & link a question.
    $question_node = $this
      ->testCreateQuizQuestion();
    $quiz_node = $this
      ->linkQuestionToQuiz($question_node);
    $question_node2 = $this
      ->testCreateQuizQuestion();
    $this
      ->linkQuestionToQuiz($question_node2, $quiz_node);

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

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

    // Test editing a question.
    $this
      ->drupalGet("node/{$quiz_node->nid}/take");
    $this
      ->drupalGet("node/{$quiz_node->nid}/take/1");
    $this
      ->drupalPost(NULL, array(
      "question[{$question_node->nid}][answer][1]" => 1,
    ), t('Next'));
    $this
      ->drupalGet("node/{$quiz_node->nid}/take/1");
    $this
      ->drupalPost(NULL, array(
      "question[{$question_node->nid}][answer][1]" => 0,
      "question[{$question_node->nid}][answer][2]" => 2,
    ), t('Next'));
  }

}

Classes

Namesort descending Description
MatchingTestCase Test class for matching questions.