Search moodle.org's
Developer Documentation

See Release Notes

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