Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 311 and 403]

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