Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * This file contains helper classes for testing the question engine.
19
 *
20
 * @package    moodlecore
21
 * @subpackage questionengine
22
 * @copyright  2009 The Open University
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
global $CFG;
30
require_once(__DIR__ . '/../lib.php');
31
require_once($CFG->dirroot . '/lib/phpunit/lib.php');
32
 
33
 
34
/**
35
 * Makes some protected methods of question_attempt public to facilitate testing.
36
 *
37
 * @copyright  2009 The Open University
38
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class testable_question_attempt extends question_attempt {
41
    public function add_step(question_attempt_step $step) {
42
        parent::add_step($step);
43
    }
44
    public function set_min_fraction($fraction) {
45
        $this->minfraction = $fraction;
46
    }
47
    public function set_max_fraction($fraction) {
48
        $this->maxfraction = $fraction;
49
    }
50
    public function set_behaviour(question_behaviour $behaviour) {
51
        $this->behaviour = $behaviour;
52
    }
53
}
54
 
55
 
56
/**
57
 * Test subclass to allow access to some protected data so that the correct
58
 * behaviour can be verified.
59
 *
60
 * @copyright  2012 The Open University
61
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
62
 */
63
class testable_question_engine_unit_of_work extends question_engine_unit_of_work {
64
    public function get_modified() {
65
        return $this->modified;
66
    }
67
 
68
    public function get_attempts_added() {
69
        return $this->attemptsadded;
70
    }
71
 
72
    public function get_attempts_modified() {
73
        return $this->attemptsmodified;
74
    }
75
 
76
    public function get_steps_added() {
77
        return $this->stepsadded;
78
    }
79
 
80
    public function get_steps_modified() {
81
        return $this->stepsmodified;
82
    }
83
 
84
    public function get_steps_deleted() {
85
        return $this->stepsdeleted;
86
    }
87
 
88
    public function get_metadata_added() {
89
        return $this->metadataadded;
90
    }
91
 
92
    public function get_metadata_modified() {
93
        return $this->metadatamodified;
94
    }
95
}
96
 
97
 
98
/**
99
 * Base class for question type test helpers.
100
 *
101
 * @copyright  2011 The Open University
102
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
103
 */
104
abstract class question_test_helper {
105
    /**
106
     * @return array of example question names that can be passed as the $which
107
     * argument of {@link test_question_maker::make_question} when $qtype is
108
     * this question type.
109
     */
110
    abstract public function get_test_questions();
111
 
112
    /**
113
     * Set up a form to create a question in $cat. This method also sets cat and contextid on $questiondata object.
114
     * @param object $cat the category
115
     * @param object $questiondata form initialisation requires question data.
116
     * @return moodleform
117
     */
118
    public static function get_question_editing_form($cat, $questiondata) {
119
        $catcontext = context::instance_by_id($cat->contextid, MUST_EXIST);
120
        $contexts = new core_question\local\bank\question_edit_contexts($catcontext);
121
        $dataforformconstructor = new stdClass();
122
        $dataforformconstructor->createdby = $questiondata->createdby;
123
        $dataforformconstructor->qtype = $questiondata->qtype;
124
        $dataforformconstructor->contextid = $questiondata->contextid = $catcontext->id;
125
        $dataforformconstructor->category = $questiondata->category = $cat->id;
126
        $dataforformconstructor->status = $questiondata->status;
127
        $dataforformconstructor->formoptions = new stdClass();
128
        $dataforformconstructor->formoptions->canmove = true;
129
        $dataforformconstructor->formoptions->cansaveasnew = true;
130
        $dataforformconstructor->formoptions->canedit = true;
131
        $dataforformconstructor->formoptions->repeatelements = true;
132
        $qtype = question_bank::get_qtype($questiondata->qtype);
133
        return  $qtype->create_editing_form('question.php', $dataforformconstructor, $cat, $contexts, true);
134
    }
135
}
136
 
137
 
138
/**
139
 * This class creates questions of various types, which can then be used when
140
 * testing.
141
 *
142
 * @copyright  2009 The Open University
143
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
144
 */
145
class test_question_maker {
146
    const STANDARD_OVERALL_CORRECT_FEEDBACK = 'Well done!';
147
    const STANDARD_OVERALL_PARTIALLYCORRECT_FEEDBACK =
148
        'Parts, but only parts, of your response are correct.';
149
    const STANDARD_OVERALL_INCORRECT_FEEDBACK = 'That is not right at all.';
150
 
151
    /** @var array qtype => qtype test helper class. */
152
    protected static $testhelpers = array();
153
 
154
    /**
155
     * Just make a question_attempt at a question. Useful for unit tests that
156
     * need to pass a $qa to methods that call format_text. Probably not safe
157
     * to use for anything beyond that.
158
     * @param question_definition $question a question.
159
     * @param number $maxmark the max mark to set.
160
     * @return question_attempt the question attempt.
161
     */
162
    public static function get_a_qa($question, $maxmark = 3) {
163
        return new question_attempt($question, 13, null, $maxmark);
164
    }
165
 
166
    /**
167
     * Initialise the common fields of a question of any type.
168
     */
169
    public static function initialise_a_question($q) {
170
        global $USER;
171
 
172
        $q->id = 0;
173
        $q->category = 0;
174
        $q->idnumber = null;
175
        $q->parent = 0;
176
        $q->questiontextformat = FORMAT_HTML;
177
        $q->generalfeedbackformat = FORMAT_HTML;
178
        $q->defaultmark = 1;
179
        $q->penalty = 0.3333333;
180
        $q->length = 1;
181
        $q->stamp = make_unique_id_code();
182
        $q->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
183
        $q->version = 1;
184
        $q->timecreated = time();
185
        $q->timemodified = time();
186
        $q->createdby = $USER->id;
187
        $q->modifiedby = $USER->id;
188
    }
189
 
190
    public static function initialise_question_data($qdata) {
191
        global $USER;
192
 
193
        $qdata->id = 0;
194
        $qdata->category = 0;
195
        $qdata->idnumber = null;
196
        $qdata->contextid = 0;
197
        $qdata->parent = 0;
198
        $qdata->questiontextformat = FORMAT_HTML;
199
        $qdata->generalfeedbackformat = FORMAT_HTML;
200
        $qdata->defaultmark = 1;
201
        $qdata->penalty = 0.3333333;
202
        $qdata->length = 1;
203
        $qdata->stamp = make_unique_id_code();
204
        $qdata->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
205
        $qdata->version = 1;
206
        $qdata->timecreated = time();
207
        $qdata->timemodified = time();
208
        $qdata->createdby = $USER->id;
209
        $qdata->modifiedby = $USER->id;
210
        $qdata->hints = array();
211
    }
212
 
213
    /**
214
     * Get the test helper class for a particular question type.
215
     * @param $qtype the question type name, e.g. 'multichoice'.
216
     * @return question_test_helper the test helper class.
217
     */
218
    public static function get_test_helper($qtype) {
219
        global $CFG;
220
 
221
        if (array_key_exists($qtype, self::$testhelpers)) {
222
            return self::$testhelpers[$qtype];
223
        }
224
 
225
        $file = core_component::get_plugin_directory('qtype', $qtype) . '/tests/helper.php';
226
        if (!is_readable($file)) {
227
            throw new coding_exception('Question type ' . $qtype .
228
                ' does not have test helper code.');
229
        }
230
        include_once($file);
231
 
232
        $class = 'qtype_' . $qtype . '_test_helper';
233
        if (!class_exists($class)) {
234
            throw new coding_exception('Class ' . $class . ' is not defined in ' . $file);
235
        }
236
 
237
        self::$testhelpers[$qtype] = new $class();
238
        return self::$testhelpers[$qtype];
239
    }
240
 
241
    /**
242
     * Call a method on a qtype_{$qtype}_test_helper class and return the result.
243
     *
244
     * @param string $methodtemplate e.g. 'make_{qtype}_question_{which}';
245
     * @param string $qtype the question type to get a test question for.
246
     * @param string $which one of the names returned by the get_test_questions
247
     *      method of the relevant qtype_{$qtype}_test_helper class.
248
     * @param unknown_type $which
249
     */
250
    protected static function call_question_helper_method($methodtemplate, $qtype, $which = null) {
251
        $helper = self::get_test_helper($qtype);
252
 
253
        $available = $helper->get_test_questions();
254
 
255
        if (is_null($which)) {
256
            $which = reset($available);
257
        } else if (!in_array($which, $available)) {
258
            throw new coding_exception('Example question ' . $which . ' of type ' .
259
                $qtype . ' does not exist.');
260
        }
261
 
262
        $method = str_replace(array('{qtype}', '{which}'),
263
            array($qtype,    $which), $methodtemplate);
264
 
265
        if (!method_exists($helper, $method)) {
266
            throw new coding_exception('Method ' . $method . ' does not exist on the ' .
267
                $qtype . ' question type test helper class.');
268
        }
269
 
270
        return $helper->$method();
271
    }
272
 
273
    /**
274
     * Question types can provide a number of test question defintions.
275
     * They do this by creating a qtype_{$qtype}_test_helper class that extends
276
     * question_test_helper. The get_test_questions method returns the list of
277
     * test questions available for this question type.
278
     *
279
     * @param string $qtype the question type to get a test question for.
280
     * @param string $which one of the names returned by the get_test_questions
281
     *      method of the relevant qtype_{$qtype}_test_helper class.
282
     * @return question_definition the requested question object.
283
     */
284
    public static function make_question($qtype, $which = null) {
285
        return self::call_question_helper_method('make_{qtype}_question_{which}',
286
            $qtype, $which);
287
    }
288
 
289
    /**
290
     * Like {@link make_question()} but returns the datastructure from
291
     * get_question_options instead of the question_definition object.
292
     *
293
     * @param string $qtype the question type to get a test question for.
294
     * @param string $which one of the names returned by the get_test_questions
295
     *      method of the relevant qtype_{$qtype}_test_helper class.
296
     * @return stdClass the requested question object.
297
     */
298
    public static function get_question_data($qtype, $which = null) {
299
        return self::call_question_helper_method('get_{qtype}_question_data_{which}',
300
            $qtype, $which);
301
    }
302
 
303
    /**
304
     * Like {@link make_question()} but returns the data what would be saved from
305
     * the question editing form instead of the question_definition object.
306
     *
307
     * @param string $qtype the question type to get a test question for.
308
     * @param string $which one of the names returned by the get_test_questions
309
     *      method of the relevant qtype_{$qtype}_test_helper class.
310
     * @return stdClass the requested question object.
311
     */
312
    public static function get_question_form_data($qtype, $which = null) {
313
        return self::call_question_helper_method('get_{qtype}_question_form_data_{which}',
314
            $qtype, $which);
315
    }
316
 
317
    /**
318
     * Makes a multichoice question with choices 'A', 'B' and 'C' shuffled. 'A'
319
     * is correct, defaultmark 1.
320
     * @return qtype_multichoice_single_question
321
     */
322
    public static function make_a_multichoice_single_question() {
323
        question_bank::load_question_definition_classes('multichoice');
324
        $mc = new qtype_multichoice_single_question();
325
        self::initialise_a_question($mc);
326
        $mc->name = 'Multi-choice question, single response';
327
        $mc->questiontext = 'The answer is A.';
328
        $mc->generalfeedback = 'You should have selected A.';
329
        $mc->qtype = question_bank::get_qtype('multichoice');
330
 
331
        $mc->shuffleanswers = 1;
332
        $mc->answernumbering = 'abc';
333
        $mc->showstandardinstruction = 0;
334
 
335
        $mc->answers = array(
336
            13 => new question_answer(13, 'A', 1, 'A is right', FORMAT_HTML),
337
            14 => new question_answer(14, 'B', -0.3333333, 'B is wrong', FORMAT_HTML),
338
            15 => new question_answer(15, 'C', -0.3333333, 'C is wrong', FORMAT_HTML),
339
        );
340
 
341
        return $mc;
342
    }
343
 
344
    /**
345
     * Makes a multichoice question with choices 'A', 'B', 'C' and 'D' shuffled.
346
     * 'A' and 'C' is correct, defaultmark 1.
347
     * @return qtype_multichoice_multi_question
348
     */
349
    public static function make_a_multichoice_multi_question() {
350
        question_bank::load_question_definition_classes('multichoice');
351
        $mc = new qtype_multichoice_multi_question();
352
        self::initialise_a_question($mc);
353
        $mc->name = 'Multi-choice question, multiple response';
354
        $mc->questiontext = 'The answer is A and C.';
355
        $mc->generalfeedback = 'You should have selected A and C.';
356
        $mc->qtype = question_bank::get_qtype('multichoice');
357
 
358
        $mc->shuffleanswers = 1;
359
        $mc->answernumbering = 'abc';
360
        $mc->showstandardinstruction = 0;
361
 
362
        self::set_standard_combined_feedback_fields($mc);
363
 
364
        $mc->answers = array(
365
            13 => new question_answer(13, 'A', 0.5, 'A is part of the right answer', FORMAT_HTML),
366
            14 => new question_answer(14, 'B', -1, 'B is wrong', FORMAT_HTML),
367
            15 => new question_answer(15, 'C', 0.5, 'C is part of the right answer', FORMAT_HTML),
368
            16 => new question_answer(16, 'D', -1, 'D is wrong', FORMAT_HTML),
369
        );
370
 
371
        return $mc;
372
    }
373
 
374
    /**
375
     * Makes a matching question to classify 'Dog', 'Frog', 'Toad' and 'Cat' as
376
     * 'Mammal', 'Amphibian' or 'Insect'.
377
     * defaultmark 1. Stems are shuffled by default.
378
     * @return qtype_match_question
379
     */
380
    public static function make_a_matching_question() {
381
        return self::make_question('match');
382
    }
383
 
384
    /**
385
     * Makes a truefalse question with correct ansewer true, defaultmark 1.
386
     * @return qtype_essay_question
387
     */
388
    public static function make_an_essay_question() {
389
        question_bank::load_question_definition_classes('essay');
390
        $essay = new qtype_essay_question();
391
        self::initialise_a_question($essay);
392
        $essay->name = 'Essay question';
393
        $essay->questiontext = 'Write an essay.';
394
        $essay->generalfeedback = 'I hope you wrote an interesting essay.';
395
        $essay->penalty = 0;
396
        $essay->qtype = question_bank::get_qtype('essay');
397
 
398
        $essay->responseformat = 'editor';
399
        $essay->responserequired = 1;
400
        $essay->responsefieldlines = 15;
401
        $essay->attachments = 0;
402
        $essay->attachmentsrequired = 0;
403
        $essay->responsetemplate = '';
404
        $essay->responsetemplateformat = FORMAT_MOODLE;
405
        $essay->graderinfo = '';
406
        $essay->graderinfoformat = FORMAT_MOODLE;
407
 
408
        return $essay;
409
    }
410
 
411
    /**
412
     * Add some standard overall feedback to a question. You need to use these
413
     * specific feedback strings for the corresponding contains_..._feedback
414
     * methods in {@link qbehaviour_walkthrough_test_base} to works.
415
     * @param question_definition|stdClass $q the question to add the feedback to.
416
     */
417
    public static function set_standard_combined_feedback_fields($q) {
418
        $q->correctfeedback = self::STANDARD_OVERALL_CORRECT_FEEDBACK;
419
        $q->correctfeedbackformat = FORMAT_HTML;
420
        $q->partiallycorrectfeedback = self::STANDARD_OVERALL_PARTIALLYCORRECT_FEEDBACK;
421
        $q->partiallycorrectfeedbackformat = FORMAT_HTML;
422
        $q->shownumcorrect = true;
423
        $q->incorrectfeedback = self::STANDARD_OVERALL_INCORRECT_FEEDBACK;
424
        $q->incorrectfeedbackformat = FORMAT_HTML;
425
    }
426
 
427
    /**
428
     * Add some standard overall feedback to a question's form data.
429
     */
430
    public static function set_standard_combined_feedback_form_data($form) {
431
        $form->correctfeedback = array('text' => self::STANDARD_OVERALL_CORRECT_FEEDBACK,
432
                                    'format' => FORMAT_HTML);
433
        $form->partiallycorrectfeedback = array('text' => self::STANDARD_OVERALL_PARTIALLYCORRECT_FEEDBACK,
434
                                             'format' => FORMAT_HTML);
435
        $form->shownumcorrect = true;
436
        $form->incorrectfeedback = array('text' => self::STANDARD_OVERALL_INCORRECT_FEEDBACK,
437
                                    'format' => FORMAT_HTML);
438
    }
439
}
440
 
441
 
442
/**
443
 * Helper for tests that need to simulate records loaded from the database.
444
 *
445
 * @copyright  2009 The Open University
446
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
447
 */
448
abstract class testing_db_record_builder {
449
    public static function build_db_records(array $table) {
450
        $columns = array_shift($table);
451
        $records = array();
452
        foreach ($table as $row) {
453
            if (count($row) != count($columns)) {
454
                throw new coding_exception("Row contains the wrong number of fields.");
455
            }
456
            $rec = new stdClass();
457
            foreach ($columns as $i => $name) {
458
                $rec->$name = $row[$i];
459
            }
460
            $records[] = $rec;
461
        }
462
        return $records;
463
    }
464
}
465
 
466
 
467
/**
468
 * Helper base class for tests that need to simulate records loaded from the
469
 * database.
470
 *
471
 * @copyright  2009 The Open University
472
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
473
 */
474
abstract class data_loading_method_test_base extends advanced_testcase {
475
    public function build_db_records(array $table) {
476
        return testing_db_record_builder::build_db_records($table);
477
    }
478
}
479
 
480
 
481
abstract class question_testcase extends advanced_testcase {
482
 
483
    /**
484
     * Tolerance accepted in some unit tests when float operations are involved.
485
     */
486
    const GRADE_DELTA = 0.00000005;
487
 
488
    public function assert($expectation, $compare, $notused = '') {
489
 
490
        if (get_class($expectation) === 'question_pattern_expectation') {
491
            $this->assertMatchesRegularExpression($expectation->pattern, $compare,
492
                    'Expected regex ' . $expectation->pattern . ' not found in ' . $compare);
493
            return;
494
 
495
        } else if (get_class($expectation) === 'question_no_pattern_expectation') {
496
            $this->assertDoesNotMatchRegularExpression($expectation->pattern, $compare,
497
                    'Unexpected regex ' . $expectation->pattern . ' found in ' . $compare);
498
            return;
499
 
500
        } else if (get_class($expectation) === 'question_contains_tag_with_attributes') {
501
            $this->assertTag(array('tag'=>$expectation->tag, 'attributes'=>$expectation->expectedvalues), $compare,
502
                    'Looking for a ' . $expectation->tag . ' with attributes ' . html_writer::attributes($expectation->expectedvalues) . ' in ' . $compare);
503
            foreach ($expectation->forbiddenvalues as $k=>$v) {
504
                $attr = $expectation->expectedvalues;
505
                $attr[$k] = $v;
506
                $this->assertNotTag(array('tag'=>$expectation->tag, 'attributes'=>$attr), $compare,
507
                        $expectation->tag . ' had a ' . $k . ' attribute that should not be there in ' . $compare);
508
            }
509
            return;
510
 
511
        } else if (get_class($expectation) === 'question_contains_tag_with_attribute') {
512
            $attr = array($expectation->attribute=>$expectation->value);
513
            $this->assertTag(array('tag'=>$expectation->tag, 'attributes'=>$attr), $compare,
514
                    'Looking for a ' . $expectation->tag . ' with attribute ' . html_writer::attributes($attr) . ' in ' . $compare);
515
            return;
516
 
517
        } else if (get_class($expectation) === 'question_does_not_contain_tag_with_attributes') {
518
            $this->assertNotTag(array('tag'=>$expectation->tag, 'attributes'=>$expectation->attributes), $compare,
519
                    'Unexpected ' . $expectation->tag . ' with attributes ' . html_writer::attributes($expectation->attributes) . ' found in ' . $compare);
520
            return;
521
 
522
        } else if (get_class($expectation) === 'question_contains_select_expectation') {
523
            $tag = array('tag'=>'select', 'attributes'=>array('name'=>$expectation->name),
524
                'children'=>array('count'=>count($expectation->choices)));
525
            if ($expectation->enabled === false) {
526
                $tag['attributes']['disabled'] = 'disabled';
527
            } else if ($expectation->enabled === true) {
528
                // TODO
529
            }
530
            foreach(array_keys($expectation->choices) as $value) {
531
                if ($expectation->selected === $value) {
532
                    $tag['child'] = array('tag'=>'option', 'attributes'=>array('value'=>$value, 'selected'=>'selected'));
533
                } else {
534
                    $tag['child'] = array('tag'=>'option', 'attributes'=>array('value'=>$value));
535
                }
536
            }
537
 
538
            $this->assertTag($tag, $compare, 'expected select not found in ' . $compare);
539
            return;
540
 
541
        } else if (get_class($expectation) === 'question_check_specified_fields_expectation') {
542
            $expect = (array)$expectation->expect;
543
            $compare = (array)$compare;
544
            foreach ($expect as $k=>$v) {
545
                if (!array_key_exists($k, $compare)) {
546
                    $this->fail("Property {$k} does not exist");
547
                }
548
                if ($v != $compare[$k]) {
549
                    $this->fail("Property {$k} is different");
550
                }
551
            }
552
            $this->assertTrue(true);
553
            return;
554
 
555
        } else if (get_class($expectation) === 'question_contains_tag_with_contents') {
556
            $this->assertTag(array('tag'=>$expectation->tag, 'content'=>$expectation->content), $compare,
557
                    'Looking for a ' . $expectation->tag . ' with content ' . $expectation->content . ' in ' . $compare);
558
            return;
559
        }
560
 
561
        throw new coding_exception('Unknown expectiontion:'.get_class($expectation));
562
    }
563
 
564
    /**
565
     * Use this function rather than assert when checking the value of options within a select element.
566
     *
567
     * @param question_contains_select_expectation $expectation The select expectation class
568
     * @param string $html The rendered output to check against
569
     */
570
    public function assert_select_options($expectation, $html) {
571
        if (get_class($expectation) !== 'question_contains_select_expectation') {
572
            throw new coding_exception('Unsuitable expectiontion: '.get_class($expectation));
573
        }
574
        $dom = new DOMDocument();
575
        $dom->loadHTML($html);
576
        $selects = $dom->getElementsByTagName('select');
577
        foreach ($selects as $select) {
578
            if ($select->getAttribute('name') == $expectation->name) {
579
                $options = $select->getElementsByTagName('option');
580
                foreach ($options as $key => $option) {
581
                    if ($key == 0) {
582
                        // Check the value of the first option. This is often 'Choose...' or a nbsp.
583
                        // Note it is necessary to pass a nbsp character in the test here and not just ' '.
584
                        // Many tests do not require checking of this option.
585
                        if (isset($expectation->choices[$option->getAttribute('value')])) {
586
                            $this->assertEquals($expectation->choices[$option->getAttribute('value')], $option->textContent);
587
                        }
588
                        continue;
589
                    }
590
                    // Check the value of the options in the select.
591
                    $this->assertEquals($expectation->choices[$option->getAttribute('value')], $option->textContent);
592
                    if ($expectation->selected && $option->getAttribute('value') == $expectation->selected) {
593
                        // Check the right option is selected.
594
                        $this->assertTrue(!empty($option->getAttribute('selected')));
595
                    }
596
                }
597
                if ($expectation->enabled) {
598
                    // Check the select element is enabled.
599
                    $this->assertTrue(!$select->getAttribute('disabled'));
600
                }
601
            }
602
        }
603
        return;
604
    }
605
 
606
    /**
607
     * Check that 2 XML strings are the same, ignoring differences in line endings.
608
     *
609
     * @param string $expectedxml The expected XML string
610
     * @param string $xml The XML string to check
611
     */
612
    public function assert_same_xml($expectedxml, $xml) {
613
        $this->assertEquals(
614
            phpunit_util::normalise_line_endings($expectedxml),
615
            phpunit_util::normalise_line_endings($xml)
616
        );
617
    }
618
}
619
 
620
 
621
class question_contains_tag_with_contents {
622
    public $tag;
623
    public $content;
624
    public $message;
625
 
626
    public function __construct($tag, $content, $message = '') {
627
        $this->tag = $tag;
628
        $this->content = $content;
629
        $this->message = $message;
630
    }
631
 
632
}
633
 
634
class question_check_specified_fields_expectation {
635
    public $expect;
636
    public $message;
637
 
638
    function __construct($expected, $message = '') {
639
        $this->expect = $expected;
640
        $this->message = $message;
641
    }
642
}
643
 
644
 
645
class question_contains_select_expectation {
646
    public $name;
647
    public $choices;
648
    public $selected;
649
    public $enabled;
650
    public $message;
651
 
652
    public function __construct($name, $choices, $selected = null, $enabled = null, $message = '') {
653
        $this->name = $name;
654
        $this->choices = $choices;
655
        $this->selected = $selected;
656
        $this->enabled = $enabled;
657
        $this->message = $message;
658
    }
659
}
660
 
661
 
662
class question_does_not_contain_tag_with_attributes {
663
    public $tag;
664
    public $attributes;
665
    public $message;
666
 
667
    public function __construct($tag, $attributes, $message = '') {
668
        $this->tag = $tag;
669
        $this->attributes = $attributes;
670
        $this->message = $message;
671
    }
672
}
673
 
674
 
675
class question_contains_tag_with_attribute {
676
    public $tag;
677
    public $attribute;
678
    public $value;
679
    public $message;
680
 
681
    public function __construct($tag, $attribute, $value, $message = '') {
682
        $this->tag = $tag;
683
        $this->attribute = $attribute;
684
        $this->value = $value;
685
        $this->message = $message;
686
    }
687
}
688
 
689
 
690
class question_contains_tag_with_attributes {
691
    public $tag;
692
    public $expectedvalues = array();
693
    public $forbiddenvalues = array();
694
    public $message;
695
 
696
    public function __construct($tag, $expectedvalues, $forbiddenvalues=array(), $message = '') {
697
        $this->tag = $tag;
698
        $this->expectedvalues = $expectedvalues;
699
        $this->forbiddenvalues = $forbiddenvalues;
700
        $this->message = $message;
701
    }
702
}
703
 
704
 
705
class question_pattern_expectation {
706
    public $pattern;
707
    public $message;
708
 
709
    public function __construct($pattern, $message = '') {
710
        $this->pattern = $pattern;
711
        $this->message = $message;
712
    }
713
}
714
 
715
 
716
class question_no_pattern_expectation {
717
    public $pattern;
718
    public $message;
719
 
720
    public function __construct($pattern, $message = '') {
721
        $this->pattern = $pattern;
722
        $this->message = $message;
723
    }
724
}
725
 
726
 
727
/**
728
 * Helper base class for question walk-through tests.
729
 *
730
 * The purpose of tests that use this base class is to simulate the entire
731
 * interaction of a student making an attempt at a question. Therefore,
732
 * these are not really unit tests. They would more accurately be described
733
 * as integration tests. However, whether they are unit tests or not,
734
 * it works well to implement them in PHPUnit.
735
 *
736
 * Historically, tests like this were made because Moodle did not have anything
737
 * like Behat for end-to-end testing. Even though we do now have Behat, it makes
738
 * sense to keep these walk-through tests. They run massively faster than Behat
739
 * tests, which gives you a much faster feedback loop while doing development.
740
 * They also make it quite easy to test things like regrading the attempt after
741
 * the question has been edited, which would be possible but very fiddly in Behat.
742
 *
743
 * Ideally, the full set of tests for the question class of a question type would be:
744
 *
745
 * 1. A lot of unit tests for each qtype_myqtype_question class method
746
 *    like grade_response, is_complete_response, is_same_response, ...
747
 *
748
 * 2. Several of these walk-through tests, to test the end-to-end interaction
749
 *    of a student with a question, for example with different behaviours.
750
 *
751
 * 3. Just one Behat test, using question preview, to verify that everything
752
 *    is plugged together correctly and works when used through the UI.
753
 *
754
 * What one would expect to see in one of these walk-through tests is:
755
 *
756
 * // 1. Set up a question: $q.
757
 *
758
 * // 2. A call to $this->start_attempt_at_question($q, ...); with the relevant options.
759
 *
760
 * // 3. Some number of calls to $this->process_submission passing an array of simulated
761
 * //    POST data that matches what would be sent back be submitting a form that contains
762
 * //    the form fields that are output by rendering the question. This is like clicking
763
 * //    the 'Check' button in a question, or navigating to the next page in a quiz.
764
 *
765
 * // 4. A call to $this->finish(); which is the equivalent of clicking
766
 * //    'Submit all and finish' in the quiz.
767
 *
768
 * // 5. After each of steps 2-4 above, one would expect to see a certain amount of
769
 * //    validation of the state of the question and how the question is rendered,
770
 * //    using methods like $this->check_current_state(), $this->check_current_output, etc.
771
 *
772
 * The best way to work out how to write tests like this is probably to look at
773
 * some examples in other question types or question behaviours.
774
 *
775
 * In writing these tests, it is worth noting the following points:
776
 *
777
 * a) The easiest mistake to make is at step 3. You need to ensure that your
778
 *    simulated post data actually matches what gets sent back when the
779
 *    question is submitted in the browser. Try checking it against the
780
 *    HTTP POST requests you see in your browser when the question is submitted.
781
 *    Some question types have a $q->prepare_simulated_post_data() method that
782
 *    can help with this.
783
 *
784
 * b) In the past, tests like these used to contain even more repetitive code,
785
 *    and so they were re-factored to add the helper methods like
786
 *    start_attempt_at_question, process_submission, finish. That change had
787
 *    good effects, like reducing duplicate code. However, there were down-sides.
788
 *    The extra layers of indirection hide what is going on, which means these
789
 *    tests are harder to understand until you know what the helpers are doing.
790
 *    If you want an interesting exercise, take one of the walk-through tests,
791
 *    and inline all the helpers. This might be a good way to understand more about
792
 *    the question engine API. However, having made the everything-inlined code
793
 *    and learned from the process, you should then just throw it away.
794
 *
795
 * c) The way check_current_output works is weird. When these tests were first written
796
 *    Moodle used SimpleTest for unit tests and check_current_output os written in a
797
 *    style that made sense there. When we moved to PHPUnit, a quick and dirty
798
 *    conversion was done. That was a pragmatic move at the time, and we just have
799
 *    to live with the result. Sorry. (And: don't copy that style for new things.)
800
 *
801
 * @copyright  2009 The Open University
802
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
803
 */
804
abstract class qbehaviour_walkthrough_test_base extends question_testcase {
805
    /** @var question_display_options */
806
    protected $displayoptions;
807
 
808
    /** @var question_usage_by_activity */
809
    protected $quba;
810
 
811
    /** @var int The slot number of the question_attempt we are using in $quba. */
812
    protected $slot;
813
 
814
    /**
815
     * @var string after {@link render()} has been called, this contains the
816
     * display of the question in its current state.
817
     */
818
    protected $currentoutput = '';
819
 
820
    protected function setUp(): void {
821
        parent::setUp();
822
        $this->resetAfterTest();
823
        $this->setAdminUser();
824
 
825
        $this->displayoptions = new question_display_options();
826
        $this->quba = question_engine::make_questions_usage_by_activity('unit_test',
827
            context_system::instance());
828
    }
829
 
830
    protected function tearDown(): void {
831
        $this->displayoptions = null;
832
        $this->quba = null;
833
        parent::tearDown();
834
    }
835
 
836
    protected function start_attempt_at_question($question, $preferredbehaviour,
837
                                                 $maxmark = null, $variant = 1) {
838
        $this->quba->set_preferred_behaviour($preferredbehaviour);
839
        $this->slot = $this->quba->add_question($question, $maxmark);
840
        $this->quba->start_question($this->slot, $variant);
841
    }
842
 
843
    /**
844
     * Convert an array of data destined for one question to the equivalent POST data.
845
     * @param array $data the data for the quetsion.
846
     * @return array the complete post data.
847
     */
848
    protected function response_data_to_post($data) {
849
        $prefix = $this->quba->get_field_prefix($this->slot);
850
        $fulldata = array(
851
            'slots' => $this->slot,
852
            $prefix . ':sequencecheck' => $this->get_question_attempt()->get_sequence_check_count(),
853
        );
854
        foreach ($data as $name => $value) {
855
            $fulldata[$prefix . $name] = $value;
856
        }
857
        return $fulldata;
858
    }
859
 
860
    protected function process_submission($data) {
861
        // Backwards compatibility.
862
        reset($data);
863
        if (count($data) == 1 && key($data) === '-finish') {
864
            $this->finish();
865
        }
866
 
867
        $this->quba->process_all_actions(time(), $this->response_data_to_post($data));
868
    }
869
 
870
    protected function process_autosave($data) {
871
        $this->quba->process_all_autosaves(null, $this->response_data_to_post($data));
872
    }
873
 
874
    protected function finish() {
875
        $this->quba->finish_all_questions();
876
    }
877
 
878
    protected function manual_grade($comment, $mark, $commentformat = null) {
879
        $this->quba->manual_grade($this->slot, $comment, $mark, $commentformat);
880
    }
881
 
882
    protected function save_quba(moodle_database $db = null) {
883
        question_engine::save_questions_usage_by_activity($this->quba, $db);
884
    }
885
 
886
    protected function load_quba(moodle_database $db = null) {
887
        $this->quba = question_engine::load_questions_usage_by_activity($this->quba->get_id(), $db);
888
    }
889
 
890
    protected function delete_quba() {
891
        question_engine::delete_questions_usage_by_activity($this->quba->get_id());
892
        $this->quba = null;
893
    }
894
 
895
    /**
896
     * Asserts if the manual comment for the question is equal to the provided arguments.
897
     * @param $comment Comment text
898
     * @param $commentformat Comment format
899
     */
900
    protected function check_comment($comment, $commentformat) {
901
        $actualcomment = $this->quba->get_question_attempt($this->slot)->get_manual_comment();
902
 
903
        $this->assertEquals(
904
                [$comment, $commentformat],
905
                [$actualcomment[0], $actualcomment[1]]
906
        );
907
    }
908
 
909
    protected function check_current_state($state) {
910
        $this->assertEquals($state, $this->quba->get_question_state($this->slot),
911
            'Questions is in the wrong state.');
912
    }
913
 
914
    protected function check_current_mark($mark) {
915
        if (is_null($mark)) {
916
            $this->assertNull($this->quba->get_question_mark($this->slot));
917
        } else {
918
            if ($mark == 0) {
919
                // PHP will think a null mark and a mark of 0 are equal,
920
                // so explicity check not null in this case.
921
                $this->assertNotNull($this->quba->get_question_mark($this->slot));
922
            }
923
            $this->assertEqualsWithDelta($mark, $this->quba->get_question_mark($this->slot),
924
                 0.000001, 'Expected mark and actual mark differ.');
925
        }
926
    }
927
 
928
    /**
929
     * Generate the HTML rendering of the question in its current state in
930
     * $this->currentoutput so that it can be verified.
931
     */
932
    protected function render() {
933
        $this->quba->preload_all_step_users();
934
        $this->currentoutput = $this->quba->render_question($this->slot, $this->displayoptions);
935
    }
936
 
937
    protected function check_output_contains_text_input($name, $value = null, $enabled = true) {
938
        $attributes = array(
939
            'type' => 'text',
940
            'name' => $this->quba->get_field_prefix($this->slot) . $name,
941
        );
942
        if (!is_null($value)) {
943
            $attributes['value'] = $value;
944
        }
945
        if (!$enabled) {
946
            $attributes['readonly'] = 'readonly';
947
        }
948
        $matcher = $this->get_tag_matcher('input', $attributes);
949
        $this->assertTag($matcher, $this->currentoutput,
950
                'Looking for an input with attributes ' . html_writer::attributes($attributes) . ' in ' . $this->currentoutput);
951
 
952
        if ($enabled) {
953
            $matcher['attributes']['readonly'] = 'readonly';
954
            $this->assertNotTag($matcher, $this->currentoutput,
955
                    'input with attributes ' . html_writer::attributes($attributes) .
956
                    ' should not be read-only in ' . $this->currentoutput);
957
        }
958
    }
959
 
960
    protected function check_output_contains_text_input_with_class($name, $class = null) {
961
        $attributes = array(
962
            'type' => 'text',
963
            'name' => $this->quba->get_field_prefix($this->slot) . $name,
964
        );
965
        if (!is_null($class)) {
966
            $attributes['class'] = 'regexp:/\b' . $class . '\b/';
967
        }
968
 
969
        $matcher = $this->get_tag_matcher('input', $attributes);
970
        $this->assertTag($matcher, $this->currentoutput,
971
                'Looking for an input with attributes ' . html_writer::attributes($attributes) . ' in ' . $this->currentoutput);
972
    }
973
 
974
    protected function check_output_does_not_contain_text_input_with_class($name, $class = null) {
975
        $attributes = array(
976
            'type' => 'text',
977
            'name' => $this->quba->get_field_prefix($this->slot) . $name,
978
        );
979
        if (!is_null($class)) {
980
            $attributes['class'] = 'regexp:/\b' . $class . '\b/';
981
        }
982
 
983
        $matcher = $this->get_tag_matcher('input', $attributes);
984
        $this->assertNotTag($matcher, $this->currentoutput,
985
                'Unexpected input with attributes ' . html_writer::attributes($attributes) . ' found in ' . $this->currentoutput);
986
    }
987
 
988
    protected function check_output_contains_hidden_input($name, $value) {
989
        $attributes = array(
990
            'type' => 'hidden',
991
            'name' => $this->quba->get_field_prefix($this->slot) . $name,
992
            'value' => $value,
993
        );
994
        $this->assertTag($this->get_tag_matcher('input', $attributes), $this->currentoutput,
995
                'Looking for a hidden input with attributes ' . html_writer::attributes($attributes) . ' in ' . $this->currentoutput);
996
    }
997
 
998
    protected function check_output_contains($string) {
999
        $this->render();
1000
        $this->assertStringContainsString($string, $this->currentoutput,
1001
                'Expected string ' . $string . ' not found in ' . $this->currentoutput);
1002
    }
1003
 
1004
    protected function check_output_does_not_contain($string) {
1005
        $this->render();
1006
        $this->assertStringNotContainsString($string, $this->currentoutput,
1007
                'String ' . $string . ' unexpectedly found in ' . $this->currentoutput);
1008
    }
1009
 
1010
    protected function check_output_contains_lang_string($identifier, $component = '', $a = null) {
1011
        $this->check_output_contains(get_string($identifier, $component, $a));
1012
    }
1013
 
1014
    protected function get_tag_matcher($tag, $attributes) {
1015
        return array(
1016
            'tag' => $tag,
1017
            'attributes' => $attributes,
1018
        );
1019
    }
1020
 
1021
    /**
1022
     * @param $condition one or more Expectations. (users varargs).
1023
     */
1024
    protected function check_current_output() {
1025
        $this->render();
1026
        foreach (func_get_args() as $condition) {
1027
            $this->assert($condition, $this->currentoutput);
1028
        }
1029
    }
1030
 
1031
    /**
1032
     * Use this function rather than check_current_output for select expectations where
1033
     * checking the value of the options is required. check_current_output only checks
1034
     * that the right number of options are available.
1035
     *
1036
     * @param question_contains_select_expectation $expectations One or more expectations.
1037
     */
1038
    protected function check_output_contains_selectoptions(...$expectations) {
1039
        $this->render();
1040
        foreach ($expectations as $expectation) {
1041
            $this->assert_select_options($expectation, $this->currentoutput);
1042
        }
1043
    }
1044
 
1045
    protected function get_question_attempt() {
1046
        return $this->quba->get_question_attempt($this->slot);
1047
    }
1048
 
1049
    protected function get_step_count() {
1050
        return $this->get_question_attempt()->get_num_steps();
1051
    }
1052
 
1053
    protected function check_step_count($expectednumsteps) {
1054
        $this->assertEquals($expectednumsteps, $this->get_step_count());
1055
    }
1056
 
1057
    protected function get_step($stepnum) {
1058
        return $this->get_question_attempt()->get_step($stepnum);
1059
    }
1060
 
1061
    protected function get_contains_question_text_expectation($question) {
1062
        return new question_pattern_expectation('/' . preg_quote($question->questiontext, '/') . '/');
1063
    }
1064
 
1065
    protected function get_contains_general_feedback_expectation($question) {
1066
        return new question_pattern_expectation('/' . preg_quote($question->generalfeedback, '/') . '/');
1067
    }
1068
 
1069
    protected function get_does_not_contain_correctness_expectation() {
1070
        return new question_no_pattern_expectation('/class=\"correctness/');
1071
    }
1072
 
1073
    protected function get_contains_correct_expectation() {
1074
        return new question_pattern_expectation('/' . preg_quote(get_string('correct', 'question'), '/') . '/');
1075
    }
1076
 
1077
    protected function get_contains_partcorrect_expectation() {
1078
        return new question_pattern_expectation('/' .
1079
            preg_quote(get_string('partiallycorrect', 'question'), '/') . '/');
1080
    }
1081
 
1082
    protected function get_contains_incorrect_expectation() {
1083
        return new question_pattern_expectation('/' . preg_quote(get_string('incorrect', 'question'), '/') . '/');
1084
    }
1085
 
1086
    protected function get_contains_standard_correct_combined_feedback_expectation() {
1087
        return new question_pattern_expectation('/' .
1088
            preg_quote(test_question_maker::STANDARD_OVERALL_CORRECT_FEEDBACK, '/') . '/');
1089
    }
1090
 
1091
    protected function get_contains_standard_partiallycorrect_combined_feedback_expectation() {
1092
        return new question_pattern_expectation('/' .
1093
            preg_quote(test_question_maker::STANDARD_OVERALL_PARTIALLYCORRECT_FEEDBACK, '/') . '/');
1094
    }
1095
 
1096
    protected function get_contains_standard_incorrect_combined_feedback_expectation() {
1097
        return new question_pattern_expectation('/' .
1098
            preg_quote(test_question_maker::STANDARD_OVERALL_INCORRECT_FEEDBACK, '/') . '/');
1099
    }
1100
 
1101
    protected function get_does_not_contain_feedback_expectation() {
1102
        return new question_no_pattern_expectation('/class="feedback"/');
1103
    }
1104
 
1105
    protected function get_does_not_contain_num_parts_correct() {
1106
        return new question_no_pattern_expectation('/class="numpartscorrect"/');
1107
    }
1108
 
1109
    protected function get_contains_num_parts_correct($num) {
1110
        $a = new stdClass();
1111
        $a->num = $num;
1112
        return new question_pattern_expectation('/<div class="numpartscorrect">' .
1113
            preg_quote(get_string('yougotnright', 'question', $a), '/') . '/');
1114
    }
1115
 
1116
    protected function get_does_not_contain_specific_feedback_expectation() {
1117
        return new question_no_pattern_expectation('/class="specificfeedback"/');
1118
    }
1119
 
1120
    protected function get_contains_validation_error_expectation() {
1121
        return new question_contains_tag_with_attribute('div', 'class', 'validationerror');
1122
    }
1123
 
1124
    protected function get_does_not_contain_validation_error_expectation() {
1125
        return new question_no_pattern_expectation('/class="validationerror"/');
1126
    }
1127
 
1128
    protected function get_contains_mark_summary($mark) {
1129
        $a = new stdClass();
1130
        $a->mark = format_float($mark, $this->displayoptions->markdp);
1131
        $a->max = format_float($this->quba->get_question_max_mark($this->slot),
1132
            $this->displayoptions->markdp);
1133
        return new question_pattern_expectation('/' .
1134
            preg_quote(get_string('markoutofmax', 'question', $a), '/') . '/');
1135
    }
1136
 
1137
    protected function get_contains_marked_out_of_summary() {
1138
        $max = format_float($this->quba->get_question_max_mark($this->slot),
1139
            $this->displayoptions->markdp);
1140
        return new question_pattern_expectation('/' .
1141
            preg_quote(get_string('markedoutofmax', 'question', $max), '/') . '/');
1142
    }
1143
 
1144
    protected function get_does_not_contain_mark_summary() {
1145
        return new question_no_pattern_expectation('/<div class="grade">/');
1146
    }
1147
 
1148
    protected function get_contains_checkbox_expectation($baseattr, $enabled, $checked) {
1149
        $expectedattributes = $baseattr;
1150
        $forbiddenattributes = array();
1151
        $expectedattributes['type'] = 'checkbox';
1152
        if ($enabled === true) {
1153
            $forbiddenattributes['disabled'] = 'disabled';
1154
        } else if ($enabled === false) {
1155
            $expectedattributes['disabled'] = 'disabled';
1156
        }
1157
        if ($checked === true) {
1158
            $expectedattributes['checked'] = 'checked';
1159
        } else if ($checked === false) {
1160
            $forbiddenattributes['checked'] = 'checked';
1161
        }
1162
        return new question_contains_tag_with_attributes('input', $expectedattributes, $forbiddenattributes);
1163
    }
1164
 
1165
    protected function get_contains_mc_checkbox_expectation($index, $enabled = null,
1166
                                                            $checked = null) {
1167
        return $this->get_contains_checkbox_expectation(array(
1168
            'name' => $this->quba->get_field_prefix($this->slot) . $index,
1169
            'value' => 1,
1170
        ), $enabled, $checked);
1171
    }
1172
 
1173
    protected function get_contains_radio_expectation($baseattr, $enabled, $checked) {
1174
        $expectedattributes = $baseattr;
1175
        $forbiddenattributes = array();
1176
        $expectedattributes['type'] = 'radio';
1177
        if ($enabled === true) {
1178
            $forbiddenattributes['disabled'] = 'disabled';
1179
        } else if ($enabled === false) {
1180
            $expectedattributes['disabled'] = 'disabled';
1181
        }
1182
        if ($checked === true) {
1183
            $expectedattributes['checked'] = 'checked';
1184
        } else if ($checked === false) {
1185
            $forbiddenattributes['checked'] = 'checked';
1186
        }
1187
        return new question_contains_tag_with_attributes('input', $expectedattributes, $forbiddenattributes);
1188
    }
1189
 
1190
    protected function get_contains_mc_radio_expectation($index, $enabled = null, $checked = null) {
1191
        return $this->get_contains_radio_expectation(array(
1192
            'name' => $this->quba->get_field_prefix($this->slot) . 'answer',
1193
            'value' => $index,
1194
        ), $enabled, $checked);
1195
    }
1196
 
1197
    protected function get_contains_hidden_expectation($name, $value = null) {
1198
        $expectedattributes = array('type' => 'hidden', 'name' => s($name));
1199
        if (!is_null($value)) {
1200
            $expectedattributes['value'] = s($value);
1201
        }
1202
        return new question_contains_tag_with_attributes('input', $expectedattributes);
1203
    }
1204
 
1205
    protected function get_does_not_contain_hidden_expectation($name, $value = null) {
1206
        $expectedattributes = array('type' => 'hidden', 'name' => s($name));
1207
        if (!is_null($value)) {
1208
            $expectedattributes['value'] = s($value);
1209
        }
1210
        return new question_does_not_contain_tag_with_attributes('input', $expectedattributes);
1211
    }
1212
 
1213
    protected function get_contains_tf_true_radio_expectation($enabled = null, $checked = null) {
1214
        return $this->get_contains_radio_expectation(array(
1215
            'name' => $this->quba->get_field_prefix($this->slot) . 'answer',
1216
            'value' => 1,
1217
        ), $enabled, $checked);
1218
    }
1219
 
1220
    protected function get_contains_tf_false_radio_expectation($enabled = null, $checked = null) {
1221
        return $this->get_contains_radio_expectation(array(
1222
            'name' => $this->quba->get_field_prefix($this->slot) . 'answer',
1223
            'value' => 0,
1224
        ), $enabled, $checked);
1225
    }
1226
 
1227
    protected function get_contains_cbm_radio_expectation($certainty, $enabled = null,
1228
                                                          $checked = null) {
1229
        return $this->get_contains_radio_expectation(array(
1230
            'name' => $this->quba->get_field_prefix($this->slot) . '-certainty',
1231
            'value' => $certainty,
1232
        ), $enabled, $checked);
1233
    }
1234
 
1235
    protected function get_contains_button_expectation($name, $value = null, $enabled = null) {
1236
        $expectedattributes = array(
1237
            'type' => 'submit',
1238
            'name' => $name,
1239
        );
1240
        $forbiddenattributes = array();
1241
        if (!is_null($value)) {
1242
            $expectedattributes['value'] = $value;
1243
        }
1244
        if ($enabled === true) {
1245
            $forbiddenattributes['disabled'] = 'disabled';
1246
        } else if ($enabled === false) {
1247
            $expectedattributes['disabled'] = 'disabled';
1248
        }
1249
        return new question_contains_tag_with_attributes('button', $expectedattributes, $forbiddenattributes);
1250
    }
1251
 
1252
    /**
1253
     * Returns an epectation that a string contains the HTML of a button with
1254
     * name {question-attempt prefix}-submit, and eiter enabled or not.
1255
     * @param bool $enabled if not null, check the enabled/disabled state of the button. True = enabled.
1256
     * @return question_contains_tag_with_attributes an expectation for use with check_current_output.
1257
     */
1258
    protected function get_contains_submit_button_expectation($enabled = null) {
1259
        return $this->get_contains_button_expectation(
1260
            $this->quba->get_field_prefix($this->slot) . '-submit', null, $enabled);
1261
    }
1262
 
1263
    /**
1264
     * Returns an epectation that a string does not contain the HTML of a button with
1265
     * name {question-attempt prefix}-submit.
1266
     * @return question_contains_tag_with_attributes an expectation for use with check_current_output.
1267
     */
1268
    protected function get_does_not_contain_submit_button_expectation() {
1269
        return new question_no_pattern_expectation('/name="' .
1270
                $this->quba->get_field_prefix($this->slot) . '-submit"/');
1271
    }
1272
 
1273
    protected function get_tries_remaining_expectation($n) {
1274
        return new question_pattern_expectation('/' .
1275
            preg_quote(get_string('triesremaining', 'qbehaviour_interactive', $n), '/') . '/');
1276
    }
1277
 
1278
    protected function get_invalid_answer_expectation() {
1279
        return new question_pattern_expectation('/' .
1280
            preg_quote(get_string('invalidanswer', 'question'), '/') . '/');
1281
    }
1282
 
1283
    protected function get_contains_try_again_button_expectation($enabled = null) {
1284
        $expectedattributes = array(
1285
            'type' => 'submit',
1286
            'name' => $this->quba->get_field_prefix($this->slot) . '-tryagain',
1287
        );
1288
        $forbiddenattributes = array();
1289
        if ($enabled === true) {
1290
            $forbiddenattributes['disabled'] = 'disabled';
1291
        } else if ($enabled === false) {
1292
            $expectedattributes['disabled'] = 'disabled';
1293
        }
1294
        return new question_contains_tag_with_attributes('input', $expectedattributes, $forbiddenattributes);
1295
    }
1296
 
1297
    protected function get_does_not_contain_try_again_button_expectation() {
1298
        return new question_no_pattern_expectation('/name="' .
1299
            $this->quba->get_field_prefix($this->slot) . '-tryagain"/');
1300
    }
1301
 
1302
    protected function get_contains_select_expectation($name, $choices,
1303
                                                       $selected = null, $enabled = null) {
1304
        $fullname = $this->quba->get_field_prefix($this->slot) . $name;
1305
        return new question_contains_select_expectation($fullname, $choices, $selected, $enabled);
1306
    }
1307
 
1308
    protected function get_mc_right_answer_index($mc) {
1309
        $order = $mc->get_order($this->get_question_attempt());
1310
        foreach ($order as $i => $ansid) {
1311
            if ($mc->answers[$ansid]->fraction == 1) {
1312
                return $i;
1313
            }
1314
        }
1315
        $this->fail('This multiple choice question does not seem to have a right answer!');
1316
    }
1317
 
1318
    protected function get_no_hint_visible_expectation() {
1319
        return new question_no_pattern_expectation('/class="hint"/');
1320
    }
1321
 
1322
    protected function get_contains_hint_expectation($hinttext) {
1323
        // Does not currently verify hint text.
1324
        return new question_contains_tag_with_attribute('div', 'class', 'hint');
1325
    }
1326
 
1327
    /**
1328
     * Returns an expectation that a string contains a corrupted question notification.
1329
     *
1330
     * @return question_pattern_expectation an expectation for use with check_current_output.
1331
     */
1332
    protected function get_contains_corruption_notification() {
1333
        return new question_pattern_expectation('/' . preg_quote(get_string('corruptedquestion', 'qtype_multianswer'), '/') . '/');
1334
    }
1335
 
1336
    /**
1337
     * Returns an expectation that a string contains a corrupted subquestion message.
1338
     *
1339
     * @return question_pattern_expectation an expectation for use with check_current_output.
1340
     */
1341
    protected function get_contains_corrupted_subquestion_message() {
1342
        return new question_pattern_expectation('/' . preg_quote(get_string('missingsubquestion', 'qtype_multianswer'), '/') . '/');
1343
    }
1344
}
1345
 
1346
/**
1347
 * Simple class that implements the {@link moodle_recordset} API based on an
1348
 * array of test data.
1349
 *
1350
 *  See the {@link question_attempt_step_db_test} class in
1351
 *  question/engine/tests/testquestionattemptstep.php for an example of how
1352
 *  this is used.
1353
 *
1354
 * @copyright  2011 The Open University
1355
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1356
 */
1357
class question_test_recordset extends moodle_recordset {
1358
    protected $records;
1359
 
1360
    /**
1361
     * Constructor
1362
     * @param $table as for {@link testing_db_record_builder::build_db_records()}
1363
     *      but does not need a unique first column.
1364
     */
1365
    public function __construct(array $table) {
1366
        $columns = array_shift($table);
1367
        $this->records = array();
1368
        foreach ($table as $row) {
1369
            if (count($row) != count($columns)) {
1370
                throw new coding_exception("Row contains the wrong number of fields.");
1371
            }
1372
            $rec = array();
1373
            foreach ($columns as $i => $name) {
1374
                $rec[$name] = $row[$i];
1375
            }
1376
            $this->records[] = $rec;
1377
        }
1378
        reset($this->records);
1379
    }
1380
 
1381
    public function __destruct() {
1382
        $this->close();
1383
    }
1384
 
1385
    #[\ReturnTypeWillChange]
1386
    public function current() {
1387
        return (object) current($this->records);
1388
    }
1389
 
1390
    #[\ReturnTypeWillChange]
1391
    public function key() {
1392
        if (is_null(key($this->records))) {
1393
            return false;
1394
        }
1395
        $current = current($this->records);
1396
        return reset($current);
1397
    }
1398
 
1399
    public function next(): void {
1400
        next($this->records);
1401
    }
1402
 
1403
    public function valid(): bool {
1404
        return !is_null(key($this->records));
1405
    }
1406
 
1407
    public function close() {
1408
        $this->records = null;
1409
    }
1410
}
1411
 
1412
/**
1413
 * Provide utility function for random question test
1414
 *
1415
 * @package   core_question
1416
 * @author     Nathan Nguyen <nathannguyen@catalyst-au.net>
1417
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1418
 */
1419
class question_filter_test_helper {
1420
    /**
1421
     * Create filters base on provided values
1422
     *
1423
     * @param array $categoryids question category filter
1424
     * @param bool $recursive subcategories filter
1425
     * @param array $qtagids tags filter
1426
     * @return array
1427
     */
1428
    public static function create_filters(array $categoryids, bool $recursive = false, array $qtagids = []): array {
1429
        $filters = [
1430
            'category' => [
1431
                'jointype' => \qbank_managecategories\category_condition::JOINTYPE_DEFAULT,
1432
                'values' => $categoryids,
1433
                'filteroptions' => ['includesubcategories' => $recursive],
1434
            ],
1435
            'qtagids' => [
1436
                'jointype' => \qbank_tagquestion\tag_condition::JOINTYPE_DEFAULT,
1437
                'values' => $qtagids,
1438
            ],
1439
        ];
1440
        return $filters;
1441
    }
1442
}