PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/question/type/calculated/tests/questiontype_test.php

http://github.com/moodle/moodle
PHP | 241 lines | 173 code | 35 blank | 33 comment | 1 complexity | 168bbf3d9e136ec7fc771adc2b411af4 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * Unit tests for (some of) question/type/calculated/questiontype.php.
  18. *
  19. * @package qtype_calculated
  20. * @copyright 2012 The Open University
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. global $CFG;
  25. require_once($CFG->dirroot . '/question/type/calculated/questiontype.php');
  26. require_once($CFG->dirroot . '/question/type/calculated/tests/helper.php');
  27. /**
  28. * Unit tests for question/type/calculated/questiontype.php.
  29. *
  30. * @copyright 2012 The Open University
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class qtype_calculated_test extends advanced_testcase {
  34. public static $includecoverage = array(
  35. 'question/type/questiontypebase.php',
  36. 'question/type/calculated/questiontype.php'
  37. );
  38. protected $tolerance = 0.00000001;
  39. protected $qtype;
  40. protected function setUp() {
  41. $this->qtype = new qtype_calculated();
  42. }
  43. protected function tearDown() {
  44. $this->qtype = null;
  45. }
  46. public function test_name() {
  47. $this->assertEquals($this->qtype->name(), 'calculated');
  48. }
  49. public function test_can_analyse_responses() {
  50. $this->assertTrue($this->qtype->can_analyse_responses());
  51. }
  52. public function test_get_random_guess_score() {
  53. $q = test_question_maker::get_question_data('calculated');
  54. $q->options->answers[17]->fraction = 0.1;
  55. $this->assertEquals(0.1, $this->qtype->get_random_guess_score($q));
  56. }
  57. public function test_load_question() {
  58. $this->resetAfterTest();
  59. $syscontext = context_system::instance();
  60. /** @var core_question_generator $generator */
  61. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  62. $category = $generator->create_question_category(['contextid' => $syscontext->id]);
  63. $fromform = test_question_maker::get_question_form_data('calculated');
  64. $fromform->category = $category->id . ',' . $syscontext->id;
  65. $question = new stdClass();
  66. $question->category = $category->id;
  67. $question->qtype = 'calculated';
  68. $question->createdby = 0;
  69. $this->qtype->save_question($question, $fromform);
  70. $questiondata = question_bank::load_question_data($question->id);
  71. $this->assertEquals(['id', 'category', 'parent', 'name', 'questiontext', 'questiontextformat',
  72. 'generalfeedback', 'generalfeedbackformat', 'defaultmark', 'penalty', 'qtype',
  73. 'length', 'stamp', 'version', 'hidden', 'timecreated', 'timemodified',
  74. 'createdby', 'modifiedby', 'idnumber', 'contextid', 'options', 'hints', 'categoryobject'],
  75. array_keys(get_object_vars($questiondata)));
  76. $this->assertEquals($category->id, $questiondata->category);
  77. $this->assertEquals(0, $questiondata->parent);
  78. $this->assertEquals($fromform->name, $questiondata->name);
  79. $this->assertEquals($fromform->questiontext, $questiondata->questiontext);
  80. $this->assertEquals($fromform->questiontextformat, $questiondata->questiontextformat);
  81. $this->assertEquals('', $questiondata->generalfeedback);
  82. $this->assertEquals(0, $questiondata->generalfeedbackformat);
  83. $this->assertEquals($fromform->defaultmark, $questiondata->defaultmark);
  84. $this->assertEquals(0, $questiondata->penalty);
  85. $this->assertEquals('calculated', $questiondata->qtype);
  86. $this->assertEquals(1, $questiondata->length);
  87. $this->assertEquals(0, $questiondata->hidden);
  88. $this->assertEquals($question->createdby, $questiondata->createdby);
  89. $this->assertEquals($question->createdby, $questiondata->modifiedby);
  90. $this->assertEquals('', $questiondata->idnumber);
  91. $this->assertEquals($syscontext->id, $questiondata->contextid);
  92. $this->assertEquals([], $questiondata->hints);
  93. // Options.
  94. $this->assertEquals($questiondata->id, $questiondata->options->question);
  95. $this->assertEquals([], $questiondata->options->units);
  96. $this->assertEquals(qtype_numerical::UNITNONE, $questiondata->options->showunits);
  97. $this->assertEquals(0, $questiondata->options->unitgradingtype); // Unit role is none, so this is 0.
  98. $this->assertEquals($fromform->unitpenalty, $questiondata->options->unitpenalty);
  99. $this->assertEquals($fromform->unitsleft, $questiondata->options->unitsleft);
  100. // Build the expected answer base.
  101. $answerbase = [
  102. 'question' => $questiondata->id,
  103. 'answerformat' => 0,
  104. ];
  105. $expectedanswers = [];
  106. foreach ($fromform->answer as $key => $value) {
  107. $answer = $answerbase + [
  108. 'answer' => $fromform->answer[$key],
  109. 'fraction' => (float)$fromform->fraction[$key],
  110. 'tolerance' => $fromform->tolerance[$key],
  111. 'tolerancetype' => $fromform->tolerancetype[$key],
  112. 'correctanswerlength' => $fromform->correctanswerlength[$key],
  113. 'correctanswerformat' => $fromform->correctanswerformat[$key],
  114. 'feedback' => $fromform->feedback[$key]['text'],
  115. 'feedbackformat' => $fromform->feedback[$key]['format'],
  116. ];
  117. $expectedanswers[] = (object)$answer;
  118. }
  119. // Need to get rid of ids.
  120. $gotanswers = array_map(function($answer) {
  121. unset($answer->id);
  122. return $answer;
  123. }, $questiondata->options->answers);
  124. // Compare answers.
  125. $this->assertEquals($expectedanswers, array_values($gotanswers));
  126. }
  127. protected function get_possible_response($ans, $tolerance, $type) {
  128. $a = new stdClass();
  129. $a->answer = $ans;
  130. $a->tolerance = $tolerance;
  131. $a->tolerancetype = get_string($type, 'qtype_numerical');
  132. return get_string('answerwithtolerance', 'qtype_calculated', $a);
  133. }
  134. public function test_get_possible_responses() {
  135. $q = test_question_maker::get_question_data('calculated');
  136. $this->assertEquals(array(
  137. $q->id => array(
  138. 13 => new question_possible_response(
  139. $this->get_possible_response('{a} + {b}', 0.001, 'nominal'), 1.0),
  140. 14 => new question_possible_response(
  141. $this->get_possible_response('{a} - {b}', 0.001, 'nominal'), 0.0),
  142. 17 => new question_possible_response('*', 0.0),
  143. null => question_possible_response::no_response()
  144. ),
  145. ), $this->qtype->get_possible_responses($q));
  146. }
  147. public function test_get_possible_responses_no_star() {
  148. $q = test_question_maker::get_question_data('calculated');
  149. unset($q->options->answers[17]);
  150. $this->assertEquals(array(
  151. $q->id => array(
  152. 13 => new question_possible_response(
  153. $this->get_possible_response('{a} + {b}', 0.001, 'nominal'), 1),
  154. 14 => new question_possible_response(
  155. $this->get_possible_response('{a} - {b}', 0.001, 'nominal'), 0),
  156. 0 => new question_possible_response(
  157. get_string('didnotmatchanyanswer', 'question'), 0),
  158. null => question_possible_response::no_response()
  159. ),
  160. ), $this->qtype->get_possible_responses($q));
  161. }
  162. public function test_get_short_question_name() {
  163. $this->resetAfterTest();
  164. // Enable multilang filter to on content and heading.
  165. filter_set_global_state('multilang', TEXTFILTER_ON);
  166. filter_set_applies_to_strings('multilang', 1);
  167. $filtermanager = filter_manager::instance();
  168. $filtermanager->reset_caches();
  169. $context = context_system::instance();
  170. $longmultilangquestionname = "<span lang=\"en\" class=\"multilang\">Lorem ipsum dolor sit amet, consetetur sadipscing elitr</span><span lang=\"fr\" class=\"multilang\">Lorem ipsum dolor sit amet, consetetur sadipscing elitr</span>";
  171. $shortmultilangquestionname = "<span lang=\"en\" class=\"multilang\">Lorem ipsum</span><span lang=\"fr\" class=\"multilang\">Lorem ipsum</span>";
  172. $longquestionname = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr";
  173. $shortquestionname = "Lorem ipsum";
  174. $this->assertEquals("Lorem ipsum dolor...", $this->qtype->get_short_question_name($longmultilangquestionname, 20));
  175. $this->assertEquals("Lorem ipsum", $this->qtype->get_short_question_name($shortmultilangquestionname, 20));
  176. $this->assertEquals("Lorem ipsum dolor...", $this->qtype->get_short_question_name($longquestionname, 20));
  177. $this->assertEquals("Lorem ipsum", $this->qtype->get_short_question_name($shortquestionname, 20));
  178. }
  179. public function test_placehodler_regex() {
  180. preg_match_all(qtype_calculated::PLACEHODLER_REGEX, '= {={a} + {b}}', $matches);
  181. $this->assertEquals([['{a}', '{b}'], ['a', 'b']], $matches);
  182. }
  183. public function test_formulas_in_text_regex() {
  184. preg_match_all(qtype_calculated::FORMULAS_IN_TEXT_REGEX, '= {={a} + {b}}', $matches);
  185. $this->assertEquals([['{={a} + {b}}'], ['{a} + {b}']], $matches);
  186. }
  187. public function test_find_dataset_names() {
  188. $this->assertEquals([], $this->qtype->find_dataset_names('Frog.'));
  189. $this->assertEquals(['a' => 'a', 'b' => 'b'],
  190. $this->qtype->find_dataset_names('= {={a} + {b}}'));
  191. $this->assertEquals(['a' => 'a', 'b' => 'b'],
  192. $this->qtype->find_dataset_names('What is {a} plus {b}? (Hint, it is not {={a}*{b}}.)'));
  193. $this->assertEquals(['a' => 'a', 'b' => 'b', 'c' => 'c'],
  194. $this->qtype->find_dataset_names('
  195. <p>If called with $a = {a} and $b = {b}, what does this PHP function return?</p>
  196. <pre>
  197. /**
  198. * What does this do?
  199. */
  200. function mystery($a, $b) {
  201. return {c}*$a + $b;
  202. }
  203. </pre>
  204. '));
  205. }
  206. }