2ae68cbc87b3a589519d42d62ac5bb3501b57986
[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         // PARAM_CLEAN is an ugly hack, do not use in new code (skodak)
250         // instead use more specific type, or submit sothing that can be verified properly
251         $this->assertEqual(clean_param('xx<script>', PARAM_CLEAN), 'xx');
252     }
254     function test_clean_param_alpha() {
255         $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHA),
256                 'DSFMOSDJ');
257     }
259     function test_clean_param_alphanum() {
260         $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHANUM),
261                 '978942897DSFMOSDJ');
262     }
264     function test_clean_param_alphaext() {
265         $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHAEXT),
266                 'DSFMOSDJ');
267     }
269     function test_clean_param_sequence() {
270         $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_SEQUENCE),
271                 ',9789,42897');
272     }
274     function test_clean_param_text() {
275         $this->assertEqual(PARAM_TEXT, PARAM_MULTILANG);
276         //standard
277         $this->assertEqual(clean_param('xx<lang lang="en">aa</lang><lang lang="yy">pp</lang>', PARAM_TEXT), 'xx<lang lang="en">aa</lang><lang lang="yy">pp</lang>');
278         $this->assertEqual(clean_param('<span lang="en" class="multilang">aa</span><span lang="xy" class="multilang">bb</span>', PARAM_TEXT), '<span lang="en" class="multilang">aa</span><span lang="xy" class="multilang">bb</span>');
279         $this->assertEqual(clean_param('xx<lang lang="en">aa'."\n".'</lang><lang lang="yy">pp</lang>', PARAM_TEXT), 'xx<lang lang="en">aa'."\n".'</lang><lang lang="yy">pp</lang>');
280         //malformed
281         $this->assertEqual(clean_param('<span lang="en" class="multilang">aa</span>', PARAM_TEXT), '<span lang="en" class="multilang">aa</span>');
282         $this->assertEqual(clean_param('<span lang="en" class="nothing" class="multilang">aa</span>', PARAM_TEXT), 'aa');
283         $this->assertEqual(clean_param('<lang lang="en" class="multilang">aa</lang>', PARAM_TEXT), 'aa');
284         $this->assertEqual(clean_param('<lang lang="en!!">aa</lang>', PARAM_TEXT), 'aa');
285         $this->assertEqual(clean_param('<span lang="en==" class="multilang">aa</span>', PARAM_TEXT), 'aa');
286         $this->assertEqual(clean_param('a<em>b</em>c', PARAM_TEXT), 'abc');
287         $this->assertEqual(clean_param('a><xx >c>', PARAM_TEXT), 'a>c>'); // standard strip_tags() behaviour
288         $this->assertEqual(clean_param('a<b', PARAM_TEXT), 'a');
289         $this->assertEqual(clean_param('a>b', PARAM_TEXT), 'a>b');
290         $this->assertEqual(clean_param('<lang lang="en">a>a</lang>', PARAM_TEXT), '<lang lang="en">a>a</lang>'); // standard strip_tags() behaviour
291         $this->assertEqual(clean_param('<lang lang="en">a<a</lang>', PARAM_TEXT), 'a');
292         $this->assertEqual(clean_param('<lang lang="en">a<br>a</lang>', PARAM_TEXT), '<lang lang="en">aa</lang>');
293     }
295     function test_clean_param_url() {
296         // Test PARAM_URL and PARAM_LOCALURL a bit
297         $this->assertEqual(clean_param('http://google.com/', PARAM_URL), 'http://google.com/');
298         $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/');
299         $this->assertEqual(clean_param('http://localhost/', PARAM_URL), 'http://localhost/');
300         $this->assertEqual(clean_param('http://0.255.1.1/numericip.php', PARAM_URL), 'http://0.255.1.1/numericip.php');
301         $this->assertEqual(clean_param('/just/a/path', PARAM_URL), '/just/a/path');
302         $this->assertEqual(clean_param('funny:thing', PARAM_URL), '');
303     }
305     function test_clean_param_localurl() {
306         global $CFG;
307         $this->assertEqual(clean_param('http://google.com/', PARAM_LOCALURL), '');
308         $this->assertEqual(clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_LOCALURL), '');
309         $this->assertEqual(clean_param($CFG->wwwroot, PARAM_LOCALURL), $CFG->wwwroot);
310         $this->assertEqual(clean_param('/just/a/path', PARAM_LOCALURL), '/just/a/path');
311         $this->assertEqual(clean_param('funny:thing', PARAM_LOCALURL), '');
312         $this->assertEqual(clean_param('course/view.php?id=3', PARAM_LOCALURL), 'course/view.php?id=3');
313     }
315     function test_clean_param_file() {
316         $this->assertEqual(clean_param('correctfile.txt', PARAM_FILE), 'correctfile.txt');
317         $this->assertEqual(clean_param('b\'a<d`\\/fi:l>e.t"x|t', PARAM_FILE), 'badfile.txt');
318         $this->assertEqual(clean_param('../parentdirfile.txt', PARAM_FILE), 'parentdirfile.txt');
319         //The following behaviours have been maintained although they seem a little odd
320         $this->assertEqual(clean_param('funny:thing', PARAM_FILE), 'funnything');
321         $this->assertEqual(clean_param('./currentdirfile.txt', PARAM_FILE), '.currentdirfile.txt');
322         $this->assertEqual(clean_param('c:\temp\windowsfile.txt', PARAM_FILE), 'ctempwindowsfile.txt');
323         $this->assertEqual(clean_param('/home/user/linuxfile.txt', PARAM_FILE), 'homeuserlinuxfile.txt');
324         $this->assertEqual(clean_param('~/myfile.txt', PARAM_FILE), '~myfile.txt');
325     }
327     function test_clean_param_username() {
328         global $CFG;
329         $currentstatus =  $CFG->extendedusernamechars;
331         // Run tests with extended character == FALSE;
332         $CFG->extendedusernamechars = FALSE;
333         $this->assertEqual(clean_param('johndoe123', PARAM_USERNAME), 'johndoe123' );
334         $this->assertEqual(clean_param('john.doe', PARAM_USERNAME), 'john.doe');
335         $this->assertEqual(clean_param('john-doe', PARAM_USERNAME), 'john-doe');
336         $this->assertEqual(clean_param('john- doe', PARAM_USERNAME), 'john-doe');
337         $this->assertEqual(clean_param('john_doe', PARAM_USERNAME), 'john_doe');
338         $this->assertEqual(clean_param('john@doe', PARAM_USERNAME), 'john@doe');
339         $this->assertEqual(clean_param('john~doe', PARAM_USERNAME), 'johndoe');
340         $this->assertEqual(clean_param('john´doe', PARAM_USERNAME), 'johndoe');
341         $this->assertEqual(clean_param('john#$%&() ', PARAM_USERNAME), 'john');
342         $this->assertEqual(clean_param('JOHNdóé ', PARAM_USERNAME), 'johnd');
343         $this->assertEqual(clean_param('john.,:;-_/|\ñÑ[]A_X-,D {} ~!@#$%^&*()_+ ?><[] ščřžžý ?ýá\9e?\9eý??\9adoe ', PARAM_USERNAME), 'john.-_a_x-d@_doe');
346         // Test success condition, if extendedusernamechars == ENABLE;
347         $CFG->extendedusernamechars = TRUE;
348         $this->assertEqual(clean_param('john_doe', PARAM_USERNAME), 'john_doe');
349         $this->assertEqual(clean_param('john@doe', PARAM_USERNAME), 'john@doe');
350         $this->assertEqual(clean_param('john# $%&()+_^', PARAM_USERNAME), 'john#$%&()+_^');
351         $this->assertEqual(clean_param('john~doe', PARAM_USERNAME), 'john~doe');
352         $this->assertEqual(clean_param('joHN´doe', PARAM_USERNAME), 'john´doe');
353         $this->assertEqual(clean_param('johnDOE', PARAM_USERNAME), 'johndoe');
354         $this->assertEqual(clean_param('johndóé ', PARAM_USERNAME), 'johndóé');
356         $CFG->extendedusernamechars = $currentstatus;
357     }
359     function test_clean_param_stringid() {
360         // Test string identifiers validation
361         // valid strings:
362         $this->assertEqual(clean_param('validstring', PARAM_STRINGID), 'validstring');
363         $this->assertEqual(clean_param('mod/foobar:valid_capability', PARAM_STRINGID), 'mod/foobar:valid_capability');
364         $this->assertEqual(clean_param('CZ', PARAM_STRINGID), 'CZ');
365         $this->assertEqual(clean_param('application/vnd.ms-powerpoint', PARAM_STRINGID), 'application/vnd.ms-powerpoint');
366         $this->assertEqual(clean_param('grade2', PARAM_STRINGID), 'grade2');
367         // invalid strings:
368         $this->assertEqual(clean_param('trailing ', PARAM_STRINGID), '');
369         $this->assertEqual(clean_param('space bar', PARAM_STRINGID), '');
370         $this->assertEqual(clean_param('0numeric', PARAM_STRINGID), '');
371         $this->assertEqual(clean_param('*', PARAM_STRINGID), '');
372         $this->assertEqual(clean_param(' ', PARAM_STRINGID), '');
373     }
375     function test_validate_param() {
376         try {
377             $param = validate_param('11a', PARAM_INT);
378             $this->fail('invalid_parameter_exception expected');
379         } catch (invalid_parameter_exception $ex) {
380             $this->assertTrue(true);
381         }
382         try {
383             $param = validate_param('11', PARAM_INT);
384             $this->assertEqual($param, 11);
385         } catch (invalid_parameter_exception $ex) {
386             $this->fail('invalid_parameter_exception not expected');
387         }
388         try {
389             $param = validate_param(null, PARAM_INT, false);
390             $this->fail('invalid_parameter_exception expected');
391         } catch (invalid_parameter_exception $ex) {
392             $this->assertTrue(true);
393         }
394         try {
395             $param = validate_param(null, PARAM_INT, true);
396             $this->assertTrue($param===null);
397         } catch (invalid_parameter_exception $ex) {
398             $this->fail('invalid_parameter_exception expected');
399         }
400         try {
401             $param = validate_param(array(), PARAM_INT);
402             $this->fail('invalid_parameter_exception expected');
403         } catch (invalid_parameter_exception $ex) {
404             $this->assertTrue(true);
405         }
406         try {
407             $param = validate_param(new stdClass, PARAM_INT);
408             $this->fail('invalid_parameter_exception expected');
409         } catch (invalid_parameter_exception $ex) {
410             $this->assertTrue(true);
411         }
412     }
414     function test_shorten_text() {
415         $text = "short text already no tags";
416         $this->assertEqual($text, shorten_text($text));
418         $text = "<p>short <b>text</b> already</p><p>with tags</p>";
419         $this->assertEqual($text, shorten_text($text));
421         $text = "long text without any tags blah de blah blah blah what";
422         $this->assertEqual('long text without any tags ...', shorten_text($text));
424         $text = "<div class='frog'><p><blockquote>Long text with tags that will ".
425             "be chopped off but <b>should be added back again</b></blockquote></p></div>";
426         $this->assertEqual("<div class='frog'><p><blockquote>Long text with " .
427             "tags that ...</blockquote></p></div>", shorten_text($text));
429         $text = "some text which shouldn't &nbsp; break there";
430         $this->assertEqual("some text which shouldn't &nbsp; ...",
431             shorten_text($text, 31));
432         $this->assertEqual("some text which shouldn't ...",
433             shorten_text($text, 30));
435         // This case caused a bug up to 1.9.5
436         $text = "<h3>standard 'break-out' sub groups in TGs?</h3>&nbsp;&lt;&lt;There are several";
437         $this->assertEqual("<h3>standard 'break-out' sub groups in ...</h3>",
438             shorten_text($text, 43));
440         $text = "<h1>123456789</h1>";//a string with no convenient breaks
441         $this->assertEqual("<h1>12345...</h1>",
442             shorten_text($text, 8));
443     }
445     function test_usergetdate() {
446         global $USER;
448         $userstimezone = $USER->timezone;
449         $USER->timezone = 2;//set the timezone to a known state
451         $ts = 1261540267; //the time this function was created
453         $arr = usergetdate($ts,1);//specify the timezone as an argument
454         $arr = array_values($arr);
456         list($seconds,$minutes,$hours,$mday,$wday,$mon,$year,$yday,$weekday,$month) = $arr;
457         $this->assertEqual($seconds,7);
458         $this->assertEqual($minutes,51);
459         $this->assertEqual($hours,4);
460         $this->assertEqual($mday,23);
461         $this->assertEqual($wday,3);
462         $this->assertEqual($mon,12);
463         $this->assertEqual($year,2009);
464         $this->assertEqual($yday,357);
465         $this->assertEqual($weekday,'Wednesday');
466         $this->assertEqual($month,'December');
468         $arr = usergetdate($ts);//gets the timezone from the $USER object
469         $arr = array_values($arr);
471         list($seconds,$minutes,$hours,$mday,$wday,$mon,$year,$yday,$weekday,$month) = $arr;
472         $this->assertEqual($seconds,7);
473         $this->assertEqual($minutes,51);
474         $this->assertEqual($hours,5);
475         $this->assertEqual($mday,23);
476         $this->assertEqual($wday,3);
477         $this->assertEqual($mon,12);
478         $this->assertEqual($year,2009);
479         $this->assertEqual($yday,357);
480         $this->assertEqual($weekday,'Wednesday');
481         $this->assertEqual($month,'December');
483         //set the timezone back to what it was
484         $USER->timezone = $userstimezone;
485     }