Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403]

   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   * Unit tests for export/import description (info) for question category in the Moodle XML format.
  18   *
  19   * @package    qformat_xml
  20   * @copyright  2014 Nikita Nikitsky, Volgograd State Technical University
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  use core_question\local\bank\question_edit_contexts;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  global $CFG;
  28  require_once($CFG->libdir . '/questionlib.php');
  29  require_once($CFG->dirroot . '/question/format/xml/format.php');
  30  require_once($CFG->dirroot . '/question/format.php');
  31  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  32  require_once($CFG->dirroot . '/question/editlib.php');
  33  
  34  /**
  35   * Unit tests for the XML question format import and export.
  36   *
  37   * @copyright  2014 Nikita Nikitsky, Volgograd State Technical University
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class qformat_xml_import_export_test extends advanced_testcase {
  41      /**
  42       * Create object qformat_xml for test.
  43       * @param string $filename with name for testing file.
  44       * @param stdClass $course
  45       * @return qformat_xml XML question format object.
  46       */
  47      public function create_qformat($filename, $course) {
  48          $qformat = new qformat_xml();
  49          $qformat->setContexts((new question_edit_contexts(context_course::instance($course->id)))->all());
  50          $qformat->setCourse($course);
  51          $qformat->setFilename(__DIR__ . '/fixtures/' . $filename);
  52          $qformat->setRealfilename($filename);
  53          $qformat->setMatchgrades('error');
  54          $qformat->setCatfromfile(1);
  55          $qformat->setContextfromfile(1);
  56          $qformat->setStoponerror(1);
  57          $qformat->setCattofile(1);
  58          $qformat->setContexttofile(1);
  59          $qformat->set_display_progress(false);
  60  
  61          return $qformat;
  62      }
  63  
  64      /**
  65       * Check xml for compliance.
  66       * @param string $expectedxml with correct string.
  67       * @param string $xml you want to check.
  68       */
  69      public function assert_same_xml($expectedxml, $xml) {
  70          $this->assertEquals($this->normalise_xml($expectedxml),
  71                  $this->normalise_xml($xml));
  72      }
  73  
  74      /**
  75       * Clean up some XML to remove irrelevant differences, before it is compared.
  76       * @param string $xml some XML.
  77       * @return string cleaned-up XML.
  78       */
  79      protected function normalise_xml($xml) {
  80          // Normalise line endings.
  81          $xml = str_replace("\r\n", "\n", $xml);
  82          $xml = preg_replace("~\n$~", "", $xml); // Strip final newline in file.
  83  
  84          // Replace all numbers in question id comments with 0.
  85          $xml = preg_replace('~(?<=<!-- question: )([0-9]+)(?=  -->)~', '0', $xml);
  86  
  87          // Deal with how different databases output numbers. Only match when only thing in a tag.
  88          $xml = preg_replace("~>.0000000<~", '>0<', $xml); // How Oracle outputs 0.0000000.
  89          $xml = preg_replace("~(\.(:?[0-9]*[1-9])?)0*<~", '$1<', $xml); // Other cases of trailing 0s
  90          $xml = preg_replace("~([0-9]).<~", '$1<', $xml); // Stray . in 1. after last step.
  91  
  92          return $xml;
  93      }
  94  
  95      /**
  96       * Check imported category.
  97       * @param string $name imported category name.
  98       * @param string $info imported category info field (description of category).
  99       * @param int $infoformat imported category info field format.
 100       */
 101      public function assert_category_imported($name, $info, $infoformat, $idnumber = null) {
 102          global $DB;
 103          $category = $DB->get_record('question_categories', ['name' => $name], '*', MUST_EXIST);
 104          $this->assertEquals($info, $category->info);
 105          $this->assertEquals($infoformat, $category->infoformat);
 106          $this->assertSame($idnumber, $category->idnumber);
 107      }
 108  
 109      /**
 110       * Check a question category has a given parent.
 111       * @param string $catname Name of the question category
 112       * @param string $parentname Name of the parent category
 113       * @throws dml_exception
 114       */
 115      public function assert_category_has_parent($catname, $parentname) {
 116          global $DB;
 117          $sql = 'SELECT qc1.*
 118                    FROM {question_categories} qc1
 119                    JOIN {question_categories} qc2 ON qc1.parent = qc2.id
 120                   WHERE qc1.name = ?
 121                     AND qc2.name = ?';
 122          $categories = $DB->get_records_sql($sql, [$catname, $parentname]);
 123          $this->assertTrue(count($categories) == 1);
 124      }
 125  
 126      /**
 127       * Check a question exists in a category.
 128       * @param string $qname The name of the question
 129       * @param string $catname The name of the category
 130       * @throws dml_exception
 131       */
 132      public function assert_question_in_category($qname, $catname) {
 133          global $DB;
 134  
 135          $sql = "SELECT q.*, qbe.questioncategoryid AS category
 136                    FROM {question} q
 137                    JOIN {question_versions} qv ON qv.questionid = q.id
 138                    JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
 139                   WHERE q.name = :name";
 140          $question = $DB->get_record_sql($sql, ['name' => $qname], MUST_EXIST);
 141          $category = $DB->get_record('question_categories', ['name' => $catname], '*', MUST_EXIST);
 142          $this->assertEquals($category->id, $question->category);
 143      }
 144  
 145      /**
 146       * Simple check for importing a category with a description.
 147       */
 148      public function test_import_category() {
 149          $this->resetAfterTest();
 150          $course = $this->getDataGenerator()->create_course();
 151          $this->setAdminUser();
 152          $qformat = $this->create_qformat('category_with_description.xml', $course);
 153          $imported = $qformat->importprocess();
 154          $this->assertTrue($imported);
 155          $this->assert_category_imported('Alpha',
 156                  'This is Alpha category for test', FORMAT_MOODLE, 'alpha-idnumber');
 157          $this->assert_category_has_parent('Alpha', 'top');
 158      }
 159  
 160      /**
 161       * Check importing nested categories.
 162       */
 163      public function test_import_nested_categories() {
 164          $this->resetAfterTest();
 165          $course = $this->getDataGenerator()->create_course();
 166          $this->setAdminUser();
 167          $qformat = $this->create_qformat('nested_categories.xml', $course);
 168          $imported = $qformat->importprocess();
 169          $this->assertTrue($imported);
 170          $this->assert_category_imported('Delta', 'This is Delta category for test', FORMAT_PLAIN);
 171          $this->assert_category_imported('Epsilon', 'This is Epsilon category for test', FORMAT_MARKDOWN);
 172          $this->assert_category_imported('Zeta', 'This is Zeta category for test', FORMAT_MOODLE);
 173          $this->assert_category_has_parent('Delta', 'top');
 174          $this->assert_category_has_parent('Epsilon', 'Delta');
 175          $this->assert_category_has_parent('Zeta', 'Epsilon');
 176      }
 177  
 178      /**
 179       * Check importing nested categories contain the right questions.
 180       */
 181      public function test_import_nested_categories_with_questions() {
 182          $this->resetAfterTest();
 183          $course = $this->getDataGenerator()->create_course();
 184          $this->setAdminUser();
 185          $qformat = $this->create_qformat('nested_categories_with_questions.xml', $course);
 186          $imported = $qformat->importprocess();
 187          $this->assertTrue($imported);
 188          $this->assert_category_imported('Iota', 'This is Iota category for test', FORMAT_PLAIN);
 189          $this->assert_category_imported('Kappa', 'This is Kappa category for test', FORMAT_MARKDOWN);
 190          $this->assert_category_imported('Lambda', 'This is Lambda category for test', FORMAT_MOODLE);
 191          $this->assert_category_imported('Mu', 'This is Mu category for test', FORMAT_MOODLE);
 192          $this->assert_question_in_category('Iota Question', 'Iota');
 193          $this->assert_question_in_category('Kappa Question', 'Kappa');
 194          $this->assert_question_in_category('Lambda Question', 'Lambda');
 195          $this->assert_question_in_category('Mu Question', 'Mu');
 196          $this->assert_category_has_parent('Iota', 'top');
 197          $this->assert_category_has_parent('Kappa', 'Iota');
 198          $this->assert_category_has_parent('Lambda', 'Kappa');
 199          $this->assert_category_has_parent('Mu', 'Iota');
 200      }
 201  
 202      /**
 203       * Check import of an old file (without format), for backward compatability.
 204       */
 205      public function test_import_old_format() {
 206          $this->resetAfterTest();
 207          $course = $this->getDataGenerator()->create_course();
 208          $this->setAdminUser();
 209          $qformat = $this->create_qformat('old_format_file.xml', $course);
 210          $imported = $qformat->importprocess();
 211          $this->assertTrue($imported);
 212          $this->assert_category_imported('Pi', '', FORMAT_MOODLE);
 213          $this->assert_category_imported('Rho', '', FORMAT_MOODLE);
 214          $this->assert_question_in_category('Pi Question', 'Pi');
 215          $this->assert_question_in_category('Rho Question', 'Rho');
 216          $this->assert_category_has_parent('Pi', 'top');
 217          $this->assert_category_has_parent('Rho', 'Pi');
 218      }
 219  
 220      /**
 221       * Check the import of an xml file where the child category exists before the parent category.
 222       */
 223      public function test_import_categories_in_reverse_order() {
 224          $this->resetAfterTest();
 225          $course = $this->getDataGenerator()->create_course();
 226          $this->setAdminUser();
 227          $qformat = $this->create_qformat('categories_reverse_order.xml', $course);
 228          $imported = $qformat->importprocess();
 229          $this->assertTrue($imported);
 230          $this->assert_category_imported('Sigma', 'This is Sigma category for test', FORMAT_HTML);
 231          $this->assert_category_imported('Tau', 'This is Tau category for test', FORMAT_HTML);
 232          $this->assert_question_in_category('Sigma Question', 'Sigma');
 233          $this->assert_question_in_category('Tau Question', 'Tau');
 234          $this->assert_category_has_parent('Sigma', 'top');
 235          $this->assert_category_has_parent('Tau', 'Sigma');
 236      }
 237  
 238      /**
 239       * Simple check for exporting a category.
 240       */
 241      public function test_export_category() {
 242          global $SITE;
 243  
 244          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 245          $this->resetAfterTest();
 246          $this->setAdminUser();
 247          // Note while this loads $qformat with all the 'right' data from the xml file,
 248          // the call to setCategory, followed by exportprocess will actually only export data
 249          // from the database (created by the generator).
 250          $qformat = $this->create_qformat('export_category.xml', $SITE);
 251  
 252          $category = $generator->create_question_category([
 253                  'name' => 'Alpha',
 254                  'contextid' => context_course::instance($SITE->id)->id,
 255                  'info' => 'This is Alpha category for test',
 256                  'infoformat' => '0',
 257                  'idnumber' => 'alpha-idnumber',
 258                  'stamp' => make_unique_id_code(),
 259                  'parent' => '0',
 260                  'sortorder' => '999']);
 261          $question = $generator->create_question('truefalse', null, [
 262                  'category' => $category->id,
 263                  'name' => 'Alpha Question',
 264                  'questiontext' => ['format' => '1', 'text' => '<p>Testing Alpha Question</p>'],
 265                  'generalfeedback' => ['format' => '1', 'text' => ''],
 266                  'correctanswer' => '1',
 267                  'feedbacktrue' => ['format' => '1', 'text' => ''],
 268                  'feedbackfalse' => ['format' => '1', 'text' => ''],
 269                  'penalty' => '1']);
 270          $qformat->setCategory($category);
 271  
 272          $expectedxml = file_get_contents(__DIR__ . '/fixtures/export_category.xml');
 273          $this->assert_same_xml($expectedxml, $qformat->exportprocess());
 274      }
 275  
 276      /**
 277       * Check exporting nested categories.
 278       */
 279      public function test_export_nested_categories() {
 280          global $SITE;
 281  
 282          $this->resetAfterTest();
 283          $this->setAdminUser();
 284          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 285          $qformat = $this->create_qformat('nested_categories.zml', $SITE);
 286  
 287          $categorydelta = $generator->create_question_category([
 288                  'name' => 'Delta',
 289                  'contextid' => context_course::instance($SITE->id)->id,
 290                  'info' => 'This is Delta category for test',
 291                  'infoformat' => '2',
 292                  'stamp' => make_unique_id_code(),
 293                  'parent' => '0',
 294                  'sortorder' => '999']);
 295          $categoryepsilon = $generator->create_question_category([
 296                  'name' => 'Epsilon',
 297                  'contextid' => context_course::instance($SITE->id)->id,
 298                  'info' => 'This is Epsilon category for test',
 299                  'infoformat' => '4',
 300                  'stamp' => make_unique_id_code(),
 301                  'parent' => $categorydelta->id,
 302                  'sortorder' => '999']);
 303          $categoryzeta = $generator->create_question_category([
 304                  'name' => 'Zeta',
 305                  'contextid' => context_course::instance($SITE->id)->id,
 306                  'info' => 'This is Zeta category for test',
 307                  'infoformat' => '0',
 308                  'stamp' => make_unique_id_code(),
 309                  'parent' => $categoryepsilon->id,
 310                  'sortorder' => '999']);
 311          $question  = $generator->create_question('truefalse', null, [
 312                  'category' => $categoryzeta->id,
 313                  'name' => 'Zeta Question',
 314                  'questiontext' => [
 315                                  'format' => '1',
 316                                  'text' => '<p>Testing Zeta Question</p>'],
 317                  'generalfeedback' => ['format' => '1', 'text' => ''],
 318                  'correctanswer' => '1',
 319                  'feedbacktrue' => ['format' => '1', 'text' => ''],
 320                  'feedbackfalse' => ['format' => '1', 'text' => ''],
 321                  'penalty' => '1']);
 322          $qformat->setCategory($categorydelta);
 323          $qformat->setCategory($categoryepsilon);
 324          $qformat->setCategory($categoryzeta);
 325  
 326          $expectedxml = file_get_contents(__DIR__ . '/fixtures/nested_categories.xml');
 327          $this->assert_same_xml($expectedxml, $qformat->exportprocess());
 328      }
 329  
 330      /**
 331       * Check exporting nested categories contain the right questions.
 332       */
 333      public function test_export_nested_categories_with_questions() {
 334          global $SITE;
 335  
 336          $this->resetAfterTest();
 337          $this->setAdminUser();
 338          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 339          $qformat = $this->create_qformat('nested_categories_with_questions.xml', $SITE);
 340  
 341          $categoryiota = $generator->create_question_category([
 342                  'name' => 'Iota',
 343                  'contextid' => context_course::instance($SITE->id)->id,
 344                  'info' => 'This is Iota category for test',
 345                  'infoformat' => '2',
 346                  'stamp' => make_unique_id_code(),
 347                  'parent' => '0',
 348                  'sortorder' => '999']);
 349          $iotaquestion  = $generator->create_question('truefalse', null, [
 350                  'category' => $categoryiota->id,
 351                  'name' => 'Iota Question',
 352                  'questiontext' => [
 353                          'format' => '1',
 354                          'text' => '<p>Testing Iota Question</p>'],
 355                  'generalfeedback' => ['format' => '1', 'text' => ''],
 356                  'correctanswer' => '1',
 357                  'feedbacktrue' => ['format' => '1', 'text' => ''],
 358                  'feedbackfalse' => ['format' => '1', 'text' => ''],
 359                  'penalty' => '1']);
 360          $categorykappa = $generator->create_question_category([
 361                  'name' => 'Kappa',
 362                  'contextid' => context_course::instance($SITE->id)->id,
 363                  'info' => 'This is Kappa category for test',
 364                  'infoformat' => '4',
 365                  'stamp' => make_unique_id_code(),
 366                  'parent' => $categoryiota->id,
 367                  'sortorder' => '999']);
 368          $kappaquestion  = $generator->create_question('essay', null, [
 369                  'category' => $categorykappa->id,
 370                  'name' => 'Kappa Essay Question',
 371                  'questiontext' => ['text' => 'Testing Kappa Essay Question'],
 372                  'generalfeedback' => '',
 373                  'responseformat' => 'editor',
 374                  'responserequired' => 1,
 375                  'responsefieldlines' => 10,
 376                  'attachments' => 0,
 377                  'attachmentsrequired' => 0,
 378                  'graderinfo' => ['format' => '1', 'text' => ''],
 379                  'responsetemplate' => ['format' => '1', 'text' => ''],
 380                  'idnumber' => '']);
 381          $kappaquestion1  = $generator->create_question('truefalse', null, [
 382                  'category' => $categorykappa->id,
 383                  'name' => 'Kappa Question',
 384                  'questiontext' => [
 385                          'format' => '1',
 386                          'text' => '<p>Testing Kappa Question</p>'],
 387                  'generalfeedback' => ['format' => '1', 'text' => ''],
 388                  'correctanswer' => '1',
 389                  'feedbacktrue' => ['format' => '1', 'text' => ''],
 390                  'feedbackfalse' => ['format' => '1', 'text' => ''],
 391                  'penalty' => '1',
 392                  'idnumber' => '']);
 393          $categorylambda = $generator->create_question_category([
 394                  'name' => 'Lambda',
 395                  'contextid' => context_course::instance($SITE->id)->id,
 396                  'info' => 'This is Lambda category for test',
 397                  'infoformat' => '0',
 398                  'stamp' => make_unique_id_code(),
 399                  'parent' => $categorykappa->id,
 400                  'sortorder' => '999']);
 401          $lambdaquestion  = $generator->create_question('truefalse', null, [
 402                  'category' => $categorylambda->id,
 403                  'name' => 'Lambda Question',
 404                  'questiontext' => [
 405                          'format' => '1',
 406                          'text' => '<p>Testing Lambda Question</p>'],
 407                  'generalfeedback' => ['format' => '1', 'text' => ''],
 408                  'correctanswer' => '1',
 409                  'feedbacktrue' => ['format' => '1', 'text' => ''],
 410                  'feedbackfalse' => ['format' => '1', 'text' => ''],
 411                  'penalty' => '1']);
 412          $categorymu = $generator->create_question_category([
 413                  'name' => 'Mu',
 414                  'contextid' => context_course::instance($SITE->id)->id,
 415                  'info' => 'This is Mu category for test',
 416                  'infoformat' => '0',
 417                  'stamp' => make_unique_id_code(),
 418                  'parent' => $categoryiota->id,
 419                  'sortorder' => '999']);
 420          $muquestion  = $generator->create_question('truefalse', null, [
 421                  'category' => $categorymu->id,
 422                  'name' => 'Mu Question',
 423                  'questiontext' => [
 424                          'format' => '1',
 425                          'text' => '<p>Testing Mu Question</p>'],
 426                  'generalfeedback' => ['format' => '1', 'text' => ''],
 427                  'correctanswer' => '1',
 428                  'feedbacktrue' => ['format' => '1', 'text' => ''],
 429                  'feedbackfalse' => ['format' => '1', 'text' => ''],
 430                  'penalty' => '1']);
 431          $qformat->setCategory($categoryiota);
 432  
 433          $expectedxml = file_get_contents(__DIR__ . '/fixtures/nested_categories_with_questions.xml');
 434          $this->assert_same_xml($expectedxml, $qformat->exportprocess());
 435      }
 436  
 437      /**
 438       * Simple check for exporting a category.
 439       */
 440      public function test_export_category_with_special_chars() {
 441          global $SITE;
 442  
 443          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 444          $this->resetAfterTest();
 445          $this->setAdminUser();
 446          // Note while this loads $qformat with all the 'right' data from the xml file,
 447          // the call to setCategory, followed by exportprocess will actually only export data
 448          // from the database (created by the generator).
 449          $qformat = $this->create_qformat('export_category.xml', $SITE);
 450  
 451          $category = $generator->create_question_category([
 452                  'name' => 'Alpha',
 453                  'contextid' => context_course::instance($SITE->id)->id,
 454                  'info' => 'This is Alpha category for test',
 455                  'infoformat' => '0',
 456                  'idnumber' => 'The inequalities < & >',
 457                  'stamp' => make_unique_id_code(),
 458                  'parent' => '0',
 459                  'sortorder' => '999']);
 460          $generator->create_question('truefalse', null, [
 461                  'category' => $category->id,
 462                  'name' => 'Alpha Question',
 463                  'questiontext' => ['format' => '1', 'text' => '<p>Testing Alpha Question</p>'],
 464                  'generalfeedback' => ['format' => '1', 'text' => ''],
 465                  'idnumber' => 'T & F',
 466                  'correctanswer' => '1',
 467                  'feedbacktrue' => ['format' => '1', 'text' => ''],
 468                  'feedbackfalse' => ['format' => '1', 'text' => ''],
 469                  'penalty' => '1']);
 470          $qformat->setCategory($category);
 471  
 472          $expectedxml = file_get_contents(__DIR__ . '/fixtures/html_chars_in_idnumbers.xml');
 473          $this->assert_same_xml($expectedxml, $qformat->exportprocess());
 474      }
 475  
 476      /**
 477       * Test that bad multianswer questions are not imported.
 478       */
 479      public function test_import_broken_multianswer_questions() {
 480          $lines = file(__DIR__ . '/fixtures/broken_cloze_questions.xml');
 481          $importer = $qformat = new qformat_xml();
 482  
 483          // The importer echoes some errors, so we need to capture and check that.
 484          ob_start();
 485          $questions = $importer->readquestions($lines);
 486          $output = ob_get_contents();
 487          ob_end_clean();
 488  
 489          // Check that there were some expected errors.
 490          $this->assertStringContainsString('Error importing question', $output);
 491          $this->assertStringContainsString('Invalid embedded answers (Cloze) question', $output);
 492          $this->assertStringContainsString('This type of question requires at least 2 choices', $output);
 493          $this->assertStringContainsString('The answer must be a number, for example -1.234 or 3e8, or \'*\'.', $output);
 494          $this->assertStringContainsString('One of the answers should have a score of 100% so it is possible to get full marks for this question.',
 495                  $output);
 496          $this->assertStringContainsString('The question text must include at least one embedded answer.', $output);
 497  
 498          // No question  have been imported.
 499          $this->assertCount(0, $questions);
 500      }
 501  }