MDL-61410 question: fix unit tests failures
[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;
82 $gapselect->timecreated = time();
83 $gapselect->timemodified = time();
84 $gapselect->createdby = $USER->id;
85 $gapselect->modifiedby = $USER->id;
86
87 $gapselect->name = 'Selection from drop down list question';
88 $gapselect->questiontext = 'The [[1]] brown [[2]] jumped over the [[3]] dog.';
89 $gapselect->generalfeedback = 'This sentence uses each letter of the alphabet.';
90 $gapselect->qtype = 'gapselect';
91
b3adccfb 92 $gapselect->options = new stdClass();
0d24b17a
TH
93 $gapselect->options->shuffleanswers = true;
94
95 test_question_maker::set_standard_combined_feedback_fields($gapselect->options);
96
97 $gapselect->options->answers = array(
98 (object) array('answer' => 'quick', 'feedback' => '1'),
99 (object) array('answer' => 'fox', 'feedback' => '2'),
100 (object) array('answer' => 'lazy', 'feedback' => '3'),
101 (object) array('answer' => 'assiduous', 'feedback' => '3'),
102 (object) array('answer' => 'dog', 'feedback' => '2'),
103 (object) array('answer' => 'slow', 'feedback' => '1'),
104 );
105
106 return $gapselect;
107 }
108
109 public function test_name() {
b3adccfb 110 $this->assertEquals($this->qtype->name(), 'gapselect');
0d24b17a
TH
111 }
112
113 public function test_can_analyse_responses() {
114 $this->assertTrue($this->qtype->can_analyse_responses());
115 }
116
117 public function test_initialise_question_instance() {
118 $qdata = $this->get_test_question_data();
119
120 $expected = qtype_gapselect_test_helper::make_a_gapselect_question();
121 $expected->stamp = $qdata->stamp;
122 $expected->version = $qdata->version;
123
124 $q = $this->qtype->make_question($qdata);
125
b3adccfb 126 $this->assertEquals($expected, $q);
0d24b17a
TH
127 }
128
129 public function test_get_random_guess_score() {
130 $q = $this->get_test_question_data();
b3adccfb 131 $this->assertEquals(0.5, $this->qtype->get_random_guess_score($q), '', 0.0000001);
0d24b17a
TH
132 }
133
134 public function test_get_possible_responses() {
135 $q = $this->get_test_question_data();
136
b3adccfb 137 $this->assertEquals(array(
0d24b17a 138 1 => array(
647d9373 139 1 => new question_possible_response('quick', 1 / 3),
0d24b17a
TH
140 2 => new question_possible_response('slow', 0),
141 null => question_possible_response::no_response()),
142 2 => array(
647d9373 143 1 => new question_possible_response('fox', 1 / 3),
0d24b17a
TH
144 2 => new question_possible_response('dog', 0),
145 null => question_possible_response::no_response()),
146 3 => array(
647d9373 147 1 => new question_possible_response('lazy', 1 / 3),
0d24b17a
TH
148 2 => new question_possible_response('assiduous', 0),
149 null => question_possible_response::no_response()),
150 ), $this->qtype->get_possible_responses($q));
151 }
152
153 public function test_xml_import() {
154 $xml = ' <question type="gapselect">
155 <name>
156 <text>A select missing words question</text>
157 </name>
158 <questiontext format="moodle_auto_format">
159 <text>Put these in order: [[1]], [[2]], [[3]].</text>
160 </questiontext>
161 <generalfeedback>
162 <text>The answer is Alpha, Beta, Gamma.</text>
163 </generalfeedback>
164 <defaultgrade>3</defaultgrade>
165 <penalty>0.3333333</penalty>
166 <hidden>0</hidden>
167 <shuffleanswers>1</shuffleanswers>
168 <correctfeedback>
169 <text><![CDATA[<p>Your answer is correct.</p>]]></text>
170 </correctfeedback>
171 <partiallycorrectfeedback>
172 <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
173 </partiallycorrectfeedback>
174 <incorrectfeedback>
175 <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
176 </incorrectfeedback>
177 <shownumcorrect/>
178 <selectoption>
179 <text>Alpha</text>
180 <group>1</group>
181 </selectoption>
182 <selectoption>
183 <text>Beta</text>
184 <group>1</group>
185 </selectoption>
186 <selectoption>
187 <text>Gamma</text>
188 <group>1</group>
189 </selectoption>
801012aa 190 <hint format="moodle_auto_format">
0d24b17a
TH
191 <text>Try again.</text>
192 <shownumcorrect />
193 </hint>
801012aa 194 <hint format="moodle_auto_format">
0d24b17a
TH
195 <text>These are the first three letters of the Greek alphabet.</text>
196 <shownumcorrect />
197 <clearwrong />
198 </hint>
199 </question>';
200 $xmldata = xmlize($xml);
201
202 $importer = new qformat_xml();
203 $q = $importer->try_importing_using_qtypes(
204 $xmldata['question'], null, null, 'gapselect');
205
5fa78cb4 206 $expectedq = new stdClass();
0d24b17a
TH
207 $expectedq->qtype = 'gapselect';
208 $expectedq->name = 'A select missing words question';
209 $expectedq->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
210 $expectedq->questiontextformat = FORMAT_MOODLE;
211 $expectedq->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
212 $expectedq->defaultmark = 3;
213 $expectedq->length = 1;
214 $expectedq->penalty = 0.3333333;
215
216 $expectedq->shuffleanswers = 1;
060e0294 217 $expectedq->correctfeedback = array('text' => '<p>Your answer is correct.</p>',
08243fa8 218 'format' => FORMAT_MOODLE);
060e0294
TH
219 $expectedq->partiallycorrectfeedback = array(
220 'text' => '<p>Your answer is partially correct.</p>',
08243fa8 221 'format' => FORMAT_MOODLE);
0d24b17a 222 $expectedq->shownumcorrect = true;
060e0294 223 $expectedq->incorrectfeedback = array('text' => '<p>Your answer is incorrect.</p>',
08243fa8 224 'format' => FORMAT_MOODLE);
0d24b17a
TH
225
226 $expectedq->choices = array(
aa6a0ff0
TH
227 array('answer' => 'Alpha', 'choicegroup' => 1),
228 array('answer' => 'Beta', 'choicegroup' => 1),
229 array('answer' => 'Gamma', 'choicegroup' => 1),
0d24b17a
TH
230 );
231
aa6a0ff0 232 $expectedq->hint = array(
08243fa8 233 array('text' => 'Try again.', 'format' => FORMAT_MOODLE),
060e0294 234 array('text' => 'These are the first three letters of the Greek alphabet.',
08243fa8 235 'format' => FORMAT_MOODLE));
0d24b17a
TH
236 $expectedq->hintshownumcorrect = array(true, true);
237 $expectedq->hintclearwrong = array(false, true);
b3adccfb
JP
238 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
239 $this->assertEquals($expectedq->hint, $q->hint);
0d24b17a
TH
240 }
241
242 public function test_xml_export() {
5fa78cb4 243 $qdata = new stdClass();
0d24b17a 244 $qdata->id = 123;
1796eb18 245 $qdata->contextid = \context_system::instance()->id;
0d24b17a
TH
246 $qdata->qtype = 'gapselect';
247 $qdata->name = 'A select missing words question';
248 $qdata->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
249 $qdata->questiontextformat = FORMAT_MOODLE;
250 $qdata->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
7540fcdc 251 $qdata->generalfeedbackformat = FORMAT_MOODLE;
0d24b17a
TH
252 $qdata->defaultmark = 3;
253 $qdata->length = 1;
254 $qdata->penalty = 0.3333333;
255 $qdata->hidden = 0;
256
b3adccfb 257 $qdata->options = new stdClass();
0d24b17a
TH
258 $qdata->options->shuffleanswers = 1;
259 $qdata->options->correctfeedback = '<p>Your answer is correct.</p>';
7540fcdc 260 $qdata->options->correctfeedbackformat = FORMAT_MOODLE;
0d24b17a 261 $qdata->options->partiallycorrectfeedback = '<p>Your answer is partially correct.</p>';
7540fcdc 262 $qdata->options->partiallycorrectfeedbackformat = FORMAT_MOODLE;
0d24b17a
TH
263 $qdata->options->shownumcorrect = true;
264 $qdata->options->incorrectfeedback = '<p>Your answer is incorrect.</p>';
7540fcdc 265 $qdata->options->incorrectfeedbackformat = FORMAT_MOODLE;
0d24b17a
TH
266
267 $qdata->options->answers = array(
7540fcdc
TH
268 13 => new question_answer(13, 'Alpha', 0, '1', FORMAT_MOODLE),
269 14 => new question_answer(14, 'Beta', 0, '1', FORMAT_MOODLE),
270 15 => new question_answer(15, 'Gamma', 0, '1', FORMAT_MOODLE),
0d24b17a
TH
271 );
272
273 $qdata->hints = array(
7540fcdc 274 1 => new question_hint_with_parts(1, 'Try again.', FORMAT_MOODLE, true, false),
060e0294
TH
275 2 => new question_hint_with_parts(2,
276 'These are the first three letters of the Greek alphabet.',
277 FORMAT_MOODLE, true, true),
0d24b17a
TH
278 );
279
280 $exporter = new qformat_xml();
281 $xml = $exporter->writequestion($qdata);
282
283 $expectedxml = '<!-- question: 123 -->
284 <question type="gapselect">
285 <name>
286 <text>A select missing words question</text>
287 </name>
288 <questiontext format="moodle_auto_format">
289 <text>Put these in order: [[1]], [[2]], [[3]].</text>
290 </questiontext>
7540fcdc 291 <generalfeedback format="moodle_auto_format">
0d24b17a
TH
292 <text>The answer is Alpha, Beta, Gamma.</text>
293 </generalfeedback>
294 <defaultgrade>3</defaultgrade>
295 <penalty>0.3333333</penalty>
296 <hidden>0</hidden>
297 <shuffleanswers>1</shuffleanswers>
7540fcdc 298 <correctfeedback format="moodle_auto_format">
0d24b17a
TH
299 <text><![CDATA[<p>Your answer is correct.</p>]]></text>
300 </correctfeedback>
7540fcdc 301 <partiallycorrectfeedback format="moodle_auto_format">
0d24b17a
TH
302 <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
303 </partiallycorrectfeedback>
7540fcdc 304 <incorrectfeedback format="moodle_auto_format">
0d24b17a
TH
305 <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
306 </incorrectfeedback>
307 <shownumcorrect/>
308 <selectoption>
309 <text>Alpha</text>
310 <group>1</group>
311 </selectoption>
312 <selectoption>
313 <text>Beta</text>
314 <group>1</group>
315 </selectoption>
316 <selectoption>
317 <text>Gamma</text>
318 <group>1</group>
319 </selectoption>
7540fcdc 320 <hint format="moodle_auto_format">
0d24b17a
TH
321 <text>Try again.</text>
322 <shownumcorrect/>
323 </hint>
7540fcdc 324 <hint format="moodle_auto_format">
0d24b17a
TH
325 <text>These are the first three letters of the Greek alphabet.</text>
326 <shownumcorrect/>
327 <clearwrong/>
328 </hint>
329 </question>
330';
331
332 $this->assert_same_xml($expectedxml, $xml);
333 }
334}