MDL-66259 quiz responses: fix unrealistic test setup
[moodle.git] / question / type / multianswer / tests / questiontype_test.php
CommitLineData
603bd001
PS
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/**
18 * Unit tests for the multianswer question definition class.
19 *
20 * @package qtype
21 * @subpackage multianswer
22 * @copyright 2011 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
31require_once($CFG->dirroot . '/question/type/multianswer/questiontype.php');
01c898ec
JP
32require_once($CFG->dirroot . '/question/type/edit_question_form.php');
33require_once($CFG->dirroot . '/question/type/multianswer/edit_multianswer_form.php');
603bd001
PS
34
35
36/**
37 * Unit tests for the multianswer question definition class.
38 *
39 * @copyright 2011 The Open University
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class qtype_multianswer_test extends advanced_testcase {
43 /** @var qtype_multianswer instance of the question type class to test. */
44 protected $qtype;
45
46 protected function setUp() {
47 $this->qtype = new qtype_multianswer();
48 }
49
50 protected function tearDown() {
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->length = 1;
71 $q->stamp = make_unique_id_code();
72 $q->version = make_unique_id_code();
73 $q->hidden = 0;
74 $q->timecreated = time();
75 $q->timemodified = time();
76 $q->createdby = $USER->id;
77 $q->modifiedby = $USER->id;
78
79 $sadata = new stdClass();
80 $sadata->id = 1;
81 $sadata->qtype = 'shortanswer';
82 $sadata->defaultmark = 1;
83 $sadata->options->usecase = true;
84 $sadata->options->answers[1] = (object) array('answer' => 'Bow-wow', 'fraction' => 0);
85 $sadata->options->answers[2] = (object) array('answer' => 'Wiggly worm', 'fraction' => 0);
86 $sadata->options->answers[3] = (object) array('answer' => 'Pussy-cat', 'fraction' => 1);
87
88 $mcdata = new stdClass();
89 $mcdata->id = 1;
90 $mcdata->qtype = 'multichoice';
91 $mcdata->defaultmark = 1;
92 $mcdata->options->single = true;
93 $mcdata->options->answers[1] = (object) array('answer' => 'Dog', 'fraction' => 0);
94 $mcdata->options->answers[2] = (object) array('answer' => 'Owl', 'fraction' => 1);
95 $mcdata->options->answers[3] = (object) array('answer' => '*', 'fraction' => 0);
96
97 $q->options->questions = array(
98 1 => $sadata,
99 2 => $mcdata,
100 );
101
102 return $q;
103 }
104
105 public function test_name() {
106 $this->assertEquals($this->qtype->name(), 'multianswer');
107 }
108
109 public function test_can_analyse_responses() {
110 $this->assertFalse($this->qtype->can_analyse_responses());
111 }
112
113 public function test_get_random_guess_score() {
114 $q = test_question_maker::get_question_data('multianswer', 'twosubq');
115 $this->assertEquals(0.1666667, $this->qtype->get_random_guess_score($q), '', 0.0000001);
116 }
01c898ec 117
04472779
EL
118 public function test_load_question() {
119 $this->resetAfterTest();
120
121 $syscontext = context_system::instance();
122 /** @var core_question_generator $generator */
123 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
124 $category = $generator->create_question_category(['contextid' => $syscontext->id]);
125
126 $fromform = test_question_maker::get_question_form_data('multianswer');
127 $fromform->category = $category->id . ',' . $syscontext->id;
128
129 $question = new stdClass();
130 $question->category = $category->id;
131 $question->qtype = 'multianswer';
132 $question->createdby = 0;
133
134 // TODO: Just check why is not consistent with others, $question should came back modified.
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(['questions'], array_keys(get_object_vars($questiondata->options)));
185 $this->assertEquals(count($fromform->options->questions), count($questiondata->options->questions));
186
187 // Build the expected questions. We aren't going deeper to subquestion answers, options... that's another qtype job.
188 $expectedquestions = [];
189 foreach ($fromform->options->questions as $key => $value) {
190 $question = [
191 'id' => $value->id,
192 'category' => $category->id,
193 'parent' => $questiondata->id,
194 'name' => $value->name,
195 'questiontext' => $value->questiontext,
196 'questiontextformat' => $value->questiontextformat,
197 'generalfeedback' => $value->generalfeedback,
198 'generalfeedbackformat' => $value->generalfeedbackformat,
199 'defaultmark' => (float) $value->defaultmark,
200 'penalty' => (float)$value->penalty,
201 'qtype' => $value->qtype,
202 'length' => $value->length,
203 'stamp' => $value->stamp,
204 'hidden' => 0,
205 'timecreated' => $value->timecreated,
206 'timemodified' => $value->timemodified,
207 'createdby' => $value->createdby,
208 'modifiedby' => $value->modifiedby,
209 ];
210 $expectedquestions[] = (object)$question;
211 }
212 // Need to get rid of (version, idnumber, options, hints, maxmark). They are missing @ fromform.
213 $gotquestions = array_map(function($question) {
214 unset($question->version);
215 unset($question->idnumber);
216 unset($question->options);
217 unset($question->hints);
218 unset($question->maxmark);
219 return $question;
220 }, $questiondata->options->questions);
221 // Compare questions.
222 $this->assertEquals($expectedquestions, array_values($gotquestions));
223 }
224
01c898ec
JP
225 public function test_question_saving_twosubq() {
226 $this->resetAfterTest(true);
227 $this->setAdminUser();
228
229 $questiondata = test_question_maker::get_question_data('multianswer');
230 $formdata = test_question_maker::get_question_form_data('multianswer');
231
232 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
233 $cat = $generator->create_question_category(array());
234
235 $formdata->category = "{$cat->id},{$cat->contextid}";
236 qtype_multianswer_edit_form::mock_submit((array)$formdata);
237
238 $form = qtype_multianswer_test_helper::get_question_editing_form($cat, $questiondata);
239
240 $this->assertTrue($form->is_validated());
241
242 $fromform = $form->get_data();
243
244 $returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
245 $actualquestionsdata = question_load_questions(array($returnedfromsave->id));
246 $actualquestiondata = end($actualquestionsdata);
247
248 foreach ($questiondata as $property => $value) {
249 if (!in_array($property, array('id', 'version', 'timemodified', 'timecreated', 'options', 'hints', 'stamp'))) {
250 $this->assertAttributeEquals($value, $property, $actualquestiondata);
251 }
252 }
253
254 foreach ($questiondata->options as $optionname => $value) {
255 if ($optionname != 'questions') {
256 $this->assertAttributeEquals($value, $optionname, $actualquestiondata->options);
257 }
258 }
259
260 foreach ($questiondata->hints as $hint) {
261 $actualhint = array_shift($actualquestiondata->hints);
262 foreach ($hint as $property => $value) {
263 if (!in_array($property, array('id', 'questionid', 'options'))) {
264 $this->assertAttributeEquals($value, $property, $actualhint);
265 }
266 }
267 }
268
269 $this->assertObjectHasAttribute('questions', $actualquestiondata->options);
270
271 $subqpropstoignore =
272 array('id', 'category', 'parent', 'contextid', 'question', 'options', 'stamp', 'version', 'timemodified',
273 'timecreated');
274 foreach ($questiondata->options->questions as $subqno => $subq) {
275 $actualsubq = $actualquestiondata->options->questions[$subqno];
276 foreach ($subq as $subqproperty => $subqvalue) {
277 if (!in_array($subqproperty, $subqpropstoignore)) {
278 $this->assertAttributeEquals($subqvalue, $subqproperty, $actualsubq);
279 }
280 }
281 foreach ($subq->options as $optionname => $value) {
282 if (!in_array($optionname, array('answers'))) {
283 $this->assertAttributeEquals($value, $optionname, $actualsubq->options);
284 }
285 }
286 foreach ($subq->options->answers as $answer) {
287 $actualanswer = array_shift($actualsubq->options->answers);
288 foreach ($answer as $ansproperty => $ansvalue) {
289 // These questions do not use 'answerformat', will ignore it.
290 if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) {
291 $this->assertAttributeEquals($ansvalue, $ansproperty, $actualanswer);
292 }
293 }
294 }
295 }
296 }
bd156853
PP
297 /**
298 * Verify that the multiplechoice variants parameters are correctly interpreted from
299 * the question text
300 *
301 *
302 */
303 public function test_questiontext_extraction_of_multiplechoice_subquestions_variants() {
304 $questiontext = array();
305 $questiontext['format'] = FORMAT_HTML;
306 $questiontext['itemid'] = '';
307 $questiontext['text'] = '<p>Match the following cities with the correct state:</p>
308 <ul>
309 <li>1 San Francisco:{1:MULTICHOICE:=California#OK~Arizona#Wrong}</li>
310 <li>2 Tucson:{1:MC:%0%California#Wrong~=Arizona#OK}</li>
311 <li>3 Los Angeles:{1:MULTICHOICE_S:=California#OK~Arizona#Wrong}</li>
312 <li>4 Phoenix:{1:MCS:%0%California#Wrong~=Arizona#OK}</li>
313 <li>5 San Francisco:{1:MULTICHOICE_H:=California#OK~Arizona#Wrong}</li>
314 <li>6 Tucson:{1:MCH:%0%California#Wrong~=Arizona#OK}</li>
315 <li>7 Los Angeles:{1:MULTICHOICE_HS:=California#OK~Arizona#Wrong}</li>
316 <li>8 Phoenix:{1:MCHS:%0%California#Wrong~=Arizona#OK}</li>
317 <li>9 San Francisco:{1:MULTICHOICE_V:=California#OK~Arizona#Wrong}</li>
318 <li>10 Tucson:{1:MCV:%0%California#Wrong~=Arizona#OK}</li>
319 <li>11 Los Angeles:{1:MULTICHOICE_VS:=California#OK~Arizona#Wrong}</li>
320 <li>12 Phoenix:{1:MCVS:%0%California#Wrong~=Arizona#OK}</li>
321 </ul>';
322
323 $q = qtype_multianswer_extract_question($questiontext);
324 foreach ($q->options->questions as $key => $sub) {
325 $this->assertSame($sub->qtype, 'multichoice');
326 if ($key == 1 || $key == 2 || $key == 5 || $key == 6 || $key == 9 || $key == 10) {
327 $this->assertSame($sub->shuffleanswers, 0);
328 } else {
329 $this->assertSame($sub->shuffleanswers, 1);
330 }
331 if ($key == 1 || $key == 2 || $key == 3 || $key == 4) {
332 $this->assertSame($sub->layout, qtype_multichoice_base::LAYOUT_DROPDOWN);
333 } else if ($key == 5 || $key == 6 || $key == 7 || $key == 8) {
334 $this->assertSame($sub->layout, qtype_multichoice_base::LAYOUT_HORIZONTAL);
335 } else if ($key == 9 || $key == 10 || $key == 11 || $key == 12) {
336 $this->assertSame($sub->layout, qtype_multichoice_base::LAYOUT_VERTICAL);
337 }
338 }
339 }
603bd001 340}