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