3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.org //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
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. //
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: //
22 // http://www.gnu.org/copyleft/gpl.html //
24 ///////////////////////////////////////////////////////////////////////////
27 * Unit tests for (some of) ../moodlelib.php.
29 * @copyright © 2006 The Open University
30 * @author T.J.Hunt@open.ac.uk
31 * @author nicolas@moodle.com
32 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
36 if (!defined('MOODLE_INTERNAL')) {
37 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
40 require_once($CFG->libdir
. '/moodlelib.php');
42 class moodlelib_test
extends UnitTestCase
{
44 var $user_agents = 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)')
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)')
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')
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)')
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'));
83 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.'));
84 $this->assertFalse(address_in_subnet('123.122.234.1', '123.121.'));
85 $this->assertFalse(address_in_subnet('223.121.234.1', '123.121.'));
86 $this->assertFalse(address_in_subnet('123.121.234.9', '123.121.234.10-20'));
87 $this->assertTrue(address_in_subnet('123.121.234.10', '123.121.234.10-20'));
88 $this->assertTrue(address_in_subnet('123.121.234.15', '123.121.234.10-20'));
89 $this->assertTrue(address_in_subnet('123.121.234.20', '123.121.234.10-20'));
90 $this->assertFalse(address_in_subnet('123.121.234.21', '123.121.234.10-20'));
91 $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 '));
92 $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 '));
93 $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 '));
94 $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 '));
95 $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 '));
96 $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 '));
97 $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 '));
98 $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 '));
102 * Modifies $_SERVER['HTTP_USER_AGENT'] manually to check if check_browser_version
105 function test_check_browser_version()
109 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents
['Safari']['2.0']['Mac OS X'];
110 $this->assertTrue(check_browser_version('Safari', '312'));
111 $this->assertFalse(check_browser_version('Safari', '500'));
113 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents
['Opera']['9.0']['Windows XP'];
114 $this->assertTrue(check_browser_version('Opera', '8.0'));
115 $this->assertFalse(check_browser_version('Opera', '10.0'));
117 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents
['MSIE']['6.0']['Windows XP SP2'];
118 $this->assertTrue(check_browser_version('MSIE', '5.0'));
119 $this->assertFalse(check_browser_version('MSIE', '7.0'));
121 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents
['Firefox']['2.0']['Windows XP'];
122 $this->assertTrue(check_browser_version('Firefox', '1.5'));
123 $this->assertFalse(check_browser_version('Firefox', '3.0'));
126 function test_optional_param()
128 $_POST['username'] = 'post_user';
129 $_GET['username'] = 'get_user';
130 $this->assertEqual(optional_param('username', 'default_user'), 'post_user');
132 unset($_POST['username']);
133 $this->assertEqual(optional_param('username', 'default_user'), 'get_user');
135 unset($_GET['username']);
136 $this->assertEqual(optional_param('username', 'default_user'), 'default_user');
140 * Used by {@link optional_param()} and {@link required_param()} to
141 * clean the variables and/or cast to specific types, based on
144 * $course->format = clean_param($course->format, PARAM_ALPHA);
145 * $selectedgrade_item = clean_param($selectedgrade_item, PARAM_CLEAN);
151 * @uses PARAM_INTEGER
153 * @uses PARAM_ALPHANUM
155 * @uses PARAM_ALPHAEXT
157 * @uses PARAM_SAFEDIR
158 * @uses PARAM_CLEANFILE
163 * @uses PARAM_LOCALURL
164 * @uses PARAM_CLEANHTML
165 * @uses PARAM_SEQUENCE
166 * @param mixed $param the variable we are cleaning
167 * @param int $type expected format of param after cleaning.
170 function test_clean_param()
173 // Test unknown parameter type
176 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_RAW
),
177 '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)');
179 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_CLEAN
),
180 '#()*#,9789\\\'\".,');
182 // Test PARAM_URL and PARAM_LOCALURL a bit
183 $this->assertEqual(clean_param('http://google.com/', PARAM_URL
), 'http://google.com/');
184 $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/');
185 $this->assertEqual(clean_param('http://localhost/', PARAM_URL
), 'http://localhost/');
186 $this->assertEqual(clean_param('http://0.255.1.1/numericip.php', PARAM_URL
), 'http://0.255.1.1/numericip.php');
187 $this->assertEqual(clean_param('/just/a/path', PARAM_URL
), '/just/a/path');
188 $this->assertEqual(clean_param('funny:thing', PARAM_URL
), '');
190 $this->assertEqual(clean_param('http://google.com/', PARAM_LOCALURL
), '');
191 $this->assertEqual(clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_LOCALURL
), '');
192 $this->assertEqual(clean_param($CFG->wwwroot
, PARAM_LOCALURL
), $CFG->wwwroot
);
193 $this->assertEqual(clean_param('/just/a/path', PARAM_LOCALURL
), '/just/a/path');
194 $this->assertEqual(clean_param('funny:thing', PARAM_LOCALURL
), '');
197 function test_make_user_directory() {
200 // Test success conditions
201 $this->assertEqual("$CFG->dataroot/user/0/0", make_user_directory(0, true));
202 $this->assertEqual("$CFG->dataroot/user/0/1", make_user_directory(1, true));
203 $this->assertEqual("$CFG->dataroot/user/0/999", make_user_directory(999, true));
204 $this->assertEqual("$CFG->dataroot/user/1000/1000", make_user_directory(1000, true));
205 $this->assertEqual("$CFG->dataroot/user/2147483000/2147483647", make_user_directory(2147483647, true)); // Largest int possible
207 // Test fail conditions
208 $this->assertFalse(make_user_directory(2147483648, true)); // outside int boundary
209 $this->assertFalse(make_user_directory(-1, true));
210 $this->assertFalse(make_user_directory('string', true));
211 $this->assertFalse(make_user_directory(false, true));
212 $this->assertFalse(make_user_directory(true, true));