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

This commit is contained in:
Marina Glancy
2018-05-16 17:53:11 +02:00
committed by Eloy Lafuente (stronk7)
parent 52b4e37154
commit 9dd3dd7e9d
2 changed files with 14 additions and 1 deletions
+7 -1
View File
@@ -424,7 +424,13 @@ class qtype_calculated_variable_substituter {
if ($error = qtype_calculated_find_formula_errors($expression)) {
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 {
$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() {
$vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => 2), '.');
$this->assertEquals('temperatures -1 and 2',