libdir . '/moodlelib.php'); class moodlelib_test extends UnitTestCase { public static $includecoverage = array('lib/moodlelib.php'); var $user_agents = array( 'MSIE' => array( '5.0' => array('Windows 98' => 'Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)'), '5.5' => array('Windows 2000' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)'), '6.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'), '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)'), '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)'), '9.0' => array('Windows 7' => 'Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))'), ), 'Firefox' => array( '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'), '1.5' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8) Gecko/20051107 Firefox/1.5'), '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'), '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', 'Ubuntu Linux AMD64' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20060601 Firefox/2.0 (Ubuntu-edgy)'), '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'), ), 'Safari' => array( '312' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312'), '412' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412') ), 'Safari iOS' => array( '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'), '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'), ), 'WebKit Android' => array( '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'), '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'), ), 'Chrome' => array( '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'), ), 'Opera' => array( '8.51' => array('Windows XP' => 'Opera/8.51 (Windows NT 5.1; U; en)'), '9.0' => array('Windows XP' => 'Opera/9.0 (Windows NT 5.1; U; en)', 'Debian Linux' => 'Opera/9.01 (X11; Linux i686; U; en)') ) ); function test_cleanremoteaddr() { //IPv4 $this->assertEqual(cleanremoteaddr('1023.121.234.1'), null); $this->assertEqual(cleanremoteaddr('123.121.234.01 '), '123.121.234.1'); //IPv6 $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:0:0'), null); $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:abh'), null); $this->assertEqual(cleanremoteaddr('0:0:0:::0:0:1'), null); $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:0', true), '::'); $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:1:1', true), '::1:1'); $this->assertEqual(cleanremoteaddr('abcd:00ef:0:0:0:0:0:0', true), 'abcd:ef::'); $this->assertEqual(cleanremoteaddr('1:0:0:0:0:0:0:1', true), '1::1'); $this->assertEqual(cleanremoteaddr('::10:1', false), '0:0:0:0:0:0:10:1'); $this->assertEqual(cleanremoteaddr('01:1::', false), '1:1:0:0:0:0:0:0'); $this->assertEqual(cleanremoteaddr('10::10', false), '10:0:0:0:0:0:0:10'); $this->assertEqual(cleanremoteaddr('::ffff:192.168.1.1', true), '::ffff:c0a8:11'); } function test_address_in_subnet() { /// 1: xxx.xxx.xxx.xxx/nn or xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/nnn (number of bits in net mask) $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.1/32')); $this->assertFalse(address_in_subnet('123.121.23.1', '123.121.23.0/32')); $this->assertTrue(address_in_subnet('10.10.10.100', '123.121.23.45/0')); $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.0/24')); $this->assertFalse(address_in_subnet('123.121.34.1', '123.121.234.0/24')); $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.0/30')); $this->assertFalse(address_in_subnet('123.121.23.8', '123.121.23.0/30')); $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba/128')); $this->assertFalse(address_in_subnet('bab:baba::baba', 'bab:baba::cece/128')); $this->assertTrue(address_in_subnet('baba:baba::baba', 'cece:cece::cece/0')); $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba/128')); $this->assertTrue(address_in_subnet('baba:baba::00ba', 'baba:baba::/120')); $this->assertFalse(address_in_subnet('baba:baba::aba', 'baba:baba::/120')); $this->assertTrue(address_in_subnet('baba::baba:00ba', 'baba::baba:0/112')); $this->assertFalse(address_in_subnet('baba::aba:00ba', 'baba::baba:0/112')); $this->assertFalse(address_in_subnet('aba::baba:0000', 'baba::baba:0/112')); // fixed input $this->assertTrue(address_in_subnet('123.121.23.1 ', ' 123.121.23.0 / 24')); $this->assertTrue(address_in_subnet('::ffff:10.1.1.1', ' 0:0:0:000:0:ffff:a1:10 / 126')); // incorrect input $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/-2')); $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/64')); $this->assertFalse(address_in_subnet('123.121.234.x', '123.121.234.1/24')); $this->assertFalse(address_in_subnet('123.121.234.0', '123.121.234.xx/24')); $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/xx0')); $this->assertFalse(address_in_subnet('::1', '::aa:0/xx0')); $this->assertFalse(address_in_subnet('::1', '::aa:0/-5')); $this->assertFalse(address_in_subnet('::1', '::aa:0/130')); $this->assertFalse(address_in_subnet('x:1', '::aa:0/130')); $this->assertFalse(address_in_subnet('::1', '::ax:0/130')); /// 2: xxx.xxx.xxx.xxx-yyy or xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx::xxxx-yyyy (a range of IP addresses in the last group) $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12-14')); $this->assertTrue(address_in_subnet('123.121.234.13', '123.121.234.12-14')); $this->assertTrue(address_in_subnet('123.121.234.14', '123.121.234.12-14')); $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.12-14')); $this->assertFalse(address_in_subnet('123.121.234.20', '123.121.234.12-14')); $this->assertFalse(address_in_subnet('123.121.23.12', '123.121.234.12-14')); $this->assertFalse(address_in_subnet('123.12.234.12', '123.121.234.12-14')); $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba-babe')); $this->assertTrue(address_in_subnet('baba:baba::babc', 'baba:baba::baba-babe')); $this->assertTrue(address_in_subnet('baba:baba::babe', 'baba:baba::baba-babe')); $this->assertFalse(address_in_subnet('bab:baba::bab0', 'bab:baba::baba-babe')); $this->assertFalse(address_in_subnet('bab:baba::babf', 'bab:baba::baba-babe')); $this->assertFalse(address_in_subnet('bab:baba::bfbe', 'bab:baba::baba-babe')); $this->assertFalse(address_in_subnet('bfb:baba::babe', 'bab:baba::baba-babe')); // fixed input $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12 - 14 ')); $this->assertTrue(address_in_subnet('bab:baba::babe', 'bab:baba::baba - babe ')); // incorrect input $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12-234.14')); $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12-256')); $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12--256')); /// 3: xxx.xxx or xxx.xxx. or xxx:xxx:xxxx or xxx:xxx:xxxx. (incomplete address, a bit non-technical ;-) $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12')); $this->assertFalse(address_in_subnet('123.121.23.12', '123.121.23.13')); $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.')); $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234')); $this->assertTrue(address_in_subnet('123.121.234.12', '123.121')); $this->assertTrue(address_in_subnet('123.121.234.12', '123')); $this->assertFalse(address_in_subnet('123.121.234.1', '12.121.234.')); $this->assertFalse(address_in_subnet('123.121.234.1', '12.121.234')); $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:baba::bab')); $this->assertFalse(address_in_subnet('baba:baba::ba', 'baba:baba::bc')); $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:baba')); $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:')); $this->assertFalse(address_in_subnet('bab:baba::bab', 'baba:')); /// multiple subnets $this->assertTrue(address_in_subnet('123.121.234.12', '::1/64, 124., 123.121.234.10-30')); $this->assertTrue(address_in_subnet('124.121.234.12', '::1/64, 124., 123.121.234.10-30')); $this->assertTrue(address_in_subnet('::2', '::1/64, 124., 123.121.234.10-30')); $this->assertFalse(address_in_subnet('12.121.234.12', '::1/64, 124., 123.121.234.10-30')); /// other incorrect input $this->assertFalse(address_in_subnet('123.123.123.123', '')); } /** * Modifies $_SERVER['HTTP_USER_AGENT'] manually to check if check_browser_version * works as expected. */ function test_check_browser_version() { global $CFG; $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['412']['Mac OS X']; $this->assertTrue(check_browser_version('Safari')); $this->assertTrue(check_browser_version('WebKit')); $this->assertTrue(check_browser_version('Safari', '312')); $this->assertFalse(check_browser_version('Safari', '500')); $this->assertFalse(check_browser_version('Chrome')); $this->assertFalse(check_browser_version('Safari iOS')); $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari iOS']['528']['iPhone']; $this->assertTrue(check_browser_version('Safari iOS')); $this->assertTrue(check_browser_version('WebKit')); $this->assertTrue(check_browser_version('Safari iOS', '527')); $this->assertFalse(check_browser_version('Safari iOS', 590)); $this->assertFalse(check_browser_version('Safari', '312')); $this->assertFalse(check_browser_version('Safari', '500')); $this->assertFalse(check_browser_version('Chrome')); $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['WebKit Android']['530']['Nexus']; $this->assertTrue(check_browser_version('WebKit')); $this->assertTrue(check_browser_version('WebKit Android', '527')); $this->assertFalse(check_browser_version('WebKit Android', 590)); $this->assertFalse(check_browser_version('Safari')); $this->assertFalse(check_browser_version('Chrome')); $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Chrome']['8']['Mac OS X']; $this->assertTrue(check_browser_version('Chrome')); $this->assertTrue(check_browser_version('WebKit')); $this->assertTrue(check_browser_version('Chrome', 8)); $this->assertFalse(check_browser_version('Chrome', 10)); $this->assertFalse(check_browser_version('Safari', '1')); $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP']; $this->assertTrue(check_browser_version('Opera')); $this->assertTrue(check_browser_version('Opera', '8.0')); $this->assertFalse(check_browser_version('Opera', '10.0')); $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2']; $this->assertTrue(check_browser_version('MSIE')); $this->assertTrue(check_browser_version('MSIE', '5.0')); $this->assertFalse(check_browser_version('MSIE', '7.0')); $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['5.0']['Windows 98']; $this->assertFalse(check_browser_version('MSIE')); $this->assertTrue(check_browser_version('MSIE', 0)); $this->assertTrue(check_browser_version('MSIE', '5.0')); $this->assertFalse(check_browser_version('MSIE', '7.0')); $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['9.0']['Windows 7']; $this->assertTrue(check_browser_version('MSIE')); $this->assertTrue(check_browser_version('MSIE', 0)); $this->assertTrue(check_browser_version('MSIE', '5.0')); $this->assertTrue(check_browser_version('MSIE', '9.0')); $this->assertFalse(check_browser_version('MSIE', '10')); $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP']; $this->assertTrue(check_browser_version('Firefox')); $this->assertTrue(check_browser_version('Firefox', '1.5')); $this->assertFalse(check_browser_version('Firefox', '3.0')); } function test_get_browser_version_classes() { $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['412']['Mac OS X']; $this->assertEqual(array('safari'), get_browser_version_classes()); $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Chrome']['8']['Mac OS X']; $this->assertEqual(array('safari'), get_browser_version_classes()); $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari iOS']['528']['iPhone']; $this->assertEqual(array('safari', 'ios'), get_browser_version_classes()); $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['WebKit Android']['530']['Nexus']; $this->assertEqual(array('safari', 'android'), get_browser_version_classes()); $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Chrome']['8']['Mac OS X']; $this->assertEqual(array('safari'), get_browser_version_classes()); $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP']; $this->assertEqual(array('opera'), get_browser_version_classes()); $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2']; $this->assertEqual(array('ie', 'ie6'), get_browser_version_classes()); $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['7.0']['Windows XP SP2']; $this->assertEqual(array('ie', 'ie7'), get_browser_version_classes()); $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['8.0']['Windows Vista']; $this->assertEqual(array('ie', 'ie8'), get_browser_version_classes()); $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP']; $this->assertEqual(array('gecko', 'gecko18'), get_browser_version_classes()); $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['3.0.6']['SUSE']; $this->assertEqual(array('gecko', 'gecko19'), get_browser_version_classes()); } function test_get_device_type() { // IE8 (common pattern ~1.5% of IE7/8 users have embedded IE6 agent)) $_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)'; $this->assertEqual('default', get_device_type()); // Genuine IE6 $_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)'; $this->assertEqual('legacy', get_device_type()); } function test_fix_utf8() { // make sure valid data including other types is not changed $this->assertidentical(null, fix_utf8(null)); $this->assertidentical(1, fix_utf8(1)); $this->assertidentical(1.1, fix_utf8(1.1)); $this->assertidentical(true, fix_utf8(true)); $this->assertidentical('abc', fix_utf8('abc')); $this->assertidentical('', fix_utf8('')); $array = array('do', 're', 'mi'); $this->assertidentical($array, fix_utf8($array)); $object = new stdClass(); $object->a = 'aa'; $object->b = 'bb'; $this->assertidentical($object, fix_utf8($object)); // valid utf8 string $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")); // invalid utf8 string $this->assertidentical('aš', fix_utf8('a'.chr(130).'š'), 'This fails with buggy iconv() when mbstring extenstion is not available as fallback.'); } function test_optional_param() { global $CFG; $_POST['username'] = 'post_user'; $_GET['username'] = 'get_user'; $this->assertIdentical(optional_param('username', 'default_user', PARAM_RAW), $_POST['username']); unset($_POST['username']); $this->assertIdentical(optional_param('username', 'default_user', PARAM_RAW), $_GET['username']); unset($_GET['username']); $this->assertIdentical(optional_param('username', 'default_user', PARAM_RAW), 'default_user'); // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2 $_POST['username'] = 'post_user'; try { optional_param('username', 'default_user', null); $this->fail('coding_exception expected'); } catch (coding_exception $ex) { $this->assertTrue(true); } try { @optional_param('username', 'default_user'); $this->fail('coding_exception expected'); } catch (coding_exception $ex) { $this->assertTrue(true); } try { @optional_param('username'); $this->fail('coding_exception expected'); } catch (coding_exception $ex) { $this->assertTrue(true); } try { optional_param('', 'default_user', PARAM_RAW); $this->fail('coding_exception expected'); } catch (coding_exception $ex) { $this->assertTrue(true); } // make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3 $debugging = isset($CFG->debug) ? $CFG->debug : null; $debugdisplay = isset($CFG->debugdisplay) ? $CFG->debugdisplay : null; $CFG->debug = 38911; $CFG->debugdisplay = true; ob_start(); $this->assertIdentical(optional_param('username', 'default_user', PARAM_RAW), $_POST['username']); $d = ob_end_clean(); $this->assertTrue($d !== ''); if ($debugging !== null) { $CFG->debug = $debugging; } else { unset($CFG->debug); } if ($debugdisplay !== null) { $CFG->debugdisplay = $debugdisplay; } else { unset($CFG->debugdisplay); } } function test_optional_param_array() { global $CFG; $_POST['username'] = array('a'=>'post_user'); $_GET['username'] = array('a'=>'get_user'); $this->assertIdentical(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), $_POST['username']); unset($_POST['username']); $this->assertIdentical(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), $_GET['username']); unset($_GET['username']); $this->assertIdentical(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), array('a'=>'default_user')); // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2 $_POST['username'] = array('a'=>'post_user'); try { optional_param_array('username', array('a'=>'default_user'), null); $this->fail('coding_exception expected'); } catch (coding_exception $ex) { $this->assertTrue(true); } try { @optional_param_array('username', array('a'=>'default_user')); $this->fail('coding_exception expected'); } catch (coding_exception $ex) { $this->assertTrue(true); } try { @optional_param_array('username'); $this->fail('coding_exception expected'); } catch (coding_exception $ex) { $this->assertTrue(true); } try { optional_param_array('', array('a'=>'default_user'), PARAM_RAW); $this->fail('coding_exception expected'); } catch (coding_exception $ex) { $this->assertTrue(true); } // do not allow nested arrays try { $_POST['username'] = array('a'=>array('b'=>'post_user')); optional_param_array('username', array('a'=>'default_user'), PARAM_RAW); $this->fail('coding_exception expected'); } catch (coding_exception $ex) { $this->assertTrue(true); } // do not allow non-arrays $debugging = isset($CFG->debug) ? $CFG->debug : null; $debugdisplay = isset($CFG->debugdisplay) ? $CFG->debugdisplay : null; $CFG->debug = 38911; $CFG->debugdisplay = true; ob_start(); $_POST['username'] = 'post_user'; $this->assertIdentical(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), array('a'=>'default_user')); $d = ob_end_clean(); $this->assertTrue($d !== ''); // make sure array keys are sanitised ob_start(); $_POST['username'] = array('abc123_;-/*-+ '=>'arrggh', 'a1_-'=>'post_user'); $this->assertIdentical(optional_param_array('username', array(), PARAM_RAW), array('a1_-'=>'post_user')); $d = ob_end_clean(); $this->assertTrue($d !== ''); if ($debugging !== null) { $CFG->debug = $debugging; } else { unset($CFG->debug); } if ($debugdisplay !== null) { $CFG->debugdisplay = $debugdisplay; } else { unset($CFG->debugdisplay); } } function test_required_param() { global $CFG; $_POST['username'] = 'post_user'; $_GET['username'] = 'get_user'; $this->assertIdentical(required_param('username', PARAM_RAW), 'post_user'); unset($_POST['username']); $this->assertIdentical(required_param('username', PARAM_RAW), 'get_user'); unset($_GET['username']); try { $this->assertIdentical(required_param('username', PARAM_RAW), 'default_user'); $this->fail('moodle_exception expected'); } catch (moodle_exception $ex) { $this->assertTrue(true); } // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2 $_POST['username'] = 'post_user'; try { @required_param('username'); $this->fail('coding_exception expected'); } catch (coding_exception $ex) { $this->assertTrue(true); } try { required_param('username', ''); $this->fail('coding_exception expected'); } catch (coding_exception $ex) { $this->assertTrue(true); } try { required_param('', PARAM_RAW); $this->fail('coding_exception expected'); } catch (coding_exception $ex) { $this->assertTrue(true); } // make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3 $debugging = isset($CFG->debug) ? $CFG->debug : null; $debugdisplay = isset($CFG->debugdisplay) ? $CFG->debugdisplay : null; $CFG->debug = 38911; $CFG->debugdisplay = true; ob_start(); $this->assertIdentical(required_param('username', PARAM_RAW), $_POST['username']); $d = ob_end_clean(); $this->assertTrue($d !== ''); if ($debugging !== null) { $CFG->debug = $debugging; } else { unset($CFG->debug); } if ($debugdisplay !== null) { $CFG->debugdisplay = $debugdisplay; } else { unset($CFG->debugdisplay); } } function test_required_param_array() { global $CFG; $_POST['username'] = array('a'=>'post_user'); $_GET['username'] = array('a'=>'get_user'); $this->assertIdentical(required_param_array('username', PARAM_RAW), $_POST['username']); unset($_POST['username']); $this->assertIdentical(required_param_array('username', PARAM_RAW), $_GET['username']); // make sure exception is triggered when some params are missing, hide error notices here - new in 2.2 $_POST['username'] = array('a'=>'post_user'); try { required_param_array('username', null); $this->fail('coding_exception expected'); } catch (coding_exception $ex) { $this->assertTrue(true); } try { @required_param_array('username'); $this->fail('coding_exception expected'); } catch (coding_exception $ex) { $this->assertTrue(true); } try { required_param_array('', PARAM_RAW); $this->fail('coding_exception expected'); } catch (coding_exception $ex) { $this->assertTrue(true); } // do not allow nested arrays try { $_POST['username'] = array('a'=>array('b'=>'post_user')); required_param_array('username', PARAM_RAW); $this->fail('coding_exception expected'); } catch (coding_exception $ex) { $this->assertTrue(true); } // do not allow non-arrays try { $_POST['username'] = 'post_user'; required_param_array('username', PARAM_RAW); $this->fail('moodle_exception expected'); } catch (moodle_exception $ex) { $this->assertTrue(true); } // do not allow non-arrays $debugging = isset($CFG->debug) ? $CFG->debug : null; $debugdisplay = isset($CFG->debugdisplay) ? $CFG->debugdisplay : null; $CFG->debug = 38911; $CFG->debugdisplay = true; // make sure array keys are sanitised ob_start(); $_POST['username'] = array('abc123_;-/*-+ '=>'arrggh', 'a1_-'=>'post_user'); $this->assertIdentical(required_param_array('username', PARAM_RAW), array('a1_-'=>'post_user')); $d = ob_end_clean(); $this->assertTrue($d !== ''); if ($debugging !== null) { $CFG->debug = $debugging; } else { unset($CFG->debug); } if ($debugdisplay !== null) { $CFG->debugdisplay = $debugdisplay; } else { unset($CFG->debugdisplay); } } function test_clean_param() { // forbid objects and arrays try { clean_param(array('x', 'y'), PARAM_RAW); $this->fail('coding_exception expected'); } catch (coding_exception $ex) { $this->assertTrue(true); } try { $param = new stdClass(); $param->id = 1; clean_param($param, PARAM_RAW); $this->fail('coding_exception expected'); } catch (coding_exception $ex) { $this->assertTrue(true); } // require correct type try { clean_param('x', 'xxxxxx'); $this->fail('moodle_exception expected'); } catch (moodle_exception $ex) { $this->assertTrue(true); } try { @clean_param('x'); $this->fail('moodle_exception expected'); } catch (moodle_exception $ex) { $this->assertTrue(true); } } function test_clean_param_array() { $this->assertIdentical(clean_param_array(null, PARAM_RAW), array()); $this->assertIdentical(clean_param_array(array('a', 'b'), PARAM_RAW), array('a', 'b')); $this->assertIdentical(clean_param_array(array('a', array('b')), PARAM_RAW, true), array('a', array('b'))); // require correct type try { clean_param_array(array('x'), 'xxxxxx'); $this->fail('moodle_exception expected'); } catch (moodle_exception $ex) { $this->assertTrue(true); } try { @clean_param_array(array('x')); $this->fail('moodle_exception expected'); } catch (moodle_exception $ex) { $this->assertTrue(true); } try { clean_param_array(array('x', array('y')), PARAM_RAW); $this->fail('coding_exception expected'); } catch (coding_exception $ex) { $this->assertTrue(true); } // test recursive } function test_clean_param_raw() { $this->assertEqual(clean_param('#()*#,9789\'".,<42897>assertEqual(clean_param(" Frog toad \r\n ", PARAM_RAW_TRIMMED), 'Frog toad'); } function test_clean_param_clean() { // PARAM_CLEAN is an ugly hack, do not use in new code (skodak) // instead use more specific type, or submit sothing that can be verified properly $this->assertEqual(clean_param('xx