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