[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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_gift 21 * @copyright 2010 The Open University 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 26 defined('MOODLE_INTERNAL') || die(); 27 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/gift/format.php'); 32 require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); 33 34 35 /** 36 * Unit tests for the GIFT import/export format. 37 * 38 * @copyright 2010 The Open University 39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 40 */ 41 class qformat_gift_test extends question_testcase { 42 public function assert_same_gift($expectedtext, $text) { 43 $this->assertEquals(str_replace("\r\n", "\n", $expectedtext), 44 str_replace("\r\n", "\n", $text)); 45 } 46 47 public function test_import_essay() { 48 $gift = ' 49 // essay 50 ::Q8:: How are you? {}'; 51 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift)); 52 53 $importer = new qformat_gift(); 54 $q = $importer->readquestion($lines); 55 56 $expectedq = (object) array( 57 'name' => 'Q8', 58 'questiontext' => 'How are you?', 59 'questiontextformat' => FORMAT_MOODLE, 60 'generalfeedback' => '', 61 'generalfeedbackformat' => FORMAT_MOODLE, 62 'qtype' => 'essay', 63 'defaultmark' => 1, 64 'penalty' => 0.3333333, 65 'length' => 1, 66 'responseformat' => 'editor', 67 'responsefieldlines' => 15, 68 'attachments' => 0, 69 'graderinfo' => array( 70 'text' => '', 71 'format' => FORMAT_HTML, 72 'files' => array()), 73 ); 74 75 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 76 } 77 78 public function test_export_essay() { 79 $qdata = (object) array( 80 'id' => 666 , 81 'name' => 'Q8', 82 'questiontext' => 'How are you?', 83 'questiontextformat' => FORMAT_MOODLE, 84 'generalfeedback' => '', 85 'generalfeedbackformat' => FORMAT_MOODLE, 86 'defaultmark' => 1, 87 'penalty' => 0.3333333, 88 'length' => 1, 89 'qtype' => 'essay', 90 'options' => (object) array( 91 'responseformat' => 'editor', 92 'responsefieldlines' => 15, 93 'attachments' => 0, 94 'graderinfo' => '', 95 'graderinfoformat' => FORMAT_HTML, 96 ), 97 ); 98 99 $exporter = new qformat_gift(); 100 $gift = $exporter->writequestion($qdata); 101 102 $expectedgift = "// question: 666 name: Q8 103 ::Q8::How are you?{} 104 105 "; 106 107 $this->assert_same_gift($expectedgift, $gift); 108 } 109 110 public function test_import_match() { 111 $gift = ' 112 // question: 2 name: Moodle activities 113 ::Moodle activities::[html]Match the <b>activity</b> to the description.{ 114 =[html]An activity supporting asynchronous discussions. -> Forum 115 =[moodle]A teacher asks a question and specifies a choice of multiple responses. -> Choice 116 =[plain]A bank of record entries which participants can add to. -> Database 117 =[markdown]A collection of web pages that anyone can add to or edit. -> Wiki 118 = -> Chat 119 }'; 120 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift)); 121 122 $importer = new qformat_gift(); 123 $q = $importer->readquestion($lines); 124 125 $expectedq = (object) array( 126 'name' => 'Moodle activities', 127 'questiontext' => 'Match the <b>activity</b> to the description.', 128 'questiontextformat' => FORMAT_HTML, 129 'generalfeedback' => '', 130 'generalfeedbackformat' => FORMAT_HTML, 131 'qtype' => 'match', 132 'defaultmark' => 1, 133 'penalty' => 0.3333333, 134 'length' => 1, 135 'shuffleanswers' => '1', 136 'correctfeedback' => array( 137 'text' => '', 138 'format' => FORMAT_HTML, 139 'files' => array(), 140 ), 141 'partiallycorrectfeedback' => array( 142 'text' => '', 143 'format' => FORMAT_HTML, 144 'files' => array(), 145 ), 146 'incorrectfeedback' => array( 147 'text' => '', 148 'format' => FORMAT_HTML, 149 'files' => array(), 150 ), 151 'subquestions' => array( 152 0 => array( 153 'text' => 'An activity supporting asynchronous discussions.', 154 'format' => FORMAT_HTML, 155 'files' => array(), 156 ), 157 1 => array( 158 'text' => 'A teacher asks a question and specifies a choice of multiple responses.', 159 'format' => FORMAT_MOODLE, 160 'files' => array(), 161 ), 162 2 => array( 163 'text' => 'A bank of record entries which participants can add to.', 164 'format' => FORMAT_PLAIN, 165 'files' => array(), 166 ), 167 3 => array( 168 'text' => 'A collection of web pages that anyone can add to or edit.', 169 'format' => FORMAT_MARKDOWN, 170 'files' => array(), 171 ), 172 4 => array( 173 'text' => '', 174 'format' => FORMAT_HTML, 175 'files' => array(), 176 ), 177 ), 178 'subanswers' => array( 179 0 => 'Forum', 180 1 => 'Choice', 181 2 => 'Database', 182 3 => 'Wiki', 183 4 => 'Chat', 184 ), 185 ); 186 187 // Repeated test for better failure messages. 188 $this->assertEquals($expectedq->subquestions, $q->subquestions); 189 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 190 } 191 192 public function test_export_match() { 193 $qdata = (object) array( 194 'id' => 666 , 195 'name' => 'Moodle activities', 196 'questiontext' => 'Match the <b>activity</b> to the description.', 197 'questiontextformat' => FORMAT_HTML, 198 'generalfeedback' => '', 199 'generalfeedbackformat' => FORMAT_HTML, 200 'defaultmark' => 1, 201 'penalty' => 0.3333333, 202 'length' => 1, 203 'qtype' => 'match', 204 'options' => (object) array( 205 'id' => 123, 206 'question' => 666, 207 'shuffleanswers' => 1, 208 'subquestions' => array( 209 42 => (object) array( 210 'id' => 1234, 211 'code' => 12341234, 212 'question' => 666, 213 'questiontext' => '<div class="frog">An activity supporting asynchronous discussions.</div>', 214 'questiontextformat' => FORMAT_HTML, 215 'answertext' => 'Forum', 216 ), 217 43 => (object) array( 218 'id' => 1234, 219 'code' => 12341234, 220 'question' => 666, 221 'questiontext' => 'A teacher asks a question and specifies a choice of multiple responses.', 222 'questiontextformat' => FORMAT_MOODLE, 223 'answertext' => 'Choice', 224 ), 225 44 => (object) array( 226 'id' => 1234, 227 'code' => 12341234, 228 'question' => 666, 229 'questiontext' => 'A bank of record entries which participants can add to.', 230 'questiontextformat' => FORMAT_PLAIN, 231 'answertext' => 'Database', 232 ), 233 45 => (object) array( 234 'id' => 1234, 235 'code' => 12341234, 236 'question' => 666, 237 'questiontext' => 'A collection of web pages that anyone can add to or edit.', 238 'questiontextformat' => FORMAT_MARKDOWN, 239 'answertext' => 'Wiki', 240 ), 241 46 => (object) array( 242 'id' => 1234, 243 'code' => 12341234, 244 'question' => 666, 245 'questiontext' => '', 246 'questiontextformat' => FORMAT_MARKDOWN, 247 'answertext' => 'Chat', 248 ), 249 ), 250 ), 251 ); 252 253 $exporter = new qformat_gift(); 254 $gift = $exporter->writequestion($qdata); 255 256 $expectedgift = "// question: 666 name: Moodle activities 257 ::Moodle activities::[html]Match the <b>activity</b> to the description.{ 258 \t=<div class\\=\"frog\">An activity supporting asynchronous discussions.</div> -> Forum 259 \t=[moodle]A teacher asks a question and specifies a choice of multiple responses. -> Choice 260 \t=[plain]A bank of record entries which participants can add to. -> Database 261 \t=[markdown]A collection of web pages that anyone can add to or edit. -> Wiki 262 \t= -> Chat 263 } 264 265 "; 266 267 $this->assert_same_gift($expectedgift, $gift); 268 } 269 270 public function test_import_multichoice() { 271 $gift = " 272 // multiple choice with specified feedback for right and wrong answers 273 ::Q2:: What's between orange and green in the spectrum? 274 { 275 =yellow # right; good! 276 ~red # [html]wrong, it's yellow 277 ~[plain]blue # wrong, it's yellow 278 }"; 279 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift)); 280 281 $importer = new qformat_gift(); 282 $q = $importer->readquestion($lines); 283 284 $expectedq = (object) array( 285 'name' => 'Q2', 286 'questiontext' => "What's between orange and green in the spectrum?", 287 'questiontextformat' => FORMAT_MOODLE, 288 'generalfeedback' => '', 289 'generalfeedbackformat' => FORMAT_MOODLE, 290 'qtype' => 'multichoice', 291 'defaultmark' => 1, 292 'penalty' => 0.3333333, 293 'length' => 1, 294 'single' => 1, 295 'shuffleanswers' => '1', 296 'answernumbering' => 'abc', 297 'correctfeedback' => array( 298 'text' => '', 299 'format' => FORMAT_MOODLE, 300 'files' => array(), 301 ), 302 'partiallycorrectfeedback' => array( 303 'text' => '', 304 'format' => FORMAT_MOODLE, 305 'files' => array(), 306 ), 307 'incorrectfeedback' => array( 308 'text' => '', 309 'format' => FORMAT_MOODLE, 310 'files' => array(), 311 ), 312 'answer' => array( 313 0 => array( 314 'text' => 'yellow', 315 'format' => FORMAT_MOODLE, 316 'files' => array(), 317 ), 318 1 => array( 319 'text' => 'red', 320 'format' => FORMAT_MOODLE, 321 'files' => array(), 322 ), 323 2 => array( 324 'text' => 'blue', 325 'format' => FORMAT_PLAIN, 326 'files' => array(), 327 ), 328 ), 329 'fraction' => array(1, 0, 0), 330 'feedback' => array( 331 0 => array( 332 'text' => 'right; good!', 333 'format' => FORMAT_MOODLE, 334 'files' => array(), 335 ), 336 1 => array( 337 'text' => "wrong, it's yellow", 338 'format' => FORMAT_HTML, 339 'files' => array(), 340 ), 341 2 => array( 342 'text' => "wrong, it's yellow", 343 'format' => FORMAT_MOODLE, 344 'files' => array(), 345 ), 346 ), 347 ); 348 349 // Repeated test for better failure messages. 350 $this->assertEquals($expectedq->answer, $q->answer); 351 $this->assertEquals($expectedq->feedback, $q->feedback); 352 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 353 } 354 355 public function test_import_multichoice_multi() { 356 $gift = " 357 // multiple choice, multiple response with specified feedback for right and wrong answers 358 ::colours:: What's between orange and green in the spectrum? 359 { 360 ~%50%yellow # right; good! 361 ~%-100%red # [html]wrong 362 ~%50%off-beige # right; good! 363 ~%-100%[plain]blue # wrong 364 }"; 365 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift)); 366 367 $importer = new qformat_gift(); 368 $q = $importer->readquestion($lines); 369 370 $expectedq = (object) array( 371 'name' => 'colours', 372 'questiontext' => "What's between orange and green in the spectrum?", 373 'questiontextformat' => FORMAT_MOODLE, 374 'generalfeedback' => '', 375 'generalfeedbackformat' => FORMAT_MOODLE, 376 'qtype' => 'multichoice', 377 'defaultmark' => 1, 378 'penalty' => 0.3333333, 379 'length' => 1, 380 'single' => 0, 381 'shuffleanswers' => '1', 382 'answernumbering' => 'abc', 383 'correctfeedback' => array( 384 'text' => '', 385 'format' => FORMAT_MOODLE, 386 'files' => array(), 387 ), 388 'partiallycorrectfeedback' => array( 389 'text' => '', 390 'format' => FORMAT_MOODLE, 391 'files' => array(), 392 ), 393 'incorrectfeedback' => array( 394 'text' => '', 395 'format' => FORMAT_MOODLE, 396 'files' => array(), 397 ), 398 'answer' => array( 399 0 => array( 400 'text' => 'yellow', 401 'format' => FORMAT_MOODLE, 402 'files' => array(), 403 ), 404 1 => array( 405 'text' => 'red', 406 'format' => FORMAT_MOODLE, 407 'files' => array(), 408 ), 409 2 => array( 410 'text' => 'off-beige', 411 'format' => FORMAT_MOODLE, 412 'files' => array(), 413 ), 414 3 => array( 415 'text' => 'blue', 416 'format' => FORMAT_PLAIN, 417 'files' => array(), 418 ), 419 ), 420 'fraction' => array(0.5, -1, 0.5, -1), 421 'feedback' => array( 422 0 => array( 423 'text' => 'right; good!', 424 'format' => FORMAT_MOODLE, 425 'files' => array(), 426 ), 427 1 => array( 428 'text' => "wrong", 429 'format' => FORMAT_HTML, 430 'files' => array(), 431 ), 432 2 => array( 433 'text' => "right; good!", 434 'format' => FORMAT_MOODLE, 435 'files' => array(), 436 ), 437 3 => array( 438 'text' => "wrong", 439 'format' => FORMAT_MOODLE, 440 'files' => array(), 441 ), 442 ), 443 ); 444 445 // Repeated test for better failure messages. 446 $this->assertEquals($expectedq->answer, $q->answer); 447 $this->assertEquals($expectedq->feedback, $q->feedback); 448 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 449 } 450 451 public function test_import_multichoice_multi_tricky() { 452 $gift = " 453 // multiple choice, multiple response with specified feedback for right and wrong answers 454 ::colours:: What's between orange and green in the spectrum? 455 { 456 ~%100%yellow # right; good! 457 ~%-50%red # wrong 458 ~%-50%blue # wrong 459 }"; 460 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift)); 461 462 $importer = new qformat_gift(); 463 $q = $importer->readquestion($lines); 464 465 $expectedq = (object) array( 466 'name' => 'colours', 467 'questiontext' => "What's between orange and green in the spectrum?", 468 'questiontextformat' => FORMAT_MOODLE, 469 'generalfeedback' => '', 470 'generalfeedbackformat' => FORMAT_MOODLE, 471 'qtype' => 'multichoice', 472 'defaultmark' => 1, 473 'penalty' => 0.3333333, 474 'length' => 1, 475 'single' => 0, 476 'shuffleanswers' => '1', 477 'answernumbering' => 'abc', 478 'correctfeedback' => array( 479 'text' => '', 480 'format' => FORMAT_MOODLE, 481 'files' => array(), 482 ), 483 'partiallycorrectfeedback' => array( 484 'text' => '', 485 'format' => FORMAT_MOODLE, 486 'files' => array(), 487 ), 488 'incorrectfeedback' => array( 489 'text' => '', 490 'format' => FORMAT_MOODLE, 491 'files' => array(), 492 ), 493 'answer' => array( 494 0 => array( 495 'text' => 'yellow', 496 'format' => FORMAT_MOODLE, 497 'files' => array(), 498 ), 499 1 => array( 500 'text' => 'red', 501 'format' => FORMAT_MOODLE, 502 'files' => array(), 503 ), 504 2 => array( 505 'text' => 'blue', 506 'format' => FORMAT_MOODLE, 507 'files' => array(), 508 ), 509 ), 510 'fraction' => array(1, -0.5, -0.5), 511 'feedback' => array( 512 0 => array( 513 'text' => 'right; good!', 514 'format' => FORMAT_MOODLE, 515 'files' => array(), 516 ), 517 1 => array( 518 'text' => "wrong", 519 'format' => FORMAT_MOODLE, 520 'files' => array(), 521 ), 522 2 => array( 523 'text' => "wrong", 524 'format' => FORMAT_MOODLE, 525 'files' => array(), 526 ), 527 ), 528 ); 529 530 // Repeated test for better failure messages. 531 $this->assertEquals($expectedq->answer, $q->answer); 532 $this->assertEquals($expectedq->feedback, $q->feedback); 533 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 534 } 535 536 public function test_export_multichoice() { 537 $qdata = (object) array( 538 'id' => 666 , 539 'name' => 'Q8', 540 'questiontext' => "What's between orange and green in the spectrum?", 541 'questiontextformat' => FORMAT_MOODLE, 542 'generalfeedback' => '', 543 'generalfeedbackformat' => FORMAT_MOODLE, 544 'defaultmark' => 1, 545 'penalty' => 0.3333333, 546 'length' => 1, 547 'qtype' => 'multichoice', 548 'options' => (object) array( 549 'single' => 1, 550 'shuffleanswers' => '1', 551 'answernumbering' => 'abc', 552 'correctfeedback' => '', 553 'correctfeedbackformat' => FORMAT_MOODLE, 554 'partiallycorrectfeedback' => '', 555 'partiallycorrectfeedbackformat' => FORMAT_MOODLE, 556 'incorrectfeedback' => '', 557 'incorrectfeedbackformat' => FORMAT_MOODLE, 558 'answers' => array( 559 123 => (object) array( 560 'id' => 123, 561 'answer' => 'yellow', 562 'answerformat' => FORMAT_MOODLE, 563 'fraction' => 1, 564 'feedback' => 'right; good!', 565 'feedbackformat' => FORMAT_MOODLE, 566 ), 567 124 => (object) array( 568 'id' => 124, 569 'answer' => 'red', 570 'answerformat' => FORMAT_MOODLE, 571 'fraction' => 0, 572 'feedback' => "wrong, it's yellow", 573 'feedbackformat' => FORMAT_HTML, 574 ), 575 125 => (object) array( 576 'id' => 125, 577 'answer' => 'blue', 578 'answerformat' => FORMAT_PLAIN, 579 'fraction' => 0, 580 'feedback' => "wrong, it's yellow", 581 'feedbackformat' => FORMAT_MOODLE, 582 ), 583 ), 584 ), 585 ); 586 587 $exporter = new qformat_gift(); 588 $gift = $exporter->writequestion($qdata); 589 590 $expectedgift = "// question: 666 name: Q8 591 ::Q8::What's between orange and green in the spectrum?{ 592 \t=yellow#right; good! 593 \t~red#[html]wrong, it's yellow 594 \t~[plain]blue#wrong, it's yellow 595 } 596 597 "; 598 599 $this->assert_same_gift($expectedgift, $gift); 600 } 601 602 public function test_export_multichoice_multi_tricky() { 603 $qdata = (object) array( 604 'id' => 666 , 605 'name' => 'Q8', 606 'questiontext' => "What's between orange and green in the spectrum?", 607 'questiontextformat' => FORMAT_MOODLE, 608 'generalfeedback' => '', 609 'generalfeedbackformat' => FORMAT_MOODLE, 610 'defaultmark' => 1, 611 'penalty' => 0.3333333, 612 'length' => 1, 613 'qtype' => 'multichoice', 614 'options' => (object) array( 615 'single' => 0, 616 'shuffleanswers' => '1', 617 'answernumbering' => 'abc', 618 'correctfeedback' => '', 619 'correctfeedbackformat' => FORMAT_MOODLE, 620 'partiallycorrectfeedback' => '', 621 'partiallycorrectfeedbackformat' => FORMAT_MOODLE, 622 'incorrectfeedback' => '', 623 'incorrectfeedbackformat' => FORMAT_MOODLE, 624 'answers' => array( 625 123 => (object) array( 626 'id' => 123, 627 'answer' => 'yellow', 628 'answerformat' => FORMAT_MOODLE, 629 'fraction' => 1, 630 'feedback' => 'right; good!', 631 'feedbackformat' => FORMAT_MOODLE, 632 ), 633 124 => (object) array( 634 'id' => 124, 635 'answer' => 'red', 636 'answerformat' => FORMAT_MOODLE, 637 'fraction' => -0.5, 638 'feedback' => "wrong, it's yellow", 639 'feedbackformat' => FORMAT_MOODLE, 640 ), 641 125 => (object) array( 642 'id' => 125, 643 'answer' => 'blue', 644 'answerformat' => FORMAT_MOODLE, 645 'fraction' => -0.5, 646 'feedback' => "wrong, it's yellow", 647 'feedbackformat' => FORMAT_MOODLE, 648 ), 649 ), 650 ), 651 ); 652 653 $exporter = new qformat_gift(); 654 $gift = $exporter->writequestion($qdata); 655 656 $expectedgift = "// question: 666 name: Q8 657 ::Q8::What's between orange and green in the spectrum?{ 658 \t~%100%yellow#right; good! 659 \t~%-50%red#wrong, it's yellow 660 \t~%-50%blue#wrong, it's yellow 661 } 662 663 "; 664 665 $this->assert_same_gift($expectedgift, $gift); 666 } 667 668 public function test_import_numerical() { 669 $gift = " 670 // math range question 671 ::Q5:: What is a number from 1 to 5? {#3:2~#Completely wrong}"; 672 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift)); 673 674 $importer = new qformat_gift(); 675 $q = $importer->readquestion($lines); 676 677 $expectedq = (object) array( 678 'name' => 'Q5', 679 'questiontext' => "What is a number from 1 to 5?", 680 'questiontextformat' => FORMAT_MOODLE, 681 'generalfeedback' => '', 682 'generalfeedbackformat' => FORMAT_MOODLE, 683 'qtype' => 'numerical', 684 'defaultmark' => 1, 685 'penalty' => 0.3333333, 686 'length' => 1, 687 'answer' => array( 688 '3', 689 '*', 690 ), 691 'fraction' => array(1, 0), 692 'feedback' => array( 693 0 => array( 694 'text' => '', 695 'format' => FORMAT_MOODLE, 696 'files' => array(), 697 ), 698 1 => array( 699 'text' => "Completely wrong", 700 'format' => FORMAT_MOODLE, 701 'files' => array(), 702 ), 703 ), 704 'tolerance' => array(2, 0), 705 ); 706 707 // Repeated test for better failure messages. 708 $this->assertEquals($expectedq->answer, $q->answer); 709 $this->assertEquals($expectedq->fraction, $q->fraction); 710 $this->assertEquals($expectedq->feedback, $q->feedback); 711 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 712 } 713 714 public function test_export_numerical() { 715 $qdata = (object) array( 716 'id' => 666 , 717 'name' => 'Q5', 718 'questiontext' => "What is a number from 1 to 5?", 719 'questiontextformat' => FORMAT_MOODLE, 720 'generalfeedback' => '', 721 'generalfeedbackformat' => FORMAT_MOODLE, 722 'defaultmark' => 1, 723 'penalty' => 1, 724 'length' => 1, 725 'qtype' => 'numerical', 726 'options' => (object) array( 727 'id' => 123, 728 'question' => 666, 729 'showunits' => 0, 730 'unitsleft' => 0, 731 'showunits' => 2, 732 'unitgradingtype' => 0, 733 'unitpenalty' => 0, 734 'answers' => array( 735 1 => (object) array( 736 'id' => 123, 737 'answer' => '3', 738 'answerformat' => 0, 739 'fraction' => 1, 740 'tolerance' => 2, 741 'feedback' => '', 742 'feedbackformat' => FORMAT_MOODLE, 743 ), 744 2 => (object) array( 745 'id' => 124, 746 'answer' => '*', 747 'answerformat' => 0, 748 'fraction' => 0, 749 'tolerance' => 0, 750 'feedback' => "Completely wrong", 751 'feedbackformat' => FORMAT_MOODLE, 752 ), 753 ), 754 ), 755 ); 756 757 $exporter = new qformat_gift(); 758 $gift = $exporter->writequestion($qdata); 759 760 $expectedgift = "// question: 666 name: Q5 761 ::Q5::What is a number from 1 to 5?{# 762 \t=%100%3:2# 763 \t~#Completely wrong 764 } 765 766 "; 767 768 $this->assert_same_gift($expectedgift, $gift); 769 } 770 771 public function test_import_shortanswer() { 772 $gift = " 773 // question: 666 name: Shortanswer 774 ::Shortanswer::Which is the best animal?{ 775 =Frog#Good! 776 =%50%Cat#What is it with Moodlers and cats? 777 =%0%*#Completely wrong 778 }"; 779 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift)); 780 781 $importer = new qformat_gift(); 782 $q = $importer->readquestion($lines); 783 784 $expectedq = (object) array( 785 'name' => 'Shortanswer', 786 'questiontext' => "Which is the best animal?", 787 'questiontextformat' => FORMAT_MOODLE, 788 'generalfeedback' => '', 789 'generalfeedbackformat' => FORMAT_MOODLE, 790 'qtype' => 'shortanswer', 791 'defaultmark' => 1, 792 'penalty' => 0.3333333, 793 'length' => 1, 794 'answer' => array( 795 'Frog', 796 'Cat', 797 '*', 798 ), 799 'fraction' => array(1, 0.5, 0), 800 'feedback' => array( 801 0 => array( 802 'text' => 'Good!', 803 'format' => FORMAT_MOODLE, 804 'files' => array(), 805 ), 806 1 => array( 807 'text' => "What is it with Moodlers and cats?", 808 'format' => FORMAT_MOODLE, 809 'files' => array(), 810 ), 811 2 => array( 812 'text' => "Completely wrong", 813 'format' => FORMAT_MOODLE, 814 'files' => array(), 815 ), 816 ), 817 ); 818 819 // Repeated test for better failure messages. 820 $this->assertEquals($expectedq->answer, $q->answer); 821 $this->assertEquals($expectedq->fraction, $q->fraction); 822 $this->assertEquals($expectedq->feedback, $q->feedback); 823 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 824 } 825 826 public function test_import_shortanswer_with_general_feedback() { 827 $gift = " 828 // question: 666 name: Shortanswer 829 ::Shortanswer::Which is the best animal?{ 830 =Frog#Good! 831 =%50%Cat#What is it with Moodlers and cats? 832 =%0%*#Completely wrong 833 ####[html]Here is some general feedback! 834 }"; 835 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift)); 836 837 $importer = new qformat_gift(); 838 $q = $importer->readquestion($lines); 839 840 $expectedq = (object) array( 841 'name' => 'Shortanswer', 842 'questiontext' => "Which is the best animal?", 843 'questiontextformat' => FORMAT_MOODLE, 844 'generalfeedback' => 'Here is some general feedback!', 845 'generalfeedbackformat' => FORMAT_HTML, 846 'qtype' => 'shortanswer', 847 'defaultmark' => 1, 848 'penalty' => 0.3333333, 849 'length' => 1, 850 'answer' => array( 851 'Frog', 852 'Cat', 853 '*', 854 ), 855 'fraction' => array(1, 0.5, 0), 856 'feedback' => array( 857 0 => array( 858 'text' => 'Good!', 859 'format' => FORMAT_MOODLE, 860 'files' => array(), 861 ), 862 1 => array( 863 'text' => "What is it with Moodlers and cats?", 864 'format' => FORMAT_MOODLE, 865 'files' => array(), 866 ), 867 2 => array( 868 'text' => "Completely wrong", 869 'format' => FORMAT_MOODLE, 870 'files' => array(), 871 ), 872 ), 873 ); 874 875 // Repeated test for better failure messages. 876 $this->assertEquals($expectedq->answer, $q->answer); 877 $this->assertEquals($expectedq->fraction, $q->fraction); 878 $this->assertEquals($expectedq->feedback, $q->feedback); 879 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 880 } 881 882 public function test_export_shortanswer() { 883 $qdata = (object) array( 884 'id' => 666 , 885 'name' => 'Shortanswer', 886 'questiontext' => "Which is the best animal?", 887 'questiontextformat' => FORMAT_MOODLE, 888 'generalfeedback' => '', 889 'generalfeedbackformat' => FORMAT_MOODLE, 890 'defaultmark' => 1, 891 'penalty' => 1, 892 'length' => 1, 893 'qtype' => 'shortanswer', 894 'options' => (object) array( 895 'id' => 123, 896 'questionid' => 666, 897 'usecase' => 1, 898 'answers' => array( 899 1 => (object) array( 900 'id' => 1, 901 'answer' => 'Frog', 902 'answerformat' => 0, 903 'fraction' => 1, 904 'feedback' => 'Good!', 905 'feedbackformat' => FORMAT_MOODLE, 906 ), 907 2 => (object) array( 908 'id' => 2, 909 'answer' => 'Cat', 910 'answerformat' => 0, 911 'fraction' => 0.5, 912 'feedback' => "What is it with Moodlers and cats?", 913 'feedbackformat' => FORMAT_MOODLE, 914 ), 915 3 => (object) array( 916 'id' => 3, 917 'answer' => '*', 918 'answerformat' => 0, 919 'fraction' => 0, 920 'feedback' => "Completely wrong", 921 'feedbackformat' => FORMAT_MOODLE, 922 ), 923 ), 924 ), 925 ); 926 927 $exporter = new qformat_gift(); 928 $gift = $exporter->writequestion($qdata); 929 930 $expectedgift = "// question: 666 name: Shortanswer 931 ::Shortanswer::Which is the best animal?{ 932 \t=%100%Frog#Good! 933 \t=%50%Cat#What is it with Moodlers and cats? 934 \t=%0%*#Completely wrong 935 } 936 937 "; 938 939 $this->assert_same_gift($expectedgift, $gift); 940 } 941 942 public function test_export_shortanswer_with_general_feedback() { 943 $qdata = (object) array( 944 'id' => 666 , 945 'name' => 'Shortanswer', 946 'questiontext' => "Which is the best animal?", 947 'questiontextformat' => FORMAT_MOODLE, 948 'generalfeedback' => 'Here is some general feedback!', 949 'generalfeedbackformat' => FORMAT_HTML, 950 'defaultmark' => 1, 951 'penalty' => 1, 952 'length' => 1, 953 'qtype' => 'shortanswer', 954 'options' => (object) array( 955 'id' => 123, 956 'questionid' => 666, 957 'usecase' => 1, 958 'answers' => array( 959 1 => (object) array( 960 'id' => 1, 961 'answer' => 'Frog', 962 'answerformat' => 0, 963 'fraction' => 1, 964 'feedback' => 'Good!', 965 'feedbackformat' => FORMAT_MOODLE, 966 ), 967 2 => (object) array( 968 'id' => 2, 969 'answer' => 'Cat', 970 'answerformat' => 0, 971 'fraction' => 0.5, 972 'feedback' => "What is it with Moodlers and cats?", 973 'feedbackformat' => FORMAT_MOODLE, 974 ), 975 3 => (object) array( 976 'id' => 3, 977 'answer' => '*', 978 'answerformat' => 0, 979 'fraction' => 0, 980 'feedback' => "Completely wrong", 981 'feedbackformat' => FORMAT_MOODLE, 982 ), 983 ), 984 ), 985 ); 986 987 $exporter = new qformat_gift(); 988 $gift = $exporter->writequestion($qdata); 989 990 $expectedgift = "// question: 666 name: Shortanswer 991 ::Shortanswer::Which is the best animal?{ 992 \t=%100%Frog#Good! 993 \t=%50%Cat#What is it with Moodlers and cats? 994 \t=%0%*#Completely wrong 995 \t####[html]Here is some general feedback! 996 } 997 998 "; 999 1000 $this->assert_same_gift($expectedgift, $gift); 1001 } 1002 1003 public function test_import_truefalse() { 1004 $gift = " 1005 // true/false 1006 ::Q1:: 42 is the Absolute Answer to everything.{ 1007 FALSE#42 is the Ultimate Answer.#You gave the right answer.}"; 1008 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift)); 1009 1010 $importer = new qformat_gift(); 1011 $q = $importer->readquestion($lines); 1012 1013 $expectedq = (object) array( 1014 'name' => 'Q1', 1015 'questiontext' => "42 is the Absolute Answer to everything.", 1016 'questiontextformat' => FORMAT_MOODLE, 1017 'generalfeedback' => '', 1018 'generalfeedbackformat' => FORMAT_MOODLE, 1019 'qtype' => 'truefalse', 1020 'defaultmark' => 1, 1021 'penalty' => 1, 1022 'length' => 1, 1023 'correctanswer' => 0, 1024 'feedbacktrue' => array( 1025 'text' => '42 is the Ultimate Answer.', 1026 'format' => FORMAT_MOODLE, 1027 'files' => array(), 1028 ), 1029 'feedbackfalse' => array( 1030 'text' => 'You gave the right answer.', 1031 'format' => FORMAT_MOODLE, 1032 'files' => array(), 1033 ), 1034 ); 1035 1036 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 1037 } 1038 1039 public function test_import_truefalse_true_answer1() { 1040 $gift = "// name 0-11 1041 ::2-08 TSL::TSL is blablabla.{T}"; 1042 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift)); 1043 1044 $importer = new qformat_gift(); 1045 $q = $importer->readquestion($lines); 1046 1047 $expectedq = (object) array( 1048 'name' => '2-08 TSL', 1049 'questiontext' => "TSL is blablabla.", 1050 'questiontextformat' => FORMAT_MOODLE, 1051 'generalfeedback' => '', 1052 'generalfeedbackformat' => FORMAT_MOODLE, 1053 'qtype' => 'truefalse', 1054 'defaultmark' => 1, 1055 'penalty' => 1, 1056 'length' => 1, 1057 'correctanswer' => 1, 1058 'feedbacktrue' => array( 1059 'text' => '', 1060 'format' => FORMAT_MOODLE, 1061 'files' => array(), 1062 ), 1063 'feedbackfalse' => array( 1064 'text' => '', 1065 'format' => FORMAT_MOODLE, 1066 'files' => array(), 1067 ), 1068 ); 1069 1070 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 1071 } 1072 1073 public function test_import_truefalse_true_answer2() { 1074 $gift = "// name 0-11 1075 ::2-08 TSL::TSL is blablabla.{TRUE}"; 1076 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift)); 1077 1078 $importer = new qformat_gift(); 1079 $q = $importer->readquestion($lines); 1080 1081 $expectedq = (object) array( 1082 'name' => '2-08 TSL', 1083 'questiontext' => "TSL is blablabla.", 1084 'questiontextformat' => FORMAT_MOODLE, 1085 'generalfeedback' => '', 1086 'generalfeedbackformat' => FORMAT_MOODLE, 1087 'qtype' => 'truefalse', 1088 'defaultmark' => 1, 1089 'penalty' => 1, 1090 'length' => 1, 1091 'correctanswer' => 1, 1092 'feedbacktrue' => array( 1093 'text' => '', 1094 'format' => FORMAT_MOODLE, 1095 'files' => array(), 1096 ), 1097 'feedbackfalse' => array( 1098 'text' => '', 1099 'format' => FORMAT_MOODLE, 1100 'files' => array(), 1101 ), 1102 ); 1103 1104 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 1105 } 1106 1107 public function test_export_truefalse() { 1108 $qdata = (object) array( 1109 'id' => 666 , 1110 'name' => 'Q1', 1111 'questiontext' => "42 is the Absolute Answer to everything.", 1112 'questiontextformat' => FORMAT_MOODLE, 1113 'generalfeedback' => '', 1114 'generalfeedbackformat' => FORMAT_MOODLE, 1115 'defaultmark' => 1, 1116 'penalty' => 1, 1117 'length' => 1, 1118 'qtype' => 'truefalse', 1119 'options' => (object) array( 1120 'id' => 123, 1121 'question' => 666, 1122 'trueanswer' => 1, 1123 'falseanswer' => 2, 1124 'answers' => array( 1125 1 => (object) array( 1126 'id' => 123, 1127 'answer' => 'True', 1128 'answerformat' => 0, 1129 'fraction' => 1, 1130 'feedback' => 'You gave the right answer.', 1131 'feedbackformat' => FORMAT_MOODLE, 1132 ), 1133 2 => (object) array( 1134 'id' => 124, 1135 'answer' => 'False', 1136 'answerformat' => 0, 1137 'fraction' => 0, 1138 'feedback' => "42 is the Ultimate Answer.", 1139 'feedbackformat' => FORMAT_HTML, 1140 ), 1141 ), 1142 ), 1143 ); 1144 1145 $exporter = new qformat_gift(); 1146 $gift = $exporter->writequestion($qdata); 1147 1148 $expectedgift = "// question: 666 name: Q1 1149 ::Q1::42 is the Absolute Answer to everything.{TRUE#[html]42 is the Ultimate Answer.#You gave the right answer.} 1150 1151 "; 1152 1153 $this->assert_same_gift($expectedgift, $gift); 1154 } 1155 1156 public function test_export_backslash() { 1157 // There was a bug (MDL-34171) where \\ was getting exported as \\, not 1158 // \\\\, and on import, \\ in converted to \. 1159 // We need \\\\ in the test code, because of PHPs string escaping rules. 1160 $qdata = (object) array( 1161 'id' => 666 , 1162 'name' => 'backslash', 1163 'questiontext' => 'A \\ B \\\\ C', 1164 'questiontextformat' => FORMAT_MOODLE, 1165 'generalfeedback' => '', 1166 'generalfeedbackformat' => FORMAT_MOODLE, 1167 'defaultmark' => 1, 1168 'penalty' => 0.3333333, 1169 'length' => 1, 1170 'qtype' => 'essay', 1171 'options' => (object) array( 1172 'responseformat' => 'editor', 1173 'responsefieldlines' => 15, 1174 'attachments' => 0, 1175 'graderinfo' => '', 1176 'graderinfoformat' => FORMAT_HTML, 1177 ), 1178 ); 1179 1180 $exporter = new qformat_gift(); 1181 $gift = $exporter->writequestion($qdata); 1182 1183 $expectedgift = "// question: 666 name: backslash 1184 ::backslash::A \\\\ B \\\\\\\\ C{} 1185 1186 "; 1187 1188 $this->assert_same_gift($expectedgift, $gift); 1189 } 1190 1191 public function test_import_backslash() { 1192 // There was a bug (MDL-34171) where \\ in the import was getting changed 1193 // to \. This test checks for that. 1194 // We need \\\\ in the test code, because of PHPs string escaping rules. 1195 $gift = ' 1196 // essay 1197 ::double backslash:: A \\\\ B \\\\\\\\ C{}'; 1198 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift)); 1199 1200 $importer = new qformat_gift(); 1201 $q = $importer->readquestion($lines); 1202 1203 $expectedq = (object) array( 1204 'name' => 'double backslash', 1205 'questiontext' => 'A \\ B \\\\ C', 1206 'questiontextformat' => FORMAT_MOODLE, 1207 'generalfeedback' => '', 1208 'generalfeedbackformat' => FORMAT_MOODLE, 1209 'qtype' => 'essay', 1210 'defaultmark' => 1, 1211 'penalty' => 0.3333333, 1212 'length' => 1, 1213 'responseformat' => 'editor', 1214 'responsefieldlines' => 15, 1215 'attachments' => 0, 1216 'graderinfo' => array( 1217 'text' => '', 1218 'format' => FORMAT_HTML, 1219 'files' => array()), 1220 ); 1221 1222 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 1223 } 1224 1225 public function test_import_pre_content() { 1226 $gift = ' 1227 ::Q001::[html]<p>What would running the test method print?</p> 1228 <pre> 1229 public void test() \{ 1230 method1(); 1231 method2(); 1232 method3(); 1233 \} 1234 </pre> 1235 {}'; 1236 $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift)); 1237 1238 $importer = new qformat_gift(); 1239 $q = $importer->readquestion($lines); 1240 1241 $expectedq = (object) array( 1242 'name' => 'Q001', 1243 'questiontext' => '<p>What would running the test method print?</p> 1244 <pre> 1245 public void test() { 1246 method1(); 1247 method2(); 1248 method3(); 1249 } 1250 </pre>', 1251 'questiontextformat' => FORMAT_HTML, 1252 'generalfeedback' => '', 1253 'generalfeedbackformat' => FORMAT_HTML, 1254 'qtype' => 'essay', 1255 'defaultmark' => 1, 1256 'penalty' => 0.3333333, 1257 'length' => 1, 1258 'responseformat' => 'editor', 1259 'responsefieldlines' => 15, 1260 'attachments' => 0, 1261 'graderinfo' => array( 1262 'text' => '', 1263 'format' => FORMAT_HTML, 1264 'files' => array()), 1265 ); 1266 1267 $this->assert(new question_check_specified_fields_expectation($expectedq), $q); 1268 } 1269 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |