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