You are here

scale.test in Quiz 7.5

Test suite for scale question type module.

File

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

/**
 * @file
 * Test suite for scale question type module.
 */

/**
 * Test class for scale questions.
 */
class ScaleTestCase extends QuizQuestionTestCase {
  var $question_node_type = 'scale';

  /**
   * The getInfo() method provides information about the test.
   *
   * In order for the test to be run, the getInfo() method needs
   * to be implemented.
   */
  public static function getInfo() {
    return array(
      'name' => t('Scale'),
      'description' => t('Unit test for scale question type.'),
      'group' => t('Quiz'),
    );
  }

  /**
   * Implementing setUp() to enable scale module testing.
   */
  public function setUp($modules = array(), $admin_permissions = array(), $user_permissions = array()) {
    parent::setUp(array(
      'scale',
    ));
  }

  /**
   * Create a new question with default settings.
   */
  public function testCreateQuizQuestion() {

    // Login as our privileged user.
    $this
      ->drupalLogin($this->admin);
    $settings = array();
    $settings['type'] = 'scale';
    $question_node = $this
      ->drupalCreateNode(array(
      'type' => 'scale',
      'title' => 'Scale 1 title',
      'alternative0' => 'This is alternative 1',
      'alternative1' => 'This is alternative 2',
      'alternative2' => 'This is alternative 3',
      'alternative3' => 'This is alternative 4',
      'alternative4' => 'This is alternative 5',
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => 'Scale 1 body text',
          ),
        ),
      ),
    ));
    return $question_node;
  }
  public 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('Scale 1 title');

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

    // Take the quiz.
    $this
      ->drupalGet("node/{$quiz_node->nid}/take");
    $this
      ->assertNoText('Scale 1 title');
    $this
      ->assertText('Scale 1 body text');
    $this
      ->assertText('This is alternative 1');
    $this
      ->assertText('This is alternative 2');

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

    // Test any question.
    $this
      ->drupalGet("node/{$quiz_node->nid}/take");
    $this
      ->drupalPost(NULL, array(
      // Alternative #2 is actually 24 (because of the presets).
      "question[{$question_node->nid}][answer]" => 24,
    ), t('Finish'));
    $this
      ->assertText('You got 1 of 1 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]" => 24,
    ), t('Next'));
    $this
      ->drupalGet("node/{$quiz_node->nid}/take/1");
    $this
      ->drupalPost(NULL, array(
      "question[{$question_node->nid}][answer]" => 24,
    ), t('Next'));
  }
  public function testProgrammaticSave() {
    $settings = array();
    $settings['type'] = 'scale';
    $question_node = $this
      ->drupalCreateNode(array(
      'type' => 'scale',
      'title' => 'Scale 1 title',
      'alternative0' => 'This is alternative 1',
      'alternative1' => 'This is alternative 2',
      'alternative2' => 'This is alternative 3',
      'alternative3' => 'This is alternative 4',
      'alternative4' => 'This is alternative 5',
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => 'Scale 1 body text',
          ),
        ),
      ),
    ));
    node_save($question_node);
    $question_node = node_load($question_node->nid);
    $this
      ->assertTrue(!empty($question_node->scale[0]), 'Alternatives persist after programmatic save.');
  }

}

Classes

Namesort descending Description
ScaleTestCase Test class for scale questions.