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