MDL-34171 qformat_gift: Fix edge case with special character escaping.
[moodle.git] / question / format / gift / tests / giftformat_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 Moodle GIFT format.
19 *
20 * @package qformat
21 * @subpackage gift
22 * @copyright 2010 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->libdir . '/questionlib.php');
31require_once($CFG->dirroot . '/question/format.php');
32require_once($CFG->dirroot . '/question/format/gift/format.php');
33require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
34
35
36/**
37 * Unit tests for the GIFT import/export format.
38 *
39 * @copyright 2010 The Open University
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class qformat_gift_test extends question_testcase {
43 public function assert_same_gift($expectedtext, $text) {
44 $this->assertEquals(str_replace("\r\n", "\n", $expectedtext),
45 str_replace("\r\n", "\n", $text));
46 }
47
48 public function test_import_essay() {
49 $gift = '
50// essay
51::Q8:: How are you? {}';
52 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
53
54 $importer = new qformat_gift();
55 $q = $importer->readquestion($lines);
56
57 $expectedq = (object) array(
58 'name' => 'Q8',
59 'questiontext' => 'How are you?',
60 'questiontextformat' => FORMAT_MOODLE,
61 'generalfeedback' => '',
62 'generalfeedbackformat' => FORMAT_MOODLE,
63 'qtype' => 'essay',
64 'defaultmark' => 1,
65 'penalty' => 0.3333333,
66 'length' => 1,
67 'responseformat' => 'editor',
68 'responsefieldlines' => 15,
69 'attachments' => 0,
70 'graderinfo' => array(
71 'text' => '',
72 'format' => FORMAT_HTML,
73 'files' => array()),
74 );
75
76 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
77 }
78
79 public function test_export_essay() {
80 $qdata = (object) array(
81 'id' => 666 ,
82 'name' => 'Q8',
83 'questiontext' => 'How are you?',
84 'questiontextformat' => FORMAT_MOODLE,
85 'generalfeedback' => '',
86 'generalfeedbackformat' => FORMAT_MOODLE,
87 'defaultmark' => 1,
88 'penalty' => 0.3333333,
89 'length' => 1,
90 'qtype' => 'essay',
91 'options' => (object) array(
92 'responseformat' => 'editor',
93 'responsefieldlines' => 15,
94 'attachments' => 0,
95 'graderinfo' => '',
96 'graderinfoformat' => FORMAT_HTML,
97 ),
98 );
99
100 $exporter = new qformat_gift();
101 $gift = $exporter->writequestion($qdata);
102
103 $expectedgift = "// question: 666 name: Q8
104::Q8::How are you?{}
105
106";
107
108 $this->assert_same_gift($expectedgift, $gift);
109 }
110
111 public function test_import_match() {
112 $gift = '
113// question: 2 name: Moodle activities
114::Moodle activities::[html]Match the <b>activity</b> to the description.{
115 =[html]An activity supporting asynchronous discussions. -> Forum
116 =[moodle]A teacher asks a question and specifies a choice of multiple responses. -> Choice
117 =[plain]A bank of record entries which participants can add to. -> Database
118 =[markdown]A collection of web pages that anyone can add to or edit. -> Wiki
119 = -> Chat
120}';
121 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
122
123 $importer = new qformat_gift();
124 $q = $importer->readquestion($lines);
125
126 $expectedq = (object) array(
127 'name' => 'Moodle activities',
128 'questiontext' => 'Match the <b>activity</b> to the description.',
129 'questiontextformat' => FORMAT_HTML,
130 'generalfeedback' => '',
131 'generalfeedbackformat' => FORMAT_HTML,
132 'qtype' => 'match',
133 'defaultmark' => 1,
134 'penalty' => 0.3333333,
135 'length' => 1,
136 'shuffleanswers' => '1',
137 'correctfeedback' => array(
138 'text' => '',
139 'format' => FORMAT_HTML,
140 'files' => array(),
141 ),
142 'partiallycorrectfeedback' => array(
143 'text' => '',
144 'format' => FORMAT_HTML,
145 'files' => array(),
146 ),
147 'incorrectfeedback' => array(
148 'text' => '',
149 'format' => FORMAT_HTML,
150 'files' => array(),
151 ),
152 'subquestions' => array(
153 0 => array(
154 'text' => 'An activity supporting asynchronous discussions.',
155 'format' => FORMAT_HTML,
156 'files' => array(),
157 ),
158 1 => array(
159 'text' => 'A teacher asks a question and specifies a choice of multiple responses.',
160 'format' => FORMAT_MOODLE,
161 'files' => array(),
162 ),
163 2 => array(
164 'text' => 'A bank of record entries which participants can add to.',
165 'format' => FORMAT_PLAIN,
166 'files' => array(),
167 ),
168 3 => array(
169 'text' => 'A collection of web pages that anyone can add to or edit.',
170 'format' => FORMAT_MARKDOWN,
171 'files' => array(),
172 ),
173 4 => array(
174 'text' => '',
175 'format' => FORMAT_HTML,
176 'files' => array(),
177 ),
178 ),
179 'subanswers' => array(
180 0 => 'Forum',
181 1 => 'Choice',
182 2 => 'Database',
183 3 => 'Wiki',
184 4 => 'Chat',
185 ),
186 );
187
188 // Repeated test for better failure messages.
189 $this->assertEquals($expectedq->subquestions, $q->subquestions);
190 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
191 }
192
193 public function test_export_match() {
194 $qdata = (object) array(
195 'id' => 666 ,
196 'name' => 'Moodle activities',
197 'questiontext' => 'Match the <b>activity</b> to the description.',
198 'questiontextformat' => FORMAT_HTML,
199 'generalfeedback' => '',
200 'generalfeedbackformat' => FORMAT_HTML,
201 'defaultmark' => 1,
202 'penalty' => 0.3333333,
203 'length' => 1,
204 'qtype' => 'match',
205 'options' => (object) array(
206 'id' => 123,
207 'question' => 666,
208 'shuffleanswers' => 1,
209 'subquestions' => array(
210 42 => (object) array(
211 'id' => 1234,
212 'code' => 12341234,
213 'question' => 666,
214 'questiontext' => '<div class="frog">An activity supporting asynchronous discussions.</div>',
215 'questiontextformat' => FORMAT_HTML,
216 'answertext' => 'Forum',
217 ),
218 43 => (object) array(
219 'id' => 1234,
220 'code' => 12341234,
221 'question' => 666,
222 'questiontext' => 'A teacher asks a question and specifies a choice of multiple responses.',
223 'questiontextformat' => FORMAT_MOODLE,
224 'answertext' => 'Choice',
225 ),
226 44 => (object) array(
227 'id' => 1234,
228 'code' => 12341234,
229 'question' => 666,
230 'questiontext' => 'A bank of record entries which participants can add to.',
231 'questiontextformat' => FORMAT_PLAIN,
232 'answertext' => 'Database',
233 ),
234 45 => (object) array(
235 'id' => 1234,
236 'code' => 12341234,
237 'question' => 666,
238 'questiontext' => 'A collection of web pages that anyone can add to or edit.',
239 'questiontextformat' => FORMAT_MARKDOWN,
240 'answertext' => 'Wiki',
241 ),
242 46 => (object) array(
243 'id' => 1234,
244 'code' => 12341234,
245 'question' => 666,
246 'questiontext' => '',
247 'questiontextformat' => FORMAT_MARKDOWN,
248 'answertext' => 'Chat',
249 ),
250 ),
251 ),
252 );
253
254 $exporter = new qformat_gift();
255 $gift = $exporter->writequestion($qdata);
256
257 $expectedgift = "// question: 666 name: Moodle activities
258::Moodle activities::[html]Match the <b>activity</b> to the description.{
259\t=<div class\\=\"frog\">An activity supporting asynchronous discussions.</div> -> Forum
260\t=[moodle]A teacher asks a question and specifies a choice of multiple responses. -> Choice
261\t=[plain]A bank of record entries which participants can add to. -> Database
262\t=[markdown]A collection of web pages that anyone can add to or edit. -> Wiki
263\t= -> Chat
264}
265
266";
267
268 $this->assert_same_gift($expectedgift, $gift);
269 }
270
271 public function test_import_multichoice() {
272 $gift = "
273// multiple choice with specified feedback for right and wrong answers
274::Q2:: What's between orange and green in the spectrum?
275{
276 =yellow # right; good!
277 ~red # [html]wrong, it's yellow
278 ~[plain]blue # wrong, it's yellow
279}";
280 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
281
282 $importer = new qformat_gift();
283 $q = $importer->readquestion($lines);
284
285 $expectedq = (object) array(
286 'name' => 'Q2',
287 'questiontext' => "What's between orange and green in the spectrum?",
288 'questiontextformat' => FORMAT_MOODLE,
289 'generalfeedback' => '',
290 'generalfeedbackformat' => FORMAT_MOODLE,
291 'qtype' => 'multichoice',
292 'defaultmark' => 1,
293 'penalty' => 0.3333333,
294 'length' => 1,
295 'single' => 1,
296 'shuffleanswers' => '1',
297 'answernumbering' => 'abc',
298 'correctfeedback' => array(
299 'text' => '',
300 'format' => FORMAT_MOODLE,
301 'files' => array(),
302 ),
303 'partiallycorrectfeedback' => array(
304 'text' => '',
305 'format' => FORMAT_MOODLE,
306 'files' => array(),
307 ),
308 'incorrectfeedback' => array(
309 'text' => '',
310 'format' => FORMAT_MOODLE,
311 'files' => array(),
312 ),
313 'answer' => array(
314 0 => array(
315 'text' => 'yellow',
316 'format' => FORMAT_MOODLE,
317 'files' => array(),
318 ),
319 1 => array(
320 'text' => 'red',
321 'format' => FORMAT_MOODLE,
322 'files' => array(),
323 ),
324 2 => array(
325 'text' => 'blue',
326 'format' => FORMAT_PLAIN,
327 'files' => array(),
328 ),
329 ),
330 'fraction' => array(1, 0, 0),
331 'feedback' => array(
332 0 => array(
333 'text' => 'right; good!',
334 'format' => FORMAT_MOODLE,
335 'files' => array(),
336 ),
337 1 => array(
338 'text' => "wrong, it's yellow",
339 'format' => FORMAT_HTML,
340 'files' => array(),
341 ),
342 2 => array(
343 'text' => "wrong, it's yellow",
344 'format' => FORMAT_MOODLE,
345 'files' => array(),
346 ),
347 ),
348 );
349
350 // Repeated test for better failure messages.
351 $this->assertEquals($expectedq->answer, $q->answer);
352 $this->assertEquals($expectedq->feedback, $q->feedback);
353 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
354 }
355
356 public function test_import_multichoice_multi() {
357 $gift = "
358// multiple choice, multiple response with specified feedback for right and wrong answers
359::colours:: What's between orange and green in the spectrum?
360{
361 ~%50%yellow # right; good!
362 ~%-100%red # [html]wrong
363 ~%50%off-beige # right; good!
364 ~%-100%[plain]blue # wrong
365}";
366 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
367
368 $importer = new qformat_gift();
369 $q = $importer->readquestion($lines);
370
371 $expectedq = (object) array(
372 'name' => 'colours',
373 'questiontext' => "What's between orange and green in the spectrum?",
374 'questiontextformat' => FORMAT_MOODLE,
375 'generalfeedback' => '',
376 'generalfeedbackformat' => FORMAT_MOODLE,
377 'qtype' => 'multichoice',
378 'defaultmark' => 1,
379 'penalty' => 0.3333333,
380 'length' => 1,
381 'single' => 0,
382 'shuffleanswers' => '1',
383 'answernumbering' => 'abc',
384 'correctfeedback' => array(
385 'text' => '',
386 'format' => FORMAT_MOODLE,
387 'files' => array(),
388 ),
389 'partiallycorrectfeedback' => array(
390 'text' => '',
391 'format' => FORMAT_MOODLE,
392 'files' => array(),
393 ),
394 'incorrectfeedback' => array(
395 'text' => '',
396 'format' => FORMAT_MOODLE,
397 'files' => array(),
398 ),
399 'answer' => array(
400 0 => array(
401 'text' => 'yellow',
402 'format' => FORMAT_MOODLE,
403 'files' => array(),
404 ),
405 1 => array(
406 'text' => 'red',
407 'format' => FORMAT_MOODLE,
408 'files' => array(),
409 ),
410 2 => array(
411 'text' => 'off-beige',
412 'format' => FORMAT_MOODLE,
413 'files' => array(),
414 ),
415 3 => array(
416 'text' => 'blue',
417 'format' => FORMAT_PLAIN,
418 'files' => array(),
419 ),
420 ),
421 'fraction' => array(0.5, -1, 0.5, -1),
422 'feedback' => array(
423 0 => array(
424 'text' => 'right; good!',
425 'format' => FORMAT_MOODLE,
426 'files' => array(),
427 ),
428 1 => array(
429 'text' => "wrong",
430 'format' => FORMAT_HTML,
431 'files' => array(),
432 ),
433 2 => array(
434 'text' => "right; good!",
435 'format' => FORMAT_MOODLE,
436 'files' => array(),
437 ),
438 3 => array(
439 'text' => "wrong",
440 'format' => FORMAT_MOODLE,
441 'files' => array(),
442 ),
443 ),
444 );
445
446 // Repeated test for better failure messages.
447 $this->assertEquals($expectedq->answer, $q->answer);
448 $this->assertEquals($expectedq->feedback, $q->feedback);
449 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
450 }
451
452 public function test_export_multichoice() {
453 $qdata = (object) array(
454 'id' => 666 ,
455 'name' => 'Q8',
456 'questiontext' => "What's between orange and green in the spectrum?",
457 'questiontextformat' => FORMAT_MOODLE,
458 'generalfeedback' => '',
459 'generalfeedbackformat' => FORMAT_MOODLE,
460 'defaultmark' => 1,
461 'penalty' => 0.3333333,
462 'length' => 1,
463 'qtype' => 'multichoice',
464 'options' => (object) array(
465 'single' => 1,
466 'shuffleanswers' => '1',
467 'answernumbering' => 'abc',
468 'correctfeedback' => '',
469 'correctfeedbackformat' => FORMAT_MOODLE,
470 'partiallycorrectfeedback' => '',
471 'partiallycorrectfeedbackformat' => FORMAT_MOODLE,
472 'incorrectfeedback' => '',
473 'incorrectfeedbackformat' => FORMAT_MOODLE,
474 'answers' => array(
475 123 => (object) array(
476 'id' => 123,
477 'answer' => 'yellow',
478 'answerformat' => FORMAT_MOODLE,
479 'fraction' => 1,
480 'feedback' => 'right; good!',
481 'feedbackformat' => FORMAT_MOODLE,
482 ),
483 124 => (object) array(
484 'id' => 124,
485 'answer' => 'red',
486 'answerformat' => FORMAT_MOODLE,
487 'fraction' => 0,
488 'feedback' => "wrong, it's yellow",
489 'feedbackformat' => FORMAT_HTML,
490 ),
491 125 => (object) array(
492 'id' => 125,
493 'answer' => 'blue',
494 'answerformat' => FORMAT_PLAIN,
495 'fraction' => 0,
496 'feedback' => "wrong, it's yellow",
497 'feedbackformat' => FORMAT_MOODLE,
498 ),
499 ),
500 ),
501 );
502
503 $exporter = new qformat_gift();
504 $gift = $exporter->writequestion($qdata);
505
506 $expectedgift = "// question: 666 name: Q8
507::Q8::What's between orange and green in the spectrum?{
508\t=yellow#right; good!
509\t~red#[html]wrong, it's yellow
510\t~[plain]blue#wrong, it's yellow
511}
512
513";
514
515 $this->assert_same_gift($expectedgift, $gift);
516 }
517
518 public function test_import_numerical() {
519 $gift = "
520// math range question
521::Q5:: What is a number from 1 to 5? {#3:2~#Completely wrong}";
522 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
523
524 $importer = new qformat_gift();
525 $q = $importer->readquestion($lines);
526
527 $expectedq = (object) array(
528 'name' => 'Q5',
529 'questiontext' => "What is a number from 1 to 5?",
530 'questiontextformat' => FORMAT_MOODLE,
531 'generalfeedback' => '',
532 'generalfeedbackformat' => FORMAT_MOODLE,
533 'qtype' => 'numerical',
534 'defaultmark' => 1,
535 'penalty' => 0.3333333,
536 'length' => 1,
537 'answer' => array(
538 '3',
539 '*',
540 ),
541 'fraction' => array(1, 0),
542 'feedback' => array(
543 0 => array(
544 'text' => '',
545 'format' => FORMAT_MOODLE,
546 'files' => array(),
547 ),
548 1 => array(
549 'text' => "Completely wrong",
550 'format' => FORMAT_MOODLE,
551 'files' => array(),
552 ),
553 ),
554 'tolerance' => array(2, 0),
555 );
556
557 // Repeated test for better failure messages.
558 $this->assertEquals($expectedq->answer, $q->answer);
559 $this->assertEquals($expectedq->fraction, $q->fraction);
560 $this->assertEquals($expectedq->feedback, $q->feedback);
561 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
562 }
563
564 public function test_export_numerical() {
565 $qdata = (object) array(
566 'id' => 666 ,
567 'name' => 'Q5',
568 'questiontext' => "What is a number from 1 to 5?",
569 'questiontextformat' => FORMAT_MOODLE,
570 'generalfeedback' => '',
571 'generalfeedbackformat' => FORMAT_MOODLE,
572 'defaultmark' => 1,
573 'penalty' => 1,
574 'length' => 1,
575 'qtype' => 'numerical',
576 'options' => (object) array(
577 'id' => 123,
578 'question' => 666,
579 'showunits' => 0,
580 'unitsleft' => 0,
581 'showunits' => 2,
582 'unitgradingtype' => 0,
583 'unitpenalty' => 0,
584 'answers' => array(
585 1 => (object) array(
586 'id' => 123,
587 'answer' => '3',
588 'answerformat' => 0,
589 'fraction' => 1,
590 'tolerance' => 2,
591 'feedback' => '',
592 'feedbackformat' => FORMAT_MOODLE,
593 ),
594 2 => (object) array(
595 'id' => 124,
596 'answer' => '*',
597 'answerformat' => 0,
598 'fraction' => 0,
599 'tolerance' => 0,
600 'feedback' => "Completely wrong",
601 'feedbackformat' => FORMAT_MOODLE,
602 ),
603 ),
604 ),
605 );
606
607 $exporter = new qformat_gift();
608 $gift = $exporter->writequestion($qdata);
609
610 $expectedgift = "// question: 666 name: Q5
611::Q5::What is a number from 1 to 5?{#
612\t=%100%3:2#
613\t~#Completely wrong
614}
615
616";
617
618 $this->assert_same_gift($expectedgift, $gift);
619 }
620
621 public function test_import_shortanswer() {
622 $gift = "
623// question: 666 name: Shortanswer
624::Shortanswer::Which is the best animal?{
625 =Frog#Good!
626 =%50%Cat#What is it with Moodlers and cats?
627 =%0%*#Completely wrong
628}";
629 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
630
631 $importer = new qformat_gift();
632 $q = $importer->readquestion($lines);
633
634 $expectedq = (object) array(
635 'name' => 'Shortanswer',
636 'questiontext' => "Which is the best animal?",
637 'questiontextformat' => FORMAT_MOODLE,
638 'generalfeedback' => '',
639 'generalfeedbackformat' => FORMAT_MOODLE,
640 'qtype' => 'shortanswer',
641 'defaultmark' => 1,
642 'penalty' => 0.3333333,
643 'length' => 1,
644 'answer' => array(
645 'Frog',
646 'Cat',
647 '*',
648 ),
649 'fraction' => array(1, 0.5, 0),
650 'feedback' => array(
651 0 => array(
652 'text' => 'Good!',
653 'format' => FORMAT_MOODLE,
654 'files' => array(),
655 ),
656 1 => array(
657 'text' => "What is it with Moodlers and cats?",
658 'format' => FORMAT_MOODLE,
659 'files' => array(),
660 ),
661 2 => array(
662 'text' => "Completely wrong",
663 'format' => FORMAT_MOODLE,
664 'files' => array(),
665 ),
666 ),
667 );
668
669 // Repeated test for better failure messages.
670 $this->assertEquals($expectedq->answer, $q->answer);
671 $this->assertEquals($expectedq->fraction, $q->fraction);
672 $this->assertEquals($expectedq->feedback, $q->feedback);
673 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
674 }
675
676 public function test_export_shortanswer() {
677 $qdata = (object) array(
678 'id' => 666 ,
679 'name' => 'Shortanswer',
680 'questiontext' => "Which is the best animal?",
681 'questiontextformat' => FORMAT_MOODLE,
682 'generalfeedback' => '',
683 'generalfeedbackformat' => FORMAT_MOODLE,
684 'defaultmark' => 1,
685 'penalty' => 1,
686 'length' => 1,
687 'qtype' => 'shortanswer',
688 'options' => (object) array(
689 'id' => 123,
690 'question' => 666,
691 'usecase' => 1,
692 'answers' => array(
693 1 => (object) array(
694 'id' => 1,
695 'answer' => 'Frog',
696 'answerformat' => 0,
697 'fraction' => 1,
698 'feedback' => 'Good!',
699 'feedbackformat' => FORMAT_MOODLE,
700 ),
701 2 => (object) array(
702 'id' => 2,
703 'answer' => 'Cat',
704 'answerformat' => 0,
705 'fraction' => 0.5,
706 'feedback' => "What is it with Moodlers and cats?",
707 'feedbackformat' => FORMAT_MOODLE,
708 ),
709 3 => (object) array(
710 'id' => 3,
711 'answer' => '*',
712 'answerformat' => 0,
713 'fraction' => 0,
714 'feedback' => "Completely wrong",
715 'feedbackformat' => FORMAT_MOODLE,
716 ),
717 ),
718 ),
719 );
720
721 $exporter = new qformat_gift();
722 $gift = $exporter->writequestion($qdata);
723
724 $expectedgift = "// question: 666 name: Shortanswer
725::Shortanswer::Which is the best animal?{
726\t=%100%Frog#Good!
727\t=%50%Cat#What is it with Moodlers and cats?
728\t=%0%*#Completely wrong
729}
730
731";
732
733 $this->assert_same_gift($expectedgift, $gift);
734 }
735
736 public function test_import_truefalse() {
737 $gift = "
738// true/false
739::Q1:: 42 is the Absolute Answer to everything.{
740FALSE#42 is the Ultimate Answer.#You gave the right answer.}";
741 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
742
743 $importer = new qformat_gift();
744 $q = $importer->readquestion($lines);
745
746 $expectedq = (object) array(
747 'name' => 'Q1',
748 'questiontext' => "42 is the Absolute Answer to everything.",
749 'questiontextformat' => FORMAT_MOODLE,
750 'generalfeedback' => '',
751 'generalfeedbackformat' => FORMAT_MOODLE,
752 'qtype' => 'truefalse',
753 'defaultmark' => 1,
754 'penalty' => 1,
755 'length' => 1,
756 'correctanswer' => 0,
757 'feedbacktrue' => array(
758 'text' => '42 is the Ultimate Answer.',
759 'format' => FORMAT_MOODLE,
760 'files' => array(),
761 ),
762 'feedbackfalse' => array(
763 'text' => 'You gave the right answer.',
764 'format' => FORMAT_MOODLE,
765 'files' => array(),
766 ),
767 );
768
769 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
770 }
771
772 public function test_import_truefalse_true_answer1() {
773 $gift = "// name 0-11
774::2-08 TSL::TSL is blablabla.{T}";
775 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
776
777 $importer = new qformat_gift();
778 $q = $importer->readquestion($lines);
779
780 $expectedq = (object) array(
781 'name' => '2-08 TSL',
782 'questiontext' => "TSL is blablabla.",
783 'questiontextformat' => FORMAT_MOODLE,
784 'generalfeedback' => '',
785 'generalfeedbackformat' => FORMAT_MOODLE,
786 'qtype' => 'truefalse',
787 'defaultmark' => 1,
788 'penalty' => 1,
789 'length' => 1,
790 'correctanswer' => 1,
791 'feedbacktrue' => array(
792 'text' => '',
793 'format' => FORMAT_MOODLE,
794 'files' => array(),
795 ),
796 'feedbackfalse' => array(
797 'text' => '',
798 'format' => FORMAT_MOODLE,
799 'files' => array(),
800 ),
801 );
802
803 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
804 }
805
806 public function test_import_truefalse_true_answer2() {
807 $gift = "// name 0-11
808::2-08 TSL::TSL is blablabla.{TRUE}";
809 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
810
811 $importer = new qformat_gift();
812 $q = $importer->readquestion($lines);
813
814 $expectedq = (object) array(
815 'name' => '2-08 TSL',
816 'questiontext' => "TSL is blablabla.",
817 'questiontextformat' => FORMAT_MOODLE,
818 'generalfeedback' => '',
819 'generalfeedbackformat' => FORMAT_MOODLE,
820 'qtype' => 'truefalse',
821 'defaultmark' => 1,
822 'penalty' => 1,
823 'length' => 1,
824 'correctanswer' => 1,
825 'feedbacktrue' => array(
826 'text' => '',
827 'format' => FORMAT_MOODLE,
828 'files' => array(),
829 ),
830 'feedbackfalse' => array(
831 'text' => '',
832 'format' => FORMAT_MOODLE,
833 'files' => array(),
834 ),
835 );
836
837 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
838 }
839
840 public function test_export_truefalse() {
841 $qdata = (object) array(
842 'id' => 666 ,
843 'name' => 'Q1',
844 'questiontext' => "42 is the Absolute Answer to everything.",
845 'questiontextformat' => FORMAT_MOODLE,
846 'generalfeedback' => '',
847 'generalfeedbackformat' => FORMAT_MOODLE,
848 'defaultmark' => 1,
849 'penalty' => 1,
850 'length' => 1,
851 'qtype' => 'truefalse',
852 'options' => (object) array(
853 'id' => 123,
854 'question' => 666,
855 'trueanswer' => 1,
856 'falseanswer' => 2,
857 'answers' => array(
858 1 => (object) array(
859 'id' => 123,
860 'answer' => 'True',
861 'answerformat' => 0,
862 'fraction' => 1,
863 'feedback' => 'You gave the right answer.',
864 'feedbackformat' => FORMAT_MOODLE,
865 ),
866 2 => (object) array(
867 'id' => 124,
868 'answer' => 'False',
869 'answerformat' => 0,
870 'fraction' => 0,
871 'feedback' => "42 is the Ultimate Answer.",
872 'feedbackformat' => FORMAT_HTML,
873 ),
874 ),
875 ),
876 );
877
878 $exporter = new qformat_gift();
879 $gift = $exporter->writequestion($qdata);
880
881 $expectedgift = "// question: 666 name: Q1
882::Q1::42 is the Absolute Answer to everything.{TRUE#[html]42 is the Ultimate Answer.#You gave the right answer.}
883
884";
885
886 $this->assert_same_gift($expectedgift, $gift);
887 }
69a600a0
TH
888
889 public function test_export_backslash() {
890 // There was a bug (MDL-34171) where \\ was getting exported as \\, not
891 // \\\\, and on import, \\ in converted to \.
892 // We need \\\\ in the test code, because of PHPs string escaping rules.
893 $qdata = (object) array(
894 'id' => 666 ,
895 'name' => 'backslash',
896 'questiontext' => 'A \\ B \\\\ C',
897 'questiontextformat' => FORMAT_MOODLE,
898 'generalfeedback' => '',
899 'generalfeedbackformat' => FORMAT_MOODLE,
900 'defaultmark' => 1,
901 'penalty' => 0.3333333,
902 'length' => 1,
903 'qtype' => 'essay',
904 'options' => (object) array(
905 'responseformat' => 'editor',
906 'responsefieldlines' => 15,
907 'attachments' => 0,
908 'graderinfo' => '',
909 'graderinfoformat' => FORMAT_HTML,
910 ),
911 );
912
913 $exporter = new qformat_gift();
914 $gift = $exporter->writequestion($qdata);
915
916 $expectedgift = "// question: 666 name: backslash
917::backslash::A \\\\ B \\\\\\\\ C{}
918
919";
920
921 $this->assert_same_gift($expectedgift, $gift);
922 }
923
924 public function test_import_backslash() {
925 // There was a bug (MDL-34171) where \\ in the import was getting changed
926 // to \. This test checks for that.
927 // We need \\\\ in the test code, because of PHPs string escaping rules.
928 $gift = '
929// essay
930::double backslash:: A \\\\ B \\\\\\\\ C{}';
931 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
932
933 $importer = new qformat_gift();
934 $q = $importer->readquestion($lines);
935
936 $expectedq = (object) array(
937 'name' => 'double backslash',
938 'questiontext' => 'A \\ B \\\\ C',
939 'questiontextformat' => FORMAT_MOODLE,
940 'generalfeedback' => '',
941 'generalfeedbackformat' => FORMAT_MOODLE,
942 'qtype' => 'essay',
943 'defaultmark' => 1,
944 'penalty' => 0.3333333,
945 'length' => 1,
946 'responseformat' => 'editor',
947 'responsefieldlines' => 15,
948 'attachments' => 0,
949 'graderinfo' => array(
950 'text' => '',
951 'format' => FORMAT_HTML,
952 'files' => array()),
953 );
954
955 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
956 }
603bd001 957}