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