MDL-63812 qtype_gapselect: test data generator to standard qtype pattern
[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
b3adccfb 42 protected function setUp() {
fdbff6ce 43 $this->qtype = question_bank::get_qtype('gapselect');
0d24b17a
TH
44 }
45
b3adccfb 46 protected function tearDown() {
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
61 /**
647d9373 62 * Get some test question data.
0d24b17a 63 * @return object the data to construct a question like
fdbff6ce 64 * {@link test_question_maker::make_question('gapselect')}.
0d24b17a
TH
65 */
66 protected function get_test_question_data() {
fdbff6ce 67 return test_question_maker::get_question_data('gapselect');
0d24b17a
TH
68 }
69
70 public function test_name() {
b3adccfb 71 $this->assertEquals($this->qtype->name(), 'gapselect');
0d24b17a
TH
72 }
73
74 public function test_can_analyse_responses() {
75 $this->assertTrue($this->qtype->can_analyse_responses());
76 }
77
78 public function test_initialise_question_instance() {
79 $qdata = $this->get_test_question_data();
80
fdbff6ce 81 $expected = test_question_maker::make_question('gapselect');
0d24b17a
TH
82 $expected->stamp = $qdata->stamp;
83 $expected->version = $qdata->version;
84
85 $q = $this->qtype->make_question($qdata);
86
b3adccfb 87 $this->assertEquals($expected, $q);
0d24b17a
TH
88 }
89
90 public function test_get_random_guess_score() {
91 $q = $this->get_test_question_data();
b3adccfb 92 $this->assertEquals(0.5, $this->qtype->get_random_guess_score($q), '', 0.0000001);
0d24b17a
TH
93 }
94
95 public function test_get_possible_responses() {
96 $q = $this->get_test_question_data();
97
b3adccfb 98 $this->assertEquals(array(
0d24b17a 99 1 => array(
647d9373 100 1 => new question_possible_response('quick', 1 / 3),
0d24b17a
TH
101 2 => new question_possible_response('slow', 0),
102 null => question_possible_response::no_response()),
103 2 => array(
647d9373 104 1 => new question_possible_response('fox', 1 / 3),
0d24b17a
TH
105 2 => new question_possible_response('dog', 0),
106 null => question_possible_response::no_response()),
107 3 => array(
647d9373 108 1 => new question_possible_response('lazy', 1 / 3),
0d24b17a
TH
109 2 => new question_possible_response('assiduous', 0),
110 null => question_possible_response::no_response()),
111 ), $this->qtype->get_possible_responses($q));
112 }
113
114 public function test_xml_import() {
115 $xml = ' <question type="gapselect">
116 <name>
117 <text>A select missing words question</text>
118 </name>
119 <questiontext format="moodle_auto_format">
120 <text>Put these in order: [[1]], [[2]], [[3]].</text>
121 </questiontext>
122 <generalfeedback>
123 <text>The answer is Alpha, Beta, Gamma.</text>
124 </generalfeedback>
125 <defaultgrade>3</defaultgrade>
126 <penalty>0.3333333</penalty>
127 <hidden>0</hidden>
128 <shuffleanswers>1</shuffleanswers>
129 <correctfeedback>
130 <text><![CDATA[<p>Your answer is correct.</p>]]></text>
131 </correctfeedback>
132 <partiallycorrectfeedback>
133 <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
134 </partiallycorrectfeedback>
135 <incorrectfeedback>
136 <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
137 </incorrectfeedback>
138 <shownumcorrect/>
139 <selectoption>
140 <text>Alpha</text>
141 <group>1</group>
142 </selectoption>
143 <selectoption>
144 <text>Beta</text>
145 <group>1</group>
146 </selectoption>
147 <selectoption>
148 <text>Gamma</text>
149 <group>1</group>
150 </selectoption>
801012aa 151 <hint format="moodle_auto_format">
0d24b17a
TH
152 <text>Try again.</text>
153 <shownumcorrect />
154 </hint>
801012aa 155 <hint format="moodle_auto_format">
0d24b17a
TH
156 <text>These are the first three letters of the Greek alphabet.</text>
157 <shownumcorrect />
158 <clearwrong />
159 </hint>
160 </question>';
161 $xmldata = xmlize($xml);
162
163 $importer = new qformat_xml();
164 $q = $importer->try_importing_using_qtypes(
165 $xmldata['question'], null, null, 'gapselect');
166
5fa78cb4 167 $expectedq = new stdClass();
0d24b17a
TH
168 $expectedq->qtype = 'gapselect';
169 $expectedq->name = 'A select missing words question';
170 $expectedq->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
171 $expectedq->questiontextformat = FORMAT_MOODLE;
172 $expectedq->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
173 $expectedq->defaultmark = 3;
174 $expectedq->length = 1;
175 $expectedq->penalty = 0.3333333;
176
177 $expectedq->shuffleanswers = 1;
060e0294 178 $expectedq->correctfeedback = array('text' => '<p>Your answer is correct.</p>',
08243fa8 179 'format' => FORMAT_MOODLE);
060e0294
TH
180 $expectedq->partiallycorrectfeedback = array(
181 'text' => '<p>Your answer is partially correct.</p>',
08243fa8 182 'format' => FORMAT_MOODLE);
0d24b17a 183 $expectedq->shownumcorrect = true;
060e0294 184 $expectedq->incorrectfeedback = array('text' => '<p>Your answer is incorrect.</p>',
08243fa8 185 'format' => FORMAT_MOODLE);
0d24b17a
TH
186
187 $expectedq->choices = array(
aa6a0ff0
TH
188 array('answer' => 'Alpha', 'choicegroup' => 1),
189 array('answer' => 'Beta', 'choicegroup' => 1),
190 array('answer' => 'Gamma', 'choicegroup' => 1),
0d24b17a
TH
191 );
192
aa6a0ff0 193 $expectedq->hint = array(
08243fa8 194 array('text' => 'Try again.', 'format' => FORMAT_MOODLE),
060e0294 195 array('text' => 'These are the first three letters of the Greek alphabet.',
08243fa8 196 'format' => FORMAT_MOODLE));
0d24b17a
TH
197 $expectedq->hintshownumcorrect = array(true, true);
198 $expectedq->hintclearwrong = array(false, true);
b3adccfb
JP
199 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
200 $this->assertEquals($expectedq->hint, $q->hint);
0d24b17a
TH
201 }
202
203 public function test_xml_export() {
5fa78cb4 204 $qdata = new stdClass();
0d24b17a 205 $qdata->id = 123;
1796eb18 206 $qdata->contextid = \context_system::instance()->id;
6189fda4 207 $qdata->idnumber = null;
0d24b17a
TH
208 $qdata->qtype = 'gapselect';
209 $qdata->name = 'A select missing words question';
210 $qdata->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
211 $qdata->questiontextformat = FORMAT_MOODLE;
212 $qdata->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
7540fcdc 213 $qdata->generalfeedbackformat = FORMAT_MOODLE;
0d24b17a
TH
214 $qdata->defaultmark = 3;
215 $qdata->length = 1;
216 $qdata->penalty = 0.3333333;
217 $qdata->hidden = 0;
218
b3adccfb 219 $qdata->options = new stdClass();
0d24b17a
TH
220 $qdata->options->shuffleanswers = 1;
221 $qdata->options->correctfeedback = '<p>Your answer is correct.</p>';
7540fcdc 222 $qdata->options->correctfeedbackformat = FORMAT_MOODLE;
0d24b17a 223 $qdata->options->partiallycorrectfeedback = '<p>Your answer is partially correct.</p>';
7540fcdc 224 $qdata->options->partiallycorrectfeedbackformat = FORMAT_MOODLE;
0d24b17a
TH
225 $qdata->options->shownumcorrect = true;
226 $qdata->options->incorrectfeedback = '<p>Your answer is incorrect.</p>';
7540fcdc 227 $qdata->options->incorrectfeedbackformat = FORMAT_MOODLE;
0d24b17a
TH
228
229 $qdata->options->answers = array(
7540fcdc
TH
230 13 => new question_answer(13, 'Alpha', 0, '1', FORMAT_MOODLE),
231 14 => new question_answer(14, 'Beta', 0, '1', FORMAT_MOODLE),
232 15 => new question_answer(15, 'Gamma', 0, '1', FORMAT_MOODLE),
0d24b17a
TH
233 );
234
235 $qdata->hints = array(
7540fcdc 236 1 => new question_hint_with_parts(1, 'Try again.', FORMAT_MOODLE, true, false),
060e0294
TH
237 2 => new question_hint_with_parts(2,
238 'These are the first three letters of the Greek alphabet.',
239 FORMAT_MOODLE, true, true),
0d24b17a
TH
240 );
241
242 $exporter = new qformat_xml();
243 $xml = $exporter->writequestion($qdata);
244
245 $expectedxml = '<!-- question: 123 -->
246 <question type="gapselect">
247 <name>
248 <text>A select missing words question</text>
249 </name>
250 <questiontext format="moodle_auto_format">
251 <text>Put these in order: [[1]], [[2]], [[3]].</text>
252 </questiontext>
7540fcdc 253 <generalfeedback format="moodle_auto_format">
0d24b17a
TH
254 <text>The answer is Alpha, Beta, Gamma.</text>
255 </generalfeedback>
256 <defaultgrade>3</defaultgrade>
257 <penalty>0.3333333</penalty>
258 <hidden>0</hidden>
6189fda4 259 <idnumber></idnumber>
0d24b17a 260 <shuffleanswers>1</shuffleanswers>
7540fcdc 261 <correctfeedback format="moodle_auto_format">
0d24b17a
TH
262 <text><![CDATA[<p>Your answer is correct.</p>]]></text>
263 </correctfeedback>
7540fcdc 264 <partiallycorrectfeedback format="moodle_auto_format">
0d24b17a
TH
265 <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
266 </partiallycorrectfeedback>
7540fcdc 267 <incorrectfeedback format="moodle_auto_format">
0d24b17a
TH
268 <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
269 </incorrectfeedback>
270 <shownumcorrect/>
271 <selectoption>
272 <text>Alpha</text>
273 <group>1</group>
274 </selectoption>
275 <selectoption>
276 <text>Beta</text>
277 <group>1</group>
278 </selectoption>
279 <selectoption>
280 <text>Gamma</text>
281 <group>1</group>
282 </selectoption>
7540fcdc 283 <hint format="moodle_auto_format">
0d24b17a
TH
284 <text>Try again.</text>
285 <shownumcorrect/>
286 </hint>
7540fcdc 287 <hint format="moodle_auto_format">
0d24b17a
TH
288 <text>These are the first three letters of the Greek alphabet.</text>
289 <shownumcorrect/>
290 <clearwrong/>
291 </hint>
292 </question>
293';
294
295 $this->assert_same_xml($expectedxml, $xml);
296 }
297}