222b6b37660d0c643293d3be2733a5944970e7b5
[moodle.git] / question / format / blackboard / tests / blackboardformat_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 Moodle Blackboard format.
19  *
20  * @package    qformat_blackboard
21  * @copyright  2012 Jean-Michel Vedrine
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->libdir . '/questionlib.php');
30 require_once($CFG->dirroot . '/question/format.php');
31 require_once($CFG->dirroot . '/question/format/blackboard/format.php');
32 require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
35 /**
36  * Unit tests for the blackboard question import format.
37  *
38  * @copyright  2012 Jean-Michel Vedrine
39  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40  */
41 class qformat_blackboard_test extends question_testcase {
43     public function make_test_xml() {
44         $xml = "<?xml version='1.0' encoding='utf-8'?>
45 <POOL>
46     <TITLE value='exam 3 2008-9'/>
47     <QUESTIONLIST>
48         <QUESTION id='q1' class='QUESTION_TRUEFALSE' points='1'/>
49         <QUESTION id='q7' class='QUESTION_MULTIPLECHOICE' points='1'/>
50         <QUESTION id='q8' class='QUESTION_MULTIPLEANSWER' points='1'/>
51         <QUESTION id='q39-44' class='QUESTION_MATCH' points='2'/>
52         <QUESTION id='q9' class='QUESTION_ESSAY' points='1'/>
53         <QUESTION id='q27' class='QUESTION_FILLINBLANK' points='1'/>
54     </QUESTIONLIST>
55     <QUESTION_TRUEFALSE id='q1'>
56         <BODY>
57             <TEXT><![CDATA[<span style=\"font-size:12pt\">42 is the Absolute Answer to everything.</span>]]></TEXT>
58             <FLAGS>
59                 <ISHTML value='true'/>
60                 <ISNEWLINELITERAL value='false'/>
61             </FLAGS>
62         </BODY>
63         <ANSWER id='q1_a1'>
64             <TEXT>False</TEXT>
65         </ANSWER>
66         <ANSWER id='q1_a2'>
67             <TEXT>True</TEXT>
68         </ANSWER>
69         <GRADABLE>
70             <CORRECTANSWER answer_id='q1_a2'/>
71             <FEEDBACK_WHEN_CORRECT><![CDATA[You gave the right answer.]]></FEEDBACK_WHEN_CORRECT>
72             <FEEDBACK_WHEN_INCORRECT><![CDATA[42 is the Ultimate Answer.]]></FEEDBACK_WHEN_INCORRECT>
73         </GRADABLE>
74     </QUESTION_TRUEFALSE>
75     <QUESTION_MULTIPLECHOICE id='q7'>
76         <BODY>
77             <TEXT><![CDATA[<span style=\"font-size:12pt\">What's between orange and green in the spectrum?</span>]]></TEXT>
78             <FLAGS>
79                 <ISHTML value='true'/>
80                 <ISNEWLINELITERAL value='false'/>
81             </FLAGS>
82         </BODY>
83         <ANSWER id='q7_a1' position='1'>
84         <TEXT><![CDATA[<span style=\"font-size:12pt\">red</span>]]></TEXT>
85         </ANSWER>
86         <ANSWER id='q7_a2' position='2'>
87         <TEXT><![CDATA[<span style=\"font-size:12pt\">yellow</span>]]></TEXT>
88         </ANSWER>
89         <ANSWER id='q7_a3' position='3'>
90         <TEXT><![CDATA[<span style=\"font-size:12pt\">blue</span>]]></TEXT>
91         </ANSWER>
92         <GRADABLE>
93             <CORRECTANSWER answer_id='q7_a2'/>
94             <FEEDBACK_WHEN_CORRECT><![CDATA[You gave the right answer.]]></FEEDBACK_WHEN_CORRECT>
95             <FEEDBACK_WHEN_INCORRECT><![CDATA[Only yellow is between orange and green in the spectrum.]]></FEEDBACK_WHEN_INCORRECT>
96             </GRADABLE>
97     </QUESTION_MULTIPLECHOICE>
98     <QUESTION_MULTIPLEANSWER id='q8'>
99         <BODY>
100             <TEXT><![CDATA[<span style=\"font-size:12pt\">What's between orange and green in the spectrum?</span>]]></TEXT>
101             <FLAGS>
102                 <ISHTML value='true'/>
103                 <ISNEWLINELITERAL value='false'/>
104             </FLAGS>
105         </BODY>
106         <ANSWER id='q8_a1' position='1'>
107         <TEXT><![CDATA[<span style=\"font-size:12pt\">yellow</span>]]></TEXT>
108         </ANSWER>
109         <ANSWER id='q8_a2' position='2'>
110         <TEXT><![CDATA[<span style=\"font-size:12pt\">red</span>]]></TEXT>
111         </ANSWER>
112         <ANSWER id='q8_a3' position='3'>
113         <TEXT><![CDATA[<span style=\"font-size:12pt\">off-beige</span>]]></TEXT>
114         </ANSWER>
115         <ANSWER id='q8_a4' position='4'>
116         <TEXT><![CDATA[<span style=\"font-size:12pt\">blue</span>]]></TEXT>
117         </ANSWER>
118         <GRADABLE>
119             <CORRECTANSWER answer_id='q8_a1'/>
120             <CORRECTANSWER answer_id='q8_a3'/>
121             <FEEDBACK_WHEN_CORRECT><![CDATA[You gave the right answer.]]></FEEDBACK_WHEN_CORRECT>
122             <FEEDBACK_WHEN_INCORRECT>
123                 <![CDATA[Only yellow and off-beige are between orange and green in the spectrum.]]>
124             </FEEDBACK_WHEN_INCORRECT>
125         </GRADABLE>
126     </QUESTION_MULTIPLEANSWER>
127     <QUESTION_MATCH id='q39-44'>
128         <BODY>
129             <TEXT><![CDATA[<i>Classify the animals.</i>]]></TEXT>
130             <FLAGS>
131                 <ISHTML value='true'/>
132                 <ISNEWLINELITERAL value='false'/>
133             </FLAGS>
134         </BODY>
135         <ANSWER id='q39-44_a1' position='1'>
136             <TEXT><![CDATA[frog]]></TEXT>
137         </ANSWER>
138         <ANSWER id='q39-44_a2' position='2'>
139             <TEXT><![CDATA[cat]]></TEXT>
140         </ANSWER>
141         <ANSWER id='q39-44_a3' position='3'>
142             <TEXT><![CDATA[newt]]></TEXT>
143         </ANSWER>
144         <CHOICE id='q39-44_c1' position='1'>
145             <TEXT><![CDATA[mammal]]></TEXT>
146         </CHOICE>
147         <CHOICE id='q39-44_c2' position='2'>
148             <TEXT><![CDATA[insect]]></TEXT>
149         </CHOICE>
150         <CHOICE id='q39-44_c3' position='3'>
151             <TEXT><![CDATA[amphibian]]></TEXT>
152         </CHOICE>
153         <GRADABLE>
154             <CORRECTANSWER answer_id='q39-44_a1' choice_id='q39-44_c3'/>
155             <CORRECTANSWER answer_id='q39-44_a2' choice_id='q39-44_c1'/>
156             <CORRECTANSWER answer_id='q39-44_a3' choice_id='q39-44_c3'/>
157         </GRADABLE>
158     </QUESTION_MATCH>
159     <QUESTION_ESSAY id='q9'>
160         <BODY>
161             <TEXT><![CDATA[How are you?]]></TEXT>
162             <FLAGS>
163                 <ISHTML value='true'/>
164                 <ISNEWLINELITERAL value='false'/>
165             </FLAGS>
166         </BODY>
167         <ANSWER id='q9_a1'>
168             <TEXT><![CDATA[Blackboard answer for essay questions will be imported as informations for graders.]]></TEXT>
169         </ANSWER>
170         <GRADABLE>
171         </GRADABLE>
172     </QUESTION_ESSAY>
173     <QUESTION_FILLINBLANK id='q27'>
174         <BODY>
175             <TEXT><![CDATA[<span style=\"font-size:12pt\">Name an amphibian: __________.</span>]]></TEXT>
176             <FLAGS>
177                 <ISHTML value='true'/>
178                 <ISNEWLINELITERAL value='false'/>
179             </FLAGS>
180         </BODY>
181         <ANSWER id='q27_a1' position='1'>
182             <TEXT>frog</TEXT>
183         </ANSWER>
184         <GRADABLE>
185         </GRADABLE>
186     </QUESTION_FILLINBLANK></POOL>";
187         return $xml;
188     }
189     public function test_import_match() {
191         $xmldata = xmlize($this->make_test_xml());
192         $questions = array();
194         $importer = new qformat_blackboard();
195         $importer->process_matching($xmldata, $questions);
196         $q = $questions[0];
197         $expectedq = new stdClass();
198         $expectedq->qtype = 'match';
199         $expectedq->name = 'Classify the animals.';
200         $expectedq->questiontext = '<i>Classify the animals.</i>';
201         $expectedq->questiontextformat = FORMAT_HTML;
202         $expectedq->correctfeedback = array('text' => '',
203                 'format' => FORMAT_HTML, 'files' => array());
204         $expectedq->partiallycorrectfeedback = array('text' => '',
205                 'format' => FORMAT_HTML, 'files' => array());
206         $expectedq->incorrectfeedback = array('text' => '',
207                 'format' => FORMAT_HTML, 'files' => array());
208         $expectedq->generalfeedback = '';
209         $expectedq->generalfeedbackformat = FORMAT_HTML;
210         $expectedq->defaultmark = 1;
211         $expectedq->length = 1;
212         $expectedq->penalty = 0.3333333;
213         $expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
214         $expectedq->subquestions = array(
215             array('text' => 'cat', 'format' => FORMAT_HTML, 'files' => array()),
216             array('text' => '', 'format' => FORMAT_HTML, 'files' => array()),
217             array('text' => 'frog', 'format' => FORMAT_HTML, 'files' => array()),
218             array('text' => 'newt', 'format' => FORMAT_HTML, 'files' => array()));
219         $expectedq->subanswers = array('mammal', 'insect', 'amphibian', 'amphibian');
221         $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
222     }
224     public function test_import_multichoice_single() {
226         $xmldata = xmlize($this->make_test_xml());
227         $questions = array();
229         $importer = new qformat_blackboard();
230         $importer->process_mc($xmldata, $questions);
231         $q = $questions[0];
233         $expectedq = new stdClass();
234         $expectedq->qtype = 'multichoice';
235         $expectedq->single = 1;
236         $expectedq->name = 'What\'s between orange and green in the spectrum?';
237         $expectedq->questiontext = '<span style="font-size:12pt">What\'s between orange and green in the spectrum?</span>';
238         $expectedq->questiontextformat = FORMAT_HTML;
239         $expectedq->correctfeedback = array('text' => 'You gave the right answer.',
240                 'format' => FORMAT_HTML, 'files' => array());
241         $expectedq->partiallycorrectfeedback = array('text' => '',
242                 'format' => FORMAT_HTML, 'files' => array());
243         $expectedq->incorrectfeedback = array('text' => 'Only yellow is between orange and green in the spectrum.',
244                 'format' => FORMAT_HTML, 'files' => array());
245         $expectedq->generalfeedback = '';
246         $expectedq->generalfeedbackformat = FORMAT_HTML;
247         $expectedq->defaultmark = 1;
248         $expectedq->length = 1;
249         $expectedq->penalty = 0.3333333;
250         $expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
251         $expectedq->answer = array(
252                 0 => array(
253                     'text' => '<span style="font-size:12pt">red</span>',
254                     'format' => FORMAT_HTML,
255                     'files' => array(),
256                 ),
257                 1 => array(
258                     'text' => '<span style="font-size:12pt">yellow</span>',
259                     'format' => FORMAT_HTML,
260                     'files' => array(),
261                 ),
262                 2 => array(
263                     'text' => '<span style="font-size:12pt">blue</span>',
264                     'format' => FORMAT_HTML,
265                     'files' => array(),
266                 )
267             );
268         $expectedq->fraction = array(0, 1, 0);
269         $expectedq->feedback = array(
270                 0 => array(
271                     'text' => '',
272                     'format' => FORMAT_HTML,
273                     'files' => array(),
274                 ),
275                 1 => array(
276                     'text' => '',
277                     'format' => FORMAT_HTML,
278                     'files' => array(),
279                 ),
280                 2 => array(
281                     'text' => '',
282                     'format' => FORMAT_HTML,
283                     'files' => array(),
284                 )
285             );
287         $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
288     }
290     public function test_import_multichoice_multi() {
292         $xmldata = xmlize($this->make_test_xml());
293         $questions = array();
295         $importer = new qformat_blackboard();
296         $importer->process_ma($xmldata, $questions);
297         $q = $questions[0];
299         $expectedq = new stdClass();
300         $expectedq->qtype = 'multichoice';
301         $expectedq->single = 0;
302         $expectedq->name = 'What\'s between orange and green in the spectrum?';
303         $expectedq->questiontext = '<span style="font-size:12pt">What\'s between orange and green in the spectrum?</span>';
304         $expectedq->questiontextformat = FORMAT_HTML;
305         $expectedq->correctfeedback = array(
306                 'text' => 'You gave the right answer.',
307                 'format' => FORMAT_HTML,
308                 'files' => array());
309         $expectedq->partiallycorrectfeedback = array(
310                 'text' => 'Only yellow and off-beige are between orange and green in the spectrum.',
311                 'format' => FORMAT_HTML,
312                 'files' => array());
313         $expectedq->incorrectfeedback = array(
314                 'text' => 'Only yellow and off-beige are between orange and green in the spectrum.',
315                 'format' => FORMAT_HTML,
316                 'files' => array());
317         $expectedq->generalfeedback = '';
318         $expectedq->generalfeedbackformat = FORMAT_HTML;
319         $expectedq->defaultmark = 1;
320         $expectedq->length = 1;
321         $expectedq->penalty = 0.3333333;
322         $expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
323         $expectedq->answer = array(
324                 0 => array(
325                     'text' => '<span style="font-size:12pt">yellow</span>',
326                     'format' => FORMAT_HTML,
327                     'files' => array(),
328                 ),
329                 1 => array(
330                     'text' => '<span style="font-size:12pt">red</span>',
331                     'format' => FORMAT_HTML,
332                     'files' => array(),
333                 ),
334                 2 => array(
335                     'text' => '<span style="font-size:12pt">off-beige</span>',
336                     'format' => FORMAT_HTML,
337                     'files' => array(),
338                 ),
339                 3 => array(
340                     'text' => '<span style="font-size:12pt">blue</span>',
341                     'format' => FORMAT_HTML,
342                     'files' => array(),
343                 )
344             );
345         $expectedq->fraction = array(0.5, 0, 0.5, 0);
346         $expectedq->feedback = array(
347                 0 => array(
348                     'text' => '',
349                     'format' => FORMAT_HTML,
350                     'files' => array(),
351                 ),
352                 1 => array(
353                     'text' => '',
354                     'format' => FORMAT_HTML,
355                     'files' => array(),
356                 ),
357                 2 => array(
358                     'text' => '',
359                     'format' => FORMAT_HTML,
360                     'files' => array(),
361                 ),
362                 3 => array(
363                     'text' => '',
364                     'format' => FORMAT_HTML,
365                     'files' => array(),
366                 )
367             );
369         $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
370     }
372     public function test_import_truefalse() {
374         $xmldata = xmlize($this->make_test_xml());
375         $questions = array();
377         $importer = new qformat_blackboard();
378         $importer->process_tf($xmldata, $questions);
379         $q = $questions[0];
381         $expectedq = new stdClass();
382         $expectedq->qtype = 'truefalse';
383         $expectedq->name = '42 is the Absolute Answer to everything.';
384         $expectedq->questiontext = '<span style="font-size:12pt">42 is the Absolute Answer to everything.</span>';
385         $expectedq->questiontextformat = FORMAT_HTML;
386         $expectedq->generalfeedback = '';
387         $expectedq->generalfeedbackformat = FORMAT_HTML;
388         $expectedq->defaultmark = 1;
389         $expectedq->length = 1;
390         $expectedq->correctanswer = 0;
391         $expectedq->feedbacktrue = array(
392                 'text' => '42 is the Ultimate Answer.',
393                 'format' => FORMAT_HTML,
394                 'files' => array(),
395             );
396         $expectedq->feedbackfalse = array(
397                 'text' => 'You gave the right answer.',
398                 'format' => FORMAT_HTML,
399                 'files' => array(),
400             );
401         $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
402     }
404     public function test_import_fill_in_the_blank() {
406         $xmldata = xmlize($this->make_test_xml());
407         $questions = array();
409         $importer = new qformat_blackboard();
410         $importer->process_fib($xmldata, $questions);
411         $q = $questions[0];
413         $expectedq = new stdClass();
414         $expectedq->qtype = 'shortanswer';
415         $expectedq->name = 'Name an amphibian: __________.';
416         $expectedq->questiontext = '<span style="font-size:12pt">Name an amphibian: __________.</span>';
417         $expectedq->questiontextformat = FORMAT_HTML;
418         $expectedq->generalfeedback = '';
419         $expectedq->generalfeedbackformat = FORMAT_HTML;
420         $expectedq->defaultmark = 1;
421         $expectedq->length = 1;
422         $expectedq->usecase = 0;
423         $expectedq->answer = array('frog', '*');
424         $expectedq->fraction = array(1, 0);
425         $expectedq->feedback = array(
426                 0 => array(
427                     'text' => '',
428                     'format' => FORMAT_HTML,
429                     'files' => array(),
430                 ),
431                 1 => array(
432                     'text' => '',
433                     'format' => FORMAT_HTML,
434                     'files' => array(),
435                 )
436             );
438         $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
439     }
441     public function test_import_essay() {
443         $xmldata = xmlize($this->make_test_xml());
444         $questions = array();
446         $importer = new qformat_blackboard();
447         $importer->process_essay($xmldata, $questions);
448         $q = $questions[0];
450         $expectedq = new stdClass();
451         $expectedq->qtype = 'essay';
452         $expectedq->name = 'How are you?';
453         $expectedq->questiontext = 'How are you?';
454         $expectedq->questiontextformat = FORMAT_HTML;
455         $expectedq->generalfeedback = '';
456         $expectedq->generalfeedbackformat = FORMAT_HTML;
457         $expectedq->defaultmark = 1;
458         $expectedq->length = 1;
459         $expectedq->responseformat = 'editor';
460         $expectedq->responsefieldlines = 15;
461         $expectedq->attachments = 0;
462         $expectedq->graderinfo = array(
463                 'text' => 'Blackboard answer for essay questions will be imported as informations for graders.',
464                 'format' => FORMAT_HTML,
465                 'files' => array());
467         $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
468     }