MDL-29401 few more tests for Eloy
[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.0' => array('Windows 98' => 'Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)'),
51                 '5.5' => array('Windows 2000' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)'),
52                 '6.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'),
53                 '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)'),
54                 '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                 '9.0' => array('Windows 7' => 'Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))'),
57             ),
58             'Firefox' => array(
59                 '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'),
60                 '1.5'     => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8) Gecko/20051107 Firefox/1.5'),
61                 '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'),
62                 '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',
63                                    'Ubuntu Linux AMD64' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20060601 Firefox/2.0 (Ubuntu-edgy)'),
64                 '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'),
65             ),
66             'Safari' => array(
67                 '312' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312'),
68                 '412' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412')
69             ),
70             'Safari iOS' => array(
71                 '528' => array('iPhone' => 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; cs-cz) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16'),
72                 '533' => array('iPad' => 'Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5'),
73             ),
74             'WebKit Android' => array(
75                 '525' => array('G1 Phone' => 'Mozilla/5.0 (Linux; U; Android 1.1; en-gb; dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2 – G1 Phone'),
76                 '530' => array('Nexus' => 'Mozilla/5.0 (Linux; U; Android 2.1; en-us; Nexus One Build/ERD62) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17 –Nexus'),
77             ),
78             'Chrome' => array(
79                 '8' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.215 Safari/534.10'),
80             ),
81             'Opera' => array(
82                 '8.51' => array('Windows XP' => 'Opera/8.51 (Windows NT 5.1; U; en)'),
83                 '9.0'  => array('Windows XP' => 'Opera/9.0 (Windows NT 5.1; U; en)',
84                                 'Debian Linux' => 'Opera/9.01 (X11; Linux i686; U; en)')
85             )
86         );
88     function test_cleanremoteaddr() {
89         //IPv4
90         $this->assertEqual(cleanremoteaddr('1023.121.234.1'), null);
91         $this->assertEqual(cleanremoteaddr('123.121.234.01 '), '123.121.234.1');
93         //IPv6
94         $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:0:0'), null);
95         $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:abh'), null);
96         $this->assertEqual(cleanremoteaddr('0:0:0:::0:0:1'), null);
97         $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:0', true), '::');
98         $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:1:1', true), '::1:1');
99         $this->assertEqual(cleanremoteaddr('abcd:00ef:0:0:0:0:0:0', true), 'abcd:ef::');
100         $this->assertEqual(cleanremoteaddr('1:0:0:0:0:0:0:1', true), '1::1');
101         $this->assertEqual(cleanremoteaddr('::10:1', false), '0:0:0:0:0:0:10:1');
102         $this->assertEqual(cleanremoteaddr('01:1::', false), '1:1:0:0:0:0:0:0');
103         $this->assertEqual(cleanremoteaddr('10::10', false), '10:0:0:0:0:0:0:10');
104         $this->assertEqual(cleanremoteaddr('::ffff:192.168.1.1', true), '::ffff:c0a8:11');
105     }
107     function test_address_in_subnet() {
108     /// 1: xxx.xxx.xxx.xxx/nn or xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/nnn          (number of bits in net mask)
109         $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.1/32'));
110         $this->assertFalse(address_in_subnet('123.121.23.1', '123.121.23.0/32'));
111         $this->assertTrue(address_in_subnet('10.10.10.100',  '123.121.23.45/0'));
112         $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.0/24'));
113         $this->assertFalse(address_in_subnet('123.121.34.1', '123.121.234.0/24'));
114         $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.0/30'));
115         $this->assertFalse(address_in_subnet('123.121.23.8', '123.121.23.0/30'));
116         $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba/128'));
117         $this->assertFalse(address_in_subnet('bab:baba::baba', 'bab:baba::cece/128'));
118         $this->assertTrue(address_in_subnet('baba:baba::baba', 'cece:cece::cece/0'));
119         $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba/128'));
120         $this->assertTrue(address_in_subnet('baba:baba::00ba', 'baba:baba::/120'));
121         $this->assertFalse(address_in_subnet('baba:baba::aba', 'baba:baba::/120'));
122         $this->assertTrue(address_in_subnet('baba::baba:00ba', 'baba::baba:0/112'));
123         $this->assertFalse(address_in_subnet('baba::aba:00ba', 'baba::baba:0/112'));
124         $this->assertFalse(address_in_subnet('aba::baba:0000', 'baba::baba:0/112'));
126         // fixed input
127         $this->assertTrue(address_in_subnet('123.121.23.1   ', ' 123.121.23.0 / 24'));
128         $this->assertTrue(address_in_subnet('::ffff:10.1.1.1', ' 0:0:0:000:0:ffff:a1:10 / 126'));
130         // incorrect input
131         $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/-2'));
132         $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/64'));
133         $this->assertFalse(address_in_subnet('123.121.234.x', '123.121.234.1/24'));
134         $this->assertFalse(address_in_subnet('123.121.234.0', '123.121.234.xx/24'));
135         $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/xx0'));
136         $this->assertFalse(address_in_subnet('::1', '::aa:0/xx0'));
137         $this->assertFalse(address_in_subnet('::1', '::aa:0/-5'));
138         $this->assertFalse(address_in_subnet('::1', '::aa:0/130'));
139         $this->assertFalse(address_in_subnet('x:1', '::aa:0/130'));
140         $this->assertFalse(address_in_subnet('::1', '::ax:0/130'));
143     /// 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)
144         $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12-14'));
145         $this->assertTrue(address_in_subnet('123.121.234.13', '123.121.234.12-14'));
146         $this->assertTrue(address_in_subnet('123.121.234.14', '123.121.234.12-14'));
147         $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.12-14'));
148         $this->assertFalse(address_in_subnet('123.121.234.20', '123.121.234.12-14'));
149         $this->assertFalse(address_in_subnet('123.121.23.12', '123.121.234.12-14'));
150         $this->assertFalse(address_in_subnet('123.12.234.12', '123.121.234.12-14'));
151         $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba-babe'));
152         $this->assertTrue(address_in_subnet('baba:baba::babc', 'baba:baba::baba-babe'));
153         $this->assertTrue(address_in_subnet('baba:baba::babe', 'baba:baba::baba-babe'));
154         $this->assertFalse(address_in_subnet('bab:baba::bab0', 'bab:baba::baba-babe'));
155         $this->assertFalse(address_in_subnet('bab:baba::babf', 'bab:baba::baba-babe'));
156         $this->assertFalse(address_in_subnet('bab:baba::bfbe', 'bab:baba::baba-babe'));
157         $this->assertFalse(address_in_subnet('bfb:baba::babe', 'bab:baba::baba-babe'));
159         // fixed input
160         $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12 - 14 '));
161         $this->assertTrue(address_in_subnet('bab:baba::babe', 'bab:baba::baba - babe  '));
163         // incorrect input
164         $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12-234.14'));
165         $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12-256'));
166         $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12--256'));
169     /// 3: xxx.xxx or xxx.xxx. or xxx:xxx:xxxx or xxx:xxx:xxxx.                  (incomplete address, a bit non-technical ;-)
170         $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12'));
171         $this->assertFalse(address_in_subnet('123.121.23.12', '123.121.23.13'));
172         $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.'));
173         $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234'));
174         $this->assertTrue(address_in_subnet('123.121.234.12', '123.121'));
175         $this->assertTrue(address_in_subnet('123.121.234.12', '123'));
176         $this->assertFalse(address_in_subnet('123.121.234.1', '12.121.234.'));
177         $this->assertFalse(address_in_subnet('123.121.234.1', '12.121.234'));
178         $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:baba::bab'));
179         $this->assertFalse(address_in_subnet('baba:baba::ba', 'baba:baba::bc'));
180         $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:baba'));
181         $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:'));
182         $this->assertFalse(address_in_subnet('bab:baba::bab', 'baba:'));
185     /// multiple subnets
186         $this->assertTrue(address_in_subnet('123.121.234.12', '::1/64, 124., 123.121.234.10-30'));
187         $this->assertTrue(address_in_subnet('124.121.234.12', '::1/64, 124., 123.121.234.10-30'));
188         $this->assertTrue(address_in_subnet('::2',            '::1/64, 124., 123.121.234.10-30'));
189         $this->assertFalse(address_in_subnet('12.121.234.12', '::1/64, 124., 123.121.234.10-30'));
192     /// other incorrect input
193         $this->assertFalse(address_in_subnet('123.123.123.123', ''));
194     }
196     /**
197      * Modifies $_SERVER['HTTP_USER_AGENT'] manually to check if check_browser_version
198      * works as expected.
199      */
200     function test_check_browser_version()
201     {
202         global $CFG;
204         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['412']['Mac OS X'];
205         $this->assertTrue(check_browser_version('Safari'));
206         $this->assertTrue(check_browser_version('WebKit'));
207         $this->assertTrue(check_browser_version('Safari', '312'));
208         $this->assertFalse(check_browser_version('Safari', '500'));
209         $this->assertFalse(check_browser_version('Chrome'));
210         $this->assertFalse(check_browser_version('Safari iOS'));
212         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari iOS']['528']['iPhone'];
213         $this->assertTrue(check_browser_version('Safari iOS'));
214         $this->assertTrue(check_browser_version('WebKit'));
215         $this->assertTrue(check_browser_version('Safari iOS', '527'));
216         $this->assertFalse(check_browser_version('Safari iOS', 590));
217         $this->assertFalse(check_browser_version('Safari', '312'));
218         $this->assertFalse(check_browser_version('Safari', '500'));
219         $this->assertFalse(check_browser_version('Chrome'));
221         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['WebKit Android']['530']['Nexus'];
222         $this->assertTrue(check_browser_version('WebKit'));
223         $this->assertTrue(check_browser_version('WebKit Android', '527'));
224         $this->assertFalse(check_browser_version('WebKit Android', 590));
225         $this->assertFalse(check_browser_version('Safari'));
226         $this->assertFalse(check_browser_version('Chrome'));
228         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Chrome']['8']['Mac OS X'];
229         $this->assertTrue(check_browser_version('Chrome'));
230         $this->assertTrue(check_browser_version('WebKit'));
231         $this->assertTrue(check_browser_version('Chrome', 8));
232         $this->assertFalse(check_browser_version('Chrome', 10));
233         $this->assertFalse(check_browser_version('Safari', '1'));
235         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
236         $this->assertTrue(check_browser_version('Opera'));
237         $this->assertTrue(check_browser_version('Opera', '8.0'));
238         $this->assertFalse(check_browser_version('Opera', '10.0'));
240         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
241         $this->assertTrue(check_browser_version('MSIE'));
242         $this->assertTrue(check_browser_version('MSIE', '5.0'));
243         $this->assertFalse(check_browser_version('MSIE', '7.0'));
245         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['5.0']['Windows 98'];
246         $this->assertFalse(check_browser_version('MSIE'));
247         $this->assertTrue(check_browser_version('MSIE', 0));
248         $this->assertTrue(check_browser_version('MSIE', '5.0'));
249         $this->assertFalse(check_browser_version('MSIE', '7.0'));
251         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['9.0']['Windows 7'];
252         $this->assertTrue(check_browser_version('MSIE'));
253         $this->assertTrue(check_browser_version('MSIE', 0));
254         $this->assertTrue(check_browser_version('MSIE', '5.0'));
255         $this->assertTrue(check_browser_version('MSIE', '9.0'));
256         $this->assertFalse(check_browser_version('MSIE', '10'));
258         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
259         $this->assertTrue(check_browser_version('Firefox'));
260         $this->assertTrue(check_browser_version('Firefox', '1.5'));
261         $this->assertFalse(check_browser_version('Firefox', '3.0'));
262     }
264     function test_get_browser_version_classes() {
265         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['412']['Mac OS X'];
266         $this->assertEqual(array('safari'), get_browser_version_classes());
268         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Chrome']['8']['Mac OS X'];
269         $this->assertEqual(array('safari'), get_browser_version_classes());
271         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari iOS']['528']['iPhone'];
272         $this->assertEqual(array('safari', 'ios'), get_browser_version_classes());
274         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['WebKit Android']['530']['Nexus'];
275         $this->assertEqual(array('safari', 'android'), get_browser_version_classes());
277         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Chrome']['8']['Mac OS X'];
278         $this->assertEqual(array('safari'), get_browser_version_classes());
280         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
281         $this->assertEqual(array('opera'), get_browser_version_classes());
283         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
284         $this->assertEqual(array('ie', 'ie6'), get_browser_version_classes());
286         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['7.0']['Windows XP SP2'];
287         $this->assertEqual(array('ie', 'ie7'), get_browser_version_classes());
289         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['8.0']['Windows Vista'];
290         $this->assertEqual(array('ie', 'ie8'), get_browser_version_classes());
292         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
293         $this->assertEqual(array('gecko', 'gecko18'), get_browser_version_classes());
295         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['3.0.6']['SUSE'];
296         $this->assertEqual(array('gecko', 'gecko19'), get_browser_version_classes());
297     }
299     function test_fix_utf8() {
300         // make sure valid data including other types is not changed
301         $this->assertidentical(null, fix_utf8(null));
302         $this->assertidentical(1, fix_utf8(1));
303         $this->assertidentical(1.1, fix_utf8(1.1));
304         $this->assertidentical(true, fix_utf8(true));
305         $this->assertidentical('', fix_utf8(''));
306         $array = array('do', 're', 'mi');
307         $this->assertidentical($array, fix_utf8($array));
308         $object = new stdClass();
309         $object->a = 'aa';
310         $object->b = 'bb';
311         $this->assertidentical($object, fix_utf8($object));
313         // valid utf8 string
314         $this->assertidentical("žlutý koníček přeskočil potůček \n\t\r\0", fix_utf8("žlutý koníček přeskočil potůček \n\t\r\0"));
316         // invalid utf8 string
317         $this->assertidentical('aaabbb', fix_utf8('aaa'.chr(130).'bbb'));
318     }
320     function test_optional_param() {
321         global $CFG;
323         $_POST['username'] = 'post_user';
324         $_GET['username'] = 'get_user';
325         $this->assertIdentical(optional_param('username', 'default_user', PARAM_RAW), $_POST['username']);
327         unset($_POST['username']);
328         $this->assertIdentical(optional_param('username', 'default_user', PARAM_RAW), $_GET['username']);
330         unset($_GET['username']);
331         $this->assertIdentical(optional_param('username', 'default_user', PARAM_RAW), 'default_user');
333         // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2
334         $_POST['username'] = 'post_user';
335         try {
336             optional_param('username', 'default_user', null);
337             $this->fail('coding_exception expected');
338         } catch (coding_exception $ex) {
339             $this->assertTrue(true);
340         }
341         try {
342             @optional_param('username', 'default_user');
343             $this->fail('coding_exception expected');
344         } catch (coding_exception $ex) {
345             $this->assertTrue(true);
346         }
347         try {
348             @optional_param('username');
349             $this->fail('coding_exception expected');
350         } catch (coding_exception $ex) {
351             $this->assertTrue(true);
352         }
353         try {
354             optional_param('', 'default_user', PARAM_RAW);
355             $this->fail('coding_exception expected');
356         } catch (coding_exception $ex) {
357             $this->assertTrue(true);
358         }
360         // make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3
361         $debugging = isset($CFG->debug) ? $CFG->debug : null;
362         $debugdisplay = isset($CFG->debugdisplay) ? $CFG->debugdisplay : null;
363         $CFG->debug = 38911;
364         $CFG->debugdisplay = true;
366         ob_start();
367         $this->assertIdentical(optional_param('username', 'default_user', PARAM_RAW), $_POST['username']);
368         $d = ob_end_clean();
369         $this->assertTrue($d !== '');
371         if ($debugging !== null) {
372             $CFG->debug = $debugging;
373         } else {
374             unset($CFG->debug);
375         }
376         if ($debugdisplay !== null) {
377             $CFG->debugdisplay = $debugdisplay;
378         } else {
379             unset($CFG->debugdisplay);
380         }
381     }
383     function test_optional_param_array() {
384         global $CFG;
386         $_POST['username'] = array('a'=>'post_user');
387         $_GET['username'] = array('a'=>'get_user');
388         $this->assertIdentical(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), $_POST['username']);
390         unset($_POST['username']);
391         $this->assertIdentical(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), $_GET['username']);
393         unset($_GET['username']);
394         $this->assertIdentical(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), array('a'=>'default_user'));
396         // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2
397         $_POST['username'] = array('a'=>'post_user');
398         try {
399             optional_param_array('username', array('a'=>'default_user'), null);
400             $this->fail('coding_exception expected');
401         } catch (coding_exception $ex) {
402             $this->assertTrue(true);
403         }
404         try {
405             @optional_param_array('username', array('a'=>'default_user'));
406             $this->fail('coding_exception expected');
407         } catch (coding_exception $ex) {
408             $this->assertTrue(true);
409         }
410         try {
411             @optional_param_array('username');
412             $this->fail('coding_exception expected');
413         } catch (coding_exception $ex) {
414             $this->assertTrue(true);
415         }
416         try {
417             optional_param_array('', array('a'=>'default_user'), PARAM_RAW);
418             $this->fail('coding_exception expected');
419         } catch (coding_exception $ex) {
420             $this->assertTrue(true);
421         }
423         // do not allow nested arrays
424         try {
425             $_POST['username'] = array('a'=>array('b'=>'post_user'));
426             optional_param_array('username', array('a'=>'default_user'), PARAM_RAW);
427             $this->fail('coding_exception expected');
428         } catch (coding_exception $ex) {
429             $this->assertTrue(true);
430         }
432         // do not allow non-arrays
433         $debugging = isset($CFG->debug) ? $CFG->debug : null;
434         $debugdisplay = isset($CFG->debugdisplay) ? $CFG->debugdisplay : null;
435         $CFG->debug = 38911;
436         $CFG->debugdisplay = true;
438         ob_start();
439         $_POST['username'] = 'post_user';
440         $this->assertIdentical(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), array('a'=>'default_user'));
441         $d = ob_end_clean();
442         $this->assertTrue($d !== '');
444         // make sure array keys are sanitised
445         ob_start();
446         $_POST['username'] = array('abc123_;-/*-+ '=>'arrggh', 'a1_-'=>'post_user');
447         $this->assertIdentical(optional_param_array('username', array(), PARAM_RAW), array('a1_-'=>'post_user'));
448         $d = ob_end_clean();
449         $this->assertTrue($d !== '');
451         if ($debugging !== null) {
452             $CFG->debug = $debugging;
453         } else {
454             unset($CFG->debug);
455         }
456         if ($debugdisplay !== null) {
457             $CFG->debugdisplay = $debugdisplay;
458         } else {
459             unset($CFG->debugdisplay);
460         }
461     }
463     function test_required_param() {
464         global $CFG;
466         $_POST['username'] = 'post_user';
467         $_GET['username'] = 'get_user';
468         $this->assertIdentical(required_param('username', PARAM_RAW), 'post_user');
470         unset($_POST['username']);
471         $this->assertIdentical(required_param('username', PARAM_RAW), 'get_user');
473         unset($_GET['username']);
474         try {
475             $this->assertIdentical(required_param('username', PARAM_RAW), 'default_user');
476             $this->fail('moodle_exception expected');
477         } catch (moodle_exception $ex) {
478             $this->assertTrue(true);
479         }
481         // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2
482         $_POST['username'] = 'post_user';
483         try {
484             @required_param('username');
485             $this->fail('coding_exception expected');
486         } catch (coding_exception $ex) {
487             $this->assertTrue(true);
488         }
489         try {
490             required_param('username', '');
491             $this->fail('coding_exception expected');
492         } catch (coding_exception $ex) {
493             $this->assertTrue(true);
494         }
495         try {
496             required_param('', PARAM_RAW);
497             $this->fail('coding_exception expected');
498         } catch (coding_exception $ex) {
499             $this->assertTrue(true);
500         }
502         // make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3
503         $debugging = isset($CFG->debug) ? $CFG->debug : null;
504         $debugdisplay = isset($CFG->debugdisplay) ? $CFG->debugdisplay : null;
505         $CFG->debug = 38911;
506         $CFG->debugdisplay = true;
508         ob_start();
509         $this->assertIdentical(required_param('username', PARAM_RAW), $_POST['username']);
510         $d = ob_end_clean();
511         $this->assertTrue($d !== '');
513         if ($debugging !== null) {
514             $CFG->debug = $debugging;
515         } else {
516             unset($CFG->debug);
517         }
518         if ($debugdisplay !== null) {
519             $CFG->debugdisplay = $debugdisplay;
520         } else {
521             unset($CFG->debugdisplay);
522         }
523     }
525     function test_required_param_array() {
526         global $CFG;
528         $_POST['username'] = array('a'=>'post_user');
529         $_GET['username'] = array('a'=>'get_user');
530         $this->assertIdentical(required_param_array('username', PARAM_RAW), $_POST['username']);
532         unset($_POST['username']);
533         $this->assertIdentical(required_param_array('username', PARAM_RAW), $_GET['username']);
535         // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2
536         $_POST['username'] = array('a'=>'post_user');
537         try {
538             required_param_array('username', null);
539             $this->fail('coding_exception expected');
540         } catch (coding_exception $ex) {
541             $this->assertTrue(true);
542         }
543         try {
544             @required_param_array('username');
545             $this->fail('coding_exception expected');
546         } catch (coding_exception $ex) {
547             $this->assertTrue(true);
548         }
549         try {
550             required_param_array('', PARAM_RAW);
551             $this->fail('coding_exception expected');
552         } catch (coding_exception $ex) {
553             $this->assertTrue(true);
554         }
556         // do not allow nested arrays
557         try {
558             $_POST['username'] = array('a'=>array('b'=>'post_user'));
559             required_param_array('username', PARAM_RAW);
560             $this->fail('coding_exception expected');
561         } catch (coding_exception $ex) {
562             $this->assertTrue(true);
563         }
565         // do not allow non-arrays
566         try {
567             $_POST['username'] = 'post_user';
568             required_param_array('username', PARAM_RAW);
569             $this->fail('moodle_exception expected');
570         } catch (moodle_exception $ex) {
571             $this->assertTrue(true);
572         }
574         // do not allow non-arrays
575         $debugging = isset($CFG->debug) ? $CFG->debug : null;
576         $debugdisplay = isset($CFG->debugdisplay) ? $CFG->debugdisplay : null;
577         $CFG->debug = 38911;
578         $CFG->debugdisplay = true;
580         // make sure array keys are sanitised
581         ob_start();
582         $_POST['username'] = array('abc123_;-/*-+ '=>'arrggh', 'a1_-'=>'post_user');
583         $this->assertIdentical(required_param_array('username', PARAM_RAW), array('a1_-'=>'post_user'));
584         $d = ob_end_clean();
585         $this->assertTrue($d !== '');
587         if ($debugging !== null) {
588             $CFG->debug = $debugging;
589         } else {
590             unset($CFG->debug);
591         }
592         if ($debugdisplay !== null) {
593             $CFG->debugdisplay = $debugdisplay;
594         } else {
595             unset($CFG->debugdisplay);
596         }
597     }
599     function test_clean_param() {
600         // forbid objects and arrays
601         try {
602             clean_param(array('x', 'y'), PARAM_RAW);
603             $this->fail('coding_exception expected');
604         } catch (coding_exception $ex) {
605             $this->assertTrue(true);
606         }
607         try {
608             $param = new stdClass();
609             $param->id = 1;
610             clean_param($param, PARAM_RAW);
611             $this->fail('coding_exception expected');
612         } catch (coding_exception $ex) {
613             $this->assertTrue(true);
614         }
616         // require correct type
617         try {
618             clean_param('x', 'xxxxxx');
619             $this->fail('moodle_exception expected');
620         } catch (moodle_exception $ex) {
621             $this->assertTrue(true);
622         }
623         try {
624             @clean_param('x');
625             $this->fail('moodle_exception expected');
626         } catch (moodle_exception $ex) {
627             $this->assertTrue(true);
628         }
630     }
632     function test_clean_param_array() {
633         $this->assertIdentical(clean_param_array(null, PARAM_RAW), array());
634         $this->assertIdentical(clean_param_array(array('a', 'b'), PARAM_RAW), array('a', 'b'));
635         $this->assertIdentical(clean_param_array(array('a', array('b')), PARAM_RAW, true), array('a', array('b')));
637         // require correct type
638         try {
639             clean_param_array(array('x'), 'xxxxxx');
640             $this->fail('moodle_exception expected');
641         } catch (moodle_exception $ex) {
642             $this->assertTrue(true);
643         }
644         try {
645             @clean_param_array(array('x'));
646             $this->fail('moodle_exception expected');
647         } catch (moodle_exception $ex) {
648             $this->assertTrue(true);
649         }
651         try {
652             clean_param_array(array('x', array('y')), PARAM_RAW);
653             $this->fail('coding_exception expected');
654         } catch (coding_exception $ex) {
655             $this->assertTrue(true);
656         }
658         // test recursive
659     }
661     function test_clean_param_raw() {
662         $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_RAW),
663             '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)');
664     }
666     function test_clean_param_trim() {
667         $this->assertEqual(clean_param("   Frog toad   \r\n  ", PARAM_RAW_TRIMMED), 'Frog toad');
668     }
670     function test_clean_param_clean() {
671         // PARAM_CLEAN is an ugly hack, do not use in new code (skodak)
672         // instead use more specific type, or submit sothing that can be verified properly
673         $this->assertEqual(clean_param('xx<script>', PARAM_CLEAN), 'xx');
674     }
676     function test_clean_param_alpha() {
677         $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHA),
678                 'DSFMOSDJ');
679     }
681     function test_clean_param_alphanum() {
682         $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHANUM),
683                 '978942897DSFMOSDJ');
684     }
686     function test_clean_param_alphaext() {
687         $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHAEXT),
688                 'DSFMOSDJ');
689     }
691     function test_clean_param_sequence() {
692         $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_SEQUENCE),
693                 ',9789,42897');
694     }
696     function test_clean_param_component() {
697         // please note the cleaning of component names is very strict, no guessing here
698         $this->assertIdentical(clean_param('mod_forum', PARAM_COMPONENT), 'mod_forum');
699         $this->assertIdentical(clean_param('block_online_users', PARAM_COMPONENT), 'block_online_users');
700         $this->assertIdentical(clean_param('block_blond_online_users', PARAM_COMPONENT), 'block_blond_online_users');
701         $this->assertIdentical(clean_param('mod_something2', PARAM_COMPONENT), 'mod_something2');
702         $this->assertIdentical(clean_param('forum', PARAM_COMPONENT), 'forum');
703         $this->assertIdentical(clean_param('user', PARAM_COMPONENT), 'user');
704         $this->assertIdentical(clean_param('rating', PARAM_COMPONENT), 'rating');
705         $this->assertIdentical(clean_param('mod_2something', PARAM_COMPONENT), '');
706         $this->assertIdentical(clean_param('2mod_something', PARAM_COMPONENT), '');
707         $this->assertIdentical(clean_param('mod_something_xx', PARAM_COMPONENT), '');
708         $this->assertIdentical(clean_param('auth_something__xx', PARAM_COMPONENT), '');
709         $this->assertIdentical(clean_param('mod_Something', PARAM_COMPONENT), '');
710         $this->assertIdentical(clean_param('mod_somethíng', PARAM_COMPONENT), '');
711         $this->assertIdentical(clean_param('auth_xx-yy', PARAM_COMPONENT), '');
712         $this->assertIdentical(clean_param('_auth_xx', PARAM_COMPONENT), '');
713         $this->assertIdentical(clean_param('a2uth_xx', PARAM_COMPONENT), '');
714         $this->assertIdentical(clean_param('auth_xx_', PARAM_COMPONENT), '');
715         $this->assertIdentical(clean_param('auth_xx.old', PARAM_COMPONENT), '');
716         $this->assertIdentical(clean_param('_user', PARAM_COMPONENT), '');
717         $this->assertIdentical(clean_param('2rating', PARAM_COMPONENT), '');
718         $this->assertIdentical(clean_param('user_', PARAM_COMPONENT), '');
719     }
721     function test_clean_param_plugin() {
722         // please note the cleaning of plugin names is very strict, no guessing here
723         $this->assertIdentical(clean_param('forum', PARAM_PLUGIN), 'forum');
724         $this->assertIdentical(clean_param('forum2', PARAM_PLUGIN), 'forum2');
725         $this->assertIdentical(clean_param('online_users', PARAM_PLUGIN), 'online_users');
726         $this->assertIdentical(clean_param('blond_online_users', PARAM_PLUGIN), 'blond_online_users');
727         $this->assertIdentical(clean_param('online__users', PARAM_PLUGIN), '');
728         $this->assertIdentical(clean_param('forum ', PARAM_PLUGIN), '');
729         $this->assertIdentical(clean_param('forum.old', PARAM_PLUGIN), '');
730         $this->assertIdentical(clean_param('xx-yy', PARAM_PLUGIN), '');
731         $this->assertIdentical(clean_param('2xx', PARAM_PLUGIN), '');
732         $this->assertIdentical(clean_param('Xx', PARAM_PLUGIN), '');
733         $this->assertIdentical(clean_param('_xx', PARAM_PLUGIN), '');
734         $this->assertIdentical(clean_param('xx_', PARAM_PLUGIN), '');
735     }
737     function test_clean_param_area() {
738         // please note the cleaning of area names is very strict, no guessing here
739         $this->assertIdentical(clean_param('something', PARAM_AREA), 'something');
740         $this->assertIdentical(clean_param('something2', PARAM_AREA), 'something2');
741         $this->assertIdentical(clean_param('some_thing', PARAM_AREA), 'some_thing');
742         $this->assertIdentical(clean_param('some_thing_xx', PARAM_AREA), 'some_thing_xx');
743         $this->assertIdentical(clean_param('_something', PARAM_AREA), '');
744         $this->assertIdentical(clean_param('something_', PARAM_AREA), '');
745         $this->assertIdentical(clean_param('2something', PARAM_AREA), '');
746         $this->assertIdentical(clean_param('Something', PARAM_AREA), '');
747         $this->assertIdentical(clean_param('some-thing', PARAM_AREA), '');
748         $this->assertIdentical(clean_param('somethííng', PARAM_AREA), '');
749         $this->assertIdentical(clean_param('something.x', PARAM_AREA), '');
750     }
752     function test_clean_param_text() {
753         $this->assertEqual(PARAM_TEXT, PARAM_MULTILANG);
754         //standard
755         $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>');
756         $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>');
757         $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>');
758         //malformed
759         $this->assertEqual(clean_param('<span lang="en" class="multilang">aa</span>', PARAM_TEXT), '<span lang="en" class="multilang">aa</span>');
760         $this->assertEqual(clean_param('<span lang="en" class="nothing" class="multilang">aa</span>', PARAM_TEXT), 'aa');
761         $this->assertEqual(clean_param('<lang lang="en" class="multilang">aa</lang>', PARAM_TEXT), 'aa');
762         $this->assertEqual(clean_param('<lang lang="en!!">aa</lang>', PARAM_TEXT), 'aa');
763         $this->assertEqual(clean_param('<span lang="en==" class="multilang">aa</span>', PARAM_TEXT), 'aa');
764         $this->assertEqual(clean_param('a<em>b</em>c', PARAM_TEXT), 'abc');
765         $this->assertEqual(clean_param('a><xx >c>', PARAM_TEXT), 'a>c>'); // standard strip_tags() behaviour
766         $this->assertEqual(clean_param('a<b', PARAM_TEXT), 'a');
767         $this->assertEqual(clean_param('a>b', PARAM_TEXT), 'a>b');
768         $this->assertEqual(clean_param('<lang lang="en">a>a</lang>', PARAM_TEXT), '<lang lang="en">a>a</lang>'); // standard strip_tags() behaviour
769         $this->assertEqual(clean_param('<lang lang="en">a<a</lang>', PARAM_TEXT), 'a');
770         $this->assertEqual(clean_param('<lang lang="en">a<br>a</lang>', PARAM_TEXT), '<lang lang="en">aa</lang>');
771     }
773     function test_clean_param_url() {
774         // Test PARAM_URL and PARAM_LOCALURL a bit
775         $this->assertEqual(clean_param('http://google.com/', PARAM_URL), 'http://google.com/');
776         $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/');
777         $this->assertEqual(clean_param('http://localhost/', PARAM_URL), 'http://localhost/');
778         $this->assertEqual(clean_param('http://0.255.1.1/numericip.php', PARAM_URL), 'http://0.255.1.1/numericip.php');
779         $this->assertEqual(clean_param('/just/a/path', PARAM_URL), '/just/a/path');
780         $this->assertEqual(clean_param('funny:thing', PARAM_URL), '');
781     }
783     function test_clean_param_localurl() {
784         global $CFG;
785         $this->assertEqual(clean_param('http://google.com/', PARAM_LOCALURL), '');
786         $this->assertEqual(clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_LOCALURL), '');
787         $this->assertEqual(clean_param($CFG->wwwroot, PARAM_LOCALURL), $CFG->wwwroot);
788         $this->assertEqual(clean_param('/just/a/path', PARAM_LOCALURL), '/just/a/path');
789         $this->assertEqual(clean_param('funny:thing', PARAM_LOCALURL), '');
790         $this->assertEqual(clean_param('course/view.php?id=3', PARAM_LOCALURL), 'course/view.php?id=3');
791     }
793     function test_clean_param_file() {
794         $this->assertEqual(clean_param('correctfile.txt', PARAM_FILE), 'correctfile.txt');
795         $this->assertEqual(clean_param('b\'a<d`\\/fi:l>e.t"x|t', PARAM_FILE), 'badfile.txt');
796         $this->assertEqual(clean_param('../parentdirfile.txt', PARAM_FILE), 'parentdirfile.txt');
797         //The following behaviours have been maintained although they seem a little odd
798         $this->assertEqual(clean_param('funny:thing', PARAM_FILE), 'funnything');
799         $this->assertEqual(clean_param('./currentdirfile.txt', PARAM_FILE), '.currentdirfile.txt');
800         $this->assertEqual(clean_param('c:\temp\windowsfile.txt', PARAM_FILE), 'ctempwindowsfile.txt');
801         $this->assertEqual(clean_param('/home/user/linuxfile.txt', PARAM_FILE), 'homeuserlinuxfile.txt');
802         $this->assertEqual(clean_param('~/myfile.txt', PARAM_FILE), '~myfile.txt');
803     }
805     function test_clean_param_username() {
806         global $CFG;
807         $currentstatus =  $CFG->extendedusernamechars;
809         // Run tests with extended character == FALSE;
810         $CFG->extendedusernamechars = FALSE;
811         $this->assertEqual(clean_param('johndoe123', PARAM_USERNAME), 'johndoe123' );
812         $this->assertEqual(clean_param('john.doe', PARAM_USERNAME), 'john.doe');
813         $this->assertEqual(clean_param('john-doe', PARAM_USERNAME), 'john-doe');
814         $this->assertEqual(clean_param('john- doe', PARAM_USERNAME), 'john-doe');
815         $this->assertEqual(clean_param('john_doe', PARAM_USERNAME), 'john_doe');
816         $this->assertEqual(clean_param('john@doe', PARAM_USERNAME), 'john@doe');
817         $this->assertEqual(clean_param('john~doe', PARAM_USERNAME), 'johndoe');
818         $this->assertEqual(clean_param('john´doe', PARAM_USERNAME), 'johndoe');
819         $this->assertEqual(clean_param('john#$%&() ', PARAM_USERNAME), 'john');
820         $this->assertEqual(clean_param('JOHNdóé ', PARAM_USERNAME), 'johnd');
821         $this->assertEqual(clean_param('john.,:;-_/|\ñÑ[]A_X-,D {} ~!@#$%^&*()_+ ?><[] ščřžžý ?ýá\9e?\9eý??\9adoe ', PARAM_USERNAME), 'john.-_a_x-d@_doe');
824         // Test success condition, if extendedusernamechars == ENABLE;
825         $CFG->extendedusernamechars = TRUE;
826         $this->assertEqual(clean_param('john_doe', PARAM_USERNAME), 'john_doe');
827         $this->assertEqual(clean_param('john@doe', PARAM_USERNAME), 'john@doe');
828         $this->assertEqual(clean_param('john# $%&()+_^', PARAM_USERNAME), 'john#$%&()+_^');
829         $this->assertEqual(clean_param('john~doe', PARAM_USERNAME), 'john~doe');
830         $this->assertEqual(clean_param('joHN´doe', PARAM_USERNAME), 'john´doe');
831         $this->assertEqual(clean_param('johnDOE', PARAM_USERNAME), 'johndoe');
832         $this->assertEqual(clean_param('johndóé ', PARAM_USERNAME), 'johndóé');
834         $CFG->extendedusernamechars = $currentstatus;
835     }
837     function test_clean_param_stringid() {
838         // Test string identifiers validation
839         // valid strings:
840         $this->assertEqual(clean_param('validstring', PARAM_STRINGID), 'validstring');
841         $this->assertEqual(clean_param('mod/foobar:valid_capability', PARAM_STRINGID), 'mod/foobar:valid_capability');
842         $this->assertEqual(clean_param('CZ', PARAM_STRINGID), 'CZ');
843         $this->assertEqual(clean_param('application/vnd.ms-powerpoint', PARAM_STRINGID), 'application/vnd.ms-powerpoint');
844         $this->assertEqual(clean_param('grade2', PARAM_STRINGID), 'grade2');
845         // invalid strings:
846         $this->assertEqual(clean_param('trailing ', PARAM_STRINGID), '');
847         $this->assertEqual(clean_param('space bar', PARAM_STRINGID), '');
848         $this->assertEqual(clean_param('0numeric', PARAM_STRINGID), '');
849         $this->assertEqual(clean_param('*', PARAM_STRINGID), '');
850         $this->assertEqual(clean_param(' ', PARAM_STRINGID), '');
851     }
853     function test_clean_param_timezone() {
854         // Test timezone validation
855         $testvalues = array (
856             'America/Jamaica'                => 'America/Jamaica',
857             'America/Argentina/Cordoba'      => 'America/Argentina/Cordoba',
858             'America/Port-au-Prince'         => 'America/Port-au-Prince',
859             'America/Argentina/Buenos_Aires' => 'America/Argentina/Buenos_Aires',
860             'PST8PDT'                        => 'PST8PDT',
861             'Wrong.Value'                    => '',
862             'Wrong/.Value'                   => '',
863             'Wrong(Value)'                   => '',
864             '0'                              => '0',
865             '0.0'                            => '0.0',
866             '0.5'                            => '0.5',
867             '-12.5'                          => '-12.5',
868             '+12.5'                          => '+12.5',
869             '13.5'                           => '',
870             '-13.5'                          => '',
871             '0.2'                            => '');
873         foreach ($testvalues as $testvalue => $expectedvalue) {
874             $actualvalue = clean_param($testvalue, PARAM_TIMEZONE);
875             $this->assertEqual($actualvalue, $expectedvalue);
876         }
877     }
879     function test_validate_param() {
880         try {
881             $param = validate_param('11a', PARAM_INT);
882             $this->fail('invalid_parameter_exception expected');
883         } catch (invalid_parameter_exception $ex) {
884             $this->assertTrue(true);
885         }
886         try {
887             $param = validate_param('11', PARAM_INT);
888             $this->assertEqual($param, 11);
889         } catch (invalid_parameter_exception $ex) {
890             $this->fail('invalid_parameter_exception not expected');
891         }
892         try {
893             $param = validate_param(null, PARAM_INT, false);
894             $this->fail('invalid_parameter_exception expected');
895         } catch (invalid_parameter_exception $ex) {
896             $this->assertTrue(true);
897         }
898         try {
899             $param = validate_param(null, PARAM_INT, true);
900             $this->assertTrue($param===null);
901         } catch (invalid_parameter_exception $ex) {
902             $this->fail('invalid_parameter_exception expected');
903         }
904         try {
905             $param = validate_param(array(), PARAM_INT);
906             $this->fail('invalid_parameter_exception expected');
907         } catch (invalid_parameter_exception $ex) {
908             $this->assertTrue(true);
909         }
910         try {
911             $param = validate_param(new stdClass, PARAM_INT);
912             $this->fail('invalid_parameter_exception expected');
913         } catch (invalid_parameter_exception $ex) {
914             $this->assertTrue(true);
915         }
916     }
918     function test_shorten_text() {
919         $text = "short text already no tags";
920         $this->assertEqual($text, shorten_text($text));
922         $text = "<p>short <b>text</b> already</p><p>with tags</p>";
923         $this->assertEqual($text, shorten_text($text));
925         $text = "long text without any tags blah de blah blah blah what";
926         $this->assertEqual('long text without any tags ...', shorten_text($text));
928         $text = "<div class='frog'><p><blockquote>Long text with tags that will ".
929             "be chopped off but <b>should be added back again</b></blockquote></p></div>";
930         $this->assertEqual("<div class='frog'><p><blockquote>Long text with " .
931             "tags that ...</blockquote></p></div>", shorten_text($text));
933         $text = "some text which shouldn't &nbsp; break there";
934         $this->assertEqual("some text which shouldn't &nbsp; ...",
935             shorten_text($text, 31));
936         $this->assertEqual("some text which shouldn't ...",
937             shorten_text($text, 30));
939         // This case caused a bug up to 1.9.5
940         $text = "<h3>standard 'break-out' sub groups in TGs?</h3>&nbsp;&lt;&lt;There are several";
941         $this->assertEqual("<h3>standard 'break-out' sub groups in ...</h3>",
942             shorten_text($text, 43));
944         $text = "<h1>123456789</h1>";//a string with no convenient breaks
945         $this->assertEqual("<h1>12345...</h1>",
946             shorten_text($text, 8));
947     }
949     function test_usergetdate() {
950         global $USER, $CFG;
952         //Check if forcetimezone is set then save it and set it to use user timezone
953         $cfgforcetimezone = null;
954         if (isset($CFG->forcetimezone)) {
955             $cfgforcetimezone = $CFG->forcetimezone;
956             $CFG->forcetimezone = 99; //get user default timezone.
957         }
959         $userstimezone = $USER->timezone;
960         $USER->timezone = 2;//set the timezone to a known state
962         // The string version of date comes from server locale setting and does
963         // not respect user language, so it is necessary to reset that.
964         $oldlocale = setlocale(LC_TIME, '0');
965         setlocale(LC_TIME, 'en_AU.UTF-8');
967         $ts = 1261540267; //the time this function was created
969         $arr = usergetdate($ts,1);//specify the timezone as an argument
970         $arr = array_values($arr);
972         list($seconds,$minutes,$hours,$mday,$wday,$mon,$year,$yday,$weekday,$month) = $arr;
973         $this->assertEqual($seconds,7);
974         $this->assertEqual($minutes,51);
975         $this->assertEqual($hours,4);
976         $this->assertEqual($mday,23);
977         $this->assertEqual($wday,3);
978         $this->assertEqual($mon,12);
979         $this->assertEqual($year,2009);
980         $this->assertEqual($yday,357);
981         $this->assertEqual($weekday, 'Wednesday');
982         $this->assertEqual($month, 'December');
984         $arr = usergetdate($ts);//gets the timezone from the $USER object
985         $arr = array_values($arr);
987         list($seconds,$minutes,$hours,$mday,$wday,$mon,$year,$yday,$weekday,$month) = $arr;
988         $this->assertEqual($seconds,7);
989         $this->assertEqual($minutes,51);
990         $this->assertEqual($hours,5);
991         $this->assertEqual($mday,23);
992         $this->assertEqual($wday,3);
993         $this->assertEqual($mon,12);
994         $this->assertEqual($year,2009);
995         $this->assertEqual($yday,357);
996         $this->assertEqual($weekday, 'Wednesday');
997         $this->assertEqual($month, 'December');
999         //set the timezone back to what it was
1000         $USER->timezone = $userstimezone;
1002         //restore forcetimezone if changed.
1003         if (!is_null($cfgforcetimezone)) {
1004             $CFG->forcetimezone = $cfgforcetimezone;
1005         }
1007         setlocale(LC_TIME, $oldlocale);
1008     }
1010     public function test_normalize_component() {
1012         // moodle core
1013         $this->assertEqual(normalize_component('moodle'), array('core', null));
1014         $this->assertEqual(normalize_component('core'), array('core', null));
1016         // moodle core subsystems
1017         $this->assertEqual(normalize_component('admin'), array('core', 'admin'));
1018         $this->assertEqual(normalize_component('core_admin'), array('core', 'admin'));
1020         // activity modules and their subplugins
1021         $this->assertEqual(normalize_component('workshop'), array('mod', 'workshop'));
1022         $this->assertEqual(normalize_component('mod_workshop'), array('mod', 'workshop'));
1023         $this->assertEqual(normalize_component('workshopform_accumulative'), array('workshopform', 'accumulative'));
1024         $this->assertEqual(normalize_component('quiz'), array('mod', 'quiz'));
1025         $this->assertEqual(normalize_component('quiz_grading'), array('quiz', 'grading'));
1026         $this->assertEqual(normalize_component('data'), array('mod', 'data'));
1027         $this->assertEqual(normalize_component('datafield_checkbox'), array('datafield', 'checkbox'));
1029         // other plugin types
1030         $this->assertEqual(normalize_component('auth_mnet'), array('auth', 'mnet'));
1031         $this->assertEqual(normalize_component('enrol_self'), array('enrol', 'self'));
1032         $this->assertEqual(normalize_component('block_html'), array('block', 'html'));
1033         $this->assertEqual(normalize_component('block_mnet_hosts'), array('block', 'mnet_hosts'));
1034         $this->assertEqual(normalize_component('local_amos'), array('local', 'amos'));
1036         // unknown components are supposed to be activity modules
1037         $this->assertEqual(normalize_component('whothefuckwouldcomewithsuchastupidnameofcomponent'),
1038                 array('mod', 'whothefuckwouldcomewithsuchastupidnameofcomponent'));
1039         $this->assertEqual(normalize_component('whothefuck_wouldcomewithsuchastupidnameofcomponent'),
1040                 array('mod', 'whothefuck_wouldcomewithsuchastupidnameofcomponent'));
1041         $this->assertEqual(normalize_component('whothefuck_would_come_withsuchastupidnameofcomponent'),
1042                 array('mod', 'whothefuck_would_come_withsuchastupidnameofcomponent'));
1043     }
1045     protected function get_fake_preference_test_userid() {
1046         global $DB;
1048         // we need some nonexistent user id
1049         $id = 2147483647 - 666;
1050         if ($DB->get_records('user', array('id'=>$id))) {
1051             //weird!
1052             return false;
1053         }
1054         return $id;
1055     }
1057     public function test_mark_user_preferences_changed() {
1058         if (!$otheruserid = $this->get_fake_preference_test_userid()) {
1059             $this->fail('Can not find unused user id for the preferences test');
1060             return;
1061         }
1063         set_cache_flag('userpreferenceschanged', $otheruserid, NULL);
1064         mark_user_preferences_changed($otheruserid);
1066         $this->assertEqual(get_cache_flag('userpreferenceschanged', $otheruserid, time()-10), 1);
1067         set_cache_flag('userpreferenceschanged', $otheruserid, NULL);
1068     }
1070     public function test_check_user_preferences_loaded() {
1071         global $DB;
1073         if (!$otheruserid = $this->get_fake_preference_test_userid()) {
1074             $this->fail('Can not find unused user id for the preferences test');
1075             return;
1076         }
1078         $DB->delete_records('user_preferences', array('userid'=>$otheruserid));
1079         set_cache_flag('userpreferenceschanged', $otheruserid, NULL);
1081         $user = new stdClass();
1082         $user->id = $otheruserid;
1084         // load
1085         check_user_preferences_loaded($user);
1086         $this->assertTrue(isset($user->preference));
1087         $this->assertTrue(is_array($user->preference));
1088         $this->assertTrue(isset($user->preference['_lastloaded']));
1089         $this->assertEqual(count($user->preference), 1);
1091         // add preference via direct call
1092         $DB->insert_record('user_preferences', array('name'=>'xxx', 'value'=>'yyy', 'userid'=>$user->id));
1094         // no cache reload yet
1095         check_user_preferences_loaded($user);
1096         $this->assertEqual(count($user->preference), 1);
1098         // forced reloading of cache
1099         unset($user->preference);
1100         check_user_preferences_loaded($user);
1101         $this->assertEqual(count($user->preference), 2);
1102         $this->assertEqual($user->preference['xxx'], 'yyy');
1104         // add preference via direct call
1105         $DB->insert_record('user_preferences', array('name'=>'aaa', 'value'=>'bbb', 'userid'=>$user->id));
1107         // test timeouts and modifications from different session
1108         set_cache_flag('userpreferenceschanged', $user->id, 1, time() + 1000);
1109         $user->preference['_lastloaded'] = $user->preference['_lastloaded'] - 20;
1110         check_user_preferences_loaded($user);
1111         $this->assertEqual(count($user->preference), 2);
1112         check_user_preferences_loaded($user, 10);
1113         $this->assertEqual(count($user->preference), 3);
1114         $this->assertEqual($user->preference['aaa'], 'bbb');
1115         set_cache_flag('userpreferenceschanged', $user->id, null);
1116     }
1118     public function test_set_user_preference() {
1119         global $DB, $USER;
1121         if (!$otheruserid = $this->get_fake_preference_test_userid()) {
1122             $this->fail('Can not find unused user id for the preferences test');
1123             return;
1124         }
1126         $DB->delete_records('user_preferences', array('userid'=>$otheruserid));
1127         set_cache_flag('userpreferenceschanged', $otheruserid, null);
1129         $user = new stdClass();
1130         $user->id = $otheruserid;
1132         set_user_preference('aaa', 'bbb', $otheruserid);
1133         $this->assertEqual('bbb', $DB->get_field('user_preferences', 'value', array('userid'=>$otheruserid, 'name'=>'aaa')));
1134         $this->assertEqual('bbb', get_user_preferences('aaa', null, $otheruserid));
1136         set_user_preference('xxx', 'yyy', $user);
1137         $this->assertEqual('yyy', $DB->get_field('user_preferences', 'value', array('userid'=>$otheruserid, 'name'=>'xxx')));
1138         $this->assertEqual('yyy', get_user_preferences('xxx', null, $otheruserid));
1139         $this->assertTrue(is_array($user->preference));
1140         $this->assertEqual($user->preference['aaa'], 'bbb');
1141         $this->assertEqual($user->preference['xxx'], 'yyy');
1143         set_user_preference('xxx', NULL, $user);
1144         $this->assertIdentical(false, $DB->get_field('user_preferences', 'value', array('userid'=>$otheruserid, 'name'=>'xxx')));
1145         $this->assertIdentical(null, get_user_preferences('xxx', null, $otheruserid));
1147         set_user_preference('ooo', true, $user);
1148         $prefs = get_user_preferences(null, null, $otheruserid);
1149         $this->assertIdentical($prefs['aaa'], $user->preference['aaa']);
1150         $this->assertIdentical($prefs['ooo'], $user->preference['ooo']);
1151         $this->assertIdentical($prefs['ooo'], '1');
1153         set_user_preference('null', 0, $user);
1154         $this->assertIdentical('0', get_user_preferences('null', null, $otheruserid));
1156         $this->assertIdentical('lala', get_user_preferences('undefined', 'lala', $otheruserid));
1158         $DB->delete_records('user_preferences', array('userid'=>$otheruserid));
1159         set_cache_flag('userpreferenceschanged', $otheruserid, null);
1161         // test $USER default
1162         set_user_preference('_test_user_preferences_pref', 'ok');
1163         $this->assertIdentical('ok', $USER->preference['_test_user_preferences_pref']);
1164         unset_user_preference('_test_user_preferences_pref');
1165         $this->assertTrue(!isset($USER->preference['_test_user_preferences_pref']));
1167         //test invalid params
1168         try {
1169             set_user_preference('_test_user_preferences_pref', array());
1170             $this->assertFail('Exception expected - array not valid preference value');
1171         } catch (Exception $ex) {
1172             $this->assertTrue(true);
1173         }
1174         try {
1175             set_user_preference('_test_user_preferences_pref', new stdClass);
1176             $this->assertFail('Exception expected - class not valid preference value');
1177         } catch (Exception $ex) {
1178             $this->assertTrue(true);
1179         }
1180         try {
1181             set_user_preference('_test_user_preferences_pref', 1, array('xx'=>1));
1182             $this->assertFail('Exception expected - user instance expected');
1183         } catch (Exception $ex) {
1184             $this->assertTrue(true);
1185         }
1186         try {
1187             set_user_preference('_test_user_preferences_pref', 1, 'abc');
1188             $this->assertFail('Exception expected - user instance expected');
1189         } catch (Exception $ex) {
1190             $this->assertTrue(true);
1191         }
1192         try {
1193             set_user_preference('', 1);
1194             $this->assertFail('Exception expected - invalid name accepted');
1195         } catch (Exception $ex) {
1196             $this->assertTrue(true);
1197         }
1198         try {
1199             set_user_preference('1', 1);
1200             $this->assertFail('Exception expected - invalid name accepted');
1201         } catch (Exception $ex) {
1202             $this->assertTrue(true);
1203         }
1204     }
1206     public function test_userdate() {
1207         global $USER, $CFG;
1209         $testvalues = array(
1210             array(
1211                 'time' => '1309514400',
1212                 'usertimezone' => 'America/Moncton',
1213                 'timezone' => '0.0', //no dst offset
1214                 'expectedoutput' => 'Friday, 1 July 2011, 10:00 AM'
1215             ),
1216             array(
1217                 'time' => '1309514400',
1218                 'usertimezone' => 'America/Moncton',
1219                 'timezone' => '99', //dst offset and timezone offset.
1220                 'expectedoutput' => 'Friday, 1 July 2011, 07:00 AM'
1221             ),
1222             array(
1223                 'time' => '1309514400',
1224                 'usertimezone' => 'America/Moncton',
1225                 'timezone' => 'America/Moncton', //dst offset and timezone offset.
1226                 'expectedoutput' => 'Friday, 1 July 2011, 07:00 AM'
1227             ),
1228             array(
1229                 'time' => '1293876000 ',
1230                 'usertimezone' => 'America/Moncton',
1231                 'timezone' => '0.0', //no dst offset
1232                 'expectedoutput' => 'Saturday, 1 January 2011, 10:00 AM'
1233             ),
1234             array(
1235                 'time' => '1293876000 ',
1236                 'usertimezone' => 'America/Moncton',
1237                 'timezone' => '99', //no dst offset in jan, so just timezone offset.
1238                 'expectedoutput' => 'Saturday, 1 January 2011, 06:00 AM'
1239             ),
1240             array(
1241                 'time' => '1293876000 ',
1242                 'usertimezone' => 'America/Moncton',
1243                 'timezone' => 'America/Moncton', //no dst offset in jan
1244                 'expectedoutput' => 'Saturday, 1 January 2011, 06:00 AM'
1245             ),
1246             array(
1247                 'time' => '1293876000 ',
1248                 'usertimezone' => '2',
1249                 'timezone' => '99', //take user timezone
1250                 'expectedoutput' => 'Saturday, 1 January 2011, 12:00 PM'
1251             ),
1252             array(
1253                 'time' => '1293876000 ',
1254                 'usertimezone' => '-2',
1255                 'timezone' => '99', //take user timezone
1256                 'expectedoutput' => 'Saturday, 1 January 2011, 08:00 AM'
1257             ),
1258             array(
1259                 'time' => '1293876000 ',
1260                 'usertimezone' => '-10',
1261                 'timezone' => '2', //take this timezone
1262                 'expectedoutput' => 'Saturday, 1 January 2011, 12:00 PM'
1263             ),
1264             array(
1265                 'time' => '1293876000 ',
1266                 'usertimezone' => '-10',
1267                 'timezone' => '-2', //take this timezone
1268                 'expectedoutput' => 'Saturday, 1 January 2011, 08:00 AM'
1269             ),
1270             array(
1271                 'time' => '1293876000 ',
1272                 'usertimezone' => '-10',
1273                 'timezone' => 'random/time', //this should show server time
1274                 'expectedoutput' => 'Saturday, 1 January 2011, 06:00 PM'
1275             ),
1276             array(
1277                 'time' => '1293876000 ',
1278                 'usertimezone' => '14', //server time zone
1279                 'timezone' => '99', //this should show user time
1280                 'expectedoutput' => 'Saturday, 1 January 2011, 06:00 PM'
1281             ),
1282         );
1284         //Check if forcetimezone is set then save it and set it to use user timezone
1285         $cfgforcetimezone = null;
1286         if (isset($CFG->forcetimezone)) {
1287             $cfgforcetimezone = $CFG->forcetimezone;
1288             $CFG->forcetimezone = 99; //get user default timezone.
1289         }
1290         //store user default timezone to restore later
1291         $userstimezone = $USER->timezone;
1293         // The string version of date comes from server locale setting and does
1294         // not respect user language, so it is necessary to reset that.
1295         $oldlocale = setlocale(LC_TIME, '0');
1296         setlocale(LC_TIME, 'en_AU.UTF-8');
1298         //set default timezone to Australia/Perth, else time calulated
1299         //will not match expected values. Before that save system defaults.
1300         $systemdefaulttimezone = date_default_timezone_get();
1301         date_default_timezone_set('Australia/Perth');
1303         //get instance of textlib for strtolower
1304         $textlib = textlib_get_instance();
1305         foreach ($testvalues as $vals) {
1306             $USER->timezone = $vals['usertimezone'];
1307             $actualoutput = userdate($vals['time'], '%A, %d %B %Y, %I:%M %p', $vals['timezone']);
1309             //On different systems case of AM PM changes so compare case insenitive
1310             $vals['expectedoutput'] = $textlib->strtolower($vals['expectedoutput']);
1311             $actualoutput = $textlib->strtolower($actualoutput);
1313             $this->assertEqual($vals['expectedoutput'], $actualoutput,
1314                 "Expected: {$vals['expectedoutput']} => Actual: {$actualoutput},
1315                 Please check if timezones are updated (Site adminstration -> location -> update timezone)");
1316         }
1318         //restore user timezone back to what it was
1319         $USER->timezone = $userstimezone;
1321         //restore forcetimezone
1322         if (!is_null($cfgforcetimezone)) {
1323             $CFG->forcetimezone = $cfgforcetimezone;
1324         }
1326         //restore system default values.
1327         date_default_timezone_set($systemdefaulttimezone);
1328         setlocale(LC_TIME, $oldlocale);
1329     }
1331     public function test_make_timestamp() {
1332         global $USER, $CFG;
1334         $testvalues = array(
1335             array(
1336                 'usertimezone' => 'America/Moncton',
1337                 'year' => '2011',
1338                 'month' => '7',
1339                 'day' => '1',
1340                 'hour' => '10',
1341                 'minutes' => '00',
1342                 'seconds' => '00',
1343                 'timezone' => '0.0', //no dst offset
1344                 'applydst' => false,
1345                 'expectedoutput' => '1309528800'
1346             ),
1347             array(
1348                 'usertimezone' => 'America/Moncton',
1349                 'year' => '2011',
1350                 'month' => '7',
1351                 'day' => '1',
1352                 'hour' => '10',
1353                 'minutes' => '00',
1354                 'seconds' => '00',
1355                 'timezone' => '99', //user default timezone
1356                 'applydst' => false, //don't apply dst
1357                 'expectedoutput' => '1309528800'
1358             ),
1359             array(
1360                 'usertimezone' => 'America/Moncton',
1361                 'year' => '2011',
1362                 'month' => '7',
1363                 'day' => '1',
1364                 'hour' => '10',
1365                 'minutes' => '00',
1366                 'seconds' => '00',
1367                 'timezone' => '99', //user default timezone
1368                 'applydst' => true, //apply dst
1369                 'expectedoutput' => '1309525200'
1370             ),
1371             array(
1372                 'usertimezone' => 'America/Moncton',
1373                 'year' => '2011',
1374                 'month' => '7',
1375                 'day' => '1',
1376                 'hour' => '10',
1377                 'minutes' => '00',
1378                 'seconds' => '00',
1379                 'timezone' => 'America/Moncton', //string timezone
1380                 'applydst' => true, //apply dst
1381                 'expectedoutput' => '1309525200'
1382             ),
1383             array(
1384                 'usertimezone' => '2',//no dst applyed
1385                 'year' => '2011',
1386                 'month' => '7',
1387                 'day' => '1',
1388                 'hour' => '10',
1389                 'minutes' => '00',
1390                 'seconds' => '00',
1391                 'timezone' => '99', //take user timezone
1392                 'applydst' => true, //apply dst
1393                 'expectedoutput' => '1309507200'
1394             ),
1395             array(
1396                 'usertimezone' => '-2',//no dst applyed
1397                 'year' => '2011',
1398                 'month' => '7',
1399                 'day' => '1',
1400                 'hour' => '10',
1401                 'minutes' => '00',
1402                 'seconds' => '00',
1403                 'timezone' => '99', //take usertimezone
1404                 'applydst' => true, //apply dst
1405                 'expectedoutput' => '1309521600'
1406             ),
1407             array(
1408                 'usertimezone' => '-10',//no dst applyed
1409                 'year' => '2011',
1410                 'month' => '7',
1411                 'day' => '1',
1412                 'hour' => '10',
1413                 'minutes' => '00',
1414                 'seconds' => '00',
1415                 'timezone' => '2', //take this timezone
1416                 'applydst' => true, //apply dst
1417                 'expectedoutput' => '1309507200'
1418             ),
1419             array(
1420                 'usertimezone' => '-10',//no dst applyed
1421                 'year' => '2011',
1422                 'month' => '7',
1423                 'day' => '1',
1424                 'hour' => '10',
1425                 'minutes' => '00',
1426                 'seconds' => '00',
1427                 'timezone' => '-2', //take this timezone
1428                 'applydst' => true, //apply dst,
1429                 'expectedoutput' => '1309521600'
1430             ),
1431             array(
1432                 'usertimezone' => '-10',//no dst applyed
1433                 'year' => '2011',
1434                 'month' => '7',
1435                 'day' => '1',
1436                 'hour' => '10',
1437                 'minutes' => '00',
1438                 'seconds' => '00',
1439                 'timezone' => 'random/time', //This should show server time
1440                 'applydst' => true, //apply dst,
1441                 'expectedoutput' => '1309485600'
1442             ),
1443             array(
1444                 'usertimezone' => '14',//server time
1445                 'year' => '2011',
1446                 'month' => '7',
1447                 'day' => '1',
1448                 'hour' => '10',
1449                 'minutes' => '00',
1450                 'seconds' => '00',
1451                 'timezone' => '99', //get user time
1452                 'applydst' => true, //apply dst,
1453                 'expectedoutput' => '1309485600'
1454             )
1455         );
1457         //Check if forcetimezone is set then save it and set it to use user timezone
1458         $cfgforcetimezone = null;
1459         if (isset($CFG->forcetimezone)) {
1460             $cfgforcetimezone = $CFG->forcetimezone;
1461             $CFG->forcetimezone = 99; //get user default timezone.
1462         }
1464         //store user default timezone to restore later
1465         $userstimezone = $USER->timezone;
1467         // The string version of date comes from server locale setting and does
1468         // not respect user language, so it is necessary to reset that.
1469         $oldlocale = setlocale(LC_TIME, '0');
1470         setlocale(LC_TIME, 'en_AU.UTF-8');
1472         //set default timezone to Australia/Perth, else time calulated
1473         //will not match expected values. Before that save system defaults.
1474         $systemdefaulttimezone = date_default_timezone_get();
1475         date_default_timezone_set('Australia/Perth');
1477         //get instance of textlib for strtolower
1478         $textlib = textlib_get_instance();
1479         //Test make_timestamp with all testvals and assert if anything wrong.
1480         foreach ($testvalues as $vals) {
1481             $USER->timezone = $vals['usertimezone'];
1482             $actualoutput = make_timestamp(
1483                     $vals['year'],
1484                     $vals['month'],
1485                     $vals['day'],
1486                     $vals['hour'],
1487                     $vals['minutes'],
1488                     $vals['seconds'],
1489                     $vals['timezone'],
1490                     $vals['applydst']
1491                     );
1493             //On different systems case of AM PM changes so compare case insenitive
1494             $vals['expectedoutput'] = $textlib->strtolower($vals['expectedoutput']);
1495             $actualoutput = $textlib->strtolower($actualoutput);
1497             $this->assertEqual($vals['expectedoutput'], $actualoutput,
1498                 "Expected: {$vals['expectedoutput']} => Actual: {$actualoutput},
1499                 Please check if timezones are updated (Site adminstration -> location -> update timezone)");
1500         }
1502         //restore user timezone back to what it was
1503         $USER->timezone = $userstimezone;
1505         //restore forcetimezone
1506         if (!is_null($cfgforcetimezone)) {
1507             $CFG->forcetimezone = $cfgforcetimezone;
1508         }
1510         //restore system default values.
1511         date_default_timezone_set($systemdefaulttimezone);
1512         setlocale(LC_TIME, $oldlocale);
1513     }