MDL-23979 PARAM_CLEAN should not be used in new code at all ,there is nothing like...
[moodle.git] / lib / simpletest / testmoodlelib.php
1 <?php
3 ///////////////////////////////////////////////////////////////////////////
4 //                                                                       //
5 // NOTICE OF COPYRIGHT                                                   //
6 //                                                                       //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
8 //          http://moodle.org                                            //
9 //                                                                       //
10 // Copyright (C) 1999 onwards Martin Dougiamas  http://dougiamas.com     //
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 ///////////////////////////////////////////////////////////////////////////
26 /**
27  * Unit tests for (some of) ../moodlelib.php.
28  *
29  * Note, tests for get_string are in the separate file testgetstring.php.
30  *
31  * @copyright &copy; 2006 The Open University
32  * @author T.J.Hunt@open.ac.uk
33  * @author nicolas@moodle.com
34  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
35  * @package moodlecore
36  */
38 if (!defined('MOODLE_INTERNAL')) {
39     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
40 }
42 require_once($CFG->libdir . '/moodlelib.php');
44 class moodlelib_test extends UnitTestCase {
46     public static $includecoverage = array('lib/moodlelib.php');
48     var $user_agents = array(
49             'MSIE' => array(
50                 '5.5' => array('Windows 2000' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)'),
51                 '6.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'),
52                 '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)'),
53                 '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)'),
55             ),
56             'Firefox' => array(
57                 '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'),
58                 '1.5'     => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8) Gecko/20051107 Firefox/1.5'),
59                 '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'),
60                 '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',
61                                    'Ubuntu Linux AMD64' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20060601 Firefox/2.0 (Ubuntu-edgy)'),
62                 '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'),
63             ),
64             'Safari' => array(
65                 '312' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312'),
66                 '2.0' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412')
67             ),
68             'Opera' => array(
69                 '8.51' => array('Windows XP' => 'Opera/8.51 (Windows NT 5.1; U; en)'),
70                 '9.0'  => array('Windows XP' => 'Opera/9.0 (Windows NT 5.1; U; en)',
71                                 'Debian Linux' => 'Opera/9.01 (X11; Linux i686; U; en)')
72             )
73         );
75     function test_cleanremoteaddr() {
76         //IPv4
77         $this->assertEqual(cleanremoteaddr('1023.121.234.1'), null);
78         $this->assertEqual(cleanremoteaddr('123.121.234.01 '), '123.121.234.1');
80         //IPv6
81         $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:0:0'), null);
82         $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:abh'), null);
83         $this->assertEqual(cleanremoteaddr('0:0:0:::0:0:1'), null);
84         $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:0', true), '::');
85         $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:1:1', true), '::1:1');
86         $this->assertEqual(cleanremoteaddr('abcd:00ef:0:0:0:0:0:0', true), 'abcd:ef::');
87         $this->assertEqual(cleanremoteaddr('1:0:0:0:0:0:0:1', true), '1::1');
88         $this->assertEqual(cleanremoteaddr('::10:1', false), '0:0:0:0:0:0:10:1');
89         $this->assertEqual(cleanremoteaddr('01:1::', false), '1:1:0:0:0:0:0:0');
90         $this->assertEqual(cleanremoteaddr('10::10', false), '10:0:0:0:0:0:0:10');
91         $this->assertEqual(cleanremoteaddr('::ffff:192.168.1.1', true), '::ffff:c0a8:11');
92     }
94     function test_address_in_subnet() {
95     /// 1: xxx.xxx.xxx.xxx/nn or xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/nnn          (number of bits in net mask)
96         $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.1/32'));
97         $this->assertFalse(address_in_subnet('123.121.23.1', '123.121.23.0/32'));
98         $this->assertTrue(address_in_subnet('10.10.10.100',  '123.121.23.45/0'));
99         $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.0/24'));
100         $this->assertFalse(address_in_subnet('123.121.34.1', '123.121.234.0/24'));
101         $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.0/30'));
102         $this->assertFalse(address_in_subnet('123.121.23.8', '123.121.23.0/30'));
103         $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba/128'));
104         $this->assertFalse(address_in_subnet('bab:baba::baba', 'bab:baba::cece/128'));
105         $this->assertTrue(address_in_subnet('baba:baba::baba', 'cece:cece::cece/0'));
106         $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba/128'));
107         $this->assertTrue(address_in_subnet('baba:baba::00ba', 'baba:baba::/120'));
108         $this->assertFalse(address_in_subnet('baba:baba::aba', 'baba:baba::/120'));
109         $this->assertTrue(address_in_subnet('baba::baba:00ba', 'baba::baba:0/112'));
110         $this->assertFalse(address_in_subnet('baba::aba:00ba', 'baba::baba:0/112'));
111         $this->assertFalse(address_in_subnet('aba::baba:0000', 'baba::baba:0/112'));
113         // fixed input
114         $this->assertTrue(address_in_subnet('123.121.23.1   ', ' 123.121.23.0 / 24'));
115         $this->assertTrue(address_in_subnet('::ffff:10.1.1.1', ' 0:0:0:000:0:ffff:a1:10 / 126'));
117         // incorrect input
118         $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/-2'));
119         $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/64'));
120         $this->assertFalse(address_in_subnet('123.121.234.x', '123.121.234.1/24'));
121         $this->assertFalse(address_in_subnet('123.121.234.0', '123.121.234.xx/24'));
122         $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/xx0'));
123         $this->assertFalse(address_in_subnet('::1', '::aa:0/xx0'));
124         $this->assertFalse(address_in_subnet('::1', '::aa:0/-5'));
125         $this->assertFalse(address_in_subnet('::1', '::aa:0/130'));
126         $this->assertFalse(address_in_subnet('x:1', '::aa:0/130'));
127         $this->assertFalse(address_in_subnet('::1', '::ax:0/130'));
130     /// 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)
131         $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12-14'));
132         $this->assertTrue(address_in_subnet('123.121.234.13', '123.121.234.12-14'));
133         $this->assertTrue(address_in_subnet('123.121.234.14', '123.121.234.12-14'));
134         $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.12-14'));
135         $this->assertFalse(address_in_subnet('123.121.234.20', '123.121.234.12-14'));
136         $this->assertFalse(address_in_subnet('123.121.23.12', '123.121.234.12-14'));
137         $this->assertFalse(address_in_subnet('123.12.234.12', '123.121.234.12-14'));
138         $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba-babe'));
139         $this->assertTrue(address_in_subnet('baba:baba::babc', 'baba:baba::baba-babe'));
140         $this->assertTrue(address_in_subnet('baba:baba::babe', 'baba:baba::baba-babe'));
141         $this->assertFalse(address_in_subnet('bab:baba::bab0', 'bab:baba::baba-babe'));
142         $this->assertFalse(address_in_subnet('bab:baba::babf', 'bab:baba::baba-babe'));
143         $this->assertFalse(address_in_subnet('bab:baba::bfbe', 'bab:baba::baba-babe'));
144         $this->assertFalse(address_in_subnet('bfb:baba::babe', 'bab:baba::baba-babe'));
146         // fixed input
147         $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12 - 14 '));
148         $this->assertTrue(address_in_subnet('bab:baba::babe', 'bab:baba::baba - babe  '));
150         // incorrect input
151         $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12-234.14'));
152         $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12-256'));
153         $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12--256'));
156     /// 3: xxx.xxx or xxx.xxx. or xxx:xxx:xxxx or xxx:xxx:xxxx.                  (incomplete address, a bit non-technical ;-)
157         $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12'));
158         $this->assertFalse(address_in_subnet('123.121.23.12', '123.121.23.13'));
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.234'));
161         $this->assertTrue(address_in_subnet('123.121.234.12', '123.121'));
162         $this->assertTrue(address_in_subnet('123.121.234.12', '123'));
163         $this->assertFalse(address_in_subnet('123.121.234.1', '12.121.234.'));
164         $this->assertFalse(address_in_subnet('123.121.234.1', '12.121.234'));
165         $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:baba::bab'));
166         $this->assertFalse(address_in_subnet('baba:baba::ba', 'baba:baba::bc'));
167         $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:baba'));
168         $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:'));
169         $this->assertFalse(address_in_subnet('bab:baba::bab', 'baba:'));
172     /// multiple subnets
173         $this->assertTrue(address_in_subnet('123.121.234.12', '::1/64, 124., 123.121.234.10-30'));
174         $this->assertTrue(address_in_subnet('124.121.234.12', '::1/64, 124., 123.121.234.10-30'));
175         $this->assertTrue(address_in_subnet('::2',            '::1/64, 124., 123.121.234.10-30'));
176         $this->assertFalse(address_in_subnet('12.121.234.12', '::1/64, 124., 123.121.234.10-30'));
179     /// other incorrect input
180         $this->assertFalse(address_in_subnet('123.123.123.123', ''));
181     }
183     /**
184      * Modifies $_SERVER['HTTP_USER_AGENT'] manually to check if check_browser_version
185      * works as expected.
186      */
187     function test_check_browser_version()
188     {
189         global $CFG;
191         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
192         $this->assertTrue(check_browser_version('Safari', '312'));
193         $this->assertFalse(check_browser_version('Safari', '500'));
195         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
196         $this->assertTrue(check_browser_version('Opera', '8.0'));
197         $this->assertFalse(check_browser_version('Opera', '10.0'));
199         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
200         $this->assertTrue(check_browser_version('MSIE', '5.0'));
201         $this->assertFalse(check_browser_version('MSIE', '7.0'));
203         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
204         $this->assertTrue(check_browser_version('Firefox', '1.5'));
205         $this->assertFalse(check_browser_version('Firefox', '3.0'));
206     }
208     function test_get_browser_version_classes() {
209         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
210         $this->assertEqual(array('safari'), get_browser_version_classes());
212         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
213         $this->assertEqual(array('opera'), get_browser_version_classes());
215         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
216         $this->assertEqual(array('ie', 'ie6'), get_browser_version_classes());
218         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['7.0']['Windows XP SP2'];
219         $this->assertEqual(array('ie', 'ie7'), get_browser_version_classes());
221         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['8.0']['Windows Vista'];
222         $this->assertEqual(array('ie', 'ie8'), get_browser_version_classes());
224         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
225         $this->assertEqual(array('gecko', 'gecko18'), get_browser_version_classes());
227         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['3.0.6']['SUSE'];
228         $this->assertEqual(array('gecko', 'gecko19'), get_browser_version_classes());
229     }
231     function test_optional_param() {
232         $_POST['username'] = 'post_user';
233         $_GET['username'] = 'get_user';
234         $this->assertEqual(optional_param('username', 'default_user', PARAM_RAW), 'post_user');
236         unset($_POST['username']);
237         $this->assertEqual(optional_param('username', 'default_user', PARAM_RAW), 'get_user');
239         unset($_GET['username']);
240         $this->assertEqual(optional_param('username', 'default_user', PARAM_RAW), 'default_user');
241     }
243     function test_clean_param_raw() {
244         $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_RAW),
245             '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)');
246     }
248     function test_clean_param_clean() {
249         //TODO: param clean is an ugly hack, do not use in new code (skodak)
250         $this->assertEqual(clean_param('xx<script>', PARAM_CLEAN), 'xx');
251     }
253     function test_clean_param_alpha() {
254         $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHA),
255                 'DSFMOSDJ');
256     }
258     function test_clean_param_alphanum() {
259         $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHANUM),
260                 '978942897DSFMOSDJ');
261     }
263     function test_clean_param_alphaext() {
264         $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHAEXT),
265                 'DSFMOSDJ');
266     }
268     function test_clean_param_sequence() {
269         $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_SEQUENCE),
270                 ',9789,42897');
271     }
273     function test_clean_param_url() {
274         // Test PARAM_URL and PARAM_LOCALURL a bit
275         $this->assertEqual(clean_param('http://google.com/', PARAM_URL), 'http://google.com/');
276         $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/');
277         $this->assertEqual(clean_param('http://localhost/', PARAM_URL), 'http://localhost/');
278         $this->assertEqual(clean_param('http://0.255.1.1/numericip.php', PARAM_URL), 'http://0.255.1.1/numericip.php');
279         $this->assertEqual(clean_param('/just/a/path', PARAM_URL), '/just/a/path');
280         $this->assertEqual(clean_param('funny:thing', PARAM_URL), '');
281     }
283     function test_clean_param_localurl() {
284         global $CFG;
285         $this->assertEqual(clean_param('http://google.com/', PARAM_LOCALURL), '');
286         $this->assertEqual(clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_LOCALURL), '');
287         $this->assertEqual(clean_param($CFG->wwwroot, PARAM_LOCALURL), $CFG->wwwroot);
288         $this->assertEqual(clean_param('/just/a/path', PARAM_LOCALURL), '/just/a/path');
289         $this->assertEqual(clean_param('funny:thing', PARAM_LOCALURL), '');
290         $this->assertEqual(clean_param('course/view.php?id=3', PARAM_LOCALURL), 'course/view.php?id=3');
291     }
293     function test_clean_param_file() {
294         $this->assertEqual(clean_param('correctfile.txt', PARAM_FILE), 'correctfile.txt');
295         $this->assertEqual(clean_param('b\'a<d`\\/fi:l>e.t"x|t', PARAM_FILE), 'badfile.txt');
296         $this->assertEqual(clean_param('../parentdirfile.txt', PARAM_FILE), 'parentdirfile.txt');
297         //The following behaviours have been maintained although they seem a little odd
298         $this->assertEqual(clean_param('funny:thing', PARAM_FILE), 'funnything');
299         $this->assertEqual(clean_param('./currentdirfile.txt', PARAM_FILE), '.currentdirfile.txt');
300         $this->assertEqual(clean_param('c:\temp\windowsfile.txt', PARAM_FILE), 'ctempwindowsfile.txt');
301         $this->assertEqual(clean_param('/home/user/linuxfile.txt', PARAM_FILE), 'homeuserlinuxfile.txt');
302         $this->assertEqual(clean_param('~/myfile.txt', PARAM_FILE), '~myfile.txt');
303     }
305     function test_clean_param_username() {
306         global $CFG;
307         $currentstatus =  $CFG->extendedusernamechars;
309         // Run tests with extended character == FALSE;
310         $CFG->extendedusernamechars = FALSE;
311         $this->assertEqual(clean_param('johndoe123', PARAM_USERNAME), 'johndoe123' );
312         $this->assertEqual(clean_param('john.doe', PARAM_USERNAME), 'john.doe');
313         $this->assertEqual(clean_param('john-doe', PARAM_USERNAME), 'john-doe');
314         $this->assertEqual(clean_param('john- doe', PARAM_USERNAME), 'john-doe');
315         $this->assertEqual(clean_param('john_doe', PARAM_USERNAME), 'john_doe');
316         $this->assertEqual(clean_param('john@doe', PARAM_USERNAME), 'john@doe');
317         $this->assertEqual(clean_param('john~doe', PARAM_USERNAME), 'johndoe');
318         $this->assertEqual(clean_param('john´doe', PARAM_USERNAME), 'johndoe');
319         $this->assertEqual(clean_param('john#$%&() ', PARAM_USERNAME), 'john');
320         $this->assertEqual(clean_param('JOHNdóé ', PARAM_USERNAME), 'johnd');
321         $this->assertEqual(clean_param('john.,:;-_/|\ñÑ[]A_X-,D {} ~!@#$%^&*()_+ ?><[] ščřžžý ?ýá\9e?\9eý??\9adoe ', PARAM_USERNAME), 'john.-_a_x-d@_doe');
324         // Test success condition, if extendedusernamechars == ENABLE;
325         $CFG->extendedusernamechars = TRUE;
326         $this->assertEqual(clean_param('john_doe', PARAM_USERNAME), 'john_doe');
327         $this->assertEqual(clean_param('john@doe', PARAM_USERNAME), 'john@doe');
328         $this->assertEqual(clean_param('john# $%&()+_^', PARAM_USERNAME), 'john#$%&()+_^');
329         $this->assertEqual(clean_param('john~doe', PARAM_USERNAME), 'john~doe');
330         $this->assertEqual(clean_param('joHN´doe', PARAM_USERNAME), 'john´doe');
331         $this->assertEqual(clean_param('johnDOE', PARAM_USERNAME), 'johndoe');
332         $this->assertEqual(clean_param('johndóé ', PARAM_USERNAME), 'johndóé');
334         $CFG->extendedusernamechars = $currentstatus;
335     }
337     function test_clean_param_stringid() {
338         // Test string identifiers validation
339         // valid strings:
340         $this->assertEqual(clean_param('validstring', PARAM_STRINGID), 'validstring');
341         $this->assertEqual(clean_param('mod/foobar:valid_capability', PARAM_STRINGID), 'mod/foobar:valid_capability');
342         $this->assertEqual(clean_param('CZ', PARAM_STRINGID), 'CZ');
343         $this->assertEqual(clean_param('application/vnd.ms-powerpoint', PARAM_STRINGID), 'application/vnd.ms-powerpoint');
344         $this->assertEqual(clean_param('grade2', PARAM_STRINGID), 'grade2');
345         // invalid strings:
346         $this->assertEqual(clean_param('trailing ', PARAM_STRINGID), '');
347         $this->assertEqual(clean_param('space bar', PARAM_STRINGID), '');
348         $this->assertEqual(clean_param('0numeric', PARAM_STRINGID), '');
349         $this->assertEqual(clean_param('*', PARAM_STRINGID), '');
350         $this->assertEqual(clean_param(' ', PARAM_STRINGID), '');
351     }
353     function test_validate_param() {
354         try {
355             $param = validate_param('11a', PARAM_INT);
356             $this->fail('invalid_parameter_exception expected');
357         } catch (invalid_parameter_exception $ex) {
358             $this->assertTrue(true);
359         }
360         try {
361             $param = validate_param('11', PARAM_INT);
362             $this->assertEqual($param, 11);
363         } catch (invalid_parameter_exception $ex) {
364             $this->fail('invalid_parameter_exception not expected');
365         }
366         try {
367             $param = validate_param(null, PARAM_INT, false);
368             $this->fail('invalid_parameter_exception expected');
369         } catch (invalid_parameter_exception $ex) {
370             $this->assertTrue(true);
371         }
372         try {
373             $param = validate_param(null, PARAM_INT, true);
374             $this->assertTrue($param===null);
375         } catch (invalid_parameter_exception $ex) {
376             $this->fail('invalid_parameter_exception expected');
377         }
378         try {
379             $param = validate_param(array(), PARAM_INT);
380             $this->fail('invalid_parameter_exception expected');
381         } catch (invalid_parameter_exception $ex) {
382             $this->assertTrue(true);
383         }
384         try {
385             $param = validate_param(new stdClass, PARAM_INT);
386             $this->fail('invalid_parameter_exception expected');
387         } catch (invalid_parameter_exception $ex) {
388             $this->assertTrue(true);
389         }
390     }
392     function test_make_user_directory() {
393         global $CFG;
395         // Test success conditions
396         $this->assertEqual("$CFG->dataroot/user/0/0", make_user_directory(0, true));
397         $this->assertEqual("$CFG->dataroot/user/0/1", make_user_directory(1, true));
398         $this->assertEqual("$CFG->dataroot/user/0/999", make_user_directory(999, true));
399         $this->assertEqual("$CFG->dataroot/user/1000/1000", make_user_directory(1000, true));
400         $this->assertEqual("$CFG->dataroot/user/2147483000/2147483647", make_user_directory(2147483647, true)); // Largest int possible
402         // Test fail conditions
403         $this->assertFalse(make_user_directory(2147483648, true)); // outside int boundary
404         $this->assertFalse(make_user_directory(-1, true));
405         $this->assertFalse(make_user_directory('string', true));
406         $this->assertFalse(make_user_directory(false, true));
407         $this->assertFalse(make_user_directory(true, true));
409     }
411     function test_shorten_text() {
412         $text = "short text already no tags";
413         $this->assertEqual($text, shorten_text($text));
415         $text = "<p>short <b>text</b> already</p><p>with tags</p>";
416         $this->assertEqual($text, shorten_text($text));
418         $text = "long text without any tags blah de blah blah blah what";
419         $this->assertEqual('long text without any tags ...', shorten_text($text));
421         $text = "<div class='frog'><p><blockquote>Long text with tags that will ".
422             "be chopped off but <b>should be added back again</b></blockquote></p></div>";
423         $this->assertEqual("<div class='frog'><p><blockquote>Long text with " .
424             "tags that ...</blockquote></p></div>", shorten_text($text));
426         $text = "some text which shouldn't &nbsp; break there";
427         $this->assertEqual("some text which shouldn't &nbsp; ...",
428             shorten_text($text, 31));
429         $this->assertEqual("some text which shouldn't ...",
430             shorten_text($text, 30));
432         // This case caused a bug up to 1.9.5
433         $text = "<h3>standard 'break-out' sub groups in TGs?</h3>&nbsp;&lt;&lt;There are several";
434         $this->assertEqual("<h3>standard 'break-out' sub groups in ...</h3>",
435             shorten_text($text, 43));
437         $text = "<h1>123456789</h1>";//a string with no convenient breaks
438         $this->assertEqual("<h1>12345...</h1>",
439             shorten_text($text, 8));
440     }
442     function test_usergetdate() {
443         global $USER;
445         $userstimezone = $USER->timezone;
446         $USER->timezone = 2;//set the timezone to a known state
448         $ts = 1261540267; //the time this function was created
450         $arr = usergetdate($ts,1);//specify the timezone as an argument
451         $arr = array_values($arr);
453         list($seconds,$minutes,$hours,$mday,$wday,$mon,$year,$yday,$weekday,$month) = $arr;
454         $this->assertEqual($seconds,7);
455         $this->assertEqual($minutes,51);
456         $this->assertEqual($hours,4);
457         $this->assertEqual($mday,23);
458         $this->assertEqual($wday,3);
459         $this->assertEqual($mon,12);
460         $this->assertEqual($year,2009);
461         $this->assertEqual($yday,357);
462         $this->assertEqual($weekday,'Wednesday');
463         $this->assertEqual($month,'December');
465         $arr = usergetdate($ts);//gets the timezone from the $USER object
466         $arr = array_values($arr);
468         list($seconds,$minutes,$hours,$mday,$wday,$mon,$year,$yday,$weekday,$month) = $arr;
469         $this->assertEqual($seconds,7);
470         $this->assertEqual($minutes,51);
471         $this->assertEqual($hours,5);
472         $this->assertEqual($mday,23);
473         $this->assertEqual($wday,3);
474         $this->assertEqual($mon,12);
475         $this->assertEqual($year,2009);
476         $this->assertEqual($yday,357);
477         $this->assertEqual($weekday,'Wednesday');
478         $this->assertEqual($month,'December');
480         //set the timezone back to what it was
481         $USER->timezone = $userstimezone;
482     }