moodle_page: MDL-12212 more ->pagepaths to kill
[moodle.git] / lib / simpletest / testmoodlelib.php
CommitLineData
d499142e 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.org //
9// //
56a1a882 10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
d499142e 11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
7c9d46c9 26/**
27 * Unit tests for (some of) ../moodlelib.php.
28 *
bb45fe62 29 * Note, tests for get_string are in the separate file testgetstring.php.
30 *
7c9d46c9 31 * @copyright &copy; 2006 The Open University
32 * @author T.J.Hunt@open.ac.uk
d499142e 33 * @author nicolas@moodle.com
7c9d46c9 34 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
35 * @package moodlecore
36 */
37
cdbbd26f 38if (!defined('MOODLE_INTERNAL')) {
39 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
40}
7c9d46c9 41
7c9d46c9 42require_once($CFG->libdir . '/moodlelib.php');
43
cc4245a3 44class moodlelib_test extends UnitTestCase {
b9c639d6 45
a205dcdc 46 var $user_agents = array(
47 'MSIE' => array(
48 '5.5' => array('Windows 2000' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)'),
49 '6.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'),
ec012320 50 '7.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; YPC 3.0.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)'),
51 '8.0' => array('Windows Vista' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648)'),
52
b9c639d6 53 ),
a205dcdc 54 'Firefox' => array(
55 '1.0.6' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10) Gecko/20050716 Firefox/1.0.6'),
56 '1.5' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8) Gecko/20051107 Firefox/1.5'),
57 '1.5.0.1' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1'),
58 '2.0' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1',
ec012320 59 'Ubuntu Linux AMD64' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20060601 Firefox/2.0 (Ubuntu-edgy)'),
60 '3.0.6' => array('SUSE' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.6) Gecko/2009012700 SUSE/3.0.6-1.4 Firefox/3.0.6'),
a205dcdc 61 ),
62 'Safari' => array(
63 '312' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312'),
64 '2.0' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412')
65 ),
66 'Opera' => array(
67 '8.51' => array('Windows XP' => 'Opera/8.51 (Windows NT 5.1; U; en)'),
68 '9.0' => array('Windows XP' => 'Opera/9.0 (Windows NT 5.1; U; en)',
69 'Debian Linux' => 'Opera/9.01 (X11; Linux i686; U; en)')
70 )
71 );
b9c639d6 72
65de6864 73 function test_cleanremoteaddr() {
74 //IPv4
75 $this->assertEqual(cleanremoteaddr('1023.121.234.1'), null);
76 $this->assertEqual(cleanremoteaddr('123.121.234.01 '), '123.121.234.1');
77
78 //IPv6
79 $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:0:0'), null);
80 $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:abh'), null);
81 $this->assertEqual(cleanremoteaddr('0:0:0:::0:0:1'), null);
82 $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:0', true), '::');
83 $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:1:1', true), '::1:1');
84 $this->assertEqual(cleanremoteaddr('abcd:00ef:0:0:0:0:0:0', true), 'abcd:ef::');
85 $this->assertEqual(cleanremoteaddr('1:0:0:0:0:0:0:1', true), '1::1');
86 $this->assertEqual(cleanremoteaddr('::10:1', false), '0:0:0:0:0:0:10:1');
87 $this->assertEqual(cleanremoteaddr('01:1::', false), '1:1:0:0:0:0:0:0');
88 $this->assertEqual(cleanremoteaddr('10::10', false), '10:0:0:0:0:0:0:10');
89 $this->assertEqual(cleanremoteaddr('::ffff:192.168.1.1', true), '::ffff:c0a8:11');
90 }
91
7c9d46c9 92 function test_address_in_subnet() {
e70da470 93 /// 1: xxx.xxx.xxx.xxx/nn or xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/nnn (number of bits in net mask)
94 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.1/32'));
95 $this->assertFalse(address_in_subnet('123.121.23.1', '123.121.23.0/32'));
96 $this->assertTrue(address_in_subnet('10.10.10.100', '123.121.23.45/0'));
97 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.0/24'));
98 $this->assertFalse(address_in_subnet('123.121.34.1', '123.121.234.0/24'));
99 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.0/30'));
100 $this->assertFalse(address_in_subnet('123.121.23.8', '123.121.23.0/30'));
101 $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba/128'));
102 $this->assertFalse(address_in_subnet('bab:baba::baba', 'bab:baba::cece/128'));
103 $this->assertTrue(address_in_subnet('baba:baba::baba', 'cece:cece::cece/0'));
104 $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba/128'));
105 $this->assertTrue(address_in_subnet('baba:baba::00ba', 'baba:baba::/120'));
106 $this->assertFalse(address_in_subnet('baba:baba::aba', 'baba:baba::/120'));
107 $this->assertTrue(address_in_subnet('baba::baba:00ba', 'baba::baba:0/112'));
108 $this->assertFalse(address_in_subnet('baba::aba:00ba', 'baba::baba:0/112'));
109 $this->assertFalse(address_in_subnet('aba::baba:0000', 'baba::baba:0/112'));
110
111 // fixed input
112 $this->assertTrue(address_in_subnet('123.121.23.1 ', ' 123.121.23.0 / 24'));
113 $this->assertTrue(address_in_subnet('::ffff:10.1.1.1', ' 0:0:0:000:0:ffff:a1:10 / 126'));
114
115 // incorrect input
116 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/-2'));
117 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/64'));
118 $this->assertFalse(address_in_subnet('123.121.234.x', '123.121.234.1/24'));
119 $this->assertFalse(address_in_subnet('123.121.234.0', '123.121.234.xx/24'));
120 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/xx0'));
121 $this->assertFalse(address_in_subnet('::1', '::aa:0/xx0'));
122 $this->assertFalse(address_in_subnet('::1', '::aa:0/-5'));
123 $this->assertFalse(address_in_subnet('::1', '::aa:0/130'));
124 $this->assertFalse(address_in_subnet('x:1', '::aa:0/130'));
125 $this->assertFalse(address_in_subnet('::1', '::ax:0/130'));
126
127
128 /// 2: xxx.xxx.xxx.xxx-yyy or xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx::xxxx-yyyy (a range of IP addresses in the last group)
129 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12-14'));
130 $this->assertTrue(address_in_subnet('123.121.234.13', '123.121.234.12-14'));
131 $this->assertTrue(address_in_subnet('123.121.234.14', '123.121.234.12-14'));
132 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.12-14'));
133 $this->assertFalse(address_in_subnet('123.121.234.20', '123.121.234.12-14'));
134 $this->assertFalse(address_in_subnet('123.121.23.12', '123.121.234.12-14'));
135 $this->assertFalse(address_in_subnet('123.12.234.12', '123.121.234.12-14'));
136 $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba-babe'));
137 $this->assertTrue(address_in_subnet('baba:baba::babc', 'baba:baba::baba-babe'));
138 $this->assertTrue(address_in_subnet('baba:baba::babe', 'baba:baba::baba-babe'));
139 $this->assertFalse(address_in_subnet('bab:baba::bab0', 'bab:baba::baba-babe'));
140 $this->assertFalse(address_in_subnet('bab:baba::babf', 'bab:baba::baba-babe'));
141 $this->assertFalse(address_in_subnet('bab:baba::bfbe', 'bab:baba::baba-babe'));
142 $this->assertFalse(address_in_subnet('bfb:baba::babe', 'bab:baba::baba-babe'));
143
144 // fixed input
145 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12 - 14 '));
146 $this->assertTrue(address_in_subnet('bab:baba::babe', 'bab:baba::baba - babe '));
147
148 // incorrect input
149 $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12-234.14'));
150 $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12-256'));
151 $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12--256'));
152
153
154 /// 3: xxx.xxx or xxx.xxx. or xxx:xxx:xxxx or xxx:xxx:xxxx. (incomplete address, a bit non-technical ;-)
155 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12'));
156 $this->assertFalse(address_in_subnet('123.121.23.12', '123.121.23.13'));
157 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.'));
158 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234'));
159 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121'));
160 $this->assertTrue(address_in_subnet('123.121.234.12', '123'));
161 $this->assertFalse(address_in_subnet('123.121.234.1', '12.121.234.'));
162 $this->assertFalse(address_in_subnet('123.121.234.1', '12.121.234'));
163 $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:baba::bab'));
164 $this->assertFalse(address_in_subnet('baba:baba::ba', 'baba:baba::bc'));
165 $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:baba'));
166 $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:'));
167 $this->assertFalse(address_in_subnet('bab:baba::bab', 'baba:'));
168
169
170 /// multiple subnets
171 $this->assertTrue(address_in_subnet('123.121.234.12', '::1/64, 124., 123.121.234.10-30'));
172 $this->assertTrue(address_in_subnet('124.121.234.12', '::1/64, 124., 123.121.234.10-30'));
173 $this->assertTrue(address_in_subnet('::2', '::1/64, 124., 123.121.234.10-30'));
174 $this->assertFalse(address_in_subnet('12.121.234.12', '::1/64, 124., 123.121.234.10-30'));
175
176
177 /// other incorrect input
178 $this->assertFalse(address_in_subnet('123.123.123.123', ''));
7c9d46c9 179 }
b9c639d6 180
239c1576 181 /**
b9c639d6 182 * Modifies $_SERVER['HTTP_USER_AGENT'] manually to check if check_browser_version
239c1576 183 * works as expected.
184 */
185 function test_check_browser_version()
186 {
a205dcdc 187 global $CFG;
b9c639d6 188
189 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
239c1576 190 $this->assertTrue(check_browser_version('Safari', '312'));
191 $this->assertFalse(check_browser_version('Safari', '500'));
b9c639d6 192
a205dcdc 193 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
239c1576 194 $this->assertTrue(check_browser_version('Opera', '8.0'));
195 $this->assertFalse(check_browser_version('Opera', '10.0'));
b9c639d6 196
a205dcdc 197 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
239c1576 198 $this->assertTrue(check_browser_version('MSIE', '5.0'));
199 $this->assertFalse(check_browser_version('MSIE', '7.0'));
b9c639d6 200
a205dcdc 201 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
239c1576 202 $this->assertTrue(check_browser_version('Firefox', '1.5'));
b9c639d6 203 $this->assertFalse(check_browser_version('Firefox', '3.0'));
d499142e 204 }
b9c639d6 205
ec012320 206 function test_get_browser_version_classes() {
207 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
208 $this->assertEqual('safari ', get_browser_version_classes());
209
210 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
211 $this->assertEqual('opera ', get_browser_version_classes());
212
213 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
214 $this->assertEqual('ie ie6 ', get_browser_version_classes());
215
216 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['7.0']['Windows XP SP2'];
217 $this->assertEqual('ie ie7 ', get_browser_version_classes());
218
219 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['8.0']['Windows Vista'];
220 $this->assertEqual('ie ie8 ', get_browser_version_classes());
221
222 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
223 $this->assertEqual('gecko gecko18 ', get_browser_version_classes());
224
225 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['3.0.6']['SUSE'];
226 $this->assertEqual('gecko gecko19 ', get_browser_version_classes());
227 }
228
facb512c 229 function test_optional_param()
230 {
b9c639d6 231 $_POST['username'] = 'post_user';
facb512c 232 $_GET['username'] = 'get_user';
233 $this->assertEqual(optional_param('username', 'default_user'), 'post_user');
b9c639d6 234
facb512c 235 unset($_POST['username']);
236 $this->assertEqual(optional_param('username', 'default_user'), 'get_user');
b9c639d6 237
facb512c 238 unset($_GET['username']);
239 $this->assertEqual(optional_param('username', 'default_user'), 'default_user');
240 }
b9c639d6 241
facb512c 242 /**
243 * Used by {@link optional_param()} and {@link required_param()} to
244 * clean the variables and/or cast to specific types, based on
245 * an options field.
246 * <code>
247 * $course->format = clean_param($course->format, PARAM_ALPHA);
248 * $selectedgrade_item = clean_param($selectedgrade_item, PARAM_CLEAN);
249 * </code>
250 *
251 * @uses $CFG
252 * @uses PARAM_CLEAN
253 * @uses PARAM_INT
254 * @uses PARAM_INTEGER
255 * @uses PARAM_ALPHA
256 * @uses PARAM_ALPHANUM
257 * @uses PARAM_NOTAGS
258 * @uses PARAM_ALPHAEXT
259 * @uses PARAM_BOOL
260 * @uses PARAM_SAFEDIR
facb512c 261 * @uses PARAM_FILE
262 * @uses PARAM_PATH
263 * @uses PARAM_HOST
264 * @uses PARAM_URL
265 * @uses PARAM_LOCALURL
266 * @uses PARAM_CLEANHTML
267 * @uses PARAM_SEQUENCE
268 * @param mixed $param the variable we are cleaning
269 * @param int $type expected format of param after cleaning.
270 * @return mixed
271 */
272 function test_clean_param()
273 {
c7fd861b 274 global $CFG;
facb512c 275 // Test unknown parameter type
b9c639d6 276
facb512c 277 // Test Raw param
b9c639d6 278 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_RAW),
facb512c 279 '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)');
b9c639d6 280
281 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_CLEAN),
65de6864 282 '#()*#,9789\'".,');
c7fd861b 283
284 // Test PARAM_URL and PARAM_LOCALURL a bit
285 $this->assertEqual(clean_param('http://google.com/', PARAM_URL), 'http://google.com/');
286 $this->assertEqual(clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_URL), 'http://some.very.long.and.silly.domain/with/a/path/');
287 $this->assertEqual(clean_param('http://localhost/', PARAM_URL), 'http://localhost/');
288 $this->assertEqual(clean_param('http://0.255.1.1/numericip.php', PARAM_URL), 'http://0.255.1.1/numericip.php');
289 $this->assertEqual(clean_param('/just/a/path', PARAM_URL), '/just/a/path');
290 $this->assertEqual(clean_param('funny:thing', PARAM_URL), '');
291
292 $this->assertEqual(clean_param('http://google.com/', PARAM_LOCALURL), '');
293 $this->assertEqual(clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_LOCALURL), '');
294 $this->assertEqual(clean_param($CFG->wwwroot, PARAM_LOCALURL), $CFG->wwwroot);
295 $this->assertEqual(clean_param('/just/a/path', PARAM_LOCALURL), '/just/a/path');
296 $this->assertEqual(clean_param('funny:thing', PARAM_LOCALURL), '');
facb512c 297 }
56a1a882 298
299 function test_make_user_directory() {
300 global $CFG;
301
302 // Test success conditions
303 $this->assertEqual("$CFG->dataroot/user/0/0", make_user_directory(0, true));
304 $this->assertEqual("$CFG->dataroot/user/0/1", make_user_directory(1, true));
305 $this->assertEqual("$CFG->dataroot/user/0/999", make_user_directory(999, true));
306 $this->assertEqual("$CFG->dataroot/user/1000/1000", make_user_directory(1000, true));
307 $this->assertEqual("$CFG->dataroot/user/2147483000/2147483647", make_user_directory(2147483647, true)); // Largest int possible
308
309 // Test fail conditions
310 $this->assertFalse(make_user_directory(2147483648, true)); // outside int boundary
311 $this->assertFalse(make_user_directory(-1, true));
312 $this->assertFalse(make_user_directory('string', true));
313 $this->assertFalse(make_user_directory(false, true));
314 $this->assertFalse(make_user_directory(true, true));
b9c639d6 315
56a1a882 316 }
7c9d46c9 317}
318
319?>