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