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