Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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.

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