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