MDL-47494 ddimageortext: Fix codechecker problems in match, description and ddwtos...
[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();
944829e9
TH
44 $this->assertEqual('The [[1]] brown [[2]] jumped over the [[3]] dog.; ' .
45 '[[1]] -> {quick / slow}; [[2]] -> {fox / dog}; [[3]] -> {lazy / assiduous}',
a2bb148a
TH
46 $dd->get_question_summary());
47 }
48
49 public function test_get_question_summary_maths() {
50 $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question();
51 $this->assertEqual('Fill in the operators to make this equation work: ' .
52 '7 [[1]] 11 [[2]] 13 [[1]] 17 [[2]] 19 = 3; [[1]] -> {+ / - / * / /}',
53 $dd->get_question_summary());
54 }
55
56 public function test_summarise_response() {
57 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
58 $dd->shufflechoices = false;
81f5dc53 59 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
60
61 $this->assertEqual('{quick} {fox} {lazy}',
62 $dd->summarise_response(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
63 }
64
65 public function test_summarise_response_maths() {
66 $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question();
67 $dd->shufflechoices = false;
81f5dc53 68 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
69
70 $this->assertEqual('{+} {-} {+} {-}',
71 $dd->summarise_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
72 }
73
74 public function test_get_random_guess_score() {
75 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
76 $this->assertEqual(0.5, $dd->get_random_guess_score());
77 }
78
79 public function test_get_random_guess_score_maths() {
80 $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question();
81 $this->assertEqual(0.25, $dd->get_random_guess_score());
82 }
83
84 public function test_get_right_choice_for() {
85 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
86 $dd->shufflechoices = false;
81f5dc53 87 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
88
89 $this->assertEqual(1, $dd->get_right_choice_for(1));
90 $this->assertEqual(1, $dd->get_right_choice_for(2));
91 }
92
93 public function test_get_right_choice_for_maths() {
94 $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question();
95 $dd->shufflechoices = false;
81f5dc53 96 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
97
98 $this->assertEqual(1, $dd->get_right_choice_for(1));
99 $this->assertEqual(2, $dd->get_right_choice_for(2));
100 }
101
102 public function test_clear_wrong_from_response() {
103 $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question();
104 $dd->shufflechoices = false;
81f5dc53 105 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
106
107 $initialresponse = array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1');
108 $this->assertEqual(array('p1' => '1', 'p2' => '0', 'p3' => '1', 'p4' => '0'),
109 $dd->clear_wrong_from_response($initialresponse));
110 }
111
112 public function test_get_num_parts_right() {
113 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
114 $dd->shufflechoices = false;
81f5dc53 115 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
116
117 $this->assertEqual(array(2, 3),
118 $dd->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '2')));
119 $this->assertEqual(array(3, 3),
120 $dd->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
121 }
122
123 public function test_get_num_parts_right_maths() {
124 $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question();
125 $dd->shufflechoices = false;
81f5dc53 126 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
127
128 $this->assertEqual(array(2, 4),
944829e9
TH
129 $dd->get_num_parts_right(array(
130 'p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
a2bb148a
TH
131 }
132
133 public function test_get_expected_data() {
134 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
81f5dc53 135 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
136
137 $this->assertEqual(array('p1' => PARAM_INT, 'p2' => PARAM_INT, 'p3' => PARAM_INT),
138 $dd->get_expected_data());
139 }
140
141 public function test_get_correct_response() {
142 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
143 $dd->shufflechoices = false;
81f5dc53 144 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
145
146 $this->assertEqual(array('p1' => '1', 'p2' => '1', 'p3' => '1'),
147 $dd->get_correct_response());
148 }
149
150 public function test_get_correct_response_maths() {
151 $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question();
152 $dd->shufflechoices = false;
81f5dc53 153 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
154
155 $this->assertEqual(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2'),
156 $dd->get_correct_response());
157 }
158
159 public function test_is_same_response() {
160 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
81f5dc53 161 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
162
163 $this->assertTrue($dd->is_same_response(
164 array(),
165 array('p1' => '0', 'p2' => '0', 'p3' => '0')));
166
167 $this->assertFalse($dd->is_same_response(
168 array(),
169 array('p1' => '1', 'p2' => '0', 'p3' => '0')));
170
171 $this->assertFalse($dd->is_same_response(
172 array('p1' => '0', 'p2' => '0', 'p3' => '0'),
173 array('p1' => '1', 'p2' => '0', 'p3' => '0')));
174
175 $this->assertTrue($dd->is_same_response(
176 array('p1' => '1', 'p2' => '2', 'p3' => '3'),
177 array('p1' => '1', 'p2' => '2', 'p3' => '3')));
178
179 $this->assertFalse($dd->is_same_response(
180 array('p1' => '1', 'p2' => '2', 'p3' => '3'),
181 array('p1' => '1', 'p2' => '2', 'p3' => '2')));
182 }
183 public function test_is_complete_response() {
184 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
81f5dc53 185 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
186
187 $this->assertFalse($dd->is_complete_response(array()));
188 $this->assertFalse($dd->is_complete_response(
189 array('p1' => '1', 'p2' => '1', 'p3' => '0')));
190 $this->assertFalse($dd->is_complete_response(array('p1' => '1')));
191 $this->assertTrue($dd->is_complete_response(
192 array('p1' => '1', 'p2' => '1', 'p3' => '1')));
193 }
194
195 public function test_is_gradable_response() {
196 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
81f5dc53 197 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
198
199 $this->assertFalse($dd->is_gradable_response(array()));
200 $this->assertFalse($dd->is_gradable_response(
201 array('p1' => '0', 'p2' => '0', 'p3' => '0')));
202 $this->assertTrue($dd->is_gradable_response(
203 array('p1' => '1', 'p2' => '1', 'p3' => '0')));
204 $this->assertTrue($dd->is_gradable_response(array('p1' => '1')));
205 $this->assertTrue($dd->is_gradable_response(
206 array('p1' => '1', 'p2' => '1', 'p3' => '1')));
207 }
208
209 public function test_grading() {
210 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
211 $dd->shufflechoices = false;
81f5dc53 212 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
213
214 $this->assertEqual(array(1, question_state::$gradedright),
215 $dd->grade_response(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
216 $this->assertEqual(array(1/3, question_state::$gradedpartial),
217 $dd->grade_response(array('p1' => '1')));
218 $this->assertEqual(array(0, question_state::$gradedwrong),
219 $dd->grade_response(array('p1' => '2', 'p2' => '2', 'p3' => '2')));
220 }
221
222 public function test_grading_maths() {
223 $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question();
224 $dd->shufflechoices = false;
81f5dc53 225 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
226
227 $this->assertEqual(array(1, question_state::$gradedright),
228 $dd->grade_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
229 $this->assertEqual(array(0.5, question_state::$gradedpartial),
230 $dd->grade_response(array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
231 $this->assertEqual(array(0, question_state::$gradedwrong),
232 $dd->grade_response(array('p1' => '0', 'p2' => '1', 'p3' => '2', 'p4' => '1')));
233 }
234
235 public function test_classify_response() {
236 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
237 $dd->shufflechoices = false;
81f5dc53 238 $dd->start_attempt(new question_attempt_step());
a2bb148a
TH
239
240 $this->assertEqual(array(
241 1 => new question_classified_response(1, 'quick', 1),
242 2 => new question_classified_response(2, 'dog', 0),
243 3 => new question_classified_response(1, 'lazy', 1),
244 ), $dd->classify_response(array('p1' => '1', 'p2' => '2', 'p3' => '1')));
245 $this->assertEqual(array(
246 1 => question_classified_response::no_response(),
247 2 => new question_classified_response(1, 'fox', 1),
248 3 => new question_classified_response(2, 'assiduous', 0),
249 ), $dd->classify_response(array('p1' => '0', 'p2' => '1', 'p3' => '2')));
250 }
251}