MDL-34738 load sample dat file in tests
[moodle.git] / question / format / blackboard / tests / blackboardformat_test.php
CommitLineData
8a8dd7d8
JMV
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 Moodle Blackboard format.
19 *
20 * @package qformat_blackboard
21 * @copyright 2012 Jean-Michel Vedrine
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->libdir . '/questionlib.php');
30require_once($CFG->dirroot . '/question/format.php');
31require_once($CFG->dirroot . '/question/format/blackboard/format.php');
32require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
33
34
35/**
36 * Unit tests for the blackboard question import format.
37 *
38 * @copyright 2012 Jean-Michel Vedrine
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class qformat_blackboard_test extends question_testcase {
42
43 public function make_test_xml() {
f3ad0ed2 44 $xml = file_get_contents(__DIR__ . '/fixtures/sample_blackboard.dat');
8a8dd7d8
JMV
45 return $xml;
46 }
f3ad0ed2 47
8a8dd7d8
JMV
48 public function test_import_match() {
49
50 $xmldata = xmlize($this->make_test_xml());
51 $questions = array();
52
53 $importer = new qformat_blackboard();
54 $importer->process_matching($xmldata, $questions);
55 $q = $questions[0];
56 $expectedq = new stdClass();
57 $expectedq->qtype = 'match';
58 $expectedq->name = 'Classify the animals.';
59 $expectedq->questiontext = '<i>Classify the animals.</i>';
60 $expectedq->questiontextformat = FORMAT_HTML;
61 $expectedq->correctfeedback = array('text' => '',
62 'format' => FORMAT_HTML, 'files' => array());
63 $expectedq->partiallycorrectfeedback = array('text' => '',
64 'format' => FORMAT_HTML, 'files' => array());
65 $expectedq->incorrectfeedback = array('text' => '',
66 'format' => FORMAT_HTML, 'files' => array());
67 $expectedq->generalfeedback = '';
68 $expectedq->generalfeedbackformat = FORMAT_HTML;
69 $expectedq->defaultmark = 1;
70 $expectedq->length = 1;
71 $expectedq->penalty = 0.3333333;
72 $expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
73 $expectedq->subquestions = array(
74 array('text' => 'cat', 'format' => FORMAT_HTML, 'files' => array()),
75 array('text' => '', 'format' => FORMAT_HTML, 'files' => array()),
76 array('text' => 'frog', 'format' => FORMAT_HTML, 'files' => array()),
77 array('text' => 'newt', 'format' => FORMAT_HTML, 'files' => array()));
78 $expectedq->subanswers = array('mammal', 'insect', 'amphibian', 'amphibian');
79
80 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
81 }
82
83 public function test_import_multichoice_single() {
84
85 $xmldata = xmlize($this->make_test_xml());
86 $questions = array();
87
88 $importer = new qformat_blackboard();
89 $importer->process_mc($xmldata, $questions);
90 $q = $questions[0];
91
92 $expectedq = new stdClass();
93 $expectedq->qtype = 'multichoice';
94 $expectedq->single = 1;
95 $expectedq->name = 'What\'s between orange and green in the spectrum?';
96 $expectedq->questiontext = '<span style="font-size:12pt">What\'s between orange and green in the spectrum?</span>';
97 $expectedq->questiontextformat = FORMAT_HTML;
98 $expectedq->correctfeedback = array('text' => 'You gave the right answer.',
99 'format' => FORMAT_HTML, 'files' => array());
100 $expectedq->partiallycorrectfeedback = array('text' => '',
101 'format' => FORMAT_HTML, 'files' => array());
102 $expectedq->incorrectfeedback = array('text' => 'Only yellow is between orange and green in the spectrum.',
103 'format' => FORMAT_HTML, 'files' => array());
104 $expectedq->generalfeedback = '';
105 $expectedq->generalfeedbackformat = FORMAT_HTML;
106 $expectedq->defaultmark = 1;
107 $expectedq->length = 1;
108 $expectedq->penalty = 0.3333333;
109 $expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
110 $expectedq->answer = array(
111 0 => array(
112 'text' => '<span style="font-size:12pt">red</span>',
113 'format' => FORMAT_HTML,
114 'files' => array(),
115 ),
116 1 => array(
117 'text' => '<span style="font-size:12pt">yellow</span>',
118 'format' => FORMAT_HTML,
119 'files' => array(),
120 ),
121 2 => array(
122 'text' => '<span style="font-size:12pt">blue</span>',
123 'format' => FORMAT_HTML,
124 'files' => array(),
125 )
126 );
127 $expectedq->fraction = array(0, 1, 0);
128 $expectedq->feedback = array(
129 0 => array(
130 'text' => '',
131 'format' => FORMAT_HTML,
132 'files' => array(),
133 ),
134 1 => array(
135 'text' => '',
136 'format' => FORMAT_HTML,
137 'files' => array(),
138 ),
139 2 => array(
140 'text' => '',
141 'format' => FORMAT_HTML,
142 'files' => array(),
143 )
144 );
145
146 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
147 }
148
149 public function test_import_multichoice_multi() {
150
151 $xmldata = xmlize($this->make_test_xml());
152 $questions = array();
153
154 $importer = new qformat_blackboard();
155 $importer->process_ma($xmldata, $questions);
156 $q = $questions[0];
157
158 $expectedq = new stdClass();
159 $expectedq->qtype = 'multichoice';
160 $expectedq->single = 0;
161 $expectedq->name = 'What\'s between orange and green in the spectrum?';
162 $expectedq->questiontext = '<span style="font-size:12pt">What\'s between orange and green in the spectrum?</span>';
163 $expectedq->questiontextformat = FORMAT_HTML;
164 $expectedq->correctfeedback = array(
165 'text' => 'You gave the right answer.',
166 'format' => FORMAT_HTML,
167 'files' => array());
168 $expectedq->partiallycorrectfeedback = array(
169 'text' => 'Only yellow and off-beige are between orange and green in the spectrum.',
170 'format' => FORMAT_HTML,
171 'files' => array());
172 $expectedq->incorrectfeedback = array(
173 'text' => 'Only yellow and off-beige are between orange and green in the spectrum.',
174 'format' => FORMAT_HTML,
175 'files' => array());
176 $expectedq->generalfeedback = '';
177 $expectedq->generalfeedbackformat = FORMAT_HTML;
178 $expectedq->defaultmark = 1;
179 $expectedq->length = 1;
180 $expectedq->penalty = 0.3333333;
181 $expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
182 $expectedq->answer = array(
183 0 => array(
184 'text' => '<span style="font-size:12pt">yellow</span>',
185 'format' => FORMAT_HTML,
186 'files' => array(),
187 ),
188 1 => array(
189 'text' => '<span style="font-size:12pt">red</span>',
190 'format' => FORMAT_HTML,
191 'files' => array(),
192 ),
193 2 => array(
194 'text' => '<span style="font-size:12pt">off-beige</span>',
195 'format' => FORMAT_HTML,
196 'files' => array(),
197 ),
198 3 => array(
199 'text' => '<span style="font-size:12pt">blue</span>',
200 'format' => FORMAT_HTML,
201 'files' => array(),
202 )
203 );
204 $expectedq->fraction = array(0.5, 0, 0.5, 0);
205 $expectedq->feedback = array(
206 0 => array(
207 'text' => '',
208 'format' => FORMAT_HTML,
209 'files' => array(),
210 ),
211 1 => array(
212 'text' => '',
213 'format' => FORMAT_HTML,
214 'files' => array(),
215 ),
216 2 => array(
217 'text' => '',
218 'format' => FORMAT_HTML,
219 'files' => array(),
220 ),
221 3 => array(
222 'text' => '',
223 'format' => FORMAT_HTML,
224 'files' => array(),
225 )
226 );
227
228 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
229 }
230
231 public function test_import_truefalse() {
232
233 $xmldata = xmlize($this->make_test_xml());
234 $questions = array();
235
236 $importer = new qformat_blackboard();
237 $importer->process_tf($xmldata, $questions);
238 $q = $questions[0];
239
240 $expectedq = new stdClass();
241 $expectedq->qtype = 'truefalse';
242 $expectedq->name = '42 is the Absolute Answer to everything.';
243 $expectedq->questiontext = '<span style="font-size:12pt">42 is the Absolute Answer to everything.</span>';
244 $expectedq->questiontextformat = FORMAT_HTML;
245 $expectedq->generalfeedback = '';
246 $expectedq->generalfeedbackformat = FORMAT_HTML;
247 $expectedq->defaultmark = 1;
248 $expectedq->length = 1;
249 $expectedq->correctanswer = 0;
250 $expectedq->feedbacktrue = array(
251 'text' => '42 is the Ultimate Answer.',
252 'format' => FORMAT_HTML,
253 'files' => array(),
254 );
255 $expectedq->feedbackfalse = array(
256 'text' => 'You gave the right answer.',
257 'format' => FORMAT_HTML,
258 'files' => array(),
259 );
260 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
261 }
262
263 public function test_import_fill_in_the_blank() {
264
265 $xmldata = xmlize($this->make_test_xml());
266 $questions = array();
267
268 $importer = new qformat_blackboard();
269 $importer->process_fib($xmldata, $questions);
270 $q = $questions[0];
271
272 $expectedq = new stdClass();
273 $expectedq->qtype = 'shortanswer';
274 $expectedq->name = 'Name an amphibian: __________.';
275 $expectedq->questiontext = '<span style="font-size:12pt">Name an amphibian: __________.</span>';
276 $expectedq->questiontextformat = FORMAT_HTML;
277 $expectedq->generalfeedback = '';
278 $expectedq->generalfeedbackformat = FORMAT_HTML;
279 $expectedq->defaultmark = 1;
280 $expectedq->length = 1;
281 $expectedq->usecase = 0;
282 $expectedq->answer = array('frog', '*');
283 $expectedq->fraction = array(1, 0);
284 $expectedq->feedback = array(
285 0 => array(
286 'text' => '',
287 'format' => FORMAT_HTML,
288 'files' => array(),
289 ),
290 1 => array(
291 'text' => '',
292 'format' => FORMAT_HTML,
293 'files' => array(),
294 )
295 );
296
297 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
298 }
299
300 public function test_import_essay() {
301
302 $xmldata = xmlize($this->make_test_xml());
303 $questions = array();
304
305 $importer = new qformat_blackboard();
306 $importer->process_essay($xmldata, $questions);
307 $q = $questions[0];
308
309 $expectedq = new stdClass();
310 $expectedq->qtype = 'essay';
311 $expectedq->name = 'How are you?';
312 $expectedq->questiontext = 'How are you?';
313 $expectedq->questiontextformat = FORMAT_HTML;
314 $expectedq->generalfeedback = '';
315 $expectedq->generalfeedbackformat = FORMAT_HTML;
316 $expectedq->defaultmark = 1;
317 $expectedq->length = 1;
318 $expectedq->responseformat = 'editor';
319 $expectedq->responsefieldlines = 15;
320 $expectedq->attachments = 0;
321 $expectedq->graderinfo = array(
322 'text' => 'Blackboard answer for essay questions will be imported as informations for graders.',
323 'format' => FORMAT_HTML,
324 'files' => array());
325
326 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
327 }
328}