3
* Unit tests for (some of) question/type/shortanswer/questiontype.php.
5
* @copyright © 2007 The Open University
6
* @author T.J.Hunt@open.ac.uk
7
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11
if (!defined('MOODLE_INTERNAL')) {
12
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
15
require_once($CFG->dirroot . '/question/type/questiontype.php');
17
class question_shortanswer_qtype_test extends UnitTestCase {
21
$this->qtype = new question_shortanswer_qtype();
28
function test_name() {
29
$this->assertEqual($this->qtype->name(), 'shortanswer');
32
function test_compare_string_with_wildcard() {
33
// Test case sensitive literal matches.
34
$this->assertTrue($this->qtype->compare_string_with_wildcard('Frog', 'Frog', false));
35
$this->assertFalse($this->qtype->compare_string_with_wildcard('Frog', 'frog', false));
36
$this->assertTrue($this->qtype->compare_string_with_wildcard(' Frog ', 'Frog', false));
37
$this->assertFalse($this->qtype->compare_string_with_wildcard('Frogs', 'Frog', false));
39
// Test case insensitive literal matches.
40
$this->assertTrue($this->qtype->compare_string_with_wildcard('Frog', 'frog', true));
41
$this->assertTrue($this->qtype->compare_string_with_wildcard(' FROG ', 'Frog', true));
42
$this->assertFalse($this->qtype->compare_string_with_wildcard('Frogs', 'Frog', true));
44
// Test case sensitive wildcard matches.
45
$this->assertTrue($this->qtype->compare_string_with_wildcard('Frog', 'F*og', false));
46
$this->assertTrue($this->qtype->compare_string_with_wildcard('Fog', 'F*og', false));
47
$this->assertTrue($this->qtype->compare_string_with_wildcard(' Fat dog ', 'F*og', false));
48
$this->assertFalse($this->qtype->compare_string_with_wildcard('Frogs', 'F*og', false));
49
$this->assertFalse($this->qtype->compare_string_with_wildcard('Fg', 'F*og', false));
50
$this->assertFalse($this->qtype->compare_string_with_wildcard('frog', 'F*og', false));
51
$this->assertFalse($this->qtype->compare_string_with_wildcard(' fat dog ', 'F*og', false));
53
// Test case insensitive wildcard matches.
54
$this->assertTrue($this->qtype->compare_string_with_wildcard('Frog', 'F*og', true));
55
$this->assertTrue($this->qtype->compare_string_with_wildcard('Fog', 'F*og', true));
56
$this->assertTrue($this->qtype->compare_string_with_wildcard(' Fat dog ', 'F*og', true));
57
$this->assertFalse($this->qtype->compare_string_with_wildcard('Frogs', 'F*og', true));
58
$this->assertFalse($this->qtype->compare_string_with_wildcard('Fg', 'F*og', true));
59
$this->assertTrue($this->qtype->compare_string_with_wildcard('frog', 'F*og', true));
60
$this->assertTrue($this->qtype->compare_string_with_wildcard(' fat dog ', 'F*og', true));
62
// Test match using regexp special chars.
63
$this->assertTrue($this->qtype->compare_string_with_wildcard(' * ', '\*', false));
64
$this->assertTrue($this->qtype->compare_string_with_wildcard('*', '\*', false));
65
$this->assertTrue($this->qtype->compare_string_with_wildcard('Frog*toad', 'Frog\*toad', false));
66
$this->assertFalse($this->qtype->compare_string_with_wildcard('a', '[a-z]', false));
67
$this->assertTrue($this->qtype->compare_string_with_wildcard('[a-z]', '[a-z]', false));
68
$this->assertTrue($this->qtype->compare_string_with_wildcard('\{}/', '\{}/', true));
71
function test_check_response() {
72
$answer1 = new stdClass;
74
$answer1->answer = "celine";
75
$answer1->fraction = 1;
76
$answer2 = new stdClass;
78
$answer2->answer = "c*line";
79
$answer2->fraction = 0.8;
80
$answer3 = new stdClass;
82
$answer3->answer = "*line";
83
$answer3->fraction = 0.7;
84
$answer4 = new stdClass;
86
$answer4->answer = "12\*13";
87
$answer4->fraction = 0.5;
89
$question = new stdClass;
90
$question->options->answers = array(
96
$question->options->usecase = true;
98
$state = new stdClass;
100
$state->responses = array('' => 'celine');
101
$this->assertEqual($this->qtype->check_response($question, $state), 17);
103
$state->responses = array('' => 'caline');
104
$this->assertEqual($this->qtype->check_response($question, $state), 23);
106
$state->responses = array('' => 'aline');
107
$this->assertEqual($this->qtype->check_response($question, $state), 29);
109
$state->responses = array('' => 'frog');
110
$this->assertFalse($this->qtype->check_response($question, $state));
112
$state->responses = array('' => '12*13');
113
$this->assertEqual($this->qtype->check_response($question, $state), 31);
115
$question->options->usecase = false;
117
$answer1->answer = "Fred's";
118
$question->options->answers[17] = $answer1;
120
$state->responses = array('' => 'frog');
121
$this->assertFalse($this->qtype->check_response($question, $state));
123
$state->responses = array('' => "fred\'s");
124
$this->assertEqual($this->qtype->check_response($question, $state), 17);
126
$state->responses = array('' => '12*13');
127
$this->assertEqual($this->qtype->check_response($question, $state), 31);
129
$state->responses = array('' => 'caLINe');
130
$this->assertEqual($this->qtype->check_response($question, $state), 23);
132
$state->responses = array('' => 'ALIne');
133
$this->assertEqual($this->qtype->check_response($question, $state), 29);
136
function test_compare_responses() {
137
$question = new stdClass;
138
$question->options->usecase = false;
140
$state = new stdClass;
141
$teststate = new stdClass;
142
$this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
144
$state->responses = array('' => '');
145
$this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
147
$state = new stdClass;
148
$teststate->responses = array('' => '');
149
$this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
151
$state->responses = array('' => '');
152
$this->assertTrue($this->qtype->compare_responses($question, $state, $teststate));
154
$state->responses = array('' => 'frog');
155
$teststate->responses = array('' => 'frog');
156
$this->assertTrue($this->qtype->compare_responses($question, $state, $teststate));
158
$state->responses = array('' => 'frog');
159
$teststate->responses = array('' => 'Frog');
160
$this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
162
$state->responses = array('' => "\'");
163
$teststate->responses = array('' => "\'");
164
$this->assertTrue($this->qtype->compare_responses($question, $state, $teststate));
166
$state->responses = array('' => 'frog*toad');
167
$teststate->responses = array('' => 'frog*TOAD');
168
$this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
170
$state->responses = array('' => 'frog*');
171
$teststate->responses = array('' => 'frogs');
172
$this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
174
$state->responses = array('' => 'frogs');
175
$teststate->responses = array('' => 'frog*');
176
$this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
178
$question->options->usecase = true;
180
$state->responses = array('' => '');
181
$teststate->responses = array('' => '');
182
$this->assertTrue($this->qtype->compare_responses($question, $state, $teststate));
184
$state->responses = array('' => 'frog');
185
$teststate->responses = array('' => 'frog');
186
$this->assertTrue($this->qtype->compare_responses($question, $state, $teststate));
188
$state->responses = array('' => 'frog');
189
$teststate->responses = array('' => 'Frog');
190
$this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
192
$state->responses = array('' => "\'");
193
$teststate->responses = array('' => "\'");
194
$this->assertTrue($this->qtype->compare_responses($question, $state, $teststate));
196
$state->responses = array('' => 'frog*toad');
197
$teststate->responses = array('' => 'frog*toad');
198
$this->assertTrue($this->qtype->compare_responses($question, $state, $teststate));
200
$state->responses = array('' => 'frog*');
201
$teststate->responses = array('' => 'frogs');
202
$this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
204
$state->responses = array('' => 'frogs');
205
$teststate->responses = array('' => 'frog*');
206
$this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
209
function test_get_correct_responses() {
210
$answer1 = new stdClass;
212
$answer1->answer = "frog";
213
$answer1->fraction = 1;
214
$answer2 = new stdClass;
216
$answer2->answer = "f*g";
217
$answer2->fraction = 1;
218
$answer3 = new stdClass;
220
$answer3->answer = "12\*13";
221
$answer3->fraction = 1;
222
$answer4 = new stdClass;
224
$answer4->answer = "*";
225
$answer4->fraction = 0;
226
$question = new stdClass;
227
$question->options->answers = array(
233
$state = new stdClass;
234
$this->assertEqual($this->qtype->get_correct_responses($question, $state), array('' => 'frog'));
235
$question->options->answers[17]->fraction = 0;
236
$this->assertEqual($this->qtype->get_correct_responses($question, $state), array('' => 'f*g'));
237
$question->options->answers[23]->fraction = 0;
238
$this->assertEqual($this->qtype->get_correct_responses($question, $state), array('' => '12*13'));
239
$question->options->answers[29]->fraction = 0;
240
$this->assertNull($this->qtype->get_correct_responses($question, $state));