Differences Between: [Versions 39 and 310]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Unit tests for (some of) question/type/calculated/questiontype.php. 19 * 20 * @package qtype_calculated 21 * @copyright 2012 The Open University 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 require_once($CFG->dirroot . '/question/type/calculated/questiontype.php'); 30 require_once($CFG->dirroot . '/question/type/calculated/tests/helper.php'); 31 32 33 /** 34 * Unit tests for question/type/calculated/questiontype.php. 35 * 36 * @copyright 2012 The Open University 37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 38 */ 39 class qtype_calculated_test extends advanced_testcase { 40 public static $includecoverage = array( 41 'question/type/questiontypebase.php', 42 'question/type/calculated/questiontype.php' 43 ); 44 45 protected $tolerance = 0.00000001; 46 protected $qtype; 47 48 protected function setUp(): void { 49 $this->qtype = new qtype_calculated(); 50 } 51 52 protected function tearDown(): void { 53 $this->qtype = null; 54 } 55 56 public function test_name() { 57 $this->assertEquals($this->qtype->name(), 'calculated'); 58 } 59 60 public function test_can_analyse_responses() { 61 $this->assertTrue($this->qtype->can_analyse_responses()); 62 } 63 64 public function test_get_random_guess_score() { 65 $q = test_question_maker::get_question_data('calculated'); 66 $q->options->answers[17]->fraction = 0.1; 67 $this->assertEquals(0.1, $this->qtype->get_random_guess_score($q)); 68 } 69 70 public function test_load_question() { 71 $this->resetAfterTest(); 72 73 $syscontext = context_system::instance(); 74 /** @var core_question_generator $generator */ 75 $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); 76 $category = $generator->create_question_category(['contextid' => $syscontext->id]); 77 78 $fromform = test_question_maker::get_question_form_data('calculated'); 79 $fromform->category = $category->id . ',' . $syscontext->id; 80 81 $question = new stdClass(); 82 $question->category = $category->id; 83 $question->qtype = 'calculated'; 84 $question->createdby = 0; 85 86 $this->qtype->save_question($question, $fromform); 87 $questiondata = question_bank::load_question_data($question->id); 88 89 $this->assertEquals(['id', 'category', 'parent', 'name', 'questiontext', 'questiontextformat', 90 'generalfeedback', 'generalfeedbackformat', 'defaultmark', 'penalty', 'qtype', 91 'length', 'stamp', 'version', 'hidden', 'timecreated', 'timemodified', 92 'createdby', 'modifiedby', 'idnumber', 'contextid', 'options', 'hints', 'categoryobject'], 93 array_keys(get_object_vars($questiondata))); 94 $this->assertEquals($category->id, $questiondata->category); 95 $this->assertEquals(0, $questiondata->parent); 96 $this->assertEquals($fromform->name, $questiondata->name); 97 $this->assertEquals($fromform->questiontext, $questiondata->questiontext); 98 $this->assertEquals($fromform->questiontextformat, $questiondata->questiontextformat); 99 $this->assertEquals('', $questiondata->generalfeedback); 100 $this->assertEquals(0, $questiondata->generalfeedbackformat); 101 $this->assertEquals($fromform->defaultmark, $questiondata->defaultmark); 102 $this->assertEquals(0, $questiondata->penalty); 103 $this->assertEquals('calculated', $questiondata->qtype); 104 $this->assertEquals(1, $questiondata->length); 105 $this->assertEquals(0, $questiondata->hidden); 106 $this->assertEquals($question->createdby, $questiondata->createdby); 107 $this->assertEquals($question->createdby, $questiondata->modifiedby); 108 $this->assertEquals('', $questiondata->idnumber); 109 $this->assertEquals($syscontext->id, $questiondata->contextid); 110 $this->assertEquals([], $questiondata->hints); 111 112 // Options. 113 $this->assertEquals($questiondata->id, $questiondata->options->question); 114 $this->assertEquals([], $questiondata->options->units); 115 $this->assertEquals(qtype_numerical::UNITNONE, $questiondata->options->showunits); 116 $this->assertEquals(0, $questiondata->options->unitgradingtype); // Unit role is none, so this is 0. 117 $this->assertEquals($fromform->unitpenalty, $questiondata->options->unitpenalty); 118 $this->assertEquals($fromform->unitsleft, $questiondata->options->unitsleft); 119 120 // Build the expected answer base. 121 $answerbase = [ 122 'question' => $questiondata->id, 123 'answerformat' => 0, 124 ]; 125 $expectedanswers = []; 126 foreach ($fromform->answer as $key => $value) { 127 $answer = $answerbase + [ 128 'answer' => $fromform->answer[$key], 129 'fraction' => (float)$fromform->fraction[$key], 130 'tolerance' => $fromform->tolerance[$key], 131 'tolerancetype' => $fromform->tolerancetype[$key], 132 'correctanswerlength' => $fromform->correctanswerlength[$key], 133 'correctanswerformat' => $fromform->correctanswerformat[$key], 134 'feedback' => $fromform->feedback[$key]['text'], 135 'feedbackformat' => $fromform->feedback[$key]['format'], 136 ]; 137 $expectedanswers[] = (object)$answer; 138 } 139 // Need to get rid of ids. 140 $gotanswers = array_map(function($answer) { 141 unset($answer->id); 142 return $answer; 143 }, $questiondata->options->answers); 144 // Compare answers. 145 $this->assertEquals($expectedanswers, array_values($gotanswers)); 146 } 147 148 protected function get_possible_response($ans, $tolerance, $type) { 149 $a = new stdClass(); 150 $a->answer = $ans; 151 $a->tolerance = $tolerance; 152 $a->tolerancetype = get_string($type, 'qtype_numerical'); 153 return get_string('answerwithtolerance', 'qtype_calculated', $a); 154 } 155 156 public function test_get_possible_responses() { 157 $q = test_question_maker::get_question_data('calculated'); 158 159 $this->assertEquals(array( 160 $q->id => array( 161 13 => new question_possible_response( 162 $this->get_possible_response('{a} + {b}', 0.001, 'nominal'), 1.0), 163 14 => new question_possible_response( 164 $this->get_possible_response('{a} - {b}', 0.001, 'nominal'), 0.0), 165 17 => new question_possible_response('*', 0.0), 166 null => question_possible_response::no_response() 167 ), 168 ), $this->qtype->get_possible_responses($q)); 169 } 170 171 public function test_get_possible_responses_no_star() { 172 $q = test_question_maker::get_question_data('calculated'); 173 unset($q->options->answers[17]); 174 175 $this->assertEquals(array( 176 $q->id => array( 177 13 => new question_possible_response( 178 $this->get_possible_response('{a} + {b}', 0.001, 'nominal'), 1), 179 14 => new question_possible_response( 180 $this->get_possible_response('{a} - {b}', 0.001, 'nominal'), 0), 181 0 => new question_possible_response( 182 get_string('didnotmatchanyanswer', 'question'), 0), 183 null => question_possible_response::no_response() 184 ), 185 ), $this->qtype->get_possible_responses($q)); 186 } 187 188 public function test_get_short_question_name() { 189 $this->resetAfterTest(); 190 191 // Enable multilang filter to on content and heading. 192 filter_set_global_state('multilang', TEXTFILTER_ON); 193 filter_set_applies_to_strings('multilang', 1); 194 $filtermanager = filter_manager::instance(); 195 $filtermanager->reset_caches(); 196 197 $context = context_system::instance(); 198 199 $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>"; 200 $shortmultilangquestionname = "<span lang=\"en\" class=\"multilang\">Lorem ipsum</span><span lang=\"fr\" class=\"multilang\">Lorem ipsum</span>"; 201 $longquestionname = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr"; 202 $shortquestionname = "Lorem ipsum"; 203 $this->assertEquals("Lorem ipsum dolor...", $this->qtype->get_short_question_name($longmultilangquestionname, 20)); 204 $this->assertEquals("Lorem ipsum", $this->qtype->get_short_question_name($shortmultilangquestionname, 20)); 205 $this->assertEquals("Lorem ipsum dolor...", $this->qtype->get_short_question_name($longquestionname, 20)); 206 $this->assertEquals("Lorem ipsum", $this->qtype->get_short_question_name($shortquestionname, 20)); 207 } 208 209 public function test_placehodler_regex() { 210 preg_match_all(qtype_calculated::PLACEHODLER_REGEX, '= {={a} + {b}}', $matches); 211 $this->assertEquals([['{a}', '{b}'], ['a', 'b']], $matches); 212 } 213 214 public function test_formulas_in_text_regex() { 215 preg_match_all(qtype_calculated::FORMULAS_IN_TEXT_REGEX, '= {={a} + {b}}', $matches); 216 $this->assertEquals([['{={a} + {b}}'], ['{a} + {b}']], $matches); 217 } 218 219 public function test_find_dataset_names() { 220 $this->assertEquals([], $this->qtype->find_dataset_names('Frog.')); 221 222 $this->assertEquals(['a' => 'a', 'b' => 'b'], 223 $this->qtype->find_dataset_names('= {={a} + {b}}')); 224 225 $this->assertEquals(['a' => 'a', 'b' => 'b'], 226 $this->qtype->find_dataset_names('What is {a} plus {b}? (Hint, it is not {={a}*{b}}.)')); 227 228 $this->assertEquals(['a' => 'a', 'b' => 'b', 'c' => 'c'], 229 $this->qtype->find_dataset_names(' 230 <p>If called with $a = {a} and $b = {b}, what does this PHP function return?</p> 231 <pre> 232 /** 233 * What does this do? 234 */ 235 function mystery($a, $b) { 236 return {c}*$a + $b; 237 } 238 </pre> 239 ')); 240 } 241 242 public function test_calculate_answer_nan_inf() { 243 $answer = qtype_calculated_calculate_answer('acos(1.1)', [], 0.1, 1, 2, 2); 244 $this->assertIsObject($answer); 245 $this->assertNan($answer->answer); 246 247 $answer = qtype_calculated_calculate_answer('log(0.0)', [], 0.1, 1, 2, 2); 248 $this->assertIsObject($answer); 249 $this->assertInfinite($answer->answer); // Actually -INF. 250 251 // Dividing by zero is hard to test, so get +INF another way. 252 $answer = qtype_calculated_calculate_answer('abs(log(0.0))', [], 0.1, 1, 2, 2); 253 $this->assertIsObject($answer); 254 $this->assertInfinite($answer->answer); 255 } 256 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body