MDL-47494 ddimageortext: work-in-progress converting the ddwtos and gapselect qtypes.
[moodle.git] / question / type / ddimageortext / simpletest / testquestiontype.php
CommitLineData
a2bb148a
TH
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18
19/**
20 * Unit tests for the drag-and-drop words into sentences question definition class.
21 *
22 * @package qtype_ddwtos
23 * @copyright 2010 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27
28require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php');
29require_once($CFG->dirroot . '/question/type/ddwtos/simpletest/helper.php');
30
31
32/**
33 * Unit tests for the drag-and-drop words into sentences question definition class.
34 *
35 * @copyright 2010 The Open University
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class qtype_ddwtos_test extends UnitTestCase {
39 /** @var qtype_ddwtos instance of the question type class to test. */
40 protected $qtype;
41
42 public function setUp() {
43 $this->qtype = question_bank::get_qtype('ddwtos');;
44 }
45
46 public function tearDown() {
47 $this->qtype = null;
48 }
49
50 public function assert_same_xml($expectedxml, $xml) {
51 $this->assertEqual(str_replace("\r\n", "\n", $expectedxml),
52 str_replace("\r\n", "\n", $xml));
53 }
54
55 /**
56 * @return object the data to construct a question like
57 * {@link qtype_ddwtos_test_helper::make_a_ddwtos_question()}.
58 */
59 protected function get_test_question_data() {
60 global $USER;
61
62 $dd = new stdClass;
63 $dd->id = 0;
64 $dd->category = 0;
65 $dd->parent = 0;
66 $dd->questiontextformat = FORMAT_HTML;
67 $dd->defaultmark = 1;
68 $dd->penalty = 0.3333333;
69 $dd->length = 1;
70 $dd->stamp = make_unique_id_code();
71 $dd->version = make_unique_id_code();
72 $dd->hidden = 0;
73 $dd->timecreated = time();
74 $dd->timemodified = time();
75 $dd->createdby = $USER->id;
76 $dd->modifiedby = $USER->id;
77
78 $dd->name = 'Drag-and-drop words into sentences question';
79 $dd->questiontext = 'The [[1]] brown [[2]] jumped over the [[3]] dog.';
80 $dd->generalfeedback = 'This sentence uses each letter of the alphabet.';
81 $dd->qtype = 'ddwtos';
82
83 $dd->options->shuffleanswers = true;
84
85 test_question_maker::set_standard_combined_feedback_fields($dd->options);
86
87 $dd->options->answers = array(
88 (object) array('answer' => 'quick', 'feedback' => 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";i:0;}'),
89 (object) array('answer' => 'fox', 'feedback' => 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"2";s:8:"infinite";i:0;}'),
90 (object) array('answer' => 'lazy', 'feedback' => 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"3";s:8:"infinite";i:0;}'),
91 (object) array('answer' => 'assiduous', 'feedback' => 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"3";s:8:"infinite";i:0;}'),
92 (object) array('answer' => 'dog', 'feedback' => 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"2";s:8:"infinite";i:0;}'),
93 (object) array('answer' => 'slow', 'feedback' => 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";i:0;}'),
94 );
95
96 return $dd;
97 }
98
99 public function test_name() {
100 $this->assertEqual($this->qtype->name(), 'ddwtos');
101 }
102
103 public function test_can_analyse_responses() {
104 $this->assertTrue($this->qtype->can_analyse_responses());
105 }
106
107 public function test_initialise_question_instance() {
108 $qdata = $this->get_test_question_data();
109
110 $expected = qtype_ddwtos_test_helper::make_a_ddwtos_question();
111 $expected->stamp = $qdata->stamp;
112 $expected->version = $qdata->version;
113
114 $q = $this->qtype->make_question($qdata);
115
116 $this->assertEqual($expected, $q);
117 }
118
119 public function test_get_random_guess_score() {
120 $q = $this->get_test_question_data();
121 $this->assertWithinMargin(0.5, $this->qtype->get_random_guess_score($q), 0.0000001);
122 }
123
124 public function test_get_possible_responses() {
125 $q = $this->get_test_question_data();
126
127 $this->assertEqual(array(
128 1 => array(
129 1 => new question_possible_response('quick', 1),
130 2 => new question_possible_response('slow', 0),
131 null => question_possible_response::no_response()),
132 2 => array(
133 1 => new question_possible_response('fox', 1),
134 2 => new question_possible_response('dog', 0),
135 null => question_possible_response::no_response()),
136 3 => array(
137 1 => new question_possible_response('lazy', 1),
138 2 => new question_possible_response('assiduous', 0),
139 null => question_possible_response::no_response()),
140 ), $this->qtype->get_possible_responses($q));
141 }
142
143 public function test_xml_import() {
144 $xml = ' <question type="ddwtos">
145 <name>
146 <text>A drag-and-drop question</text>
147 </name>
148 <questiontext format="moodle_auto_format">
149 <text>Put these in order: [[1]], [[2]], [[3]].</text>
150 </questiontext>
151 <generalfeedback>
152 <text>The answer is Alpha, Beta, Gamma.</text>
153 </generalfeedback>
154 <defaultgrade>3</defaultgrade>
155 <penalty>0.3333333</penalty>
156 <hidden>0</hidden>
157 <shuffleanswers>1</shuffleanswers>
158 <correctfeedback>
159 <text><![CDATA[<p>Your answer is correct.</p>]]></text>
160 </correctfeedback>
161 <partiallycorrectfeedback>
162 <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
163 </partiallycorrectfeedback>
164 <incorrectfeedback>
165 <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
166 </incorrectfeedback>
167 <shownumcorrect/>
168 <dragbox>
169 <text>Alpha</text>
170 <group>1</group>
171 </dragbox>
172 <dragbox>
173 <text>Beta</text>
174 <group>1</group>
175 </dragbox>
176 <dragbox>
177 <text>Gamma</text>
178 <group>1</group>
179 <infinite/>
180 </dragbox>
181 <hint>
182 <text>Try again.</text>
183 <shownumcorrect />
184 </hint>
185 <hint>
186 <text>These are the first three letters of the Greek alphabet.</text>
187 <shownumcorrect />
188 <clearwrong />
189 </hint>
190 </question>';
191 $xmldata = xmlize($xml);
192
193 $importer = new qformat_xml();
194 $q = $importer->try_importing_using_qtypes(
195 $xmldata['question'], null, null, 'ddwtos');
196
197 $expectedq = new stdClass;
198 $expectedq->qtype = 'ddwtos';
199 $expectedq->name = 'A drag-and-drop question';
200 $expectedq->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
201 $expectedq->questiontextformat = FORMAT_MOODLE;
202 $expectedq->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
203 $expectedq->defaultmark = 3;
204 $expectedq->length = 1;
205 $expectedq->penalty = 0.3333333;
206
207 $expectedq->shuffleanswers = 1;
208 $expectedq->correctfeedback = '<p>Your answer is correct.</p>';
209 $expectedq->partiallycorrectfeedback = '<p>Your answer is partially correct.</p>';
210 $expectedq->shownumcorrect = true;
211 $expectedq->incorrectfeedback = '<p>Your answer is incorrect.</p>';
212
213 $expectedq->choices = array(
214 array('answer' => 'Alpha', 'draggroup' => 1, 'infinite' => false),
215 array('answer' => 'Beta', 'draggroup' => 1, 'infinite' => false),
216 array('answer' => 'Gamma', 'draggroup' => 1, 'infinite' => true),
217 );
218
219 $expectedq->hint = array('Try again.', 'These are the first three letters of the Greek alphabet.');
220 $expectedq->hintshownumcorrect = array(true, true);
221 $expectedq->hintclearwrong = array(false, true);
222
223 $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
224 }
225
226 public function test_xml_import_legacy() {
227 $xml = ' <question type="ddwtos">
228 <name>
229 <text>QDandD1 Base definition</text>
230 </name>
231 <questiontext format="html">
232 <text>&lt;p&gt;Drag and drop the words from the list below to fill the blank spaces and correctly complete the sentence.&lt;/p&gt; &lt;p&gt;At 25°C all aqueous basic solutions have [[1]]&#160;ion concentrations less than [[8]]&lt;br /&gt;mol litre&lt;sup&gt;-1&lt;/sup&gt; and pH values [[9]] than [[6]].&lt;/p&gt; &lt;!--DONOTCLEAN--&gt;</text>
233 </questiontext>
234 <image></image>
235 <generalfeedback>
236 <text>&lt;p&gt;At 25 &amp;#xB0;C all aqueous basic solutions have hydrogen ion concentrations less than 10&lt;sup&gt;&amp;#x2212;7&lt;/sup&gt; mol litre&lt;sup&gt;&amp;#x2212;1&lt;/sup&gt; and pH values greater than 7.&lt;/p&gt; &lt;p&gt;See Section 9 of S103 &lt;em class="italic"&gt;Discovering Science&lt;/em&gt; Block 8.&lt;/p&gt;</text>
237 </generalfeedback>
238 <defaultgrade>1</defaultgrade>
239 <penalty>0.33</penalty>
240 <hidden>0</hidden>
241 <shuffleanswers>0</shuffleanswers>
242 <shuffleanswers>false</shuffleanswers>
243 <answer>
244 <correctanswer>1</correctanswer>
245 <text>hydrogen</text>
246 <feedback>
247 <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";i:0;}</text>
248 </feedback>
249 </answer>
250 <answer>
251 <correctanswer>0</correctanswer>
252 <text>positive</text>
253 <feedback>
254 <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";i:0;}</text>
255 </feedback>
256 </answer>
257 <answer>
258 <correctanswer>0</correctanswer>
259 <text>hydroxide</text>
260 <feedback>
261 <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";i:0;}</text>
262 </feedback>
263 </answer>
264 <answer>
265 <correctanswer>0</correctanswer>
266 <text>negative</text>
267 <feedback>
268 <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";i:0;}</text>
269 </feedback>
270 </answer>
271 <answer>
272 <correctanswer>0</correctanswer>
273 <text>10&lt;sup&gt;7&lt;/sup&gt;</text>
274 <feedback>
275 <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"2";s:8:"infinite";i:0;}</text>
276 </feedback>
277 </answer>
278 <answer>
279 <correctanswer>1</correctanswer>
280 <text>7</text>
281 <feedback>
282 <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"2";s:8:"infinite";i:0;}</text>
283 </feedback>
284 </answer>
285 <answer>
286 <correctanswer>0</correctanswer>
287 <text>1</text>
288 <feedback>
289 <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"2";s:8:"infinite";i:0;}</text>
290 </feedback>
291 </answer>
292 <answer>
293 <correctanswer>1</correctanswer>
294 <text>10&lt;sup&gt;-7&lt;/sup&gt;</text>
295 <feedback>
296 <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"2";s:8:"infinite";i:0;}</text>
297 </feedback>
298 </answer>
299 <answer>
300 <correctanswer>1</correctanswer>
301 <text>greater</text>
302 <feedback>
303 <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"3";s:8:"infinite";i:0;}</text>
304 </feedback>
305 </answer>
306 <answer>
307 <correctanswer>0</correctanswer>
308 <text>less</text>
309 <feedback>
310 <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"3";s:8:"infinite";i:0;}</text>
311 </feedback>
312 </answer>
313 <correctfeedback>
314 <text>Your answer is correct.</text>
315 </correctfeedback>
316 <correctresponsesfeedback>1</correctresponsesfeedback>
317 <partiallycorrectfeedback>
318 <text>Your answer is partially correct.</text>
319 </partiallycorrectfeedback>
320 <incorrectfeedback>
321 <text>Your answer is incorrect.</text>
322 </incorrectfeedback>
323 <unlimited>0</unlimited>
324 <penalty>0.33</penalty>
325 <hint>
326 <statenumberofcorrectresponses>1</statenumberofcorrectresponses>
327 <clearincorrectresponses>0</clearincorrectresponses>
328 <hintcontent>
329 <text>You may wish to read&#160;Section 9 of&#160;&lt;em class="italic"&gt;Discovering Science&lt;/em&gt; Block 8.</text>
330 </hintcontent>
331 </hint>
332 <hint>
333 <statenumberofcorrectresponses>1</statenumberofcorrectresponses>
334 <clearincorrectresponses>1</clearincorrectresponses>
335 <hintcontent>
336 <text>Any incorrect choices&#160;will be removed before your final try.</text>
337 </hintcontent>
338 </hint>
339 </question>';
340 $xmldata = xmlize($xml);
341
342 $importer = new qformat_xml();
343 $q = $importer->try_importing_using_qtypes(
344 $xmldata['question'], null, null, 'ddwtos');
345
346 $expectedq = new stdClass;
347 $expectedq->qtype = 'ddwtos';
348 $expectedq->name = 'QDandD1 Base definition';
349 $expectedq->questiontext = '<p>Drag and drop the words from the list below to fill the blank spaces and correctly complete the sentence.</p><p>At 25°C all aqueous basic solutions have [[1]] ion concentrations less than [[8]]<br />mol litre<sup>-1</sup> and pH values [[9]] than [[6]].</p><!--DONOTCLEAN-->';
350 $expectedq->questiontextformat = FORMAT_HTML;
351 $expectedq->generalfeedback = '<p>At 25 &#xB0;C all aqueous basic solutions have hydrogen ion concentrations less than 10<sup>&#x2212;7</sup> mol litre<sup>&#x2212;1</sup> and pH values greater than 7.</p><p>See Section 9 of S103 <em class=\"italic\">Discovering Science</em> Block 8.</p>';
352 $expectedq->defaultmark = 1;
353 $expectedq->length = 1;
354 $expectedq->penalty = 0.3333333;
355
356 $expectedq->shuffleanswers = 0;
357 $expectedq->correctfeedback = 'Your answer is correct.';
358 $expectedq->partiallycorrectfeedback = 'Your answer is partially correct.';
359 $expectedq->shownumcorrect = true;
360 $expectedq->incorrectfeedback = 'Your answer is incorrect.';
361
362 $expectedq->choices = array(
363 array('answer' => 'hydrogen', 'draggroup' => 1, 'infinite' => false),
364 array('answer' => 'positive', 'draggroup' => 1, 'infinite' => false),
365 array('answer' => 'hydroxide', 'draggroup' => 1, 'infinite' => false),
366 array('answer' => 'negative', 'draggroup' => 1, 'infinite' => false),
367 array('answer' => '10<sup>7</sup>', 'draggroup' => 2, 'infinite' => false),
368 array('answer' => '7', 'draggroup' => 2, 'infinite' => false),
369 array('answer' => '1', 'draggroup' => 2, 'infinite' => false),
370 array('answer' => '10<sup>-7</sup>', 'draggroup' => 2, 'infinite' => false),
371 array('answer' => 'greater', 'draggroup' => 3, 'infinite' => false),
372 array('answer' => 'less', 'draggroup' => 3, 'infinite' => false),
373 );
374
375 $expectedq->hint = array(
376 'You may wish to read Section 9 of <em class=\"italic\">Discovering Science</em> Block 8.',
377 'Any incorrect choices will be removed before your final try.'
378 );
379 $expectedq->hintshownumcorrect = array(true, true);
380 $expectedq->hintclearwrong = array(false, true);
381
382 $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
383 }
384
385 public function test_xml_export() {
386 $qdata = new stdClass;
387 $qdata->id = 123;
388 $qdata->qtype = 'ddwtos';
389 $qdata->name = 'A drag-and-drop question';
390 $qdata->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
391 $qdata->questiontextformat = FORMAT_MOODLE;
392 $qdata->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
393 $qdata->defaultmark = 3;
394 $qdata->length = 1;
395 $qdata->penalty = 0.3333333;
396 $qdata->hidden = 0;
397
398 $qdata->options->shuffleanswers = 1;
399 $qdata->options->correctfeedback = '<p>Your answer is correct.</p>';
400 $qdata->options->partiallycorrectfeedback = '<p>Your answer is partially correct.</p>';
401 $qdata->options->shownumcorrect = true;
402 $qdata->options->incorrectfeedback = '<p>Your answer is incorrect.</p>';
403
404 $qdata->options->answers = array(
405 new question_answer('Alpha', 0, 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";b:0;}'),
406 new question_answer('Beta', 0, 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";b:0;}'),
407 new question_answer('Gamma', 0, 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";b:1;}'),
408 );
409
410 $qdata->hints = array(
411 new question_hint_with_parts('Try again.', true, false),
412 new question_hint_with_parts('These are the first three letters of the Greek alphabet.', true, true),
413 );
414
415 $exporter = new qformat_xml();
416 $xml = $exporter->writequestion($qdata);
417
418 $expectedxml = '<!-- question: 123 -->
419 <question type="ddwtos">
420 <name>
421 <text>A drag-and-drop question</text>
422 </name>
423 <questiontext format="moodle_auto_format">
424 <text>Put these in order: [[1]], [[2]], [[3]].</text>
425 </questiontext>
426 <generalfeedback>
427 <text>The answer is Alpha, Beta, Gamma.</text>
428 </generalfeedback>
429 <defaultgrade>3</defaultgrade>
430 <penalty>0.3333333</penalty>
431 <hidden>0</hidden>
432 <shuffleanswers>1</shuffleanswers>
433 <correctfeedback>
434 <text><![CDATA[<p>Your answer is correct.</p>]]></text>
435 </correctfeedback>
436 <partiallycorrectfeedback>
437 <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
438 </partiallycorrectfeedback>
439 <incorrectfeedback>
440 <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
441 </incorrectfeedback>
442 <shownumcorrect/>
443 <dragbox>
444 <text>Alpha</text>
445 <group>1</group>
446 </dragbox>
447 <dragbox>
448 <text>Beta</text>
449 <group>1</group>
450 </dragbox>
451 <dragbox>
452 <text>Gamma</text>
453 <group>1</group>
454 <infinite/>
455 </dragbox>
456 <hint>
457 <text>Try again.</text>
458 <shownumcorrect/>
459 </hint>
460 <hint>
461 <text>These are the first three letters of the Greek alphabet.</text>
462 <shownumcorrect/>
463 <clearwrong/>
464 </hint>
465 </question>
466';
467
468 $this->assert_same_xml($expectedxml, $xml);
469 }
470}