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