restorelib: fix incorrect indenting.
[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
b9c639d6 42class moodlelib_test extends MoodleUnitTestCase {
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
74 function test_address_in_subnet() {
75 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.1'));
76 $this->assertFalse(address_in_subnet('123.121.234.2', '123.121.234.1'));
77 $this->assertFalse(address_in_subnet('123.121.134.1', '123.121.234.1'));
78 $this->assertFalse(address_in_subnet('113.121.234.1', '123.121.234.1'));
79 $this->assertTrue(address_in_subnet('123.121.234.0', '123.121.234.2/28'));
80 $this->assertTrue(address_in_subnet('123.121.234.15', '123.121.234.2/28'));
81 $this->assertFalse(address_in_subnet('123.121.234.16', '123.121.234.2/28'));
82 $this->assertFalse(address_in_subnet('123.121.234.255', '123.121.234.2/28'));
1cc6b516 83 $this->assertTrue(address_in_subnet('123.121.234.0', '123.121.234.0/')); // / is like /32.
84 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.0/'));
85 $this->assertFalse(address_in_subnet('232.232.232.232', '123.121.234.0/0'));
7c9d46c9 86 $this->assertFalse(address_in_subnet('123.122.234.1', '123.121.'));
87 $this->assertFalse(address_in_subnet('223.121.234.1', '123.121.'));
1cc6b516 88 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121'));
89 $this->assertFalse(address_in_subnet('123.122.234.1', '123.121'));
90 $this->assertFalse(address_in_subnet('223.121.234.1', '123.121'));
91 $this->assertFalse(address_in_subnet('123.121.234.100', '123.121.234.10'));
7c9d46c9 92 $this->assertFalse(address_in_subnet('123.121.234.9', '123.121.234.10-20'));
93 $this->assertTrue(address_in_subnet('123.121.234.10', '123.121.234.10-20'));
94 $this->assertTrue(address_in_subnet('123.121.234.15', '123.121.234.10-20'));
95 $this->assertTrue(address_in_subnet('123.121.234.20', '123.121.234.10-20'));
96 $this->assertFalse(address_in_subnet('123.121.234.21', '123.121.234.10-20'));
97 $this->assertTrue(address_in_subnet(' 123.121.234.1 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
98 $this->assertTrue(address_in_subnet(' 1.1.2.3 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
99 $this->assertTrue(address_in_subnet(' 2.2.234.1 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
100 $this->assertTrue(address_in_subnet(' 3.3.3.4 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
101 $this->assertFalse(address_in_subnet(' 123.121.234.2 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
102 $this->assertFalse(address_in_subnet(' 2.1.2.3 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
103 $this->assertFalse(address_in_subnet(' 2.3.234.1 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
104 $this->assertFalse(address_in_subnet(' 3.3.3.7 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
6ff2be37 105 $this->assertFalse(address_in_subnet('172.16.1.142', '172.16.1.143/148'));
7c9d46c9 106 }
b9c639d6 107
239c1576 108 /**
b9c639d6 109 * Modifies $_SERVER['HTTP_USER_AGENT'] manually to check if check_browser_version
239c1576 110 * works as expected.
111 */
112 function test_check_browser_version()
113 {
a205dcdc 114 global $CFG;
b9c639d6 115
116 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
239c1576 117 $this->assertTrue(check_browser_version('Safari', '312'));
118 $this->assertFalse(check_browser_version('Safari', '500'));
b9c639d6 119
a205dcdc 120 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
239c1576 121 $this->assertTrue(check_browser_version('Opera', '8.0'));
122 $this->assertFalse(check_browser_version('Opera', '10.0'));
b9c639d6 123
a205dcdc 124 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
239c1576 125 $this->assertTrue(check_browser_version('MSIE', '5.0'));
126 $this->assertFalse(check_browser_version('MSIE', '7.0'));
b9c639d6 127
a205dcdc 128 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
239c1576 129 $this->assertTrue(check_browser_version('Firefox', '1.5'));
b9c639d6 130 $this->assertFalse(check_browser_version('Firefox', '3.0'));
d499142e 131 }
b9c639d6 132
facb512c 133 function test_optional_param()
134 {
b9c639d6 135 $_POST['username'] = 'post_user';
facb512c 136 $_GET['username'] = 'get_user';
137 $this->assertEqual(optional_param('username', 'default_user'), 'post_user');
b9c639d6 138
facb512c 139 unset($_POST['username']);
140 $this->assertEqual(optional_param('username', 'default_user'), 'get_user');
b9c639d6 141
facb512c 142 unset($_GET['username']);
143 $this->assertEqual(optional_param('username', 'default_user'), 'default_user');
144 }
b9c639d6 145
facb512c 146 /**
147 * Used by {@link optional_param()} and {@link required_param()} to
148 * clean the variables and/or cast to specific types, based on
149 * an options field.
150 * <code>
151 * $course->format = clean_param($course->format, PARAM_ALPHA);
152 * $selectedgrade_item = clean_param($selectedgrade_item, PARAM_CLEAN);
153 * </code>
154 *
155 * @uses $CFG
156 * @uses PARAM_CLEAN
157 * @uses PARAM_INT
158 * @uses PARAM_INTEGER
159 * @uses PARAM_ALPHA
160 * @uses PARAM_ALPHANUM
161 * @uses PARAM_NOTAGS
162 * @uses PARAM_ALPHAEXT
163 * @uses PARAM_BOOL
164 * @uses PARAM_SAFEDIR
facb512c 165 * @uses PARAM_FILE
166 * @uses PARAM_PATH
167 * @uses PARAM_HOST
168 * @uses PARAM_URL
169 * @uses PARAM_LOCALURL
170 * @uses PARAM_CLEANHTML
171 * @uses PARAM_SEQUENCE
172 * @param mixed $param the variable we are cleaning
173 * @param int $type expected format of param after cleaning.
174 * @return mixed
175 */
176 function test_clean_param()
177 {
c7fd861b 178 global $CFG;
facb512c 179 // Test unknown parameter type
b9c639d6 180
facb512c 181 // Test Raw param
b9c639d6 182 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_RAW),
facb512c 183 '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)');
b9c639d6 184
185 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_CLEAN),
facb512c 186 '#()*#,9789\\\'\".,');
c7fd861b 187
188 // Test PARAM_URL and PARAM_LOCALURL a bit
189 $this->assertEqual(clean_param('http://google.com/', PARAM_URL), 'http://google.com/');
190 $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/');
191 $this->assertEqual(clean_param('http://localhost/', PARAM_URL), 'http://localhost/');
192 $this->assertEqual(clean_param('http://0.255.1.1/numericip.php', PARAM_URL), 'http://0.255.1.1/numericip.php');
193 $this->assertEqual(clean_param('/just/a/path', PARAM_URL), '/just/a/path');
194 $this->assertEqual(clean_param('funny:thing', PARAM_URL), '');
195
196 $this->assertEqual(clean_param('http://google.com/', PARAM_LOCALURL), '');
197 $this->assertEqual(clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_LOCALURL), '');
198 $this->assertEqual(clean_param($CFG->wwwroot, PARAM_LOCALURL), $CFG->wwwroot);
199 $this->assertEqual(clean_param('/just/a/path', PARAM_LOCALURL), '/just/a/path');
200 $this->assertEqual(clean_param('funny:thing', PARAM_LOCALURL), '');
facb512c 201 }
56a1a882 202
203 function test_make_user_directory() {
204 global $CFG;
205
206 // Test success conditions
207 $this->assertEqual("$CFG->dataroot/user/0/0", make_user_directory(0, true));
208 $this->assertEqual("$CFG->dataroot/user/0/1", make_user_directory(1, true));
209 $this->assertEqual("$CFG->dataroot/user/0/999", make_user_directory(999, true));
210 $this->assertEqual("$CFG->dataroot/user/1000/1000", make_user_directory(1000, true));
211 $this->assertEqual("$CFG->dataroot/user/2147483000/2147483647", make_user_directory(2147483647, true)); // Largest int possible
212
213 // Test fail conditions
214 $this->assertFalse(make_user_directory(2147483648, true)); // outside int boundary
215 $this->assertFalse(make_user_directory(-1, true));
216 $this->assertFalse(make_user_directory('string', true));
217 $this->assertFalse(make_user_directory(false, true));
218 $this->assertFalse(make_user_directory(true, true));
b9c639d6 219
56a1a882 220 }
7c9d46c9 221}
222
223?>