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