Differences Between: [Versions 311 and 402]
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 108 $q = $this->qtype->make_question($qdata); 109 110 $this->assertEquals($expected, $q); 111 } 112 113 public function test_get_random_guess_score() { 114 $q = $this->get_test_question_data(); 115 $this->assertEqualsWithDelta(0.5, $this->qtype->get_random_guess_score($q), 0.0000001); 116 } 117 118 public function test_get_possible_responses() { 119 $q = $this->get_test_question_data(); 120 121 $this->assertEquals(array( 122 1 => array( 123 1 => new question_possible_response('quick', 1 / 3), 124 2 => new question_possible_response('slow', 0), 125 null => question_possible_response::no_response()), 126 2 => array( 127 1 => new question_possible_response('fox', 1 / 3), 128 2 => new question_possible_response('dog', 0), 129 null => question_possible_response::no_response()), 130 3 => array( 131 1 => new question_possible_response('lazy', 1 / 3), 132 2 => new question_possible_response('assiduous', 0), 133 null => question_possible_response::no_response()), 134 ), $this->qtype->get_possible_responses($q)); 135 } 136 137 public function test_xml_import() { 138 $xml = ' <question type="gapselect"> 139 <name> 140 <text>A select missing words question</text> 141 </name> 142 <questiontext format="moodle_auto_format"> 143 <text>Put these in order: [[1]], [[2]], [[3]].</text> 144 </questiontext> 145 <generalfeedback> 146 <text>The answer is Alpha, Beta, Gamma.</text> 147 </generalfeedback> 148 <defaultgrade>3</defaultgrade> 149 <penalty>0.3333333</penalty> 150 <hidden>0</hidden> 151 <shuffleanswers>1</shuffleanswers> 152 <correctfeedback> 153 <text><![CDATA[<p>Your answer is correct.</p>]]></text> 154 </correctfeedback> 155 <partiallycorrectfeedback> 156 <text><![CDATA[<p>Your answer is partially correct.</p>]]></text> 157 </partiallycorrectfeedback> 158 <incorrectfeedback> 159 <text><![CDATA[<p>Your answer is incorrect.</p>]]></text> 160 </incorrectfeedback> 161 <shownumcorrect/> 162 <selectoption> 163 <text>Alpha</text> 164 <group>1</group> 165 </selectoption> 166 <selectoption> 167 <text>Beta</text> 168 <group>1</group> 169 </selectoption> 170 <selectoption> 171 <text>Gamma</text> 172 <group>1</group> 173 </selectoption> 174 <hint format="moodle_auto_format"> 175 <text>Try again.</text> 176 <shownumcorrect /> 177 </hint> 178 <hint format="moodle_auto_format"> 179 <text>These are the first three letters of the Greek alphabet.</text> 180 <shownumcorrect /> 181 <clearwrong /> 182 </hint> 183 </question>'; 184 $xmldata = xmlize($xml); 185 186 $importer = new \qformat_xml(); 187 $q = $importer->try_importing_using_qtypes( 188 $xmldata['question'], null, null, 'gapselect'); 189 190 $expectedq = new \stdClass(); 191 $expectedq->qtype = 'gapselect'; 192 $expectedq->name = 'A select missing words question'; 193 $expectedq->questiontext = 'Put these in order: [[1]], [[2]], [[3]].'; 194 $expectedq->questiontextformat = FORMAT_MOODLE; 195 $expectedq->generalfeedback = 'The answer is Alpha, Beta, Gamma.'; 196 $expectedq->defaultmark = 3; 197 $expectedq->length = 1; 198 $expectedq->penalty = 0.3333333; 199 200 $expectedq->shuffleanswers = 1; 201 $expectedq->correctfeedback = array('text' => '<p>Your answer is correct.</p>', 202 'format' => FORMAT_MOODLE); 203 $expectedq->partiallycorrectfeedback = array( 204 'text' => '<p>Your answer is partially correct.</p>', 205 'format' => FORMAT_MOODLE); 206 $expectedq->shownumcorrect = true; 207 $expectedq->incorrectfeedback = array('text' => '<p>Your answer is incorrect.</p>', 208 'format' => FORMAT_MOODLE); 209 210 $expectedq->choices = array( 211 array('answer' => 'Alpha', 'choicegroup' => 1), 212 array('answer' => 'Beta', 'choicegroup' => 1), 213 array('answer' => 'Gamma', 'choicegroup' => 1), 214 ); 215 216 $expectedq->hint = array( 217 array('text' => 'Try again.', 'format' => FORMAT_MOODLE), 218 array('text' => 'These are the first three letters of the Greek alphabet.', 219 'format' => FORMAT_MOODLE)); 220 $expectedq->hintshownumcorrect = array(true, true); 221 $expectedq->hintclearwrong = array(false, true); 222 $this->assert(new \question_check_specified_fields_expectation($expectedq), $q); 223 $this->assertEquals($expectedq->hint, $q->hint); 224 } 225 226 public function test_xml_export() { 227 $qdata = new \stdClass(); 228 $qdata->id = 123; 229 $qdata->contextid = \context_system::instance()->id; 230 $qdata->idnumber = null; 231 $qdata->qtype = 'gapselect'; 232 $qdata->name = 'A select missing words question'; 233 $qdata->questiontext = 'Put these in order: [[1]], [[2]], [[3]].'; 234 $qdata->questiontextformat = FORMAT_MOODLE; 235 $qdata->generalfeedback = 'The answer is Alpha, Beta, Gamma.'; 236 $qdata->generalfeedbackformat = FORMAT_MOODLE; 237 $qdata->defaultmark = 3; 238 $qdata->length = 1; 239 $qdata->penalty = 0.3333333; 240 $qdata->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY; 241 242 $qdata->options = new \stdClass(); 243 $qdata->options->shuffleanswers = 1; 244 $qdata->options->correctfeedback = '<p>Your answer is correct.</p>'; 245 $qdata->options->correctfeedbackformat = FORMAT_MOODLE; 246 $qdata->options->partiallycorrectfeedback = '<p>Your answer is partially correct.</p>'; 247 $qdata->options->partiallycorrectfeedbackformat = FORMAT_MOODLE; 248 $qdata->options->shownumcorrect = true; 249 $qdata->options->incorrectfeedback = '<p>Your answer is incorrect.</p>'; 250 $qdata->options->incorrectfeedbackformat = FORMAT_MOODLE; 251 252 $qdata->options->answers = array( 253 13 => new question_answer(13, 'Alpha', 0, '1', FORMAT_MOODLE), 254 14 => new question_answer(14, 'Beta', 0, '1', FORMAT_MOODLE), 255 15 => new question_answer(15, 'Gamma', 0, '1', FORMAT_MOODLE), 256 ); 257 258 $qdata->hints = array( 259 1 => new question_hint_with_parts(1, 'Try again.', FORMAT_MOODLE, true, false), 260 2 => new question_hint_with_parts(2, 261 'These are the first three letters of the Greek alphabet.', 262 FORMAT_MOODLE, true, true), 263 ); 264 265 $exporter = new \qformat_xml(); 266 $xml = $exporter->writequestion($qdata); 267 268 $expectedxml = '<!-- question: 123 --> 269 <question type="gapselect"> 270 <name> 271 <text>A select missing words question</text> 272 </name> 273 <questiontext format="moodle_auto_format"> 274 <text>Put these in order: [[1]], [[2]], [[3]].</text> 275 </questiontext> 276 <generalfeedback format="moodle_auto_format"> 277 <text>The answer is Alpha, Beta, Gamma.</text> 278 </generalfeedback> 279 <defaultgrade>3</defaultgrade> 280 <penalty>0.3333333</penalty> 281 <hidden>0</hidden> 282 <idnumber></idnumber> 283 <shuffleanswers>1</shuffleanswers> 284 <correctfeedback format="moodle_auto_format"> 285 <text><![CDATA[<p>Your answer is correct.</p>]]></text> 286 </correctfeedback> 287 <partiallycorrectfeedback format="moodle_auto_format"> 288 <text><![CDATA[<p>Your answer is partially correct.</p>]]></text> 289 </partiallycorrectfeedback> 290 <incorrectfeedback format="moodle_auto_format"> 291 <text><![CDATA[<p>Your answer is incorrect.</p>]]></text> 292 </incorrectfeedback> 293 <shownumcorrect/> 294 <selectoption> 295 <text>Alpha</text> 296 <group>1</group> 297 </selectoption> 298 <selectoption> 299 <text>Beta</text> 300 <group>1</group> 301 </selectoption> 302 <selectoption> 303 <text>Gamma</text> 304 <group>1</group> 305 </selectoption> 306 <hint format="moodle_auto_format"> 307 <text>Try again.</text> 308 <shownumcorrect/> 309 </hint> 310 <hint format="moodle_auto_format"> 311 <text>These are the first three letters of the Greek alphabet.</text> 312 <shownumcorrect/> 313 <clearwrong/> 314 </hint> 315 </question> 316 '; 317 318 $this->assert_same_xml($expectedxml, $xml); 319 } 320 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body