MDL-47494 ddimageortext: Updated the readme file to add install instructions.
[moodle.git] / question / type / ddimageortext / simpletest / testquestion.php
CommitLineData
a2bb148a 1<?php
a2bb148a
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
a2bb148a
TH
17/**
18 * Unit tests for the drag-and-drop words into sentences question definition class.
19 *
5b4602d2
TH
20 * @package qtype
21 * @subpackage ddwtos
22 * @copyright 2010 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
a2bb148a
TH
24 */
25
5b4602d2 26
42536280
TH
27defined('MOODLE_INTERNAL') || die();
28
a2bb148a
TH
29require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php');
30require_once($CFG->dirroot . '/question/type/ddwtos/simpletest/helper.php');
31
32
33/**
34 * Unit tests for the matching question definition class.
35 *
5b4602d2
TH
36 * @copyright 2009 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
a2bb148a
TH
38 */
39class qtype_ddwtos_question_test extends UnitTestCase {
40
41 public function test_get_question_summary() {
46b0a973 42 $dd = test_question_maker::make_question('ddwtos');
944829e9
TH
43 $this->assertEqual('The [[1]] brown [[2]] jumped over the [[3]] dog.; ' .
44 '[[1]] -> {quick / slow}; [[2]] -> {fox / dog}; [[3]] -> {lazy / assiduous}',
a2bb148a
TH
45 $dd->get_question_summary());
46 }
47
48 public function test_get_question_summary_maths() {
46b0a973 49 $dd = test_question_maker::make_question('ddwtos', 'maths');
a2bb148a
TH
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() {
46b0a973 56 $dd = test_question_maker::make_question('ddwtos');
a2bb148a 57 $dd->shufflechoices = false;
81f5dc53 58 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
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() {
46b0a973 65 $dd = test_question_maker::make_question('ddwtos', 'maths');
a2bb148a 66 $dd->shufflechoices = false;
81f5dc53 67 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
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() {
46b0a973 74 $dd = test_question_maker::make_question('ddwtos');
a2bb148a
TH
75 $this->assertEqual(0.5, $dd->get_random_guess_score());
76 }
77
78 public function test_get_random_guess_score_maths() {
46b0a973 79 $dd = test_question_maker::make_question('ddwtos', 'maths');
a2bb148a
TH
80 $this->assertEqual(0.25, $dd->get_random_guess_score());
81 }
82
83 public function test_get_right_choice_for() {
46b0a973 84 $dd = test_question_maker::make_question('ddwtos');
a2bb148a 85 $dd->shufflechoices = false;
81f5dc53 86 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
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() {
46b0a973 93 $dd = test_question_maker::make_question('ddwtos', 'maths');
a2bb148a 94 $dd->shufflechoices = false;
81f5dc53 95 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
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() {
46b0a973 102 $dd = test_question_maker::make_question('ddwtos', 'maths');
a2bb148a 103 $dd->shufflechoices = false;
81f5dc53 104 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
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() {
46b0a973 112 $dd = test_question_maker::make_question('ddwtos');
a2bb148a 113 $dd->shufflechoices = false;
81f5dc53 114 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
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() {
46b0a973 123 $dd = test_question_maker::make_question('ddwtos', 'maths');
a2bb148a 124 $dd->shufflechoices = false;
81f5dc53 125 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
126
127 $this->assertEqual(array(2, 4),
944829e9
TH
128 $dd->get_num_parts_right(array(
129 'p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
a2bb148a
TH
130 }
131
132 public function test_get_expected_data() {
46b0a973 133 $dd = test_question_maker::make_question('ddwtos');
81f5dc53 134 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
135
136 $this->assertEqual(array('p1' => PARAM_INT, 'p2' => PARAM_INT, 'p3' => PARAM_INT),
137 $dd->get_expected_data());
138 }
139
140 public function test_get_correct_response() {
46b0a973 141 $dd = test_question_maker::make_question('ddwtos');
a2bb148a 142 $dd->shufflechoices = false;
81f5dc53 143 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
144
145 $this->assertEqual(array('p1' => '1', 'p2' => '1', 'p3' => '1'),
146 $dd->get_correct_response());
147 }
148
149 public function test_get_correct_response_maths() {
46b0a973 150 $dd = test_question_maker::make_question('ddwtos', 'maths');
a2bb148a 151 $dd->shufflechoices = false;
81f5dc53 152 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
153
154 $this->assertEqual(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2'),
155 $dd->get_correct_response());
156 }
157
158 public function test_is_same_response() {
46b0a973 159 $dd = test_question_maker::make_question('ddwtos');
81f5dc53 160 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
161
162 $this->assertTrue($dd->is_same_response(
163 array(),
164 array('p1' => '0', 'p2' => '0', 'p3' => '0')));
165
166 $this->assertFalse($dd->is_same_response(
167 array(),
168 array('p1' => '1', 'p2' => '0', 'p3' => '0')));
169
170 $this->assertFalse($dd->is_same_response(
171 array('p1' => '0', 'p2' => '0', 'p3' => '0'),
172 array('p1' => '1', 'p2' => '0', 'p3' => '0')));
173
174 $this->assertTrue($dd->is_same_response(
175 array('p1' => '1', 'p2' => '2', 'p3' => '3'),
176 array('p1' => '1', 'p2' => '2', 'p3' => '3')));
177
178 $this->assertFalse($dd->is_same_response(
179 array('p1' => '1', 'p2' => '2', 'p3' => '3'),
180 array('p1' => '1', 'p2' => '2', 'p3' => '2')));
181 }
182 public function test_is_complete_response() {
46b0a973 183 $dd = test_question_maker::make_question('ddwtos');
81f5dc53 184 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
185
186 $this->assertFalse($dd->is_complete_response(array()));
187 $this->assertFalse($dd->is_complete_response(
188 array('p1' => '1', 'p2' => '1', 'p3' => '0')));
189 $this->assertFalse($dd->is_complete_response(array('p1' => '1')));
190 $this->assertTrue($dd->is_complete_response(
191 array('p1' => '1', 'p2' => '1', 'p3' => '1')));
192 }
193
194 public function test_is_gradable_response() {
46b0a973 195 $dd = test_question_maker::make_question('ddwtos');
81f5dc53 196 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
197
198 $this->assertFalse($dd->is_gradable_response(array()));
199 $this->assertFalse($dd->is_gradable_response(
200 array('p1' => '0', 'p2' => '0', 'p3' => '0')));
201 $this->assertTrue($dd->is_gradable_response(
202 array('p1' => '1', 'p2' => '1', 'p3' => '0')));
203 $this->assertTrue($dd->is_gradable_response(array('p1' => '1')));
204 $this->assertTrue($dd->is_gradable_response(
205 array('p1' => '1', 'p2' => '1', 'p3' => '1')));
206 }
207
208 public function test_grading() {
46b0a973 209 $dd = test_question_maker::make_question('ddwtos');
a2bb148a 210 $dd->shufflechoices = false;
81f5dc53 211 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
212
213 $this->assertEqual(array(1, question_state::$gradedright),
214 $dd->grade_response(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
215 $this->assertEqual(array(1/3, question_state::$gradedpartial),
216 $dd->grade_response(array('p1' => '1')));
217 $this->assertEqual(array(0, question_state::$gradedwrong),
218 $dd->grade_response(array('p1' => '2', 'p2' => '2', 'p3' => '2')));
219 }
220
221 public function test_grading_maths() {
46b0a973 222 $dd = test_question_maker::make_question('ddwtos', 'maths');
a2bb148a 223 $dd->shufflechoices = false;
81f5dc53 224 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
225
226 $this->assertEqual(array(1, question_state::$gradedright),
227 $dd->grade_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
228 $this->assertEqual(array(0.5, question_state::$gradedpartial),
229 $dd->grade_response(array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
230 $this->assertEqual(array(0, question_state::$gradedwrong),
231 $dd->grade_response(array('p1' => '0', 'p2' => '1', 'p3' => '2', 'p4' => '1')));
232 }
233
234 public function test_classify_response() {
46b0a973 235 $dd = test_question_maker::make_question('ddwtos');
a2bb148a 236 $dd->shufflechoices = false;
81f5dc53 237 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
238
239 $this->assertEqual(array(
240 1 => new question_classified_response(1, 'quick', 1),
241 2 => new question_classified_response(2, 'dog', 0),
242 3 => new question_classified_response(1, 'lazy', 1),
243 ), $dd->classify_response(array('p1' => '1', 'p2' => '2', 'p3' => '1')));
244 $this->assertEqual(array(
245 1 => question_classified_response::no_response(),
246 2 => new question_classified_response(1, 'fox', 1),
247 3 => new question_classified_response(2, 'assiduous', 0),
248 ), $dd->classify_response(array('p1' => '0', 'p2' => '1', 'p3' => '2')));
249 }
250}