Merge branch 'MDL-73457-master-2' of https://github.com/HuongNV13/moodle
[moodle.git] / question / tests / importexport_test.php
blobb0b0b2725f633be9cc92355c963a6b13c0e1f6df
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.
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/>.
17 /**
18 * Unit tests for the question import and export system.
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
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->libdir . '/questionlib.php');
31 require_once($CFG->dirroot . '/question/format.php');
34 /**
35 * Subclass to make it easier to test qformat_default.
37 * @copyright 2009 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class testable_qformat extends qformat_default {
41 public function assemble_category_path($names) {
42 return parent::assemble_category_path($names);
45 public function split_category_path($names) {
46 return parent::split_category_path($names);
51 /**
52 * Unit tests for the matching question definition class.
54 * @copyright 2009 The Open University
55 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
57 class qformat_default_test extends advanced_testcase {
58 public function test_assemble_category_path() {
59 $format = new testable_qformat();
60 $pathsections = [
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>'
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));
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([
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));
85 public function test_split_category_path_cleans() {
86 $format = new testable_qformat();
87 $path = '<evil>Nasty <virus //> thing<//evil>';
88 $this->assertEquals(['Nasty thing'], $format->split_category_path($path));
91 public function test_clean_question_name() {
92 $format = new testable_qformat();
94 $name = 'Nice simple name';
95 $this->assertEquals($name, $format->clean_question_name($name));
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));
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));
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));
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));
123 public function test_create_default_question_name() {
124 $format = new testable_qformat();
126 $text = 'Nice simple name';
127 $this->assertEquals($text, $format->create_default_question_name($text, 'Default'));
129 $this->assertEquals('Default', $format->create_default_question_name('', 'Default'));
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'));
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'));
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'));
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'));