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 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  2007 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/type/truefalse/questiontype.php');
  31  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  32  require_once($CFG->dirroot . '/question/type/edit_question_form.php');
  33  require_once($CFG->dirroot . '/question/type/truefalse/edit_truefalse_form.php');
  34  
  35  /**
  36   * Unit tests for the true-false question definition class.
  37   *
  38   * @copyright  2007 The Open University
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class qtype_truefalse_test extends advanced_testcase {
  42      protected $qtype;
  43  
  44      protected function setUp(): void {
  45          $this->qtype = new qtype_truefalse();
  46      }
  47  
  48      protected function tearDown(): void {
  49          $this->qtype = null;
  50      }
  51  
  52      public function test_name() {
  53          $this->assertEquals($this->qtype->name(), 'truefalse');
  54      }
  55  
  56      public function test_can_analyse_responses() {
  57          $this->assertTrue($this->qtype->can_analyse_responses());
  58      }
  59  
  60      public function test_get_random_guess_score() {
  61          $this->assertEquals(0.5, $this->qtype->get_random_guess_score(null));
  62      }
  63  
  64      public function test_load_question() {
  65          $this->resetAfterTest();
  66  
  67          $syscontext = context_system::instance();
  68          /** @var core_question_generator $generator */
  69          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  70          $category = $generator->create_question_category(['contextid' => $syscontext->id]);
  71  
  72          $fromform = test_question_maker::get_question_form_data('truefalse');
  73          $fromform->category = $category->id . ',' . $syscontext->id;
  74  
  75          $question = new stdClass();
  76          $question->category = $category->id;
  77          $question->qtype = 'truefalse';
  78          $question->createdby = 0;
  79  
  80          $this->qtype->save_question($question, $fromform);
  81          $questiondata = question_bank::load_question_data($question->id);
  82  
  83          $this->assertEquals(['id', 'category', 'parent', 'name', 'questiontext', 'questiontextformat',
  84                  'generalfeedback', 'generalfeedbackformat', 'defaultmark', 'penalty', 'qtype',
  85                  'length', 'stamp', 'version', 'hidden', 'timecreated', 'timemodified',
  86                  'createdby', 'modifiedby', 'idnumber', 'contextid', 'options', 'hints', 'categoryobject'],
  87                  array_keys(get_object_vars($questiondata)));
  88          $this->assertEquals($category->id, $questiondata->category);
  89          $this->assertEquals(0, $questiondata->parent);
  90          $this->assertEquals($fromform->name, $questiondata->name);
  91          $this->assertEquals($fromform->questiontext, $questiondata->questiontext);
  92          $this->assertEquals($fromform->questiontextformat, $questiondata->questiontextformat);
  93          $this->assertEquals($fromform->generalfeedback['text'], $questiondata->generalfeedback);
  94          $this->assertEquals($fromform->generalfeedback['format'], $questiondata->generalfeedbackformat);
  95          $this->assertEquals($fromform->defaultmark, $questiondata->defaultmark);
  96          $this->assertEquals(1, $questiondata->penalty);
  97          $this->assertEquals('truefalse', $questiondata->qtype);
  98          $this->assertEquals(1, $questiondata->length);
  99          $this->assertEquals(0, $questiondata->hidden);
 100          $this->assertEquals($question->createdby, $questiondata->createdby);
 101          $this->assertEquals($question->createdby, $questiondata->modifiedby);
 102          $this->assertEquals('', $questiondata->idnumber);
 103          $this->assertEquals($syscontext->id, $questiondata->contextid);
 104  
 105          // Options.
 106          $this->assertEquals($questiondata->id, $questiondata->options->question);
 107          $this->assertEquals('True', $questiondata->options->answers[$questiondata->options->trueanswer]->answer);
 108          $this->assertEquals('False', $questiondata->options->answers[$questiondata->options->falseanswer]->answer);
 109          $this->assertEquals(1.0, $questiondata->options->answers[$questiondata->options->trueanswer]->fraction);
 110          $this->assertEquals(0.0, $questiondata->options->answers[$questiondata->options->falseanswer]->fraction);
 111          $this->assertEquals('This is the right answer.',
 112                  $questiondata->options->answers[$questiondata->options->trueanswer]->feedback);
 113          $this->assertEquals('This is the wrong answer.',
 114                  $questiondata->options->answers[$questiondata->options->falseanswer]->feedback);
 115          $this->assertEquals(FORMAT_HTML, $questiondata->options->answers[$questiondata->options->trueanswer]->feedbackformat);
 116          $this->assertEquals(FORMAT_HTML, $questiondata->options->answers[$questiondata->options->falseanswer]->feedbackformat);
 117  
 118          // Hints.
 119          $this->assertEquals([], $questiondata->hints);
 120      }
 121  
 122      public function test_get_possible_responses() {
 123          $q = new stdClass();
 124          $q->id = 1;
 125          $q->options = new stdClass();
 126          $q->options->trueanswer = 1;
 127          $q->options->falseanswer = 2;
 128          $q->options->answers[1] = (object) array('fraction' => 1);
 129          $q->options->answers[2] = (object) array('fraction' => 0);
 130  
 131          $this->assertEquals(array(
 132              $q->id => array(
 133                  0 => new question_possible_response(get_string('false', 'qtype_truefalse'), 0),
 134                  1 => new question_possible_response(get_string('true', 'qtype_truefalse'), 1),
 135                  null => question_possible_response::no_response()),
 136          ), $this->qtype->get_possible_responses($q));
 137      }
 138  
 139      public function test_question_saving_true() {
 140          $this->resetAfterTest(true);
 141          $this->setAdminUser();
 142  
 143          $questiondata = test_question_maker::get_question_data('truefalse');
 144          $formdata = test_question_maker::get_question_form_data('truefalse');
 145  
 146          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 147          $cat = $generator->create_question_category(array());
 148  
 149          $formdata->category = "{$cat->id},{$cat->contextid}";
 150          qtype_truefalse_edit_form::mock_submit((array)$formdata);
 151  
 152          $form = qtype_truefalse_test_helper::get_question_editing_form($cat, $questiondata);
 153  
 154          $this->assertTrue($form->is_validated());
 155  
 156          $fromform = $form->get_data();
 157  
 158          $returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
 159          $actualquestionsdata = question_load_questions(array($returnedfromsave->id));
 160          $actualquestiondata = end($actualquestionsdata);
 161  
 162          foreach ($questiondata as $property => $value) {
 163              if (!in_array($property, array('options'))) {
 164                  $this->assertEquals($value, $actualquestiondata->$property);
 165              }
 166          }
 167  
 168          foreach ($questiondata->options as $optionname => $value) {
 169              if (!in_array($optionname, array('trueanswer', 'falseanswer', 'answers'))) {
 170                  $this->assertEquals($value, $actualquestiondata->options->$optionname);
 171              }
 172          }
 173  
 174          $answerindexes = array();
 175          foreach ($questiondata->options->answers as $ansindex => $answer) {
 176              $actualanswer = array_shift($actualquestiondata->options->answers);
 177              foreach ($answer as $ansproperty => $ansvalue) {
 178                  // This question does not use 'answerformat', will ignore it.
 179                  if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) {
 180                      $this->assertEquals($ansvalue, $actualanswer->$ansproperty);
 181                  }
 182              }
 183              $answerindexes[$answer->answer] = $ansindex;
 184          }
 185  
 186          $this->assertEquals($questiondata->options->trueanswer, $answerindexes['True']);
 187          $this->assertEquals($questiondata->options->falseanswer, $answerindexes['False']);
 188      }
 189  }