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 |
namespace qtype_gapselect;
|
|
|
18 |
|
|
|
19 |
use question_answer;
|
|
|
20 |
use question_bank;
|
|
|
21 |
use question_hint_with_parts;
|
|
|
22 |
use question_possible_response;
|
|
|
23 |
|
|
|
24 |
defined('MOODLE_INTERNAL') || die();
|
|
|
25 |
global $CFG;
|
|
|
26 |
|
|
|
27 |
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
|
|
|
28 |
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Unit tests for the select missing words question definition class.
|
|
|
32 |
*
|
|
|
33 |
* @package qtype_gapselect
|
|
|
34 |
* @copyright 2012 The Open University
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
*/
|
1441 |
ariadna |
37 |
final class question_type_test extends \question_testcase {
|
1 |
efrain |
38 |
/** @var qtype_gapselect instance of the question type class to test. */
|
|
|
39 |
protected $qtype;
|
|
|
40 |
|
|
|
41 |
protected function setUp(): void {
|
1441 |
ariadna |
42 |
parent::setUp();
|
1 |
efrain |
43 |
$this->qtype = question_bank::get_qtype('gapselect');
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
protected function tearDown(): void {
|
|
|
47 |
$this->qtype = null;
|
1441 |
ariadna |
48 |
parent::tearDown();
|
1 |
efrain |
49 |
}
|
|
|
50 |
|
11 |
efrain |
51 |
public function test_save_question(): void {
|
1 |
efrain |
52 |
$this->resetAfterTest();
|
|
|
53 |
|
|
|
54 |
$syscontext = \context_system::instance();
|
|
|
55 |
/** @var core_question_generator $generator */
|
|
|
56 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
57 |
$category = $generator->create_question_category(['contextid' => $syscontext->id]);
|
|
|
58 |
|
|
|
59 |
$fromform = \test_question_maker::get_question_form_data('gapselect', 'missingchoiceno');
|
|
|
60 |
$fromform->category = $category->id . ',' . $syscontext->id;
|
|
|
61 |
|
|
|
62 |
$question = new \stdClass();
|
|
|
63 |
$question->category = $category->id;
|
|
|
64 |
$question->qtype = 'gapselect';
|
|
|
65 |
$question->createdby = 0;
|
|
|
66 |
|
|
|
67 |
$this->qtype->save_question($question, $fromform);
|
|
|
68 |
$q = question_bank::load_question($question->id);
|
|
|
69 |
// We just want to verify that this does not cause errors,
|
|
|
70 |
// but also verify some of the outcome.
|
|
|
71 |
$this->assertEquals('The [[1]] sat on the [[2]].', $q->questiontext);
|
|
|
72 |
$this->assertEquals([1 => 1, 2 => 1], $q->places);
|
|
|
73 |
$this->assertEquals([1 => 1, 2 => 2], $q->rightchoices);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Get some test question data.
|
|
|
78 |
* @return object the data to construct a question like
|
|
|
79 |
* {@see \test_question_maker::make_question('gapselect')}.
|
|
|
80 |
*/
|
|
|
81 |
protected function get_test_question_data() {
|
|
|
82 |
return \test_question_maker::get_question_data('gapselect');
|
|
|
83 |
}
|
|
|
84 |
|
11 |
efrain |
85 |
public function test_name(): void {
|
1 |
efrain |
86 |
$this->assertEquals($this->qtype->name(), 'gapselect');
|
|
|
87 |
}
|
|
|
88 |
|
11 |
efrain |
89 |
public function test_can_analyse_responses(): void {
|
1 |
efrain |
90 |
$this->assertTrue($this->qtype->can_analyse_responses());
|
|
|
91 |
}
|
|
|
92 |
|
11 |
efrain |
93 |
public function test_initialise_question_instance(): void {
|
1 |
efrain |
94 |
$qdata = $this->get_test_question_data();
|
|
|
95 |
|
|
|
96 |
$expected = \test_question_maker::make_question('gapselect');
|
|
|
97 |
$expected->stamp = $qdata->stamp;
|
|
|
98 |
|
|
|
99 |
$q = $this->qtype->make_question($qdata);
|
|
|
100 |
|
|
|
101 |
$this->assertEquals($expected, $q);
|
|
|
102 |
}
|
|
|
103 |
|
11 |
efrain |
104 |
public function test_get_random_guess_score(): void {
|
1 |
efrain |
105 |
$q = $this->get_test_question_data();
|
|
|
106 |
$this->assertEqualsWithDelta(0.5, $this->qtype->get_random_guess_score($q), 0.0000001);
|
|
|
107 |
}
|
|
|
108 |
|
11 |
efrain |
109 |
public function test_get_possible_responses(): void {
|
1 |
efrain |
110 |
$q = $this->get_test_question_data();
|
|
|
111 |
|
|
|
112 |
$this->assertEquals(array(
|
|
|
113 |
1 => array(
|
|
|
114 |
1 => new question_possible_response('quick', 1 / 3),
|
|
|
115 |
2 => new question_possible_response('slow', 0),
|
|
|
116 |
null => question_possible_response::no_response()),
|
|
|
117 |
2 => array(
|
|
|
118 |
1 => new question_possible_response('fox', 1 / 3),
|
|
|
119 |
2 => new question_possible_response('dog', 0),
|
|
|
120 |
null => question_possible_response::no_response()),
|
|
|
121 |
3 => array(
|
|
|
122 |
1 => new question_possible_response('lazy', 1 / 3),
|
|
|
123 |
2 => new question_possible_response('assiduous', 0),
|
|
|
124 |
null => question_possible_response::no_response()),
|
|
|
125 |
), $this->qtype->get_possible_responses($q));
|
|
|
126 |
}
|
|
|
127 |
|
11 |
efrain |
128 |
public function test_xml_import(): void {
|
1 |
efrain |
129 |
$xml = ' <question type="gapselect">
|
|
|
130 |
<name>
|
|
|
131 |
<text>A select missing words question</text>
|
|
|
132 |
</name>
|
|
|
133 |
<questiontext format="moodle_auto_format">
|
|
|
134 |
<text>Put these in order: [[1]], [[2]], [[3]].</text>
|
|
|
135 |
</questiontext>
|
|
|
136 |
<generalfeedback>
|
|
|
137 |
<text>The answer is Alpha, Beta, Gamma.</text>
|
|
|
138 |
</generalfeedback>
|
|
|
139 |
<defaultgrade>3</defaultgrade>
|
|
|
140 |
<penalty>0.3333333</penalty>
|
|
|
141 |
<hidden>0</hidden>
|
|
|
142 |
<shuffleanswers>1</shuffleanswers>
|
|
|
143 |
<correctfeedback>
|
|
|
144 |
<text><![CDATA[<p>Your answer is correct.</p>]]></text>
|
|
|
145 |
</correctfeedback>
|
|
|
146 |
<partiallycorrectfeedback>
|
|
|
147 |
<text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
|
|
|
148 |
</partiallycorrectfeedback>
|
|
|
149 |
<incorrectfeedback>
|
|
|
150 |
<text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
|
|
|
151 |
</incorrectfeedback>
|
|
|
152 |
<shownumcorrect/>
|
|
|
153 |
<selectoption>
|
|
|
154 |
<text>Alpha</text>
|
|
|
155 |
<group>1</group>
|
|
|
156 |
</selectoption>
|
|
|
157 |
<selectoption>
|
|
|
158 |
<text>Beta</text>
|
|
|
159 |
<group>1</group>
|
|
|
160 |
</selectoption>
|
|
|
161 |
<selectoption>
|
|
|
162 |
<text>Gamma</text>
|
|
|
163 |
<group>1</group>
|
|
|
164 |
</selectoption>
|
|
|
165 |
<hint format="moodle_auto_format">
|
|
|
166 |
<text>Try again.</text>
|
|
|
167 |
<shownumcorrect />
|
|
|
168 |
</hint>
|
|
|
169 |
<hint format="moodle_auto_format">
|
|
|
170 |
<text>These are the first three letters of the Greek alphabet.</text>
|
|
|
171 |
<shownumcorrect />
|
|
|
172 |
<clearwrong />
|
|
|
173 |
</hint>
|
|
|
174 |
</question>';
|
|
|
175 |
$xmldata = xmlize($xml);
|
|
|
176 |
|
|
|
177 |
$importer = new \qformat_xml();
|
|
|
178 |
$q = $importer->try_importing_using_qtypes(
|
|
|
179 |
$xmldata['question'], null, null, 'gapselect');
|
|
|
180 |
|
|
|
181 |
$expectedq = new \stdClass();
|
|
|
182 |
$expectedq->qtype = 'gapselect';
|
|
|
183 |
$expectedq->name = 'A select missing words question';
|
|
|
184 |
$expectedq->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
|
|
|
185 |
$expectedq->questiontextformat = FORMAT_MOODLE;
|
|
|
186 |
$expectedq->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
|
|
|
187 |
$expectedq->defaultmark = 3;
|
|
|
188 |
$expectedq->length = 1;
|
|
|
189 |
$expectedq->penalty = 0.3333333;
|
|
|
190 |
|
|
|
191 |
$expectedq->shuffleanswers = 1;
|
|
|
192 |
$expectedq->correctfeedback = array('text' => '<p>Your answer is correct.</p>',
|
|
|
193 |
'format' => FORMAT_MOODLE);
|
|
|
194 |
$expectedq->partiallycorrectfeedback = array(
|
|
|
195 |
'text' => '<p>Your answer is partially correct.</p>',
|
|
|
196 |
'format' => FORMAT_MOODLE);
|
|
|
197 |
$expectedq->shownumcorrect = true;
|
|
|
198 |
$expectedq->incorrectfeedback = array('text' => '<p>Your answer is incorrect.</p>',
|
|
|
199 |
'format' => FORMAT_MOODLE);
|
|
|
200 |
|
|
|
201 |
$expectedq->choices = array(
|
|
|
202 |
array('answer' => 'Alpha', 'choicegroup' => 1),
|
|
|
203 |
array('answer' => 'Beta', 'choicegroup' => 1),
|
|
|
204 |
array('answer' => 'Gamma', 'choicegroup' => 1),
|
|
|
205 |
);
|
|
|
206 |
|
|
|
207 |
$expectedq->hint = array(
|
|
|
208 |
array('text' => 'Try again.', 'format' => FORMAT_MOODLE),
|
|
|
209 |
array('text' => 'These are the first three letters of the Greek alphabet.',
|
|
|
210 |
'format' => FORMAT_MOODLE));
|
|
|
211 |
$expectedq->hintshownumcorrect = array(true, true);
|
|
|
212 |
$expectedq->hintclearwrong = array(false, true);
|
|
|
213 |
$this->assert(new \question_check_specified_fields_expectation($expectedq), $q);
|
|
|
214 |
$this->assertEquals($expectedq->hint, $q->hint);
|
|
|
215 |
}
|
|
|
216 |
|
11 |
efrain |
217 |
public function test_xml_export(): void {
|
1 |
efrain |
218 |
$qdata = new \stdClass();
|
|
|
219 |
$qdata->id = 123;
|
|
|
220 |
$qdata->contextid = \context_system::instance()->id;
|
|
|
221 |
$qdata->idnumber = null;
|
|
|
222 |
$qdata->qtype = 'gapselect';
|
|
|
223 |
$qdata->name = 'A select missing words question';
|
|
|
224 |
$qdata->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
|
|
|
225 |
$qdata->questiontextformat = FORMAT_MOODLE;
|
|
|
226 |
$qdata->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
|
|
|
227 |
$qdata->generalfeedbackformat = FORMAT_MOODLE;
|
|
|
228 |
$qdata->defaultmark = 3;
|
|
|
229 |
$qdata->length = 1;
|
|
|
230 |
$qdata->penalty = 0.3333333;
|
|
|
231 |
$qdata->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
|
|
|
232 |
|
|
|
233 |
$qdata->options = new \stdClass();
|
|
|
234 |
$qdata->options->shuffleanswers = 1;
|
|
|
235 |
$qdata->options->correctfeedback = '<p>Your answer is correct.</p>';
|
|
|
236 |
$qdata->options->correctfeedbackformat = FORMAT_MOODLE;
|
|
|
237 |
$qdata->options->partiallycorrectfeedback = '<p>Your answer is partially correct.</p>';
|
|
|
238 |
$qdata->options->partiallycorrectfeedbackformat = FORMAT_MOODLE;
|
|
|
239 |
$qdata->options->shownumcorrect = true;
|
|
|
240 |
$qdata->options->incorrectfeedback = '<p>Your answer is incorrect.</p>';
|
|
|
241 |
$qdata->options->incorrectfeedbackformat = FORMAT_MOODLE;
|
|
|
242 |
|
|
|
243 |
$qdata->options->answers = array(
|
|
|
244 |
13 => new question_answer(13, 'Alpha', 0, '1', FORMAT_MOODLE),
|
|
|
245 |
14 => new question_answer(14, 'Beta', 0, '1', FORMAT_MOODLE),
|
|
|
246 |
15 => new question_answer(15, 'Gamma', 0, '1', FORMAT_MOODLE),
|
|
|
247 |
);
|
|
|
248 |
|
|
|
249 |
$qdata->hints = array(
|
|
|
250 |
1 => new question_hint_with_parts(1, 'Try again.', FORMAT_MOODLE, true, false),
|
|
|
251 |
2 => new question_hint_with_parts(2,
|
|
|
252 |
'These are the first three letters of the Greek alphabet.',
|
|
|
253 |
FORMAT_MOODLE, true, true),
|
|
|
254 |
);
|
|
|
255 |
|
|
|
256 |
$exporter = new \qformat_xml();
|
|
|
257 |
$xml = $exporter->writequestion($qdata);
|
|
|
258 |
|
|
|
259 |
$expectedxml = '<!-- question: 123 -->
|
|
|
260 |
<question type="gapselect">
|
|
|
261 |
<name>
|
|
|
262 |
<text>A select missing words question</text>
|
|
|
263 |
</name>
|
|
|
264 |
<questiontext format="moodle_auto_format">
|
|
|
265 |
<text>Put these in order: [[1]], [[2]], [[3]].</text>
|
|
|
266 |
</questiontext>
|
|
|
267 |
<generalfeedback format="moodle_auto_format">
|
|
|
268 |
<text>The answer is Alpha, Beta, Gamma.</text>
|
|
|
269 |
</generalfeedback>
|
|
|
270 |
<defaultgrade>3</defaultgrade>
|
|
|
271 |
<penalty>0.3333333</penalty>
|
|
|
272 |
<hidden>0</hidden>
|
|
|
273 |
<idnumber></idnumber>
|
|
|
274 |
<shuffleanswers>1</shuffleanswers>
|
|
|
275 |
<correctfeedback format="moodle_auto_format">
|
|
|
276 |
<text><![CDATA[<p>Your answer is correct.</p>]]></text>
|
|
|
277 |
</correctfeedback>
|
|
|
278 |
<partiallycorrectfeedback format="moodle_auto_format">
|
|
|
279 |
<text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
|
|
|
280 |
</partiallycorrectfeedback>
|
|
|
281 |
<incorrectfeedback format="moodle_auto_format">
|
|
|
282 |
<text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
|
|
|
283 |
</incorrectfeedback>
|
|
|
284 |
<shownumcorrect/>
|
|
|
285 |
<selectoption>
|
|
|
286 |
<text>Alpha</text>
|
|
|
287 |
<group>1</group>
|
|
|
288 |
</selectoption>
|
|
|
289 |
<selectoption>
|
|
|
290 |
<text>Beta</text>
|
|
|
291 |
<group>1</group>
|
|
|
292 |
</selectoption>
|
|
|
293 |
<selectoption>
|
|
|
294 |
<text>Gamma</text>
|
|
|
295 |
<group>1</group>
|
|
|
296 |
</selectoption>
|
|
|
297 |
<hint format="moodle_auto_format">
|
|
|
298 |
<text>Try again.</text>
|
|
|
299 |
<shownumcorrect/>
|
|
|
300 |
</hint>
|
|
|
301 |
<hint format="moodle_auto_format">
|
|
|
302 |
<text>These are the first three letters of the Greek alphabet.</text>
|
|
|
303 |
<shownumcorrect/>
|
|
|
304 |
<clearwrong/>
|
|
|
305 |
</hint>
|
|
|
306 |
</question>
|
|
|
307 |
';
|
|
|
308 |
|
|
|
309 |
$this->assert_same_xml($expectedxml, $xml);
|
|
|
310 |
}
|
|
|
311 |
}
|