3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.org //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
22 // http://www.gnu.org/copyleft/gpl.html //
24 ///////////////////////////////////////////////////////////////////////////
27 * Unit tests for (some of) ../moodlelib.php.
29 * Note, tests for get_string are in the separate file testgetstring.php.
31 * @copyright © 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
38 if (!defined('MOODLE_INTERNAL')) {
39 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
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(
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))'),
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'),
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')
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'),
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'),
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'),
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)')
88 function test_cleanremoteaddr() {
90 $this->assertEqual(cleanremoteaddr('1023.121.234.1'), null);
91 $this->assertEqual(cleanremoteaddr('123.121.234.01 '), '123.121.234.1');
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');
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'));
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'));
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'));
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 '));
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:'));
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', ''));
197 * Modifies $_SERVER['HTTP_USER_AGENT'] manually to check if check_browser_version
200 function test_check_browser_version()
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'));
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());
299 function test_get_device_type() {
300 // IE8 (common pattern ~1.5% of IE7/8 users have embedded IE6 agent))
301 $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BT Openworld BB; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; Hotbar 10.2.197.0; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727)';
302 $this->assertEqual('default', get_device_type());
304 $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 6.0; AOL 9.0; Windows NT 5.1; SV1; FunWebProducts; .NET CLR 1.0.3705; Media Center PC 2.8)';
305 $this->assertEqual('legacy', get_device_type());
308 function test_fix_utf8() {
309 // make sure valid data including other types is not changed
310 $this->assertidentical(null, fix_utf8(null));
311 $this->assertidentical(1, fix_utf8(1));
312 $this->assertidentical(1.1, fix_utf8(1.1));
313 $this->assertidentical(true, fix_utf8(true));
314 $this->assertidentical('abc', fix_utf8('abc'));
315 $this->assertidentical('', fix_utf8(''));
316 $array = array('do', 're', 'mi');
317 $this->assertidentical($array, fix_utf8($array));
318 $object = new stdClass();
321 $this->assertidentical($object, fix_utf8($object));
324 $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"));
326 // invalid utf8 string
327 $this->assertidentical('aš', fix_utf8('a'.chr(130).'š'), 'This fails with buggy iconv() when mbstring extenstion is not available as fallback.');
330 function test_optional_param() {
333 $_POST['username'] = 'post_user';
334 $_GET['username'] = 'get_user';
335 $this->assertIdentical(optional_param('username', 'default_user', PARAM_RAW
), $_POST['username']);
337 unset($_POST['username']);
338 $this->assertIdentical(optional_param('username', 'default_user', PARAM_RAW
), $_GET['username']);
340 unset($_GET['username']);
341 $this->assertIdentical(optional_param('username', 'default_user', PARAM_RAW
), 'default_user');
343 // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2
344 $_POST['username'] = 'post_user';
346 optional_param('username', 'default_user', null);
347 $this->fail('coding_exception expected');
348 } catch (coding_exception
$ex) {
349 $this->assertTrue(true);
352 @optional_param
('username', 'default_user');
353 $this->fail('coding_exception expected');
354 } catch (coding_exception
$ex) {
355 $this->assertTrue(true);
358 @optional_param
('username');
359 $this->fail('coding_exception expected');
360 } catch (coding_exception
$ex) {
361 $this->assertTrue(true);
364 optional_param('', 'default_user', PARAM_RAW
);
365 $this->fail('coding_exception expected');
366 } catch (coding_exception
$ex) {
367 $this->assertTrue(true);
370 // make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3
371 $debugging = isset($CFG->debug
) ?
$CFG->debug
: null;
372 $debugdisplay = isset($CFG->debugdisplay
) ?
$CFG->debugdisplay
: null;
374 $CFG->debugdisplay
= true;
377 $this->assertIdentical(optional_param('username', 'default_user', PARAM_RAW
), $_POST['username']);
379 $this->assertTrue($d !== '');
381 if ($debugging !== null) {
382 $CFG->debug
= $debugging;
386 if ($debugdisplay !== null) {
387 $CFG->debugdisplay
= $debugdisplay;
389 unset($CFG->debugdisplay
);
393 function test_optional_param_array() {
396 $_POST['username'] = array('a'=>'post_user');
397 $_GET['username'] = array('a'=>'get_user');
398 $this->assertIdentical(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW
), $_POST['username']);
400 unset($_POST['username']);
401 $this->assertIdentical(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW
), $_GET['username']);
403 unset($_GET['username']);
404 $this->assertIdentical(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW
), array('a'=>'default_user'));
406 // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2
407 $_POST['username'] = array('a'=>'post_user');
409 optional_param_array('username', array('a'=>'default_user'), null);
410 $this->fail('coding_exception expected');
411 } catch (coding_exception
$ex) {
412 $this->assertTrue(true);
415 @optional_param_array
('username', array('a'=>'default_user'));
416 $this->fail('coding_exception expected');
417 } catch (coding_exception
$ex) {
418 $this->assertTrue(true);
421 @optional_param_array
('username');
422 $this->fail('coding_exception expected');
423 } catch (coding_exception
$ex) {
424 $this->assertTrue(true);
427 optional_param_array('', array('a'=>'default_user'), PARAM_RAW
);
428 $this->fail('coding_exception expected');
429 } catch (coding_exception
$ex) {
430 $this->assertTrue(true);
433 // do not allow nested arrays
435 $_POST['username'] = array('a'=>array('b'=>'post_user'));
436 optional_param_array('username', array('a'=>'default_user'), PARAM_RAW
);
437 $this->fail('coding_exception expected');
438 } catch (coding_exception
$ex) {
439 $this->assertTrue(true);
442 // do not allow non-arrays
443 $debugging = isset($CFG->debug
) ?
$CFG->debug
: null;
444 $debugdisplay = isset($CFG->debugdisplay
) ?
$CFG->debugdisplay
: null;
446 $CFG->debugdisplay
= true;
449 $_POST['username'] = 'post_user';
450 $this->assertIdentical(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW
), array('a'=>'default_user'));
452 $this->assertTrue($d !== '');
454 // make sure array keys are sanitised
456 $_POST['username'] = array('abc123_;-/*-+ '=>'arrggh', 'a1_-'=>'post_user');
457 $this->assertIdentical(optional_param_array('username', array(), PARAM_RAW
), array('a1_-'=>'post_user'));
459 $this->assertTrue($d !== '');
461 if ($debugging !== null) {
462 $CFG->debug
= $debugging;
466 if ($debugdisplay !== null) {
467 $CFG->debugdisplay
= $debugdisplay;
469 unset($CFG->debugdisplay
);
473 function test_required_param() {
476 $_POST['username'] = 'post_user';
477 $_GET['username'] = 'get_user';
478 $this->assertIdentical(required_param('username', PARAM_RAW
), 'post_user');
480 unset($_POST['username']);
481 $this->assertIdentical(required_param('username', PARAM_RAW
), 'get_user');
483 unset($_GET['username']);
485 $this->assertIdentical(required_param('username', PARAM_RAW
), 'default_user');
486 $this->fail('moodle_exception expected');
487 } catch (moodle_exception
$ex) {
488 $this->assertTrue(true);
491 // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2
492 $_POST['username'] = 'post_user';
494 @required_param
('username');
495 $this->fail('coding_exception expected');
496 } catch (coding_exception
$ex) {
497 $this->assertTrue(true);
500 required_param('username', '');
501 $this->fail('coding_exception expected');
502 } catch (coding_exception
$ex) {
503 $this->assertTrue(true);
506 required_param('', PARAM_RAW
);
507 $this->fail('coding_exception expected');
508 } catch (coding_exception
$ex) {
509 $this->assertTrue(true);
512 // make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3
513 $debugging = isset($CFG->debug
) ?
$CFG->debug
: null;
514 $debugdisplay = isset($CFG->debugdisplay
) ?
$CFG->debugdisplay
: null;
516 $CFG->debugdisplay
= true;
519 $this->assertIdentical(required_param('username', PARAM_RAW
), $_POST['username']);
521 $this->assertTrue($d !== '');
523 if ($debugging !== null) {
524 $CFG->debug
= $debugging;
528 if ($debugdisplay !== null) {
529 $CFG->debugdisplay
= $debugdisplay;
531 unset($CFG->debugdisplay
);
535 function test_required_param_array() {
538 $_POST['username'] = array('a'=>'post_user');
539 $_GET['username'] = array('a'=>'get_user');
540 $this->assertIdentical(required_param_array('username', PARAM_RAW
), $_POST['username']);
542 unset($_POST['username']);
543 $this->assertIdentical(required_param_array('username', PARAM_RAW
), $_GET['username']);
545 // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2
546 $_POST['username'] = array('a'=>'post_user');
548 required_param_array('username', null);
549 $this->fail('coding_exception expected');
550 } catch (coding_exception
$ex) {
551 $this->assertTrue(true);
554 @required_param_array
('username');
555 $this->fail('coding_exception expected');
556 } catch (coding_exception
$ex) {
557 $this->assertTrue(true);
560 required_param_array('', PARAM_RAW
);
561 $this->fail('coding_exception expected');
562 } catch (coding_exception
$ex) {
563 $this->assertTrue(true);
566 // do not allow nested arrays
568 $_POST['username'] = array('a'=>array('b'=>'post_user'));
569 required_param_array('username', PARAM_RAW
);
570 $this->fail('coding_exception expected');
571 } catch (coding_exception
$ex) {
572 $this->assertTrue(true);
575 // do not allow non-arrays
577 $_POST['username'] = 'post_user';
578 required_param_array('username', PARAM_RAW
);
579 $this->fail('moodle_exception expected');
580 } catch (moodle_exception
$ex) {
581 $this->assertTrue(true);
584 // do not allow non-arrays
585 $debugging = isset($CFG->debug
) ?
$CFG->debug
: null;
586 $debugdisplay = isset($CFG->debugdisplay
) ?
$CFG->debugdisplay
: null;
588 $CFG->debugdisplay
= true;
590 // make sure array keys are sanitised
592 $_POST['username'] = array('abc123_;-/*-+ '=>'arrggh', 'a1_-'=>'post_user');
593 $this->assertIdentical(required_param_array('username', PARAM_RAW
), array('a1_-'=>'post_user'));
595 $this->assertTrue($d !== '');
597 if ($debugging !== null) {
598 $CFG->debug
= $debugging;
602 if ($debugdisplay !== null) {
603 $CFG->debugdisplay
= $debugdisplay;
605 unset($CFG->debugdisplay
);
609 function test_clean_param() {
610 // forbid objects and arrays
612 clean_param(array('x', 'y'), PARAM_RAW
);
613 $this->fail('coding_exception expected');
614 } catch (coding_exception
$ex) {
615 $this->assertTrue(true);
618 $param = new stdClass();
620 clean_param($param, PARAM_RAW
);
621 $this->fail('coding_exception expected');
622 } catch (coding_exception
$ex) {
623 $this->assertTrue(true);
626 // require correct type
628 clean_param('x', 'xxxxxx');
629 $this->fail('moodle_exception expected');
630 } catch (moodle_exception
$ex) {
631 $this->assertTrue(true);
635 $this->fail('moodle_exception expected');
636 } catch (moodle_exception
$ex) {
637 $this->assertTrue(true);
642 function test_clean_param_array() {
643 $this->assertIdentical(clean_param_array(null, PARAM_RAW
), array());
644 $this->assertIdentical(clean_param_array(array('a', 'b'), PARAM_RAW
), array('a', 'b'));
645 $this->assertIdentical(clean_param_array(array('a', array('b')), PARAM_RAW
, true), array('a', array('b')));
647 // require correct type
649 clean_param_array(array('x'), 'xxxxxx');
650 $this->fail('moodle_exception expected');
651 } catch (moodle_exception
$ex) {
652 $this->assertTrue(true);
655 @clean_param_array
(array('x'));
656 $this->fail('moodle_exception expected');
657 } catch (moodle_exception
$ex) {
658 $this->assertTrue(true);
662 clean_param_array(array('x', array('y')), PARAM_RAW
);
663 $this->fail('coding_exception expected');
664 } catch (coding_exception
$ex) {
665 $this->assertTrue(true);
671 function test_clean_param_raw() {
672 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_RAW
),
673 '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)');
676 function test_clean_param_trim() {
677 $this->assertEqual(clean_param(" Frog toad \r\n ", PARAM_RAW_TRIMMED
), 'Frog toad');
680 function test_clean_param_clean() {
681 // PARAM_CLEAN is an ugly hack, do not use in new code (skodak)
682 // instead use more specific type, or submit sothing that can be verified properly
683 $this->assertEqual(clean_param('xx<script>', PARAM_CLEAN
), 'xx');
686 function test_clean_param_alpha() {
687 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHA
),
691 function test_clean_param_alphanum() {
692 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHANUM
),
693 '978942897DSFMOSDJ');
696 function test_clean_param_alphaext() {
697 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHAEXT
),
701 function test_clean_param_sequence() {
702 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_SEQUENCE
),
706 function test_clean_param_component() {
707 // please note the cleaning of component names is very strict, no guessing here
708 $this->assertIdentical(clean_param('mod_forum', PARAM_COMPONENT
), 'mod_forum');
709 $this->assertIdentical(clean_param('block_online_users', PARAM_COMPONENT
), 'block_online_users');
710 $this->assertIdentical(clean_param('block_blond_online_users', PARAM_COMPONENT
), 'block_blond_online_users');
711 $this->assertIdentical(clean_param('mod_something2', PARAM_COMPONENT
), 'mod_something2');
712 $this->assertIdentical(clean_param('forum', PARAM_COMPONENT
), 'forum');
713 $this->assertIdentical(clean_param('user', PARAM_COMPONENT
), 'user');
714 $this->assertIdentical(clean_param('rating', PARAM_COMPONENT
), 'rating');
715 $this->assertIdentical(clean_param('mod_2something', PARAM_COMPONENT
), '');
716 $this->assertIdentical(clean_param('2mod_something', PARAM_COMPONENT
), '');
717 $this->assertIdentical(clean_param('mod_something_xx', PARAM_COMPONENT
), '');
718 $this->assertIdentical(clean_param('auth_something__xx', PARAM_COMPONENT
), '');
719 $this->assertIdentical(clean_param('mod_Something', PARAM_COMPONENT
), '');
720 $this->assertIdentical(clean_param('mod_somethíng', PARAM_COMPONENT
), '');
721 $this->assertIdentical(clean_param('auth_xx-yy', PARAM_COMPONENT
), '');
722 $this->assertIdentical(clean_param('_auth_xx', PARAM_COMPONENT
), '');
723 $this->assertIdentical(clean_param('a2uth_xx', PARAM_COMPONENT
), '');
724 $this->assertIdentical(clean_param('auth_xx_', PARAM_COMPONENT
), '');
725 $this->assertIdentical(clean_param('auth_xx.old', PARAM_COMPONENT
), '');
726 $this->assertIdentical(clean_param('_user', PARAM_COMPONENT
), '');
727 $this->assertIdentical(clean_param('2rating', PARAM_COMPONENT
), '');
728 $this->assertIdentical(clean_param('user_', PARAM_COMPONENT
), '');
731 function test_clean_param_plugin() {
732 // please note the cleaning of plugin names is very strict, no guessing here
733 $this->assertIdentical(clean_param('forum', PARAM_PLUGIN
), 'forum');
734 $this->assertIdentical(clean_param('forum2', PARAM_PLUGIN
), 'forum2');
735 $this->assertIdentical(clean_param('online_users', PARAM_PLUGIN
), 'online_users');
736 $this->assertIdentical(clean_param('blond_online_users', PARAM_PLUGIN
), 'blond_online_users');
737 $this->assertIdentical(clean_param('online__users', PARAM_PLUGIN
), '');
738 $this->assertIdentical(clean_param('forum ', PARAM_PLUGIN
), '');
739 $this->assertIdentical(clean_param('forum.old', PARAM_PLUGIN
), '');
740 $this->assertIdentical(clean_param('xx-yy', PARAM_PLUGIN
), '');
741 $this->assertIdentical(clean_param('2xx', PARAM_PLUGIN
), '');
742 $this->assertIdentical(clean_param('Xx', PARAM_PLUGIN
), '');
743 $this->assertIdentical(clean_param('_xx', PARAM_PLUGIN
), '');
744 $this->assertIdentical(clean_param('xx_', PARAM_PLUGIN
), '');
747 function test_clean_param_area() {
748 // please note the cleaning of area names is very strict, no guessing here
749 $this->assertIdentical(clean_param('something', PARAM_AREA
), 'something');
750 $this->assertIdentical(clean_param('something2', PARAM_AREA
), 'something2');
751 $this->assertIdentical(clean_param('some_thing', PARAM_AREA
), 'some_thing');
752 $this->assertIdentical(clean_param('some_thing_xx', PARAM_AREA
), 'some_thing_xx');
753 $this->assertIdentical(clean_param('_something', PARAM_AREA
), '');
754 $this->assertIdentical(clean_param('something_', PARAM_AREA
), '');
755 $this->assertIdentical(clean_param('2something', PARAM_AREA
), '');
756 $this->assertIdentical(clean_param('Something', PARAM_AREA
), '');
757 $this->assertIdentical(clean_param('some-thing', PARAM_AREA
), '');
758 $this->assertIdentical(clean_param('somethííng', PARAM_AREA
), '');
759 $this->assertIdentical(clean_param('something.x', PARAM_AREA
), '');
762 function test_clean_param_text() {
763 $this->assertEqual(PARAM_TEXT
, PARAM_MULTILANG
);
765 $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>');
766 $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>');
767 $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>');
769 $this->assertEqual(clean_param('<span lang="en" class="multilang">aa</span>', PARAM_TEXT
), '<span lang="en" class="multilang">aa</span>');
770 $this->assertEqual(clean_param('<span lang="en" class="nothing" class="multilang">aa</span>', PARAM_TEXT
), 'aa');
771 $this->assertEqual(clean_param('<lang lang="en" class="multilang">aa</lang>', PARAM_TEXT
), 'aa');
772 $this->assertEqual(clean_param('<lang lang="en!!">aa</lang>', PARAM_TEXT
), 'aa');
773 $this->assertEqual(clean_param('<span lang="en==" class="multilang">aa</span>', PARAM_TEXT
), 'aa');
774 $this->assertEqual(clean_param('a<em>b</em>c', PARAM_TEXT
), 'abc');
775 $this->assertEqual(clean_param('a><xx >c>', PARAM_TEXT
), 'a>c>'); // standard strip_tags() behaviour
776 $this->assertEqual(clean_param('a<b', PARAM_TEXT
), 'a');
777 $this->assertEqual(clean_param('a>b', PARAM_TEXT
), 'a>b');
778 $this->assertEqual(clean_param('<lang lang="en">a>a</lang>', PARAM_TEXT
), '<lang lang="en">a>a</lang>'); // standard strip_tags() behaviour
779 $this->assertEqual(clean_param('<lang lang="en">a<a</lang>', PARAM_TEXT
), 'a');
780 $this->assertEqual(clean_param('<lang lang="en">a<br>a</lang>', PARAM_TEXT
), '<lang lang="en">aa</lang>');
783 function test_clean_param_url() {
784 // Test PARAM_URL and PARAM_LOCALURL a bit
785 $this->assertEqual(clean_param('http://google.com/', PARAM_URL
), 'http://google.com/');
786 $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/');
787 $this->assertEqual(clean_param('http://localhost/', PARAM_URL
), 'http://localhost/');
788 $this->assertEqual(clean_param('http://0.255.1.1/numericip.php', PARAM_URL
), 'http://0.255.1.1/numericip.php');
789 $this->assertEqual(clean_param('/just/a/path', PARAM_URL
), '/just/a/path');
790 $this->assertEqual(clean_param('funny:thing', PARAM_URL
), '');
793 function test_clean_param_localurl() {
795 $this->assertEqual(clean_param('http://google.com/', PARAM_LOCALURL
), '');
796 $this->assertEqual(clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_LOCALURL
), '');
797 $this->assertEqual(clean_param($CFG->wwwroot
, PARAM_LOCALURL
), $CFG->wwwroot
);
798 $this->assertEqual(clean_param('/just/a/path', PARAM_LOCALURL
), '/just/a/path');
799 $this->assertEqual(clean_param('funny:thing', PARAM_LOCALURL
), '');
800 $this->assertEqual(clean_param('course/view.php?id=3', PARAM_LOCALURL
), 'course/view.php?id=3');
803 function test_clean_param_file() {
804 $this->assertEqual(clean_param('correctfile.txt', PARAM_FILE
), 'correctfile.txt');
805 $this->assertEqual(clean_param('b\'a<d`\\/fi:l>e.t"x|t', PARAM_FILE
), 'badfile.txt');
806 $this->assertEqual(clean_param('../parentdirfile.txt', PARAM_FILE
), 'parentdirfile.txt');
807 //The following behaviours have been maintained although they seem a little odd
808 $this->assertEqual(clean_param('funny:thing', PARAM_FILE
), 'funnything');
809 $this->assertEqual(clean_param('./currentdirfile.txt', PARAM_FILE
), '.currentdirfile.txt');
810 $this->assertEqual(clean_param('c:\temp\windowsfile.txt', PARAM_FILE
), 'ctempwindowsfile.txt');
811 $this->assertEqual(clean_param('/home/user/linuxfile.txt', PARAM_FILE
), 'homeuserlinuxfile.txt');
812 $this->assertEqual(clean_param('~/myfile.txt', PARAM_FILE
), '~myfile.txt');
815 function test_clean_param_username() {
817 $currentstatus = $CFG->extendedusernamechars
;
819 // Run tests with extended character == FALSE;
820 $CFG->extendedusernamechars
= FALSE;
821 $this->assertEqual(clean_param('johndoe123', PARAM_USERNAME
), 'johndoe123' );
822 $this->assertEqual(clean_param('john.doe', PARAM_USERNAME
), 'john.doe');
823 $this->assertEqual(clean_param('john-doe', PARAM_USERNAME
), 'john-doe');
824 $this->assertEqual(clean_param('john- doe', PARAM_USERNAME
), 'john-doe');
825 $this->assertEqual(clean_param('john_doe', PARAM_USERNAME
), 'john_doe');
826 $this->assertEqual(clean_param('john@doe', PARAM_USERNAME
), 'john@doe');
827 $this->assertEqual(clean_param('john~doe', PARAM_USERNAME
), 'johndoe');
828 $this->assertEqual(clean_param('john´doe', PARAM_USERNAME
), 'johndoe');
829 $this->assertEqual(clean_param('john#$%&() ', PARAM_USERNAME
), 'john');
830 $this->assertEqual(clean_param('JOHNdóé ', PARAM_USERNAME
), 'johnd');
831 $this->assertEqual(clean_param('john.,:;-_/|\ñÑ[]A_X-,D {} ~!@#$%^&*()_+ ?><[] ščřžžý ?ýá?ý??doe ', PARAM_USERNAME
), 'john.-_a_x-d@_doe');
834 // Test success condition, if extendedusernamechars == ENABLE;
835 $CFG->extendedusernamechars
= TRUE;
836 $this->assertEqual(clean_param('john_doe', PARAM_USERNAME
), 'john_doe');
837 $this->assertEqual(clean_param('john@doe', PARAM_USERNAME
), 'john@doe');
838 $this->assertEqual(clean_param('john# $%&()+_^', PARAM_USERNAME
), 'john#$%&()+_^');
839 $this->assertEqual(clean_param('john~doe', PARAM_USERNAME
), 'john~doe');
840 $this->assertEqual(clean_param('joHN´doe', PARAM_USERNAME
), 'john´doe');
841 $this->assertEqual(clean_param('johnDOE', PARAM_USERNAME
), 'johndoe');
842 $this->assertEqual(clean_param('johndóé ', PARAM_USERNAME
), 'johndóé');
844 $CFG->extendedusernamechars
= $currentstatus;
847 function test_clean_param_stringid() {
848 // Test string identifiers validation
850 $this->assertEqual(clean_param('validstring', PARAM_STRINGID
), 'validstring');
851 $this->assertEqual(clean_param('mod/foobar:valid_capability', PARAM_STRINGID
), 'mod/foobar:valid_capability');
852 $this->assertEqual(clean_param('CZ', PARAM_STRINGID
), 'CZ');
853 $this->assertEqual(clean_param('application/vnd.ms-powerpoint', PARAM_STRINGID
), 'application/vnd.ms-powerpoint');
854 $this->assertEqual(clean_param('grade2', PARAM_STRINGID
), 'grade2');
856 $this->assertEqual(clean_param('trailing ', PARAM_STRINGID
), '');
857 $this->assertEqual(clean_param('space bar', PARAM_STRINGID
), '');
858 $this->assertEqual(clean_param('0numeric', PARAM_STRINGID
), '');
859 $this->assertEqual(clean_param('*', PARAM_STRINGID
), '');
860 $this->assertEqual(clean_param(' ', PARAM_STRINGID
), '');
863 function test_clean_param_timezone() {
864 // Test timezone validation
865 $testvalues = array (
866 'America/Jamaica' => 'America/Jamaica',
867 'America/Argentina/Cordoba' => 'America/Argentina/Cordoba',
868 'America/Port-au-Prince' => 'America/Port-au-Prince',
869 'America/Argentina/Buenos_Aires' => 'America/Argentina/Buenos_Aires',
870 'PST8PDT' => 'PST8PDT',
872 'Wrong/.Value' => '',
873 'Wrong(Value)' => '',
883 foreach ($testvalues as $testvalue => $expectedvalue) {
884 $actualvalue = clean_param($testvalue, PARAM_TIMEZONE
);
885 $this->assertEqual($actualvalue, $expectedvalue);
889 function test_validate_param() {
891 $param = validate_param('11a', PARAM_INT
);
892 $this->fail('invalid_parameter_exception expected');
893 } catch (invalid_parameter_exception
$ex) {
894 $this->assertTrue(true);
897 $param = validate_param('11', PARAM_INT
);
898 $this->assertEqual($param, 11);
899 } catch (invalid_parameter_exception
$ex) {
900 $this->fail('invalid_parameter_exception not expected');
903 $param = validate_param(null, PARAM_INT
, false);
904 $this->fail('invalid_parameter_exception expected');
905 } catch (invalid_parameter_exception
$ex) {
906 $this->assertTrue(true);
909 $param = validate_param(null, PARAM_INT
, true);
910 $this->assertTrue($param===null);
911 } catch (invalid_parameter_exception
$ex) {
912 $this->fail('invalid_parameter_exception expected');
915 $param = validate_param(array(), PARAM_INT
);
916 $this->fail('invalid_parameter_exception expected');
917 } catch (invalid_parameter_exception
$ex) {
918 $this->assertTrue(true);
921 $param = validate_param(new stdClass
, PARAM_INT
);
922 $this->fail('invalid_parameter_exception expected');
923 } catch (invalid_parameter_exception
$ex) {
924 $this->assertTrue(true);
928 function test_shorten_text() {
929 $text = "short text already no tags";
930 $this->assertEqual($text, shorten_text($text));
932 $text = "<p>short <b>text</b> already</p><p>with tags</p>";
933 $this->assertEqual($text, shorten_text($text));
935 $text = "long text without any tags blah de blah blah blah what";
936 $this->assertEqual('long text without any tags ...', shorten_text($text));
938 $text = "<div class='frog'><p><blockquote>Long text with tags that will ".
939 "be chopped off but <b>should be added back again</b></blockquote></p></div>";
940 $this->assertEqual("<div class='frog'><p><blockquote>Long text with " .
941 "tags that ...</blockquote></p></div>", shorten_text($text));
943 $text = "some text which shouldn't break there";
944 $this->assertEqual("some text which shouldn't ...",
945 shorten_text($text, 31));
946 $this->assertEqual("some text which shouldn't ...",
947 shorten_text($text, 30));
949 // This case caused a bug up to 1.9.5
950 $text = "<h3>standard 'break-out' sub groups in TGs?</h3> <<There are several";
951 $this->assertEqual("<h3>standard 'break-out' sub groups in ...</h3>",
952 shorten_text($text, 43));
954 $text = "<h1>123456789</h1>";//a string with no convenient breaks
955 $this->assertEqual("<h1>12345...</h1>",
956 shorten_text($text, 8));
958 // ==== this must work with UTF-8 too! ======
961 $text = "Žluťoučký koníček přeskočil";
962 $this->assertEqual($text, shorten_text($text)); // 30 chars by default
963 $this->assertEqual("Žluťoučký koníče...", shorten_text($text, 19, true));
964 $this->assertEqual("Žluťoučký ...", shorten_text($text, 19, false));
965 // And try it with 2-less (that are, in bytes, the middle of a sequence)
966 $this->assertEqual("Žluťoučký koní...", shorten_text($text, 17, true));
967 $this->assertEqual("Žluťoučký ...", shorten_text($text, 17, false));
969 $text = "<p>Žluťoučký koníček <b>přeskočil</b> potůček</p>";
970 $this->assertEqual($text, shorten_text($text, 60));
971 $this->assertEqual("<p>Žluťoučký koníček ...</p>", shorten_text($text, 21));
972 $this->assertEqual("<p>Žluťoučký koníče...</p>", shorten_text($text, 19, true));
973 $this->assertEqual("<p>Žluťoučký ...</p>", shorten_text($text, 19, false));
974 // And try it with 2-less (that are, in bytes, the middle of a sequence)
975 $this->assertEqual("<p>Žluťoučký koní...</p>", shorten_text($text, 17, true));
976 $this->assertEqual("<p>Žluťoučký ...</p>", shorten_text($text, 17, false));
977 // And try over one tag (start/end), it does proper text len
978 $this->assertEqual("<p>Žluťoučký koníček <b>př...</b></p>", shorten_text($text, 23, true));
979 $this->assertEqual("<p>Žluťoučký koníček <b>přeskočil</b> pot...</p>", shorten_text($text, 34, true));
980 // And in the middle of one tag
981 $this->assertEqual("<p>Žluťoučký koníček <b>přeskočil...</b></p>", shorten_text($text, 30, true));
984 $text = '言語設定言語設定abcdefghijkl';
985 $this->assertEqual($text, shorten_text($text)); // 30 chars by default
986 $this->assertEqual("言語設定言語...", shorten_text($text, 9, true));
987 $this->assertEqual("言語設定言語...", shorten_text($text, 9, false));
988 $this->assertEqual("言語設定言語設定ab...", shorten_text($text, 13, true));
989 $this->assertEqual("言語設定言語設定...", shorten_text($text, 13, false));
992 $text = '简体中文简体中文abcdefghijkl';
993 $this->assertEqual($text, shorten_text($text)); // 30 chars by default
994 $this->assertEqual("简体中文简体...", shorten_text($text, 9, true));
995 $this->assertEqual("简体中文简体...", shorten_text($text, 9, false));
996 $this->assertEqual("简体中文简体中文ab...", shorten_text($text, 13, true));
997 $this->assertEqual("简体中文简体中文...", shorten_text($text, 13, false));
1001 function test_usergetdate() {
1004 //Check if forcetimezone is set then save it and set it to use user timezone
1005 $cfgforcetimezone = null;
1006 if (isset($CFG->forcetimezone
)) {
1007 $cfgforcetimezone = $CFG->forcetimezone
;
1008 $CFG->forcetimezone
= 99; //get user default timezone.
1011 $userstimezone = $USER->timezone
;
1012 $USER->timezone
= 2;//set the timezone to a known state
1014 // The string version of date comes from server locale setting and does
1015 // not respect user language, so it is necessary to reset that.
1016 $oldlocale = setlocale(LC_TIME
, '0');
1017 setlocale(LC_TIME
, 'en_AU.UTF-8');
1019 $ts = 1261540267; //the time this function was created
1021 $arr = usergetdate($ts,1);//specify the timezone as an argument
1022 $arr = array_values($arr);
1024 list($seconds,$minutes,$hours,$mday,$wday,$mon,$year,$yday,$weekday,$month) = $arr;
1025 $this->assertIdentical($seconds, 7);
1026 $this->assertIdentical($minutes, 51);
1027 $this->assertIdentical($hours, 4);
1028 $this->assertIdentical($mday, 23);
1029 $this->assertIdentical($wday, 3);
1030 $this->assertIdentical($mon, 12);
1031 $this->assertIdentical($year, 2009);
1032 $this->assertIdentical($yday, 356);
1033 $this->assertIdentical($weekday, 'Wednesday');
1034 $this->assertIdentical($month, 'December');
1036 $arr = usergetdate($ts);//gets the timezone from the $USER object
1037 $arr = array_values($arr);
1039 list($seconds,$minutes,$hours,$mday,$wday,$mon,$year,$yday,$weekday,$month) = $arr;
1040 $this->assertIdentical($seconds, 7);
1041 $this->assertIdentical($minutes, 51);
1042 $this->assertIdentical($hours, 5);
1043 $this->assertIdentical($mday, 23);
1044 $this->assertIdentical($wday, 3);
1045 $this->assertIdentical($mon, 12);
1046 $this->assertIdentical($year, 2009);
1047 $this->assertIdentical($yday, 356);
1048 $this->assertIdentical($weekday, 'Wednesday');
1049 $this->assertIdentical($month, 'December');
1051 //set the timezone back to what it was
1052 $USER->timezone
= $userstimezone;
1054 //restore forcetimezone if changed.
1055 if (!is_null($cfgforcetimezone)) {
1056 $CFG->forcetimezone
= $cfgforcetimezone;
1059 setlocale(LC_TIME
, $oldlocale);
1062 public function test_normalize_component() {
1065 $this->assertEqual(normalize_component('moodle'), array('core', null));
1066 $this->assertEqual(normalize_component('core'), array('core', null));
1068 // moodle core subsystems
1069 $this->assertEqual(normalize_component('admin'), array('core', 'admin'));
1070 $this->assertEqual(normalize_component('core_admin'), array('core', 'admin'));
1072 // activity modules and their subplugins
1073 $this->assertEqual(normalize_component('workshop'), array('mod', 'workshop'));
1074 $this->assertEqual(normalize_component('mod_workshop'), array('mod', 'workshop'));
1075 $this->assertEqual(normalize_component('workshopform_accumulative'), array('workshopform', 'accumulative'));
1076 $this->assertEqual(normalize_component('quiz'), array('mod', 'quiz'));
1077 $this->assertEqual(normalize_component('quiz_grading'), array('quiz', 'grading'));
1078 $this->assertEqual(normalize_component('data'), array('mod', 'data'));
1079 $this->assertEqual(normalize_component('datafield_checkbox'), array('datafield', 'checkbox'));
1081 // other plugin types
1082 $this->assertEqual(normalize_component('auth_mnet'), array('auth', 'mnet'));
1083 $this->assertEqual(normalize_component('enrol_self'), array('enrol', 'self'));
1084 $this->assertEqual(normalize_component('block_html'), array('block', 'html'));
1085 $this->assertEqual(normalize_component('block_mnet_hosts'), array('block', 'mnet_hosts'));
1086 $this->assertEqual(normalize_component('local_amos'), array('local', 'amos'));
1088 // unknown components are supposed to be activity modules
1089 $this->assertEqual(normalize_component('whothefuckwouldcomewithsuchastupidnameofcomponent'),
1090 array('mod', 'whothefuckwouldcomewithsuchastupidnameofcomponent'));
1091 $this->assertEqual(normalize_component('whothefuck_wouldcomewithsuchastupidnameofcomponent'),
1092 array('mod', 'whothefuck_wouldcomewithsuchastupidnameofcomponent'));
1093 $this->assertEqual(normalize_component('whothefuck_would_come_withsuchastupidnameofcomponent'),
1094 array('mod', 'whothefuck_would_come_withsuchastupidnameofcomponent'));
1097 protected function get_fake_preference_test_userid() {
1100 // we need some nonexistent user id
1101 $id = 2147483647 - 666;
1102 if ($DB->get_records('user', array('id'=>$id))) {
1109 public function test_mark_user_preferences_changed() {
1110 if (!$otheruserid = $this->get_fake_preference_test_userid()) {
1111 $this->fail('Can not find unused user id for the preferences test');
1115 set_cache_flag('userpreferenceschanged', $otheruserid, NULL);
1116 mark_user_preferences_changed($otheruserid);
1118 $this->assertEqual(get_cache_flag('userpreferenceschanged', $otheruserid, time()-10), 1);
1119 set_cache_flag('userpreferenceschanged', $otheruserid, NULL);
1122 public function test_check_user_preferences_loaded() {
1125 if (!$otheruserid = $this->get_fake_preference_test_userid()) {
1126 $this->fail('Can not find unused user id for the preferences test');
1130 $DB->delete_records('user_preferences', array('userid'=>$otheruserid));
1131 set_cache_flag('userpreferenceschanged', $otheruserid, NULL);
1133 $user = new stdClass();
1134 $user->id
= $otheruserid;
1137 check_user_preferences_loaded($user);
1138 $this->assertTrue(isset($user->preference
));
1139 $this->assertTrue(is_array($user->preference
));
1140 $this->assertTrue(isset($user->preference
['_lastloaded']));
1141 $this->assertEqual(count($user->preference
), 1);
1143 // add preference via direct call
1144 $DB->insert_record('user_preferences', array('name'=>'xxx', 'value'=>'yyy', 'userid'=>$user->id
));
1146 // no cache reload yet
1147 check_user_preferences_loaded($user);
1148 $this->assertEqual(count($user->preference
), 1);
1150 // forced reloading of cache
1151 unset($user->preference
);
1152 check_user_preferences_loaded($user);
1153 $this->assertEqual(count($user->preference
), 2);
1154 $this->assertEqual($user->preference
['xxx'], 'yyy');
1156 // add preference via direct call
1157 $DB->insert_record('user_preferences', array('name'=>'aaa', 'value'=>'bbb', 'userid'=>$user->id
));
1159 // test timeouts and modifications from different session
1160 set_cache_flag('userpreferenceschanged', $user->id
, 1, time() +
1000);
1161 $user->preference
['_lastloaded'] = $user->preference
['_lastloaded'] - 20;
1162 check_user_preferences_loaded($user);
1163 $this->assertEqual(count($user->preference
), 2);
1164 check_user_preferences_loaded($user, 10);
1165 $this->assertEqual(count($user->preference
), 3);
1166 $this->assertEqual($user->preference
['aaa'], 'bbb');
1167 set_cache_flag('userpreferenceschanged', $user->id
, null);
1170 public function test_set_user_preference() {
1173 if (!$otheruserid = $this->get_fake_preference_test_userid()) {
1174 $this->fail('Can not find unused user id for the preferences test');
1178 $DB->delete_records('user_preferences', array('userid'=>$otheruserid));
1179 set_cache_flag('userpreferenceschanged', $otheruserid, null);
1181 $user = new stdClass();
1182 $user->id
= $otheruserid;
1184 set_user_preference('aaa', 'bbb', $otheruserid);
1185 $this->assertEqual('bbb', $DB->get_field('user_preferences', 'value', array('userid'=>$otheruserid, 'name'=>'aaa')));
1186 $this->assertEqual('bbb', get_user_preferences('aaa', null, $otheruserid));
1188 set_user_preference('xxx', 'yyy', $user);
1189 $this->assertEqual('yyy', $DB->get_field('user_preferences', 'value', array('userid'=>$otheruserid, 'name'=>'xxx')));
1190 $this->assertEqual('yyy', get_user_preferences('xxx', null, $otheruserid));
1191 $this->assertTrue(is_array($user->preference
));
1192 $this->assertEqual($user->preference
['aaa'], 'bbb');
1193 $this->assertEqual($user->preference
['xxx'], 'yyy');
1195 set_user_preference('xxx', NULL, $user);
1196 $this->assertIdentical(false, $DB->get_field('user_preferences', 'value', array('userid'=>$otheruserid, 'name'=>'xxx')));
1197 $this->assertIdentical(null, get_user_preferences('xxx', null, $otheruserid));
1199 set_user_preference('ooo', true, $user);
1200 $prefs = get_user_preferences(null, null, $otheruserid);
1201 $this->assertIdentical($prefs['aaa'], $user->preference
['aaa']);
1202 $this->assertIdentical($prefs['ooo'], $user->preference
['ooo']);
1203 $this->assertIdentical($prefs['ooo'], '1');
1205 set_user_preference('null', 0, $user);
1206 $this->assertIdentical('0', get_user_preferences('null', null, $otheruserid));
1208 $this->assertIdentical('lala', get_user_preferences('undefined', 'lala', $otheruserid));
1210 $DB->delete_records('user_preferences', array('userid'=>$otheruserid));
1211 set_cache_flag('userpreferenceschanged', $otheruserid, null);
1213 // test $USER default
1214 set_user_preference('_test_user_preferences_pref', 'ok');
1215 $this->assertIdentical('ok', $USER->preference
['_test_user_preferences_pref']);
1216 unset_user_preference('_test_user_preferences_pref');
1217 $this->assertTrue(!isset($USER->preference
['_test_user_preferences_pref']));
1219 // Test 1333 char values (no need for unicode, there are already tests for that in DB tests)
1220 $longvalue = str_repeat('a', 1333);
1221 set_user_preference('_test_long_user_preference', $longvalue);
1222 $this->assertEqual($longvalue, get_user_preferences('_test_long_user_preference'));
1223 $this->assertEqual($longvalue,
1224 $DB->get_field('user_preferences', 'value', array('userid' => $USER->id
, 'name' => '_test_long_user_preference')));
1226 // Test > 1333 char values, coding_exception expected
1227 $longvalue = str_repeat('a', 1334);
1229 set_user_preference('_test_long_user_preference', $longvalue);
1230 $this->assertFail('Exception expected - longer than 1333 chars not allowed as preference value');
1231 } catch (Exception
$e) {
1232 $this->assertTrue($e instanceof coding_exception
);
1235 //test invalid params
1237 set_user_preference('_test_user_preferences_pref', array());
1238 $this->assertFail('Exception expected - array not valid preference value');
1239 } catch (Exception
$ex) {
1240 $this->assertTrue(true);
1243 set_user_preference('_test_user_preferences_pref', new stdClass
);
1244 $this->assertFail('Exception expected - class not valid preference value');
1245 } catch (Exception
$ex) {
1246 $this->assertTrue(true);
1249 set_user_preference('_test_user_preferences_pref', 1, array('xx'=>1));
1250 $this->assertFail('Exception expected - user instance expected');
1251 } catch (Exception
$ex) {
1252 $this->assertTrue(true);
1255 set_user_preference('_test_user_preferences_pref', 1, 'abc');
1256 $this->assertFail('Exception expected - user instance expected');
1257 } catch (Exception
$ex) {
1258 $this->assertTrue(true);
1261 set_user_preference('', 1);
1262 $this->assertFail('Exception expected - invalid name accepted');
1263 } catch (Exception
$ex) {
1264 $this->assertTrue(true);
1267 set_user_preference('1', 1);
1268 $this->assertFail('Exception expected - invalid name accepted');
1269 } catch (Exception
$ex) {
1270 $this->assertTrue(true);
1274 public function test_get_extra_user_fields() {
1276 $oldshowuseridentity = $CFG->showuseridentity
;
1278 // It would be really nice if there were a way to 'mock' has_capability
1279 // checks (either to return true or false) but as there is not, this
1280 // test doesn't test the capability check. Presumably, anyone running
1281 // unit tests will have the capability.
1282 $context = context_system
::instance();
1285 $CFG->showuseridentity
= '';
1286 $this->assertEqual(array(), get_extra_user_fields($context));
1289 $CFG->showuseridentity
= 'frog';
1290 $this->assertEqual(array('frog'), get_extra_user_fields($context));
1293 $CFG->showuseridentity
= 'frog,zombie';
1294 $this->assertEqual(array('frog', 'zombie'), get_extra_user_fields($context));
1296 // No fields, except
1297 $CFG->showuseridentity
= '';
1298 $this->assertEqual(array(), get_extra_user_fields($context, array('frog')));
1301 $CFG->showuseridentity
= 'frog';
1302 $this->assertEqual(array(), get_extra_user_fields($context, array('frog')));
1305 $CFG->showuseridentity
= 'frog,zombie';
1306 $this->assertEqual(array('zombie'), get_extra_user_fields($context, array('frog')));
1308 // As long as this test passes, the value will be set back. This is only
1310 $CFG->showuseridentity
= $oldshowuseridentity;
1313 public function test_get_extra_user_fields_sql() {
1315 $oldshowuseridentity = $CFG->showuseridentity
;
1316 $context = context_system
::instance();
1319 $CFG->showuseridentity
= '';
1320 $this->assertEqual('', get_extra_user_fields_sql($context));
1323 $CFG->showuseridentity
= 'frog';
1324 $this->assertEqual(', frog', get_extra_user_fields_sql($context));
1326 // Two fields with table prefix
1327 $CFG->showuseridentity
= 'frog,zombie';
1328 $this->assertEqual(', u1.frog, u1.zombie', get_extra_user_fields_sql($context, 'u1'));
1330 // Two fields with field prefix
1331 $CFG->showuseridentity
= 'frog,zombie';
1332 $this->assertEqual(', frog AS u_frog, zombie AS u_zombie',
1333 get_extra_user_fields_sql($context, '', 'u_'));
1335 // One field excluded
1336 $CFG->showuseridentity
= 'frog';
1337 $this->assertEqual('', get_extra_user_fields_sql($context, '', '', array('frog')));
1339 // Two fields, one excluded, table+field prefix
1340 $CFG->showuseridentity
= 'frog,zombie';
1341 $this->assertEqual(', u1.zombie AS u_zombie',
1342 get_extra_user_fields_sql($context, 'u1', 'u_', array('frog')));
1344 // As long as this test passes, the value will be set back. This is only
1346 $CFG->showuseridentity
= $oldshowuseridentity;
1349 public function test_userdate() {
1352 $testvalues = array(
1354 'time' => '1309514400',
1355 'usertimezone' => 'America/Moncton',
1356 'timezone' => '0.0', //no dst offset
1357 'expectedoutput' => 'Friday, 1 July 2011, 10:00 AM'
1360 'time' => '1309514400',
1361 'usertimezone' => 'America/Moncton',
1362 'timezone' => '99', //dst offset and timezone offset.
1363 'expectedoutput' => 'Friday, 1 July 2011, 07:00 AM'
1366 'time' => '1309514400',
1367 'usertimezone' => 'America/Moncton',
1368 'timezone' => 'America/Moncton', //dst offset and timezone offset.
1369 'expectedoutput' => 'Friday, 1 July 2011, 07:00 AM'
1372 'time' => '1293876000 ',
1373 'usertimezone' => 'America/Moncton',
1374 'timezone' => '0.0', //no dst offset
1375 'expectedoutput' => 'Saturday, 1 January 2011, 10:00 AM'
1378 'time' => '1293876000 ',
1379 'usertimezone' => 'America/Moncton',
1380 'timezone' => '99', //no dst offset in jan, so just timezone offset.
1381 'expectedoutput' => 'Saturday, 1 January 2011, 06:00 AM'
1384 'time' => '1293876000 ',
1385 'usertimezone' => 'America/Moncton',
1386 'timezone' => 'America/Moncton', //no dst offset in jan
1387 'expectedoutput' => 'Saturday, 1 January 2011, 06:00 AM'
1390 'time' => '1293876000 ',
1391 'usertimezone' => '2',
1392 'timezone' => '99', //take user timezone
1393 'expectedoutput' => 'Saturday, 1 January 2011, 12:00 PM'
1396 'time' => '1293876000 ',
1397 'usertimezone' => '-2',
1398 'timezone' => '99', //take user timezone
1399 'expectedoutput' => 'Saturday, 1 January 2011, 08:00 AM'
1402 'time' => '1293876000 ',
1403 'usertimezone' => '-10',
1404 'timezone' => '2', //take this timezone
1405 'expectedoutput' => 'Saturday, 1 January 2011, 12:00 PM'
1408 'time' => '1293876000 ',
1409 'usertimezone' => '-10',
1410 'timezone' => '-2', //take this timezone
1411 'expectedoutput' => 'Saturday, 1 January 2011, 08:00 AM'
1414 'time' => '1293876000 ',
1415 'usertimezone' => '-10',
1416 'timezone' => 'random/time', //this should show server time
1417 'expectedoutput' => 'Saturday, 1 January 2011, 06:00 PM'
1420 'time' => '1293876000 ',
1421 'usertimezone' => '14', //server time zone
1422 'timezone' => '99', //this should show user time
1423 'expectedoutput' => 'Saturday, 1 January 2011, 06:00 PM'
1427 //Check if forcetimezone is set then save it and set it to use user timezone
1428 $cfgforcetimezone = null;
1429 if (isset($CFG->forcetimezone
)) {
1430 $cfgforcetimezone = $CFG->forcetimezone
;
1431 $CFG->forcetimezone
= 99; //get user default timezone.
1433 //store user default timezone to restore later
1434 $userstimezone = $USER->timezone
;
1436 // The string version of date comes from server locale setting and does
1437 // not respect user language, so it is necessary to reset that.
1438 $oldlocale = setlocale(LC_TIME
, '0');
1439 setlocale(LC_TIME
, 'en_AU.UTF-8');
1441 //set default timezone to Australia/Perth, else time calulated
1442 //will not match expected values. Before that save system defaults.
1443 $systemdefaulttimezone = date_default_timezone_get();
1444 date_default_timezone_set('Australia/Perth');
1446 //get instance of textlib for strtolower
1447 $textlib = textlib_get_instance();
1448 foreach ($testvalues as $vals) {
1449 $USER->timezone
= $vals['usertimezone'];
1450 $actualoutput = userdate($vals['time'], '%A, %d %B %Y, %I:%M %p', $vals['timezone']);
1452 //On different systems case of AM PM changes so compare case insenitive
1453 $vals['expectedoutput'] = $textlib->strtolower($vals['expectedoutput']);
1454 $actualoutput = $textlib->strtolower($actualoutput);
1456 $this->assertEqual($vals['expectedoutput'], $actualoutput,
1457 "Expected: {$vals['expectedoutput']} => Actual: {$actualoutput},
1458 Please check if timezones are updated (Site adminstration -> location -> update timezone)");
1461 //restore user timezone back to what it was
1462 $USER->timezone
= $userstimezone;
1464 //restore forcetimezone
1465 if (!is_null($cfgforcetimezone)) {
1466 $CFG->forcetimezone
= $cfgforcetimezone;
1469 //restore system default values.
1470 date_default_timezone_set($systemdefaulttimezone);
1471 setlocale(LC_TIME
, $oldlocale);
1474 public function test_make_timestamp() {
1477 $testvalues = array(
1479 'usertimezone' => 'America/Moncton',
1486 'timezone' => '0.0',
1487 'applydst' => false, //no dst offset
1488 'expectedoutput' => '1309514400' // 6pm at UTC+0
1491 'usertimezone' => 'America/Moncton',
1498 'timezone' => '99', //user default timezone
1499 'applydst' => false, //don't apply dst
1500 'expectedoutput' => '1309528800'
1503 'usertimezone' => 'America/Moncton',
1510 'timezone' => '99', //user default timezone
1511 'applydst' => true, //apply dst
1512 'expectedoutput' => '1309525200'
1515 'usertimezone' => 'America/Moncton',
1522 'timezone' => 'America/Moncton', //string timezone
1523 'applydst' => true, //apply dst
1524 'expectedoutput' => '1309525200'
1527 'usertimezone' => '2',//no dst applyed
1534 'timezone' => '99', //take user timezone
1535 'applydst' => true, //apply dst
1536 'expectedoutput' => '1309507200'
1539 'usertimezone' => '-2',//no dst applyed
1546 'timezone' => '99', //take usertimezone
1547 'applydst' => true, //apply dst
1548 'expectedoutput' => '1309521600'
1551 'usertimezone' => '-10',//no dst applyed
1558 'timezone' => '2', //take this timezone
1559 'applydst' => true, //apply dst
1560 'expectedoutput' => '1309507200'
1563 'usertimezone' => '-10',//no dst applyed
1570 'timezone' => '-2', //take this timezone
1571 'applydst' => true, //apply dst,
1572 'expectedoutput' => '1309521600'
1575 'usertimezone' => '-10',//no dst applyed
1582 'timezone' => 'random/time', //This should show server time
1583 'applydst' => true, //apply dst,
1584 'expectedoutput' => '1309485600'
1587 'usertimezone' => '14',//server time
1594 'timezone' => '99', //get user time
1595 'applydst' => true, //apply dst,
1596 'expectedoutput' => '1309485600'
1600 //Check if forcetimezone is set then save it and set it to use user timezone
1601 $cfgforcetimezone = null;
1602 if (isset($CFG->forcetimezone
)) {
1603 $cfgforcetimezone = $CFG->forcetimezone
;
1604 $CFG->forcetimezone
= 99; //get user default timezone.
1607 //store user default timezone to restore later
1608 $userstimezone = $USER->timezone
;
1610 // The string version of date comes from server locale setting and does
1611 // not respect user language, so it is necessary to reset that.
1612 $oldlocale = setlocale(LC_TIME
, '0');
1613 setlocale(LC_TIME
, 'en_AU.UTF-8');
1615 //set default timezone to Australia/Perth, else time calulated
1616 //will not match expected values. Before that save system defaults.
1617 $systemdefaulttimezone = date_default_timezone_get();
1618 date_default_timezone_set('Australia/Perth');
1620 //get instance of textlib for strtolower
1621 $textlib = textlib_get_instance();
1622 //Test make_timestamp with all testvals and assert if anything wrong.
1623 foreach ($testvalues as $vals) {
1624 $USER->timezone
= $vals['usertimezone'];
1625 $actualoutput = make_timestamp(
1636 //On different systems case of AM PM changes so compare case insenitive
1637 $vals['expectedoutput'] = $textlib->strtolower($vals['expectedoutput']);
1638 $actualoutput = $textlib->strtolower($actualoutput);
1640 $this->assertEqual($vals['expectedoutput'], $actualoutput,
1641 "Expected: {$vals['expectedoutput']} => Actual: {$actualoutput},
1642 Please check if timezones are updated (Site adminstration -> location -> update timezone)");
1645 //restore user timezone back to what it was
1646 $USER->timezone
= $userstimezone;
1648 //restore forcetimezone
1649 if (!is_null($cfgforcetimezone)) {
1650 $CFG->forcetimezone
= $cfgforcetimezone;
1653 //restore system default values.
1654 date_default_timezone_set($systemdefaulttimezone);
1655 setlocale(LC_TIME
, $oldlocale);