MDL-62469 qtype_calculated: check remaining placeholders, see MDL-62275
authorMarina Glancy <marina@moodle.com>
Wed, 16 May 2018 07:25:11 +0000 (15:25 +0800)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Wed, 16 May 2018 15:20:38 +0000 (17:20 +0200)
question/type/calculated/question.php
question/type/calculated/tests/variablesubstituter_test.php

index 6fb1cf1..2936c05 100644 (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);
     }
 
     /**
index 0a66ad8..a42d426 100644 (file)
@@ -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',