Merge branch 'MDL-73883-master' of https://github.com/andrewnicols/moodle
[moodle.git] / lib / tests / configonlylib_test.php
blob2dc10cca40c2f8f6f797088efd8cbafbedaef207
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.
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/>.
17 /**
18 * Unit tests for config only library functions-
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
26 defined('MOODLE_INTERNAL') || die();
28 // Global $CFG not used here intentionally to make sure it is not required inside the lib.
29 require_once(__DIR__ . '/../configonlylib.php');
32 /**
33 * Unit tests for config only library functions.
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
40 class core_configonlylib_testcase extends advanced_testcase {
42 /**
43 * Test cleaning of invalid utf-8 entities.
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.');
51 /**
52 * Test minimalistic parameter cleaning.
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'));
58 $this->assertSame(1, min_clean_param('1', 'INT'));
59 $this->assertSame(1, min_clean_param('1aa', 'INT'));
61 $this->assertSame('1abc-d_f', min_clean_param('/.1ačž"b?;c-d{}\\_f.', 'SAFEDIR'));
62 $this->assertSame(1, min_clean_param('1aa', 'INT'));
64 $this->assertSame('/a/b/./c5', min_clean_param('/a*?$//b/.../c5', 'SAFEPATH'));
65 $this->assertSame(1, min_clean_param('1aa', 'INT'));
68 /**
69 * Test minimalistic getting of page parameters.
71 public function test_min_optional_param() {
72 $this->resetAfterTest();
74 $_GET['foo'] = 'bar';
75 $_GET['num'] = '1';
76 $_GET['xnum'] = '1aa';
78 $_POST['foo'] = 'rebar';
79 $_POST['oof'] = 'rab';
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'));
85 $this->assertSame(1, min_optional_param('num', null, 'INT'));
86 $this->assertSame(1, min_optional_param('xnum', null, 'INT'));
89 /**
90 * Test fail-safe minimalistic slashargument processing.
92 public function test_min_get_slash_argument() {
93 global $CFG;
95 $this->resetAfterTest();
96 $this->assertEquals('https://www.example.com/moodle', $CFG->wwwroot);
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());
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());
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());
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());
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());
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());