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