Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
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
 
17
namespace qtype_ddimageortext;
18
 
19
use question_attempt_step;
20
use question_classified_response;
21
use question_state;
22
 
23
defined('MOODLE_INTERNAL') || die();
24
 
25
global $CFG;
26
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
27
require_once($CFG->dirroot . '/question/type/ddimageortext/tests/helper.php');
28
 
29
/**
30
 * Unit tests for the matching question definition class.
31
 *
32
 * @package   qtype_ddimageortext
33
 * @copyright 2009 The Open University
34
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 * @covers qtype_ddimageortext_question
36
 */
11 efrain 37
final class question_test extends \basic_testcase {
1 efrain 38
 
11 efrain 39
    public function test_get_question_summary(): void {
1 efrain 40
        $dd = \test_question_maker::make_question('ddimageortext');
41
        $this->assertEquals('The quick brown fox jumped over the lazy dog.; '.
42
                '[[Drop zone 1]] -> {1. quick / 2. fox}; '.
43
                '[[Drop zone 2]] -> {1. quick / 2. fox}; '.
44
                '[[Drop zone 3]] -> {3. lazy / 4. dog}; '.
45
                '[[Drop zone 4]] -> {3. lazy / 4. dog}',
46
            $dd->get_question_summary());
47
    }
48
 
11 efrain 49
    public function test_get_question_summary_maths(): void {
1 efrain 50
        $dd = \test_question_maker::make_question('ddimageortext', 'maths');
51
        $this->assertEquals('Fill in the operators to make this equation work: '.
52
                '7 [[1]] 11 [[2]] 13 [[1]] 17 [[2]] 19 = 3; '.
53
                '[[Drop zone 1]] -> {1. + / 2. -}; '.
54
                '[[Drop zone 2]] -> {1. + / 2. -}; '.
55
                '[[Drop zone 3]] -> {1. + / 2. -}; '.
56
                '[[Drop zone 4]] -> {1. + / 2. -}',
57
            $dd->get_question_summary());
58
    }
59
 
11 efrain 60
    public function test_summarise_response(): void {
1 efrain 61
        $dd = \test_question_maker::make_question('ddimageortext');
62
        $dd->shufflechoices = false;
63
        $dd->start_attempt(new question_attempt_step(), 1);
64
 
65
        $this->assertEquals('Drop zone 1 -> {1. quick} '.
66
                'Drop zone 2 -> {1. quick} '.
67
                'Drop zone 3 -> {3. lazy} '.
68
                'Drop zone 4 -> {3. lazy}',
69
            $dd->summarise_response(array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
70
    }
71
 
11 efrain 72
    public function test_summarise_response_maths(): void {
1 efrain 73
        $dd = \test_question_maker::make_question('ddimageortext', 'maths');
74
        $dd->shufflechoices = false;
75
        $dd->start_attempt(new question_attempt_step(), 1);
76
 
77
        $this->assertEquals('Drop zone 1 -> {1. +} '.
78
                'Drop zone 2 -> {2. -} '.
79
                'Drop zone 3 -> {1. +} '.
80
                'Drop zone 4 -> {2. -}',
81
            $dd->summarise_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
82
    }
83
 
11 efrain 84
    public function test_get_random_guess_score(): void {
1 efrain 85
        $dd = \test_question_maker::make_question('ddimageortext');
86
        $this->assertEquals(0.5, $dd->get_random_guess_score());
87
    }
88
 
11 efrain 89
    public function test_get_random_guess_score_maths(): void {
1 efrain 90
        $dd = \test_question_maker::make_question('ddimageortext', 'maths');
91
        $this->assertEquals(0.5, $dd->get_random_guess_score());
92
    }
93
 
11 efrain 94
    public function test_get_random_guess_score_broken_question(): void {
1 efrain 95
        // Before MDL-76298 was fixed, it was possible to create a question with
96
        // no drop zones, and that caused a fatal division by zero error. Because
97
        // people might have questions like that in their database, we need to
98
        // check the calculation is robust to it.
99
        /** @var \qtype_ddimageortext_question $dd */
100
        $dd = \test_question_maker::make_question('ddimageortext');
101
        $dd->places = [];
102
        $this->assertNull($dd->get_random_guess_score());
103
    }
104
 
11 efrain 105
    public function test_get_right_choice_for(): void {
1 efrain 106
        $dd = \test_question_maker::make_question('ddimageortext');
107
        $dd->shufflechoices = false;
108
        $dd->start_attempt(new question_attempt_step(), 1);
109
 
110
        $this->assertEquals(1, $dd->get_right_choice_for(1));
111
        $this->assertEquals(2, $dd->get_right_choice_for(2));
112
    }
113
 
11 efrain 114
    public function test_get_right_choice_for_maths(): void {
1 efrain 115
        $dd = \test_question_maker::make_question('ddimageortext', 'maths');
116
        $dd->shufflechoices = false;
117
        $dd->start_attempt(new question_attempt_step(), 1);
118
 
119
        $this->assertEquals(1, $dd->get_right_choice_for(1));
120
        $this->assertEquals(2, $dd->get_right_choice_for(2));
121
        $this->assertEquals(1, $dd->get_right_choice_for(3));
122
        $this->assertEquals(2, $dd->get_right_choice_for(4));
123
    }
124
 
11 efrain 125
    public function test_clear_wrong_from_response(): void {
1 efrain 126
        $dd = \test_question_maker::make_question('ddimageortext', 'maths');
127
        $dd->shufflechoices = false;
128
        $dd->start_attempt(new question_attempt_step(), 1);
129
 
130
        $initialresponse = array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1');
131
        $this->assertEquals(array('p1' => '1', 'p2' => '', 'p3' => '1', 'p4' => ''),
132
            $dd->clear_wrong_from_response($initialresponse));
133
    }
134
 
11 efrain 135
    public function test_get_num_parts_right(): void {
1 efrain 136
        $dd = \test_question_maker::make_question('ddimageortext');
137
        $dd->shufflechoices = false;
138
        $dd->start_attempt(new question_attempt_step(), 1);
139
 
140
        $this->assertEquals(array(2, 4),
141
            $dd->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '2', 'p4' => '2')));
142
        $this->assertEquals(array(4, 4),
143
            $dd->get_num_parts_right(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
144
    }
145
 
11 efrain 146
    public function test_get_num_parts_right_maths(): void {
1 efrain 147
        $dd = \test_question_maker::make_question('ddimageortext', 'maths');
148
        $dd->shufflechoices = false;
149
        $dd->start_attempt(new question_attempt_step(), 1);
150
 
151
        $this->assertEquals(array(2, 4),
152
            $dd->get_num_parts_right(array(
153
                'p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
154
    }
155
 
11 efrain 156
    public function test_get_expected_data(): void {
1 efrain 157
        $dd = \test_question_maker::make_question('ddimageortext');
158
        $dd->start_attempt(new question_attempt_step(), 1);
159
 
160
        $this->assertEquals(
161
            array('p1' => PARAM_INT, 'p2' => PARAM_INT, 'p3' => PARAM_INT, 'p4' => PARAM_INT),
162
            $dd->get_expected_data()
163
        );
164
    }
165
 
11 efrain 166
    public function test_get_correct_response(): void {
1 efrain 167
        $dd = \test_question_maker::make_question('ddimageortext');
168
        $dd->shufflechoices = false;
169
        $dd->start_attempt(new question_attempt_step(), 1);
170
 
171
        $this->assertEquals(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2'),
172
            $dd->get_correct_response());
173
    }
174
 
11 efrain 175
    public function test_get_correct_response_maths(): void {
1 efrain 176
        $dd = \test_question_maker::make_question('ddimageortext', 'maths');
177
        $dd->shufflechoices = false;
178
        $dd->start_attempt(new question_attempt_step(), 1);
179
 
180
        $this->assertEquals(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2'),
181
            $dd->get_correct_response());
182
    }
183
 
11 efrain 184
    public function test_is_same_response(): void {
1 efrain 185
        $dd = \test_question_maker::make_question('ddimageortext');
186
        $dd->start_attempt(new question_attempt_step(), 1);
187
 
188
        $this->assertTrue($dd->is_same_response(
189
            array(),
190
            array('p1' => '', 'p2' => '', 'p3' => '', 'p4' => '')));
191
 
192
        $this->assertFalse($dd->is_same_response(
193
            array(),
194
            array('p1' => '1', 'p2' => '', 'p3' => '', 'p4' => '')));
195
 
196
        $this->assertFalse($dd->is_same_response(
197
            array('p1' => '', 'p2' => '', 'p3' => '', 'p4' => ''),
198
            array('p1' => '1', 'p2' => '', 'p3' => '', 'p4' => '')));
199
 
200
        $this->assertTrue($dd->is_same_response(
201
            array('p1' => '1', 'p2' => '2', 'p3' => '3', 'p4' => '4'),
202
            array('p1' => '1', 'p2' => '2', 'p3' => '3', 'p4' => '4')));
203
 
204
        $this->assertFalse($dd->is_same_response(
205
            array('p1' => '1', 'p2' => '2', 'p3' => '3', 'p4' => '4'),
206
            array('p1' => '1', 'p2' => '2', 'p3' => '2', 'p4' => '4')));
207
    }
11 efrain 208
    public function test_is_complete_response(): void {
1 efrain 209
        $dd = \test_question_maker::make_question('ddimageortext');
210
        $dd->start_attempt(new question_attempt_step(), 1);
211
 
212
        $this->assertFalse($dd->is_complete_response(array()));
213
        $this->assertFalse($dd->is_complete_response(
214
            array('p1' => '1', 'p2' => '1', 'p3' => '', 'p4' => '')));
215
        $this->assertFalse($dd->is_complete_response(array('p1' => '1')));
216
        $this->assertTrue($dd->is_complete_response(
217
            array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
218
    }
219
 
11 efrain 220
    public function test_is_gradable_response(): void {
1 efrain 221
        $dd = \test_question_maker::make_question('ddimageortext');
222
        $dd->start_attempt(new question_attempt_step(), 1);
223
 
224
        $this->assertFalse($dd->is_gradable_response(array()));
225
        $this->assertFalse($dd->is_gradable_response(
11 efrain 226
            array('p1' => '', 'p2' => '', 'p3' => '')));
1 efrain 227
        $this->assertTrue($dd->is_gradable_response(
228
            array('p1' => '1', 'p2' => '1', 'p3' => '')));
229
        $this->assertTrue($dd->is_gradable_response(array('p1' => '1')));
230
        $this->assertTrue($dd->is_gradable_response(
231
            array('p1' => '1', 'p2' => '1', 'p3' => '1')));
232
    }
233
 
11 efrain 234
    public function test_grading(): void {
1 efrain 235
        $dd = \test_question_maker::make_question('ddimageortext');
236
        $dd->shufflechoices = false;
237
        $dd->start_attempt(new question_attempt_step(), 1);
238
 
239
        $this->assertEquals(array(1, question_state::$gradedright),
240
            $dd->grade_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
241
        $this->assertEquals(array(0.25, question_state::$gradedpartial),
242
            $dd->grade_response(array('p1' => '1')));
243
        $this->assertEquals(array(0, question_state::$gradedwrong),
244
            $dd->grade_response(array('p1' => '2', 'p2' => '1', 'p3' => '2', 'p4' => '1')));
245
    }
246
 
11 efrain 247
    public function test_grading_maths(): void {
1 efrain 248
        $dd = \test_question_maker::make_question('ddimageortext', 'maths');
249
        $dd->shufflechoices = false;
250
        $dd->start_attempt(new question_attempt_step(), 1);
251
 
252
        $this->assertEquals(array(1, question_state::$gradedright),
253
            $dd->grade_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
254
        $this->assertEquals(array(0.5, question_state::$gradedpartial),
255
            $dd->grade_response(array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
256
        $this->assertEquals(array(0, question_state::$gradedwrong),
257
            $dd->grade_response(array('p1' => '', 'p2' => '1', 'p3' => '2', 'p4' => '1')));
258
    }
259
 
11 efrain 260
    public function test_classify_response(): void {
1 efrain 261
        $dd = \test_question_maker::make_question('ddimageortext');
262
        $dd->shufflechoices = false;
263
        $dd->start_attempt(new question_attempt_step(), 1);
264
 
265
        $this->assertEquals(array(
266
            1 => new question_classified_response(1, '1. quick', 1),
267
            2 => new question_classified_response(2, '2. fox', 1),
268
            3 => new question_classified_response(3, '3. lazy', 1),
269
            4 => new question_classified_response(4, '4. dog', 1)
270
        ), $dd->classify_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
271
        $this->assertEquals(array(
272
            1 => question_classified_response::no_response(),
273
            2 => new question_classified_response(1, '1. quick', 0),
274
            3 => new question_classified_response(4, '4. dog', 0),
275
            4 => new question_classified_response(4, '4. dog', 1)
276
        ), $dd->classify_response(array('p1' => '', 'p2' => '1', 'p3' => '2', 'p4' => '2')));
277
    }
278
 
11 efrain 279
    public function test_summarise_response_choice_deleted(): void {
1 efrain 280
        /** @var qtype_ddtoimage_question_base $dd */
281
        $dd = \test_question_maker::make_question('ddimageortext');
282
        $dd->shufflechoices = false;
283
        $dd->start_attempt(new question_attempt_step(), 1);
284
        // Simulation of an instructor deleting 1 choice after an attempt has been made.
285
        unset($dd->choices[1][1]);
286
        $delquestionstr = get_string('deletedchoice', 'qtype_ddimageortext');
287
        $this->assertEquals("Drop zone 1 -> {{$delquestionstr}} ".
288
            "Drop zone 2 -> {{$delquestionstr}} ".
289
            'Drop zone 3 -> {3. lazy} '.
290
            'Drop zone 4 -> {3. lazy}',
291
            $dd->summarise_response(array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
292
    }
293
}