/lib/tests/moodlelib_test.php
PHP | 4690 lines | 3410 code | 542 blank | 738 comment | 18 complexity | 7f786953036f6286f8245824fb854600 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0, Apache-2.0
Large files files are truncated, but you can click here to view the full file
- <?php
- // This file is part of Moodle - http://moodle.org/
- //
- // Moodle is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // Moodle is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
- /**
- * Unit tests for (some of) ../moodlelib.php.
- *
- * @package core
- * @category phpunit
- * @copyright © 2006 The Open University
- * @author T.J.Hunt@open.ac.uk
- * @author nicolas@moodle.com
- */
- defined('MOODLE_INTERNAL') || die();
- class core_moodlelib_testcase extends advanced_testcase {
- public static $includecoverage = array('lib/moodlelib.php');
- /**
- * Define a local decimal separator.
- *
- * It is not possible to directly change the result of get_string in
- * a unit test. Instead, we create a language pack for language 'xx' in
- * dataroot and make langconfig.php with the string we need to change.
- * The default example separator used here is 'X'; on PHP 5.3 and before this
- * must be a single byte character due to PHP bug/limitation in
- * number_format, so you can't use UTF-8 characters.
- *
- * @param string $decsep Separator character. Defaults to `'X'`.
- */
- protected function define_local_decimal_separator(string $decsep = 'X') {
- global $SESSION, $CFG;
- $SESSION->lang = 'xx';
- $langconfig = "<?php\n\$string['decsep'] = '$decsep';";
- $langfolder = $CFG->dataroot . '/lang/xx';
- check_dir_exists($langfolder);
- file_put_contents($langfolder . '/langconfig.php', $langconfig);
- // Ensure the new value is picked up and not taken from the cache.
- $stringmanager = get_string_manager();
- $stringmanager->reset_caches(true);
- }
- public function test_cleanremoteaddr() {
- // IPv4.
- $this->assertNull(cleanremoteaddr('1023.121.234.1'));
- $this->assertSame('123.121.234.1', cleanremoteaddr('123.121.234.01 '));
- // IPv6.
- $this->assertNull(cleanremoteaddr('0:0:0:0:0:0:0:0:0'));
- $this->assertNull(cleanremoteaddr('0:0:0:0:0:0:0:abh'));
- $this->assertNull(cleanremoteaddr('0:0:0:::0:0:1'));
- $this->assertSame('::', cleanremoteaddr('0:0:0:0:0:0:0:0', true));
- $this->assertSame('::1:1', cleanremoteaddr('0:0:0:0:0:0:1:1', true));
- $this->assertSame('abcd:ef::', cleanremoteaddr('abcd:00ef:0:0:0:0:0:0', true));
- $this->assertSame('1::1', cleanremoteaddr('1:0:0:0:0:0:0:1', true));
- $this->assertSame('0:0:0:0:0:0:10:1', cleanremoteaddr('::10:1', false));
- $this->assertSame('1:1:0:0:0:0:0:0', cleanremoteaddr('01:1::', false));
- $this->assertSame('10:0:0:0:0:0:0:10', cleanremoteaddr('10::10', false));
- $this->assertSame('::ffff:c0a8:11', cleanremoteaddr('::ffff:192.168.1.1', true));
- }
- public 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', ''));
- }
- public function test_fix_utf8() {
- // Make sure valid data including other types is not changed.
- $this->assertSame(null, fix_utf8(null));
- $this->assertSame(1, fix_utf8(1));
- $this->assertSame(1.1, fix_utf8(1.1));
- $this->assertSame(true, fix_utf8(true));
- $this->assertSame('', fix_utf8(''));
- $this->assertSame('abc', fix_utf8('abc'));
- $array = array('do', 're', 'mi');
- $this->assertSame($array, fix_utf8($array));
- $object = new stdClass();
- $object->a = 'aa';
- $object->b = 'bb';
- $this->assertEquals($object, fix_utf8($object));
- // valid utf8 string
- $this->assertSame("žlutý koníček přeskočil potůček \n\t\r", fix_utf8("žlutý koníček přeskočil potůček \n\t\r\0"));
- // Invalid utf8 string.
- $this->assertSame('aš', fix_utf8('a'.chr(130).'š'), 'This fails with buggy iconv() when mbstring extenstion is not available as fallback.');
- }
- public function test_optional_param() {
- global $CFG;
- $_POST['username'] = 'post_user';
- $_GET['username'] = 'get_user';
- $this->assertSame($_POST['username'], optional_param('username', 'default_user', PARAM_RAW));
- unset($_POST['username']);
- $this->assertSame($_GET['username'], optional_param('username', 'default_user', PARAM_RAW));
- unset($_GET['username']);
- $this->assertSame('default_user', optional_param('username', 'default_user', PARAM_RAW));
- // 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 (moodle_exception $ex) {
- $this->assertInstanceOf('coding_exception', $ex);
- }
- try {
- @optional_param('username', 'default_user');
- $this->fail('coding_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('coding_exception', $ex);
- } catch (Error $error) {
- // PHP 7.1 throws Error even earlier.
- $this->assertRegExp('/Too few arguments to function/', $error->getMessage());
- }
- try {
- @optional_param('username');
- $this->fail('coding_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('coding_exception', $ex);
- } catch (Error $error) {
- // PHP 7.1 throws Error even earlier.
- $this->assertRegExp('/Too few arguments to function/', $error->getMessage());
- }
- try {
- optional_param('', 'default_user', PARAM_RAW);
- $this->fail('coding_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('coding_exception', $ex);
- }
- // Make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3.
- $_POST['username'] = array('a'=>'a');
- $this->assertSame($_POST['username'], optional_param('username', 'default_user', PARAM_RAW));
- $this->assertDebuggingCalled();
- }
- public function test_optional_param_array() {
- global $CFG;
- $_POST['username'] = array('a'=>'post_user');
- $_GET['username'] = array('a'=>'get_user');
- $this->assertSame($_POST['username'], optional_param_array('username', array('a'=>'default_user'), PARAM_RAW));
- unset($_POST['username']);
- $this->assertSame($_GET['username'], optional_param_array('username', array('a'=>'default_user'), PARAM_RAW));
- unset($_GET['username']);
- $this->assertSame(array('a'=>'default_user'), optional_param_array('username', array('a'=>'default_user'), PARAM_RAW));
- // 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 (moodle_exception $ex) {
- $this->assertInstanceOf('coding_exception', $ex);
- }
- try {
- @optional_param_array('username', array('a'=>'default_user'));
- $this->fail('coding_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('coding_exception', $ex);
- } catch (Error $error) {
- // PHP 7.1 throws Error even earlier.
- $this->assertRegExp('/Too few arguments to function/', $error->getMessage());
- }
- try {
- @optional_param_array('username');
- $this->fail('coding_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('coding_exception', $ex);
- } catch (Error $error) {
- // PHP 7.1 throws Error even earlier.
- $this->assertRegExp('/Too few arguments to function/', $error->getMessage());
- }
- try {
- optional_param_array('', array('a'=>'default_user'), PARAM_RAW);
- $this->fail('coding_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('coding_exception', $ex);
- }
- // 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.
- $_POST['username'] = 'post_user';
- $this->assertSame(array('a'=>'default_user'), optional_param_array('username', array('a'=>'default_user'), PARAM_RAW));
- $this->assertDebuggingCalled();
- // Make sure array keys are sanitised.
- $_POST['username'] = array('abc123_;-/*-+ '=>'arrggh', 'a1_-'=>'post_user');
- $this->assertSame(array('a1_-'=>'post_user'), optional_param_array('username', array(), PARAM_RAW));
- $this->assertDebuggingCalled();
- }
- public function test_required_param() {
- $_POST['username'] = 'post_user';
- $_GET['username'] = 'get_user';
- $this->assertSame('post_user', required_param('username', PARAM_RAW));
- unset($_POST['username']);
- $this->assertSame('get_user', required_param('username', PARAM_RAW));
- unset($_GET['username']);
- try {
- $this->assertSame('default_user', required_param('username', PARAM_RAW));
- $this->fail('moodle_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('moodle_exception', $ex);
- }
- // 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 (moodle_exception $ex) {
- $this->assertInstanceOf('coding_exception', $ex);
- } catch (Error $error) {
- // PHP 7.1 throws Error even earlier.
- $this->assertRegExp('/Too few arguments to function/', $error->getMessage());
- }
- try {
- required_param('username', '');
- $this->fail('coding_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('coding_exception', $ex);
- }
- try {
- required_param('', PARAM_RAW);
- $this->fail('coding_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('coding_exception', $ex);
- }
- // Make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3.
- $_POST['username'] = array('a'=>'a');
- $this->assertSame($_POST['username'], required_param('username', PARAM_RAW));
- $this->assertDebuggingCalled();
- }
- public function test_required_param_array() {
- global $CFG;
- $_POST['username'] = array('a'=>'post_user');
- $_GET['username'] = array('a'=>'get_user');
- $this->assertSame($_POST['username'], required_param_array('username', PARAM_RAW));
- unset($_POST['username']);
- $this->assertSame($_GET['username'], required_param_array('username', PARAM_RAW));
- // 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 (moodle_exception $ex) {
- $this->assertInstanceOf('coding_exception', $ex);
- }
- try {
- @required_param_array('username');
- $this->fail('coding_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('coding_exception', $ex);
- } catch (Error $error) {
- // PHP 7.1 throws Error.
- $this->assertRegExp('/Too few arguments to function/', $error->getMessage());
- }
- try {
- required_param_array('', PARAM_RAW);
- $this->fail('coding_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('coding_exception', $ex);
- }
- // 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 (moodle_exception $ex) {
- $this->assertInstanceOf('coding_exception', $ex);
- }
- // 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->assertInstanceOf('moodle_exception', $ex);
- }
- // Make sure array keys are sanitised.
- $_POST['username'] = array('abc123_;-/*-+ '=>'arrggh', 'a1_-'=>'post_user');
- $this->assertSame(array('a1_-'=>'post_user'), required_param_array('username', PARAM_RAW));
- $this->assertDebuggingCalled();
- }
- public function test_clean_param() {
- // Forbid objects and arrays.
- try {
- clean_param(array('x', 'y'), PARAM_RAW);
- $this->fail('coding_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('coding_exception', $ex);
- }
- try {
- $param = new stdClass();
- $param->id = 1;
- clean_param($param, PARAM_RAW);
- $this->fail('coding_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('coding_exception', $ex);
- }
- // Require correct type.
- try {
- clean_param('x', 'xxxxxx');
- $this->fail('moodle_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('moodle_exception', $ex);
- }
- try {
- @clean_param('x');
- $this->fail('moodle_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('moodle_exception', $ex);
- } catch (Error $error) {
- // PHP 7.1 throws Error even earlier.
- $this->assertRegExp('/Too few arguments to function/', $error->getMessage());
- }
- }
- public function test_clean_param_array() {
- $this->assertSame(array(), clean_param_array(null, PARAM_RAW));
- $this->assertSame(array('a', 'b'), clean_param_array(array('a', 'b'), PARAM_RAW));
- $this->assertSame(array('a', array('b')), clean_param_array(array('a', array('b')), PARAM_RAW, true));
- // Require correct type.
- try {
- clean_param_array(array('x'), 'xxxxxx');
- $this->fail('moodle_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('moodle_exception', $ex);
- }
- try {
- @clean_param_array(array('x'));
- $this->fail('moodle_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('moodle_exception', $ex);
- } catch (Error $error) {
- // PHP 7.1 throws Error even earlier.
- $this->assertRegExp('/Too few arguments to function/', $error->getMessage());
- }
- try {
- clean_param_array(array('x', array('y')), PARAM_RAW);
- $this->fail('coding_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('coding_exception', $ex);
- }
- // Test recursive.
- }
- public function test_clean_param_raw() {
- $this->assertSame(
- '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)',
- clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_RAW));
- }
- public function test_clean_param_trim() {
- $this->assertSame('Frog toad', clean_param(" Frog toad \r\n ", PARAM_RAW_TRIMMED));
- }
- public 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->assertSame('xx', clean_param('xx<script>', PARAM_CLEAN));
- }
- public function test_clean_param_alpha() {
- $this->assertSame('DSFMOSDJ', clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHA));
- }
- public function test_clean_param_alphanum() {
- $this->assertSame('978942897DSFMOSDJ', clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHANUM));
- }
- public function test_clean_param_alphaext() {
- $this->assertSame('DSFMOSDJ', clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHAEXT));
- }
- public function test_clean_param_sequence() {
- $this->assertSame(',9789,42897', clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_SEQUENCE));
- }
- public function test_clean_param_component() {
- // Please note the cleaning of component names is very strict, no guessing here.
- $this->assertSame('mod_forum', clean_param('mod_forum', PARAM_COMPONENT));
- $this->assertSame('block_online_users', clean_param('block_online_users', PARAM_COMPONENT));
- $this->assertSame('block_blond_online_users', clean_param('block_blond_online_users', PARAM_COMPONENT));
- $this->assertSame('mod_something2', clean_param('mod_something2', PARAM_COMPONENT));
- $this->assertSame('forum', clean_param('forum', PARAM_COMPONENT));
- $this->assertSame('user', clean_param('user', PARAM_COMPONENT));
- $this->assertSame('rating', clean_param('rating', PARAM_COMPONENT));
- $this->assertSame('feedback360', clean_param('feedback360', PARAM_COMPONENT));
- $this->assertSame('mod_feedback360', clean_param('mod_feedback360', PARAM_COMPONENT));
- $this->assertSame('', clean_param('mod_2something', PARAM_COMPONENT));
- $this->assertSame('', clean_param('2mod_something', PARAM_COMPONENT));
- $this->assertSame('', clean_param('mod_something_xx', PARAM_COMPONENT));
- $this->assertSame('', clean_param('auth_something__xx', PARAM_COMPONENT));
- $this->assertSame('', clean_param('mod_Something', PARAM_COMPONENT));
- $this->assertSame('', clean_param('mod_somethíng', PARAM_COMPONENT));
- $this->assertSame('', clean_param('mod__something', PARAM_COMPONENT));
- $this->assertSame('', clean_param('auth_xx-yy', PARAM_COMPONENT));
- $this->assertSame('', clean_param('_auth_xx', PARAM_COMPONENT));
- $this->assertSame('a2uth_xx', clean_param('a2uth_xx', PARAM_COMPONENT));
- $this->assertSame('', clean_param('auth_xx_', PARAM_COMPONENT));
- $this->assertSame('', clean_param('auth_xx.old', PARAM_COMPONENT));
- $this->assertSame('', clean_param('_user', PARAM_COMPONENT));
- $this->assertSame('', clean_param('2rating', PARAM_COMPONENT));
- $this->assertSame('', clean_param('user_', PARAM_COMPONENT));
- }
- public function test_clean_param_localisedfloat() {
- $this->assertSame(0.5, clean_param('0.5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(false, clean_param('0X5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(0.5, clean_param('.5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(false, clean_param('X5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(10.5, clean_param('10.5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(false, clean_param('10X5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(1000.5, clean_param('1 000.5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(false, clean_param('1 000X5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(false, clean_param('1.000.5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(false, clean_param('1X000X5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(false, clean_param('nan', PARAM_LOCALISEDFLOAT));
- $this->assertSame(false, clean_param('10.6blah', PARAM_LOCALISEDFLOAT));
- // Tests with a localised decimal separator.
- $this->define_local_decimal_separator();
- $this->assertSame(0.5, clean_param('0.5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(0.5, clean_param('0X5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(0.5, clean_param('.5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(0.5, clean_param('X5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(10.5, clean_param('10.5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(10.5, clean_param('10X5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(1000.5, clean_param('1 000.5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(1000.5, clean_param('1 000X5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(false, clean_param('1.000.5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(false, clean_param('1X000X5', PARAM_LOCALISEDFLOAT));
- $this->assertSame(false, clean_param('nan', PARAM_LOCALISEDFLOAT));
- $this->assertSame(false, clean_param('10X6blah', PARAM_LOCALISEDFLOAT));
- }
- public function test_is_valid_plugin_name() {
- $this->assertTrue(is_valid_plugin_name('forum'));
- $this->assertTrue(is_valid_plugin_name('forum2'));
- $this->assertTrue(is_valid_plugin_name('feedback360'));
- $this->assertTrue(is_valid_plugin_name('online_users'));
- $this->assertTrue(is_valid_plugin_name('blond_online_users'));
- $this->assertFalse(is_valid_plugin_name('online__users'));
- $this->assertFalse(is_valid_plugin_name('forum '));
- $this->assertFalse(is_valid_plugin_name('forum.old'));
- $this->assertFalse(is_valid_plugin_name('xx-yy'));
- $this->assertFalse(is_valid_plugin_name('2xx'));
- $this->assertFalse(is_valid_plugin_name('Xx'));
- $this->assertFalse(is_valid_plugin_name('_xx'));
- $this->assertFalse(is_valid_plugin_name('xx_'));
- }
- public function test_clean_param_plugin() {
- // Please note the cleaning of plugin names is very strict, no guessing here.
- $this->assertSame('forum', clean_param('forum', PARAM_PLUGIN));
- $this->assertSame('forum2', clean_param('forum2', PARAM_PLUGIN));
- $this->assertSame('feedback360', clean_param('feedback360', PARAM_PLUGIN));
- $this->assertSame('online_users', clean_param('online_users', PARAM_PLUGIN));
- $this->assertSame('blond_online_users', clean_param('blond_online_users', PARAM_PLUGIN));
- $this->assertSame('', clean_param('online__users', PARAM_PLUGIN));
- $this->assertSame('', clean_param('forum ', PARAM_PLUGIN));
- $this->assertSame('', clean_param('forum.old', PARAM_PLUGIN));
- $this->assertSame('', clean_param('xx-yy', PARAM_PLUGIN));
- $this->assertSame('', clean_param('2xx', PARAM_PLUGIN));
- $this->assertSame('', clean_param('Xx', PARAM_PLUGIN));
- $this->assertSame('', clean_param('_xx', PARAM_PLUGIN));
- $this->assertSame('', clean_param('xx_', PARAM_PLUGIN));
- }
- public function test_clean_param_area() {
- // Please note the cleaning of area names is very strict, no guessing here.
- $this->assertSame('something', clean_param('something', PARAM_AREA));
- $this->assertSame('something2', clean_param('something2', PARAM_AREA));
- $this->assertSame('some_thing', clean_param('some_thing', PARAM_AREA));
- $this->assertSame('some_thing_xx', clean_param('some_thing_xx', PARAM_AREA));
- $this->assertSame('feedback360', clean_param('feedback360', PARAM_AREA));
- $this->assertSame('', clean_param('_something', PARAM_AREA));
- $this->assertSame('', clean_param('something_', PARAM_AREA));
- $this->assertSame('', clean_param('2something', PARAM_AREA));
- $this->assertSame('', clean_param('Something', PARAM_AREA));
- $this->assertSame('', clean_param('some-thing', PARAM_AREA));
- $this->assertSame('', clean_param('somethííng', PARAM_AREA));
- $this->assertSame('', clean_param('something.x', PARAM_AREA));
- }
- public function test_clean_param_text() {
- $this->assertSame(PARAM_TEXT, PARAM_MULTILANG);
- // Standard.
- $this->assertSame('xx<lang lang="en">aa</lang><lang lang="yy">pp</lang>', clean_param('xx<lang lang="en">aa</lang><lang lang="yy">pp</lang>', PARAM_TEXT));
- $this->assertSame('<span lang="en" class="multilang">aa</span><span lang="xy" class="multilang">bb</span>', clean_param('<span lang="en" class="multilang">aa</span><span lang="xy" class="multilang">bb</span>', PARAM_TEXT));
- $this->assertSame('xx<lang lang="en">aa'."\n".'</lang><lang lang="yy">pp</lang>', clean_param('xx<lang lang="en">aa'."\n".'</lang><lang lang="yy">pp</lang>', PARAM_TEXT));
- // Malformed.
- $this->assertSame('<span lang="en" class="multilang">aa</span>', clean_param('<span lang="en" class="multilang">aa</span>', PARAM_TEXT));
- $this->assertSame('aa', clean_param('<span lang="en" class="nothing" class="multilang">aa</span>', PARAM_TEXT));
- $this->assertSame('aa', clean_param('<lang lang="en" class="multilang">aa</lang>', PARAM_TEXT));
- $this->assertSame('aa', clean_param('<lang lang="en!!">aa</lang>', PARAM_TEXT));
- $this->assertSame('aa', clean_param('<span lang="en==" class="multilang">aa</span>', PARAM_TEXT));
- $this->assertSame('abc', clean_param('a<em>b</em>c', PARAM_TEXT));
- $this->assertSame('a>c>', clean_param('a><xx >c>', PARAM_TEXT)); // Standard strip_tags() behaviour.
- $this->assertSame('a', clean_param('a<b', PARAM_TEXT));
- $this->assertSame('a>b', clean_param('a>b', PARAM_TEXT));
- $this->assertSame('<lang lang="en">a>a</lang>', clean_param('<lang lang="en">a>a</lang>', PARAM_TEXT)); // Standard strip_tags() behaviour.
- $this->assertSame('a', clean_param('<lang lang="en">a<a</lang>', PARAM_TEXT));
- $this->assertSame('<lang lang="en">aa</lang>', clean_param('<lang lang="en">a<br>a</lang>', PARAM_TEXT));
- }
- public function test_clean_param_url() {
- // Test PARAM_URL and PARAM_LOCALURL a bit.
- // Valid URLs.
- $this->assertSame('http://google.com/', clean_param('http://google.com/', PARAM_URL));
- $this->assertSame('http://some.very.long.and.silly.domain/with/a/path/', clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_URL));
- $this->assertSame('http://localhost/', clean_param('http://localhost/', PARAM_URL));
- $this->assertSame('http://0.255.1.1/numericip.php', clean_param('http://0.255.1.1/numericip.php', PARAM_URL));
- $this->assertSame('https://google.com/', clean_param('https://google.com/', PARAM_URL));
- $this->assertSame('https://some.very.long.and.silly.domain/with/a/path/', clean_param('https://some.very.long.and.silly.domain/with/a/path/', PARAM_URL));
- $this->assertSame('https://localhost/', clean_param('https://localhost/', PARAM_URL));
- $this->assertSame('https://0.255.1.1/numericip.php', clean_param('https://0.255.1.1/numericip.php', PARAM_URL));
- $this->assertSame('ftp://ftp.debian.org/debian/', clean_param('ftp://ftp.debian.org/debian/', PARAM_URL));
- $this->assertSame('/just/a/path', clean_param('/just/a/path', PARAM_URL));
- // Invalid URLs.
- $this->assertSame('', clean_param('funny:thing', PARAM_URL));
- $this->assertSame('', clean_param('http://example.ee/sdsf"f', PARAM_URL));
- $this->assertSame('', clean_param('javascript://comment%0Aalert(1)', PARAM_URL));
- $this->assertSame('', clean_param('rtmp://example.com/livestream', PARAM_URL));
- $this->assertSame('', clean_param('rtmp://example.com/live&foo', PARAM_URL));
- $this->assertSame('', clean_param('rtmp://example.com/fms&mp4:path/to/file.mp4', PARAM_URL));
- $this->assertSame('', clean_param('mailto:support@moodle.org', PARAM_URL));
- $this->assertSame('', clean_param('mailto:support@moodle.org?subject=Hello%20Moodle', PARAM_URL));
- $this->assertSame('', clean_param('mailto:support@moodle.org?subject=Hello%20Moodle&cc=feedback@moodle.org', PARAM_URL));
- }
- public function test_clean_param_localurl() {
- global $CFG;
- $this->resetAfterTest();
- // External, invalid.
- $this->assertSame('', clean_param('funny:thing', PARAM_LOCALURL));
- $this->assertSame('', clean_param('http://google.com/', PARAM_LOCALURL));
- $this->assertSame('', clean_param('https://google.com/?test=true', PARAM_LOCALURL));
- $this->assertSame('', clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_LOCALURL));
- // Local absolute.
- $this->assertSame(clean_param($CFG->wwwroot, PARAM_LOCALURL), $CFG->wwwroot);
- $this->assertSame($CFG->wwwroot . '/with/something?else=true',
- clean_param($CFG->wwwroot . '/with/something?else=true', PARAM_LOCALURL));
- // Local relative.
- $this->assertSame('/just/a/path', clean_param('/just/a/path', PARAM_LOCALURL));
- $this->assertSame('course/view.php?id=3', clean_param('course/view.php?id=3', PARAM_LOCALURL));
- // Local absolute HTTPS in a non HTTPS site.
- $CFG->wwwroot = str_replace('https:', 'http:', $CFG->wwwroot); // Need to simulate non-https site.
- $httpsroot = str_replace('http:', 'https:', $CFG->wwwroot);
- $this->assertSame('', clean_param($httpsroot, PARAM_LOCALURL));
- $this->assertSame('', clean_param($httpsroot . '/with/something?else=true', PARAM_LOCALURL));
- // Local absolute HTTPS in a HTTPS site.
- $CFG->wwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
- $httpsroot = $CFG->wwwroot;
- $this->assertSame($httpsroot, clean_param($httpsroot, PARAM_LOCALURL));
- $this->assertSame($httpsroot . '/with/something?else=true',
- clean_param($httpsroot . '/with/something?else=true', PARAM_LOCALURL));
- // Test open redirects are not possible.
- $CFG->wwwroot = 'http://www.example.com';
- $this->assertSame('', clean_param('http://www.example.com.evil.net/hack.php', PARAM_LOCALURL));
- $CFG->wwwroot = 'https://www.example.com';
- $this->assertSame('', clean_param('https://www.example.com.evil.net/hack.php', PARAM_LOCALURL));
- }
- public function test_clean_param_file() {
- $this->assertSame('correctfile.txt', clean_param('correctfile.txt', PARAM_FILE));
- $this->assertSame('badfile.txt', clean_param('b\'a<d`\\/fi:l>e.t"x|t', PARAM_FILE));
- $this->assertSame('..parentdirfile.txt', clean_param('../parentdirfile.txt', PARAM_FILE));
- $this->assertSame('....grandparentdirfile.txt', clean_param('../../grandparentdirfile.txt', PARAM_FILE));
- $this->assertSame('..winparentdirfile.txt', clean_param('..\winparentdirfile.txt', PARAM_FILE));
- $this->assertSame('....wingrandparentdir.txt', clean_param('..\..\wingrandparentdir.txt', PARAM_FILE));
- $this->assertSame('myfile.a.b.txt', clean_param('myfile.a.b.txt', PARAM_FILE));
- $this->assertSame('myfile..a..b.txt', clean_param('myfile..a..b.txt', PARAM_FILE));
- $this->assertSame('myfile.a..b...txt', clean_param('myfile.a..b...txt', PARAM_FILE));
- $this->assertSame('myfile.a.txt', clean_param('myfile.a.txt', PARAM_FILE));
- $this->assertSame('myfile...txt', clean_param('myfile...txt', PARAM_FILE));
- $this->assertSame('...jpg', clean_param('...jpg', PARAM_FILE));
- $this->assertSame('.a.b.', clean_param('.a.b.', PARAM_FILE));
- $this->assertSame('', clean_param('.', PARAM_FILE));
- $this->assertSame('', clean_param('..', PARAM_FILE));
- $this->assertSame('...', clean_param('...', PARAM_FILE));
- $this->assertSame('. . . .', clean_param('. . . .', PARAM_FILE));
- $this->assertSame('dontrtrim.me. .. .. . ', clean_param('dontrtrim.me. .. .. . ', PARAM_FILE));
- $this->assertSame(' . .dontltrim.me', clean_param(' . .dontltrim.me', PARAM_FILE));
- $this->assertSame('here is a tab.txt', clean_param("here is a tab\t.txt", PARAM_FILE));
- $this->assertSame('here is a linebreak.txt', clean_param("here is a line\r\nbreak.txt", PARAM_FILE));
- // The following behaviours have been maintained although they seem a little odd.
- $this->assertSame('funnything', clean_param('funny:thing', PARAM_FILE));
- $this->assertSame('.currentdirfile.txt', clean_param('./currentdirfile.txt', PARAM_FILE));
- $this->assertSame('ctempwindowsfile.txt', clean_param('c:\temp\windowsfile.txt', PARAM_FILE));
- $this->assertSame('homeuserlinuxfile.txt', clean_param('/home/user/linuxfile.txt', PARAM_FILE));
- $this->assertSame('~myfile.txt', clean_param('~/myfile.txt', PARAM_FILE));
- }
- public function test_clean_param_path() {
- $this->assertSame('correctfile.txt', clean_param('correctfile.txt', PARAM_PATH));
- $this->assertSame('bad/file.txt', clean_param('b\'a<d`\\/fi:l>e.t"x|t', PARAM_PATH));
- $this->assertSame('/parentdirfile.txt', clean_param('../parentdirfile.txt', PARAM_PATH));
- $this->assertSame('/grandparentdirfile.txt', clean_param('../../grandparentdirfile.txt', PARAM_PATH));
- $this->assertSame('/winparentdirfile.txt', clean_param('..\winparentdirfile.txt', PARAM_PATH));
- $this->assertSame('/wingrandparentdir.txt', clean_param('..\..\wingrandparentdir.txt', PARAM_PATH));
- $this->assertSame('funnything', clean_param('funny:thing', PARAM_PATH));
- $this->assertSame('./here', clean_param('./././here', PARAM_PATH));
- $this->assertSame('./currentdirfile.txt', clean_param('./currentdirfile.txt', PARAM_PATH));
- $this->assertSame('c/temp/windowsfile.txt', clean_param('c:\temp\windowsfile.txt', PARAM_PATH));
- $this->assertSame('/home/user/linuxfile.txt', clean_param('/home/user/linuxfile.txt', PARAM_PATH));
- $this->assertSame('/home../user ./.linuxfile.txt', clean_param('/home../user ./.linuxfile.txt', PARAM_PATH));
- $this->assertSame('~/myfile.txt', clean_param('~/myfile.txt', PARAM_PATH));
- $this->assertSame('~/myfile.txt', clean_param('~/../myfile.txt', PARAM_PATH));
- $this->assertSame('/..b../.../myfile.txt', clean_param('/..b../.../myfile.txt', PARAM_PATH));
- $this->assertSame('..b../.../myfile.txt', clean_param('..b../.../myfile.txt', PARAM_PATH));
- $this->assertSame('/super/slashes/', clean_param('/super//slashes///', PARAM_PATH));
- }
- public function test_clean_param_username() {
- global $CFG;
- $currentstatus = $CFG->extendedusernamechars;
- // Run tests with extended character == false;.
- $CFG->extendedusernamechars = false;
- $this->assertSame('johndoe123', clean_param('johndoe123', PARAM_USERNAME) );
- $this->assertSame('john.doe', clean_param('john.doe', PARAM_USERNAME));
- $this->assertSame('john-doe', clean_param('john-doe', PARAM_USERNAME));
- $this->assertSame('john-doe', clean_param('john- doe', PARAM_USERNAME));
- $this->assertSame('john_doe', clean_param('john_doe', PARAM_USERNAME));
- $this->assertSame('john@doe', clean_param('john@doe', PARAM_USERNAME));
- $this->assertSame('johndoe', clean_param('john~doe', PARAM_USERNAME));
- $this->assertSame('johndoe', clean_param('john´doe', PARAM_USERNAME));
- $this->assertSame(clean_param('john# $%&()+_^', PARAM_USERNAME), 'john_');
- $this->assertSame(clean_param(' john# $%&()+_^ ', PARAM_USERNAME), 'john_');
- $this->assertSame(clean_param('john#$%&() ', PARAM_USERNAME), 'john');
- $this->assertSame('johnd', clean_param('JOHNdóé ', PARAM_USERNAME));
- $this->assertSame(clean_param('john.,:;-_/|\ñÑ[]A_X-,D {} ~!@#$%^&*()_+ ?><[] ščřžžý ?ýá?ý??doe ', PARAM_USERNAME), 'john.-_a_x-d@_doe');
- // Test success condition, if extendedusernamechars == ENABLE;.
- $CFG->extendedusernamechars = true;
- $this->assertSame('john_doe', clean_param('john_doe', PARAM_USERNAME));
- $this->assertSame('john@doe', clean_param('john@doe', PARAM_USERNAME));
- $this->assertSame(clean_param('john# $%&()+_^', PARAM_USERNAME), 'john# $%&()+_^');
- $this->assertSame(clean_param(' john# $%&()+_^ ', PARAM_USERNAME), 'john# $%&()+_^');
- $this->assertSame('john~doe', clean_param('john~doe', PARAM_USERNAME));
- $this->assertSame('john´doe', clean_param('joHN´doe', PARAM_USERNAME));
- $this->assertSame('johndoe', clean_param('johnDOE', PARAM_USERNAME));
- $this->assertSame('johndóé', clean_param('johndóé ', PARAM_USERNAME));
- $CFG->extendedusernamechars = $currentstatus;
- }
- public function test_clean_param_stringid() {
- // Test string identifiers validation.
- // Valid strings.
- $this->assertSame('validstring', clean_param('validstring', PARAM_STRINGID));
- $this->assertSame('mod/foobar:valid_capability', clean_param('mod/foobar:valid_capability', PARAM_STRINGID));
- $this->assertSame('CZ', clean_param('CZ', PARAM_STRINGID));
- $this->assertSame('application/vnd.ms-powerpoint', clean_param('application/vnd.ms-powerpoint', PARAM_STRINGID));
- $this->assertSame('grade2', clean_param('grade2', PARAM_STRINGID));
- // Invalid strings.
- $this->assertSame('', clean_param('trailing ', PARAM_STRINGID));
- $this->assertSame('', clean_param('space bar', PARAM_STRINGID));
- $this->assertSame('', clean_param('0numeric', PARAM_STRINGID));
- $this->assertSame('', clean_param('*', PARAM_STRINGID));
- $this->assertSame('', clean_param(' ', PARAM_STRINGID));
- }
- public function test_clean_param_timezone() {
- // Test timezone validation.
- $testvalues = array (
- 'America/Jamaica' => 'America/Jamaica',
- 'America/Argentina/Cordoba' => 'America/Argentina/Cordoba',
- 'America/Port-au-Prince' => 'America/Port-au-Prince',
- 'America/Argentina/Buenos_Aires' => 'America/Argentina/Buenos_Aires',
- 'PST8PDT' => 'PST8PDT',
- 'Wrong.Value' => '',
- 'Wrong/.Value' => '',
- 'Wrong(Value)' => '',
- '0' => '0',
- '0.0' => '0.0',
- '0.5' => '0.5',
- '9.0' => '9.0',
- '-9.0' => '-9.0',
- '+9.0' => '+9.0',
- '9.5' => '9.5',
- '-9.5' => '-9.5',
- '+9.5' => '+9.5',
- '12.0' => '12.0',
- '-12.0' => '-12.0',
- '+12.0' => '+12.0',
- '12.5' => '12.5',
- '-12.5' => '-12.5',
- '+12.5' => '+12.5',
- '13.0' => '13.0',
- '-13.0' => '-13.0',
- '+13.0' => '+13.0',
- '13.5' => '',
- '+13.5' => '',
- '-13.5' => '',
- '0.2' => '');
- foreach ($testvalues as $testvalue => $expectedvalue) {
- $actualvalue = clean_param($testvalue, PARAM_TIMEZONE);
- $this->assertEquals($expectedvalue, $actualvalue);
- }
- }
- public function test_validate_param() {
- try {
- $param = validate_param('11a', PARAM_INT);
- $this->fail('invalid_parameter_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('invalid_parameter_exception', $ex);
- }
- $param = validate_param('11', PARAM_INT);
- $this->assertSame(11, $param);
- try {
- $param = validate_param(null, PARAM_INT, false);
- $this->fail('invalid_parameter_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('invalid_parameter_exception', $ex);
- }
- $param = validate_param(null, PARAM_INT, true);
- $this->assertSame(null, $param);
- try {
- $param = validate_param(array(), PARAM_INT);
- $this->fail('invalid_parameter_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('invalid_parameter_exception', $ex);
- }
- try {
- $param = validate_param(new stdClass, PARAM_INT);
- $this->fail('invalid_parameter_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('invalid_parameter_exception', $ex);
- }
- $param = validate_param('1.0', PARAM_FLOAT);
- $this->assertSame(1.0, $param);
- // Make sure valid floats do not cause exception.
- validate_param(1.0, PARAM_FLOAT);
- validate_param(10, PARAM_FLOAT);
- validate_param('0', PARAM_FLOAT);
- validate_param('119813454.545464564564546564545646556564465465456465465465645645465645645645', PARAM_FLOAT);
- validate_param('011.1', PARAM_FLOAT);
- validate_param('11', PARAM_FLOAT);
- validate_param('+.1', PARAM_FLOAT);
- validate_param('-.1', PARAM_FLOAT);
- validate_param('1e10', PARAM_FLOAT);
- validate_param('.1e+10', PARAM_FLOAT);
- validate_param('1E-1', PARAM_FLOAT);
- try {
- $param = validate_param('1,2', PARAM_FLOAT);
- $this->fail('invalid_parameter_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('invalid_parameter_exception', $ex);
- }
- try {
- $param = validate_param('', PARAM_FLOAT);
- $this->fail('invalid_parameter_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('invalid_parameter_exception', $ex);
- }
- try {
- $param = validate_param('.', PARAM_FLOAT);
- $this->fail('invalid_parameter_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('invalid_parameter_exception', $ex);
- }
- try {
- $param = validate_param('e10', PARAM_FLOAT);
- $this->fail('invalid_parameter_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('invalid_parameter_exception', $ex);
- }
- try {
- $param = validate_param('abc', PARAM_FLOAT);
- $this->fail('invalid_parameter_exception expected');
- } catch (moodle_exception $ex) {
- $this->assertInstanceOf('invalid_parameter_exception', $ex);
- }
- }
- public function test_shorten_text_no_tags_already_short_enough() {
- // ......12345678901234567890123456.
- $text = "short text already no tags";
- $this->assertSame($text, shorten_text($text));
- }
- public fun…
Large files files are truncated, but you can click here to view the full file