Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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 Web CT question importer.
  19   *
  20   * @package    qformat_webct
  21   * @copyright  2013 Jean-Michel Vedrine
  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/webct/format.php');
  32  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  33  
  34  
  35  /**
  36   * Unit tests for the webct question import format.
  37   *
  38   * @copyright  2013 Jean-Michel Vedrine
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class qformat_webct_test extends question_testcase {
  42  
  43      public function make_test() {
  44          $lines = file(__DIR__ . '/fixtures/sample_webct.txt');
  45          return $lines;
  46      }
  47  
  48      public function test_import_match() {
  49  
  50          $txt = $this->make_test();
  51          $importer = new qformat_webct();
  52          $questions = $importer->readquestions($txt);
  53          $q = $questions[3];
  54  
  55          $expectedq = new stdClass();
  56          $expectedq->qtype = 'match';
  57          $expectedq->name = 'Classify the animals.';
  58          $expectedq->questiontext = '<i>Classify the animals.</i>';
  59          $expectedq->questiontextformat = FORMAT_HTML;
  60          $expectedq->correctfeedback = array(
  61                  'text' => '',
  62                  'format' => FORMAT_HTML,
  63                  'files' => array(),
  64              );
  65          $expectedq->partiallycorrectfeedback = array(
  66                  'text' => '',
  67                  'format' => FORMAT_HTML,
  68                  'files' => array(),
  69              );
  70          $expectedq->incorrectfeedback = array(
  71                  'text' => '',
  72                  'format' => FORMAT_HTML,
  73                  'files' => array(),
  74                  );
  75          $expectedq->generalfeedback = '';
  76          $expectedq->generalfeedbackformat = FORMAT_MOODLE;
  77          $expectedq->defaultmark = 1;
  78          $expectedq->length = 1;
  79          $expectedq->penalty = 0.3333333;
  80          $expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
  81          $expectedq->subquestions = array(
  82              1 => array('text' => 'cat', 'format' => FORMAT_HTML),
  83              2 => array('text' => 'frog', 'format' => FORMAT_HTML),
  84              3 => array('text' => 'newt', 'format' => FORMAT_HTML));
  85          $expectedq->subanswers = array(1 => 'mammal', 2 => 'amphibian', 3 => 'amphibian');
  86  
  87          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
  88      }
  89  
  90      public function test_import_multichoice_single() {
  91  
  92          $txt = $this->make_test();
  93  
  94          $importer = new qformat_webct();
  95          $questions = $importer->readquestions($txt);
  96          $q = $questions[1];
  97  
  98          $expectedq = new stdClass();
  99          $expectedq->qtype = 'multichoice';
 100          $expectedq->single = 1;
 101          $expectedq->name = 'USER-2';
 102          $expectedq->questiontext = '<font size="+1">What\'s between orange and green in the spectrum?</font>';
 103          $expectedq->questiontextformat = FORMAT_HTML;
 104          $expectedq->correctfeedback = array(
 105                  'text' => '',
 106                  'format' => FORMAT_HTML,
 107                  'files' => array(),
 108              );
 109          $expectedq->partiallycorrectfeedback = array(
 110                  'text' => '',
 111                  'format' => FORMAT_HTML,
 112                  'files' => array(),
 113              );
 114          $expectedq->incorrectfeedback = array(
 115                  'text' => '',
 116                  'format' => FORMAT_HTML,
 117                  'files' => array(),
 118                  );
 119          $expectedq->generalfeedback = '';
 120          $expectedq->generalfeedbackformat = FORMAT_MOODLE;
 121          $expectedq->defaultmark = 1;
 122          $expectedq->length = 1;
 123          $expectedq->penalty = 0.3333333;
 124          $expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
 125          $expectedq->answer = array(
 126                  1 => array(
 127                      'text' => 'red',
 128                      'format' => FORMAT_HTML,
 129                  ),
 130                  2 => array(
 131                      'text' => 'yellow',
 132                      'format' => FORMAT_HTML,
 133                  ),
 134                  3 => array(
 135                      'text' => 'blue',
 136                      'format' => FORMAT_HTML,
 137                  )
 138              );
 139          $expectedq->fraction = array(
 140                  1 => 0,
 141                  2 => 1,
 142                  3 => 0,
 143              );
 144          $expectedq->feedback = array(
 145                  1 => array(
 146                      'text' => 'Red is not between orange and green in the spectrum but yellow is.',
 147                      'format' => FORMAT_HTML,
 148                  ),
 149                  2 => array(
 150                      'text' => 'You gave the right answer.',
 151                      'format' => FORMAT_HTML,
 152                  ),
 153                  3 => array(
 154                      'text' => 'Blue is not between orange and green in the spectrum but yellow is.',
 155                      'format' => FORMAT_HTML,
 156                  )
 157              );
 158  
 159          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 160      }
 161  
 162      public function test_import_multichoice_multi() {
 163  
 164          $txt = $this->make_test();
 165  
 166          $importer = new qformat_webct();
 167          $questions = $importer->readquestions($txt);
 168          $q = $questions[2];
 169  
 170          $expectedq = new stdClass();
 171          $expectedq->qtype = 'multichoice';
 172          $expectedq->single = 0;
 173          $expectedq->name = 'USER-3';
 174          $expectedq->questiontext = '<i>What\'s between orange and green in the spectrum?</i>';
 175          $expectedq->questiontextformat = FORMAT_HTML;
 176          $expectedq->correctfeedback = array(
 177                  'text' => '',
 178                  'format' => FORMAT_HTML,
 179                  'files' => array(),
 180              );
 181          $expectedq->partiallycorrectfeedback = array(
 182                  'text' => '',
 183                  'format' => FORMAT_HTML,
 184                  'files' => array(),
 185              );
 186          $expectedq->incorrectfeedback = array(
 187                  'text' => '',
 188                  'format' => FORMAT_HTML,
 189                  'files' => array(),
 190                  );
 191          $expectedq->generalfeedback = '';
 192          $expectedq->generalfeedbackformat = FORMAT_MOODLE;
 193          $expectedq->defaultmark = 1;
 194          $expectedq->length = 1;
 195          $expectedq->penalty = 0.3333333;
 196          $expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
 197          $expectedq->answer = array(
 198                  1 => array(
 199                      'text' => 'yellow',
 200                      'format' => FORMAT_HTML,
 201                  ),
 202                  2 => array(
 203                      'text' => 'red',
 204                      'format' => FORMAT_HTML,
 205                  ),
 206                  3 => array(
 207                      'text' => 'off-beige',
 208                      'format' => FORMAT_HTML,
 209                  ),
 210                  4 => array(
 211                      'text' => 'blue',
 212                      'format' => FORMAT_HTML,
 213                  )
 214              );
 215          $expectedq->fraction = array(
 216                  1 => 0.5,
 217                  2 => 0,
 218                  3 => 0.5,
 219                  4 => 0,
 220              );
 221          $expectedq->feedback = array(
 222                  1 => array(
 223                      'text' => 'True, yellow is between orange and green in the spectrum,',
 224                      'format' => FORMAT_HTML,
 225                  ),
 226                  2 => array(
 227                      'text' => 'False, red is not between orange and green in the spectrum,',
 228                      'format' => FORMAT_HTML,
 229                  ),
 230                  3 => array(
 231                      'text' => 'True, off-beige is between orange and green in the spectrum,',
 232                      'format' => FORMAT_HTML,
 233                  ),
 234                  4 => array(
 235                      'text' => 'False, red is not between orange and green in the spectrum,',
 236                      'format' => FORMAT_HTML,
 237                  )
 238              );
 239  
 240          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 241      }
 242  
 243      public function test_import_truefalse() {
 244  
 245          $txt = $this->make_test();
 246  
 247          $importer = new qformat_webct();
 248          $questions = $importer->readquestions($txt);
 249          $q = $questions[0];
 250  
 251          $expectedq = new stdClass();
 252          $expectedq->qtype = 'multichoice';
 253          $expectedq->single = 1;
 254          $expectedq->name = 'USER-1';
 255          $expectedq->questiontext = '42 is the Absolute Answer to everything.';
 256          $expectedq->questiontextformat = FORMAT_HTML;
 257          $expectedq->correctfeedback = array(
 258                  'text' => '',
 259                  'format' => FORMAT_HTML,
 260                  'files' => array(),
 261              );
 262          $expectedq->partiallycorrectfeedback = array(
 263                  'text' => '',
 264                  'format' => FORMAT_HTML,
 265                  'files' => array(),
 266              );
 267          $expectedq->incorrectfeedback = array(
 268                  'text' => '',
 269                  'format' => FORMAT_HTML,
 270                  'files' => array(),
 271                  );
 272          $expectedq->generalfeedback = '';
 273          $expectedq->generalfeedbackformat = FORMAT_MOODLE;
 274          $expectedq->defaultmark = 1;
 275          $expectedq->length = 1;
 276          $expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
 277          $expectedq->answer = array(
 278                  1 => array(
 279                      'text' => 'True',
 280                      'format' => FORMAT_HTML,
 281                  ),
 282                  2 => array(
 283                      'text' => 'False',
 284                      'format' => FORMAT_HTML,
 285                  ),
 286              );
 287          $expectedq->fraction = array(
 288                  1 => 0,
 289                  2 => 1,
 290              );
 291          $expectedq->feedback = array(
 292                  1 => array(
 293                      'text' => '42 is the <b>Ultimate</b> Answer.',
 294                      'format' => FORMAT_HTML,
 295                  ),
 296                  2 => array(
 297                      'text' => '42 is the <b>Ultimate</b> Answer.',
 298                      'format' => FORMAT_HTML,
 299                  ),
 300              );
 301  
 302          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 303      }
 304  
 305      public function test_import_fill_in_the_blank() {
 306  
 307          $txt = $this->make_test();
 308  
 309          $importer = new qformat_webct();
 310          $questions = $importer->readquestions($txt);
 311          $q = $questions[4];
 312  
 313          $expectedq = new stdClass();
 314          $expectedq->qtype = 'shortanswer';
 315          $expectedq->name = 'USER-5';
 316          $expectedq->questiontext = 'Name an amphibian&#58; __________';
 317          $expectedq->questiontextformat = FORMAT_HTML;
 318          $expectedq->generalfeedback = 'A frog is an amphibian';
 319          $expectedq->generalfeedbackformat = FORMAT_HTML;
 320          $expectedq->defaultmark = 1;
 321          $expectedq->length = 1;
 322          $expectedq->usecase = 0;
 323          $expectedq->answer = array(
 324                  1 => 'frog',
 325              );
 326          $expectedq->fraction = array(
 327                  1 => 1,
 328              );
 329          $expectedq->feedback = array(
 330                  1 => array(
 331                      'text' => '',
 332                      'format' => FORMAT_HTML,
 333                  ),
 334              );
 335  
 336          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 337      }
 338  
 339      public function test_import_essay() {
 340  
 341          $txt = $this->make_test();
 342  
 343          $importer = new qformat_webct();
 344          $questions = $importer->readquestions($txt);
 345          $q = $questions[5];
 346  
 347          $expectedq = new stdClass();
 348          $expectedq->qtype = 'essay';
 349          $expectedq->name = 'USER-6';
 350          $expectedq->questiontext = 'How are you?';
 351          $expectedq->questiontextformat = FORMAT_HTML;
 352          $expectedq->generalfeedback = '';
 353          $expectedq->generalfeedbackformat = FORMAT_HTML;
 354          $expectedq->defaultmark = 1;
 355          $expectedq->length = 1;
 356          $expectedq->responseformat = 'editor';
 357          $expectedq->responsefieldlines = 15;
 358          $expectedq->attachments = 0;
 359          $expectedq->graderinfo = array(
 360                  'text' => 'Blackboard answer for essay questions will be imported as informations for graders.',
 361                  'format' => FORMAT_HTML,
 362              );
 363  
 364          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 365      }
 366  }