Commit | Line | Data |
---|---|---|
0d24b17a TH |
1 | <?php |
2 | ||
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
0d24b17a TH |
18 | /** |
19 | * Unit tests for the select missing words question question definition class. | |
20 | * | |
9df0480d TH |
21 | * @package qtype |
22 | * @subpackage gapselect | |
23 | * @copyright 2011 The Open University | |
24 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
0d24b17a TH |
25 | */ |
26 | ||
27 | ||
b28ad86a TH |
28 | defined('MOODLE_INTERNAL') || die(); |
29 | ||
0d24b17a TH |
30 | require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php'); |
31 | require_once($CFG->dirroot . '/question/type/gapselect/simpletest/helper.php'); | |
32 | ||
33 | ||
34 | /** | |
35 | * Unit tests for the select missing words question definition class. | |
36 | * | |
9df0480d TH |
37 | * @copyright 2011 The Open University |
38 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
0d24b17a TH |
39 | */ |
40 | class qtype_gapselect_test extends UnitTestCase { | |
41 | /** @var qtype_gapselect instance of the question type class to test. */ | |
42 | protected $qtype; | |
43 | ||
44 | public function setUp() { | |
45 | $this->qtype = question_bank::get_qtype('gapselect');; | |
46 | } | |
47 | ||
48 | public function tearDown() { | |
49 | $this->qtype = null; | |
50 | } | |
51 | ||
52 | public function assert_same_xml($expectedxml, $xml) { | |
53 | $this->assertEqual(str_replace("\r\n", "\n", $expectedxml), | |
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 | ||
87 | $gapselect->options->shuffleanswers = true; | |
88 | ||
89 | test_question_maker::set_standard_combined_feedback_fields($gapselect->options); | |
90 | ||
91 | $gapselect->options->answers = array( | |
92 | (object) array('answer' => 'quick', 'feedback' => '1'), | |
93 | (object) array('answer' => 'fox', 'feedback' => '2'), | |
94 | (object) array('answer' => 'lazy', 'feedback' => '3'), | |
95 | (object) array('answer' => 'assiduous', 'feedback' => '3'), | |
96 | (object) array('answer' => 'dog', 'feedback' => '2'), | |
97 | (object) array('answer' => 'slow', 'feedback' => '1'), | |
98 | ); | |
99 | ||
100 | return $gapselect; | |
101 | } | |
102 | ||
103 | public function test_name() { | |
104 | $this->assertEqual($this->qtype->name(), 'gapselect'); | |
105 | } | |
106 | ||
107 | public function test_can_analyse_responses() { | |
108 | $this->assertTrue($this->qtype->can_analyse_responses()); | |
109 | } | |
110 | ||
111 | public function test_initialise_question_instance() { | |
112 | $qdata = $this->get_test_question_data(); | |
113 | ||
114 | $expected = qtype_gapselect_test_helper::make_a_gapselect_question(); | |
115 | $expected->stamp = $qdata->stamp; | |
116 | $expected->version = $qdata->version; | |
117 | ||
118 | $q = $this->qtype->make_question($qdata); | |
119 | ||
120 | $this->assertEqual($expected, $q); | |
121 | } | |
122 | ||
123 | public function test_get_random_guess_score() { | |
124 | $q = $this->get_test_question_data(); | |
125 | $this->assertWithinMargin(0.5, $this->qtype->get_random_guess_score($q), 0.0000001); | |
126 | } | |
127 | ||
128 | public function test_get_possible_responses() { | |
129 | $q = $this->get_test_question_data(); | |
130 | ||
131 | $this->assertEqual(array( | |
132 | 1 => array( | |
133 | 1 => new question_possible_response('quick', 1), | |
134 | 2 => new question_possible_response('slow', 0), | |
135 | null => question_possible_response::no_response()), | |
136 | 2 => array( | |
137 | 1 => new question_possible_response('fox', 1), | |
138 | 2 => new question_possible_response('dog', 0), | |
139 | null => question_possible_response::no_response()), | |
140 | 3 => array( | |
141 | 1 => new question_possible_response('lazy', 1), | |
142 | 2 => new question_possible_response('assiduous', 0), | |
143 | null => question_possible_response::no_response()), | |
144 | ), $this->qtype->get_possible_responses($q)); | |
145 | } | |
146 | ||
147 | public function test_xml_import() { | |
148 | $xml = ' <question type="gapselect"> | |
149 | <name> | |
150 | <text>A select missing words question</text> | |
151 | </name> | |
152 | <questiontext format="moodle_auto_format"> | |
153 | <text>Put these in order: [[1]], [[2]], [[3]].</text> | |
154 | </questiontext> | |
155 | <generalfeedback> | |
156 | <text>The answer is Alpha, Beta, Gamma.</text> | |
157 | </generalfeedback> | |
158 | <defaultgrade>3</defaultgrade> | |
159 | <penalty>0.3333333</penalty> | |
160 | <hidden>0</hidden> | |
161 | <shuffleanswers>1</shuffleanswers> | |
162 | <correctfeedback> | |
163 | <text><![CDATA[<p>Your answer is correct.</p>]]></text> | |
164 | </correctfeedback> | |
165 | <partiallycorrectfeedback> | |
166 | <text><![CDATA[<p>Your answer is partially correct.</p>]]></text> | |
167 | </partiallycorrectfeedback> | |
168 | <incorrectfeedback> | |
169 | <text><![CDATA[<p>Your answer is incorrect.</p>]]></text> | |
170 | </incorrectfeedback> | |
171 | <shownumcorrect/> | |
172 | <selectoption> | |
173 | <text>Alpha</text> | |
174 | <group>1</group> | |
175 | </selectoption> | |
176 | <selectoption> | |
177 | <text>Beta</text> | |
178 | <group>1</group> | |
179 | </selectoption> | |
180 | <selectoption> | |
181 | <text>Gamma</text> | |
182 | <group>1</group> | |
183 | </selectoption> | |
184 | <hint> | |
185 | <text>Try again.</text> | |
186 | <shownumcorrect /> | |
187 | </hint> | |
188 | <hint> | |
189 | <text>These are the first three letters of the Greek alphabet.</text> | |
190 | <shownumcorrect /> | |
191 | <clearwrong /> | |
192 | </hint> | |
193 | </question>'; | |
194 | $xmldata = xmlize($xml); | |
195 | ||
196 | $importer = new qformat_xml(); | |
197 | $q = $importer->try_importing_using_qtypes( | |
198 | $xmldata['question'], null, null, 'gapselect'); | |
199 | ||
5fa78cb4 | 200 | $expectedq = new stdClass(); |
0d24b17a TH |
201 | $expectedq->qtype = 'gapselect'; |
202 | $expectedq->name = 'A select missing words question'; | |
203 | $expectedq->questiontext = 'Put these in order: [[1]], [[2]], [[3]].'; | |
204 | $expectedq->questiontextformat = FORMAT_MOODLE; | |
205 | $expectedq->generalfeedback = 'The answer is Alpha, Beta, Gamma.'; | |
206 | $expectedq->defaultmark = 3; | |
207 | $expectedq->length = 1; | |
208 | $expectedq->penalty = 0.3333333; | |
209 | ||
210 | $expectedq->shuffleanswers = 1; | |
7540fcdc TH |
211 | $expectedq->correctfeedback = array('text' => '<p>Your answer is correct.</p>', 'format' => FORMAT_MOODLE, 'files' => array()); |
212 | $expectedq->partiallycorrectfeedback = array('text' => '<p>Your answer is partially correct.</p>', 'format' => FORMAT_MOODLE, 'files' => array()); | |
0d24b17a | 213 | $expectedq->shownumcorrect = true; |
7540fcdc | 214 | $expectedq->incorrectfeedback = array('text' => '<p>Your answer is incorrect.</p>', 'format' => FORMAT_MOODLE, 'files' => array()); |
0d24b17a TH |
215 | |
216 | $expectedq->choices = array( | |
aa6a0ff0 TH |
217 | array('answer' => 'Alpha', 'choicegroup' => 1), |
218 | array('answer' => 'Beta', 'choicegroup' => 1), | |
219 | array('answer' => 'Gamma', 'choicegroup' => 1), | |
0d24b17a TH |
220 | ); |
221 | ||
aa6a0ff0 TH |
222 | $expectedq->hint = array( |
223 | array('text' => 'Try again.', 'format' => FORMAT_MOODLE, 'files' => array()), | |
224 | array('text' => 'These are the first three letters of the Greek alphabet.', 'format' => FORMAT_MOODLE, 'files' => array())); | |
0d24b17a TH |
225 | $expectedq->hintshownumcorrect = array(true, true); |
226 | $expectedq->hintclearwrong = array(false, true); | |
227 | ||
228 | $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q); | |
aa6a0ff0 | 229 | $this->assertEqual($expectedq->hint, $q->hint); |
0d24b17a TH |
230 | } |
231 | ||
232 | public function test_xml_export() { | |
5fa78cb4 | 233 | $qdata = new stdClass(); |
0d24b17a | 234 | $qdata->id = 123; |
7540fcdc | 235 | $qdata->contextid = 0; |
0d24b17a TH |
236 | $qdata->qtype = 'gapselect'; |
237 | $qdata->name = 'A select missing words question'; | |
238 | $qdata->questiontext = 'Put these in order: [[1]], [[2]], [[3]].'; | |
239 | $qdata->questiontextformat = FORMAT_MOODLE; | |
240 | $qdata->generalfeedback = 'The answer is Alpha, Beta, Gamma.'; | |
7540fcdc | 241 | $qdata->generalfeedbackformat = FORMAT_MOODLE; |
0d24b17a TH |
242 | $qdata->defaultmark = 3; |
243 | $qdata->length = 1; | |
244 | $qdata->penalty = 0.3333333; | |
245 | $qdata->hidden = 0; | |
246 | ||
247 | $qdata->options->shuffleanswers = 1; | |
248 | $qdata->options->correctfeedback = '<p>Your answer is correct.</p>'; | |
7540fcdc | 249 | $qdata->options->correctfeedbackformat = FORMAT_MOODLE; |
0d24b17a | 250 | $qdata->options->partiallycorrectfeedback = '<p>Your answer is partially correct.</p>'; |
7540fcdc | 251 | $qdata->options->partiallycorrectfeedbackformat = FORMAT_MOODLE; |
0d24b17a TH |
252 | $qdata->options->shownumcorrect = true; |
253 | $qdata->options->incorrectfeedback = '<p>Your answer is incorrect.</p>'; | |
7540fcdc | 254 | $qdata->options->incorrectfeedbackformat = FORMAT_MOODLE; |
0d24b17a TH |
255 | |
256 | $qdata->options->answers = array( | |
7540fcdc TH |
257 | 13 => new question_answer(13, 'Alpha', 0, '1', FORMAT_MOODLE), |
258 | 14 => new question_answer(14, 'Beta', 0, '1', FORMAT_MOODLE), | |
259 | 15 => new question_answer(15, 'Gamma', 0, '1', FORMAT_MOODLE), | |
0d24b17a TH |
260 | ); |
261 | ||
262 | $qdata->hints = array( | |
7540fcdc TH |
263 | 1 => new question_hint_with_parts(1, 'Try again.', FORMAT_MOODLE, true, false), |
264 | 2 => new question_hint_with_parts(2, 'These are the first three letters of the Greek alphabet.', 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> | |
284 | <shuffleanswers>1</shuffleanswers> | |
7540fcdc | 285 | <correctfeedback format="moodle_auto_format"> |
0d24b17a TH |
286 | <text><![CDATA[<p>Your answer is correct.</p>]]></text> |
287 | </correctfeedback> | |
7540fcdc | 288 | <partiallycorrectfeedback format="moodle_auto_format"> |
0d24b17a TH |
289 | <text><![CDATA[<p>Your answer is partially correct.</p>]]></text> |
290 | </partiallycorrectfeedback> | |
7540fcdc | 291 | <incorrectfeedback format="moodle_auto_format"> |
0d24b17a TH |
292 | <text><![CDATA[<p>Your answer is incorrect.</p>]]></text> |
293 | </incorrectfeedback> | |
294 | <shownumcorrect/> | |
295 | <selectoption> | |
296 | <text>Alpha</text> | |
297 | <group>1</group> | |
298 | </selectoption> | |
299 | <selectoption> | |
300 | <text>Beta</text> | |
301 | <group>1</group> | |
302 | </selectoption> | |
303 | <selectoption> | |
304 | <text>Gamma</text> | |
305 | <group>1</group> | |
306 | </selectoption> | |
7540fcdc | 307 | <hint format="moodle_auto_format"> |
0d24b17a TH |
308 | <text>Try again.</text> |
309 | <shownumcorrect/> | |
310 | </hint> | |
7540fcdc | 311 | <hint format="moodle_auto_format"> |
0d24b17a TH |
312 | <text>These are the first three letters of the Greek alphabet.</text> |
313 | <shownumcorrect/> | |
314 | <clearwrong/> | |
315 | </hint> | |
316 | </question> | |
317 | '; | |
318 | ||
319 | $this->assert_same_xml($expectedxml, $xml); | |
320 | } | |
321 | } |