MDL-20636 Current work-in-progress converting the question engine to Moodle 2.0
[moodle.git] / question / engine / simpletest / helpers.php
CommitLineData
d1b7e03d
TH
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18
19/**
20 * This file contains helper classes for testing the question engine.
21 *
22 * @package moodlecore
23 * @subpackage questionengine
24 * @copyright 2009 The Open University
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28
29require_once(dirname(__FILE__) . '/../lib.php');
30
31
32/**
33 * Makes some protected methods of question_attempt public to facilitate testing.
34 *
35 * @copyright 2009 The Open University
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class testable_question_attempt extends question_attempt {
39 public function add_step($step) {#
40 parent::add_step($step);
41 }
42 public function set_min_fraction($fraction) {
43 $this->minfraction = $fraction;
44 }
45 public function set_behaviour(question_behaviour $behaviour) {
46 $this->behaviour = $behaviour;
47 }
48}
49
50
51/**
52 * This class creates questions of various types, which can then be used when
53 * testing.
54 *
55 * @copyright 2009 The Open University
56 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
57 */
58class test_question_maker {
59 const STANDARD_OVERALL_CORRECT_FEEDBACK = 'Well done!';
60 const STANDARD_OVERALL_PARTIALLYCORRECT_FEEDBACK = 'Parts, but only parts, of your response are correct.';
61 const STANDARD_OVERALL_INCORRECT_FEEDBACK = 'That is not right at all.';
62
63 /**
64 * Initialise the common fields of a question of any type.
65 */
66 public static function initialise_a_question($q) {
67 global $USER;
68
69 $q->id = 0;
70 $q->category = 0;
71 $q->parent = 0;
72 $q->questiontextformat = FORMAT_HTML;
73 $q->defaultmark = 1;
74 $q->penalty = 0.3333333;
75 $q->length = 1;
76 $q->stamp = make_unique_id_code();
77 $q->version = make_unique_id_code();
78 $q->hidden = 0;
79 $q->timecreated = time();
80 $q->timemodified = time();
81 $q->createdby = $USER->id;
82 $q->modifiedby = $USER->id;
83 }
84
85 /**
86 * Makes a truefalse question with correct answer true, defaultmark 1.
87 * @return qtype_truefalse_question
88 */
89 public static function make_a_truefalse_question() {
90 question_bank::load_question_definition_classes('truefalse');
91 $tf = new qtype_truefalse_question();
92 self::initialise_a_question($tf);
93 $tf->name = 'True/false question';
94 $tf->questiontext = 'The answer is true.';
95 $tf->generalfeedback = 'You should have selected true.';
96 $tf->penalty = 1;
97 $tf->qtype = question_bank::get_qtype('truefalse');
98
99 $tf->rightanswer = true;
100 $tf->truefeedback = 'This is the right answer.';
101 $tf->falsefeedback = 'This is the wrong answer.';
102
103 return $tf;
104 }
105
106 /**
107 * Makes a multichoice question with choices 'A', 'B' and 'C' shuffled. 'A'
108 * is correct, defaultmark 1.
109 * @return qtype_multichoice_single_question
110 */
111 public static function make_a_multichoice_single_question() {
112 question_bank::load_question_definition_classes('multichoice');
113 $mc = new qtype_multichoice_single_question();
114 self::initialise_a_question($mc);
115 $mc->name = 'Multi-choice question, single response';
116 $mc->questiontext = 'The answer is A.';
117 $mc->generalfeedback = 'You should have selected A.';
118 $mc->qtype = question_bank::get_qtype('multichoice');
119
120 $mc->shuffleanswers = 1;
121 $mc->answernumbering = 'abc';
122
123 $mc->answers = array(
124 13 => new question_answer('A', 1, 'A is right'),
125 14 => new question_answer('B', -0.3333333, 'B is wrong'),
126 15 => new question_answer('C', -0.3333333, 'C is wrong'),
127 );
128
129 return $mc;
130 }
131
132 /**
133 * Makes a multichoice question with choices 'A', 'B', 'C' and 'D' shuffled.
134 * 'A' and 'C' is correct, defaultmark 1.
135 * @return qtype_multichoice_multi_question
136 */
137 public static function make_a_multichoice_multi_question() {
138 question_bank::load_question_definition_classes('multichoice');
139 $mc = new qtype_multichoice_multi_question();
140 self::initialise_a_question($mc);
141 $mc->name = 'Multi-choice question, multiple response';
142 $mc->questiontext = 'The answer is A and C.';
143 $mc->generalfeedback = 'You should have selected A and C.';
144 $mc->qtype = question_bank::get_qtype('multichoice');
145
146 $mc->shuffleanswers = 1;
147 $mc->answernumbering = 'abc';
148
149 self::set_standard_combined_feedback_fields($mc);
150
151 $mc->answers = array(
152 13 => new question_answer('A', 0.5, 'A is part of the right answer'),
153 14 => new question_answer('B', -1, 'B is wrong'),
154 15 => new question_answer('C', 0.5, 'C is part of the right answer'),
155 16 => new question_answer('D', -1, 'D is wrong'),
156 );
157
158 return $mc;
159 }
160
161 /**
162 * Makes a matching question to classify 'Dog', 'Frog', 'Toad' and 'Cat' as
163 * 'Mammal', 'Amphibian' or 'Insect'.
164 * defaultmark 1. Stems are shuffled by default.
165 * @return qtype_match_question
166 */
167 public static function make_a_matching_question() {
168 question_bank::load_question_definition_classes('match');
169 $match = new qtype_match_question();
170 self::initialise_a_question($match);
171 $match->name = 'Matching question';
172 $match->questiontext = 'Classify the animals.';
173 $match->generalfeedback = 'Frogs and toads are amphibians, the others are mammals.';
174 $match->qtype = question_bank::get_qtype('match');
175
176 $match->shufflestems = 1;
177
178 self::set_standard_combined_feedback_fields($match);
179
180 $match->stems = array('', 'Dog', 'Frog', 'Toad', 'Cat');
181 $match->choices = array('', 'Mammal', 'Amphibian', 'Insect');
182 $match->right = array('', 1, 2, 2, 1);
183 unset($match->stems[0]);
184 unset($match->choices[0]);
185 unset($match->right[0]);
186
187 return $match;
188 }
189
190 /**
191 * Makes a shortanswer question with correct ansewer 'frog', partially
192 * correct answer 'toad' and defaultmark 1.
193 * @return qtype_shortanswer_question
194 */
195 public static function make_a_shortanswer_question() {
196 question_bank::load_question_definition_classes('shortanswer');
197 $sa = new qtype_shortanswer_question();
198 self::initialise_a_question($sa);
199 $sa->name = 'Short answer question';
200 $sa->questiontext = 'Name an amphibian: __________';
201 $sa->generalfeedback = 'Generalfeedback: frog or toad would have been OK.';
202 $sa->usecase = false;
203 $sa->answers = array(
204 new question_answer('frog', 1.0, 'Frog is a very good answer.'),
205 new question_answer('toad', 0.8, 'Toad is an OK good answer.'),
206 new question_answer('*', 0.0, 'That is a bad answer.'),
207 );
208 $sa->qtype = question_bank::get_qtype('shortanswer');
209
210 return $sa;
211 }
212
213 /**
214 * Makes a numerical question with correct ansewer 3.14, and various incorrect
215 * answers with different feedback.
216 * @return qtype_numerical_question
217 */
218 public static function make_a_numerical_question() {
219 question_bank::load_question_definition_classes('numerical');
220 $num = new qtype_numerical_question();
221 self::initialise_a_question($num);
222 $num->name = 'Pi to two d.p.';
223 $num->questiontext = 'What is pi to two d.p.?';
224 $num->generalfeedback = 'Generalfeedback: 3.14 is the right answer.';
225 $num->answers = array(
226 new qtype_numerical_answer('3.14', 1.0, 'Very good.', 0),
227 new qtype_numerical_answer('3.142', 0.0, 'Too accurate.', 0.005),
228 new qtype_numerical_answer('3.1', 0.0, 'Not accurate enough.', 0.05),
229 new qtype_numerical_answer('3', 0.0, 'Not accurate enough.', 0.5),
230 new qtype_numerical_answer('*', 0.0, 'Completely wrong.', 0),
231 );
232 $num->qtype = question_bank::get_qtype('numerical');
233 $num->ap = new qtype_numerical_answer_processor(array());
234
235 return $num;
236 }
237
238 /**
239 * Makes a truefalse question with correct ansewer true, defaultmark 1.
240 * @return qtype_essay_question
241 */
242 public static function make_an_essay_question() {
243 question_bank::load_question_definition_classes('essay');
244 $essay = new qtype_essay_question();
245 self::initialise_a_question($essay);
246 $essay->name = 'Essay question';
247 $essay->questiontext = 'Write an essay.';
248 $essay->generalfeedback = 'I hope you wrote an interesting essay.';
249 $essay->penalty = 0;
250 $essay->qtype = question_bank::get_qtype('essay');
251
252 return $essay;
253 }
254
255 /**
256 * Makes a truefalse question with correct ansewer true, defaultmark 1.
257 * @return question_truefalse
258 */
259 public static function make_a_description_question() {
260 question_bank::load_question_definition_classes('description');
261 $description = new qtype_description_question();
262 self::initialise_a_question($description);
263 $description->name = 'Description question';
264 $description->questiontext = 'This text tells you a bit about the next few questions in this quiz.';
265 $description->generalfeedback = 'This is what this seciton of the quiz should have taught you.';
266 $description->qtype = question_bank::get_qtype('description');
267
268 return $description;
269 }
270
271 /**
272 * Add some standard overall feedback to a question. You need to use these
273 * specific feedback strings for the corresponding contains_..._feedback
274 * methods in {@link qbehaviour_walkthrough_test_base} to works.
275 * @param question_definition $q the question to add the feedback to.
276 */
277 public static function set_standard_combined_feedback_fields($q) {
278 $q->correctfeedback = self::STANDARD_OVERALL_CORRECT_FEEDBACK;
279 $q->partiallycorrectfeedback = self::STANDARD_OVERALL_PARTIALLYCORRECT_FEEDBACK;
280 $q->shownumcorrect = true;
281 $q->incorrectfeedback = self::STANDARD_OVERALL_INCORRECT_FEEDBACK;
282 }
283}
284
285
286abstract class testing_db_record_builder {
287 public static function build_db_records(array $table) {
288 $columns = array_shift($table);
289 $records = array();
290 foreach ($table as $row) {
291 if (count($row) != count($columns)) {
292 throw new Exception("Row contains the wrong number of fields.");
293 }
294 $rec = new stdClass;
295 foreach ($columns as $i => $name) {
296 $rec->$name = $row[$i];
297 }
298 $records[] = $rec;
299 }
300 return $records;
301 }
302}
303
304
305class data_loading_method_test_base extends UnitTestCase {
306 public function build_db_records(array $table) {
307 return testing_db_record_builder::build_db_records($table);
308 }
309}
310
311
312class qbehaviour_walkthrough_test_base extends UnitTestCase {
313 /** @var question_display_options */
314 protected $displayoptions;
315 /** @var question_usage_by_activity */
316 protected $quba;
317 /** @var unknown_type integer */
318 protected $slot;
319
320 public function setUp() {
321 $this->displayoptions = new question_display_options();
322 $this->quba = question_engine::make_questions_usage_by_activity('unit_test',
323 get_context_instance(CONTEXT_SYSTEM));
324 }
325
326 public function tearDown() {
327 $this->displayoptions = null;
328 $this->quba = null;
329 }
330
331 protected function start_attempt_at_question($question, $preferredbehaviour, $maxmark = null) {
332 $this->quba->set_preferred_behaviour($preferredbehaviour);
333 $this->slot = $this->quba->add_question($question, $maxmark);
334 $this->quba->start_all_questions();
335 }
336 protected function process_submission($data) {
337 $this->quba->process_action($this->slot, $data);
338 }
339
340 protected function manual_grade($comment, $mark) {
341 $this->quba->manual_grade($this->slot, $comment, $mark);
342 }
343
344 protected function check_current_state($state) {
345 $this->assertEqual($this->quba->get_question_state($this->slot), $state, 'Questions is in the wrong state: %s.');
346 }
347
348 protected function check_current_mark($mark) {
349 if (is_null($mark)) {
350 $this->assertNull($this->quba->get_question_mark($this->slot));
351 } else {
352 if ($mark == 0) {
353 // PHP will think a null mark and a mark of 0 are equal,
354 // so explicity check not null in this case.
355 $this->assertNotNull($this->quba->get_question_mark($this->slot));
356 }
357 $this->assertWithinMargin($mark, $this->quba->get_question_mark($this->slot),
358 0.000001, 'Expected mark and actual mark differ: %s.');
359 }
360 }
361
362 /**
363 * @param $condition one or more Expectations. (users varargs).
364 */
365 protected function check_current_output() {
366 $html = $this->quba->render_question($this->slot, $this->displayoptions);
367 foreach (func_get_args() as $condition) {
368 $this->assert($condition, $html);
369 }
370 }
371
372 protected function get_question_attempt() {
373 return $this->quba->get_question_attempt($this->slot);
374 }
375
376 protected function get_step_count() {
377 return $this->get_question_attempt()->get_num_steps();
378 }
379
380 protected function check_step_count($expectednumsteps) {
381 $this->assertEqual($expectednumsteps, $this->get_step_count());
382 }
383
384 protected function get_step($stepnum) {
385 return $this->get_question_attempt()->get_step($stepnum);
386 }
387
388 protected function get_contains_question_text_expectation($question) {
389 return new PatternExpectation('/' . preg_quote($question->questiontext) . '/');
390 }
391
392 protected function get_contains_general_feedback_expectation() {
393 return new PatternExpectation('/' . preg_quote($question->generalfeedback) . '/');
394 }
395
396 protected function get_does_not_contain_correctness_expectation() {
397 return new NoPatternExpectation('/class=\"correctness/');
398 }
399
400 protected function get_contains_correct_expectation() {
401 return new PatternExpectation('/' . preg_quote(get_string('correct', 'question')) . '/');
402 }
403
404 protected function get_contains_partcorrect_expectation() {
405 return new PatternExpectation('/' . preg_quote(get_string('partiallycorrect', 'question')) . '/');
406 }
407
408 protected function get_contains_incorrect_expectation() {
409 return new PatternExpectation('/' . preg_quote(get_string('incorrect', 'question')) . '/');
410 }
411
412 protected function get_contains_standard_correct_combined_feedback_expectation() {
413 return new PatternExpectation('/' . preg_quote(test_question_maker::STANDARD_OVERALL_CORRECT_FEEDBACK) . '/');
414 }
415
416 protected function get_contains_standard_partiallycorrect_combined_feedback_expectation() {
417 return new PatternExpectation('/' . preg_quote(test_question_maker::STANDARD_OVERALL_PARTIALLYCORRECT_FEEDBACK) . '/');
418 }
419
420 protected function get_contains_standard_incorrect_combined_feedback_expectation() {
421 return new PatternExpectation('/' . preg_quote(test_question_maker::STANDARD_OVERALL_INCORRECT_FEEDBACK) . '/');
422 }
423
424 protected function get_does_not_contain_feedback_expectation() {
425 return new NoPatternExpectation('/class="feedback"/');
426 }
427
428 protected function get_does_not_contain_num_parts_correct() {
429 return new NoPatternExpectation('/class="numpartscorrect"/');
430 }
431
432 protected function get_contains_num_parts_correct($num) {
433 $a = new stdClass;
434 $a->num = $num;
435 return new PatternExpectation('/<div class="numpartscorrect">' .
436 preg_quote(get_string('yougotnright', 'question', $a)) . '/');
437 }
438
439 protected function get_does_not_contain_specific_feedback_expectation() {
440 return new NoPatternExpectation('/class="specificfeedback"/');
441 }
442
443 protected function get_contains_validation_error_expectation() {
444 return new ContainsTagWithAttribute('div', 'class', 'validationerror');
445 }
446
447 protected function get_does_not_contain_validation_error_expectation() {
448 return new NoPatternExpectation('/class="validationerror"/');
449 }
450
451 protected function get_contains_mark_summary($mark) {
452 $a = new stdClass;
453 $a->mark = format_float($mark, $this->displayoptions->markdp);
454 $a->max = format_float($this->quba->get_question_max_mark($this->slot),
455 $this->displayoptions->markdp);
456 return new PatternExpectation('/' .
457 preg_quote(get_string('markoutofmax', 'question', $a)) . '/');
458 }
459
460 protected function get_contains_marked_out_of_summary() {
461 $max = format_float($this->quba->get_question_max_mark($this->slot),
462 $this->displayoptions->markdp);
463 return new PatternExpectation('/' .
464 preg_quote(get_string('markedoutofmax', 'question', $max)) . '/');
465 }
466
467 protected function get_does_not_contain_mark_summary() {
468 return new NoPatternExpectation('/<div class="grade">/');
469 }
470
471 protected function get_contains_checkbox_expectation($baseattr, $enabled, $checked) {
472 $expectedattributes = $baseattr;
473 $forbiddenattributes = array();
474 $expectedattributes['type'] = 'checkbox';
475 if ($enabled === true) {
476 $forbiddenattributes['disabled'] = 'disabled';
477 } else if ($enabled === false) {
478 $expectedattributes['disabled'] = 'disabled';
479 }
480 if ($checked === true) {
481 $expectedattributes['checked'] = 'checked';
482 } else if ($checked === false) {
483 $forbiddenattributes['checked'] = 'checked';
484 }
485 return new ContainsTagWithAttributes('input', $expectedattributes, $forbiddenattributes);
486 }
487
488 protected function get_contains_mc_checkbox_expectation($index, $enabled = null, $checked = null) {
489 return $this->get_contains_checkbox_expectation(array(
490 'name' => $this->quba->get_field_prefix($this->slot) . $index,
491 'value' => 1,
492 ), $enabled, $checked);
493 }
494
495 protected function get_contains_radio_expectation($baseattr, $enabled, $checked) {
496 $expectedattributes = $baseattr;
497 $forbiddenattributes = array();
498 $expectedattributes['type'] = 'radio';
499 if ($enabled === true) {
500 $forbiddenattributes['disabled'] = 'disabled';
501 } else if ($enabled === false) {
502 $expectedattributes['disabled'] = 'disabled';
503 }
504 if ($checked === true) {
505 $expectedattributes['checked'] = 'checked';
506 } else if ($checked === false) {
507 $forbiddenattributes['checked'] = 'checked';
508 }
509 return new ContainsTagWithAttributes('input', $expectedattributes, $forbiddenattributes);
510 }
511
512 protected function get_contains_mc_radio_expectation($index, $enabled = null, $checked = null) {
513 return $this->get_contains_radio_expectation(array(
514 'name' => $this->quba->get_field_prefix($this->slot) . 'answer',
515 'value' => $index,
516 ), $enabled, $checked);
517 }
518
519 protected function get_contains_hidden_expectation($name, $value = null) {
520 $expectedattributes = array('type' => 'hidden', 'name' => s($name));
521 if (!is_null($value)) {
522 $expectedattributes['value'] = s($value);
523 }
524 return new ContainsTagWithAttributes('input', $expectedattributes);
525 }
526
527 protected function get_does_not_contain_hidden_expectation($name, $value = null) {
528 $expectedattributes = array('type' => 'hidden', 'name' => s($name));
529 if (!is_null($value)) {
530 $expectedattributes['value'] = s($value);
531 }
532 return new DoesNotContainTagWithAttributes('input', $expectedattributes);
533 }
534
535 protected function get_contains_tf_true_radio_expectation($enabled = null, $checked = null) {
536 return $this->get_contains_radio_expectation(array(
537 'name' => $this->quba->get_field_prefix($this->slot) . 'answer',
538 'value' => 1,
539 ), $enabled, $checked);
540 }
541
542 protected function get_contains_tf_false_radio_expectation($enabled = null, $checked = null) {
543 return $this->get_contains_radio_expectation(array(
544 'name' => $this->quba->get_field_prefix($this->slot) . 'answer',
545 'value' => 0,
546 ), $enabled, $checked);
547 }
548
549 protected function get_contains_cbm_radio_expectation($certainty, $enabled = null, $checked = null) {
550 return $this->get_contains_radio_expectation(array(
551 'name' => $this->quba->get_field_prefix($this->slot) . '-certainty',
552 'value' => $certainty,
553 ), $enabled, $checked);
554 }
555
556 protected function get_contains_button_expectation($name, $value = null, $enabled = null) {
557 $expectedattributes = array(
558 'type' => 'submit',
559 'name' => $name,
560 );
561 $forbiddenattributes = array();
562 if (!is_null($value)) {
563 $expectedattributes['value'] = $value;
564 }
565 if ($enabled === true) {
566 $forbiddenattributes['disabled'] = 'disabled';
567 } else if ($enabled === false) {
568 $expectedattributes['disabled'] = 'disabled';
569 }
570 return new ContainsTagWithAttributes('input', $expectedattributes, $forbiddenattributes);
571 }
572
573 protected function get_contains_submit_button_expectation($enabled = null) {
574 return $this->get_contains_button_expectation(
575 $this->quba->get_field_prefix($this->slot) . '-submit', null, $enabled);
576 }
577
578 protected function get_tries_remaining_expectation($n) {
579 return new PatternExpectation('/' . preg_quote(get_string('triesremaining', 'qbehaviour_interactive', $n)) . '/');
580 }
581
582 protected function get_invalid_answer_expectation() {
583 return new PatternExpectation('/' . preg_quote(get_string('invalidanswer', 'question')) . '/');
584 }
585
586 protected function get_contains_try_again_button_expectation($enabled = null) {
587 $expectedattributes = array(
588 'type' => 'submit',
589 'name' => $this->quba->get_field_prefix($this->slot) . '-tryagain',
590 );
591 $forbiddenattributes = array();
592 if ($enabled === true) {
593 $forbiddenattributes['disabled'] = 'disabled';
594 } else if ($enabled === false) {
595 $expectedattributes['disabled'] = 'disabled';
596 }
597 return new ContainsTagWithAttributes('input', $expectedattributes, $forbiddenattributes);
598 }
599
600 protected function get_does_not_contain_try_again_button_expectation() {
601 return new NoPatternExpectation('/name="' .
602 $this->quba->get_field_prefix($this->slot) . '-tryagain"/');
603 }
604
605 protected function get_contains_select_expectation($name, $choices,
606 $selected = null, $enabled = null) {
607 $fullname = $this->quba->get_field_prefix($this->slot) . $name;
608 return new ContainsSelectExpectation($fullname, $choices, $selected, $enabled);
609 }
610
611 protected function get_mc_right_answer_index($mc) {
612 $order = $mc->get_order($this->get_question_attempt());
613 foreach ($order as $i => $ansid) {
614 if ($mc->answers[$ansid]->fraction == 1) {
615 return $i;
616 }
617 }
618 $this->fail('This multiple choice question does not seem to have a right answer!');
619 }
620
621 protected function get_no_hint_visible_expectation() {
622 return new NoPatternExpectation('/class="hint"/');
623 }
624
625 protected function get_contains_hint_expectation($hinttext) {
626 // Does not currently verify hint text.
627 return new ContainsTagWithAttribute('div', 'class', 'hint');
628 }
629}