2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Unit tests for the Moodle Examview format.
20 * @package qformat_examview
21 * @copyright 2012 jean-Michel Vedrine
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir . '/questionlib.php');
30 require_once($CFG->dirroot . '/question/format.php');
31 require_once($CFG->dirroot . '/question/format/examview/format.php');
32 require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
36 * Unit tests for the examview question import format.
38 * @copyright 2012 Jean-Michel Vedrine
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class qformat_examview_test extends question_testcase {
43 public function make_test_xml() {
44 $xml = $xml = file_get_contents(__DIR__ . '/fixtures/examview_sample.xml');
45 return array(0=>$xml);
48 public function test_import_truefalse() {
50 $xml = $this->make_test_xml();
53 $importer = new qformat_examview();
54 $questions = $importer->readquestions($xml);
57 $expectedq = new stdClass();
58 $expectedq->qtype = 'truefalse';
59 $expectedq->name = '42 is the Absolute Answer to everything.';
60 $expectedq->questiontext = "42 is the Absolute Answer to everything.";
61 $expectedq->questiontextformat = FORMAT_HTML;
62 $expectedq->generalfeedback = '';
63 $expectedq->generalfeedbackformat = FORMAT_MOODLE;
64 $expectedq->defaultmark = 1;
65 $expectedq->length = 1;
66 $expectedq->correctanswer = 0;
67 $expectedq->feedbacktrue = array(
68 'text' => get_string('incorrect', 'question'),
69 'format' => FORMAT_HTML,
72 $expectedq->feedbackfalse = array(
73 'text' => get_string('correct', 'question'),
74 'format' => FORMAT_HTML,
78 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
80 public function test_import_multichoice_single() {
81 $xml = $this->make_test_xml();
84 $importer = new qformat_examview();
85 $questions = $importer->readquestions($xml);
88 $expectedq = new stdClass();
89 $expectedq->qtype = 'multichoice';
90 $expectedq->single = 1;
91 $expectedq->name = "What's between orange and green in the spectrum?";
92 $expectedq->questiontext = "What's between orange and green in the spectrum?";
93 $expectedq->questiontextformat = FORMAT_HTML;
94 $expectedq->correctfeedback = array('text' => '',
95 'format' => FORMAT_HTML, 'files' => array());
96 $expectedq->partiallycorrectfeedback = array('text' => '',
97 'format' => FORMAT_HTML, 'files' => array());
98 $expectedq->incorrectfeedback = array('text' => '',
99 'format' => FORMAT_HTML, 'files' => array());
100 $expectedq->generalfeedback = '';
101 $expectedq->generalfeedbackformat = FORMAT_MOODLE;
102 $expectedq->defaultmark = 1;
103 $expectedq->length = 1;
104 $expectedq->penalty = 0.3333333;
105 $expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
106 $expectedq->answer = array(
109 'format' => FORMAT_HTML,
114 'format' => FORMAT_HTML,
119 'format' => FORMAT_HTML,
123 $expectedq->fraction = array(0, 1, 0);
124 $expectedq->feedback = array(
126 'text' => get_string('incorrect', 'question'),
127 'format' => FORMAT_HTML,
131 'text' => get_string('correct', 'question'),
132 'format' => FORMAT_HTML,
136 'text' => get_string('incorrect', 'question'),
137 'format' => FORMAT_HTML,
142 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
145 public function test_import_numerical() {
147 $xml = $this->make_test_xml();
148 $questions = array();
150 $importer = new qformat_examview();
151 $questions = $importer->readquestions($xml);
154 $expectedq = new stdClass();
155 $expectedq->qtype = 'numerical';
156 $expectedq->name = 'This is a numeric response question. How much is 12 * 2?';
157 $expectedq->questiontext = 'This is a numeric response question. How much is 12 * 2?';
158 $expectedq->questiontextformat = FORMAT_HTML;
159 $expectedq->generalfeedback = '';
160 $expectedq->generalfeedbackformat = FORMAT_MOODLE;
161 $expectedq->defaultmark = 1;
162 $expectedq->length = 1;
163 $expectedq->penalty = 0.3333333;
164 $expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
165 $expectedq->answer = array('24');
166 $expectedq->fraction = array(1);
167 $expectedq->feedback = array(
169 'text' => get_string('correct', 'question'),
170 'format' => FORMAT_HTML,
175 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
180 public function test_import_fill_in_the_blank() {
182 $xml = $this->make_test_xml();
183 $questions = array();
185 $importer = new qformat_examview();
186 $questions = $importer->readquestions($xml);
189 $expectedq = new stdClass();
190 $expectedq->qtype = 'shortanswer';
191 $expectedq->name = 'Name an amphibian: __________.';
192 $expectedq->questiontext = 'Name an amphibian: __________.';
193 $expectedq->questiontextformat = FORMAT_HTML;
194 $expectedq->generalfeedback = '';
195 $expectedq->generalfeedbackformat = FORMAT_MOODLE;
196 $expectedq->defaultmark = 1;
197 $expectedq->length = 1;
198 $expectedq->usecase = 0;
199 $expectedq->answer = array('frog', '*');
200 $expectedq->fraction = array(1, 0);
201 $expectedq->feedback = array(
203 'text' => get_string('correct', 'question'),
204 'format' => FORMAT_HTML,
208 'text' => get_string('incorrect', 'question'),
209 'format' => FORMAT_HTML,
214 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
217 public function test_import_essay() {
219 $xml = $this->make_test_xml();
220 $questions = array();
222 $importer = new qformat_examview();
223 $questions = $importer->readquestions($xml);
226 $expectedq = new stdClass();
227 $expectedq->qtype = 'essay';
228 $expectedq->name = 'How are you?';
229 $expectedq->questiontext = 'How are you?';
230 $expectedq->questiontextformat = FORMAT_HTML;
231 $expectedq->generalfeedback = '';
232 $expectedq->generalfeedbackformat = FORMAT_MOODLE;
233 $expectedq->defaultmark = 1;
234 $expectedq->length = 1;
235 $expectedq->responseformat = 'editor';
236 $expectedq->responsefieldlines = 15;
237 $expectedq->attachments = 0;
238 $expectedq->graderinfo = array(
239 'text' => 'Examview answer for essay questions will be imported as informations for graders.',
240 'format' => FORMAT_HTML,
243 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
246 // Due to the way matching questions are parsed,
247 // the test for matching questions is somewhat different.
248 // First we test the parse_matching_groups method alone.
249 // Then we test the whole process wich involve parse_matching_groups,
250 // parse_ma and process_matches methods.
251 public function test_parse_matching_groups() {
252 $lines = $this->make_test_xml();
254 $importer = new qformat_examview();
255 $text = implode($lines, ' ');
257 $xml = xmlize($text, 0);
258 $importer->parse_matching_groups($xml['examview']['#']['matching-group']);
259 $matching = $importer->matching_questions;
260 $group = new stdClass();
261 $group->questiontext = 'Classify the animals.';
262 $group->subchoices = array('A' => 'insect', 'B' => 'amphibian', 'C' =>'mammal');
263 $group->subquestions = array();
264 $group->subanswers = array();
265 $expectedmatching = array( 'Matching 1' => $group);
267 $this->assertEquals($matching, $expectedmatching);
270 public function test_import_match() {
272 $xml = $this->make_test_xml();
273 $questions = array();
275 $importer = new qformat_examview();
276 $questions = $importer->readquestions($xml);
279 $expectedq = new stdClass();
280 $expectedq->qtype = 'match';
281 $expectedq->name = 'Classify the animals.';
282 $expectedq->questiontext = 'Classify the animals.';
283 $expectedq->questiontextformat = FORMAT_HTML;
284 $expectedq->correctfeedback = array('text' => '',
285 'format' => FORMAT_HTML, 'files' => array());
286 $expectedq->partiallycorrectfeedback = array('text' => '',
287 'format' => FORMAT_HTML, 'files' => array());
288 $expectedq->incorrectfeedback = array('text' => '',
289 'format' => FORMAT_HTML, 'files' => array());
290 $expectedq->generalfeedback = '';
291 $expectedq->generalfeedbackformat = FORMAT_MOODLE;
292 $expectedq->defaultmark = 1;
293 $expectedq->length = 1;
294 $expectedq->penalty = 0.3333333;
295 $expectedq->shuffleanswers = get_config('quiz', 'shuffleanswers');
296 $expectedq->subquestions = array(
297 array('text' => '', 'format' => FORMAT_HTML, 'files' => array()),
298 array('text' => 'frog', 'format' => FORMAT_HTML, 'files' => array()),
299 array('text' => 'newt', 'format' => FORMAT_HTML, 'files' => array()),
300 array('text' => 'cat', 'format' => FORMAT_HTML, 'files' => array()));
301 $expectedq->subanswers = array('insect', 'amphibian', 'amphibian', 'mammal');
303 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);