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