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_aiken
|
|
|
20 |
* @copyright 2018 Jean-Michel Vedrine
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
1441 |
ariadna |
24 |
use core_question\local\bank\question_version_status;
|
|
|
25 |
|
1 |
efrain |
26 |
defined('MOODLE_INTERNAL') || die();
|
|
|
27 |
|
|
|
28 |
global $CFG;
|
|
|
29 |
require_once($CFG->libdir . '/questionlib.php');
|
|
|
30 |
require_once($CFG->dirroot . '/question/format.php');
|
|
|
31 |
require_once($CFG->dirroot . '/question/format/aiken/format.php');
|
|
|
32 |
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
|
|
|
33 |
require_once($CFG->dirroot . '/question/editlib.php');
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Unit tests for the Aiken question format export.
|
|
|
37 |
*
|
|
|
38 |
* @copyright 2018 Jean-Michel vedrine)
|
|
|
39 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
1441 |
ariadna |
40 |
* @covers \qformat_aiken
|
1 |
efrain |
41 |
*/
|
1441 |
ariadna |
42 |
final class qformat_aiken_export_test extends advanced_testcase {
|
1 |
efrain |
43 |
/**
|
|
|
44 |
* Assert that 2 strings are the same, ignoring ends of line.
|
|
|
45 |
* We need to override this function because we don't want any output
|
|
|
46 |
* @param string $expectedtext The expected string.
|
|
|
47 |
* @param string $text The actual string.
|
|
|
48 |
*/
|
|
|
49 |
public function assert_same_aiken($expectedtext, $text) {
|
|
|
50 |
$this->assertEquals(
|
|
|
51 |
phpunit_util::normalise_line_endings($expectedtext),
|
|
|
52 |
phpunit_util::normalise_line_endings($text)
|
|
|
53 |
);
|
|
|
54 |
}
|
|
|
55 |
|
11 |
efrain |
56 |
public function test_export_questions(): void {
|
1 |
efrain |
57 |
$this->resetAfterTest();
|
|
|
58 |
$this->setAdminUser();
|
|
|
59 |
// Create a new course category and and a new course in that.
|
|
|
60 |
$category = $this->getDataGenerator()->create_category();
|
|
|
61 |
$course = $this->getDataGenerator()->create_course(array('category' => $category->id));
|
1441 |
ariadna |
62 |
$qbank = self::getDataGenerator()->create_module('qbank', ['course' => $course->id]);
|
|
|
63 |
$context = context_module::instance($qbank->cmid);
|
1 |
efrain |
64 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
65 |
$cat = $generator->create_question_category(array('contextid' => $context->id));
|
|
|
66 |
$question1 = $generator->create_question('shortanswer', null,
|
|
|
67 |
array('category' => $cat->id));
|
|
|
68 |
$question2 = $generator->create_question('essay', null,
|
|
|
69 |
array('category' => $cat->id));
|
|
|
70 |
$question3 = $generator->create_question('numerical', null,
|
|
|
71 |
array('category' => $cat->id));
|
|
|
72 |
$question4 = $generator->create_question('multichoice', 'one_of_four',
|
|
|
73 |
array('category' => $cat->id));
|
|
|
74 |
$question4 = $generator->create_question('multichoice', 'two_of_four',
|
|
|
75 |
array('category' => $cat->id));
|
|
|
76 |
$exporter = new qformat_aiken();
|
|
|
77 |
$exporter->category = $cat;
|
|
|
78 |
$exporter->setCourse($course);
|
|
|
79 |
$expectedoutput = <<<EOT
|
|
|
80 |
Which is the oddest number?
|
|
|
81 |
A) One
|
|
|
82 |
B) Two
|
|
|
83 |
C) Three
|
|
|
84 |
D) Four
|
|
|
85 |
ANSWER: A
|
|
|
86 |
|
|
|
87 |
EOT;
|
|
|
88 |
$this->assert_same_aiken($expectedoutput, $exporter->exportprocess());
|
|
|
89 |
}
|
|
|
90 |
|
11 |
efrain |
91 |
public function test_export_multiline_question(): void {
|
1 |
efrain |
92 |
$this->resetAfterTest();
|
|
|
93 |
$this->setAdminUser();
|
|
|
94 |
// Create a new course category and and a new course in that.
|
|
|
95 |
$category = $this->getDataGenerator()->create_category();
|
|
|
96 |
$course = $this->getDataGenerator()->create_course(array('category' => $category->id));
|
|
|
97 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
1441 |
ariadna |
98 |
$qbank = self::getDataGenerator()->create_module('qbank', ['course' => $course->id]);
|
|
|
99 |
$context = context_module::instance($qbank->cmid);
|
1 |
efrain |
100 |
$cat = $generator->create_question_category(array('contextid' => $context->id));
|
|
|
101 |
$question = $generator->create_question('multichoice', 'one_of_four',
|
|
|
102 |
array('category' => $cat->id));
|
|
|
103 |
$question->questiontext = <<<EOT
|
|
|
104 |
<p>Which is the</p>
|
|
|
105 |
<p>oddest number?</p>
|
|
|
106 |
EOT;
|
|
|
107 |
$exporter = new qformat_aiken();
|
|
|
108 |
$exporter->category = $cat;
|
|
|
109 |
$exporter->setCourse($course);
|
|
|
110 |
$expectedoutput = <<<EOT
|
|
|
111 |
Which is the oddest number?
|
|
|
112 |
A) One
|
|
|
113 |
B) Two
|
|
|
114 |
C) Three
|
|
|
115 |
D) Four
|
|
|
116 |
ANSWER: A
|
|
|
117 |
|
|
|
118 |
EOT;
|
|
|
119 |
$this->assert_same_aiken($expectedoutput, $exporter->exportprocess());
|
|
|
120 |
}
|
1441 |
ariadna |
121 |
|
|
|
122 |
public function test_hidden_question_not_exported(): void {
|
|
|
123 |
$this->resetAfterTest();
|
|
|
124 |
$this->setAdminUser();
|
|
|
125 |
|
|
|
126 |
// Create a new course category and a new course in that.
|
|
|
127 |
$category = $this->getDataGenerator()->create_category();
|
|
|
128 |
$course = $this->getDataGenerator()->create_course(['category' => $category->id]);
|
|
|
129 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
130 |
$context = context_coursecat::instance($category->id);
|
|
|
131 |
$cat = $generator->create_question_category(['contextid' => $context->id]);
|
|
|
132 |
|
|
|
133 |
// Create a visible and a hidden question.
|
|
|
134 |
$generator->create_question('multichoice', 'one_of_four', [
|
|
|
135 |
'category' => $cat->id,
|
|
|
136 |
'questiontext' => ['text' => 'This question is the visible one.', 'format' => FORMAT_HTML],
|
|
|
137 |
]);
|
|
|
138 |
$generator->create_question('multichoice', 'one_of_four', [
|
|
|
139 |
'category' => $cat->id,
|
|
|
140 |
'questiontext' => ['text' => 'This question is the hidden one.', 'format' => FORMAT_HTML],
|
|
|
141 |
'status' => question_version_status::QUESTION_STATUS_HIDDEN,
|
|
|
142 |
]);
|
|
|
143 |
|
|
|
144 |
// Prepared the expected result.
|
|
|
145 |
$expectedoutput = <<<EOT
|
|
|
146 |
This question is the visible one.
|
|
|
147 |
A) One
|
|
|
148 |
B) Two
|
|
|
149 |
C) Three
|
|
|
150 |
D) Four
|
|
|
151 |
ANSWER: A
|
|
|
152 |
|
|
|
153 |
EOT;
|
|
|
154 |
|
|
|
155 |
// Do the export and verify.
|
|
|
156 |
$exporter = new qformat_aiken();
|
|
|
157 |
$exporter->category = $cat;
|
|
|
158 |
$exporter->setCourse($course);
|
|
|
159 |
$this->assert_same_aiken($expectedoutput, $exporter->exportprocess());
|
|
|
160 |
}
|
1 |
efrain |
161 |
}
|