Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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 the Moodle Examview format.
  19   *
  20   * @package    qformat_examview
  21   * @copyright  2012 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/examview/format.php');
  32  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  33  
  34  
  35  /**
  36   * Unit tests for the examview question import format.
  37   *
  38   * @copyright  2012 Jean-Michel Vedrine
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class qformat_examview_test extends question_testcase {
  42  
  43      public function make_test_xml() {
  44          $xml = $xml = file_get_contents(__DIR__ . '/fixtures/examview_sample.xml');
  45          return array(0=>$xml);
  46      }
  47  
  48      public function test_import_truefalse() {
  49  
  50          $xml = $this->make_test_xml();
  51          $questions = array();
  52  
  53          $importer = new qformat_examview();
  54          $questions = $importer->readquestions($xml);
  55          $q = $questions[0];
  56  
  57          $expectedq = new stdClass();
  58          $expectedq->qtype = 'truefalse';
  59          $expectedq->name = '42 is the Absolute Answer to everything.';
  60          $expectedq->questiontext = "42 is the Absolute Answer to everything.";
  61          $expectedq->questiontextformat = FORMAT_HTML;
  62          $expectedq->generalfeedback = '';
  63          $expectedq->generalfeedbackformat = FORMAT_MOODLE;
  64          $expectedq->defaultmark = 1;
  65          $expectedq->length = 1;
  66          $expectedq->correctanswer = 0;
  67          $expectedq->feedbacktrue = array(
  68                  'text' => get_string('incorrect', 'question'),
  69                  'format' => FORMAT_HTML,
  70                  'files' => array(),
  71              );
  72          $expectedq->feedbackfalse = array(
  73                  'text' => get_string('correct', 'question'),
  74                  'format' => FORMAT_HTML,
  75                  'files' => array(),
  76              );
  77  
  78          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
  79      }
  80      public function test_import_multichoice_single() {
  81          $xml = $this->make_test_xml();
  82          $questions = array();
  83  
  84          $importer = new qformat_examview();
  85          $questions = $importer->readquestions($xml);
  86          $q = $questions[1];
  87  
  88          $expectedq = new stdClass();
  89          $expectedq->qtype = 'multichoice';
  90          $expectedq->single = 1;
  91          $expectedq->name = "What's between orange and green in the spectrum?";
  92          $expectedq->questiontext = "What's between orange and green in the spectrum?";
  93          $expectedq->questiontextformat = FORMAT_HTML;
  94          $expectedq->correctfeedback = array('text' => '',
  95                  'format' => FORMAT_HTML, 'files' => array());
  96          $expectedq->partiallycorrectfeedback = array('text' => '',
  97                  'format' => FORMAT_HTML, 'files' => array());
  98          $expectedq->incorrectfeedback = array('text' => '',
  99                  'format' => FORMAT_HTML, 'files' => array());
 100          $expectedq->generalfeedback = '';
 101          $expectedq->generalfeedbackformat = FORMAT_MOODLE;
 102          $expectedq->defaultmark = 1;
 103          $expectedq->length = 1;
 104          $expectedq->penalty = 0.3333333;
 105          $expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
 106          $expectedq->answer = array(
 107                  0 => array(
 108                      'text' => 'red',
 109                      'format' => FORMAT_HTML,
 110                      'files' => array(),
 111                  ),
 112                  1 => array(
 113                      'text' => 'yellow',
 114                      'format' => FORMAT_HTML,
 115                      'files' => array(),
 116                  ),
 117                  2 => array(
 118                      'text' => 'blue',
 119                      'format' => FORMAT_HTML,
 120                      'files' => array(),
 121                  )
 122              );
 123          $expectedq->fraction = array(0, 1, 0);
 124          $expectedq->feedback = array(
 125                  0 => array(
 126                      'text' => get_string('incorrect', 'question'),
 127                      'format' => FORMAT_HTML,
 128                      'files' => array(),
 129                  ),
 130                  1 => array(
 131                      'text' => get_string('correct', 'question'),
 132                      'format' => FORMAT_HTML,
 133                      'files' => array(),
 134                  ),
 135                  2 => array(
 136                      'text' => get_string('incorrect', 'question'),
 137                      'format' => FORMAT_HTML,
 138                      'files' => array(),
 139                  )
 140              );
 141  
 142          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 143      }
 144  
 145      public function test_import_numerical() {
 146  
 147          $xml = $this->make_test_xml();
 148          $questions = array();
 149  
 150          $importer = new qformat_examview();
 151          $questions = $importer->readquestions($xml);
 152          $q = $questions[2];
 153  
 154          $expectedq = new stdClass();
 155          $expectedq->qtype = 'numerical';
 156          $expectedq->name = 'This is a numeric response question.  How much is 12 * 2?';
 157          $expectedq->questiontext = 'This is a numeric response question.  How much is 12 * 2?';
 158          $expectedq->questiontextformat = FORMAT_HTML;
 159          $expectedq->generalfeedback = '';
 160          $expectedq->generalfeedbackformat = FORMAT_MOODLE;
 161          $expectedq->defaultmark = 1;
 162          $expectedq->length = 1;
 163          $expectedq->penalty = 0.3333333;
 164          $expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
 165          $expectedq->answer = array('24');
 166          $expectedq->fraction = array(1);
 167          $expectedq->feedback = array(
 168                  0 => array(
 169                      'text' => get_string('correct', 'question'),
 170                      'format' => FORMAT_HTML,
 171                      'files' => array(),
 172                  ),
 173              );
 174  
 175          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 176      }
 177  
 178  
 179  
 180      public function test_import_fill_in_the_blank() {
 181  
 182          $xml = $this->make_test_xml();
 183          $questions = array();
 184  
 185          $importer = new qformat_examview();
 186          $questions = $importer->readquestions($xml);
 187          $q = $questions[3];
 188  
 189          $expectedq = new stdClass();
 190          $expectedq->qtype = 'shortanswer';
 191          $expectedq->name = 'Name an amphibian: __________.';
 192          $expectedq->questiontext = 'Name an amphibian: __________.';
 193          $expectedq->questiontextformat = FORMAT_HTML;
 194          $expectedq->generalfeedback = '';
 195          $expectedq->generalfeedbackformat = FORMAT_MOODLE;
 196          $expectedq->defaultmark = 1;
 197          $expectedq->length = 1;
 198          $expectedq->usecase = 0;
 199          $expectedq->answer = array('frog', '*');
 200          $expectedq->fraction = array(1, 0);
 201          $expectedq->feedback = array(
 202                  0 => array(
 203                      'text' => get_string('correct', 'question'),
 204                      'format' => FORMAT_HTML,
 205                      'files' => array(),
 206                  ),
 207                  1 => array(
 208                      'text' => get_string('incorrect', 'question'),
 209                      'format' => FORMAT_HTML,
 210                      'files' => array(),
 211                  )
 212              );
 213  
 214          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 215      }
 216  
 217      public function test_import_essay() {
 218  
 219          $xml = $this->make_test_xml();
 220          $questions = array();
 221  
 222          $importer = new qformat_examview();
 223          $questions = $importer->readquestions($xml);
 224          $q = $questions[4];
 225  
 226          $expectedq = new stdClass();
 227          $expectedq->qtype = 'essay';
 228          $expectedq->name = 'How are you?';
 229          $expectedq->questiontext = 'How are you?';
 230          $expectedq->questiontextformat = FORMAT_HTML;
 231          $expectedq->generalfeedback = '';
 232          $expectedq->generalfeedbackformat = FORMAT_MOODLE;
 233          $expectedq->defaultmark = 1;
 234          $expectedq->length = 1;
 235          $expectedq->responseformat = 'editor';
 236          $expectedq->responsefieldlines = 15;
 237          $expectedq->attachments = 0;
 238          $expectedq->graderinfo = array(
 239                  'text' => 'Examview answer for essay questions will be imported as informations for graders.',
 240                  'format' => FORMAT_HTML,
 241                  'files' => array());
 242  
 243          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 244      }
 245  
 246      // Due to the way matching questions are parsed,
 247      // the test for matching questions is somewhat different.
 248      // First we test the parse_matching_groups method alone.
 249      // Then we test the whole process wich involve parse_matching_groups,
 250      // parse_ma and process_matches methods.
 251      public function test_parse_matching_groups() {
 252          $lines = $this->make_test_xml();
 253  
 254          $importer = new qformat_examview();
 255          $text = implode(' ', $lines);
 256  
 257          $xml = xmlize($text, 0);
 258          $importer->parse_matching_groups($xml['examview']['#']['matching-group']);
 259          $matching = $importer->matching_questions;
 260          $group = new stdClass();
 261          $group->questiontext = 'Classify the animals.';
 262          $group->subchoices = array('A' => 'insect', 'B' => 'amphibian', 'C' =>'mammal');
 263              $group->subquestions = array();
 264              $group->subanswers = array();
 265          $expectedmatching = array( 'Matching 1' => $group);
 266  
 267          $this->assertEquals($matching, $expectedmatching);
 268      }
 269  
 270      public function test_import_match() {
 271  
 272          $xml = $this->make_test_xml();
 273          $questions = array();
 274  
 275          $importer = new qformat_examview();
 276          $questions = $importer->readquestions($xml);
 277          $q = $questions[5];
 278  
 279          $expectedq = new stdClass();
 280          $expectedq->qtype = 'match';
 281          $expectedq->name = 'Classify the animals.';
 282          $expectedq->questiontext = 'Classify the animals.';
 283          $expectedq->questiontextformat = FORMAT_HTML;
 284          $expectedq->correctfeedback = array('text' => '',
 285                  'format' => FORMAT_HTML, 'files' => array());
 286          $expectedq->partiallycorrectfeedback = array('text' => '',
 287                  'format' => FORMAT_HTML, 'files' => array());
 288          $expectedq->incorrectfeedback = array('text' => '',
 289                  'format' => FORMAT_HTML, 'files' => array());
 290          $expectedq->generalfeedback = '';
 291          $expectedq->generalfeedbackformat = FORMAT_MOODLE;
 292          $expectedq->defaultmark = 1;
 293          $expectedq->length = 1;
 294          $expectedq->penalty = 0.3333333;
 295          $expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
 296          $expectedq->subquestions = array(
 297              array('text' => '', 'format' => FORMAT_HTML, 'files' => array()),
 298              array('text' => 'frog', 'format' => FORMAT_HTML, 'files' => array()),
 299              array('text' => 'newt', 'format' => FORMAT_HTML, 'files' => array()),
 300              array('text' => 'cat', 'format' => FORMAT_HTML, 'files' => array()));
 301          $expectedq->subanswers = array('insect', 'amphibian', 'amphibian', 'mammal');
 302  
 303          $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
 304      }
 305  }