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