[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/question/format/gift/tests/ -> giftformat_test.php (source)

   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_export_multichoice() {
 452          $qdata = (object) array(
 453              'id' => 666 ,
 454              'name' => 'Q8',
 455              'questiontext' => "What's between orange and green in the spectrum?",
 456              'questiontextformat' => FORMAT_MOODLE,
 457              'generalfeedback' => '',
 458              'generalfeedbackformat' => FORMAT_MOODLE,
 459              'defaultmark' => 1,
 460              'penalty' => 0.3333333,
 461              'length' => 1,
 462              'qtype' => 'multichoice',
 463              'options' => (object) array(
 464                  'single' => 1,
 465                  'shuffleanswers' => '1',
 466                  'answernumbering' => 'abc',
 467                  'correctfeedback' => '',
 468                  'correctfeedbackformat' => FORMAT_MOODLE,
 469                  'partiallycorrectfeedback' => '',
 470                  'partiallycorrectfeedbackformat' => FORMAT_MOODLE,
 471                  'incorrectfeedback' => '',
 472                  'incorrectfeedbackformat' => FORMAT_MOODLE,
 473                  'answers' => array(
 474                      123 => (object) array(
 475                          'id' => 123,
 476                          'answer' => 'yellow',
 477                          'answerformat' => FORMAT_MOODLE,
 478                          'fraction' => 1,
 479                          'feedback' => 'right; good!',
 480                          'feedbackformat' => FORMAT_MOODLE,
 481                      ),
 482                      124 => (object) array(
 483                          'id' => 124,
 484                          'answer' => 'red',
 485                          'answerformat' => FORMAT_MOODLE,
 486                          'fraction' => 0,
 487                          'feedback' => "wrong, it's yellow",
 488                          'feedbackformat' => FORMAT_HTML,
 489                      ),
 490                      125 => (object) array(
 491                          'id' => 125,
 492                          'answer' => 'blue',
 493                          'answerformat' => FORMAT_PLAIN,
 494                          'fraction' => 0,
 495                          'feedback' => "wrong, it's yellow",
 496                          'feedbackformat' => FORMAT_MOODLE,
 497                      ),
 498                  ),
 499              ),
 500          );
 501  
 502          $exporter = new qformat_gift();
 503          $gift = $exporter->writequestion($qdata);
 504  
 505          $expectedgift = "// question: 666  name: Q8
 506  ::Q8::What's between orange and green in the spectrum?{
 507  \t=yellow#right; good!
 508  \t~red#[html]wrong, it's yellow
 509  \t~[plain]blue#wrong, it's yellow
 510  }
 511  
 512  ";
 513  
 514          $this->assert_same_gift($expectedgift, $gift);
 515      }
 516  
 517      public function test_import_numerical() {
 518          $gift = "
 519  // math range question
 520  ::Q5:: What is a number from 1 to 5? {#3:2~#Completely wrong}";
 521          $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
 522  
 523          $importer = new qformat_gift();
 524          $q = $importer->readquestion($lines);
 525  
 526          $expectedq = (object) array(
 527              'name' => 'Q5',
 528              'questiontext' => "What is a number from 1 to 5?",
 529              'questiontextformat' => FORMAT_MOODLE,
 530              'generalfeedback' => '',
 531              'generalfeedbackformat' => FORMAT_MOODLE,
 532              'qtype' => 'numerical',
 533              'defaultmark' => 1,
 534              'penalty' => 0.3333333,
 535              'length' => 1,
 536              'answer' => array(
 537                  '3',
 538                  '*',
 539              ),
 540              'fraction' => array(1, 0),
 541              'feedback' => array(
 542                  0 => array(
 543                      'text' => '',
 544                      'format' => FORMAT_MOODLE,
 545                      'files' => array(),
 546                  ),
 547                  1 => array(
 548                      'text' => "Completely wrong",
 549                      'format' => FORMAT_MOODLE,
 550                      'files' => array(),
 551                  ),
 552              ),
 553              'tolerance' => array(2, 0),
 554          );
 555  
 556          // Repeated test for better failure messages.
 557          $this->assertEquals($expectedq->answer, $q->answer);
 558          $this->assertEquals($expectedq->fraction, $q->fraction);
 559          $this->assertEquals($expectedq->feedback, $q->feedback);
 560          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 561      }
 562  
 563      public function test_export_numerical() {
 564          $qdata = (object) array(
 565              'id' => 666 ,
 566              'name' => 'Q5',
 567              'questiontext' => "What is a number from 1 to 5?",
 568              'questiontextformat' => FORMAT_MOODLE,
 569              'generalfeedback' => '',
 570              'generalfeedbackformat' => FORMAT_MOODLE,
 571              'defaultmark' => 1,
 572              'penalty' => 1,
 573              'length' => 1,
 574              'qtype' => 'numerical',
 575              'options' => (object) array(
 576                  'id' => 123,
 577                  'question' => 666,
 578                  'showunits' => 0,
 579                  'unitsleft' => 0,
 580                  'showunits' => 2,
 581                  'unitgradingtype' => 0,
 582                  'unitpenalty' => 0,
 583                  'answers' => array(
 584                      1 => (object) array(
 585                          'id' => 123,
 586                          'answer' => '3',
 587                          'answerformat' => 0,
 588                          'fraction' => 1,
 589                          'tolerance' => 2,
 590                          'feedback' => '',
 591                          'feedbackformat' => FORMAT_MOODLE,
 592                      ),
 593                      2 => (object) array(
 594                          'id' => 124,
 595                          'answer' => '*',
 596                          'answerformat' => 0,
 597                          'fraction' => 0,
 598                          'tolerance' => 0,
 599                          'feedback' => "Completely wrong",
 600                          'feedbackformat' => FORMAT_MOODLE,
 601                      ),
 602                  ),
 603              ),
 604          );
 605  
 606          $exporter = new qformat_gift();
 607          $gift = $exporter->writequestion($qdata);
 608  
 609          $expectedgift = "// question: 666  name: Q5
 610  ::Q5::What is a number from 1 to 5?{#
 611  \t=%100%3:2#
 612  \t~#Completely wrong
 613  }
 614  
 615  ";
 616  
 617          $this->assert_same_gift($expectedgift, $gift);
 618      }
 619  
 620      public function test_import_shortanswer() {
 621          $gift = "
 622  // question: 666  name: Shortanswer
 623  ::Shortanswer::Which is the best animal?{
 624      =Frog#Good!
 625      =%50%Cat#What is it with Moodlers and cats?
 626      =%0%*#Completely wrong
 627  }";
 628          $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
 629  
 630          $importer = new qformat_gift();
 631          $q = $importer->readquestion($lines);
 632  
 633          $expectedq = (object) array(
 634              'name' => 'Shortanswer',
 635              'questiontext' => "Which is the best animal?",
 636              'questiontextformat' => FORMAT_MOODLE,
 637              'generalfeedback' => '',
 638              'generalfeedbackformat' => FORMAT_MOODLE,
 639              'qtype' => 'shortanswer',
 640              'defaultmark' => 1,
 641              'penalty' => 0.3333333,
 642              'length' => 1,
 643              'answer' => array(
 644                  'Frog',
 645                  'Cat',
 646                  '*',
 647              ),
 648              'fraction' => array(1, 0.5, 0),
 649              'feedback' => array(
 650                  0 => array(
 651                      'text' => 'Good!',
 652                      'format' => FORMAT_MOODLE,
 653                      'files' => array(),
 654                  ),
 655                  1 => array(
 656                      'text' => "What is it with Moodlers and cats?",
 657                      'format' => FORMAT_MOODLE,
 658                      'files' => array(),
 659                  ),
 660                  2 => array(
 661                      'text' => "Completely wrong",
 662                      'format' => FORMAT_MOODLE,
 663                      'files' => array(),
 664                  ),
 665              ),
 666          );
 667  
 668          // Repeated test for better failure messages.
 669          $this->assertEquals($expectedq->answer, $q->answer);
 670          $this->assertEquals($expectedq->fraction, $q->fraction);
 671          $this->assertEquals($expectedq->feedback, $q->feedback);
 672          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 673      }
 674  
 675      public function test_import_shortanswer_with_general_feedback() {
 676          $gift = "
 677  // question: 666  name: Shortanswer
 678  ::Shortanswer::Which is the best animal?{
 679      =Frog#Good!
 680      =%50%Cat#What is it with Moodlers and cats?
 681      =%0%*#Completely wrong
 682      ####[html]Here is some general feedback!
 683  }";
 684          $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
 685  
 686          $importer = new qformat_gift();
 687          $q = $importer->readquestion($lines);
 688  
 689          $expectedq = (object) array(
 690              'name' => 'Shortanswer',
 691              'questiontext' => "Which is the best animal?",
 692              'questiontextformat' => FORMAT_MOODLE,
 693              'generalfeedback' => 'Here is some general feedback!',
 694              'generalfeedbackformat' => FORMAT_HTML,
 695              'qtype' => 'shortanswer',
 696              'defaultmark' => 1,
 697              'penalty' => 0.3333333,
 698              'length' => 1,
 699              'answer' => array(
 700                  'Frog',
 701                  'Cat',
 702                  '*',
 703              ),
 704              'fraction' => array(1, 0.5, 0),
 705              'feedback' => array(
 706                  0 => array(
 707                      'text' => 'Good!',
 708                      'format' => FORMAT_MOODLE,
 709                      'files' => array(),
 710                  ),
 711                  1 => array(
 712                      'text' => "What is it with Moodlers and cats?",
 713                      'format' => FORMAT_MOODLE,
 714                      'files' => array(),
 715                  ),
 716                  2 => array(
 717                      'text' => "Completely wrong",
 718                      'format' => FORMAT_MOODLE,
 719                      'files' => array(),
 720                  ),
 721              ),
 722          );
 723  
 724          // Repeated test for better failure messages.
 725          $this->assertEquals($expectedq->answer, $q->answer);
 726          $this->assertEquals($expectedq->fraction, $q->fraction);
 727          $this->assertEquals($expectedq->feedback, $q->feedback);
 728          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 729      }
 730  
 731      public function test_export_shortanswer() {
 732          $qdata = (object) array(
 733              'id' => 666 ,
 734              'name' => 'Shortanswer',
 735              'questiontext' => "Which is the best animal?",
 736              'questiontextformat' => FORMAT_MOODLE,
 737              'generalfeedback' => '',
 738              'generalfeedbackformat' => FORMAT_MOODLE,
 739              'defaultmark' => 1,
 740              'penalty' => 1,
 741              'length' => 1,
 742              'qtype' => 'shortanswer',
 743              'options' => (object) array(
 744                  'id' => 123,
 745                  'questionid' => 666,
 746                  'usecase' => 1,
 747                  'answers' => array(
 748                      1 => (object) array(
 749                          'id' => 1,
 750                          'answer' => 'Frog',
 751                          'answerformat' => 0,
 752                          'fraction' => 1,
 753                          'feedback' => 'Good!',
 754                          'feedbackformat' => FORMAT_MOODLE,
 755                      ),
 756                      2 => (object) array(
 757                          'id' => 2,
 758                          'answer' => 'Cat',
 759                          'answerformat' => 0,
 760                          'fraction' => 0.5,
 761                          'feedback' => "What is it with Moodlers and cats?",
 762                          'feedbackformat' => FORMAT_MOODLE,
 763                      ),
 764                      3 => (object) array(
 765                          'id' => 3,
 766                          'answer' => '*',
 767                          'answerformat' => 0,
 768                          'fraction' => 0,
 769                          'feedback' => "Completely wrong",
 770                          'feedbackformat' => FORMAT_MOODLE,
 771                      ),
 772                  ),
 773              ),
 774          );
 775  
 776          $exporter = new qformat_gift();
 777          $gift = $exporter->writequestion($qdata);
 778  
 779          $expectedgift = "// question: 666  name: Shortanswer
 780  ::Shortanswer::Which is the best animal?{
 781  \t=%100%Frog#Good!
 782  \t=%50%Cat#What is it with Moodlers and cats?
 783  \t=%0%*#Completely wrong
 784  }
 785  
 786  ";
 787  
 788          $this->assert_same_gift($expectedgift, $gift);
 789      }
 790  
 791      public function test_export_shortanswer_with_general_feedback() {
 792          $qdata = (object) array(
 793              'id' => 666 ,
 794              'name' => 'Shortanswer',
 795              'questiontext' => "Which is the best animal?",
 796              'questiontextformat' => FORMAT_MOODLE,
 797              'generalfeedback' => 'Here is some general feedback!',
 798              'generalfeedbackformat' => FORMAT_HTML,
 799              'defaultmark' => 1,
 800              'penalty' => 1,
 801              'length' => 1,
 802              'qtype' => 'shortanswer',
 803              'options' => (object) array(
 804                  'id' => 123,
 805                  'questionid' => 666,
 806                  'usecase' => 1,
 807                  'answers' => array(
 808                      1 => (object) array(
 809                          'id' => 1,
 810                          'answer' => 'Frog',
 811                          'answerformat' => 0,
 812                          'fraction' => 1,
 813                          'feedback' => 'Good!',
 814                          'feedbackformat' => FORMAT_MOODLE,
 815                      ),
 816                      2 => (object) array(
 817                          'id' => 2,
 818                          'answer' => 'Cat',
 819                          'answerformat' => 0,
 820                          'fraction' => 0.5,
 821                          'feedback' => "What is it with Moodlers and cats?",
 822                          'feedbackformat' => FORMAT_MOODLE,
 823                      ),
 824                      3 => (object) array(
 825                          'id' => 3,
 826                          'answer' => '*',
 827                          'answerformat' => 0,
 828                          'fraction' => 0,
 829                          'feedback' => "Completely wrong",
 830                          'feedbackformat' => FORMAT_MOODLE,
 831                      ),
 832                  ),
 833              ),
 834          );
 835  
 836          $exporter = new qformat_gift();
 837          $gift = $exporter->writequestion($qdata);
 838  
 839          $expectedgift = "// question: 666  name: Shortanswer
 840  ::Shortanswer::Which is the best animal?{
 841  \t=%100%Frog#Good!
 842  \t=%50%Cat#What is it with Moodlers and cats?
 843  \t=%0%*#Completely wrong
 844  \t####[html]Here is some general feedback!
 845  }
 846  
 847  ";
 848  
 849          $this->assert_same_gift($expectedgift, $gift);
 850      }
 851  
 852      public function test_import_truefalse() {
 853          $gift = "
 854  // true/false
 855  ::Q1:: 42 is the Absolute Answer to everything.{
 856  FALSE#42 is the Ultimate Answer.#You gave the right answer.}";
 857          $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
 858  
 859          $importer = new qformat_gift();
 860          $q = $importer->readquestion($lines);
 861  
 862          $expectedq = (object) array(
 863              'name' => 'Q1',
 864              'questiontext' => "42 is the Absolute Answer to everything.",
 865              'questiontextformat' => FORMAT_MOODLE,
 866              'generalfeedback' => '',
 867              'generalfeedbackformat' => FORMAT_MOODLE,
 868              'qtype' => 'truefalse',
 869              'defaultmark' => 1,
 870              'penalty' => 1,
 871              'length' => 1,
 872              'correctanswer' => 0,
 873              'feedbacktrue' => array(
 874                  'text' => '42 is the Ultimate Answer.',
 875                  'format' => FORMAT_MOODLE,
 876                  'files' => array(),
 877              ),
 878              'feedbackfalse' => array(
 879                  'text' => 'You gave the right answer.',
 880                  'format' => FORMAT_MOODLE,
 881                  'files' => array(),
 882              ),
 883          );
 884  
 885          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 886      }
 887  
 888      public function test_import_truefalse_true_answer1() {
 889          $gift = "// name 0-11
 890  ::2-08 TSL::TSL is blablabla.{T}";
 891          $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
 892  
 893          $importer = new qformat_gift();
 894          $q = $importer->readquestion($lines);
 895  
 896          $expectedq = (object) array(
 897              'name' => '2-08 TSL',
 898              'questiontext' => "TSL is blablabla.",
 899              'questiontextformat' => FORMAT_MOODLE,
 900              'generalfeedback' => '',
 901              'generalfeedbackformat' => FORMAT_MOODLE,
 902              'qtype' => 'truefalse',
 903              'defaultmark' => 1,
 904              'penalty' => 1,
 905              'length' => 1,
 906              'correctanswer' => 1,
 907              'feedbacktrue' => array(
 908                  'text' => '',
 909                  'format' => FORMAT_MOODLE,
 910                  'files' => array(),
 911              ),
 912              'feedbackfalse' => array(
 913                  'text' => '',
 914                  'format' => FORMAT_MOODLE,
 915                  'files' => array(),
 916              ),
 917          );
 918  
 919          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 920      }
 921  
 922      public function test_import_truefalse_true_answer2() {
 923          $gift = "// name 0-11
 924  ::2-08 TSL::TSL is blablabla.{TRUE}";
 925          $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
 926  
 927          $importer = new qformat_gift();
 928          $q = $importer->readquestion($lines);
 929  
 930          $expectedq = (object) array(
 931              'name' => '2-08 TSL',
 932              'questiontext' => "TSL is blablabla.",
 933              'questiontextformat' => FORMAT_MOODLE,
 934              'generalfeedback' => '',
 935              'generalfeedbackformat' => FORMAT_MOODLE,
 936              'qtype' => 'truefalse',
 937              'defaultmark' => 1,
 938              'penalty' => 1,
 939              'length' => 1,
 940              'correctanswer' => 1,
 941              'feedbacktrue' => array(
 942                  'text' => '',
 943                  'format' => FORMAT_MOODLE,
 944                  'files' => array(),
 945              ),
 946              'feedbackfalse' => array(
 947                  'text' => '',
 948                  'format' => FORMAT_MOODLE,
 949                  'files' => array(),
 950              ),
 951          );
 952  
 953          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 954      }
 955  
 956      public function test_export_truefalse() {
 957          $qdata = (object) array(
 958              'id' => 666 ,
 959              'name' => 'Q1',
 960              'questiontext' => "42 is the Absolute Answer to everything.",
 961              'questiontextformat' => FORMAT_MOODLE,
 962              'generalfeedback' => '',
 963              'generalfeedbackformat' => FORMAT_MOODLE,
 964              'defaultmark' => 1,
 965              'penalty' => 1,
 966              'length' => 1,
 967              'qtype' => 'truefalse',
 968              'options' => (object) array(
 969                  'id' => 123,
 970                  'question' => 666,
 971                  'trueanswer' => 1,
 972                  'falseanswer' => 2,
 973                  'answers' => array(
 974                      1 => (object) array(
 975                          'id' => 123,
 976                          'answer' => 'True',
 977                          'answerformat' => 0,
 978                          'fraction' => 1,
 979                          'feedback' => 'You gave the right answer.',
 980                          'feedbackformat' => FORMAT_MOODLE,
 981                      ),
 982                      2 => (object) array(
 983                          'id' => 124,
 984                          'answer' => 'False',
 985                          'answerformat' => 0,
 986                          'fraction' => 0,
 987                          'feedback' => "42 is the Ultimate Answer.",
 988                          'feedbackformat' => FORMAT_HTML,
 989                      ),
 990                  ),
 991              ),
 992          );
 993  
 994          $exporter = new qformat_gift();
 995          $gift = $exporter->writequestion($qdata);
 996  
 997          $expectedgift = "// question: 666  name: Q1
 998  ::Q1::42 is the Absolute Answer to everything.{TRUE#[html]42 is the Ultimate Answer.#You gave the right answer.}
 999  
1000  ";
1001  
1002          $this->assert_same_gift($expectedgift, $gift);
1003      }
1004  
1005      public function test_export_backslash() {
1006          // There was a bug (MDL-34171) where \\ was getting exported as \\, not
1007          // \\\\, and on import, \\ in converted to \.
1008          // We need \\\\ in the test code, because of PHPs string escaping rules.
1009          $qdata = (object) array(
1010              'id' => 666 ,
1011              'name' => 'backslash',
1012              'questiontext' => 'A \\ B \\\\ C',
1013              'questiontextformat' => FORMAT_MOODLE,
1014              'generalfeedback' => '',
1015              'generalfeedbackformat' => FORMAT_MOODLE,
1016              'defaultmark' => 1,
1017              'penalty' => 0.3333333,
1018              'length' => 1,
1019              'qtype' => 'essay',
1020              'options' => (object) array(
1021                  'responseformat' => 'editor',
1022                  'responsefieldlines' => 15,
1023                  'attachments' => 0,
1024                  'graderinfo' => '',
1025                  'graderinfoformat' => FORMAT_HTML,
1026              ),
1027          );
1028  
1029          $exporter = new qformat_gift();
1030          $gift = $exporter->writequestion($qdata);
1031  
1032          $expectedgift = "// question: 666  name: backslash
1033  ::backslash::A \\\\ B \\\\\\\\ C{}
1034  
1035  ";
1036  
1037          $this->assert_same_gift($expectedgift, $gift);
1038      }
1039  
1040      public function test_import_backslash() {
1041          // There was a bug (MDL-34171) where \\ in the import was getting changed
1042          // to \. This test checks for that.
1043          // We need \\\\ in the test code, because of PHPs string escaping rules.
1044          $gift = '
1045  // essay
1046  ::double backslash:: A \\\\ B \\\\\\\\ C{}';
1047          $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
1048  
1049          $importer = new qformat_gift();
1050          $q = $importer->readquestion($lines);
1051  
1052          $expectedq = (object) array(
1053              'name' => 'double backslash',
1054              'questiontext' => 'A \\ B \\\\ C',
1055              'questiontextformat' => FORMAT_MOODLE,
1056              'generalfeedback' => '',
1057              'generalfeedbackformat' => FORMAT_MOODLE,
1058              'qtype' => 'essay',
1059              'defaultmark' => 1,
1060              'penalty' => 0.3333333,
1061              'length' => 1,
1062              'responseformat' => 'editor',
1063              'responsefieldlines' => 15,
1064              'attachments' => 0,
1065              'graderinfo' => array(
1066                  'text' => '',
1067                  'format' => FORMAT_HTML,
1068                  'files' => array()),
1069          );
1070  
1071          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
1072      }
1073  
1074      public function test_import_pre_content() {
1075          $gift = '
1076  ::Q001::[html]<p>What would running the test method print?</p>
1077  <pre>
1078      public void test() \{
1079          method1();
1080          method2();
1081          method3();
1082      \}
1083  </pre>
1084  {}';
1085          $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
1086  
1087          $importer = new qformat_gift();
1088          $q = $importer->readquestion($lines);
1089  
1090          $expectedq = (object) array(
1091              'name' => 'Q001',
1092              'questiontext' => '<p>What would running the test method print?</p>
1093  <pre>
1094      public void test() {
1095          method1();
1096          method2();
1097          method3();
1098      }
1099  </pre>',
1100              'questiontextformat' => FORMAT_HTML,
1101              'generalfeedback' => '',
1102              'generalfeedbackformat' => FORMAT_HTML,
1103              'qtype' => 'essay',
1104              'defaultmark' => 1,
1105              'penalty' => 0.3333333,
1106              'length' => 1,
1107              'responseformat' => 'editor',
1108              'responsefieldlines' => 15,
1109              'attachments' => 0,
1110              'graderinfo' => array(
1111                  'text' => '',
1112                  'format' => FORMAT_HTML,
1113                  'files' => array()),
1114          );
1115  
1116          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
1117      }
1118  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1