Commit | Line | Data |
---|---|---|
a2bb148a 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 drag-and-drop words into sentences question definition class. | |
21 | * | |
22 | * @package qtype_ddwtos | |
23 | * @copyright 2010 The Open University | |
24 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
25 | */ | |
26 | ||
27 | require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php'); | |
28 | require_once($CFG->dirroot . '/question/type/ddwtos/simpletest/helper.php'); | |
29 | ||
30 | ||
31 | /** | |
32 | * Unit tests for the matching question definition class. | |
33 | * | |
34 | * @copyright 2009 The Open University | |
35 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
36 | */ | |
37 | class qtype_ddwtos_question_test extends UnitTestCase { | |
38 | ||
39 | public function test_get_question_summary() { | |
40 | $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question(); | |
41 | $this->assertEqual('The [[1]] brown [[2]] jumped over the [[3]] dog.; [[1]] -> {quick / slow}; [[2]] -> {fox / dog}; [[3]] -> {lazy / assiduous}', | |
42 | $dd->get_question_summary()); | |
43 | } | |
44 | ||
45 | public function test_get_question_summary_maths() { | |
46 | $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question(); | |
47 | $this->assertEqual('Fill in the operators to make this equation work: ' . | |
48 | '7 [[1]] 11 [[2]] 13 [[1]] 17 [[2]] 19 = 3; [[1]] -> {+ / - / * / /}', | |
49 | $dd->get_question_summary()); | |
50 | } | |
51 | ||
52 | public function test_summarise_response() { | |
53 | $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question(); | |
54 | $dd->shufflechoices = false; | |
55 | $dd->init_first_step(new question_attempt_step()); | |
56 | ||
57 | $this->assertEqual('{quick} {fox} {lazy}', | |
58 | $dd->summarise_response(array('p1' => '1', 'p2' => '1', 'p3' => '1'))); | |
59 | } | |
60 | ||
61 | public function test_summarise_response_maths() { | |
62 | $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question(); | |
63 | $dd->shufflechoices = false; | |
64 | $dd->init_first_step(new question_attempt_step()); | |
65 | ||
66 | $this->assertEqual('{+} {-} {+} {-}', | |
67 | $dd->summarise_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2'))); | |
68 | } | |
69 | ||
70 | public function test_get_random_guess_score() { | |
71 | $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question(); | |
72 | $this->assertEqual(0.5, $dd->get_random_guess_score()); | |
73 | } | |
74 | ||
75 | public function test_get_random_guess_score_maths() { | |
76 | $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question(); | |
77 | $this->assertEqual(0.25, $dd->get_random_guess_score()); | |
78 | } | |
79 | ||
80 | public function test_get_right_choice_for() { | |
81 | $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question(); | |
82 | $dd->shufflechoices = false; | |
83 | $dd->init_first_step(new question_attempt_step()); | |
84 | ||
85 | $this->assertEqual(1, $dd->get_right_choice_for(1)); | |
86 | $this->assertEqual(1, $dd->get_right_choice_for(2)); | |
87 | } | |
88 | ||
89 | public function test_get_right_choice_for_maths() { | |
90 | $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question(); | |
91 | $dd->shufflechoices = false; | |
92 | $dd->init_first_step(new question_attempt_step()); | |
93 | ||
94 | $this->assertEqual(1, $dd->get_right_choice_for(1)); | |
95 | $this->assertEqual(2, $dd->get_right_choice_for(2)); | |
96 | } | |
97 | ||
98 | public function test_clear_wrong_from_response() { | |
99 | $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question(); | |
100 | $dd->shufflechoices = false; | |
101 | $dd->init_first_step(new question_attempt_step()); | |
102 | ||
103 | $initialresponse = array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1'); | |
104 | $this->assertEqual(array('p1' => '1', 'p2' => '0', 'p3' => '1', 'p4' => '0'), | |
105 | $dd->clear_wrong_from_response($initialresponse)); | |
106 | } | |
107 | ||
108 | public function test_get_num_parts_right() { | |
109 | $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question(); | |
110 | $dd->shufflechoices = false; | |
111 | $dd->init_first_step(new question_attempt_step()); | |
112 | ||
113 | $this->assertEqual(array(2, 3), | |
114 | $dd->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '2'))); | |
115 | $this->assertEqual(array(3, 3), | |
116 | $dd->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '1'))); | |
117 | } | |
118 | ||
119 | public function test_get_num_parts_right_maths() { | |
120 | $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question(); | |
121 | $dd->shufflechoices = false; | |
122 | $dd->init_first_step(new question_attempt_step()); | |
123 | ||
124 | $this->assertEqual(array(2, 4), | |
125 | $dd->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1'))); | |
126 | } | |
127 | ||
128 | public function test_get_expected_data() { | |
129 | $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question(); | |
130 | $dd->init_first_step(new question_attempt_step()); | |
131 | ||
132 | $this->assertEqual(array('p1' => PARAM_INT, 'p2' => PARAM_INT, 'p3' => PARAM_INT), | |
133 | $dd->get_expected_data()); | |
134 | } | |
135 | ||
136 | public function test_get_correct_response() { | |
137 | $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question(); | |
138 | $dd->shufflechoices = false; | |
139 | $dd->init_first_step(new question_attempt_step()); | |
140 | ||
141 | $this->assertEqual(array('p1' => '1', 'p2' => '1', 'p3' => '1'), | |
142 | $dd->get_correct_response()); | |
143 | } | |
144 | ||
145 | public function test_get_correct_response_maths() { | |
146 | $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question(); | |
147 | $dd->shufflechoices = false; | |
148 | $dd->init_first_step(new question_attempt_step()); | |
149 | ||
150 | $this->assertEqual(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2'), | |
151 | $dd->get_correct_response()); | |
152 | } | |
153 | ||
154 | public function test_is_same_response() { | |
155 | $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question(); | |
156 | $dd->init_first_step(new question_attempt_step()); | |
157 | ||
158 | $this->assertTrue($dd->is_same_response( | |
159 | array(), | |
160 | array('p1' => '0', 'p2' => '0', 'p3' => '0'))); | |
161 | ||
162 | $this->assertFalse($dd->is_same_response( | |
163 | array(), | |
164 | array('p1' => '1', 'p2' => '0', 'p3' => '0'))); | |
165 | ||
166 | $this->assertFalse($dd->is_same_response( | |
167 | array('p1' => '0', 'p2' => '0', 'p3' => '0'), | |
168 | array('p1' => '1', 'p2' => '0', 'p3' => '0'))); | |
169 | ||
170 | $this->assertTrue($dd->is_same_response( | |
171 | array('p1' => '1', 'p2' => '2', 'p3' => '3'), | |
172 | array('p1' => '1', 'p2' => '2', 'p3' => '3'))); | |
173 | ||
174 | $this->assertFalse($dd->is_same_response( | |
175 | array('p1' => '1', 'p2' => '2', 'p3' => '3'), | |
176 | array('p1' => '1', 'p2' => '2', 'p3' => '2'))); | |
177 | } | |
178 | public function test_is_complete_response() { | |
179 | $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question(); | |
180 | $dd->init_first_step(new question_attempt_step()); | |
181 | ||
182 | $this->assertFalse($dd->is_complete_response(array())); | |
183 | $this->assertFalse($dd->is_complete_response( | |
184 | array('p1' => '1', 'p2' => '1', 'p3' => '0'))); | |
185 | $this->assertFalse($dd->is_complete_response(array('p1' => '1'))); | |
186 | $this->assertTrue($dd->is_complete_response( | |
187 | array('p1' => '1', 'p2' => '1', 'p3' => '1'))); | |
188 | } | |
189 | ||
190 | public function test_is_gradable_response() { | |
191 | $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question(); | |
192 | $dd->init_first_step(new question_attempt_step()); | |
193 | ||
194 | $this->assertFalse($dd->is_gradable_response(array())); | |
195 | $this->assertFalse($dd->is_gradable_response( | |
196 | array('p1' => '0', 'p2' => '0', 'p3' => '0'))); | |
197 | $this->assertTrue($dd->is_gradable_response( | |
198 | array('p1' => '1', 'p2' => '1', 'p3' => '0'))); | |
199 | $this->assertTrue($dd->is_gradable_response(array('p1' => '1'))); | |
200 | $this->assertTrue($dd->is_gradable_response( | |
201 | array('p1' => '1', 'p2' => '1', 'p3' => '1'))); | |
202 | } | |
203 | ||
204 | public function test_grading() { | |
205 | $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question(); | |
206 | $dd->shufflechoices = false; | |
207 | $dd->init_first_step(new question_attempt_step()); | |
208 | ||
209 | $this->assertEqual(array(1, question_state::$gradedright), | |
210 | $dd->grade_response(array('p1' => '1', 'p2' => '1', 'p3' => '1'))); | |
211 | $this->assertEqual(array(1/3, question_state::$gradedpartial), | |
212 | $dd->grade_response(array('p1' => '1'))); | |
213 | $this->assertEqual(array(0, question_state::$gradedwrong), | |
214 | $dd->grade_response(array('p1' => '2', 'p2' => '2', 'p3' => '2'))); | |
215 | } | |
216 | ||
217 | public function test_grading_maths() { | |
218 | $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question(); | |
219 | $dd->shufflechoices = false; | |
220 | $dd->init_first_step(new question_attempt_step()); | |
221 | ||
222 | $this->assertEqual(array(1, question_state::$gradedright), | |
223 | $dd->grade_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2'))); | |
224 | $this->assertEqual(array(0.5, question_state::$gradedpartial), | |
225 | $dd->grade_response(array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1'))); | |
226 | $this->assertEqual(array(0, question_state::$gradedwrong), | |
227 | $dd->grade_response(array('p1' => '0', 'p2' => '1', 'p3' => '2', 'p4' => '1'))); | |
228 | } | |
229 | ||
230 | public function test_classify_response() { | |
231 | $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question(); | |
232 | $dd->shufflechoices = false; | |
233 | $dd->init_first_step(new question_attempt_step()); | |
234 | ||
235 | $this->assertEqual(array( | |
236 | 1 => new question_classified_response(1, 'quick', 1), | |
237 | 2 => new question_classified_response(2, 'dog', 0), | |
238 | 3 => new question_classified_response(1, 'lazy', 1), | |
239 | ), $dd->classify_response(array('p1' => '1', 'p2' => '2', 'p3' => '1'))); | |
240 | $this->assertEqual(array( | |
241 | 1 => question_classified_response::no_response(), | |
242 | 2 => new question_classified_response(1, 'fox', 1), | |
243 | 3 => new question_classified_response(2, 'assiduous', 0), | |
244 | ), $dd->classify_response(array('p1' => '0', 'p2' => '1', 'p3' => '2'))); | |
245 | } | |
246 | } |