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_multianswer; 18 19 use qtype_multianswer; 20 use qtype_multianswer_edit_form; 21 use qtype_multichoice_base; 22 use question_bank; 23 use test_question_maker; 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 global $CFG; 28 require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); 29 require_once($CFG->dirroot . '/question/type/multianswer/questiontype.php'); 30 require_once($CFG->dirroot . '/question/type/edit_question_form.php'); 31 require_once($CFG->dirroot . '/question/type/multianswer/edit_multianswer_form.php'); 32 33 34 /** 35 * Unit tests for the multianswer question definition class. 36 * 37 * @package qtype_multianswer 38 * @copyright 2011 The Open University 39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 40 * @covers \qtype_multianswer 41 */ 42 class question_type_test extends \advanced_testcase { 43 /** @var qtype_multianswer instance of the question type class to test. */ 44 protected $qtype; 45 46 protected function setUp(): void { 47 $this->qtype = new qtype_multianswer(); 48 } 49 50 protected function tearDown(): void { 51 $this->qtype = null; 52 } 53 54 protected function get_test_question_data() { 55 global $USER; 56 $q = new \stdClass(); 57 $q->id = 0; 58 $q->name = 'Simple multianswer'; 59 $q->category = 0; 60 $q->contextid = 0; 61 $q->parent = 0; 62 $q->questiontext = 63 'Complete this opening line of verse: "The {#1} and the {#2} went to sea".'; 64 $q->questiontextformat = FORMAT_HTML; 65 $q->generalfeedback = 'Generalfeedback: It\'s from "The Owl and the Pussy-cat" by Lear: ' . 66 '"The owl and the pussycat went to see'; 67 $q->generalfeedbackformat = FORMAT_HTML; 68 $q->defaultmark = 2; 69 $q->penalty = 0.3333333; 70 $q->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY; 71 $q->versionid = 0; 72 $q->version = 1; 73 $q->questionbankentryid = 0; 74 $q->length = 1; 75 $q->stamp = make_unique_id_code(); 76 $q->timecreated = time(); 77 $q->timemodified = time(); 78 $q->createdby = $USER->id; 79 $q->modifiedby = $USER->id; 80 81 $sadata = new \stdClass(); 82 $sadata->id = 1; 83 $sadata->qtype = 'shortanswer'; 84 $sadata->defaultmark = 1; 85 $sadata->options->usecase = true; 86 $sadata->options->answers[1] = (object) array('answer' => 'Bow-wow', 'fraction' => 0); 87 $sadata->options->answers[2] = (object) array('answer' => 'Wiggly worm', 'fraction' => 0); 88 $sadata->options->answers[3] = (object) array('answer' => 'Pussy-cat', 'fraction' => 1); 89 90 $mcdata = new \stdClass(); 91 $mcdata->id = 1; 92 $mcdata->qtype = 'multichoice'; 93 $mcdata->defaultmark = 1; 94 $mcdata->options->single = true; 95 $mcdata->options->answers[1] = (object) array('answer' => 'Dog', 'fraction' => 0); 96 $mcdata->options->answers[2] = (object) array('answer' => 'Owl', 'fraction' => 1); 97 $mcdata->options->answers[3] = (object) array('answer' => '*', 'fraction' => 0); 98 99 $q->options->questions = array( 100 1 => $sadata, 101 2 => $mcdata, 102 ); 103 104 return $q; 105 } 106 107 public function test_name() { 108 $this->assertEquals($this->qtype->name(), 'multianswer'); 109 } 110 111 public function test_can_analyse_responses() { 112 $this->assertFalse($this->qtype->can_analyse_responses()); 113 } 114 115 public function test_get_random_guess_score() { 116 $q = test_question_maker::get_question_data('multianswer', 'twosubq'); 117 $this->assertEqualsWithDelta(0.1666667, $this->qtype->get_random_guess_score($q), 0.0000001); 118 } 119 120 public function test_get_random_guess_score_with_missing_subquestion() { 121 global $DB; 122 $this->resetAfterTest(); 123 124 // Create a question referring to a subquesion that has got lost (which happens some time). 125 /** @var \core_question_generator $generator */ 126 $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); 127 $category = $generator->create_question_category(); 128 $question = $generator->create_question('multianswer', 'twosubq', ['category' => $category->id]); 129 // Add a non-existent subquestion id to the list. 130 $sequence = $DB->get_field('question_multianswer', 'sequence', ['question' => $question->id], MUST_EXIST); 131 $DB->set_field('question_multianswer', 'sequence', $sequence . ',-1', ['question' => $question->id]); 132 133 // Verify that computing the random guess score does not give an error. 134 $questiondata = question_bank::load_question_data($question->id); 135 $this->assertEqualsWithDelta(0.1666667, $this->qtype->get_random_guess_score($questiondata), 0.0000001); 136 } 137 138 public function test_get_random_guess_score_with_all_missing_subquestions() { 139 $this->resetAfterTest(); 140 141 // Create a question where all subquestions are missing. 142 $questiondata = test_question_maker::get_question_data('multianswer', 'twosubq'); 143 foreach ($questiondata->options->questions as $subq) { 144 $subq->qtype = 'subquestion_replacement'; 145 } 146 147 // Verify that computing the random guess score does not give an error. 148 $this->assertNull($this->qtype->get_random_guess_score($questiondata)); 149 } 150 151 public function test_load_question() { 152 $this->resetAfterTest(); 153 154 $syscontext = \context_system::instance(); 155 /** @var \core_question_generator $generator */ 156 $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); 157 $category = $generator->create_question_category(['contextid' => $syscontext->id]); 158 159 $fromform = \test_question_maker::get_question_form_data('multianswer'); 160 $fromform->category = $category->id . ',' . $syscontext->id; 161 162 $question = new \stdClass(); 163 $question->category = $category->id; 164 $question->qtype = 'multianswer'; 165 $question->createdby = 0; 166 167 // Note, $question gets modified during save because of the way subquestions 168 // are extracted. 169 $question = $this->qtype->save_question($question, $fromform); 170 171 $questiondata = question_bank::load_question_data($question->id); 172 173 $this->assertEquals(['id', 'category', 'parent', 'name', 'questiontext', 'questiontextformat', 174 'generalfeedback', 'generalfeedbackformat', 'defaultmark', 'penalty', 'qtype', 175 'length', 'stamp', 'timecreated', 'timemodified', 'createdby', 'modifiedby', 'idnumber', 'contextid', 176 'status', 'versionid', 'version', 'questionbankentryid', 'categoryobject', 'options', 'hints'], 177 array_keys(get_object_vars($questiondata))); 178 $this->assertEquals($category->id, $questiondata->category); 179 $this->assertEquals(0, $questiondata->parent); 180 $this->assertEquals($fromform->name, $questiondata->name); 181 $this->assertEquals($fromform->questiontext, $questiondata->questiontext); 182 $this->assertEquals($fromform->questiontextformat, $questiondata->questiontextformat); 183 $this->assertEquals($fromform->generalfeedback['text'], $questiondata->generalfeedback); 184 $this->assertEquals($fromform->generalfeedback['format'], $questiondata->generalfeedbackformat); 185 $this->assertEquals($fromform->defaultmark, $questiondata->defaultmark); 186 $this->assertEquals(0, $questiondata->penalty); 187 $this->assertEquals('multianswer', $questiondata->qtype); 188 $this->assertEquals(1, $questiondata->length); 189 $this->assertEquals(\core_question\local\bank\question_version_status::QUESTION_STATUS_READY, $questiondata->status); 190 $this->assertEquals($question->createdby, $questiondata->createdby); 191 $this->assertEquals($question->createdby, $questiondata->modifiedby); 192 $this->assertEquals('', $questiondata->idnumber); 193 $this->assertEquals($syscontext->id, $questiondata->contextid); 194 195 // Build the expected hint base. 196 $hintbase = [ 197 'questionid' => $questiondata->id, 198 'shownumcorrect' => 0, 199 'clearwrong' => 0, 200 'options' => null]; 201 $expectedhints = []; 202 foreach ($fromform->hint as $key => $value) { 203 $hint = $hintbase + [ 204 'hint' => $value['text'], 205 'hintformat' => $value['format'], 206 ]; 207 $expectedhints[] = (object)$hint; 208 } 209 // Need to get rid of ids. 210 $gothints = array_map(function($hint) { 211 unset($hint->id); 212 return $hint; 213 }, $questiondata->hints); 214 // Compare hints. 215 $this->assertEquals($expectedhints, array_values($gothints)); 216 217 // Options. 218 $this->assertEquals(['answers', 'questions'], array_keys(get_object_vars($questiondata->options))); 219 $this->assertEquals(count($fromform->options->questions), count($questiondata->options->questions)); 220 221 // Option answers. 222 $this->assertEquals([], $questiondata->options->answers); 223 224 // Build the expected questions. We aren't going deeper to subquestion answers, options... that's another qtype job. 225 $expectedquestions = []; 226 foreach ($fromform->options->questions as $key => $value) { 227 $question = [ 228 'id' => $value->id, 229 'category' => $category->id, 230 'parent' => $questiondata->id, 231 'name' => $value->name, 232 'questiontext' => $value->questiontext, 233 'questiontextformat' => $value->questiontextformat, 234 'generalfeedback' => $value->generalfeedback, 235 'generalfeedbackformat' => $value->generalfeedbackformat, 236 'defaultmark' => (float) $value->defaultmark, 237 'penalty' => (float)$value->penalty, 238 'qtype' => $value->qtype, 239 'length' => $value->length, 240 'stamp' => $value->stamp, 241 'timecreated' => $value->timecreated, 242 'timemodified' => $value->timemodified, 243 'createdby' => $value->createdby, 244 'modifiedby' => $value->modifiedby, 245 ]; 246 $expectedquestions[] = (object)$question; 247 } 248 // Need to get rid of (version, idnumber, options, hints, maxmark). They are missing @ fromform. 249 $gotquestions = array_map(function($question) { 250 $question->id = (int) $question->id; 251 $question->category = (int) $question->category; 252 $question->defaultmark = (float) $question->defaultmark; 253 $question->penalty = (float) $question->penalty; 254 $question->length = (int) $question->length; 255 $question->timecreated = (int) $question->timecreated; 256 $question->timemodified = (int) $question->timemodified; 257 $question->createdby = (int) $question->createdby; 258 $question->modifiedby = (int) $question->modifiedby; 259 unset($question->idnumber); 260 unset($question->options); 261 unset($question->hints); 262 unset($question->maxmark); 263 return $question; 264 }, $questiondata->options->questions); 265 // Compare questions. 266 $this->assertEquals($expectedquestions, array_values($gotquestions)); 267 } 268 269 public function test_question_saving_twosubq() { 270 $this->resetAfterTest(true); 271 $this->setAdminUser(); 272 273 $questiondata = \test_question_maker::get_question_data('multianswer'); 274 275 $formdata = \test_question_maker::get_question_form_data('multianswer'); 276 277 $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); 278 $cat = $generator->create_question_category(array()); 279 280 $formdata->category = "{$cat->id},{$cat->contextid}"; 281 qtype_multianswer_edit_form::mock_submit((array)$formdata); 282 283 $form = \qtype_multianswer_test_helper::get_question_editing_form($cat, $questiondata); 284 285 $this->assertTrue($form->is_validated()); 286 287 $fromform = $form->get_data(); 288 // Create a new question version with the form submission. 289 unset($questiondata->id); 290 $returnedfromsave = $this->qtype->save_question($questiondata, $fromform); 291 $actualquestionsdata = question_load_questions(array($returnedfromsave->id)); 292 $actualquestiondata = end($actualquestionsdata); 293 294 foreach ($questiondata as $property => $value) { 295 if (!in_array($property, ['id', 'timemodified', 'timecreated', 'options', 'hints', 'stamp', 296 'idnumber', 'version', 'versionid', 'questionbankentryid', 'contextid', 'category', 'status'])) { 297 $this->assertEquals($value, $actualquestiondata->$property); 298 } 299 } 300 301 foreach ($questiondata->options as $optionname => $value) { 302 if ($optionname != 'questions') { 303 $this->assertEquals($value, $actualquestiondata->options->$optionname); 304 } 305 } 306 307 foreach ($questiondata->hints as $hint) { 308 $actualhint = array_shift($actualquestiondata->hints); 309 foreach ($hint as $property => $value) { 310 if (!in_array($property, array('id', 'questionid', 'options'))) { 311 $this->assertEquals($value, $actualhint->$property); 312 } 313 } 314 } 315 316 $this->assertObjectHasAttribute('questions', $actualquestiondata->options); 317 318 $subqpropstoignore = 319 ['id', 'category', 'parent', 'contextid', 'question', 'options', 'stamp', 'timemodified', 320 'timecreated', 'status', 'idnumber', 'version', 'versionid', 'questionbankentryid']; 321 foreach ($questiondata->options->questions as $subqno => $subq) { 322 $actualsubq = $actualquestiondata->options->questions[$subqno]; 323 foreach ($subq as $subqproperty => $subqvalue) { 324 if (!in_array($subqproperty, $subqpropstoignore)) { 325 $this->assertEquals($subqvalue, $actualsubq->$subqproperty); 326 } 327 } 328 foreach ($subq->options as $optionname => $value) { 329 if (!in_array($optionname, array('answers'))) { 330 $this->assertEquals($value, $actualsubq->options->$optionname); 331 } 332 } 333 foreach ($subq->options->answers as $answer) { 334 $actualanswer = array_shift($actualsubq->options->answers); 335 foreach ($answer as $ansproperty => $ansvalue) { 336 // These questions do not use 'answerformat', will ignore it. 337 if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) { 338 $this->assertEquals($ansvalue, $actualanswer->$ansproperty); 339 } 340 } 341 } 342 } 343 } 344 345 /** 346 * Verify that the multiplechoice variants parameters are correctly interpreted from 347 * the question text 348 */ 349 public function test_questiontext_extraction_of_multiplechoice_subquestions_variants() { 350 $questiontext = array(); 351 $questiontext['format'] = FORMAT_HTML; 352 $questiontext['itemid'] = ''; 353 $questiontext['text'] = '<p>Match the following cities with the correct state:</p> 354 <ul> 355 <li>1 San Francisco:{1:MULTICHOICE:=California#OK~%33.33333%Ohio#Not really~Arizona#Wrong}</li> 356 <li>2 Tucson:{1:MC:%0%California#Wrong~%33,33333%Ohio#Not really~=Arizona#OK}</li> 357 <li>3 Los Angeles:{1:MULTICHOICE_S:=California#OK~%33.33333%Ohio#Not really~Arizona#Wrong}</li> 358 <li>4 Phoenix:{1:MCS:%0%California#Wrong~%33,33333%Ohio#Not really~=Arizona#OK}</li> 359 <li>5 San Francisco:{1:MULTICHOICE_H:=California#OK~%33.33333%Ohio#Not really~Arizona#Wrong}</li> 360 <li>6 Tucson:{1:MCH:%0%California#Wrong~%33,33333%Ohio#Not really~=Arizona#OK}</li> 361 <li>7 Los Angeles:{1:MULTICHOICE_HS:=California#OK~%33.33333%Ohio#Not really~Arizona#Wrong}</li> 362 <li>8 Phoenix:{1:MCHS:%0%California#Wrong~%33,33333%Ohio#Not really~=Arizona#OK}</li> 363 <li>9 San Francisco:{1:MULTICHOICE_V:=California#OK~%33.33333%Ohio#Not really~Arizona#Wrong}</li> 364 <li>10 Tucson:{1:MCV:%0%California#Wrong~%33,33333%Ohio#Not really~=Arizona#OK}</li> 365 <li>11 Los Angeles:{1:MULTICHOICE_VS:=California#OK~%33.33333%Ohio#Not really~Arizona#Wrong}</li> 366 <li>12 Phoenix:{1:MCVS:%0%California#Wrong~%33,33333%Ohio#Not really~=Arizona#OK}</li> 367 </ul>'; 368 369 $q = qtype_multianswer_extract_question($questiontext); 370 foreach ($q->options->questions as $key => $sub) { 371 $this->assertSame($sub->qtype, 'multichoice'); 372 if ($key == 1 || $key == 2 || $key == 5 || $key == 6 || $key == 9 || $key == 10) { 373 $this->assertSame($sub->shuffleanswers, 0); 374 } else { 375 $this->assertSame($sub->shuffleanswers, 1); 376 } 377 if ($key == 1 || $key == 2 || $key == 3 || $key == 4) { 378 $this->assertSame($sub->layout, qtype_multichoice_base::LAYOUT_DROPDOWN); 379 } else if ($key == 5 || $key == 6 || $key == 7 || $key == 8) { 380 $this->assertSame($sub->layout, qtype_multichoice_base::LAYOUT_HORIZONTAL); 381 } else if ($key == 9 || $key == 10 || $key == 11 || $key == 12) { 382 $this->assertSame($sub->layout, qtype_multichoice_base::LAYOUT_VERTICAL); 383 } 384 foreach ($sub->feedback as $key => $feedback) { 385 if ($feedback['text'] === 'OK') { 386 $this->assertEquals(1, $sub->fraction[$key]); 387 } else if ($feedback['text'] === 'Wrong') { 388 $this->assertEquals(0, $sub->fraction[$key]); 389 } else { 390 $this->assertEquals('Not really', $feedback['text']); 391 $this->assertEquals(0.3333333, $sub->fraction[$key]); 392 } 393 } 394 } 395 } 396 397 /** 398 * Test get_question_options. 399 * 400 * @covers \qtype_multianswer::get_question_options 401 */ 402 public function test_get_question_options() { 403 global $DB; 404 405 $this->resetAfterTest(true); 406 407 $syscontext = \context_system::instance(); 408 $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); 409 $category = $generator->create_question_category(['contextid' => $syscontext->id]); 410 411 $fromform = test_question_maker::get_question_form_data('multianswer', 'twosubq'); 412 $fromform->category = $category->id . ',' . $syscontext->id; 413 414 $question = new \stdClass(); 415 $question->category = $category->id; 416 $question->qtype = 'multianswer'; 417 $question->createdby = 0; 418 419 $question = $this->qtype->save_question($question, $fromform); 420 $questiondata = question_bank::load_question_data($question->id); 421 422 $questiontodeletekey = array_keys($questiondata->options->questions)[0]; 423 $questiontodelete = $questiondata->options->questions[$questiontodeletekey]; 424 425 $this->assertCount(2, $questiondata->options->questions); 426 $this->assertEquals('shortanswer', $questiondata->options->questions[$questiontodeletekey]->qtype); 427 428 // Forcibly delete a subquestion to ensure get_question_options replaces it. 429 $DB->delete_records('question', ['id' => $questiontodelete->id]); 430 $this->qtype->get_question_options($questiondata); 431 432 $this->assertCount(2, $questiondata->options->questions); 433 $this->assertEquals('subquestion_replacement', $questiondata->options->questions[$questiontodeletekey]->qtype); 434 } 435 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body