MDL-18190
[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 *
29 * @copyright &copy; 2006 The Open University
30 * @author T.J.Hunt@open.ac.uk
d499142e 31 * @author nicolas@moodle.com
7c9d46c9 32 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
33 * @package moodlecore
34 */
35
cdbbd26f 36if (!defined('MOODLE_INTERNAL')) {
37 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
38}
7c9d46c9 39
7c9d46c9 40require_once($CFG->libdir . '/moodlelib.php');
41
cc4245a3 42class moodlelib_test extends UnitTestCase {
b9c639d6 43
a205dcdc 44 var $user_agents = array(
45 'MSIE' => array(
46 '5.5' => array('Windows 2000' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)'),
47 '6.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'),
48 '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)')
b9c639d6 49 ),
a205dcdc 50 'Firefox' => array(
51 '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'),
52 '1.5' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8) Gecko/20051107 Firefox/1.5'),
53 '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'),
54 '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',
55 'Ubuntu Linux AMD64' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20060601 Firefox/2.0 (Ubuntu-edgy)')
56 ),
57 'Safari' => array(
58 '312' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312'),
59 '2.0' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412')
60 ),
61 'Opera' => array(
62 '8.51' => array('Windows XP' => 'Opera/8.51 (Windows NT 5.1; U; en)'),
63 '9.0' => array('Windows XP' => 'Opera/9.0 (Windows NT 5.1; U; en)',
64 'Debian Linux' => 'Opera/9.01 (X11; Linux i686; U; en)')
65 )
66 );
b9c639d6 67
7c9d46c9 68 function setUp() {
69 }
70
71 function tearDown() {
72 }
73
65de6864 74 function test_cleanremoteaddr() {
75 //IPv4
76 $this->assertEqual(cleanremoteaddr('1023.121.234.1'), null);
77 $this->assertEqual(cleanremoteaddr('123.121.234.01 '), '123.121.234.1');
78
79 //IPv6
80 $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:0:0'), null);
81 $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:abh'), null);
82 $this->assertEqual(cleanremoteaddr('0:0:0:::0:0:1'), null);
83 $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:0', true), '::');
84 $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:1:1', true), '::1:1');
85 $this->assertEqual(cleanremoteaddr('abcd:00ef:0:0:0:0:0:0', true), 'abcd:ef::');
86 $this->assertEqual(cleanremoteaddr('1:0:0:0:0:0:0:1', true), '1::1');
87 $this->assertEqual(cleanremoteaddr('::10:1', false), '0:0:0:0:0:0:10:1');
88 $this->assertEqual(cleanremoteaddr('01:1::', false), '1:1:0:0:0:0:0:0');
89 $this->assertEqual(cleanremoteaddr('10::10', false), '10:0:0:0:0:0:0:10');
90 $this->assertEqual(cleanremoteaddr('::ffff:192.168.1.1', true), '::ffff:c0a8:11');
91 }
92
7c9d46c9 93 function test_address_in_subnet() {
e70da470 94 /// 1: xxx.xxx.xxx.xxx/nn or xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/nnn (number of bits in net mask)
95 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.1/32'));
96 $this->assertFalse(address_in_subnet('123.121.23.1', '123.121.23.0/32'));
97 $this->assertTrue(address_in_subnet('10.10.10.100', '123.121.23.45/0'));
98 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.0/24'));
99 $this->assertFalse(address_in_subnet('123.121.34.1', '123.121.234.0/24'));
100 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.0/30'));
101 $this->assertFalse(address_in_subnet('123.121.23.8', '123.121.23.0/30'));
102 $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba/128'));
103 $this->assertFalse(address_in_subnet('bab:baba::baba', 'bab:baba::cece/128'));
104 $this->assertTrue(address_in_subnet('baba:baba::baba', 'cece:cece::cece/0'));
105 $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba/128'));
106 $this->assertTrue(address_in_subnet('baba:baba::00ba', 'baba:baba::/120'));
107 $this->assertFalse(address_in_subnet('baba:baba::aba', 'baba:baba::/120'));
108 $this->assertTrue(address_in_subnet('baba::baba:00ba', 'baba::baba:0/112'));
109 $this->assertFalse(address_in_subnet('baba::aba:00ba', 'baba::baba:0/112'));
110 $this->assertFalse(address_in_subnet('aba::baba:0000', 'baba::baba:0/112'));
111
112 // fixed input
113 $this->assertTrue(address_in_subnet('123.121.23.1 ', ' 123.121.23.0 / 24'));
114 $this->assertTrue(address_in_subnet('::ffff:10.1.1.1', ' 0:0:0:000:0:ffff:a1:10 / 126'));
115
116 // incorrect input
117 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/-2'));
118 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/64'));
119 $this->assertFalse(address_in_subnet('123.121.234.x', '123.121.234.1/24'));
120 $this->assertFalse(address_in_subnet('123.121.234.0', '123.121.234.xx/24'));
121 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/xx0'));
122 $this->assertFalse(address_in_subnet('::1', '::aa:0/xx0'));
123 $this->assertFalse(address_in_subnet('::1', '::aa:0/-5'));
124 $this->assertFalse(address_in_subnet('::1', '::aa:0/130'));
125 $this->assertFalse(address_in_subnet('x:1', '::aa:0/130'));
126 $this->assertFalse(address_in_subnet('::1', '::ax:0/130'));
127
128
129 /// 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)
130 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12-14'));
131 $this->assertTrue(address_in_subnet('123.121.234.13', '123.121.234.12-14'));
132 $this->assertTrue(address_in_subnet('123.121.234.14', '123.121.234.12-14'));
133 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.12-14'));
134 $this->assertFalse(address_in_subnet('123.121.234.20', '123.121.234.12-14'));
135 $this->assertFalse(address_in_subnet('123.121.23.12', '123.121.234.12-14'));
136 $this->assertFalse(address_in_subnet('123.12.234.12', '123.121.234.12-14'));
137 $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba-babe'));
138 $this->assertTrue(address_in_subnet('baba:baba::babc', 'baba:baba::baba-babe'));
139 $this->assertTrue(address_in_subnet('baba:baba::babe', 'baba:baba::baba-babe'));
140 $this->assertFalse(address_in_subnet('bab:baba::bab0', 'bab:baba::baba-babe'));
141 $this->assertFalse(address_in_subnet('bab:baba::babf', 'bab:baba::baba-babe'));
142 $this->assertFalse(address_in_subnet('bab:baba::bfbe', 'bab:baba::baba-babe'));
143 $this->assertFalse(address_in_subnet('bfb:baba::babe', 'bab:baba::baba-babe'));
144
145 // fixed input
146 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12 - 14 '));
147 $this->assertTrue(address_in_subnet('bab:baba::babe', 'bab:baba::baba - babe '));
148
149 // incorrect input
150 $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12-234.14'));
151 $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12-256'));
152 $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12--256'));
153
154
155 /// 3: xxx.xxx or xxx.xxx. or xxx:xxx:xxxx or xxx:xxx:xxxx. (incomplete address, a bit non-technical ;-)
156 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12'));
157 $this->assertFalse(address_in_subnet('123.121.23.12', '123.121.23.13'));
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.234'));
160 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121'));
161 $this->assertTrue(address_in_subnet('123.121.234.12', '123'));
162 $this->assertFalse(address_in_subnet('123.121.234.1', '12.121.234.'));
163 $this->assertFalse(address_in_subnet('123.121.234.1', '12.121.234'));
164 $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:baba::bab'));
165 $this->assertFalse(address_in_subnet('baba:baba::ba', 'baba:baba::bc'));
166 $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:baba'));
167 $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:'));
168 $this->assertFalse(address_in_subnet('bab:baba::bab', 'baba:'));
169
170
171 /// multiple subnets
172 $this->assertTrue(address_in_subnet('123.121.234.12', '::1/64, 124., 123.121.234.10-30'));
173 $this->assertTrue(address_in_subnet('124.121.234.12', '::1/64, 124., 123.121.234.10-30'));
174 $this->assertTrue(address_in_subnet('::2', '::1/64, 124., 123.121.234.10-30'));
175 $this->assertFalse(address_in_subnet('12.121.234.12', '::1/64, 124., 123.121.234.10-30'));
176
177
178 /// other incorrect input
179 $this->assertFalse(address_in_subnet('123.123.123.123', ''));
7c9d46c9 180 }
b9c639d6 181
239c1576 182 /**
b9c639d6 183 * Modifies $_SERVER['HTTP_USER_AGENT'] manually to check if check_browser_version
239c1576 184 * works as expected.
185 */
186 function test_check_browser_version()
187 {
a205dcdc 188 global $CFG;
b9c639d6 189
190 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
239c1576 191 $this->assertTrue(check_browser_version('Safari', '312'));
192 $this->assertFalse(check_browser_version('Safari', '500'));
b9c639d6 193
a205dcdc 194 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
239c1576 195 $this->assertTrue(check_browser_version('Opera', '8.0'));
196 $this->assertFalse(check_browser_version('Opera', '10.0'));
b9c639d6 197
a205dcdc 198 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
239c1576 199 $this->assertTrue(check_browser_version('MSIE', '5.0'));
200 $this->assertFalse(check_browser_version('MSIE', '7.0'));
b9c639d6 201
a205dcdc 202 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
239c1576 203 $this->assertTrue(check_browser_version('Firefox', '1.5'));
b9c639d6 204 $this->assertFalse(check_browser_version('Firefox', '3.0'));
d499142e 205 }
b9c639d6 206
facb512c 207 function test_optional_param()
208 {
b9c639d6 209 $_POST['username'] = 'post_user';
facb512c 210 $_GET['username'] = 'get_user';
211 $this->assertEqual(optional_param('username', 'default_user'), 'post_user');
b9c639d6 212
facb512c 213 unset($_POST['username']);
214 $this->assertEqual(optional_param('username', 'default_user'), 'get_user');
b9c639d6 215
facb512c 216 unset($_GET['username']);
217 $this->assertEqual(optional_param('username', 'default_user'), 'default_user');
218 }
b9c639d6 219
facb512c 220 /**
221 * Used by {@link optional_param()} and {@link required_param()} to
222 * clean the variables and/or cast to specific types, based on
223 * an options field.
224 * <code>
225 * $course->format = clean_param($course->format, PARAM_ALPHA);
226 * $selectedgrade_item = clean_param($selectedgrade_item, PARAM_CLEAN);
227 * </code>
228 *
229 * @uses $CFG
230 * @uses PARAM_CLEAN
231 * @uses PARAM_INT
232 * @uses PARAM_INTEGER
233 * @uses PARAM_ALPHA
234 * @uses PARAM_ALPHANUM
235 * @uses PARAM_NOTAGS
236 * @uses PARAM_ALPHAEXT
237 * @uses PARAM_BOOL
238 * @uses PARAM_SAFEDIR
facb512c 239 * @uses PARAM_FILE
240 * @uses PARAM_PATH
241 * @uses PARAM_HOST
242 * @uses PARAM_URL
243 * @uses PARAM_LOCALURL
244 * @uses PARAM_CLEANHTML
245 * @uses PARAM_SEQUENCE
246 * @param mixed $param the variable we are cleaning
247 * @param int $type expected format of param after cleaning.
248 * @return mixed
249 */
250 function test_clean_param()
251 {
c7fd861b 252 global $CFG;
facb512c 253 // Test unknown parameter type
b9c639d6 254
facb512c 255 // Test Raw param
b9c639d6 256 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_RAW),
facb512c 257 '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)');
b9c639d6 258
259 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_CLEAN),
65de6864 260 '#()*#,9789\'".,');
c7fd861b 261
262 // Test PARAM_URL and PARAM_LOCALURL a bit
263 $this->assertEqual(clean_param('http://google.com/', PARAM_URL), 'http://google.com/');
264 $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/');
265 $this->assertEqual(clean_param('http://localhost/', PARAM_URL), 'http://localhost/');
266 $this->assertEqual(clean_param('http://0.255.1.1/numericip.php', PARAM_URL), 'http://0.255.1.1/numericip.php');
267 $this->assertEqual(clean_param('/just/a/path', PARAM_URL), '/just/a/path');
268 $this->assertEqual(clean_param('funny:thing', PARAM_URL), '');
269
270 $this->assertEqual(clean_param('http://google.com/', PARAM_LOCALURL), '');
271 $this->assertEqual(clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_LOCALURL), '');
272 $this->assertEqual(clean_param($CFG->wwwroot, PARAM_LOCALURL), $CFG->wwwroot);
273 $this->assertEqual(clean_param('/just/a/path', PARAM_LOCALURL), '/just/a/path');
274 $this->assertEqual(clean_param('funny:thing', PARAM_LOCALURL), '');
facb512c 275 }
56a1a882 276
277 function test_make_user_directory() {
278 global $CFG;
279
280 // Test success conditions
281 $this->assertEqual("$CFG->dataroot/user/0/0", make_user_directory(0, true));
282 $this->assertEqual("$CFG->dataroot/user/0/1", make_user_directory(1, true));
283 $this->assertEqual("$CFG->dataroot/user/0/999", make_user_directory(999, true));
284 $this->assertEqual("$CFG->dataroot/user/1000/1000", make_user_directory(1000, true));
285 $this->assertEqual("$CFG->dataroot/user/2147483000/2147483647", make_user_directory(2147483647, true)); // Largest int possible
286
287 // Test fail conditions
288 $this->assertFalse(make_user_directory(2147483648, true)); // outside int boundary
289 $this->assertFalse(make_user_directory(-1, true));
290 $this->assertFalse(make_user_directory('string', true));
291 $this->assertFalse(make_user_directory(false, true));
292 $this->assertFalse(make_user_directory(true, true));
b9c639d6 293
56a1a882 294 }
7c9d46c9 295}
296
297?>