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] [Versions 39 and 310]

   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 config only library functions-
  19   *
  20   * @package    core
  21   * @category   phpunit
  22   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  // Global $CFG not used here intentionally to make sure it is not required inside the lib.
  29  require_once (__DIR__ . '/../configonlylib.php');
  30  
  31  
  32  /**
  33   * Unit tests for config only library functions.
  34   *
  35   * @package    core
  36   * @category   phpunit
  37   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class core_configonlylib_testcase extends advanced_testcase {
  41  
  42      /**
  43       * Test cleaning of invalid utf-8 entities.
  44       */
  45      public function test_min_fix_utf8() {
  46          $this->assertSame('abc', min_fix_utf8('abc'));
  47          $this->assertSame("žlutý koníček přeskočil potůček \n\t\r", min_fix_utf8("žlutý koníček přeskočil potůček \n\t\r\0"));
  48          $this->assertSame('aš', min_fix_utf8('a'.chr(130).'š'), 'This fails with buggy iconv() when mbstring extenstion is not available as fallback.');
  49      }
  50  
  51      /**
  52       * Test minimalistic parameter cleaning.
  53       */
  54      public function test_min_clean_param() {
  55          $this->assertSame('foo', min_clean_param('foo', 'RAW'));
  56          $this->assertSame('aš', min_clean_param('a'.chr(130).'š', 'RAW'));
  57  
  58          $this->assertSame(1, min_clean_param('1', 'INT'));
  59          $this->assertSame(1, min_clean_param('1aa', 'INT'));
  60  
  61          $this->assertSame('1abc-d_f', min_clean_param('/.1ačž"b?;c-d{}\\_f.', 'SAFEDIR'));
  62          $this->assertSame(1, min_clean_param('1aa', 'INT'));
  63  
  64          $this->assertSame('/a/b/./c5', min_clean_param('/a*?$//b/.../c5', 'SAFEPATH'));
  65          $this->assertSame(1, min_clean_param('1aa', 'INT'));
  66      }
  67  
  68      /**
  69       * Test minimalistic getting of page parameters.
  70       */
  71      public function test_min_optional_param() {
  72          $this->resetAfterTest();
  73  
  74          $_GET['foo'] = 'bar';
  75          $_GET['num'] = '1';
  76          $_GET['xnum'] = '1aa';
  77  
  78          $_POST['foo'] = 'rebar';
  79          $_POST['oof'] = 'rab';
  80  
  81          $this->assertSame('bar', min_optional_param('foo', null, 'RAW'));
  82          $this->assertSame(null, min_optional_param('foo2', null, 'RAW'));
  83          $this->assertSame('rab', min_optional_param('oof', null, 'RAW'));
  84  
  85          $this->assertSame(1, min_optional_param('num', null, 'INT'));
  86          $this->assertSame(1, min_optional_param('xnum', null, 'INT'));
  87      }
  88  
  89      /**
  90       * Test fail-safe minimalistic slashargument processing.
  91       */
  92      public function test_min_get_slash_argument() {
  93          global $CFG;
  94  
  95          $this->resetAfterTest();
  96          $this->assertEquals('https://www.example.com/moodle', $CFG->wwwroot);
  97  
  98          $_SERVER = array();
  99          $_SERVER['SERVER_SOFTWARE'] = 'Apache/2.2.22 (Unix)';
 100          $_SERVER['QUERY_STRING'] = 'theme=standard&component=core&rev=5&image=u/f1';
 101          $_SERVER['REQUEST_URI'] = '/moodle/theme/image.php?theme=standard&component=core&rev=5&image=u/f1';
 102          $_SERVER['SCRIPT_NAME'] = '/moodle/theme/image.php';
 103          $this->assertSame('', min_get_slash_argument());
 104  
 105          $_SERVER = array();
 106          $_SERVER['SERVER_SOFTWARE'] = 'Apache/2.2.22 (Unix)';
 107          $_SERVER['QUERY_STRING'] = '';
 108          $_SERVER['REQUEST_URI'] = '/moodle/theme/image.php/standard/core/5/u/f1';
 109          $_SERVER['PATH_INFO'] = '/standard/core/5/u/f1';
 110          $_SERVER['SCRIPT_NAME'] = '/moodle/theme/image.php';
 111          $_GET = array();
 112          $this->assertSame('/standard/core/5/u/f1', min_get_slash_argument());
 113  
 114          // IIS no url rewriting.
 115          $_SERVER = array();
 116          $_SERVER['SERVER_SOFTWARE'] = 'Microsoft-IIS/7.0';
 117          $_SERVER['QUERY_STRING'] = '';
 118          $_SERVER['REQUEST_URI'] = '/moodle/theme/image.php/standard/core/5/u/f1';
 119          $_SERVER['PATH_INFO'] = '/standard/core/5/u/f1';
 120          $_SERVER['SCRIPT_NAME'] = '/moodle/theme/image.php';
 121          $_GET = array();
 122          $this->assertSame('/standard/core/5/u/f1', min_get_slash_argument());
 123  
 124          // IIS with url rewriting.
 125          $_SERVER = array();
 126          $_SERVER['SERVER_SOFTWARE'] = 'Microsoft-IIS/7.0';
 127          $_SERVER['QUERY_STRING'] = 'file=/standard/core/5/u/f1';
 128          $_SERVER['REQUEST_URI'] = '/moodle/theme/image.php/standard/core/5/u/f1';
 129          $_SERVER['PATH_INFO'] = '/';
 130          $_SERVER['SCRIPT_NAME'] = '/moodle/theme/image.php';
 131          $_GET = array();
 132          $_GET['file'] = '/standard/core/5/u/f1';
 133          $this->assertSame('/standard/core/5/u/f1', min_get_slash_argument());
 134  
 135          $_SERVER = array();
 136          $_SERVER['SERVER_SOFTWARE'] = 'Weird server';
 137          $_SERVER['QUERY_STRING'] = '';
 138          $_SERVER['REQUEST_URI'] = '/moodle/theme/image.php/standard/core/5/u/f1';
 139          $_SERVER['PATH_INFO'] = '/moodle/theme/image.php/standard/core/5/u/f1';
 140          $_SERVER['SCRIPT_NAME'] = '/moodle/theme/image.php';
 141          $_GET = array();
 142          $this->assertSame('/standard/core/5/u/f1', min_get_slash_argument());
 143  
 144          $_SERVER = array();
 145          $_SERVER['SERVER_SOFTWARE'] = 'Hacker server';
 146          $_SERVER['QUERY_STRING'] = '';
 147          $_SERVER['REQUEST_URI'] = '/moodle/theme/image.php/standard/core/5/u/f1';
 148          $_SERVER['PATH_INFO'] = '/moodle/theme/image.php/standard\\core/..\\../5/u/f1';
 149          $_SERVER['SCRIPT_NAME'] = '/moodle/theme/image.php';
 150          $_GET = array();
 151          // Windows dir separators are removed, multiple ... gets collapsed to one .
 152          $this->assertSame('/standardcore/./5/u/f1', min_get_slash_argument());
 153      }
 154  }