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