Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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