You are here

long_answer.test in Quiz 7.5

Unit tests for the long_answer Module.

File

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

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

/**
 * Test class for long answer.
 */
class LongAnswerTestCase extends QuizQuestionTestCase {
  var $question_node_type = 'long_answer';
  public static function getInfo() {
    return array(
      'name' => t('Long answer'),
      'description' => t('Unit test for Long answers question type.'),
      'group' => t('Quiz'),
    );
  }

  /**
   * Set up a question test case.
   *
   * @param array $modules
   *   Array of modules to enable.
   * @param array $admin_permissions
   *   Array of admin permissions to add.
   * @param array $user_permissions
   *   Array of user permissions to add.
   *
   * @return void|false
   */
  public function setUp($modules = array(), $admin_permissions = array(), $user_permissions = array()) {
    parent::setUp(array(
      'long_answer',
    ), array(
      'score any quiz',
    ));
  }

  /**
   * Test manually graded questions.
   *
   * Also test feedback here instead of its own test case.
   *
   * Note: we use two questions here to make sure the grading form is handled
   * correctly.
   */
  public function testGradeAnswerManualFeedback() {
    $this
      ->drupalLogin($this->admin);
    $question_node1 = $this
      ->testCreateQuizQuestion();
    $quiz_node = $this
      ->linkQuestionToQuiz($question_node1);
    $question_node2 = $this
      ->testCreateQuizQuestion();
    $this
      ->linkQuestionToQuiz($question_node2, $quiz_node);

    // Test correct.
    $this
      ->drupalLogin($this->user);
    $this
      ->drupalGet("node/{$quiz_node->nid}/take");
    $this
      ->drupalPost(NULL, array(
      "question[{$question_node1->nid}][answer]" => 'the answer is the zero one infinity rule',
    ), t('Next'));
    $this
      ->drupalPost(NULL, array(
      "question[{$question_node2->nid}][answer]" => 'the number two really is ridiculous',
    ), t('Finish'));
    $this
      ->assertText('Your score: 0%');

    // Strange behavior - extra spacing in the HTML.

    //$this->assertText('Score ? of 10');
    $this
      ->assertText('This answer has not yet been scored.');
    $this
      ->assertNoFieldByName('question[0][score]');
    $this
      ->assertNoFieldByName('question[1][score]');
    $url_of_result = $this
      ->getUrl();
    $admin_url_of_result = preg_replace('#quiz-results/(\\d+)#', 'quiz/results/$1', $this
      ->getUrl());

    // Test grading the question.
    $this
      ->drupalLogin($this->admin);
    $this
      ->drupalGet($admin_url_of_result);
    $this
      ->drupalPost(NULL, array(
      "question[0][score]" => 3,
      "question[1][score]" => 7,
      "question[0][answer_feedback][value]" => 'Feedback for answer 1.',
      "question[1][answer_feedback][value]" => 'Feedback for answer 2.',
      "question[0][answer_feedback][format]" => 'filtered_html',
      "question[1][answer_feedback][format]" => 'filtered_html',
    ), t('Save score'));
    $this
      ->assertText('The scoring data you provided has been saved.');

    // Test the score and feedback are visible to the user.
    $this
      ->drupalLogin($this->user);
    $this
      ->drupalGet($url_of_result);
    $this
      ->assertText('You got 10 of 20 possible points.');
    $this
      ->assertText('Your score: 50%');

    // Strange behavior - extra spacing in the HTML.

    //$this->assertText('Score 3 of 10');

    //$this->assertText('Score 7 of 10');
    $this
      ->assertText('Feedback for answer 1.');
    $this
      ->assertText('Feedback for answer 2.');
  }

  /**
   * Test adding and taking a long answer question.
   */
  public function testCreateQuizQuestion($settings = array()) {
    if (!$settings) {
      $settings = array(
        'rubric' => array(
          'value' => 'LA 1 rubric.',
          'format' => 'plain_text',
        ),
        'answer_text_processing' => 0,
      );
    }

    // Login as our privileged user.
    $this
      ->drupalLogin($this->admin);
    $question_node = $this
      ->drupalCreateNode(array(
      'type' => $this->question_node_type,
      'title' => 'LA 1 title',
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => 'LA 1 body text.',
          ),
        ),
      ),
    ) + $settings);
    return $question_node;
  }

  /**
   * Test that rubric and answer filter settings are respected.
   */
  public function testFilterFormats() {

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

    // Question that has no filtering, for rubric or answer.
    $question_node1 = $this
      ->drupalCreateNode(array(
      'type' => $this->question_node_type,
      'title' => 'LA 1 title',
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => 'LA 1 body text.',
          ),
        ),
      ),
      'rubric' => array(
        'value' => 'Rubric for LA 1, you will see the next tag <img src="http://httpbin.org/image/png?findmeRubricPlaintext"/>',
        'format' => 'plain_text',
      ),
      'answer_text_processing' => 0,
    ));

    // Question that has filtering, for rubric and answer.
    $question_node2 = $this
      ->drupalCreateNode(array(
      'type' => $this->question_node_type,
      'title' => 'LA 2 title',
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => 'LA 2 body text.',
          ),
        ),
      ),
      'rubric' => array(
        'value' => 'Rubric for LA 2, you will not see the next tag <img src="http://httpbin.org/image/png?findmeRubricFiltered"/>',
        'format' => 'full_html',
      ),
      'answer_text_processing' => 1,
    ));
    $quiz = $this
      ->linkQuestionToQuiz($question_node1);
    $this
      ->linkQuestionToQuiz($question_node2, $quiz);

    // Login as a user and take the quiz.
    $this
      ->drupalLogin($this->user);
    $this
      ->drupalGet("node/{$quiz->nid}/take");

    // Post plaintext answer.
    $this
      ->drupalPost(NULL, array(
      "question[{$question_node1->nid}][answer]" => 'plaintext answer, you will see the next tag: <img src="http://httpbin.org/image/png?findmeAnswerPlaintext"/>',
    ), t('Next'));

    // Post rich text answer.
    $this
      ->drupalPost(NULL, array(
      "question[{$question_node2->nid}][answer][value]" => 'filtered answer, you will see not see the next tag: <img src="http://httpbin.org/image/png?findmeAnswerFiltered"/>',
      "question[{$question_node2->nid}][answer][format]" => 'filtered_html',
    ), t('Finish'));

    // Login as a user and check the result.
    $this
      ->drupalLogin($this->admin);
    $this
      ->drupalGet("node/{$quiz->nid}/quiz/results/1/view");
    $this
      ->assertText('findmeRubricPlaintext', 'Plain text rubric image tag did not get rendered on page');
    $this
      ->assertNoText('findmeRubricFiltered', 'Filtered text rubric image tag got stripped');
    $this
      ->assertText('findmeAnswerPlaintext', 'Plain text answer image tag did not get rendered on page');
    $this
      ->assertNoText('findmeAnswerFiltered', 'Filtered text answer image tag got stripped');
  }

  /**
   * Test that the question response can be edited.
   */
  public function testEditQuestionResponse() {
    $this
      ->drupalLogin($this->admin);

    // 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);
    $this
      ->drupalGet("node/{$quiz_node->nid}/take");
    $this
      ->drupalGet("node/{$quiz_node->nid}/take/1");
    $this
      ->drupalPost(NULL, array(
      "question[{$question_node->nid}][answer]" => 'um some rule, I forget',
    ), t('Next'));
    $this
      ->drupalGet("node/{$quiz_node->nid}/take/1");
    $this
      ->drupalPost(NULL, array(
      "question[{$question_node->nid}][answer]" => 'um some rule, I forget',
    ), t('Next'));
  }

}

Classes

Namesort descending Description
LongAnswerTestCase Test class for long answer.