Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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

   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 question import and export system.
  19   *
  20   * @package    core_question
  21   * @category   test
  22   * @copyright  2009 The Open University
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core_question;
  27  
  28  use qformat_default;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  global $CFG;
  33  require_once($CFG->libdir . '/questionlib.php');
  34  require_once($CFG->dirroot . '/question/format.php');
  35  
  36  /**
  37   * Subclass to make it easier to test qformat_default.
  38   *
  39   * @copyright  2009 The Open University
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class testable_qformat extends qformat_default {
  43      public function assemble_category_path($names) {
  44          return parent::assemble_category_path($names);
  45      }
  46  
  47      public function split_category_path($names) {
  48          return parent::split_category_path($names);
  49      }
  50  }
  51  
  52  /**
  53   * Unit tests for the matching question definition class.
  54   *
  55   * @copyright  2009 The Open University
  56   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  57   */
  58  class importexport_test extends \advanced_testcase {
  59      public function test_assemble_category_path() {
  60          $format = new testable_qformat();
  61          $pathsections = [
  62              '$course$',
  63              "Tim's questions",
  64              "Tricky things like / // and so on",
  65              'Category name ending in /',
  66              '/ and one that starts with one',
  67              '<span lang="en" class="multilang">Matematically</span> <span lang="sv" class="multilang">Matematiskt (svenska)</span>'
  68          ];
  69          $this->assertEquals('$course$/Tim\'s questions/Tricky things like // //// and so on/Category name ending in // / // and one that starts with one/<span lang="en" class="multilang">Matematically<//span> <span lang="sv" class="multilang">Matematiskt (svenska)<//span>',
  70                  $format->assemble_category_path($pathsections));
  71      }
  72  
  73      public function test_split_category_path() {
  74          $format = new testable_qformat();
  75          $path = '$course$/Tim\'s questions/Tricky things like // //// and so on/Category name ending in // / // and one that starts with one/<span lang="en" class="multilang">Matematically<//span> <span lang="sv" class="multilang">Matematiskt (svenska)<//span>';
  76          $this->assertEquals([
  77                      '$course$',
  78                      "Tim's questions",
  79                      "Tricky things like / // and so on",
  80                      'Category name ending in /',
  81                      '/ and one that starts with one',
  82                      '<span lang="en" class="multilang">Matematically</span> <span lang="sv" class="multilang">Matematiskt (svenska)</span>'
  83                  ], $format->split_category_path($path));
  84      }
  85  
  86      public function test_split_category_path_cleans() {
  87          $format = new testable_qformat();
  88          $path = '<evil>Nasty <virus //> thing<//evil>';
  89          $this->assertEquals(['Nasty  thing'], $format->split_category_path($path));
  90      }
  91  
  92      public function test_clean_question_name() {
  93          $format = new testable_qformat();
  94  
  95          $name = 'Nice simple name';
  96          $this->assertEquals($name, $format->clean_question_name($name));
  97  
  98          $name = 'Question in <span lang="en" class="multilang">English</span><span lang="fr" class="multilang">French</span>';
  99          $this->assertEquals($name, $format->clean_question_name($name));
 100  
 101          $name = 'Evil <script type="text/javascrip">alert("You have been hacked!");</script>';
 102          $this->assertEquals('Evil alert("You have been hacked!");', $format->clean_question_name($name));
 103  
 104          $name = 'This is a very long question name. It goes on and on and on. ' .
 105                  'I wonder if it will ever stop. The quetsion name field in the database is only ' .
 106                  'two hundred and fifty five characters wide, so if the import file contains a ' .
 107                  'name longer than that, the code had better truncate it!';
 108          $this->assertEquals(shorten_text($name, 251), $format->clean_question_name($name));
 109  
 110          // The worst case scenario is a whole lot of single charaters in separate multilang tags.
 111          $name = '<span lang="en" class="multilang">A</span>' .
 112                  '<span lang="fr" class="multilang">B</span>' .
 113                  '<span lang="fr_ca" class="multilang">C</span>' .
 114                  '<span lang="en_us" class="multilang">D</span>' .
 115                  '<span lang="de" class="multilang">E</span>' .
 116                  '<span lang="cz" class="multilang">F</span>' .
 117                  '<span lang="it" class="multilang">G</span>' .
 118                  '<span lang="es" class="multilang">H</span>' .
 119                  '<span lang="pt" class="multilang">I</span>' .
 120                  '<span lang="ch" class="multilang">J</span>';
 121          $this->assertEquals(shorten_text($name, 1), $format->clean_question_name($name));
 122      }
 123  
 124      public function test_create_default_question_name() {
 125          $format = new testable_qformat();
 126  
 127          $text = 'Nice simple name';
 128          $this->assertEquals($text, $format->create_default_question_name($text, 'Default'));
 129  
 130          $this->assertEquals('Default', $format->create_default_question_name('', 'Default'));
 131  
 132          $text = 'Question in <span lang="en" class="multilang">English</span><span lang="fr" class="multilang">French</span>';
 133          $this->assertEquals($text, $format->create_default_question_name($text, 'Default'));
 134  
 135          $text = 'Evil <script type="text/javascrip">alert("You have been hacked!");</script>';
 136          $this->assertEquals('Evil alert("You have been hacked!");',
 137                  $format->create_default_question_name($text, 'Default'));
 138  
 139          $text = 'This is a very long question text. It goes on and on and on. ' .
 140                  'I wonder if it will ever stop. The question name field in the database is only ' .
 141                  'two hundred and fifty five characters wide, so if the import file contains a ' .
 142                  'name longer than that, the code had better truncate it!';
 143          $this->assertEquals(shorten_text($text, 80), $format->create_default_question_name($text, 'Default'));
 144  
 145          // The worst case scenario is a whole lot of single charaters in separate multilang tags.
 146          $text = '<span lang="en" class="multilang">A</span>' .
 147                  '<span lang="fr" class="multilang">B</span>' .
 148                  '<span lang="fr_ca" class="multilang">C</span>' .
 149                  '<span lang="en_us" class="multilang">D</span>' .
 150                  '<span lang="de" class="multilang">E</span>' .
 151                  '<span lang="cz" class="multilang">F</span>' .
 152                  '<span lang="it" class="multilang">G</span>' .
 153                  '<span lang="es" class="multilang">H</span>' .
 154                  '<span lang="pt" class="multilang">I</span>' .
 155                  '<span lang="ch" class="multilang">J</span>';
 156          $this->assertEquals(shorten_text($text, 1), $format->create_default_question_name($text, 'Default'));
 157      }
 158  }