Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [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_gapselect;
  18  
  19  use question_answer;
  20  use question_bank;
  21  use question_hint_with_parts;
  22  use question_possible_response;
  23  
  24  defined('MOODLE_INTERNAL') || die();
  25  global $CFG;
  26  
  27  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  28  
  29  
  30  /**
  31   * Unit tests for the select missing words question definition class.
  32   *
  33   * @package   qtype_gapselect
  34   * @copyright 2012 The Open University
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class question_type_test extends \question_testcase {
  38      /** @var qtype_gapselect instance of the question type class to test. */
  39      protected $qtype;
  40  
  41      protected function setUp(): void {
  42          $this->qtype = question_bank::get_qtype('gapselect');
  43      }
  44  
  45      protected function tearDown(): void {
  46          $this->qtype = null;
  47      }
  48  
  49      /**
  50       * Asserts that two strings containing XML are the same ignoring the line-endings.
  51       *
  52       * @param string $expectedxml
  53       * @param string $xml
  54       */
  55      public function assert_same_xml($expectedxml, $xml) {
  56          $this->assertEquals(str_replace("\r\n", "\n", $expectedxml),
  57                  str_replace("\r\n", "\n", $xml));
  58      }
  59  
  60      public function test_save_question() {
  61          $this->resetAfterTest();
  62  
  63          $syscontext = \context_system::instance();
  64          /** @var core_question_generator $generator */
  65          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  66          $category = $generator->create_question_category(['contextid' => $syscontext->id]);
  67  
  68          $fromform = \test_question_maker::get_question_form_data('gapselect', 'missingchoiceno');
  69          $fromform->category = $category->id . ',' . $syscontext->id;
  70  
  71          $question = new \stdClass();
  72          $question->category = $category->id;
  73          $question->qtype = 'gapselect';
  74          $question->createdby = 0;
  75  
  76          $this->qtype->save_question($question, $fromform);
  77          $q = question_bank::load_question($question->id);
  78          // We just want to verify that this does not cause errors,
  79          // but also verify some of the outcome.
  80          $this->assertEquals('The [[1]] sat on the [[2]].', $q->questiontext);
  81          $this->assertEquals([1 => 1, 2 => 1], $q->places);
  82          $this->assertEquals([1 => 1, 2 => 2], $q->rightchoices);
  83      }
  84  
  85      /**
  86       * Get some test question data.
  87       * @return object the data to construct a question like
  88       * {@see \test_question_maker::make_question('gapselect')}.
  89       */
  90      protected function get_test_question_data() {
  91          return \test_question_maker::get_question_data('gapselect');
  92      }
  93  
  94      public function test_name() {
  95          $this->assertEquals($this->qtype->name(), 'gapselect');
  96      }
  97  
  98      public function test_can_analyse_responses() {
  99          $this->assertTrue($this->qtype->can_analyse_responses());
 100      }
 101  
 102      public function test_initialise_question_instance() {
 103          $qdata = $this->get_test_question_data();
 104  
 105          $expected = \test_question_maker::make_question('gapselect');
 106          $expected->stamp = $qdata->stamp;
 107          $expected->version = $qdata->version;
 108  
 109          $q = $this->qtype->make_question($qdata);
 110  
 111          $this->assertEquals($expected, $q);
 112      }
 113  
 114      public function test_get_random_guess_score() {
 115          $q = $this->get_test_question_data();
 116          $this->assertEqualsWithDelta(0.5, $this->qtype->get_random_guess_score($q), 0.0000001);
 117      }
 118  
 119      public function test_get_possible_responses() {
 120          $q = $this->get_test_question_data();
 121  
 122          $this->assertEquals(array(
 123              1 => array(
 124                  1 => new question_possible_response('quick', 1 / 3),
 125                  2 => new question_possible_response('slow', 0),
 126                  null => question_possible_response::no_response()),
 127              2 => array(
 128                  1 => new question_possible_response('fox', 1 / 3),
 129                  2 => new question_possible_response('dog', 0),
 130                  null => question_possible_response::no_response()),
 131              3 => array(
 132                  1 => new question_possible_response('lazy', 1 / 3),
 133                  2 => new question_possible_response('assiduous', 0),
 134                  null => question_possible_response::no_response()),
 135          ), $this->qtype->get_possible_responses($q));
 136      }
 137  
 138      public function test_xml_import() {
 139          $xml = '  <question type="gapselect">
 140      <name>
 141        <text>A select missing words question</text>
 142      </name>
 143      <questiontext format="moodle_auto_format">
 144        <text>Put these in order: [[1]], [[2]], [[3]].</text>
 145      </questiontext>
 146      <generalfeedback>
 147        <text>The answer is Alpha, Beta, Gamma.</text>
 148      </generalfeedback>
 149      <defaultgrade>3</defaultgrade>
 150      <penalty>0.3333333</penalty>
 151      <hidden>0</hidden>
 152      <shuffleanswers>1</shuffleanswers>
 153      <correctfeedback>
 154        <text><![CDATA[<p>Your answer is correct.</p>]]></text>
 155      </correctfeedback>
 156      <partiallycorrectfeedback>
 157        <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
 158      </partiallycorrectfeedback>
 159      <incorrectfeedback>
 160        <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
 161      </incorrectfeedback>
 162      <shownumcorrect/>
 163      <selectoption>
 164        <text>Alpha</text>
 165        <group>1</group>
 166      </selectoption>
 167      <selectoption>
 168        <text>Beta</text>
 169        <group>1</group>
 170      </selectoption>
 171      <selectoption>
 172        <text>Gamma</text>
 173        <group>1</group>
 174      </selectoption>
 175      <hint format="moodle_auto_format">
 176        <text>Try again.</text>
 177        <shownumcorrect />
 178      </hint>
 179      <hint format="moodle_auto_format">
 180        <text>These are the first three letters of the Greek alphabet.</text>
 181        <shownumcorrect />
 182        <clearwrong />
 183      </hint>
 184    </question>';
 185          $xmldata = xmlize($xml);
 186  
 187          $importer = new \qformat_xml();
 188          $q = $importer->try_importing_using_qtypes(
 189                  $xmldata['question'], null, null, 'gapselect');
 190  
 191          $expectedq = new \stdClass();
 192          $expectedq->qtype = 'gapselect';
 193          $expectedq->name = 'A select missing words question';
 194          $expectedq->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
 195          $expectedq->questiontextformat = FORMAT_MOODLE;
 196          $expectedq->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
 197          $expectedq->defaultmark = 3;
 198          $expectedq->length = 1;
 199          $expectedq->penalty = 0.3333333;
 200  
 201          $expectedq->shuffleanswers = 1;
 202          $expectedq->correctfeedback = array('text' => '<p>Your answer is correct.</p>',
 203                  'format' => FORMAT_MOODLE);
 204          $expectedq->partiallycorrectfeedback = array(
 205                  'text' => '<p>Your answer is partially correct.</p>',
 206                  'format' => FORMAT_MOODLE);
 207          $expectedq->shownumcorrect = true;
 208          $expectedq->incorrectfeedback = array('text' => '<p>Your answer is incorrect.</p>',
 209                  'format' => FORMAT_MOODLE);
 210  
 211          $expectedq->choices = array(
 212              array('answer' => 'Alpha', 'choicegroup' => 1),
 213              array('answer' => 'Beta', 'choicegroup' => 1),
 214              array('answer' => 'Gamma', 'choicegroup' => 1),
 215          );
 216  
 217          $expectedq->hint = array(
 218                  array('text' => 'Try again.', 'format' => FORMAT_MOODLE),
 219                  array('text' => 'These are the first three letters of the Greek alphabet.',
 220                          'format' => FORMAT_MOODLE));
 221          $expectedq->hintshownumcorrect = array(true, true);
 222          $expectedq->hintclearwrong = array(false, true);
 223          $this->assert(new \question_check_specified_fields_expectation($expectedq), $q);
 224          $this->assertEquals($expectedq->hint, $q->hint);
 225      }
 226  
 227      public function test_xml_export() {
 228          $qdata = new \stdClass();
 229          $qdata->id = 123;
 230          $qdata->contextid = \context_system::instance()->id;
 231          $qdata->idnumber = null;
 232          $qdata->qtype = 'gapselect';
 233          $qdata->name = 'A select missing words question';
 234          $qdata->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
 235          $qdata->questiontextformat = FORMAT_MOODLE;
 236          $qdata->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
 237          $qdata->generalfeedbackformat = FORMAT_MOODLE;
 238          $qdata->defaultmark = 3;
 239          $qdata->length = 1;
 240          $qdata->penalty = 0.3333333;
 241          $qdata->hidden = 0;
 242  
 243          $qdata->options = new \stdClass();
 244          $qdata->options->shuffleanswers = 1;
 245          $qdata->options->correctfeedback = '<p>Your answer is correct.</p>';
 246          $qdata->options->correctfeedbackformat = FORMAT_MOODLE;
 247          $qdata->options->partiallycorrectfeedback = '<p>Your answer is partially correct.</p>';
 248                  $qdata->options->partiallycorrectfeedbackformat = FORMAT_MOODLE;
 249          $qdata->options->shownumcorrect = true;
 250          $qdata->options->incorrectfeedback = '<p>Your answer is incorrect.</p>';
 251          $qdata->options->incorrectfeedbackformat = FORMAT_MOODLE;
 252  
 253          $qdata->options->answers = array(
 254              13 => new question_answer(13, 'Alpha', 0, '1', FORMAT_MOODLE),
 255              14 => new question_answer(14, 'Beta', 0, '1', FORMAT_MOODLE),
 256              15 => new question_answer(15, 'Gamma', 0, '1', FORMAT_MOODLE),
 257          );
 258  
 259          $qdata->hints = array(
 260              1 => new question_hint_with_parts(1, 'Try again.', FORMAT_MOODLE, true, false),
 261              2 => new question_hint_with_parts(2,
 262                      'These are the first three letters of the Greek alphabet.',
 263                      FORMAT_MOODLE, true, true),
 264          );
 265  
 266          $exporter = new \qformat_xml();
 267          $xml = $exporter->writequestion($qdata);
 268  
 269          $expectedxml = '<!-- question: 123  -->
 270    <question type="gapselect">
 271      <name>
 272        <text>A select missing words question</text>
 273      </name>
 274      <questiontext format="moodle_auto_format">
 275        <text>Put these in order: [[1]], [[2]], [[3]].</text>
 276      </questiontext>
 277      <generalfeedback format="moodle_auto_format">
 278        <text>The answer is Alpha, Beta, Gamma.</text>
 279      </generalfeedback>
 280      <defaultgrade>3</defaultgrade>
 281      <penalty>0.3333333</penalty>
 282      <hidden>0</hidden>
 283      <idnumber></idnumber>
 284      <shuffleanswers>1</shuffleanswers>
 285      <correctfeedback format="moodle_auto_format">
 286        <text><![CDATA[<p>Your answer is correct.</p>]]></text>
 287      </correctfeedback>
 288      <partiallycorrectfeedback format="moodle_auto_format">
 289        <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
 290      </partiallycorrectfeedback>
 291      <incorrectfeedback format="moodle_auto_format">
 292        <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
 293      </incorrectfeedback>
 294      <shownumcorrect/>
 295      <selectoption>
 296        <text>Alpha</text>
 297        <group>1</group>
 298      </selectoption>
 299      <selectoption>
 300        <text>Beta</text>
 301        <group>1</group>
 302      </selectoption>
 303      <selectoption>
 304        <text>Gamma</text>
 305        <group>1</group>
 306      </selectoption>
 307      <hint format="moodle_auto_format">
 308        <text>Try again.</text>
 309        <shownumcorrect/>
 310      </hint>
 311      <hint format="moodle_auto_format">
 312        <text>These are the first three letters of the Greek alphabet.</text>
 313        <shownumcorrect/>
 314        <clearwrong/>
 315      </hint>
 316    </question>
 317  ';
 318  
 319          $this->assert_same_xml($expectedxml, $xml);
 320      }
 321  }