MDL-61410 question: fix unit tests failures
[moodle.git] / question / type / ddwtos / tests / questiontype_test.php
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/>.
17 /**
18  * Unit tests for the drag-and-drop words into sentences question definition class.
19  *
20  * @package   qtype_ddwtos
21  * @copyright 2012 The Open University
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
26 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
29 require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
30 require_once($CFG->dirroot . '/question/type/ddwtos/tests/helper.php');
33 /**
34  * Unit tests for the drag-and-drop words into sentences question definition class.
35  *
36  * @copyright 2012 The Open University
37  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38  */
39 class qtype_ddwtos_test extends question_testcase {
40     /** @var qtype_ddwtos instance of the question type class to test. */
41     protected $qtype;
43     protected function setUp() {
44         $this->qtype = question_bank::get_qtype('ddwtos');;
45     }
47     protected function tearDown() {
48         $this->qtype = null;
49     }
51     public function assert_same_xml($expectedxml, $xml) {
52         $this->assertEquals(str_replace("\r\n", "\n", $expectedxml),
53                 str_replace("\r\n", "\n", $xml));
54     }
56     /**
57      * Get some test question data.
58      *
59      * @return object the data to construct a question like
60      * {@link qtype_ddwtos_test_helper::make_ddwtos_question_fox()}.
61      */
62     protected function get_test_question_data() {
63         global $USER;
65         $dd = new stdClass();
66         $dd->id = 0;
67         $dd->category = 0;
68         $dd->contextid = 0;
69         $dd->parent = 0;
70         $dd->questiontextformat = FORMAT_HTML;
71         $dd->generalfeedbackformat = FORMAT_HTML;
72         $dd->defaultmark = 1;
73         $dd->penalty = 0.3333333;
74         $dd->length = 1;
75         $dd->stamp = make_unique_id_code();
76         $dd->version = make_unique_id_code();
77         $dd->hidden = 0;
78         $dd->timecreated = time();
79         $dd->timemodified = time();
80         $dd->createdby = $USER->id;
81         $dd->modifiedby = $USER->id;
83         $dd->name = 'Drag-and-drop words into sentences question';
84         $dd->questiontext = 'The [[1]] brown [[2]] jumped over the [[3]] dog.';
85         $dd->generalfeedback = 'This sentence uses each letter of the alphabet.';
86         $dd->qtype = 'ddwtos';
88         $dd->options = new stdClass();
89         $dd->options->shuffleanswers = true;
91         test_question_maker::set_standard_combined_feedback_fields($dd->options);
93         $dd->options->answers = array(
94             (object) array('answer' => 'quick', 'feedback' =>
95                     'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";i:0;}'),
96             (object) array('answer' => 'fox', 'feedback' =>
97                     'O:8:"stdClass":2:{s:9:"draggroup";s:1:"2";s:8:"infinite";i:0;}'),
98             (object) array('answer' => 'lazy', 'feedback' =>
99                     'O:8:"stdClass":2:{s:9:"draggroup";s:1:"3";s:8:"infinite";i:0;}'),
100             (object) array('answer' => 'assiduous', 'feedback' =>
101                     'O:8:"stdClass":2:{s:9:"draggroup";s:1:"3";s:8:"infinite";i:0;}'),
102             (object) array('answer' => 'dog', 'feedback' =>
103                     'O:8:"stdClass":2:{s:9:"draggroup";s:1:"2";s:8:"infinite";i:0;}'),
104             (object) array('answer' => 'slow', 'feedback' =>
105                     'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";i:0;}'),
106         );
108         return $dd;
109     }
111     public function test_name() {
112         $this->assertEquals($this->qtype->name(), 'ddwtos');
113     }
115     public function test_can_analyse_responses() {
116         $this->assertTrue($this->qtype->can_analyse_responses());
117     }
119     public function test_initialise_question_instance() {
120         $qdata = $this->get_test_question_data();
122         $expected = test_question_maker::make_question('ddwtos');
123         $expected->stamp = $qdata->stamp;
124         $expected->version = $qdata->version;
126         $q = $this->qtype->make_question($qdata);
128         $this->assertEquals($expected, $q);
129     }
131     public function test_get_random_guess_score() {
132         $q = $this->get_test_question_data();
133         $this->assertEquals(0.5, $this->qtype->get_random_guess_score($q), '', 0.0000001);
134     }
136     public function test_get_possible_responses() {
137         $q = $this->get_test_question_data();
139         $this->assertEquals(array(
140             1 => array(
141                 1 => new question_possible_response('quick', 1 / 3),
142                 2 => new question_possible_response('slow', 0),
143                 null => question_possible_response::no_response()),
144             2 => array(
145                 1 => new question_possible_response('fox', 1 / 3),
146                 2 => new question_possible_response('dog', 0),
147                 null => question_possible_response::no_response()),
148             3 => array(
149                 1 => new question_possible_response('lazy', 1 / 3),
150                 2 => new question_possible_response('assiduous', 0),
151                 null => question_possible_response::no_response()),
152         ), $this->qtype->get_possible_responses($q));
153     }
155     public function test_xml_import() {
156         $xml = '  <question type="ddwtos">
157     <name>
158       <text>A drag-and-drop question</text>
159     </name>
160     <questiontext format="moodle_auto_format">
161       <text>Put these in order: [[1]], [[2]], [[3]].</text>
162     </questiontext>
163     <generalfeedback>
164       <text>The answer is Alpha, Beta, Gamma.</text>
165     </generalfeedback>
166     <defaultgrade>3</defaultgrade>
167     <penalty>0.3333333</penalty>
168     <hidden>0</hidden>
169     <shuffleanswers>1</shuffleanswers>
170     <correctfeedback>
171       <text><![CDATA[<p>Your answer is correct.</p>]]></text>
172     </correctfeedback>
173     <partiallycorrectfeedback>
174       <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
175     </partiallycorrectfeedback>
176     <incorrectfeedback>
177       <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
178     </incorrectfeedback>
179     <shownumcorrect/>
180     <dragbox>
181       <text>Alpha</text>
182       <group>1</group>
183     </dragbox>
184     <dragbox>
185       <text>Beta</text>
186       <group>1</group>
187     </dragbox>
188     <dragbox>
189       <text>Gamma</text>
190       <group>1</group>
191       <infinite/>
192     </dragbox>
193     <hint>
194       <text>Try again.</text>
195       <shownumcorrect />
196     </hint>
197     <hint>
198       <text>These are the first three letters of the Greek alphabet.</text>
199       <shownumcorrect />
200       <clearwrong />
201     </hint>
202   </question>';
203         $xmldata = xmlize($xml);
205         $importer = new qformat_xml();
206         $q = $importer->try_importing_using_qtypes(
207                 $xmldata['question'], null, null, 'ddwtos');
209         $expectedq = new stdClass();
210         $expectedq->qtype = 'ddwtos';
211         $expectedq->name = 'A drag-and-drop question';
212         $expectedq->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
213         $expectedq->questiontextformat = FORMAT_MOODLE;
214         $expectedq->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
215         $expectedq->defaultmark = 3;
216         $expectedq->length = 1;
217         $expectedq->penalty = 0.3333333;
219         $expectedq->shuffleanswers = 1;
220         $expectedq->correctfeedback = array('text' => '<p>Your answer is correct.</p>',
221                 'format' => FORMAT_MOODLE);
222         $expectedq->partiallycorrectfeedback = array(
223                 'text' => '<p>Your answer is partially correct.</p>',
224                 'format' => FORMAT_MOODLE);
225         $expectedq->shownumcorrect = true;
226         $expectedq->incorrectfeedback = array('text' => '<p>Your answer is incorrect.</p>',
227                 'format' => FORMAT_MOODLE);
229         $expectedq->choices = array(
230             array('answer' => 'Alpha', 'choicegroup' => 1, 'infinite' => false),
231             array('answer' => 'Beta', 'choicegroup' => 1, 'infinite' => false),
232             array('answer' => 'Gamma', 'choicegroup' => 1, 'infinite' => true),
233         );
235         $expectedq->hint = array(
236                 array('text' => 'Try again.', 'format' => FORMAT_MOODLE),
237                 array('text' => 'These are the first three letters of the Greek alphabet.',
238                         'format' => FORMAT_MOODLE));
239         $expectedq->hintshownumcorrect = array(true, true);
240         $expectedq->hintclearwrong = array(false, true);
242         $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
243         $this->assertEquals($expectedq->hint, $q->hint);
244     }
246     public function test_xml_import_legacy() {
247         $xml = '  <question type="ddwtos">
248     <name>
249       <text>QDandD1 Base definition</text>
250     </name>
251     <questiontext format="html">
252       <text>&lt;p&gt;Drag and drop the words from the list below to fill the blank spaces ' .
253             'and correctly complete the sentence.&lt;/p&gt; &lt;p&gt;At 25°C all aqueous basic ' .
254             'solutions have [[1]]&#160;ion concentrations less than [[8]]&lt;br /&gt;mol ' .
255             'litre&lt;sup&gt;-1&lt;/sup&gt; and pH values [[9]] than [[6]].&lt;/p&gt; ' .
256             '&lt;!--DONOTCLEAN--&gt;</text>
257     </questiontext>
258     <image></image>
259     <generalfeedback>
260       <text>&lt;p&gt;At 25 &amp;#xB0;C all aqueous basic solutions have hydrogen ion ' .
261             'concentrations less than 10&lt;sup&gt;&amp;#x2212;7&lt;/sup&gt; mol ' .
262             'litre&lt;sup&gt;&amp;#x2212;1&lt;/sup&gt; and pH values greater than 7.&lt;/p&gt; ' .
263             '&lt;p&gt;See Section 9 of S103 &lt;em class="italic"&gt;Discovering ' .
264             'Science&lt;/em&gt; Block 8.&lt;/p&gt;</text>
265     </generalfeedback>
266     <defaultgrade>1</defaultgrade>
267     <penalty>0.33</penalty>
268     <hidden>0</hidden>
269     <shuffleanswers>0</shuffleanswers>
270     <shuffleanswers>false</shuffleanswers>
271     <answer>
272       <correctanswer>1</correctanswer>
273       <text>hydrogen</text>
274       <feedback>
275         <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";i:0;}</text>
276       </feedback>
277     </answer>
278     <answer>
279       <correctanswer>0</correctanswer>
280       <text>positive</text>
281       <feedback>
282         <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";i:0;}</text>
283       </feedback>
284     </answer>
285     <answer>
286       <correctanswer>0</correctanswer>
287       <text>hydroxide</text>
288       <feedback>
289         <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";i:0;}</text>
290       </feedback>
291     </answer>
292     <answer>
293       <correctanswer>0</correctanswer>
294       <text>negative</text>
295       <feedback>
296         <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";i:0;}</text>
297       </feedback>
298     </answer>
299     <answer>
300       <correctanswer>0</correctanswer>
301       <text>10&lt;sup&gt;7&lt;/sup&gt;</text>
302       <feedback>
303         <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"2";s:8:"infinite";i:0;}</text>
304       </feedback>
305     </answer>
306     <answer>
307       <correctanswer>1</correctanswer>
308       <text>7</text>
309       <feedback>
310         <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"2";s:8:"infinite";i:0;}</text>
311       </feedback>
312     </answer>
313     <answer>
314       <correctanswer>0</correctanswer>
315       <text>1</text>
316       <feedback>
317         <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"2";s:8:"infinite";i:0;}</text>
318       </feedback>
319     </answer>
320     <answer>
321       <correctanswer>1</correctanswer>
322       <text>10&lt;sup&gt;-7&lt;/sup&gt;</text>
323       <feedback>
324         <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"2";s:8:"infinite";i:0;}</text>
325       </feedback>
326     </answer>
327     <answer>
328       <correctanswer>1</correctanswer>
329       <text>greater</text>
330       <feedback>
331         <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"3";s:8:"infinite";i:0;}</text>
332       </feedback>
333     </answer>
334     <answer>
335       <correctanswer>0</correctanswer>
336       <text>less</text>
337       <feedback>
338         <text>O:8:"stdClass":2:{s:9:"draggroup";s:1:"3";s:8:"infinite";i:0;}</text>
339       </feedback>
340     </answer>
341     <correctfeedback>
342       <text>Your answer is correct.</text>
343     </correctfeedback>
344     <correctresponsesfeedback>1</correctresponsesfeedback>
345     <partiallycorrectfeedback>
346       <text>Your answer is partially correct.</text>
347     </partiallycorrectfeedback>
348     <incorrectfeedback>
349       <text>Your answer is incorrect.</text>
350     </incorrectfeedback>
351     <unlimited>0</unlimited>
352     <penalty>0.33</penalty>
353     <hint>
354       <statenumberofcorrectresponses>1</statenumberofcorrectresponses>
355       <clearincorrectresponses>0</clearincorrectresponses>
356       <hintcontent>
357         <text>You may wish to read&#160;Section 9 of&#160;&lt;em ' .
358             'class="italic"&gt;Discovering Science&lt;/em&gt; Block 8.</text>
359       </hintcontent>
360     </hint>
361     <hint>
362       <statenumberofcorrectresponses>1</statenumberofcorrectresponses>
363       <clearincorrectresponses>1</clearincorrectresponses>
364       <hintcontent>
365         <text>Any incorrect choices&#160;will be removed before your final try.</text>
366       </hintcontent>
367     </hint>
368   </question>';
369         $xmldata = xmlize($xml);
371         $importer = new qformat_xml();
372         $q = $importer->try_importing_using_qtypes(
373                 $xmldata['question'], null, null, 'ddwtos');
375         $expectedq = new stdClass();
376         $expectedq->qtype = 'ddwtos';
377         $expectedq->name = 'QDandD1 Base definition';
378         $expectedq->questiontext = '<p>Drag and drop the words from the list below ' .
379                 'to fill the blank spaces and correctly complete the sentence.</p>' .
380                 '<p>At 25°C all aqueous basic solutions have [[1]] ion concentrations ' .
381                 'less than [[8]]<br />mol litre<sup>-1</sup> and pH values [[9]] than [[6]].</p>' .
382                 '<!--DONOTCLEAN-->';
383         $expectedq->questiontextformat = FORMAT_HTML;
384         $expectedq->generalfeedback = '<p>At 25 &#xB0;C all aqueous basic solutions ' .
385                 'have hydrogen ion concentrations less than 10<sup>&#x2212;7</sup> ' .
386                 'mol litre<sup>&#x2212;1</sup> and pH values greater than 7.</p><p>See ' .
387                 'Section 9 of S103 <em class="italic">Discovering Science</em> Block 8.</p>';
388         $expectedq->defaultmark = 1;
389         $expectedq->length = 1;
390         $expectedq->penalty = 0.3333333;
392         $expectedq->shuffleanswers = 0;
393         $expectedq->correctfeedback = array('text' => 'Your answer is correct.',
394                 'format' => FORMAT_HTML);
395         $expectedq->partiallycorrectfeedback = array(
396                 'text' => 'Your answer is partially correct.',
397                 'format' => FORMAT_HTML);
398         $expectedq->shownumcorrect = true;
399         $expectedq->incorrectfeedback = array('text' => 'Your answer is incorrect.',
400                 'format' => FORMAT_HTML);
402         $expectedq->choices = array(
403             array('answer' => array('text' => 'hydrogen',        'format' => FORMAT_PLAIN),
404                     'choicegroup' => 1, 'infinite' => false),
405             array('answer' => array('text' => 'positive',        'format' => FORMAT_PLAIN),
406                     'choicegroup' => 1, 'infinite' => false),
407             array('answer' => array('text' => 'hydroxide',       'format' => FORMAT_PLAIN),
408                     'choicegroup' => 1, 'infinite' => false),
409             array('answer' => array('text' => 'negative',        'format' => FORMAT_PLAIN),
410                     'choicegroup' => 1, 'infinite' => false),
411             array('answer' => array('text' => '10<sup>7</sup>',  'format' => FORMAT_PLAIN),
412                     'choicegroup' => 2, 'infinite' => false),
413             array('answer' => array('text' => '7',               'format' => FORMAT_PLAIN),
414                     'choicegroup' => 2, 'infinite' => false),
415             array('answer' => array('text' => '1',               'format' => FORMAT_PLAIN),
416                     'choicegroup' => 2, 'infinite' => false),
417             array('answer' => array('text' => '10<sup>-7</sup>', 'format' => FORMAT_PLAIN),
418                     'choicegroup' => 2, 'infinite' => false),
419             array('answer' => array('text' => 'greater',         'format' => FORMAT_PLAIN),
420                     'choicegroup' => 3, 'infinite' => false),
421             array('answer' => array('text' => 'less',            'format' => FORMAT_PLAIN),
422                     'choicegroup' => 3, 'infinite' => false),
423         );
425         $expectedq->hint = array(array('text' => 'You may wish to read Section 9 of ' .
426                 '<em class="italic">Discovering Science</em> Block 8.',
427                     'format' => FORMAT_HTML),
428                 array('text' => 'Any incorrect choices will be removed before your final try.',
429                     'format' => FORMAT_HTML),
430         );
431         $expectedq->hintshownumcorrect = array(true, true);
432         $expectedq->hintclearwrong = array(false, true);
434         $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
435         $this->assertEquals($expectedq->choices, $q->choices);
436         $this->assertEquals($expectedq->hint, $q->hint);
437     }
439     public function test_xml_export() {
440         $qdata = new stdClass();
441         $qdata->id = 123;
442         $qdata->contextid = \context_system::instance()->id;
443         $qdata->qtype = 'ddwtos';
444         $qdata->name = 'A drag-and-drop question';
445         $qdata->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
446         $qdata->questiontextformat = FORMAT_MOODLE;
447         $qdata->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
448         $qdata->generalfeedbackformat = FORMAT_MOODLE;
449         $qdata->defaultmark = 3;
450         $qdata->length = 1;
451         $qdata->penalty = 0.3333333;
452         $qdata->hidden = 0;
454         $qdata->options = new stdClass();
455         $qdata->options->shuffleanswers = 1;
456         $qdata->options->correctfeedback = '<p>Your answer is correct.</p>';
457         $qdata->options->correctfeedbackformat = FORMAT_MOODLE;
458         $qdata->options->partiallycorrectfeedback = '<p>Your answer is partially correct.</p>';
459         $qdata->options->partiallycorrectfeedbackformat = FORMAT_MOODLE;
460         $qdata->options->shownumcorrect = 1;
461         $qdata->options->incorrectfeedback = '<p>Your answer is incorrect.</p>';
462         $qdata->options->incorrectfeedbackformat = FORMAT_MOODLE;
464         $qdata->options->answers = array(
465             13 => new question_answer(13, 'Alpha', 0,
466                     'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";b:0;}',
467                     FORMAT_MOODLE),
468             14 => new question_answer(14, 'Beta', 0,
469                     'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";b:0;}',
470                     FORMAT_MOODLE),
471             15 => new question_answer(15, 'Gamma', 0,
472                     'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";b:1;}',
473                     FORMAT_MOODLE),
474         );
476         $qdata->hints = array(
477             1 => new question_hint_with_parts(1, 'Try again.', FORMAT_MOODLE, true, false),
478             2 => new question_hint_with_parts(2,
479                     'These are the first three letters of the Greek alphabet.',
480                     FORMAT_MOODLE, true, true),
481         );
483         $exporter = new qformat_xml();
484         $xml = $exporter->writequestion($qdata);
486         $expectedxml = '<!-- question: 123  -->
487   <question type="ddwtos">
488     <name>
489       <text>A drag-and-drop question</text>
490     </name>
491     <questiontext format="moodle_auto_format">
492       <text>Put these in order: [[1]], [[2]], [[3]].</text>
493     </questiontext>
494     <generalfeedback format="moodle_auto_format">
495       <text>The answer is Alpha, Beta, Gamma.</text>
496     </generalfeedback>
497     <defaultgrade>3</defaultgrade>
498     <penalty>0.3333333</penalty>
499     <hidden>0</hidden>
500     <shuffleanswers>1</shuffleanswers>
501     <correctfeedback format="moodle_auto_format">
502       <text><![CDATA[<p>Your answer is correct.</p>]]></text>
503     </correctfeedback>
504     <partiallycorrectfeedback format="moodle_auto_format">
505       <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
506     </partiallycorrectfeedback>
507     <incorrectfeedback format="moodle_auto_format">
508       <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
509     </incorrectfeedback>
510     <shownumcorrect/>
511     <dragbox>
512       <text>Alpha</text>
513       <group>1</group>
514     </dragbox>
515     <dragbox>
516       <text>Beta</text>
517       <group>1</group>
518     </dragbox>
519     <dragbox>
520       <text>Gamma</text>
521       <group>1</group>
522       <infinite/>
523     </dragbox>
524     <hint format="moodle_auto_format">
525       <text>Try again.</text>
526       <shownumcorrect/>
527     </hint>
528     <hint format="moodle_auto_format">
529       <text>These are the first three letters of the Greek alphabet.</text>
530       <shownumcorrect/>
531       <clearwrong/>
532     </hint>
533   </question>
534 ';
536         $this->assert_same_xml($expectedxml, $xml);
537     }