Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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 qtype_calculated_variable_substituter.
  19   *
  20   * @package    qtype
  21   * @subpackage calculated
  22   * @copyright  2011 The Open University
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once($CFG->dirroot . '/question/type/calculated/question.php');
  31  require_once($CFG->dirroot . '/question/type/calculated/questiontype.php');
  32  
  33  
  34  /**
  35   * Unit tests for {@link qtype_calculated_variable_substituter}.
  36   *
  37   * @copyright  2011 The Open University
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class qtype_calculated_variable_substituter_test extends advanced_testcase {
  41      public function test_simple_expression() {
  42          $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
  43          $this->assertEquals(3, $vs->calculate('{a} + {b}'));
  44      }
  45  
  46      public function test_simple_expression_negatives() {
  47          $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => -2), '.');
  48          $this->assertEquals(1, $vs->calculate('{a}-{b}'));
  49      }
  50  
  51      /**
  52       * @expectedException moodle_exception
  53       */
  54      public function test_cannot_use_nonnumbers() {
  55          $vs = new qtype_calculated_variable_substituter(array('a' => 'frog', 'b' => -2), '.');
  56      }
  57  
  58      /**
  59       * @expectedException moodle_exception
  60       */
  61      public function test_invalid_expression() {
  62          $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
  63          $vs->calculate('{a} + {b}?');
  64      }
  65  
  66      /**
  67       * @expectedException moodle_exception
  68       */
  69      public function test_tricky_invalid_expression() {
  70          $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
  71          $vs->calculate('{a}{b}'); // Have to make sure this does not just evaluate to 12.
  72      }
  73  
  74      /**
  75       * @expectedException moodle_exception
  76       */
  77      public function test_division_by_zero_expression() {
  78  
  79          if (intval(PHP_VERSION) < 7) {
  80              $this->markTestSkipped('Division by zero triggers a PHP warning before PHP 7.');
  81          }
  82  
  83          $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 0), '.');
  84          $vs->calculate('{a} / {b}');
  85      }
  86  
  87      public function test_replace_expressions_in_text_simple_var() {
  88          $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
  89          $this->assertEquals('1 + 2', $vs->replace_expressions_in_text('{a} + {b}'));
  90      }
  91  
  92      public function test_replace_expressions_in_confusing_text() {
  93          $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
  94          $this->assertEquals("(1) 1\n(2) 2", $vs->replace_expressions_in_text("(1) {a}\n(2) {b}"));
  95      }
  96  
  97      public function test_replace_expressions_in_text_formula() {
  98          $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
  99          $this->assertEquals('= 3', $vs->replace_expressions_in_text('= {={a} + {b}}'));
 100      }
 101  
 102      public function test_expression_has_unmapped_placeholder() {
 103          $this->expectException('moodle_exception');
 104          $this->expectExceptionMessage(get_string('illegalformulasyntax', 'qtype_calculated', '{c}'));
 105          $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
 106          $vs->calculate('{c} - {a} + {b}');
 107      }
 108  
 109      public function test_replace_expressions_in_text_negative() {
 110          $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => 2), '.');
 111          $this->assertEquals('temperatures -1 and 2',
 112                  $vs->replace_expressions_in_text('temperatures {a} and {b}'));
 113      }
 114  
 115      public function test_replace_expressions_in_text_commas_for_decimals() {
 116          $vs = new qtype_calculated_variable_substituter(
 117                  array('phi' => 1.61803399, 'pi' => 3.14159265), ',');
 118          $this->assertEquals('phi (1,61803399) + pi (3,14159265) = 4,75962664',
 119                  $vs->replace_expressions_in_text('phi ({phi}) + pi ({pi}) = {={phi} + {pi}}'));
 120      }
 121  
 122      public function test_format_float_dot() {
 123          $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => 2), '.');
 124          $this->assertSame('0.12345', $vs->format_float(0.12345));
 125  
 126          $this->assertSame('0', $vs->format_float(0.12345, 0, 1));
 127          $this->assertSame('0.12', $vs->format_float(0.12345, 2, 1));
 128          $this->assertSame('0.1235', $vs->format_float(0.12345, 4, 1));
 129  
 130          $this->assertSame('0.12', $vs->format_float(0.12345, 2, 2));
 131          $this->assertSame('0.0012', $vs->format_float(0.0012345, 4, 1));
 132      }
 133  
 134      public function test_format_float_comma() {
 135          $vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => 2), ',');
 136          $this->assertSame('0,12345', $vs->format_float(0.12345));
 137  
 138          $this->assertSame('0', $vs->format_float(0.12345, 0, 1));
 139          $this->assertSame('0,12', $vs->format_float(0.12345, 2, 1));
 140          $this->assertSame('0,1235', $vs->format_float(0.12345, 4, 1));
 141  
 142          $this->assertSame('0,12', $vs->format_float(0.12345, 2, 2));
 143          $this->assertSame('0,0012', $vs->format_float(0.0012345, 4, 1));
 144      }
 145  }