Commit 9dd3dd7e authored by Marina Glancy's avatar Marina Glancy Committed by Eloy Lafuente (stronk7)

MDL-62469 qtype_calculated: check remaining placeholders, see MDL-62275

parent 52b4e371
...@@ -424,7 +424,13 @@ class qtype_calculated_variable_substituter { ...@@ -424,7 +424,13 @@ class qtype_calculated_variable_substituter {
if ($error = qtype_calculated_find_formula_errors($expression)) { if ($error = qtype_calculated_find_formula_errors($expression)) {
throw new moodle_exception('illegalformulasyntax', 'qtype_calculated', '', $error); throw new moodle_exception('illegalformulasyntax', 'qtype_calculated', '', $error);
} }
return $this->calculate_raw($this->substitute_values_for_eval($expression)); $expression = $this->substitute_values_for_eval($expression);
if ($datasets = question_bank::get_qtype('calculated')->find_dataset_names($expression)) {
// Some placeholders were not substituted.
throw new moodle_exception('illegalformulasyntax', 'qtype_calculated', '',
'{' . reset($datasets) . '}');
}
return $this->calculate_raw($expression);
} }
/** /**
......
...@@ -99,6 +99,13 @@ class qtype_calculated_variable_substituter_test extends advanced_testcase { ...@@ -99,6 +99,13 @@ class qtype_calculated_variable_substituter_test extends advanced_testcase {
$this->assertEquals('= 3', $vs->replace_expressions_in_text('= {={a} + {b}}')); $this->assertEquals('= 3', $vs->replace_expressions_in_text('= {={a} + {b}}'));
} }
public function test_expression_has_unmapped_placeholder() {
$this->expectException('moodle_exception');
$this->expectExceptionMessage(get_string('illegalformulasyntax', 'qtype_calculated', '{c}'));
$vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
$vs->calculate('{c} - {a} + {b}');
}
public function test_replace_expressions_in_text_negative() { public function test_replace_expressions_in_text_negative() {
$vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => 2), '.'); $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => 2), '.');
$this->assertEquals('temperatures -1 and 2', $this->assertEquals('temperatures -1 and 2',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment