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 core_form;
  18  
  19  use MoodleQuickForm_float;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  require_once($CFG->libdir . '/form/float.php');
  25  
  26  /**
  27   * Unit tests for MoodleQuickForm_float
  28   *
  29   * Contains test cases for testing MoodleQuickForm_float
  30   *
  31   * @package    core_form
  32   * @category   test
  33   * @copyright  2019 Shamim Rezaie <shamim@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class float_test extends \advanced_testcase {
  37  
  38      /**
  39       * Define a local decimal separator.
  40       *
  41       * It is not possible to directly change the result of get_string in
  42       * a unit test. Instead, we create a language pack for language 'xx' in
  43       * dataroot and make langconfig.php with the string we need to change.
  44       * The example separator used here is 'X'.
  45       */
  46      protected function define_local_decimal_separator() {
  47          global $SESSION, $CFG;
  48  
  49          $SESSION->lang = 'xx';
  50          $langconfig = "<?php\n\$string['decsep'] = 'X';";
  51          $langfolder = $CFG->dataroot . '/lang/xx';
  52          check_dir_exists($langfolder);
  53          file_put_contents($langfolder . '/langconfig.php', $langconfig);
  54      }
  55  
  56      /**
  57       * Testcase to check generated timestamp
  58       */
  59      public function test_exportValue() {
  60          $element = new MoodleQuickForm_float('testel');
  61  
  62          $value = ['testel' => 3.14];
  63          $this->assertEquals(3.14, $element->exportValue($value));
  64  
  65          $value = ['testel' => '3.14'];
  66          $this->assertEquals(3.14, $element->exportValue($value));
  67  
  68          $value = ['testel' => '-3.14'];
  69          $this->assertEquals(-3.14, $element->exportValue($value));
  70  
  71          $value = ['testel' => '3.14blah'];
  72          $this->assertEquals(false, $element->exportValue($value));
  73  
  74          $value = ['testel' => 'blah'];
  75          $this->assertEquals(false, $element->exportValue($value));
  76  
  77          // Tests with a localised decimal separator.
  78          $this->define_local_decimal_separator();
  79  
  80          $value = ['testel' => 3.14];
  81          $this->assertEquals(3.14, $element->exportValue($value));
  82  
  83          $value = ['testel' => '3X14'];
  84          $this->assertEquals(3.14, $element->exportValue($value));
  85  
  86          $value = ['testel' => '-3X14'];
  87          $this->assertEquals(-3.14, $element->exportValue($value));
  88  
  89          $value = ['testel' => '3X14blah'];
  90          $this->assertEquals(false, $element->exportValue($value));
  91  
  92          $value = ['testel' => 'blah'];
  93          $this->assertEquals(false, $element->exportValue($value));
  94      }
  95  }