MDL-47494 ddimageortext: NOBUG continued converting functionality of question type...
[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 20 * @package qtype
a7098511 21 * @subpackage ddimagetoimage
5b4602d2
TH
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 29require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php');
a7098511 30require_once($CFG->dirroot . '/question/type/ddimagetoimage/simpletest/helper.php');
a2bb148a
TH
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 38 */
a7098511 39class qtype_ddimagetoimage_question_test extends UnitTestCase {
a2bb148a
TH
40
41 public function test_get_question_summary() {
a7098511 42 $dd = test_question_maker::make_question('ddimagetoimage');
bfe79134
JP
43 $this->assertEqual('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}; '.
46 '[[Drop zone 3]] -> {1. lazy / 2. dog}; '.
47 '[[Drop zone 4]] -> {1. lazy / 2. dog}',
48 $dd->get_question_summary());
a2bb148a
TH
49 }
50
51 public function test_get_question_summary_maths() {
a7098511 52 $dd = test_question_maker::make_question('ddimagetoimage', 'maths');
bfe79134
JP
53 $this->assertEqual('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() {
a7098511 63 $dd = test_question_maker::make_question('ddimagetoimage');
a2bb148a 64 $dd->shufflechoices = false;
a8448b3f 65 $dd->start_attempt(new question_attempt_step(), 1);
a2bb148a 66
bfe79134
JP
67 $this->assertEqual('Drop zone 1 -> {1. quick} '.
68 'Drop zone 2 -> {1. quick} '.
69 'Drop zone 3 -> {1. lazy} '.
70 'Drop zone 4 -> {1. lazy}',
71 $dd->summarise_response(array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
a2bb148a
TH
72 }
73
74 public function test_summarise_response_maths() {
a7098511 75 $dd = test_question_maker::make_question('ddimagetoimage', 'maths');
a2bb148a 76 $dd->shufflechoices = false;
a8448b3f 77 $dd->start_attempt(new question_attempt_step(), 1);
a2bb148a 78
bfe79134
JP
79 $this->assertEqual('Drop zone 1 -> {1. +} '.
80 'Drop zone 2 -> {2. -} '.
81 'Drop zone 3 -> {1. +} '.
82 'Drop zone 4 -> {2. -}',
a2bb148a
TH
83 $dd->summarise_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
84 }
85
86 public function test_get_random_guess_score() {
a7098511 87 $dd = test_question_maker::make_question('ddimagetoimage');
a2bb148a
TH
88 $this->assertEqual(0.5, $dd->get_random_guess_score());
89 }
90
91 public function test_get_random_guess_score_maths() {
a7098511 92 $dd = test_question_maker::make_question('ddimagetoimage', 'maths');
bfe79134 93 $this->assertEqual(0.5, $dd->get_random_guess_score());
a2bb148a
TH
94 }
95
96 public function test_get_right_choice_for() {
a7098511 97 $dd = test_question_maker::make_question('ddimagetoimage');
a2bb148a 98 $dd->shufflechoices = false;
a8448b3f 99 $dd->start_attempt(new question_attempt_step(), 1);
a2bb148a
TH
100
101 $this->assertEqual(1, $dd->get_right_choice_for(1));
bfe79134 102 $this->assertEqual(2, $dd->get_right_choice_for(2));
a2bb148a
TH
103 }
104
105 public function test_get_right_choice_for_maths() {
a7098511 106 $dd = test_question_maker::make_question('ddimagetoimage', 'maths');
a2bb148a 107 $dd->shufflechoices = false;
a8448b3f 108 $dd->start_attempt(new question_attempt_step(), 1);
a2bb148a
TH
109
110 $this->assertEqual(1, $dd->get_right_choice_for(1));
111 $this->assertEqual(2, $dd->get_right_choice_for(2));
bfe79134
JP
112 $this->assertEqual(1, $dd->get_right_choice_for(3));
113 $this->assertEqual(2, $dd->get_right_choice_for(4));
a2bb148a
TH
114 }
115
116 public function test_clear_wrong_from_response() {
a7098511 117 $dd = test_question_maker::make_question('ddimagetoimage', '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');
bfe79134 122 $this->assertEqual(array('p1' => '1', 'p2' => '', 'p3' => '1', 'p4' => ''),
a2bb148a
TH
123 $dd->clear_wrong_from_response($initialresponse));
124 }
125
126 public function test_get_num_parts_right() {
a7098511 127 $dd = test_question_maker::make_question('ddimagetoimage');
a2bb148a 128 $dd->shufflechoices = false;
a8448b3f 129 $dd->start_attempt(new question_attempt_step(), 1);
a2bb148a 130
bfe79134
JP
131 $this->assertEqual(array(2, 4),
132 $dd->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '2', 'p4' => '2')));
133 $this->assertEqual(array(4, 4),
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() {
a7098511 138 $dd = test_question_maker::make_question('ddimagetoimage', 'maths');
a2bb148a 139 $dd->shufflechoices = false;
a8448b3f 140 $dd->start_attempt(new question_attempt_step(), 1);
a2bb148a
TH
141
142 $this->assertEqual(array(2, 4),
944829e9
TH
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() {
a7098511 148 $dd = test_question_maker::make_question('ddimagetoimage');
a8448b3f 149 $dd->start_attempt(new question_attempt_step(), 1);
a2bb148a 150
bfe79134
JP
151 $this->assertEqual(
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() {
a7098511 158 $dd = test_question_maker::make_question('ddimagetoimage');
a2bb148a 159 $dd->shufflechoices = false;
a8448b3f 160 $dd->start_attempt(new question_attempt_step(), 1);
a2bb148a 161
bfe79134 162 $this->assertEqual(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2'),
a2bb148a
TH
163 $dd->get_correct_response());
164 }
165
166 public function test_get_correct_response_maths() {
a7098511 167 $dd = test_question_maker::make_question('ddimagetoimage', 'maths');
a2bb148a 168 $dd->shufflechoices = false;
a8448b3f 169 $dd->start_attempt(new question_attempt_step(), 1);
a2bb148a
TH
170
171 $this->assertEqual(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2'),
172 $dd->get_correct_response());
173 }
174
175 public function test_is_same_response() {
a7098511 176 $dd = test_question_maker::make_question('ddimagetoimage');
a8448b3f 177 $dd->start_attempt(new question_attempt_step(), 1);
a2bb148a
TH
178
179 $this->assertTrue($dd->is_same_response(
180 array(),
bfe79134 181 array('p1' => '', 'p2' => '', 'p3' => '', 'p4' => '')));
a2bb148a
TH
182
183 $this->assertFalse($dd->is_same_response(
184 array(),
bfe79134 185 array('p1' => '1', 'p2' => '', 'p3' => '', 'p4' => '')));
a2bb148a
TH
186
187 $this->assertFalse($dd->is_same_response(
bfe79134
JP
188 array('p1' => '', 'p2' => '', 'p3' => '', 'p4' => ''),
189 array('p1' => '1', 'p2' => '', 'p3' => '', 'p4' => '')));
a2bb148a
TH
190
191 $this->assertTrue($dd->is_same_response(
bfe79134
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(
bfe79134
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() {
a7098511 200 $dd = test_question_maker::make_question('ddimagetoimage');
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(
bfe79134 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(
bfe79134 208 array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
a2bb148a
TH
209 }
210
211 public function test_is_gradable_response() {
a7098511 212 $dd = test_question_maker::make_question('ddimagetoimage');
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(
bfe79134 217 array('p1' => '', 'p2' => '', 'p3' => '', 'p3' => '')));
a2bb148a 218 $this->assertTrue($dd->is_gradable_response(
bfe79134 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(
222 array('p1' => '1', 'p2' => '1', 'p3' => '1')));
223 }
224
225 public function test_grading() {
a7098511 226 $dd = test_question_maker::make_question('ddimagetoimage');
a2bb148a 227 $dd->shufflechoices = false;
a8448b3f 228 $dd->start_attempt(new question_attempt_step(), 1);
a2bb148a
TH
229
230 $this->assertEqual(array(1, question_state::$gradedright),
bfe79134
JP
231 $dd->grade_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
232 $this->assertEqual(array(0.25, question_state::$gradedpartial),
a2bb148a
TH
233 $dd->grade_response(array('p1' => '1')));
234 $this->assertEqual(array(0, question_state::$gradedwrong),
bfe79134 235 $dd->grade_response(array('p1' => '2', 'p2' => '1', 'p3' => '2', 'p4' => '1')));
a2bb148a
TH
236 }
237
238 public function test_grading_maths() {
a7098511 239 $dd = test_question_maker::make_question('ddimagetoimage', 'maths');
a2bb148a 240 $dd->shufflechoices = false;
a8448b3f 241 $dd->start_attempt(new question_attempt_step(), 1);
a2bb148a
TH
242
243 $this->assertEqual(array(1, question_state::$gradedright),
244 $dd->grade_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
245 $this->assertEqual(array(0.5, question_state::$gradedpartial),
246 $dd->grade_response(array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
247 $this->assertEqual(array(0, question_state::$gradedwrong),
bfe79134 248 $dd->grade_response(array('p1' => '', 'p2' => '1', 'p3' => '2', 'p4' => '1')));
a2bb148a
TH
249 }
250
251 public function test_classify_response() {
a7098511 252 $dd = test_question_maker::make_question('ddimagetoimage');
a2bb148a 253 $dd->shufflechoices = false;
a8448b3f 254 $dd->start_attempt(new question_attempt_step(), 1);
a2bb148a
TH
255
256 $this->assertEqual(array(
bfe79134
JP
257 1 => new question_classified_response(1, '1. quick', 1),
258 2 => new question_classified_response(2, '2. fox', 1),
259 3 => new question_classified_response(1, '1. lazy', 1),
260 4 => new question_classified_response(2, '2. dog', 1)
261 ), $dd->classify_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
a2bb148a
TH
262 $this->assertEqual(array(
263 1 => question_classified_response::no_response(),
bfe79134
JP
264 2 => new question_classified_response(1, '1. quick', 0),
265 3 => new question_classified_response(2, '2. dog', 0),
266 4 => new question_classified_response(2, '2. dog', 1)
267 ), $dd->classify_response(array('p1' => '', 'p2' => '1', 'p3' => '2', 'p4' => '2')));
a2bb148a
TH
268 }
269}