testquestion.php 11.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Unit tests for the select missing words question definition class.
 *
20
 * @package    qtype
21
 * @subpackage gapselect
22
23
 * @copyright  2011 The Open University
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
25
 */

26
27
28

defined('MOODLE_INTERNAL') || die();

29
30
31
32
33
require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php');
require_once($CFG->dirroot . '/question/type/gapselect/simpletest/helper.php');


/**
34
 * Unit tests for the select missing words question definition class.
35
 *
36
37
 * @copyright  2009 The Open University
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
39
40
41
42
 */
class qtype_gapselect_question_test extends UnitTestCase {

    public function test_get_question_summary() {
        $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
43
44
        $this->assertEqual('The [[1]] brown [[2]] jumped over the [[3]] dog.; ' .
                '[[1]] -> {quick / slow}; [[2]] -> {fox / dog}; [[3]] -> {lazy / assiduous}',
45
46
47
48
49
50
51
52
53
54
55
56
57
                $gapselect->get_question_summary());
    }

    public function test_get_question_summary_maths() {
        $gapselect = qtype_gapselect_test_helper::make_a_maths_gapselect_question();
        $this->assertEqual('Fill in the operators to make this equation work: ' .
                '7 [[1]] 11 [[2]] 13 [[1]] 17 [[2]] 19 = 3; [[1]] -> {+ / - / * / /}',
                $gapselect->get_question_summary());
    }

    public function test_summarise_response() {
        $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
        $gapselect->shufflechoices = false;
58
        $gapselect->start_attempt(new question_attempt_step(), 1);
59
60
61
62
63
64
65
66

        $this->assertEqual('{quick} {fox} {lazy}',
                $gapselect->summarise_response(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
    }

    public function test_summarise_response_maths() {
        $gapselect = qtype_gapselect_test_helper::make_a_maths_gapselect_question();
        $gapselect->shufflechoices = false;
67
        $gapselect->start_attempt(new question_attempt_step(), 1);
68

69
70
        $this->assertEqual('{+} {-} {+} {-}', $gapselect->summarise_response(
                array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    }

    public function test_get_random_guess_score() {
        $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
        $this->assertEqual(0.5, $gapselect->get_random_guess_score());
    }

    public function test_get_random_guess_score_maths() {
        $gapselect = qtype_gapselect_test_helper::make_a_maths_gapselect_question();
        $this->assertEqual(0.25, $gapselect->get_random_guess_score());
    }

    public function test_get_right_choice_for() {
        $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
        $gapselect->shufflechoices = false;
86
        $gapselect->start_attempt(new question_attempt_step(), 1);
87
88
89
90
91
92
93
94

        $this->assertEqual(1, $gapselect->get_right_choice_for(1));
        $this->assertEqual(1, $gapselect->get_right_choice_for(2));
    }

    public function test_get_right_choice_for_maths() {
        $gapselect = qtype_gapselect_test_helper::make_a_maths_gapselect_question();
        $gapselect->shufflechoices = false;
95
        $gapselect->start_attempt(new question_attempt_step(), 1);
96
97
98
99
100
101
102
103

        $this->assertEqual(1, $gapselect->get_right_choice_for(1));
        $this->assertEqual(2, $gapselect->get_right_choice_for(2));
    }

    public function test_clear_wrong_from_response() {
        $gapselect = qtype_gapselect_test_helper::make_a_maths_gapselect_question();
        $gapselect->shufflechoices = false;
104
        $gapselect->start_attempt(new question_attempt_step(), 1);
105
106
107
108
109
110
111
112
113

        $initialresponse = array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1');
        $this->assertEqual(array('p1' => '1', 'p2' => '0', 'p3' => '1', 'p4' => '0'),
                $gapselect->clear_wrong_from_response($initialresponse));
    }

    public function test_get_num_parts_right() {
        $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
        $gapselect->shufflechoices = false;
114
        $gapselect->start_attempt(new question_attempt_step(), 1);
115
116
117
118
119
120
121
122
123
124

        $this->assertEqual(array(2, 3),
                $gapselect->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '2')));
        $this->assertEqual(array(3, 3),
                $gapselect->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
    }

    public function test_get_num_parts_right_maths() {
        $gapselect = qtype_gapselect_test_helper::make_a_maths_gapselect_question();
        $gapselect->shufflechoices = false;
125
        $gapselect->start_attempt(new question_attempt_step(), 1);
126

127
128
        $this->assertEqual(array(2, 4), $gapselect->get_num_parts_right(
                array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
129
130
131
132
    }

    public function test_get_expected_data() {
        $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
133
        $gapselect->start_attempt(new question_attempt_step(), 1);
134
135
136
137
138
139
140
141

        $this->assertEqual(array('p1' => PARAM_INT, 'p2' => PARAM_INT, 'p3' => PARAM_INT),
                $gapselect->get_expected_data());
    }

    public function test_get_correct_response() {
        $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
        $gapselect->shufflechoices = false;
142
        $gapselect->start_attempt(new question_attempt_step(), 1);
143
144
145
146
147
148
149
150

        $this->assertEqual(array('p1' => '1', 'p2' => '1', 'p3' => '1'),
                $gapselect->get_correct_response());
    }

    public function test_get_correct_response_maths() {
        $gapselect = qtype_gapselect_test_helper::make_a_maths_gapselect_question();
        $gapselect->shufflechoices = false;
151
        $gapselect->start_attempt(new question_attempt_step(), 1);
152
153
154
155
156
157
158

        $this->assertEqual(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2'),
                $gapselect->get_correct_response());
    }

    public function test_is_same_response() {
        $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
159
        $gapselect->start_attempt(new question_attempt_step(), 1);
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

        $this->assertTrue($gapselect->is_same_response(
                array(),
                array('p1' => '0', 'p2' => '0', 'p3' => '0')));

        $this->assertFalse($gapselect->is_same_response(
                array(),
                array('p1' => '1', 'p2' => '0', 'p3' => '0')));

        $this->assertFalse($gapselect->is_same_response(
                array('p1' => '0', 'p2' => '0', 'p3' => '0'),
                array('p1' => '1', 'p2' => '0', 'p3' => '0')));

        $this->assertTrue($gapselect->is_same_response(
                array('p1' => '1', 'p2' => '2', 'p3' => '3'),
                array('p1' => '1', 'p2' => '2', 'p3' => '3')));

        $this->assertFalse($gapselect->is_same_response(
                array('p1' => '1', 'p2' => '2', 'p3' => '3'),
                array('p1' => '1', 'p2' => '2', 'p3' => '2')));
    }
    public function test_is_complete_response() {
        $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
183
        $gapselect->start_attempt(new question_attempt_step(), 1);
184
185
186
187
188
189
190
191
192
193
194

        $this->assertFalse($gapselect->is_complete_response(array()));
        $this->assertFalse($gapselect->is_complete_response(
                array('p1' => '1', 'p2' => '1', 'p3' => '0')));
        $this->assertFalse($gapselect->is_complete_response(array('p1' => '1')));
        $this->assertTrue($gapselect->is_complete_response(
                array('p1' => '1', 'p2' => '1', 'p3' => '1')));
    }

    public function test_is_gradable_response() {
        $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
195
        $gapselect->start_attempt(new question_attempt_step(), 1);
196
197
198
199
200
201
202
203
204
205
206
207
208
209

        $this->assertFalse($gapselect->is_gradable_response(array()));
        $this->assertFalse($gapselect->is_gradable_response(
                array('p1' => '0', 'p2' => '0', 'p3' => '0')));
        $this->assertTrue($gapselect->is_gradable_response(
                array('p1' => '1', 'p2' => '1', 'p3' => '0')));
        $this->assertTrue($gapselect->is_gradable_response(array('p1' => '1')));
        $this->assertTrue($gapselect->is_gradable_response(
                array('p1' => '1', 'p2' => '1', 'p3' => '1')));
    }

    public function test_grading() {
        $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
        $gapselect->shufflechoices = false;
210
        $gapselect->start_attempt(new question_attempt_step(), 1);
211
212
213
214
215
216
217
218
219
220
221
222

        $this->assertEqual(array(1, question_state::$gradedright),
                $gapselect->grade_response(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
        $this->assertEqual(array(1/3, question_state::$gradedpartial),
                $gapselect->grade_response(array('p1' => '1')));
        $this->assertEqual(array(0, question_state::$gradedwrong),
                $gapselect->grade_response(array('p1' => '2', 'p2' => '2', 'p3' => '2')));
    }

    public function test_grading_maths() {
        $gapselect = qtype_gapselect_test_helper::make_a_maths_gapselect_question();
        $gapselect->shufflechoices = false;
223
        $gapselect->start_attempt(new question_attempt_step(), 1);
224

225
226
227
228
229
230
        $this->assertEqual(array(1, question_state::$gradedright), $gapselect->grade_response(
                array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
        $this->assertEqual(array(0.5, question_state::$gradedpartial), $gapselect->grade_response(
                array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
        $this->assertEqual(array(0, question_state::$gradedwrong), $gapselect->grade_response(
                array('p1' => '0', 'p2' => '1', 'p3' => '2', 'p4' => '1')));
231
232
233
234
235
    }

    public function test_classify_response() {
        $gapselect = qtype_gapselect_test_helper::make_a_gapselect_question();
        $gapselect->shufflechoices = false;
236
        $gapselect->start_attempt(new question_attempt_step(), 1);
237
238

        $this->assertEqual(array(
239
                    1 => new question_classified_response(1, 'quick', 1/3),
240
                    2 => new question_classified_response(2, 'dog', 0),
241
                    3 => new question_classified_response(1, 'lazy', 1/3),
242
243
244
                ), $gapselect->classify_response(array('p1' => '1', 'p2' => '2', 'p3' => '1')));
        $this->assertEqual(array(
                    1 => question_classified_response::no_response(),
245
                    2 => new question_classified_response(1, 'fox', 1/3),
246
247
248
249
                    3 => new question_classified_response(2, 'assiduous', 0),
                ), $gapselect->classify_response(array('p1' => '0', 'p2' => '1', 'p3' => '2')));
    }
}