|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00004 // // 00005 // NOTICE OF COPYRIGHT // 00006 // // 00007 // Moodle - Modular Object-Oriented Dynamic Learning Environment // 00008 // http://moodle.org // 00009 // // 00010 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // 00011 // // 00012 // This program is free software; you can redistribute it and/or modify // 00013 // it under the terms of the GNU General Public License as published by // 00014 // the Free Software Foundation; either version 2 of the License, or // 00015 // (at your option) any later version. // 00016 // // 00017 // This program is distributed in the hope that it will be useful, // 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of // 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 00020 // GNU General Public License for more details: // 00021 // // 00022 // http://www.gnu.org/copyleft/gpl.html // 00023 // // 00025 00038 if (!defined('MOODLE_INTERNAL')) { 00039 die('Direct access to this script is forbidden.'); 00040 } 00041 00042 require_once($CFG->libdir . '/moodlelib.php'); 00043 00044 class moodlelib_test extends UnitTestCase { 00045 00046 public static $includecoverage = array('lib/moodlelib.php'); 00047 00048 var $user_agents = array( 00049 'MSIE' => array( 00050 '5.0' => array('Windows 98' => 'Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)'), 00051 '5.5' => array('Windows 2000' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)'), 00052 '6.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'), 00053 '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)'), 00054 '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)'), 00055 '9.0' => array('Windows 7' => 'Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))'), 00056 00057 ), 00058 'Firefox' => array( 00059 '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'), 00060 '1.5' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8) Gecko/20051107 Firefox/1.5'), 00061 '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'), 00062 '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', 00063 'Ubuntu Linux AMD64' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20060601 Firefox/2.0 (Ubuntu-edgy)'), 00064 '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'), 00065 ), 00066 'Safari' => array( 00067 '312' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312'), 00068 '412' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412') 00069 ), 00070 'Safari iOS' => array( 00071 '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'), 00072 '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'), 00073 ), 00074 'WebKit Android' => array( 00075 '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'), 00076 '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'), 00077 ), 00078 'Chrome' => array( 00079 '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'), 00080 ), 00081 'Opera' => array( 00082 '8.51' => array('Windows XP' => 'Opera/8.51 (Windows NT 5.1; U; en)'), 00083 '9.0' => array('Windows XP' => 'Opera/9.0 (Windows NT 5.1; U; en)', 00084 'Debian Linux' => 'Opera/9.01 (X11; Linux i686; U; en)') 00085 ) 00086 ); 00087 00088 function test_cleanremoteaddr() { 00089 //IPv4 00090 $this->assertEqual(cleanremoteaddr('1023.121.234.1'), null); 00091 $this->assertEqual(cleanremoteaddr('123.121.234.01 '), '123.121.234.1'); 00092 00093 //IPv6 00094 $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:0:0'), null); 00095 $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:abh'), null); 00096 $this->assertEqual(cleanremoteaddr('0:0:0:::0:0:1'), null); 00097 $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:0', true), '::'); 00098 $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:1:1', true), '::1:1'); 00099 $this->assertEqual(cleanremoteaddr('abcd:00ef:0:0:0:0:0:0', true), 'abcd:ef::'); 00100 $this->assertEqual(cleanremoteaddr('1:0:0:0:0:0:0:1', true), '1::1'); 00101 $this->assertEqual(cleanremoteaddr('::10:1', false), '0:0:0:0:0:0:10:1'); 00102 $this->assertEqual(cleanremoteaddr('01:1::', false), '1:1:0:0:0:0:0:0'); 00103 $this->assertEqual(cleanremoteaddr('10::10', false), '10:0:0:0:0:0:0:10'); 00104 $this->assertEqual(cleanremoteaddr('::ffff:192.168.1.1', true), '::ffff:c0a8:11'); 00105 } 00106 00107 function test_address_in_subnet() { 00109 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.1/32')); 00110 $this->assertFalse(address_in_subnet('123.121.23.1', '123.121.23.0/32')); 00111 $this->assertTrue(address_in_subnet('10.10.10.100', '123.121.23.45/0')); 00112 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.0/24')); 00113 $this->assertFalse(address_in_subnet('123.121.34.1', '123.121.234.0/24')); 00114 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.0/30')); 00115 $this->assertFalse(address_in_subnet('123.121.23.8', '123.121.23.0/30')); 00116 $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba/128')); 00117 $this->assertFalse(address_in_subnet('bab:baba::baba', 'bab:baba::cece/128')); 00118 $this->assertTrue(address_in_subnet('baba:baba::baba', 'cece:cece::cece/0')); 00119 $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba/128')); 00120 $this->assertTrue(address_in_subnet('baba:baba::00ba', 'baba:baba::/120')); 00121 $this->assertFalse(address_in_subnet('baba:baba::aba', 'baba:baba::/120')); 00122 $this->assertTrue(address_in_subnet('baba::baba:00ba', 'baba::baba:0/112')); 00123 $this->assertFalse(address_in_subnet('baba::aba:00ba', 'baba::baba:0/112')); 00124 $this->assertFalse(address_in_subnet('aba::baba:0000', 'baba::baba:0/112')); 00125 00126 // fixed input 00127 $this->assertTrue(address_in_subnet('123.121.23.1 ', ' 123.121.23.0 / 24')); 00128 $this->assertTrue(address_in_subnet('::ffff:10.1.1.1', ' 0:0:0:000:0:ffff:a1:10 / 126')); 00129 00130 // incorrect input 00131 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/-2')); 00132 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/64')); 00133 $this->assertFalse(address_in_subnet('123.121.234.x', '123.121.234.1/24')); 00134 $this->assertFalse(address_in_subnet('123.121.234.0', '123.121.234.xx/24')); 00135 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/xx0')); 00136 $this->assertFalse(address_in_subnet('::1', '::aa:0/xx0')); 00137 $this->assertFalse(address_in_subnet('::1', '::aa:0/-5')); 00138 $this->assertFalse(address_in_subnet('::1', '::aa:0/130')); 00139 $this->assertFalse(address_in_subnet('x:1', '::aa:0/130')); 00140 $this->assertFalse(address_in_subnet('::1', '::ax:0/130')); 00141 00142 00144 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12-14')); 00145 $this->assertTrue(address_in_subnet('123.121.234.13', '123.121.234.12-14')); 00146 $this->assertTrue(address_in_subnet('123.121.234.14', '123.121.234.12-14')); 00147 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.12-14')); 00148 $this->assertFalse(address_in_subnet('123.121.234.20', '123.121.234.12-14')); 00149 $this->assertFalse(address_in_subnet('123.121.23.12', '123.121.234.12-14')); 00150 $this->assertFalse(address_in_subnet('123.12.234.12', '123.121.234.12-14')); 00151 $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba-babe')); 00152 $this->assertTrue(address_in_subnet('baba:baba::babc', 'baba:baba::baba-babe')); 00153 $this->assertTrue(address_in_subnet('baba:baba::babe', 'baba:baba::baba-babe')); 00154 $this->assertFalse(address_in_subnet('bab:baba::bab0', 'bab:baba::baba-babe')); 00155 $this->assertFalse(address_in_subnet('bab:baba::babf', 'bab:baba::baba-babe')); 00156 $this->assertFalse(address_in_subnet('bab:baba::bfbe', 'bab:baba::baba-babe')); 00157 $this->assertFalse(address_in_subnet('bfb:baba::babe', 'bab:baba::baba-babe')); 00158 00159 // fixed input 00160 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12 - 14 ')); 00161 $this->assertTrue(address_in_subnet('bab:baba::babe', 'bab:baba::baba - babe ')); 00162 00163 // incorrect input 00164 $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12-234.14')); 00165 $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12-256')); 00166 $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12--256')); 00167 00168 00170 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12')); 00171 $this->assertFalse(address_in_subnet('123.121.23.12', '123.121.23.13')); 00172 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.')); 00173 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234')); 00174 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121')); 00175 $this->assertTrue(address_in_subnet('123.121.234.12', '123')); 00176 $this->assertFalse(address_in_subnet('123.121.234.1', '12.121.234.')); 00177 $this->assertFalse(address_in_subnet('123.121.234.1', '12.121.234')); 00178 $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:baba::bab')); 00179 $this->assertFalse(address_in_subnet('baba:baba::ba', 'baba:baba::bc')); 00180 $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:baba')); 00181 $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:')); 00182 $this->assertFalse(address_in_subnet('bab:baba::bab', 'baba:')); 00183 00184 00186 $this->assertTrue(address_in_subnet('123.121.234.12', '::1/64, 124., 123.121.234.10-30')); 00187 $this->assertTrue(address_in_subnet('124.121.234.12', '::1/64, 124., 123.121.234.10-30')); 00188 $this->assertTrue(address_in_subnet('::2', '::1/64, 124., 123.121.234.10-30')); 00189 $this->assertFalse(address_in_subnet('12.121.234.12', '::1/64, 124., 123.121.234.10-30')); 00190 00191 00193 $this->assertFalse(address_in_subnet('123.123.123.123', '')); 00194 } 00195 00200 function test_check_browser_version() 00201 { 00202 global $CFG; 00203 00204 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['412']['Mac OS X']; 00205 $this->assertTrue(check_browser_version('Safari')); 00206 $this->assertTrue(check_browser_version('WebKit')); 00207 $this->assertTrue(check_browser_version('Safari', '312')); 00208 $this->assertFalse(check_browser_version('Safari', '500')); 00209 $this->assertFalse(check_browser_version('Chrome')); 00210 $this->assertFalse(check_browser_version('Safari iOS')); 00211 00212 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari iOS']['528']['iPhone']; 00213 $this->assertTrue(check_browser_version('Safari iOS')); 00214 $this->assertTrue(check_browser_version('WebKit')); 00215 $this->assertTrue(check_browser_version('Safari iOS', '527')); 00216 $this->assertFalse(check_browser_version('Safari iOS', 590)); 00217 $this->assertFalse(check_browser_version('Safari', '312')); 00218 $this->assertFalse(check_browser_version('Safari', '500')); 00219 $this->assertFalse(check_browser_version('Chrome')); 00220 00221 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['WebKit Android']['530']['Nexus']; 00222 $this->assertTrue(check_browser_version('WebKit')); 00223 $this->assertTrue(check_browser_version('WebKit Android', '527')); 00224 $this->assertFalse(check_browser_version('WebKit Android', 590)); 00225 $this->assertFalse(check_browser_version('Safari')); 00226 $this->assertFalse(check_browser_version('Chrome')); 00227 00228 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Chrome']['8']['Mac OS X']; 00229 $this->assertTrue(check_browser_version('Chrome')); 00230 $this->assertTrue(check_browser_version('WebKit')); 00231 $this->assertTrue(check_browser_version('Chrome', 8)); 00232 $this->assertFalse(check_browser_version('Chrome', 10)); 00233 $this->assertFalse(check_browser_version('Safari', '1')); 00234 00235 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP']; 00236 $this->assertTrue(check_browser_version('Opera')); 00237 $this->assertTrue(check_browser_version('Opera', '8.0')); 00238 $this->assertFalse(check_browser_version('Opera', '10.0')); 00239 00240 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2']; 00241 $this->assertTrue(check_browser_version('MSIE')); 00242 $this->assertTrue(check_browser_version('MSIE', '5.0')); 00243 $this->assertFalse(check_browser_version('MSIE', '7.0')); 00244 00245 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['5.0']['Windows 98']; 00246 $this->assertFalse(check_browser_version('MSIE')); 00247 $this->assertTrue(check_browser_version('MSIE', 0)); 00248 $this->assertTrue(check_browser_version('MSIE', '5.0')); 00249 $this->assertFalse(check_browser_version('MSIE', '7.0')); 00250 00251 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['9.0']['Windows 7']; 00252 $this->assertTrue(check_browser_version('MSIE')); 00253 $this->assertTrue(check_browser_version('MSIE', 0)); 00254 $this->assertTrue(check_browser_version('MSIE', '5.0')); 00255 $this->assertTrue(check_browser_version('MSIE', '9.0')); 00256 $this->assertFalse(check_browser_version('MSIE', '10')); 00257 00258 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP']; 00259 $this->assertTrue(check_browser_version('Firefox')); 00260 $this->assertTrue(check_browser_version('Firefox', '1.5')); 00261 $this->assertFalse(check_browser_version('Firefox', '3.0')); 00262 } 00263 00264 function test_get_browser_version_classes() { 00265 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['412']['Mac OS X']; 00266 $this->assertEqual(array('safari'), get_browser_version_classes()); 00267 00268 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Chrome']['8']['Mac OS X']; 00269 $this->assertEqual(array('safari'), get_browser_version_classes()); 00270 00271 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari iOS']['528']['iPhone']; 00272 $this->assertEqual(array('safari', 'ios'), get_browser_version_classes()); 00273 00274 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['WebKit Android']['530']['Nexus']; 00275 $this->assertEqual(array('safari', 'android'), get_browser_version_classes()); 00276 00277 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Chrome']['8']['Mac OS X']; 00278 $this->assertEqual(array('safari'), get_browser_version_classes()); 00279 00280 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP']; 00281 $this->assertEqual(array('opera'), get_browser_version_classes()); 00282 00283 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2']; 00284 $this->assertEqual(array('ie', 'ie6'), get_browser_version_classes()); 00285 00286 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['7.0']['Windows XP SP2']; 00287 $this->assertEqual(array('ie', 'ie7'), get_browser_version_classes()); 00288 00289 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['8.0']['Windows Vista']; 00290 $this->assertEqual(array('ie', 'ie8'), get_browser_version_classes()); 00291 00292 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP']; 00293 $this->assertEqual(array('gecko', 'gecko18'), get_browser_version_classes()); 00294 00295 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['3.0.6']['SUSE']; 00296 $this->assertEqual(array('gecko', 'gecko19'), get_browser_version_classes()); 00297 } 00298 00299 function test_get_device_type() { 00300 // IE8 (common pattern ~1.5% of IE7/8 users have embedded IE6 agent)) 00301 $_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)'; 00302 $this->assertEqual('default', get_device_type()); 00303 // Genuine IE6 00304 $_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)'; 00305 $this->assertEqual('legacy', get_device_type()); 00306 } 00307 00308 function test_fix_utf8() { 00309 // make sure valid data including other types is not changed 00310 $this->assertidentical(null, fix_utf8(null)); 00311 $this->assertidentical(1, fix_utf8(1)); 00312 $this->assertidentical(1.1, fix_utf8(1.1)); 00313 $this->assertidentical(true, fix_utf8(true)); 00314 $this->assertidentical('', fix_utf8('')); 00315 $array = array('do', 're', 'mi'); 00316 $this->assertidentical($array, fix_utf8($array)); 00317 $object = new stdClass(); 00318 $object->a = 'aa'; 00319 $object->b = 'bb'; 00320 $this->assertidentical($object, fix_utf8($object)); 00321 00322 // valid utf8 string 00323 $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")); 00324 00325 // invalid utf8 string 00326 $this->assertidentical('aaabbb', fix_utf8('aaa'.chr(130).'bbb')); 00327 } 00328 00329 function test_optional_param() { 00330 global $CFG; 00331 00332 $_POST['username'] = 'post_user'; 00333 $_GET['username'] = 'get_user'; 00334 $this->assertIdentical(optional_param('username', 'default_user', PARAM_RAW), $_POST['username']); 00335 00336 unset($_POST['username']); 00337 $this->assertIdentical(optional_param('username', 'default_user', PARAM_RAW), $_GET['username']); 00338 00339 unset($_GET['username']); 00340 $this->assertIdentical(optional_param('username', 'default_user', PARAM_RAW), 'default_user'); 00341 00342 // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2 00343 $_POST['username'] = 'post_user'; 00344 try { 00345 optional_param('username', 'default_user', null); 00346 $this->fail('coding_exception expected'); 00347 } catch (coding_exception $ex) { 00348 $this->assertTrue(true); 00349 } 00350 try { 00351 @optional_param('username', 'default_user'); 00352 $this->fail('coding_exception expected'); 00353 } catch (coding_exception $ex) { 00354 $this->assertTrue(true); 00355 } 00356 try { 00357 @optional_param('username'); 00358 $this->fail('coding_exception expected'); 00359 } catch (coding_exception $ex) { 00360 $this->assertTrue(true); 00361 } 00362 try { 00363 optional_param('', 'default_user', PARAM_RAW); 00364 $this->fail('coding_exception expected'); 00365 } catch (coding_exception $ex) { 00366 $this->assertTrue(true); 00367 } 00368 00369 // make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3 00370 $debugging = isset($CFG->debug) ? $CFG->debug : null; 00371 $debugdisplay = isset($CFG->debugdisplay) ? $CFG->debugdisplay : null; 00372 $CFG->debug = 38911; 00373 $CFG->debugdisplay = true; 00374 00375 ob_start(); 00376 $this->assertIdentical(optional_param('username', 'default_user', PARAM_RAW), $_POST['username']); 00377 $d = ob_end_clean(); 00378 $this->assertTrue($d !== ''); 00379 00380 if ($debugging !== null) { 00381 $CFG->debug = $debugging; 00382 } else { 00383 unset($CFG->debug); 00384 } 00385 if ($debugdisplay !== null) { 00386 $CFG->debugdisplay = $debugdisplay; 00387 } else { 00388 unset($CFG->debugdisplay); 00389 } 00390 } 00391 00392 function test_optional_param_array() { 00393 global $CFG; 00394 00395 $_POST['username'] = array('a'=>'post_user'); 00396 $_GET['username'] = array('a'=>'get_user'); 00397 $this->assertIdentical(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), $_POST['username']); 00398 00399 unset($_POST['username']); 00400 $this->assertIdentical(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), $_GET['username']); 00401 00402 unset($_GET['username']); 00403 $this->assertIdentical(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), array('a'=>'default_user')); 00404 00405 // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2 00406 $_POST['username'] = array('a'=>'post_user'); 00407 try { 00408 optional_param_array('username', array('a'=>'default_user'), null); 00409 $this->fail('coding_exception expected'); 00410 } catch (coding_exception $ex) { 00411 $this->assertTrue(true); 00412 } 00413 try { 00414 @optional_param_array('username', array('a'=>'default_user')); 00415 $this->fail('coding_exception expected'); 00416 } catch (coding_exception $ex) { 00417 $this->assertTrue(true); 00418 } 00419 try { 00420 @optional_param_array('username'); 00421 $this->fail('coding_exception expected'); 00422 } catch (coding_exception $ex) { 00423 $this->assertTrue(true); 00424 } 00425 try { 00426 optional_param_array('', array('a'=>'default_user'), PARAM_RAW); 00427 $this->fail('coding_exception expected'); 00428 } catch (coding_exception $ex) { 00429 $this->assertTrue(true); 00430 } 00431 00432 // do not allow nested arrays 00433 try { 00434 $_POST['username'] = array('a'=>array('b'=>'post_user')); 00435 optional_param_array('username', array('a'=>'default_user'), PARAM_RAW); 00436 $this->fail('coding_exception expected'); 00437 } catch (coding_exception $ex) { 00438 $this->assertTrue(true); 00439 } 00440 00441 // do not allow non-arrays 00442 $debugging = isset($CFG->debug) ? $CFG->debug : null; 00443 $debugdisplay = isset($CFG->debugdisplay) ? $CFG->debugdisplay : null; 00444 $CFG->debug = 38911; 00445 $CFG->debugdisplay = true; 00446 00447 ob_start(); 00448 $_POST['username'] = 'post_user'; 00449 $this->assertIdentical(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), array('a'=>'default_user')); 00450 $d = ob_end_clean(); 00451 $this->assertTrue($d !== ''); 00452 00453 // make sure array keys are sanitised 00454 ob_start(); 00455 $_POST['username'] = array('abc123_;-/*-+ '=>'arrggh', 'a1_-'=>'post_user'); 00456 $this->assertIdentical(optional_param_array('username', array(), PARAM_RAW), array('a1_-'=>'post_user')); 00457 $d = ob_end_clean(); 00458 $this->assertTrue($d !== ''); 00459 00460 if ($debugging !== null) { 00461 $CFG->debug = $debugging; 00462 } else { 00463 unset($CFG->debug); 00464 } 00465 if ($debugdisplay !== null) { 00466 $CFG->debugdisplay = $debugdisplay; 00467 } else { 00468 unset($CFG->debugdisplay); 00469 } 00470 } 00471 00472 function test_required_param() { 00473 global $CFG; 00474 00475 $_POST['username'] = 'post_user'; 00476 $_GET['username'] = 'get_user'; 00477 $this->assertIdentical(required_param('username', PARAM_RAW), 'post_user'); 00478 00479 unset($_POST['username']); 00480 $this->assertIdentical(required_param('username', PARAM_RAW), 'get_user'); 00481 00482 unset($_GET['username']); 00483 try { 00484 $this->assertIdentical(required_param('username', PARAM_RAW), 'default_user'); 00485 $this->fail('moodle_exception expected'); 00486 } catch (moodle_exception $ex) { 00487 $this->assertTrue(true); 00488 } 00489 00490 // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2 00491 $_POST['username'] = 'post_user'; 00492 try { 00493 @required_param('username'); 00494 $this->fail('coding_exception expected'); 00495 } catch (coding_exception $ex) { 00496 $this->assertTrue(true); 00497 } 00498 try { 00499 required_param('username', ''); 00500 $this->fail('coding_exception expected'); 00501 } catch (coding_exception $ex) { 00502 $this->assertTrue(true); 00503 } 00504 try { 00505 required_param('', PARAM_RAW); 00506 $this->fail('coding_exception expected'); 00507 } catch (coding_exception $ex) { 00508 $this->assertTrue(true); 00509 } 00510 00511 // make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3 00512 $debugging = isset($CFG->debug) ? $CFG->debug : null; 00513 $debugdisplay = isset($CFG->debugdisplay) ? $CFG->debugdisplay : null; 00514 $CFG->debug = 38911; 00515 $CFG->debugdisplay = true; 00516 00517 ob_start(); 00518 $this->assertIdentical(required_param('username', PARAM_RAW), $_POST['username']); 00519 $d = ob_end_clean(); 00520 $this->assertTrue($d !== ''); 00521 00522 if ($debugging !== null) { 00523 $CFG->debug = $debugging; 00524 } else { 00525 unset($CFG->debug); 00526 } 00527 if ($debugdisplay !== null) { 00528 $CFG->debugdisplay = $debugdisplay; 00529 } else { 00530 unset($CFG->debugdisplay); 00531 } 00532 } 00533 00534 function test_required_param_array() { 00535 global $CFG; 00536 00537 $_POST['username'] = array('a'=>'post_user'); 00538 $_GET['username'] = array('a'=>'get_user'); 00539 $this->assertIdentical(required_param_array('username', PARAM_RAW), $_POST['username']); 00540 00541 unset($_POST['username']); 00542 $this->assertIdentical(required_param_array('username', PARAM_RAW), $_GET['username']); 00543 00544 // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2 00545 $_POST['username'] = array('a'=>'post_user'); 00546 try { 00547 required_param_array('username', null); 00548 $this->fail('coding_exception expected'); 00549 } catch (coding_exception $ex) { 00550 $this->assertTrue(true); 00551 } 00552 try { 00553 @required_param_array('username'); 00554 $this->fail('coding_exception expected'); 00555 } catch (coding_exception $ex) { 00556 $this->assertTrue(true); 00557 } 00558 try { 00559 required_param_array('', PARAM_RAW); 00560 $this->fail('coding_exception expected'); 00561 } catch (coding_exception $ex) { 00562 $this->assertTrue(true); 00563 } 00564 00565 // do not allow nested arrays 00566 try { 00567 $_POST['username'] = array('a'=>array('b'=>'post_user')); 00568 required_param_array('username', PARAM_RAW); 00569 $this->fail('coding_exception expected'); 00570 } catch (coding_exception $ex) { 00571 $this->assertTrue(true); 00572 } 00573 00574 // do not allow non-arrays 00575 try { 00576 $_POST['username'] = 'post_user'; 00577 required_param_array('username', PARAM_RAW); 00578 $this->fail('moodle_exception expected'); 00579 } catch (moodle_exception $ex) { 00580 $this->assertTrue(true); 00581 } 00582 00583 // do not allow non-arrays 00584 $debugging = isset($CFG->debug) ? $CFG->debug : null; 00585 $debugdisplay = isset($CFG->debugdisplay) ? $CFG->debugdisplay : null; 00586 $CFG->debug = 38911; 00587 $CFG->debugdisplay = true; 00588 00589 // make sure array keys are sanitised 00590 ob_start(); 00591 $_POST['username'] = array('abc123_;-/*-+ '=>'arrggh', 'a1_-'=>'post_user'); 00592 $this->assertIdentical(required_param_array('username', PARAM_RAW), array('a1_-'=>'post_user')); 00593 $d = ob_end_clean(); 00594 $this->assertTrue($d !== ''); 00595 00596 if ($debugging !== null) { 00597 $CFG->debug = $debugging; 00598 } else { 00599 unset($CFG->debug); 00600 } 00601 if ($debugdisplay !== null) { 00602 $CFG->debugdisplay = $debugdisplay; 00603 } else { 00604 unset($CFG->debugdisplay); 00605 } 00606 } 00607 00608 function test_clean_param() { 00609 // forbid objects and arrays 00610 try { 00611 clean_param(array('x', 'y'), PARAM_RAW); 00612 $this->fail('coding_exception expected'); 00613 } catch (coding_exception $ex) { 00614 $this->assertTrue(true); 00615 } 00616 try { 00617 $param = new stdClass(); 00618 $param->id = 1; 00619 clean_param($param, PARAM_RAW); 00620 $this->fail('coding_exception expected'); 00621 } catch (coding_exception $ex) { 00622 $this->assertTrue(true); 00623 } 00624 00625 // require correct type 00626 try { 00627 clean_param('x', 'xxxxxx'); 00628 $this->fail('moodle_exception expected'); 00629 } catch (moodle_exception $ex) { 00630 $this->assertTrue(true); 00631 } 00632 try { 00633 @clean_param('x'); 00634 $this->fail('moodle_exception expected'); 00635 } catch (moodle_exception $ex) { 00636 $this->assertTrue(true); 00637 } 00638 00639 } 00640 00641 function test_clean_param_array() { 00642 $this->assertIdentical(clean_param_array(null, PARAM_RAW), array()); 00643 $this->assertIdentical(clean_param_array(array('a', 'b'), PARAM_RAW), array('a', 'b')); 00644 $this->assertIdentical(clean_param_array(array('a', array('b')), PARAM_RAW, true), array('a', array('b'))); 00645 00646 // require correct type 00647 try { 00648 clean_param_array(array('x'), 'xxxxxx'); 00649 $this->fail('moodle_exception expected'); 00650 } catch (moodle_exception $ex) { 00651 $this->assertTrue(true); 00652 } 00653 try { 00654 @clean_param_array(array('x')); 00655 $this->fail('moodle_exception expected'); 00656 } catch (moodle_exception $ex) { 00657 $this->assertTrue(true); 00658 } 00659 00660 try { 00661 clean_param_array(array('x', array('y')), PARAM_RAW); 00662 $this->fail('coding_exception expected'); 00663 } catch (coding_exception $ex) { 00664 $this->assertTrue(true); 00665 } 00666 00667 // test recursive 00668 } 00669 00670 function test_clean_param_raw() { 00671 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_RAW), 00672 '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)'); 00673 } 00674 00675 function test_clean_param_trim() { 00676 $this->assertEqual(clean_param(" Frog toad \r\n ", PARAM_RAW_TRIMMED), 'Frog toad'); 00677 } 00678 00679 function test_clean_param_clean() { 00680 // PARAM_CLEAN is an ugly hack, do not use in new code (skodak) 00681 // instead use more specific type, or submit sothing that can be verified properly 00682 $this->assertEqual(clean_param('xx<script>', PARAM_CLEAN), 'xx'); 00683 } 00684 00685 function test_clean_param_alpha() { 00686 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHA), 00687 'DSFMOSDJ'); 00688 } 00689 00690 function test_clean_param_alphanum() { 00691 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHANUM), 00692 '978942897DSFMOSDJ'); 00693 } 00694 00695 function test_clean_param_alphaext() { 00696 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHAEXT), 00697 'DSFMOSDJ'); 00698 } 00699 00700 function test_clean_param_sequence() { 00701 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_SEQUENCE), 00702 ',9789,42897'); 00703 } 00704 00705 function test_clean_param_component() { 00706 // please note the cleaning of component names is very strict, no guessing here 00707 $this->assertIdentical(clean_param('mod_forum', PARAM_COMPONENT), 'mod_forum'); 00708 $this->assertIdentical(clean_param('block_online_users', PARAM_COMPONENT), 'block_online_users'); 00709 $this->assertIdentical(clean_param('block_blond_online_users', PARAM_COMPONENT), 'block_blond_online_users'); 00710 $this->assertIdentical(clean_param('mod_something2', PARAM_COMPONENT), 'mod_something2'); 00711 $this->assertIdentical(clean_param('forum', PARAM_COMPONENT), 'forum'); 00712 $this->assertIdentical(clean_param('user', PARAM_COMPONENT), 'user'); 00713 $this->assertIdentical(clean_param('rating', PARAM_COMPONENT), 'rating'); 00714 $this->assertIdentical(clean_param('mod_2something', PARAM_COMPONENT), ''); 00715 $this->assertIdentical(clean_param('2mod_something', PARAM_COMPONENT), ''); 00716 $this->assertIdentical(clean_param('mod_something_xx', PARAM_COMPONENT), ''); 00717 $this->assertIdentical(clean_param('auth_something__xx', PARAM_COMPONENT), ''); 00718 $this->assertIdentical(clean_param('mod_Something', PARAM_COMPONENT), ''); 00719 $this->assertIdentical(clean_param('mod_somethíng', PARAM_COMPONENT), ''); 00720 $this->assertIdentical(clean_param('auth_xx-yy', PARAM_COMPONENT), ''); 00721 $this->assertIdentical(clean_param('_auth_xx', PARAM_COMPONENT), ''); 00722 $this->assertIdentical(clean_param('a2uth_xx', PARAM_COMPONENT), ''); 00723 $this->assertIdentical(clean_param('auth_xx_', PARAM_COMPONENT), ''); 00724 $this->assertIdentical(clean_param('auth_xx.old', PARAM_COMPONENT), ''); 00725 $this->assertIdentical(clean_param('_user', PARAM_COMPONENT), ''); 00726 $this->assertIdentical(clean_param('2rating', PARAM_COMPONENT), ''); 00727 $this->assertIdentical(clean_param('user_', PARAM_COMPONENT), ''); 00728 } 00729 00730 function test_clean_param_plugin() { 00731 // please note the cleaning of plugin names is very strict, no guessing here 00732 $this->assertIdentical(clean_param('forum', PARAM_PLUGIN), 'forum'); 00733 $this->assertIdentical(clean_param('forum2', PARAM_PLUGIN), 'forum2'); 00734 $this->assertIdentical(clean_param('online_users', PARAM_PLUGIN), 'online_users'); 00735 $this->assertIdentical(clean_param('blond_online_users', PARAM_PLUGIN), 'blond_online_users'); 00736 $this->assertIdentical(clean_param('online__users', PARAM_PLUGIN), ''); 00737 $this->assertIdentical(clean_param('forum ', PARAM_PLUGIN), ''); 00738 $this->assertIdentical(clean_param('forum.old', PARAM_PLUGIN), ''); 00739 $this->assertIdentical(clean_param('xx-yy', PARAM_PLUGIN), ''); 00740 $this->assertIdentical(clean_param('2xx', PARAM_PLUGIN), ''); 00741 $this->assertIdentical(clean_param('Xx', PARAM_PLUGIN), ''); 00742 $this->assertIdentical(clean_param('_xx', PARAM_PLUGIN), ''); 00743 $this->assertIdentical(clean_param('xx_', PARAM_PLUGIN), ''); 00744 } 00745 00746 function test_clean_param_area() { 00747 // please note the cleaning of area names is very strict, no guessing here 00748 $this->assertIdentical(clean_param('something', PARAM_AREA), 'something'); 00749 $this->assertIdentical(clean_param('something2', PARAM_AREA), 'something2'); 00750 $this->assertIdentical(clean_param('some_thing', PARAM_AREA), 'some_thing'); 00751 $this->assertIdentical(clean_param('some_thing_xx', PARAM_AREA), 'some_thing_xx'); 00752 $this->assertIdentical(clean_param('_something', PARAM_AREA), ''); 00753 $this->assertIdentical(clean_param('something_', PARAM_AREA), ''); 00754 $this->assertIdentical(clean_param('2something', PARAM_AREA), ''); 00755 $this->assertIdentical(clean_param('Something', PARAM_AREA), ''); 00756 $this->assertIdentical(clean_param('some-thing', PARAM_AREA), ''); 00757 $this->assertIdentical(clean_param('somethííng', PARAM_AREA), ''); 00758 $this->assertIdentical(clean_param('something.x', PARAM_AREA), ''); 00759 } 00760 00761 function test_clean_param_text() { 00762 $this->assertEqual(PARAM_TEXT, PARAM_MULTILANG); 00763 //standard 00764 $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>'); 00765 $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>'); 00766 $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>'); 00767 //malformed 00768 $this->assertEqual(clean_param('<span lang="en" class="multilang">aa</span>', PARAM_TEXT), '<span lang="en" class="multilang">aa</span>'); 00769 $this->assertEqual(clean_param('<span lang="en" class="nothing" class="multilang">aa</span>', PARAM_TEXT), 'aa'); 00770 $this->assertEqual(clean_param('<lang lang="en" class="multilang">aa</lang>', PARAM_TEXT), 'aa'); 00771 $this->assertEqual(clean_param('<lang lang="en!!">aa</lang>', PARAM_TEXT), 'aa'); 00772 $this->assertEqual(clean_param('<span lang="en==" class="multilang">aa</span>', PARAM_TEXT), 'aa'); 00773 $this->assertEqual(clean_param('a<em>b</em>c', PARAM_TEXT), 'abc'); 00774 $this->assertEqual(clean_param('a><xx >c>', PARAM_TEXT), 'a>c>'); // standard strip_tags() behaviour 00775 $this->assertEqual(clean_param('a<b', PARAM_TEXT), 'a'); 00776 $this->assertEqual(clean_param('a>b', PARAM_TEXT), 'a>b'); 00777 $this->assertEqual(clean_param('<lang lang="en">a>a</lang>', PARAM_TEXT), '<lang lang="en">a>a</lang>'); // standard strip_tags() behaviour 00778 $this->assertEqual(clean_param('<lang lang="en">a<a</lang>', PARAM_TEXT), 'a'); 00779 $this->assertEqual(clean_param('<lang lang="en">a<br>a</lang>', PARAM_TEXT), '<lang lang="en">aa</lang>'); 00780 } 00781 00782 function test_clean_param_url() { 00783 // Test PARAM_URL and PARAM_LOCALURL a bit 00784 $this->assertEqual(clean_param('http://google.com/', PARAM_URL), 'http://google.com/'); 00785 $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/'); 00786 $this->assertEqual(clean_param('http://localhost/', PARAM_URL), 'http://localhost/'); 00787 $this->assertEqual(clean_param('http://0.255.1.1/numericip.php', PARAM_URL), 'http://0.255.1.1/numericip.php'); 00788 $this->assertEqual(clean_param('/just/a/path', PARAM_URL), '/just/a/path'); 00789 $this->assertEqual(clean_param('funny:thing', PARAM_URL), ''); 00790 } 00791 00792 function test_clean_param_localurl() { 00793 global $CFG; 00794 $this->assertEqual(clean_param('http://google.com/', PARAM_LOCALURL), ''); 00795 $this->assertEqual(clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_LOCALURL), ''); 00796 $this->assertEqual(clean_param($CFG->wwwroot, PARAM_LOCALURL), $CFG->wwwroot); 00797 $this->assertEqual(clean_param('/just/a/path', PARAM_LOCALURL), '/just/a/path'); 00798 $this->assertEqual(clean_param('funny:thing', PARAM_LOCALURL), ''); 00799 $this->assertEqual(clean_param('course/view.php?id=3', PARAM_LOCALURL), 'course/view.php?id=3'); 00800 } 00801 00802 function test_clean_param_file() { 00803 $this->assertEqual(clean_param('correctfile.txt', PARAM_FILE), 'correctfile.txt'); 00804 $this->assertEqual(clean_param('b\'a<d`\\/fi:l>e.t"x|t', PARAM_FILE), 'badfile.txt'); 00805 $this->assertEqual(clean_param('../parentdirfile.txt', PARAM_FILE), 'parentdirfile.txt'); 00806 //The following behaviours have been maintained although they seem a little odd 00807 $this->assertEqual(clean_param('funny:thing', PARAM_FILE), 'funnything'); 00808 $this->assertEqual(clean_param('./currentdirfile.txt', PARAM_FILE), '.currentdirfile.txt'); 00809 $this->assertEqual(clean_param('c:\temp\windowsfile.txt', PARAM_FILE), 'ctempwindowsfile.txt'); 00810 $this->assertEqual(clean_param('/home/user/linuxfile.txt', PARAM_FILE), 'homeuserlinuxfile.txt'); 00811 $this->assertEqual(clean_param('~/myfile.txt', PARAM_FILE), '~myfile.txt'); 00812 } 00813 00814 function test_clean_param_username() { 00815 global $CFG; 00816 $currentstatus = $CFG->extendedusernamechars; 00817 00818 // Run tests with extended character == FALSE; 00819 $CFG->extendedusernamechars = FALSE; 00820 $this->assertEqual(clean_param('johndoe123', PARAM_USERNAME), 'johndoe123' ); 00821 $this->assertEqual(clean_param('john.doe', PARAM_USERNAME), 'john.doe'); 00822 $this->assertEqual(clean_param('john-doe', PARAM_USERNAME), 'john-doe'); 00823 $this->assertEqual(clean_param('john- doe', PARAM_USERNAME), 'john-doe'); 00824 $this->assertEqual(clean_param('john_doe', PARAM_USERNAME), 'john_doe'); 00825 $this->assertEqual(clean_param('john@doe', PARAM_USERNAME), 'john@doe'); 00826 $this->assertEqual(clean_param('john~doe', PARAM_USERNAME), 'johndoe'); 00827 $this->assertEqual(clean_param('john´doe', PARAM_USERNAME), 'johndoe'); 00828 $this->assertEqual(clean_param('john#$%&() ', PARAM_USERNAME), 'john'); 00829 $this->assertEqual(clean_param('JOHNdóé ', PARAM_USERNAME), 'johnd'); 00830 $this->assertEqual(clean_param('john.,:;-_/|\ñÑ[]A_X-,D {} ~!@#$%^&*()_+ ?><[] ščřžžý ?ýá?ý??doe ', PARAM_USERNAME), 'john.-_a_x-d@_doe'); 00831 00832 00833 // Test success condition, if extendedusernamechars == ENABLE; 00834 $CFG->extendedusernamechars = TRUE; 00835 $this->assertEqual(clean_param('john_doe', PARAM_USERNAME), 'john_doe'); 00836 $this->assertEqual(clean_param('john@doe', PARAM_USERNAME), 'john@doe'); 00837 $this->assertEqual(clean_param('john# $%&()+_^', PARAM_USERNAME), 'john#$%&()+_^'); 00838 $this->assertEqual(clean_param('john~doe', PARAM_USERNAME), 'john~doe'); 00839 $this->assertEqual(clean_param('joHN´doe', PARAM_USERNAME), 'john´doe'); 00840 $this->assertEqual(clean_param('johnDOE', PARAM_USERNAME), 'johndoe'); 00841 $this->assertEqual(clean_param('johndóé ', PARAM_USERNAME), 'johndóé'); 00842 00843 $CFG->extendedusernamechars = $currentstatus; 00844 } 00845 00846 function test_clean_param_stringid() { 00847 // Test string identifiers validation 00848 // valid strings: 00849 $this->assertEqual(clean_param('validstring', PARAM_STRINGID), 'validstring'); 00850 $this->assertEqual(clean_param('mod/foobar:valid_capability', PARAM_STRINGID), 'mod/foobar:valid_capability'); 00851 $this->assertEqual(clean_param('CZ', PARAM_STRINGID), 'CZ'); 00852 $this->assertEqual(clean_param('application/vnd.ms-powerpoint', PARAM_STRINGID), 'application/vnd.ms-powerpoint'); 00853 $this->assertEqual(clean_param('grade2', PARAM_STRINGID), 'grade2'); 00854 // invalid strings: 00855 $this->assertEqual(clean_param('trailing ', PARAM_STRINGID), ''); 00856 $this->assertEqual(clean_param('space bar', PARAM_STRINGID), ''); 00857 $this->assertEqual(clean_param('0numeric', PARAM_STRINGID), ''); 00858 $this->assertEqual(clean_param('*', PARAM_STRINGID), ''); 00859 $this->assertEqual(clean_param(' ', PARAM_STRINGID), ''); 00860 } 00861 00862 function test_clean_param_timezone() { 00863 // Test timezone validation 00864 $testvalues = array ( 00865 'America/Jamaica' => 'America/Jamaica', 00866 'America/Argentina/Cordoba' => 'America/Argentina/Cordoba', 00867 'America/Port-au-Prince' => 'America/Port-au-Prince', 00868 'America/Argentina/Buenos_Aires' => 'America/Argentina/Buenos_Aires', 00869 'PST8PDT' => 'PST8PDT', 00870 'Wrong.Value' => '', 00871 'Wrong/.Value' => '', 00872 'Wrong(Value)' => '', 00873 '0' => '0', 00874 '0.0' => '0.0', 00875 '0.5' => '0.5', 00876 '-12.5' => '-12.5', 00877 '+12.5' => '+12.5', 00878 '13.5' => '', 00879 '-13.5' => '', 00880 '0.2' => ''); 00881 00882 foreach ($testvalues as $testvalue => $expectedvalue) { 00883 $actualvalue = clean_param($testvalue, PARAM_TIMEZONE); 00884 $this->assertEqual($actualvalue, $expectedvalue); 00885 } 00886 } 00887 00888 function test_validate_param() { 00889 try { 00890 $param = validate_param('11a', PARAM_INT); 00891 $this->fail('invalid_parameter_exception expected'); 00892 } catch (invalid_parameter_exception $ex) { 00893 $this->assertTrue(true); 00894 } 00895 try { 00896 $param = validate_param('11', PARAM_INT); 00897 $this->assertEqual($param, 11); 00898 } catch (invalid_parameter_exception $ex) { 00899 $this->fail('invalid_parameter_exception not expected'); 00900 } 00901 try { 00902 $param = validate_param(null, PARAM_INT, false); 00903 $this->fail('invalid_parameter_exception expected'); 00904 } catch (invalid_parameter_exception $ex) { 00905 $this->assertTrue(true); 00906 } 00907 try { 00908 $param = validate_param(null, PARAM_INT, true); 00909 $this->assertTrue($param===null); 00910 } catch (invalid_parameter_exception $ex) { 00911 $this->fail('invalid_parameter_exception expected'); 00912 } 00913 try { 00914 $param = validate_param(array(), PARAM_INT); 00915 $this->fail('invalid_parameter_exception expected'); 00916 } catch (invalid_parameter_exception $ex) { 00917 $this->assertTrue(true); 00918 } 00919 try { 00920 $param = validate_param(new stdClass, PARAM_INT); 00921 $this->fail('invalid_parameter_exception expected'); 00922 } catch (invalid_parameter_exception $ex) { 00923 $this->assertTrue(true); 00924 } 00925 } 00926 00927 function test_shorten_text() { 00928 $text = "short text already no tags"; 00929 $this->assertEqual($text, shorten_text($text)); 00930 00931 $text = "<p>short <b>text</b> already</p><p>with tags</p>"; 00932 $this->assertEqual($text, shorten_text($text)); 00933 00934 $text = "long text without any tags blah de blah blah blah what"; 00935 $this->assertEqual('long text without any tags ...', shorten_text($text)); 00936 00937 $text = "<div class='frog'><p><blockquote>Long text with tags that will ". 00938 "be chopped off but <b>should be added back again</b></blockquote></p></div>"; 00939 $this->assertEqual("<div class='frog'><p><blockquote>Long text with " . 00940 "tags that ...</blockquote></p></div>", shorten_text($text)); 00941 00942 $text = "some text which shouldn't break there"; 00943 $this->assertEqual("some text which shouldn't ...", 00944 shorten_text($text, 31)); 00945 $this->assertEqual("some text which shouldn't ...", 00946 shorten_text($text, 30)); 00947 00948 // This case caused a bug up to 1.9.5 00949 $text = "<h3>standard 'break-out' sub groups in TGs?</h3> <<There are several"; 00950 $this->assertEqual("<h3>standard 'break-out' sub groups in ...</h3>", 00951 shorten_text($text, 43)); 00952 00953 $text = "<h1>123456789</h1>";//a string with no convenient breaks 00954 $this->assertEqual("<h1>12345...</h1>", 00955 shorten_text($text, 8)); 00956 } 00957 00958 function test_usergetdate() { 00959 global $USER, $CFG; 00960 00961 //Check if forcetimezone is set then save it and set it to use user timezone 00962 $cfgforcetimezone = null; 00963 if (isset($CFG->forcetimezone)) { 00964 $cfgforcetimezone = $CFG->forcetimezone; 00965 $CFG->forcetimezone = 99; //get user default timezone. 00966 } 00967 00968 $userstimezone = $USER->timezone; 00969 $USER->timezone = 2;//set the timezone to a known state 00970 00971 // The string version of date comes from server locale setting and does 00972 // not respect user language, so it is necessary to reset that. 00973 $oldlocale = setlocale(LC_TIME, '0'); 00974 setlocale(LC_TIME, 'en_AU.UTF-8'); 00975 00976 $ts = 1261540267; //the time this function was created 00977 00978 $arr = usergetdate($ts,1);//specify the timezone as an argument 00979 $arr = array_values($arr); 00980 00981 list($seconds,$minutes,$hours,$mday,$wday,$mon,$year,$yday,$weekday,$month) = $arr; 00982 $this->assertEqual($seconds,7); 00983 $this->assertEqual($minutes,51); 00984 $this->assertEqual($hours,4); 00985 $this->assertEqual($mday,23); 00986 $this->assertEqual($wday,3); 00987 $this->assertEqual($mon,12); 00988 $this->assertEqual($year,2009); 00989 $this->assertEqual($yday,357); 00990 $this->assertEqual($weekday, 'Wednesday'); 00991 $this->assertEqual($month, 'December'); 00992 00993 $arr = usergetdate($ts);//gets the timezone from the $USER object 00994 $arr = array_values($arr); 00995 00996 list($seconds,$minutes,$hours,$mday,$wday,$mon,$year,$yday,$weekday,$month) = $arr; 00997 $this->assertEqual($seconds,7); 00998 $this->assertEqual($minutes,51); 00999 $this->assertEqual($hours,5); 01000 $this->assertEqual($mday,23); 01001 $this->assertEqual($wday,3); 01002 $this->assertEqual($mon,12); 01003 $this->assertEqual($year,2009); 01004 $this->assertEqual($yday,357); 01005 $this->assertEqual($weekday, 'Wednesday'); 01006 $this->assertEqual($month, 'December'); 01007 01008 //set the timezone back to what it was 01009 $USER->timezone = $userstimezone; 01010 01011 //restore forcetimezone if changed. 01012 if (!is_null($cfgforcetimezone)) { 01013 $CFG->forcetimezone = $cfgforcetimezone; 01014 } 01015 01016 setlocale(LC_TIME, $oldlocale); 01017 } 01018 01019 public function test_normalize_component() { 01020 01021 // moodle core 01022 $this->assertEqual(normalize_component('moodle'), array('core', null)); 01023 $this->assertEqual(normalize_component('core'), array('core', null)); 01024 01025 // moodle core subsystems 01026 $this->assertEqual(normalize_component('admin'), array('core', 'admin')); 01027 $this->assertEqual(normalize_component('core_admin'), array('core', 'admin')); 01028 01029 // activity modules and their subplugins 01030 $this->assertEqual(normalize_component('workshop'), array('mod', 'workshop')); 01031 $this->assertEqual(normalize_component('mod_workshop'), array('mod', 'workshop')); 01032 $this->assertEqual(normalize_component('workshopform_accumulative'), array('workshopform', 'accumulative')); 01033 $this->assertEqual(normalize_component('quiz'), array('mod', 'quiz')); 01034 $this->assertEqual(normalize_component('quiz_grading'), array('quiz', 'grading')); 01035 $this->assertEqual(normalize_component('data'), array('mod', 'data')); 01036 $this->assertEqual(normalize_component('datafield_checkbox'), array('datafield', 'checkbox')); 01037 01038 // other plugin types 01039 $this->assertEqual(normalize_component('auth_mnet'), array('auth', 'mnet')); 01040 $this->assertEqual(normalize_component('enrol_self'), array('enrol', 'self')); 01041 $this->assertEqual(normalize_component('block_html'), array('block', 'html')); 01042 $this->assertEqual(normalize_component('block_mnet_hosts'), array('block', 'mnet_hosts')); 01043 $this->assertEqual(normalize_component('local_amos'), array('local', 'amos')); 01044 01045 // unknown components are supposed to be activity modules 01046 $this->assertEqual(normalize_component('whothefuckwouldcomewithsuchastupidnameofcomponent'), 01047 array('mod', 'whothefuckwouldcomewithsuchastupidnameofcomponent')); 01048 $this->assertEqual(normalize_component('whothefuck_wouldcomewithsuchastupidnameofcomponent'), 01049 array('mod', 'whothefuck_wouldcomewithsuchastupidnameofcomponent')); 01050 $this->assertEqual(normalize_component('whothefuck_would_come_withsuchastupidnameofcomponent'), 01051 array('mod', 'whothefuck_would_come_withsuchastupidnameofcomponent')); 01052 } 01053 01054 protected function get_fake_preference_test_userid() { 01055 global $DB; 01056 01057 // we need some nonexistent user id 01058 $id = 2147483647 - 666; 01059 if ($DB->get_records('user', array('id'=>$id))) { 01060 //weird! 01061 return false; 01062 } 01063 return $id; 01064 } 01065 01066 public function test_mark_user_preferences_changed() { 01067 if (!$otheruserid = $this->get_fake_preference_test_userid()) { 01068 $this->fail('Can not find unused user id for the preferences test'); 01069 return; 01070 } 01071 01072 set_cache_flag('userpreferenceschanged', $otheruserid, NULL); 01073 mark_user_preferences_changed($otheruserid); 01074 01075 $this->assertEqual(get_cache_flag('userpreferenceschanged', $otheruserid, time()-10), 1); 01076 set_cache_flag('userpreferenceschanged', $otheruserid, NULL); 01077 } 01078 01079 public function test_check_user_preferences_loaded() { 01080 global $DB; 01081 01082 if (!$otheruserid = $this->get_fake_preference_test_userid()) { 01083 $this->fail('Can not find unused user id for the preferences test'); 01084 return; 01085 } 01086 01087 $DB->delete_records('user_preferences', array('userid'=>$otheruserid)); 01088 set_cache_flag('userpreferenceschanged', $otheruserid, NULL); 01089 01090 $user = new stdClass(); 01091 $user->id = $otheruserid; 01092 01093 // load 01094 check_user_preferences_loaded($user); 01095 $this->assertTrue(isset($user->preference)); 01096 $this->assertTrue(is_array($user->preference)); 01097 $this->assertTrue(isset($user->preference['_lastloaded'])); 01098 $this->assertEqual(count($user->preference), 1); 01099 01100 // add preference via direct call 01101 $DB->insert_record('user_preferences', array('name'=>'xxx', 'value'=>'yyy', 'userid'=>$user->id)); 01102 01103 // no cache reload yet 01104 check_user_preferences_loaded($user); 01105 $this->assertEqual(count($user->preference), 1); 01106 01107 // forced reloading of cache 01108 unset($user->preference); 01109 check_user_preferences_loaded($user); 01110 $this->assertEqual(count($user->preference), 2); 01111 $this->assertEqual($user->preference['xxx'], 'yyy'); 01112 01113 // add preference via direct call 01114 $DB->insert_record('user_preferences', array('name'=>'aaa', 'value'=>'bbb', 'userid'=>$user->id)); 01115 01116 // test timeouts and modifications from different session 01117 set_cache_flag('userpreferenceschanged', $user->id, 1, time() + 1000); 01118 $user->preference['_lastloaded'] = $user->preference['_lastloaded'] - 20; 01119 check_user_preferences_loaded($user); 01120 $this->assertEqual(count($user->preference), 2); 01121 check_user_preferences_loaded($user, 10); 01122 $this->assertEqual(count($user->preference), 3); 01123 $this->assertEqual($user->preference['aaa'], 'bbb'); 01124 set_cache_flag('userpreferenceschanged', $user->id, null); 01125 } 01126 01127 public function test_set_user_preference() { 01128 global $DB, $USER; 01129 01130 if (!$otheruserid = $this->get_fake_preference_test_userid()) { 01131 $this->fail('Can not find unused user id for the preferences test'); 01132 return; 01133 } 01134 01135 $DB->delete_records('user_preferences', array('userid'=>$otheruserid)); 01136 set_cache_flag('userpreferenceschanged', $otheruserid, null); 01137 01138 $user = new stdClass(); 01139 $user->id = $otheruserid; 01140 01141 set_user_preference('aaa', 'bbb', $otheruserid); 01142 $this->assertEqual('bbb', $DB->get_field('user_preferences', 'value', array('userid'=>$otheruserid, 'name'=>'aaa'))); 01143 $this->assertEqual('bbb', get_user_preferences('aaa', null, $otheruserid)); 01144 01145 set_user_preference('xxx', 'yyy', $user); 01146 $this->assertEqual('yyy', $DB->get_field('user_preferences', 'value', array('userid'=>$otheruserid, 'name'=>'xxx'))); 01147 $this->assertEqual('yyy', get_user_preferences('xxx', null, $otheruserid)); 01148 $this->assertTrue(is_array($user->preference)); 01149 $this->assertEqual($user->preference['aaa'], 'bbb'); 01150 $this->assertEqual($user->preference['xxx'], 'yyy'); 01151 01152 set_user_preference('xxx', NULL, $user); 01153 $this->assertIdentical(false, $DB->get_field('user_preferences', 'value', array('userid'=>$otheruserid, 'name'=>'xxx'))); 01154 $this->assertIdentical(null, get_user_preferences('xxx', null, $otheruserid)); 01155 01156 set_user_preference('ooo', true, $user); 01157 $prefs = get_user_preferences(null, null, $otheruserid); 01158 $this->assertIdentical($prefs['aaa'], $user->preference['aaa']); 01159 $this->assertIdentical($prefs['ooo'], $user->preference['ooo']); 01160 $this->assertIdentical($prefs['ooo'], '1'); 01161 01162 set_user_preference('null', 0, $user); 01163 $this->assertIdentical('0', get_user_preferences('null', null, $otheruserid)); 01164 01165 $this->assertIdentical('lala', get_user_preferences('undefined', 'lala', $otheruserid)); 01166 01167 $DB->delete_records('user_preferences', array('userid'=>$otheruserid)); 01168 set_cache_flag('userpreferenceschanged', $otheruserid, null); 01169 01170 // test $USER default 01171 set_user_preference('_test_user_preferences_pref', 'ok'); 01172 $this->assertIdentical('ok', $USER->preference['_test_user_preferences_pref']); 01173 unset_user_preference('_test_user_preferences_pref'); 01174 $this->assertTrue(!isset($USER->preference['_test_user_preferences_pref'])); 01175 01176 // Test 1333 char values (no need for unicode, there are already tests for that in DB tests) 01177 $longvalue = str_repeat('a', 1333); 01178 set_user_preference('_test_long_user_preference', $longvalue); 01179 $this->assertEqual($longvalue, get_user_preferences('_test_long_user_preference')); 01180 $this->assertEqual($longvalue, 01181 $DB->get_field('user_preferences', 'value', array('userid' => $USER->id, 'name' => '_test_long_user_preference'))); 01182 01183 // Test > 1333 char values, coding_exception expected 01184 $longvalue = str_repeat('a', 1334); 01185 try { 01186 set_user_preference('_test_long_user_preference', $longvalue); 01187 $this->assertFail('Exception expected - longer than 1333 chars not allowed as preference value'); 01188 } catch (Exception $e) { 01189 $this->assertTrue($e instanceof coding_exception); 01190 } 01191 01192 //test invalid params 01193 try { 01194 set_user_preference('_test_user_preferences_pref', array()); 01195 $this->assertFail('Exception expected - array not valid preference value'); 01196 } catch (Exception $ex) { 01197 $this->assertTrue(true); 01198 } 01199 try { 01200 set_user_preference('_test_user_preferences_pref', new stdClass); 01201 $this->assertFail('Exception expected - class not valid preference value'); 01202 } catch (Exception $ex) { 01203 $this->assertTrue(true); 01204 } 01205 try { 01206 set_user_preference('_test_user_preferences_pref', 1, array('xx'=>1)); 01207 $this->assertFail('Exception expected - user instance expected'); 01208 } catch (Exception $ex) { 01209 $this->assertTrue(true); 01210 } 01211 try { 01212 set_user_preference('_test_user_preferences_pref', 1, 'abc'); 01213 $this->assertFail('Exception expected - user instance expected'); 01214 } catch (Exception $ex) { 01215 $this->assertTrue(true); 01216 } 01217 try { 01218 set_user_preference('', 1); 01219 $this->assertFail('Exception expected - invalid name accepted'); 01220 } catch (Exception $ex) { 01221 $this->assertTrue(true); 01222 } 01223 try { 01224 set_user_preference('1', 1); 01225 $this->assertFail('Exception expected - invalid name accepted'); 01226 } catch (Exception $ex) { 01227 $this->assertTrue(true); 01228 } 01229 } 01230 01231 public function test_get_extra_user_fields() { 01232 global $CFG; 01233 $oldshowuseridentity = $CFG->showuseridentity; 01234 01235 // It would be really nice if there were a way to 'mock' has_capability 01236 // checks (either to return true or false) but as there is not, this 01237 // test doesn't test the capability check. Presumably, anyone running 01238 // unit tests will have the capability. 01239 $context = context_system::instance(); 01240 01241 // No fields 01242 $CFG->showuseridentity = ''; 01243 $this->assertEqual(array(), get_extra_user_fields($context)); 01244 01245 // One field 01246 $CFG->showuseridentity = 'frog'; 01247 $this->assertEqual(array('frog'), get_extra_user_fields($context)); 01248 01249 // Two fields 01250 $CFG->showuseridentity = 'frog,zombie'; 01251 $this->assertEqual(array('frog', 'zombie'), get_extra_user_fields($context)); 01252 01253 // No fields, except 01254 $CFG->showuseridentity = ''; 01255 $this->assertEqual(array(), get_extra_user_fields($context, array('frog'))); 01256 01257 // One field 01258 $CFG->showuseridentity = 'frog'; 01259 $this->assertEqual(array(), get_extra_user_fields($context, array('frog'))); 01260 01261 // Two fields 01262 $CFG->showuseridentity = 'frog,zombie'; 01263 $this->assertEqual(array('zombie'), get_extra_user_fields($context, array('frog'))); 01264 01265 // As long as this test passes, the value will be set back. This is only 01266 // in-memory anyhow 01267 $CFG->showuseridentity = $oldshowuseridentity; 01268 } 01269 01270 public function test_get_extra_user_fields_sql() { 01271 global $CFG; 01272 $oldshowuseridentity = $CFG->showuseridentity; 01273 $context = context_system::instance(); 01274 01275 // No fields 01276 $CFG->showuseridentity = ''; 01277 $this->assertEqual('', get_extra_user_fields_sql($context)); 01278 01279 // One field 01280 $CFG->showuseridentity = 'frog'; 01281 $this->assertEqual(', frog', get_extra_user_fields_sql($context)); 01282 01283 // Two fields with table prefix 01284 $CFG->showuseridentity = 'frog,zombie'; 01285 $this->assertEqual(', u1.frog, u1.zombie', get_extra_user_fields_sql($context, 'u1')); 01286 01287 // Two fields with field prefix 01288 $CFG->showuseridentity = 'frog,zombie'; 01289 $this->assertEqual(', frog AS u_frog, zombie AS u_zombie', 01290 get_extra_user_fields_sql($context, '', 'u_')); 01291 01292 // One field excluded 01293 $CFG->showuseridentity = 'frog'; 01294 $this->assertEqual('', get_extra_user_fields_sql($context, '', '', array('frog'))); 01295 01296 // Two fields, one excluded, table+field prefix 01297 $CFG->showuseridentity = 'frog,zombie'; 01298 $this->assertEqual(', u1.zombie AS u_zombie', 01299 get_extra_user_fields_sql($context, 'u1', 'u_', array('frog'))); 01300 01301 // As long as this test passes, the value will be set back. This is only 01302 // in-memory anyhow 01303 $CFG->showuseridentity = $oldshowuseridentity; 01304 } 01305 01306 public function test_userdate() { 01307 global $USER, $CFG; 01308 01309 $testvalues = array( 01310 array( 01311 'time' => '1309514400', 01312 'usertimezone' => 'America/Moncton', 01313 'timezone' => '0.0', //no dst offset 01314 'expectedoutput' => 'Friday, 1 July 2011, 10:00 AM' 01315 ), 01316 array( 01317 'time' => '1309514400', 01318 'usertimezone' => 'America/Moncton', 01319 'timezone' => '99', //dst offset and timezone offset. 01320 'expectedoutput' => 'Friday, 1 July 2011, 07:00 AM' 01321 ), 01322 array( 01323 'time' => '1309514400', 01324 'usertimezone' => 'America/Moncton', 01325 'timezone' => 'America/Moncton', //dst offset and timezone offset. 01326 'expectedoutput' => 'Friday, 1 July 2011, 07:00 AM' 01327 ), 01328 array( 01329 'time' => '1293876000 ', 01330 'usertimezone' => 'America/Moncton', 01331 'timezone' => '0.0', //no dst offset 01332 'expectedoutput' => 'Saturday, 1 January 2011, 10:00 AM' 01333 ), 01334 array( 01335 'time' => '1293876000 ', 01336 'usertimezone' => 'America/Moncton', 01337 'timezone' => '99', //no dst offset in jan, so just timezone offset. 01338 'expectedoutput' => 'Saturday, 1 January 2011, 06:00 AM' 01339 ), 01340 array( 01341 'time' => '1293876000 ', 01342 'usertimezone' => 'America/Moncton', 01343 'timezone' => 'America/Moncton', //no dst offset in jan 01344 'expectedoutput' => 'Saturday, 1 January 2011, 06:00 AM' 01345 ), 01346 array( 01347 'time' => '1293876000 ', 01348 'usertimezone' => '2', 01349 'timezone' => '99', //take user timezone 01350 'expectedoutput' => 'Saturday, 1 January 2011, 12:00 PM' 01351 ), 01352 array( 01353 'time' => '1293876000 ', 01354 'usertimezone' => '-2', 01355 'timezone' => '99', //take user timezone 01356 'expectedoutput' => 'Saturday, 1 January 2011, 08:00 AM' 01357 ), 01358 array( 01359 'time' => '1293876000 ', 01360 'usertimezone' => '-10', 01361 'timezone' => '2', //take this timezone 01362 'expectedoutput' => 'Saturday, 1 January 2011, 12:00 PM' 01363 ), 01364 array( 01365 'time' => '1293876000 ', 01366 'usertimezone' => '-10', 01367 'timezone' => '-2', //take this timezone 01368 'expectedoutput' => 'Saturday, 1 January 2011, 08:00 AM' 01369 ), 01370 array( 01371 'time' => '1293876000 ', 01372 'usertimezone' => '-10', 01373 'timezone' => 'random/time', //this should show server time 01374 'expectedoutput' => 'Saturday, 1 January 2011, 06:00 PM' 01375 ), 01376 array( 01377 'time' => '1293876000 ', 01378 'usertimezone' => '14', //server time zone 01379 'timezone' => '99', //this should show user time 01380 'expectedoutput' => 'Saturday, 1 January 2011, 06:00 PM' 01381 ), 01382 ); 01383 01384 //Check if forcetimezone is set then save it and set it to use user timezone 01385 $cfgforcetimezone = null; 01386 if (isset($CFG->forcetimezone)) { 01387 $cfgforcetimezone = $CFG->forcetimezone; 01388 $CFG->forcetimezone = 99; //get user default timezone. 01389 } 01390 //store user default timezone to restore later 01391 $userstimezone = $USER->timezone; 01392 01393 // The string version of date comes from server locale setting and does 01394 // not respect user language, so it is necessary to reset that. 01395 $oldlocale = setlocale(LC_TIME, '0'); 01396 setlocale(LC_TIME, 'en_AU.UTF-8'); 01397 01398 //set default timezone to Australia/Perth, else time calulated 01399 //will not match expected values. Before that save system defaults. 01400 $systemdefaulttimezone = date_default_timezone_get(); 01401 date_default_timezone_set('Australia/Perth'); 01402 01403 //get instance of textlib for strtolower 01404 $textlib = textlib_get_instance(); 01405 foreach ($testvalues as $vals) { 01406 $USER->timezone = $vals['usertimezone']; 01407 $actualoutput = userdate($vals['time'], '%A, %d %B %Y, %I:%M %p', $vals['timezone']); 01408 01409 //On different systems case of AM PM changes so compare case insenitive 01410 $vals['expectedoutput'] = $textlib->strtolower($vals['expectedoutput']); 01411 $actualoutput = $textlib->strtolower($actualoutput); 01412 01413 $this->assertEqual($vals['expectedoutput'], $actualoutput, 01414 "Expected: {$vals['expectedoutput']} => Actual: {$actualoutput}, 01415 Please check if timezones are updated (Site adminstration -> location -> update timezone)"); 01416 } 01417 01418 //restore user timezone back to what it was 01419 $USER->timezone = $userstimezone; 01420 01421 //restore forcetimezone 01422 if (!is_null($cfgforcetimezone)) { 01423 $CFG->forcetimezone = $cfgforcetimezone; 01424 } 01425 01426 //restore system default values. 01427 date_default_timezone_set($systemdefaulttimezone); 01428 setlocale(LC_TIME, $oldlocale); 01429 } 01430 01431 public function test_make_timestamp() { 01432 global $USER, $CFG; 01433 01434 $testvalues = array( 01435 array( 01436 'usertimezone' => 'America/Moncton', 01437 'year' => '2011', 01438 'month' => '7', 01439 'day' => '1', 01440 'hour' => '10', 01441 'minutes' => '00', 01442 'seconds' => '00', 01443 'timezone' => '0.0', //no dst offset 01444 'applydst' => false, 01445 'expectedoutput' => '1309528800' 01446 ), 01447 array( 01448 'usertimezone' => 'America/Moncton', 01449 'year' => '2011', 01450 'month' => '7', 01451 'day' => '1', 01452 'hour' => '10', 01453 'minutes' => '00', 01454 'seconds' => '00', 01455 'timezone' => '99', //user default timezone 01456 'applydst' => false, //don't apply dst 01457 'expectedoutput' => '1309528800' 01458 ), 01459 array( 01460 'usertimezone' => 'America/Moncton', 01461 'year' => '2011', 01462 'month' => '7', 01463 'day' => '1', 01464 'hour' => '10', 01465 'minutes' => '00', 01466 'seconds' => '00', 01467 'timezone' => '99', //user default timezone 01468 'applydst' => true, //apply dst 01469 'expectedoutput' => '1309525200' 01470 ), 01471 array( 01472 'usertimezone' => 'America/Moncton', 01473 'year' => '2011', 01474 'month' => '7', 01475 'day' => '1', 01476 'hour' => '10', 01477 'minutes' => '00', 01478 'seconds' => '00', 01479 'timezone' => 'America/Moncton', //string timezone 01480 'applydst' => true, //apply dst 01481 'expectedoutput' => '1309525200' 01482 ), 01483 array( 01484 'usertimezone' => '2',//no dst applyed 01485 'year' => '2011', 01486 'month' => '7', 01487 'day' => '1', 01488 'hour' => '10', 01489 'minutes' => '00', 01490 'seconds' => '00', 01491 'timezone' => '99', //take user timezone 01492 'applydst' => true, //apply dst 01493 'expectedoutput' => '1309507200' 01494 ), 01495 array( 01496 'usertimezone' => '-2',//no dst applyed 01497 'year' => '2011', 01498 'month' => '7', 01499 'day' => '1', 01500 'hour' => '10', 01501 'minutes' => '00', 01502 'seconds' => '00', 01503 'timezone' => '99', //take usertimezone 01504 'applydst' => true, //apply dst 01505 'expectedoutput' => '1309521600' 01506 ), 01507 array( 01508 'usertimezone' => '-10',//no dst applyed 01509 'year' => '2011', 01510 'month' => '7', 01511 'day' => '1', 01512 'hour' => '10', 01513 'minutes' => '00', 01514 'seconds' => '00', 01515 'timezone' => '2', //take this timezone 01516 'applydst' => true, //apply dst 01517 'expectedoutput' => '1309507200' 01518 ), 01519 array( 01520 'usertimezone' => '-10',//no dst applyed 01521 'year' => '2011', 01522 'month' => '7', 01523 'day' => '1', 01524 'hour' => '10', 01525 'minutes' => '00', 01526 'seconds' => '00', 01527 'timezone' => '-2', //take this timezone 01528 'applydst' => true, //apply dst, 01529 'expectedoutput' => '1309521600' 01530 ), 01531 array( 01532 'usertimezone' => '-10',//no dst applyed 01533 'year' => '2011', 01534 'month' => '7', 01535 'day' => '1', 01536 'hour' => '10', 01537 'minutes' => '00', 01538 'seconds' => '00', 01539 'timezone' => 'random/time', //This should show server time 01540 'applydst' => true, //apply dst, 01541 'expectedoutput' => '1309485600' 01542 ), 01543 array( 01544 'usertimezone' => '14',//server time 01545 'year' => '2011', 01546 'month' => '7', 01547 'day' => '1', 01548 'hour' => '10', 01549 'minutes' => '00', 01550 'seconds' => '00', 01551 'timezone' => '99', //get user time 01552 'applydst' => true, //apply dst, 01553 'expectedoutput' => '1309485600' 01554 ) 01555 ); 01556 01557 //Check if forcetimezone is set then save it and set it to use user timezone 01558 $cfgforcetimezone = null; 01559 if (isset($CFG->forcetimezone)) { 01560 $cfgforcetimezone = $CFG->forcetimezone; 01561 $CFG->forcetimezone = 99; //get user default timezone. 01562 } 01563 01564 //store user default timezone to restore later 01565 $userstimezone = $USER->timezone; 01566 01567 // The string version of date comes from server locale setting and does 01568 // not respect user language, so it is necessary to reset that. 01569 $oldlocale = setlocale(LC_TIME, '0'); 01570 setlocale(LC_TIME, 'en_AU.UTF-8'); 01571 01572 //set default timezone to Australia/Perth, else time calulated 01573 //will not match expected values. Before that save system defaults. 01574 $systemdefaulttimezone = date_default_timezone_get(); 01575 date_default_timezone_set('Australia/Perth'); 01576 01577 //get instance of textlib for strtolower 01578 $textlib = textlib_get_instance(); 01579 //Test make_timestamp with all testvals and assert if anything wrong. 01580 foreach ($testvalues as $vals) { 01581 $USER->timezone = $vals['usertimezone']; 01582 $actualoutput = make_timestamp( 01583 $vals['year'], 01584 $vals['month'], 01585 $vals['day'], 01586 $vals['hour'], 01587 $vals['minutes'], 01588 $vals['seconds'], 01589 $vals['timezone'], 01590 $vals['applydst'] 01591 ); 01592 01593 //On different systems case of AM PM changes so compare case insenitive 01594 $vals['expectedoutput'] = $textlib->strtolower($vals['expectedoutput']); 01595 $actualoutput = $textlib->strtolower($actualoutput); 01596 01597 $this->assertEqual($vals['expectedoutput'], $actualoutput, 01598 "Expected: {$vals['expectedoutput']} => Actual: {$actualoutput}, 01599 Please check if timezones are updated (Site adminstration -> location -> update timezone)"); 01600 } 01601 01602 //restore user timezone back to what it was 01603 $USER->timezone = $userstimezone; 01604 01605 //restore forcetimezone 01606 if (!is_null($cfgforcetimezone)) { 01607 $CFG->forcetimezone = $cfgforcetimezone; 01608 } 01609 01610 //restore system default values. 01611 date_default_timezone_set($systemdefaulttimezone); 01612 setlocale(LC_TIME, $oldlocale); 01613 } 01614 }