Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Unit tests for the true-false question definition class.
  19   *
  20   * @package    qtype
  21   * @subpackage truefalse
  22   * @copyright  2008 The Open University
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  31  
  32  
  33  /**
  34   * Unit tests for the true-false question definition class.
  35   *
  36   * @copyright  2008 The Open University
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class qtype_truefalse_question_test extends advanced_testcase {
  40      public function test_is_complete_response() {
  41          $question = test_question_maker::make_question('truefalse', 'true');
  42  
  43          $this->assertFalse($question->is_complete_response(array()));
  44          $this->assertTrue($question->is_complete_response(array('answer' => 0)));
  45          $this->assertTrue($question->is_complete_response(array('answer' => 1)));
  46      }
  47  
  48      public function test_is_gradable_response() {
  49          $question = test_question_maker::make_question('truefalse', 'true');
  50  
  51          $this->assertFalse($question->is_gradable_response(array()));
  52          $this->assertTrue($question->is_gradable_response(array('answer' => 0)));
  53          $this->assertTrue($question->is_gradable_response(array('answer' => 1)));
  54      }
  55  
  56      public function test_grading() {
  57          $question = test_question_maker::make_question('truefalse', 'true');
  58  
  59          $this->assertEquals(array(0, question_state::$gradedwrong),
  60                  $question->grade_response(array('answer' => 0)));
  61          $this->assertEquals(array(1, question_state::$gradedright),
  62                  $question->grade_response(array('answer' => 1)));
  63      }
  64  
  65      public function test_get_correct_response() {
  66          $question = test_question_maker::make_question('truefalse', 'true');
  67  
  68          // True.
  69          $this->assertSame(array('answer' => 1),
  70                  $question->get_correct_response());
  71  
  72          // False.
  73          $question->rightanswer = false;
  74          $this->assertSame(array('answer' => 0),
  75                  $question->get_correct_response());
  76      }
  77  
  78      public function test_get_question_summary() {
  79          $tf = test_question_maker::make_question('truefalse', 'true');
  80          $qsummary = $tf->get_question_summary();
  81          $this->assertEquals('The answer is true.', $qsummary);
  82      }
  83  
  84      public function test_summarise_response() {
  85          $tf = test_question_maker::make_question('truefalse', 'true');
  86  
  87          $this->assertEquals(get_string('false', 'qtype_truefalse'),
  88                  $tf->summarise_response(array('answer' => '0')));
  89  
  90          $this->assertEquals(get_string('true', 'qtype_truefalse'),
  91                  $tf->summarise_response(array('answer' => '1')));
  92      }
  93  
  94      public function test_classify_response() {
  95          $tf = test_question_maker::make_question('truefalse', 'true');
  96          $tf->start_attempt(new question_attempt_step(), 1);
  97  
  98          $this->assertEquals(array(
  99                  $tf->id => new question_classified_response(
 100                          0, get_string('false', 'qtype_truefalse'), 0.0)),
 101                  $tf->classify_response(array('answer' => '0')));
 102          $this->assertEquals(array(
 103                  $tf->id => new question_classified_response(
 104                          1, get_string('true', 'qtype_truefalse'), 1.0)),
 105                  $tf->classify_response(array('answer' => '1')));
 106          $this->assertEquals(array(
 107                  $tf->id => question_classified_response::no_response()),
 108                  $tf->classify_response(array()));
 109      }
 110  
 111      /**
 112       * test_get_question_definition_for_external_rendering
 113       */
 114      public function test_get_question_definition_for_external_rendering() {
 115          $this->resetAfterTest();
 116  
 117          $question = test_question_maker::make_question('truefalse', 'true');
 118          $question->start_attempt(new question_attempt_step(), 1);
 119          $qa = test_question_maker::get_a_qa($question);
 120          $displayoptions = new question_display_options();
 121  
 122          $options = $question->get_question_definition_for_external_rendering($qa, $displayoptions);
 123          $this->assertNull($options);
 124      }
 125  }