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