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