PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/question/type/calculated/tests/question_type_test.php

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