~ubuntu-branches/ubuntu/jaunty/moodle/jaunty

« back to all changes in this revision

Viewing changes to question/type/shortanswer/simpletest/testquestiontype.php

  • Committer: Bazaar Package Importer
  • Author(s): Jordan Mantha, Matt Oquist
  • Date: 2009-02-25 15:16:22 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090225151622-0ekt1liwhv2obfza
Tags: 1.9.4.dfsg-0ubuntu1
* Merge with Debian git (Closes LP: #322961, #239481, #334611):
  - use Ubuntu's smarty lib directory for linking
  - use internal yui library 
  - add update-notifier support back in

[Matt Oquist]
  * renamed prerm script
  * significantly rewrote postinst and other maintainer scripts to improve
    user experience and package maintainability
    (Closes LP: #225662, #325450, #327843, #303078, #234609)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Unit tests for (some of) question/type/shortanswer/questiontype.php.
 
4
 *
 
5
 * @copyright &copy; 2007 The Open University
 
6
 * @author T.J.Hunt@open.ac.uk
 
7
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 
8
 * @package question
 
9
 */
 
10
 
 
11
if (!defined('MOODLE_INTERNAL')) {
 
12
    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 
13
}
 
14
 
 
15
require_once($CFG->dirroot . '/question/type/questiontype.php');
 
16
 
 
17
class question_shortanswer_qtype_test extends UnitTestCase {
 
18
    var $qtype;
 
19
    
 
20
    function setUp() {
 
21
        $this->qtype = new question_shortanswer_qtype();
 
22
    }
 
23
    
 
24
    function tearDown() {
 
25
        $this->qtype = null;   
 
26
    }
 
27
 
 
28
    function test_name() {
 
29
        $this->assertEqual($this->qtype->name(), 'shortanswer');
 
30
    }
 
31
 
 
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));
 
38
 
 
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));
 
43
 
 
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));
 
52
 
 
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));
 
61
 
 
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));
 
69
    }
 
70
 
 
71
    function test_check_response() {
 
72
        $answer1 = new stdClass;
 
73
        $answer1->id = 17;
 
74
        $answer1->answer = "celine";
 
75
        $answer1->fraction = 1;
 
76
        $answer2 = new stdClass;
 
77
        $answer2->id = 23;
 
78
        $answer2->answer = "c*line";
 
79
        $answer2->fraction = 0.8;
 
80
        $answer3 = new stdClass;
 
81
        $answer3->id = 23;
 
82
        $answer3->answer = "*line";
 
83
        $answer3->fraction = 0.7;
 
84
        $answer4 = new stdClass;
 
85
        $answer4->id = 29;
 
86
        $answer4->answer = "12\*13";
 
87
        $answer4->fraction = 0.5;
 
88
 
 
89
        $question = new stdClass;
 
90
        $question->options->answers = array(
 
91
            17 => $answer1,
 
92
            23 => $answer2,
 
93
            29 => $answer3,
 
94
            31 => $answer4
 
95
        );
 
96
        $question->options->usecase = true;
 
97
 
 
98
        $state = new stdClass;
 
99
 
 
100
        $state->responses = array('' => 'celine');
 
101
        $this->assertEqual($this->qtype->check_response($question, $state), 17);
 
102
 
 
103
        $state->responses = array('' => 'caline');
 
104
        $this->assertEqual($this->qtype->check_response($question, $state), 23);
 
105
 
 
106
        $state->responses = array('' => 'aline');
 
107
        $this->assertEqual($this->qtype->check_response($question, $state), 29);
 
108
 
 
109
        $state->responses = array('' => 'frog');
 
110
        $this->assertFalse($this->qtype->check_response($question, $state));
 
111
 
 
112
        $state->responses = array('' => '12*13');
 
113
        $this->assertEqual($this->qtype->check_response($question, $state), 31);
 
114
 
 
115
        $question->options->usecase = false;
 
116
 
 
117
        $answer1->answer = "Fred's";
 
118
        $question->options->answers[17] = $answer1;
 
119
 
 
120
        $state->responses = array('' => 'frog');
 
121
        $this->assertFalse($this->qtype->check_response($question, $state));
 
122
 
 
123
        $state->responses = array('' => "fred\'s");
 
124
        $this->assertEqual($this->qtype->check_response($question, $state), 17);
 
125
 
 
126
        $state->responses = array('' => '12*13');
 
127
        $this->assertEqual($this->qtype->check_response($question, $state), 31);
 
128
 
 
129
        $state->responses = array('' => 'caLINe');
 
130
        $this->assertEqual($this->qtype->check_response($question, $state), 23);
 
131
 
 
132
        $state->responses = array('' => 'ALIne');
 
133
        $this->assertEqual($this->qtype->check_response($question, $state), 29);
 
134
    }
 
135
 
 
136
    function test_compare_responses() {
 
137
        $question = new stdClass;
 
138
        $question->options->usecase = false;
 
139
 
 
140
        $state = new stdClass;
 
141
        $teststate = new stdClass;
 
142
        $this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
 
143
 
 
144
        $state->responses = array('' => '');
 
145
        $this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
 
146
 
 
147
        $state = new stdClass;
 
148
        $teststate->responses = array('' => '');
 
149
        $this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
 
150
 
 
151
        $state->responses = array('' => '');
 
152
        $this->assertTrue($this->qtype->compare_responses($question, $state, $teststate));
 
153
 
 
154
        $state->responses = array('' => 'frog');
 
155
        $teststate->responses = array('' => 'frog');
 
156
        $this->assertTrue($this->qtype->compare_responses($question, $state, $teststate));
 
157
 
 
158
        $state->responses = array('' => 'frog');
 
159
        $teststate->responses = array('' => 'Frog');
 
160
        $this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
 
161
 
 
162
        $state->responses = array('' => "\'");
 
163
        $teststate->responses = array('' => "\'");
 
164
        $this->assertTrue($this->qtype->compare_responses($question, $state, $teststate));
 
165
 
 
166
        $state->responses = array('' => 'frog*toad');
 
167
        $teststate->responses = array('' => 'frog*TOAD');
 
168
        $this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
 
169
 
 
170
        $state->responses = array('' => 'frog*');
 
171
        $teststate->responses = array('' => 'frogs');
 
172
        $this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
 
173
 
 
174
        $state->responses = array('' => 'frogs');
 
175
        $teststate->responses = array('' => 'frog*');
 
176
        $this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
 
177
 
 
178
        $question->options->usecase = true;
 
179
 
 
180
        $state->responses = array('' => '');
 
181
        $teststate->responses = array('' => '');
 
182
        $this->assertTrue($this->qtype->compare_responses($question, $state, $teststate));
 
183
 
 
184
        $state->responses = array('' => 'frog');
 
185
        $teststate->responses = array('' => 'frog');
 
186
        $this->assertTrue($this->qtype->compare_responses($question, $state, $teststate));
 
187
 
 
188
        $state->responses = array('' => 'frog');
 
189
        $teststate->responses = array('' => 'Frog');
 
190
        $this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
 
191
 
 
192
        $state->responses = array('' => "\'");
 
193
        $teststate->responses = array('' => "\'");
 
194
        $this->assertTrue($this->qtype->compare_responses($question, $state, $teststate));
 
195
 
 
196
        $state->responses = array('' => 'frog*toad');
 
197
        $teststate->responses = array('' => 'frog*toad');
 
198
        $this->assertTrue($this->qtype->compare_responses($question, $state, $teststate));
 
199
 
 
200
        $state->responses = array('' => 'frog*');
 
201
        $teststate->responses = array('' => 'frogs');
 
202
        $this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
 
203
 
 
204
        $state->responses = array('' => 'frogs');
 
205
        $teststate->responses = array('' => 'frog*');
 
206
        $this->assertFalse($this->qtype->compare_responses($question, $state, $teststate));
 
207
    }
 
208
 
 
209
    function test_get_correct_responses() {
 
210
        $answer1 = new stdClass;
 
211
        $answer1->id = 17;
 
212
        $answer1->answer = "frog";
 
213
        $answer1->fraction = 1;
 
214
        $answer2 = new stdClass;
 
215
        $answer2->id = 23;
 
216
        $answer2->answer = "f*g";
 
217
        $answer2->fraction = 1;
 
218
        $answer3 = new stdClass;
 
219
        $answer3->id = 29;
 
220
        $answer3->answer = "12\*13";
 
221
        $answer3->fraction = 1;
 
222
        $answer4 = new stdClass;
 
223
        $answer4->id = 31;
 
224
        $answer4->answer = "*";
 
225
        $answer4->fraction = 0;
 
226
        $question = new stdClass;
 
227
        $question->options->answers = array(
 
228
            17 => $answer1,
 
229
            23 => $answer2,
 
230
            29 => $answer3,
 
231
            31 => $answer4
 
232
        );
 
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));
 
241
    }
 
242
}
 
243
 
 
244
?>