PageRenderTime 47ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/question/engine/tests/helpers.php

https://bitbucket.org/moodle/moodle
PHP | 1386 lines | 883 code | 183 blank | 320 comment | 81 complexity | cb047ab60cae0606928739b217aab6a0 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file