2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Unit tests for the question import and export system.
20 * @package core_question
22 * @copyright 2009 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 namespace core_question
;
30 defined('MOODLE_INTERNAL') ||
die();
33 require_once($CFG->libdir
. '/questionlib.php');
34 require_once($CFG->dirroot
. '/question/format.php');
37 * Subclass to make it easier to test qformat_default.
39 * @copyright 2009 The Open University
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class testable_qformat
extends qformat_default
{
43 public function assemble_category_path($names) {
44 return parent
::assemble_category_path($names);
47 public function split_category_path($names) {
48 return parent
::split_category_path($names);
53 * Unit tests for the matching question definition class.
55 * @copyright 2009 The Open University
56 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
58 class importexport_test
extends \advanced_testcase
{
59 public function test_assemble_category_path(): void
{
60 $format = new testable_qformat();
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>'
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));
73 public function test_split_category_path(): void
{
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>';
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));
86 public function test_split_category_path_cleans(): void
{
87 $format = new testable_qformat();
88 $path = '<evil>Nasty <virus //> thing<//evil>';
89 $this->assertEquals(['Nasty thing'], $format->split_category_path($path));
92 public function test_clean_question_name(): void
{
93 $format = new testable_qformat();
95 $name = 'Nice simple name';
96 $this->assertEquals($name, $format->clean_question_name($name));
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));
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));
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));
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));
124 public function test_create_default_question_name(): void
{
125 $format = new testable_qformat();
127 $text = 'Nice simple name';
128 $this->assertEquals($text, $format->create_default_question_name($text, 'Default'));
130 $this->assertEquals('Default', $format->create_default_question_name('', 'Default'));
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'));
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'));
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'));
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'));