Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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 mod_lesson;
  18  
  19  use mod_lesson\local\numeric\helper;
  20  
  21  /**
  22   * This class contains the test cases for the numeric helper functions
  23   *
  24   * @package   mod_lesson
  25   * @category  test
  26   * @copyright 2020 Peter Dias
  27   * @license   http://www.gnu.org/copyleft/gpl.html GNU Public License
  28   */
  29  class numeric_helper_test extends \advanced_testcase {
  30      /**
  31       * Test the lesson_unformat_numeric_value function.
  32       *
  33       * @dataProvider lesson_unformat_dataprovider
  34       * @param $decsep
  35       * @param $tests
  36       */
  37      public function test_lesson_unformat_numeric_value($decsep, $tests) {
  38          $this->define_local_decimal_separator($decsep);
  39  
  40          foreach ($tests as $test) {
  41              $this->assertEquals($test[1], helper::lesson_unformat_numeric_value($test[0]));
  42          }
  43      }
  44  
  45      /**
  46       * Test the lesson_format_numeric_value function.
  47       *
  48       * @dataProvider lesson_format_dataprovider
  49       * @param $decsep
  50       * @param $tests
  51       */
  52      public function test_lesson_format_numeric_value($decsep, $tests) {
  53          $this->define_local_decimal_separator($decsep);
  54  
  55          foreach ($tests as $test) {
  56              $this->assertEquals($test[1], helper::lesson_format_numeric_value($test[0]));
  57          }
  58      }
  59  
  60      /**
  61       * Provide various cases for the unformat test function
  62       *
  63       * @return array
  64       */
  65      public function lesson_unformat_dataprovider() {
  66          return [
  67              "Using a decimal as a separator" => [
  68                  "decsep" => ".",
  69                  "test" => [
  70                      ["2.1", 2.1],
  71                      ["1:4.2", "1:4.2"],
  72                      ["2,1", 2],
  73                      ["1:4,2", "1:4"],
  74                      ["", null]
  75                  ]
  76              ],
  77              "Using a comma as a separator" => [
  78                  "decsep" => ",",
  79                  "test" => [
  80                      ["2,1", 2.1],
  81                      ["1:4,2", "1:4.2"],
  82                      ["2.1", 2.1],
  83                      ["1:4.2", "1:4.2"],
  84                  ]
  85              ],
  86              "Using a X as a separator" => [
  87                  "decsep" => "X",
  88                  "test" => [
  89                      ["2X1", 2.1],
  90                      ["1:4X2", "1:4.2"],
  91                      ["2.1", 2.1],
  92                      ["1:4.2", "1:4.2"],
  93                  ]
  94              ]
  95          ];
  96      }
  97  
  98      /**
  99       * Provide various cases for the unformat test function
 100       *
 101       * @return array
 102       */
 103      public function lesson_format_dataprovider() {
 104          return [
 105              "Using a decimal as a separator" => [
 106                  "decsep" => ".",
 107                  "test" => [
 108                      ["2.1", 2.1],
 109                      ["1:4.2", "1:4.2"],
 110                      ["2,1", "2,1"],
 111                      ["1:4,2", "1:4,2"]
 112                  ]
 113              ],
 114              "Using a comma as a separator" => [
 115                  "decsep" => ",",
 116                  "test" => [
 117                      ["2,1", "2,1"],
 118                      ["1:4,2", "1:4,2"],
 119                      ["2.1", "2,1"],
 120                      [2.1, "2,1"],
 121                      ["1:4.2", "1:4,2"],
 122                  ]
 123              ],
 124              "Using a X as a separator" => [
 125                  "decsep" => "X",
 126                  "test" => [
 127                      ["2X1", "2X1"],
 128                      ["1:4X2", "1:4X2"],
 129                      ["2.1", "2X1"],
 130                      ["1:4.2", "1:4X2"],
 131                  ]
 132              ]
 133          ];
 134      }
 135  
 136  
 137      /**
 138       * Define a local decimal separator.
 139       *
 140       * It is not possible to directly change the result of get_string in
 141       * a unit test. Instead, we create a language pack for language 'xx' in
 142       * dataroot and make langconfig.php with the string we need to change.
 143       * The default example separator used here is 'X'.
 144       *
 145       * @param string $decsep Separator character. Defaults to `'X'`.
 146       */
 147      protected function define_local_decimal_separator(string $decsep = 'X') {
 148          global $SESSION, $CFG;
 149  
 150          $SESSION->lang = 'xx';
 151          $langconfig = "<?php\n\$string['decsep'] = '$decsep';";
 152          $langfolder = $CFG->dataroot . '/lang/xx';
 153          check_dir_exists($langfolder);
 154          file_put_contents($langfolder . '/langconfig.php', $langconfig);
 155  
 156          // Ensure the new value is picked up and not taken from the cache.
 157          $stringmanager = get_string_manager();
 158          $stringmanager->reset_caches(true);
 159      }
 160  }