MDL-36571 qtype multichoice: don't corrupt unicode characters.
[moodle.git] / question / type / multichoice / tests / question_test.php
CommitLineData
603bd001
PS
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/**
18 * Unit tests for the multiple choice question definition classes.
19 *
20 * @package qtype
21 * @subpackage multichoice
22 * @copyright 2009 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
31
32
33/**
34 * Unit tests for the multiple choice, multiple response question definition class.
35 *
36 * @copyright 2009 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class qtype_multichoice_single_question_test extends advanced_testcase {
40
41 public function test_get_expected_data() {
42 $question = test_question_maker::make_a_multichoice_single_question();
43 $this->assertEquals(array('answer' => PARAM_INT), $question->get_expected_data());
44 }
45
46 public function test_is_complete_response() {
47 $question = test_question_maker::make_a_multichoice_single_question();
48
49 $this->assertFalse($question->is_complete_response(array()));
50 $this->assertTrue($question->is_complete_response(array('answer' => '0')));
51 $this->assertTrue($question->is_complete_response(array('answer' => '2')));
52 }
53
54 public function test_is_gradable_response() {
55 $question = test_question_maker::make_a_multichoice_single_question();
56
57 $this->assertFalse($question->is_gradable_response(array()));
58 $this->assertTrue($question->is_gradable_response(array('answer' => '0')));
59 $this->assertTrue($question->is_gradable_response(array('answer' => '2')));
60 }
61
62 public function test_grading() {
63 $question = test_question_maker::make_a_multichoice_single_question();
64 $question->shuffleanswers = false;
65 $question->start_attempt(new question_attempt_step(), 1);
66
67 $this->assertEquals(array(1, question_state::$gradedright),
68 $question->grade_response(array('answer' => 0)));
69 $this->assertEquals(array(-0.3333333, question_state::$gradedwrong),
70 $question->grade_response(array('answer' => 1)));
71 $this->assertEquals(array(-0.3333333, question_state::$gradedwrong),
72 $question->grade_response(array('answer' => 2)));
73 }
74
75 public function test_grading_rounding_three_right() {
76 question_bank::load_question_definition_classes('multichoice');
77 $mc = new qtype_multichoice_multi_question();
78 test_question_maker::initialise_a_question($mc);
79 $mc->name = 'Odd numbers';
80 $mc->questiontext = 'Which are the odd numbers?';
81 $mc->generalfeedback = '1, 3 and 5 are the odd numbers.';
82 $mc->qtype = question_bank::get_qtype('multichoice');
83
84 $mc->shuffleanswers = 0;
85 $mc->answernumbering = 'abc';
86
87 test_question_maker::set_standard_combined_feedback_fields($mc);
88
89 $mc->answers = array(
90 11 => new question_answer(11, '1', 0.3333333, '', FORMAT_HTML),
91 12 => new question_answer(12, '2', -1, '', FORMAT_HTML),
92 13 => new question_answer(13, '3', 0.3333333, '', FORMAT_HTML),
93 14 => new question_answer(14, '4', -1, '', FORMAT_HTML),
94 15 => new question_answer(15, '5', 0.3333333, '', FORMAT_HTML),
95 16 => new question_answer(16, '6', -1, '', FORMAT_HTML),
96 );
97
98 $mc->start_attempt(new question_attempt_step(), 1);
99
100 list($grade, $state) = $mc->grade_response(
101 array('choice0' => 1, 'choice2' => 1, 'choice4' => 1));
102 $this->assertEquals(1, $grade, '', 0.000001);
103 $this->assertEquals(question_state::$gradedright, $state);
104 }
105
106 public function test_get_correct_response() {
107 $question = test_question_maker::make_a_multichoice_single_question();
108 $question->shuffleanswers = false;
109 $question->start_attempt(new question_attempt_step(), 1);
110
111 $this->assertEquals(array('answer' => 0),
112 $question->get_correct_response());
113 }
114
115 public function test_summarise_response() {
116 $mc = test_question_maker::make_a_multichoice_single_question();
117 $mc->shuffleanswers = false;
118 $mc->start_attempt(new question_attempt_step(), 1);
119
120 $summary = $mc->summarise_response(array('answer' => 0),
121 test_question_maker::get_a_qa($mc));
122
123 $this->assertEquals('A', $summary);
124 }
125
126 public function test_classify_response() {
127 $mc = test_question_maker::make_a_multichoice_single_question();
128 $mc->shuffleanswers = false;
129 $mc->start_attempt(new question_attempt_step(), 1);
130
131 $this->assertEquals(array(
132 $mc->id => new question_classified_response(14, 'B', -0.3333333),
133 ), $mc->classify_response(array('answer' => 1)));
134
135 $this->assertEquals(array(
136 $mc->id => question_classified_response::no_response(),
137 ), $mc->classify_response(array()));
138 }
139
140 public function test_make_html_inline() {
141 $mc = test_question_maker::make_a_multichoice_single_question();
142 $this->assertEquals('Frog', $mc->make_html_inline('<p>Frog</p>'));
143 $this->assertEquals('Frog<br />Toad', $mc->make_html_inline("<p>Frog</p>\n<p>Toad</p>"));
144 $this->assertEquals('<img src="http://example.com/pic.png" alt="Graph" />',
145 $mc->make_html_inline(
146 '<p><img src="http://example.com/pic.png" alt="Graph" /></p>'));
147 $this->assertEquals("Frog<br />XXX <img src='http://example.com/pic.png' alt='Graph' />",
148 $mc->make_html_inline(" <p> Frog </p> \n\r
149 <p> XXX <img src='http://example.com/pic.png' alt='Graph' /> </p> "));
4c1a35e3
TH
150 $this->assertEquals('Frog', $mc->make_html_inline('<p>Frog</p><p></p>'));
151 $this->assertEquals('Frog<br />†', $mc->make_html_inline('<p>Frog</p><p>†</p>'));
603bd001
PS
152 }
153}
154
155
156/**
157 * Unit tests for the multiple choice, single response question definition class.
158 *
159 * @copyright 2009 The Open University
160 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
161 */
162class qtype_multichoice_multi_question_test extends advanced_testcase {
163
164 public function test_get_expected_data() {
165 $question = test_question_maker::make_a_multichoice_multi_question();
166 $question->start_attempt(new question_attempt_step(), 1);
167
168 $this->assertEquals(array('choice0' => PARAM_BOOL, 'choice1' => PARAM_BOOL,
169 'choice2' => PARAM_BOOL, 'choice3' => PARAM_BOOL), $question->get_expected_data());
170 }
171
172 public function test_is_complete_response() {
173 $question = test_question_maker::make_a_multichoice_multi_question();
174 $question->start_attempt(new question_attempt_step(), 1);
175
176 $this->assertFalse($question->is_complete_response(array()));
177 $this->assertFalse($question->is_complete_response(
178 array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
179 $this->assertTrue($question->is_complete_response(array('choice1' => '1')));
180 $this->assertTrue($question->is_complete_response(
181 array('choice0' => '1', 'choice1' => '1', 'choice2' => '1', 'choice3' => '1')));
182 }
183
184 public function test_is_gradable_response() {
185 $question = test_question_maker::make_a_multichoice_multi_question();
186 $question->start_attempt(new question_attempt_step(), 1);
187
188 $this->assertFalse($question->is_gradable_response(array()));
189 $this->assertFalse($question->is_gradable_response(
190 array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
191 $this->assertTrue($question->is_gradable_response(array('choice1' => '1')));
192 $this->assertTrue($question->is_gradable_response(
193 array('choice0' => '1', 'choice1' => '1', 'choice2' => '1', 'choice3' => '1')));
194 }
195
196 public function test_grading() {
197 $question = test_question_maker::make_a_multichoice_multi_question();
198 $question->shuffleanswers = false;
199 $question->start_attempt(new question_attempt_step(), 1);
200
201 $this->assertEquals(array(1, question_state::$gradedright),
202 $question->grade_response(array('choice0' => '1', 'choice2' => '1')));
203 $this->assertEquals(array(0.5, question_state::$gradedpartial),
204 $question->grade_response(array('choice0' => '1')));
205 $this->assertEquals(array(0, question_state::$gradedwrong),
206 $question->grade_response(
207 array('choice0' => '1', 'choice1' => '1', 'choice2' => '1')));
208 $this->assertEquals(array(0, question_state::$gradedwrong),
209 $question->grade_response(array('choice1' => '1')));
210 }
211
212 public function test_get_correct_response() {
213 $question = test_question_maker::make_a_multichoice_multi_question();
214 $question->shuffleanswers = false;
215 $question->start_attempt(new question_attempt_step(), 1);
216
217 $this->assertEquals(array('choice0' => '1', 'choice2' => '1'),
218 $question->get_correct_response());
219 }
220
221 public function test_get_question_summary() {
222 $mc = test_question_maker::make_a_multichoice_single_question();
223 $mc->start_attempt(new question_attempt_step(), 1);
224
225 $qsummary = $mc->get_question_summary();
226
227 $this->assertRegExp('/' . preg_quote($mc->questiontext) . '/', $qsummary);
228 foreach ($mc->answers as $answer) {
229 $this->assertRegExp('/' . preg_quote($answer->answer) . '/', $qsummary);
230 }
231 }
232
233 public function test_summarise_response() {
234 $mc = test_question_maker::make_a_multichoice_multi_question();
235 $mc->shuffleanswers = false;
236 $mc->start_attempt(new question_attempt_step(), 1);
237
238 $summary = $mc->summarise_response(array('choice1' => 1, 'choice2' => 1),
239 test_question_maker::get_a_qa($mc));
240
241 $this->assertEquals('B; C', $summary);
242 }
243
244 public function test_classify_response() {
245 $mc = test_question_maker::make_a_multichoice_multi_question();
246 $mc->shuffleanswers = false;
247 $mc->start_attempt(new question_attempt_step(), 1);
248
249 $this->assertEquals(array(
250 13 => new question_classified_response(13, 'A', 0.5),
251 14 => new question_classified_response(14, 'B', -1.0),
252 ), $mc->classify_response(array('choice0' => 1, 'choice1' => 1)));
253
254 $this->assertEquals(array(), $mc->classify_response(array()));
255 }
256}