Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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  namespace qtype_truefalse;
  18  
  19  use question_attempt_step;
  20  use question_classified_response;
  21  use question_display_options;
  22  use question_state;
  23  
  24  defined('MOODLE_INTERNAL') || die();
  25  
  26  global $CFG;
  27  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  28  
  29  
  30  /**
  31   * Unit tests for the true-false question definition class.
  32   *
  33   * @package    qtype_truefalse
  34   * @copyright  2008 The Open University
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class question_test extends \advanced_testcase {
  38      public function test_is_complete_response() {
  39          $question = \test_question_maker::make_question('truefalse', 'true');
  40  
  41          $this->assertFalse($question->is_complete_response(array()));
  42          $this->assertTrue($question->is_complete_response(array('answer' => 0)));
  43          $this->assertTrue($question->is_complete_response(array('answer' => 1)));
  44      }
  45  
  46      public function test_is_gradable_response() {
  47          $question = \test_question_maker::make_question('truefalse', 'true');
  48  
  49          $this->assertFalse($question->is_gradable_response(array()));
  50          $this->assertTrue($question->is_gradable_response(array('answer' => 0)));
  51          $this->assertTrue($question->is_gradable_response(array('answer' => 1)));
  52      }
  53  
  54      public function test_grading() {
  55          $question = \test_question_maker::make_question('truefalse', 'true');
  56  
  57          $this->assertEquals(array(0, question_state::$gradedwrong),
  58                  $question->grade_response(array('answer' => 0)));
  59          $this->assertEquals(array(1, question_state::$gradedright),
  60                  $question->grade_response(array('answer' => 1)));
  61      }
  62  
  63      public function test_get_correct_response() {
  64          $question = \test_question_maker::make_question('truefalse', 'true');
  65  
  66          // True.
  67          $this->assertSame(array('answer' => 1),
  68                  $question->get_correct_response());
  69  
  70          // False.
  71          $question->rightanswer = false;
  72          $this->assertSame(array('answer' => 0),
  73                  $question->get_correct_response());
  74      }
  75  
  76      public function test_get_question_summary() {
  77          $tf = \test_question_maker::make_question('truefalse', 'true');
  78          $qsummary = $tf->get_question_summary();
  79          $this->assertEquals('The answer is true.', $qsummary);
  80      }
  81  
  82      public function test_summarise_response() {
  83          $tf = \test_question_maker::make_question('truefalse', 'true');
  84  
  85          $this->assertEquals(get_string('false', 'qtype_truefalse'),
  86                  $tf->summarise_response(array('answer' => '0')));
  87  
  88          $this->assertEquals(get_string('true', 'qtype_truefalse'),
  89                  $tf->summarise_response(array('answer' => '1')));
  90      }
  91  
  92      public function test_classify_response() {
  93          $tf = \test_question_maker::make_question('truefalse', 'true');
  94          $tf->start_attempt(new question_attempt_step(), 1);
  95  
  96          $this->assertEquals(array(
  97                  $tf->id => new question_classified_response(
  98                          0, get_string('false', 'qtype_truefalse'), 0.0)),
  99                  $tf->classify_response(array('answer' => '0')));
 100          $this->assertEquals(array(
 101                  $tf->id => new question_classified_response(
 102                          1, get_string('true', 'qtype_truefalse'), 1.0)),
 103                  $tf->classify_response(array('answer' => '1')));
 104          $this->assertEquals(array(
 105                  $tf->id => question_classified_response::no_response()),
 106                  $tf->classify_response(array()));
 107      }
 108  
 109      /**
 110       * test_get_question_definition_for_external_rendering
 111       */
 112      public function test_get_question_definition_for_external_rendering() {
 113          $this->resetAfterTest();
 114  
 115          $question = \test_question_maker::make_question('truefalse', 'true');
 116          $question->start_attempt(new question_attempt_step(), 1);
 117          $qa = \test_question_maker::get_a_qa($question);
 118          $displayoptions = new question_display_options();
 119  
 120          $options = $question->get_question_definition_for_external_rendering($qa, $displayoptions);
 121          $this->assertNull($options);
 122      }
 123  }