Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   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  
 608  class question_contains_tag_with_contents {
 609      public $tag;
 610      public $content;
 611      public $message;
 612  
 613      public function __construct($tag, $content, $message = '') {
 614          $this->tag = $tag;
 615          $this->content = $content;
 616          $this->message = $message;
 617      }
 618  
 619  }
 620  
 621  class question_check_specified_fields_expectation {
 622      public $expect;
 623      public $message;
 624  
 625      function __construct($expected, $message = '') {
 626          $this->expect = $expected;
 627          $this->message = $message;
 628      }
 629  }
 630  
 631  
 632  class question_contains_select_expectation {
 633      public $name;
 634      public $choices;
 635      public $selected;
 636      public $enabled;
 637      public $message;
 638  
 639      public function __construct($name, $choices, $selected = null, $enabled = null, $message = '') {
 640          $this->name = $name;
 641          $this->choices = $choices;
 642          $this->selected = $selected;
 643          $this->enabled = $enabled;
 644          $this->message = $message;
 645      }
 646  }
 647  
 648  
 649  class question_does_not_contain_tag_with_attributes {
 650      public $tag;
 651      public $attributes;
 652      public $message;
 653  
 654      public function __construct($tag, $attributes, $message = '') {
 655          $this->tag = $tag;
 656          $this->attributes = $attributes;
 657          $this->message = $message;
 658      }
 659  }
 660  
 661  
 662  class question_contains_tag_with_attribute {
 663      public $tag;
 664      public $attribute;
 665      public $value;
 666      public $message;
 667  
 668      public function __construct($tag, $attribute, $value, $message = '') {
 669          $this->tag = $tag;
 670          $this->attribute = $attribute;
 671          $this->value = $value;
 672          $this->message = $message;
 673      }
 674  }
 675  
 676  
 677  class question_contains_tag_with_attributes {
 678      public $tag;
 679      public $expectedvalues = array();
 680      public $forbiddenvalues = array();
 681      public $message;
 682  
 683      public function __construct($tag, $expectedvalues, $forbiddenvalues=array(), $message = '') {
 684          $this->tag = $tag;
 685          $this->expectedvalues = $expectedvalues;
 686          $this->forbiddenvalues = $forbiddenvalues;
 687          $this->message = $message;
 688      }
 689  }
 690  
 691  
 692  class question_pattern_expectation {
 693      public $pattern;
 694      public $message;
 695  
 696      public function __construct($pattern, $message = '') {
 697          $this->pattern = $pattern;
 698          $this->message = $message;
 699      }
 700  }
 701  
 702  
 703  class question_no_pattern_expectation {
 704      public $pattern;
 705      public $message;
 706  
 707      public function __construct($pattern, $message = '') {
 708          $this->pattern = $pattern;
 709          $this->message = $message;
 710      }
 711  }
 712  
 713  
 714  /**
 715   * Helper base class for question walk-through tests.
 716   *
 717   * The purpose of tests that use this base class is to simulate the entire
 718   * interaction of a student making an attempt at a question. Therefore,
 719   * these are not really unit tests. They would more accurately be described
 720   * as integration tests. However, whether they are unit tests or not,
 721   * it works well to implement them in PHPUnit.
 722   *
 723   * Historically, tests like this were made because Moodle did not have anything
 724   * like Behat for end-to-end testing. Even though we do now have Behat, it makes
 725   * sense to keep these walk-through tests. They run massively faster than Behat
 726   * tests, which gives you a much faster feedback loop while doing development.
 727   * They also make it quite easy to test things like regrading the attempt after
 728   * the question has been edited, which would be possible but very fiddly in Behat.
 729   *
 730   * Ideally, the full set of tests for the question class of a question type would be:
 731   *
 732   * 1. A lot of unit tests for each qtype_myqtype_question class method
 733   *    like grade_response, is_complete_response, is_same_response, ...
 734   *
 735   * 2. Several of these walk-through tests, to test the end-to-end interaction
 736   *    of a student with a question, for example with different behaviours.
 737   *
 738   * 3. Just one Behat test, using question preview, to verify that everything
 739   *    is plugged together correctly and works when used through the UI.
 740   *
 741   * What one would expect to see in one of these walk-through tests is:
 742   *
 743   * // 1. Set up a question: $q.
 744   *
 745   * // 2. A call to $this->start_attempt_at_question($q, ...); with the relevant options.
 746   *
 747   * // 3. Some number of calls to $this->process_submission passing an array of simulated
 748   * //    POST data that matches what would be sent back be submitting a form that contains
 749   * //    the form fields that are output by rendering the question. This is like clicking
 750   * //    the 'Check' button in a question, or navigating to the next page in a quiz.
 751   *
 752   * // 4. A call to $this->finish(); which is the equivalent of clicking
 753   * //    'Submit all and finish' in the quiz.
 754   *
 755   * // 5. After each of steps 2-4 above, one would expect to see a certain amount of
 756   * //    validation of the state of the question and how the question is rendered,
 757   * //    using methods like $this->check_current_state(), $this->check_current_output, etc.
 758   *
 759   * The best way to work out how to write tests like this is probably to look at
 760   * some examples in other question types or question behaviours.
 761   *
 762   * In writing these tests, it is worth noting the following points:
 763   *
 764   * a) The easiest mistake to make is at step 3. You need to ensure that your
 765   *    simulated post data actually matches what gets sent back when the
 766   *    question is submitted in the browser. Try checking it against the
 767   *    HTTP POST requests you see in your browser when the question is submitted.
 768   *    Some question types have a $q->prepare_simulated_post_data() method that
 769   *    can help with this.
 770   *
 771   * b) In the past, tests like these used to contain even more repetitive code,
 772   *    and so they were re-factored to add the helper methods like
 773   *    start_attempt_at_question, process_submission, finish. That change had
 774   *    good effects, like reducing duplicate code. However, there were down-sides.
 775   *    The extra layers of indirection hide what is going on, which means these
 776   *    tests are harder to understand until you know what the helpers are doing.
 777   *    If you want an interesting exercise, take one of the walk-through tests,
 778   *    and inline all the helpers. This might be a good way to understand more about
 779   *    the question engine API. However, having made the everything-inlined code
 780   *    and learned from the process, you should then just throw it away.
 781   *
 782   * c) The way check_current_output works is weird. When these tests were first written
 783   *    Moodle used SimpleTest for unit tests and check_current_output os written in a
 784   *    style that made sense there. When we moved to PHPUnit, a quick and dirty
 785   *    conversion was done. That was a pragmatic move at the time, and we just have
 786   *    to live with the result. Sorry. (And: don't copy that style for new things.)
 787   *
 788   * @copyright  2009 The Open University
 789   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 790   */
 791  abstract class qbehaviour_walkthrough_test_base extends question_testcase {
 792      /** @var question_display_options */
 793      protected $displayoptions;
 794      /** @var question_usage_by_activity */
 795      protected $quba;
 796      /** @var integer */
 797  
 798      protected $slot;
 799      /**
 800       * @var string after {@link render()} has been called, this contains the
 801       * display of the question in its current state.
 802       */
 803      protected $currentoutput = '';
 804  
 805      protected function setUp(): void {
 806          parent::setUp();
 807          $this->resetAfterTest(true);
 808  
 809          $this->displayoptions = new question_display_options();
 810          $this->quba = question_engine::make_questions_usage_by_activity('unit_test',
 811              context_system::instance());
 812      }
 813  
 814      protected function tearDown(): void {
 815          $this->displayoptions = null;
 816          $this->quba = null;
 817          parent::tearDown();
 818      }
 819  
 820      protected function start_attempt_at_question($question, $preferredbehaviour,
 821                                                   $maxmark = null, $variant = 1) {
 822          $this->quba->set_preferred_behaviour($preferredbehaviour);
 823          $this->slot = $this->quba->add_question($question, $maxmark);
 824          $this->quba->start_question($this->slot, $variant);
 825      }
 826  
 827      /**
 828       * Convert an array of data destined for one question to the equivalent POST data.
 829       * @param array $data the data for the quetsion.
 830       * @return array the complete post data.
 831       */
 832      protected function response_data_to_post($data) {
 833          $prefix = $this->quba->get_field_prefix($this->slot);
 834          $fulldata = array(
 835              'slots' => $this->slot,
 836              $prefix . ':sequencecheck' => $this->get_question_attempt()->get_sequence_check_count(),
 837          );
 838          foreach ($data as $name => $value) {
 839              $fulldata[$prefix . $name] = $value;
 840          }
 841          return $fulldata;
 842      }
 843  
 844      protected function process_submission($data) {
 845          // Backwards compatibility.
 846          reset($data);
 847          if (count($data) == 1 && key($data) === '-finish') {
 848              $this->finish();
 849          }
 850  
 851          $this->quba->process_all_actions(time(), $this->response_data_to_post($data));
 852      }
 853  
 854      protected function process_autosave($data) {
 855          $this->quba->process_all_autosaves(null, $this->response_data_to_post($data));
 856      }
 857  
 858      protected function finish() {
 859          $this->quba->finish_all_questions();
 860      }
 861  
 862      protected function manual_grade($comment, $mark, $commentformat = null) {
 863          $this->quba->manual_grade($this->slot, $comment, $mark, $commentformat);
 864      }
 865  
 866      protected function save_quba(moodle_database $db = null) {
 867          question_engine::save_questions_usage_by_activity($this->quba, $db);
 868      }
 869  
 870      protected function load_quba(moodle_database $db = null) {
 871          $this->quba = question_engine::load_questions_usage_by_activity($this->quba->get_id(), $db);
 872      }
 873  
 874      protected function delete_quba() {
 875          question_engine::delete_questions_usage_by_activity($this->quba->get_id());
 876          $this->quba = null;
 877      }
 878  
 879      /**
 880       * Asserts if the manual comment for the question is equal to the provided arguments.
 881       * @param $comment Comment text
 882       * @param $commentformat Comment format
 883       */
 884      protected function check_comment($comment, $commentformat) {
 885          $actualcomment = $this->quba->get_question_attempt($this->slot)->get_manual_comment();
 886  
 887          $this->assertEquals(
 888                  [$comment, $commentformat],
 889                  [$actualcomment[0], $actualcomment[1]]
 890          );
 891      }
 892  
 893      protected function check_current_state($state) {
 894          $this->assertEquals($state, $this->quba->get_question_state($this->slot),
 895              'Questions is in the wrong state.');
 896      }
 897  
 898      protected function check_current_mark($mark) {
 899          if (is_null($mark)) {
 900              $this->assertNull($this->quba->get_question_mark($this->slot));
 901          } else {
 902              if ($mark == 0) {
 903                  // PHP will think a null mark and a mark of 0 are equal,
 904                  // so explicity check not null in this case.
 905                  $this->assertNotNull($this->quba->get_question_mark($this->slot));
 906              }
 907              $this->assertEqualsWithDelta($mark, $this->quba->get_question_mark($this->slot),
 908                   0.000001, 'Expected mark and actual mark differ.');
 909          }
 910      }
 911  
 912      /**
 913       * Generate the HTML rendering of the question in its current state in
 914       * $this->currentoutput so that it can be verified.
 915       */
 916      protected function render() {
 917          $this->currentoutput = $this->quba->render_question($this->slot, $this->displayoptions);
 918      }
 919  
 920      protected function check_output_contains_text_input($name, $value = null, $enabled = true) {
 921          $attributes = array(
 922              'type' => 'text',
 923              'name' => $this->quba->get_field_prefix($this->slot) . $name,
 924          );
 925          if (!is_null($value)) {
 926              $attributes['value'] = $value;
 927          }
 928          if (!$enabled) {
 929              $attributes['readonly'] = 'readonly';
 930          }
 931          $matcher = $this->get_tag_matcher('input', $attributes);
 932          $this->assertTag($matcher, $this->currentoutput,
 933                  'Looking for an input with attributes ' . html_writer::attributes($attributes) . ' in ' . $this->currentoutput);
 934  
 935          if ($enabled) {
 936              $matcher['attributes']['readonly'] = 'readonly';
 937              $this->assertNotTag($matcher, $this->currentoutput,
 938                      'input with attributes ' . html_writer::attributes($attributes) .
 939                      ' should not be read-only in ' . $this->currentoutput);
 940          }
 941      }
 942  
 943      protected function check_output_contains_text_input_with_class($name, $class = null) {
 944          $attributes = array(
 945              'type' => 'text',
 946              'name' => $this->quba->get_field_prefix($this->slot) . $name,
 947          );
 948          if (!is_null($class)) {
 949              $attributes['class'] = 'regexp:/\b' . $class . '\b/';
 950          }
 951  
 952          $matcher = $this->get_tag_matcher('input', $attributes);
 953          $this->assertTag($matcher, $this->currentoutput,
 954                  'Looking for an input with attributes ' . html_writer::attributes($attributes) . ' in ' . $this->currentoutput);
 955      }
 956  
 957      protected function check_output_does_not_contain_text_input_with_class($name, $class = null) {
 958          $attributes = array(
 959              'type' => 'text',
 960              'name' => $this->quba->get_field_prefix($this->slot) . $name,
 961          );
 962          if (!is_null($class)) {
 963              $attributes['class'] = 'regexp:/\b' . $class . '\b/';
 964          }
 965  
 966          $matcher = $this->get_tag_matcher('input', $attributes);
 967          $this->assertNotTag($matcher, $this->currentoutput,
 968                  'Unexpected input with attributes ' . html_writer::attributes($attributes) . ' found in ' . $this->currentoutput);
 969      }
 970  
 971      protected function check_output_contains_hidden_input($name, $value) {
 972          $attributes = array(
 973              'type' => 'hidden',
 974              'name' => $this->quba->get_field_prefix($this->slot) . $name,
 975              'value' => $value,
 976          );
 977          $this->assertTag($this->get_tag_matcher('input', $attributes), $this->currentoutput,
 978                  'Looking for a hidden input with attributes ' . html_writer::attributes($attributes) . ' in ' . $this->currentoutput);
 979      }
 980  
 981      protected function check_output_contains($string) {
 982          $this->render();
 983          $this->assertStringContainsString($string, $this->currentoutput,
 984                  'Expected string ' . $string . ' not found in ' . $this->currentoutput);
 985      }
 986  
 987      protected function check_output_does_not_contain($string) {
 988          $this->render();
 989          $this->assertStringNotContainsString($string, $this->currentoutput,
 990                  'String ' . $string . ' unexpectedly found in ' . $this->currentoutput);
 991      }
 992  
 993      protected function check_output_contains_lang_string($identifier, $component = '', $a = null) {
 994          $this->check_output_contains(get_string($identifier, $component, $a));
 995      }
 996  
 997      protected function get_tag_matcher($tag, $attributes) {
 998          return array(
 999              'tag' => $tag,
1000              'attributes' => $attributes,
1001          );
1002      }
1003  
1004      /**
1005       * @param $condition one or more Expectations. (users varargs).
1006       */
1007      protected function check_current_output() {
1008          $html = $this->quba->render_question($this->slot, $this->displayoptions);
1009          foreach (func_get_args() as $condition) {
1010              $this->assert($condition, $html);
1011          }
1012      }
1013  
1014      /**
1015       * Use this function rather than check_current_output for select expectations where
1016       * checking the value of the options is required. check_current_output only checks
1017       * that the right number of options are available.
1018       *
1019       * @param question_contains_select_expectation $expectations One or more expectations.
1020       */
1021      protected function check_output_contains_selectoptions(...$expectations) {
1022          $html = $this->quba->render_question($this->slot, $this->displayoptions);
1023          foreach ($expectations as $expectation) {
1024              $this->assert_select_options($expectation, $html);
1025          }
1026      }
1027  
1028      protected function get_question_attempt() {
1029          return $this->quba->get_question_attempt($this->slot);
1030      }
1031  
1032      protected function get_step_count() {
1033          return $this->get_question_attempt()->get_num_steps();
1034      }
1035  
1036      protected function check_step_count($expectednumsteps) {
1037          $this->assertEquals($expectednumsteps, $this->get_step_count());
1038      }
1039  
1040      protected function get_step($stepnum) {
1041          return $this->get_question_attempt()->get_step($stepnum);
1042      }
1043  
1044      protected function get_contains_question_text_expectation($question) {
1045          return new question_pattern_expectation('/' . preg_quote($question->questiontext, '/') . '/');
1046      }
1047  
1048      protected function get_contains_general_feedback_expectation($question) {
1049          return new question_pattern_expectation('/' . preg_quote($question->generalfeedback, '/') . '/');
1050      }
1051  
1052      protected function get_does_not_contain_correctness_expectation() {
1053          return new question_no_pattern_expectation('/class=\"correctness/');
1054      }
1055  
1056      protected function get_contains_correct_expectation() {
1057          return new question_pattern_expectation('/' . preg_quote(get_string('correct', 'question'), '/') . '/');
1058      }
1059  
1060      protected function get_contains_partcorrect_expectation() {
1061          return new question_pattern_expectation('/' .
1062              preg_quote(get_string('partiallycorrect', 'question'), '/') . '/');
1063      }
1064  
1065      protected function get_contains_incorrect_expectation() {
1066          return new question_pattern_expectation('/' . preg_quote(get_string('incorrect', 'question'), '/') . '/');
1067      }
1068  
1069      protected function get_contains_standard_correct_combined_feedback_expectation() {
1070          return new question_pattern_expectation('/' .
1071              preg_quote(test_question_maker::STANDARD_OVERALL_CORRECT_FEEDBACK, '/') . '/');
1072      }
1073  
1074      protected function get_contains_standard_partiallycorrect_combined_feedback_expectation() {
1075          return new question_pattern_expectation('/' .
1076              preg_quote(test_question_maker::STANDARD_OVERALL_PARTIALLYCORRECT_FEEDBACK, '/') . '/');
1077      }
1078  
1079      protected function get_contains_standard_incorrect_combined_feedback_expectation() {
1080          return new question_pattern_expectation('/' .
1081              preg_quote(test_question_maker::STANDARD_OVERALL_INCORRECT_FEEDBACK, '/') . '/');
1082      }
1083  
1084      protected function get_does_not_contain_feedback_expectation() {
1085          return new question_no_pattern_expectation('/class="feedback"/');
1086      }
1087  
1088      protected function get_does_not_contain_num_parts_correct() {
1089          return new question_no_pattern_expectation('/class="numpartscorrect"/');
1090      }
1091  
1092      protected function get_contains_num_parts_correct($num) {
1093          $a = new stdClass();
1094          $a->num = $num;
1095          return new question_pattern_expectation('/<div class="numpartscorrect">' .
1096              preg_quote(get_string('yougotnright', 'question', $a), '/') . '/');
1097      }
1098  
1099      protected function get_does_not_contain_specific_feedback_expectation() {
1100          return new question_no_pattern_expectation('/class="specificfeedback"/');
1101      }
1102  
1103      protected function get_contains_validation_error_expectation() {
1104          return new question_contains_tag_with_attribute('div', 'class', 'validationerror');
1105      }
1106  
1107      protected function get_does_not_contain_validation_error_expectation() {
1108          return new question_no_pattern_expectation('/class="validationerror"/');
1109      }
1110  
1111      protected function get_contains_mark_summary($mark) {
1112          $a = new stdClass();
1113          $a->mark = format_float($mark, $this->displayoptions->markdp);
1114          $a->max = format_float($this->quba->get_question_max_mark($this->slot),
1115              $this->displayoptions->markdp);
1116          return new question_pattern_expectation('/' .
1117              preg_quote(get_string('markoutofmax', 'question', $a), '/') . '/');
1118      }
1119  
1120      protected function get_contains_marked_out_of_summary() {
1121          $max = format_float($this->quba->get_question_max_mark($this->slot),
1122              $this->displayoptions->markdp);
1123          return new question_pattern_expectation('/' .
1124              preg_quote(get_string('markedoutofmax', 'question', $max), '/') . '/');
1125      }
1126  
1127      protected function get_does_not_contain_mark_summary() {
1128          return new question_no_pattern_expectation('/<div class="grade">/');
1129      }
1130  
1131      protected function get_contains_checkbox_expectation($baseattr, $enabled, $checked) {
1132          $expectedattributes = $baseattr;
1133          $forbiddenattributes = array();
1134          $expectedattributes['type'] = 'checkbox';
1135          if ($enabled === true) {
1136              $forbiddenattributes['disabled'] = 'disabled';
1137          } else if ($enabled === false) {
1138              $expectedattributes['disabled'] = 'disabled';
1139          }
1140          if ($checked === true) {
1141              $expectedattributes['checked'] = 'checked';
1142          } else if ($checked === false) {
1143              $forbiddenattributes['checked'] = 'checked';
1144          }
1145          return new question_contains_tag_with_attributes('input', $expectedattributes, $forbiddenattributes);
1146      }
1147  
1148      protected function get_contains_mc_checkbox_expectation($index, $enabled = null,
1149                                                              $checked = null) {
1150          return $this->get_contains_checkbox_expectation(array(
1151              'name' => $this->quba->get_field_prefix($this->slot) . $index,
1152              'value' => 1,
1153          ), $enabled, $checked);
1154      }
1155  
1156      protected function get_contains_radio_expectation($baseattr, $enabled, $checked) {
1157          $expectedattributes = $baseattr;
1158          $forbiddenattributes = array();
1159          $expectedattributes['type'] = 'radio';
1160          if ($enabled === true) {
1161              $forbiddenattributes['disabled'] = 'disabled';
1162          } else if ($enabled === false) {
1163              $expectedattributes['disabled'] = 'disabled';
1164          }
1165          if ($checked === true) {
1166              $expectedattributes['checked'] = 'checked';
1167          } else if ($checked === false) {
1168              $forbiddenattributes['checked'] = 'checked';
1169          }
1170          return new question_contains_tag_with_attributes('input', $expectedattributes, $forbiddenattributes);
1171      }
1172  
1173      protected function get_contains_mc_radio_expectation($index, $enabled = null, $checked = null) {
1174          return $this->get_contains_radio_expectation(array(
1175              'name' => $this->quba->get_field_prefix($this->slot) . 'answer',
1176              'value' => $index,
1177          ), $enabled, $checked);
1178      }
1179  
1180      protected function get_contains_hidden_expectation($name, $value = null) {
1181          $expectedattributes = array('type' => 'hidden', 'name' => s($name));
1182          if (!is_null($value)) {
1183              $expectedattributes['value'] = s($value);
1184          }
1185          return new question_contains_tag_with_attributes('input', $expectedattributes);
1186      }
1187  
1188      protected function get_does_not_contain_hidden_expectation($name, $value = null) {
1189          $expectedattributes = array('type' => 'hidden', 'name' => s($name));
1190          if (!is_null($value)) {
1191              $expectedattributes['value'] = s($value);
1192          }
1193          return new question_does_not_contain_tag_with_attributes('input', $expectedattributes);
1194      }
1195  
1196      protected function get_contains_tf_true_radio_expectation($enabled = null, $checked = null) {
1197          return $this->get_contains_radio_expectation(array(
1198              'name' => $this->quba->get_field_prefix($this->slot) . 'answer',
1199              'value' => 1,
1200          ), $enabled, $checked);
1201      }
1202  
1203      protected function get_contains_tf_false_radio_expectation($enabled = null, $checked = null) {
1204          return $this->get_contains_radio_expectation(array(
1205              'name' => $this->quba->get_field_prefix($this->slot) . 'answer',
1206              'value' => 0,
1207          ), $enabled, $checked);
1208      }
1209  
1210      protected function get_contains_cbm_radio_expectation($certainty, $enabled = null,
1211                                                            $checked = null) {
1212          return $this->get_contains_radio_expectation(array(
1213              'name' => $this->quba->get_field_prefix($this->slot) . '-certainty',
1214              'value' => $certainty,
1215          ), $enabled, $checked);
1216      }
1217  
1218      protected function get_contains_button_expectation($name, $value = null, $enabled = null) {
1219          $expectedattributes = array(
1220              'type' => 'submit',
1221              'name' => $name,
1222          );
1223          $forbiddenattributes = array();
1224          if (!is_null($value)) {
1225              $expectedattributes['value'] = $value;
1226          }
1227          if ($enabled === true) {
1228              $forbiddenattributes['disabled'] = 'disabled';
1229          } else if ($enabled === false) {
1230              $expectedattributes['disabled'] = 'disabled';
1231          }
1232          return new question_contains_tag_with_attributes('button', $expectedattributes, $forbiddenattributes);
1233      }
1234  
1235      /**
1236       * Returns an epectation that a string contains the HTML of a button with
1237       * name {question-attempt prefix}-submit, and eiter enabled or not.
1238       * @param bool $enabled if not null, check the enabled/disabled state of the button. True = enabled.
1239       * @return question_contains_tag_with_attributes an expectation for use with check_current_output.
1240       */
1241      protected function get_contains_submit_button_expectation($enabled = null) {
1242          return $this->get_contains_button_expectation(
1243              $this->quba->get_field_prefix($this->slot) . '-submit', null, $enabled);
1244      }
1245  
1246      /**
1247       * Returns an epectation that a string does not contain the HTML of a button with
1248       * name {question-attempt prefix}-submit.
1249       * @return question_contains_tag_with_attributes an expectation for use with check_current_output.
1250       */
1251      protected function get_does_not_contain_submit_button_expectation() {
1252          return new question_no_pattern_expectation('/name="' .
1253                  $this->quba->get_field_prefix($this->slot) . '-submit"/');
1254      }
1255  
1256      protected function get_tries_remaining_expectation($n) {
1257          return new question_pattern_expectation('/' .
1258              preg_quote(get_string('triesremaining', 'qbehaviour_interactive', $n), '/') . '/');
1259      }
1260  
1261      protected function get_invalid_answer_expectation() {
1262          return new question_pattern_expectation('/' .
1263              preg_quote(get_string('invalidanswer', 'question'), '/') . '/');
1264      }
1265  
1266      protected function get_contains_try_again_button_expectation($enabled = null) {
1267          $expectedattributes = array(
1268              'type' => 'submit',
1269              'name' => $this->quba->get_field_prefix($this->slot) . '-tryagain',
1270          );
1271          $forbiddenattributes = array();
1272          if ($enabled === true) {
1273              $forbiddenattributes['disabled'] = 'disabled';
1274          } else if ($enabled === false) {
1275              $expectedattributes['disabled'] = 'disabled';
1276          }
1277          return new question_contains_tag_with_attributes('input', $expectedattributes, $forbiddenattributes);
1278      }
1279  
1280      protected function get_does_not_contain_try_again_button_expectation() {
1281          return new question_no_pattern_expectation('/name="' .
1282              $this->quba->get_field_prefix($this->slot) . '-tryagain"/');
1283      }
1284  
1285      protected function get_contains_select_expectation($name, $choices,
1286                                                         $selected = null, $enabled = null) {
1287          $fullname = $this->quba->get_field_prefix($this->slot) . $name;
1288          return new question_contains_select_expectation($fullname, $choices, $selected, $enabled);
1289      }
1290  
1291      protected function get_mc_right_answer_index($mc) {
1292          $order = $mc->get_order($this->get_question_attempt());
1293          foreach ($order as $i => $ansid) {
1294              if ($mc->answers[$ansid]->fraction == 1) {
1295                  return $i;
1296              }
1297          }
1298          $this->fail('This multiple choice question does not seem to have a right answer!');
1299      }
1300  
1301      protected function get_no_hint_visible_expectation() {
1302          return new question_no_pattern_expectation('/class="hint"/');
1303      }
1304  
1305      protected function get_contains_hint_expectation($hinttext) {
1306          // Does not currently verify hint text.
1307          return new question_contains_tag_with_attribute('div', 'class', 'hint');
1308      }
1309  
1310      /**
1311       * Returns an expectation that a string contains a corrupted question notification.
1312       *
1313       * @return question_pattern_expectation an expectation for use with check_current_output.
1314       */
1315      protected function get_contains_corruption_notification() {
1316          return new question_pattern_expectation('/' . preg_quote(get_string('corruptedquestion', 'qtype_multianswer'), '/') . '/');
1317      }
1318  
1319      /**
1320       * Returns an expectation that a string contains a corrupted subquestion message.
1321       *
1322       * @return question_pattern_expectation an expectation for use with check_current_output.
1323       */
1324      protected function get_contains_corrupted_subquestion_message() {
1325          return new question_pattern_expectation('/' . preg_quote(get_string('missingsubquestion', 'qtype_multianswer'), '/') . '/');
1326      }
1327  }
1328  
1329  /**
1330   * Simple class that implements the {@link moodle_recordset} API based on an
1331   * array of test data.
1332   *
1333   *  See the {@link question_attempt_step_db_test} class in
1334   *  question/engine/tests/testquestionattemptstep.php for an example of how
1335   *  this is used.
1336   *
1337   * @copyright  2011 The Open University
1338   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1339   */
1340  class question_test_recordset extends moodle_recordset {
1341      protected $records;
1342  
1343      /**
1344       * Constructor
1345       * @param $table as for {@link testing_db_record_builder::build_db_records()}
1346       *      but does not need a unique first column.
1347       */
1348      public function __construct(array $table) {
1349          $columns = array_shift($table);
1350          $this->records = array();
1351          foreach ($table as $row) {
1352              if (count($row) != count($columns)) {
1353                  throw new coding_exception("Row contains the wrong number of fields.");
1354              }
1355              $rec = array();
1356              foreach ($columns as $i => $name) {
1357                  $rec[$name] = $row[$i];
1358              }
1359              $this->records[] = $rec;
1360          }
1361          reset($this->records);
1362      }
1363  
1364      public function __destruct() {
1365          $this->close();
1366      }
1367  
1368      #[\ReturnTypeWillChange]
1369      public function current() {
1370          return (object) current($this->records);
1371      }
1372  
1373      #[\ReturnTypeWillChange]
1374      public function key() {
1375          if (is_null(key($this->records))) {
1376              return false;
1377          }
1378          $current = current($this->records);
1379          return reset($current);
1380      }
1381  
1382      public function next(): void {
1383          next($this->records);
1384      }
1385  
1386      public function valid(): bool {
1387          return !is_null(key($this->records));
1388      }
1389  
1390      public function close() {
1391          $this->records = null;
1392      }
1393  }
1394  
1395  /**
1396   * Provide utility function for random question test
1397   *
1398   * @package   core_question
1399   * @author     Nathan Nguyen <nathannguyen@catalyst-au.net>
1400   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1401   */
1402  class question_filter_test_helper {
1403      /**
1404       * Create filters base on provided values
1405       *
1406       * @param array $categoryids question category filter
1407       * @param bool $recursive subcategories filter
1408       * @param array $qtagids tags filter
1409       * @return array
1410       */
1411      public static function create_filters(array $categoryids, bool $recursive = false, array $qtagids = []): array {
1412          $filters = [
1413              'category' => [
1414                  'jointype' => \qbank_managecategories\category_condition::JOINTYPE_DEFAULT,
1415                  'values' => $categoryids,
1416                  'filteroptions' => ['includesubcategories' => $recursive],
1417              ],
1418              'qtagids' => [
1419                  'jointype' => \qbank_tagquestion\tag_condition::JOINTYPE_DEFAULT,
1420                  'values' => $qtagids,
1421              ],
1422          ];
1423          return $filters;
1424      }
1425  }