2012-03-31 23:51:02 +02:00
< ? 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 & copy ; 2006 The Open University
* @ author T . J . Hunt @ open . ac . uk
* @ author nicolas @ moodle . com
*/
defined ( 'MOODLE_INTERNAL' ) || die ();
2013-07-27 21:41:03 +02:00
class core_moodlelib_testcase extends advanced_testcase {
2012-03-31 23:51:02 +02:00
public static $includecoverage = array ( 'lib/moodlelib.php' );
2013-05-09 11:35:38 -04:00
/**
* 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 .
2019-04-08 13:58:30 +02:00
* The default example separator used here is 'X' ; on PHP 5.3 and before this
2013-05-09 11:35:38 -04:00
* must be a single byte character due to PHP bug / limitation in
* number_format , so you can ' t use UTF - 8 characters .
2019-04-08 13:58:30 +02:00
*
* @ param string $decsep Separator character . Defaults to `'X'` .
2013-05-09 11:35:38 -04:00
*/
2019-04-08 13:58:30 +02:00
protected function define_local_decimal_separator ( string $decsep = 'X' ) {
2013-05-09 11:35:38 -04:00
global $SESSION , $CFG ;
$SESSION -> lang = 'xx' ;
2019-04-08 13:58:30 +02:00
$langconfig = " <?php \n \$ string['decsep'] = ' $decsep '; " ;
2013-05-09 11:35:38 -04:00
$langfolder = $CFG -> dataroot . '/lang/xx' ;
check_dir_exists ( $langfolder );
file_put_contents ( $langfolder . '/langconfig.php' , $langconfig );
2019-04-08 13:58:30 +02:00
// Ensure the new value is picked up and not taken from the cache.
$stringmanager = get_string_manager ();
$stringmanager -> reset_caches ( true );
2013-05-09 11:35:38 -04:00
}
2013-07-27 21:41:03 +02:00
public function test_cleanremoteaddr () {
// IPv4.
$this -> assertNull ( cleanremoteaddr ( '1023.121.234.1' ));
$this -> assertSame ( '123.121.234.1' , cleanremoteaddr ( '123.121.234.01 ' ));
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
// 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 ));
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
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).
2012-03-31 23:51:02 +02:00
$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' ));
2013-07-27 21:41:03 +02:00
// Fixed input.
2012-03-31 23:51:02 +02:00
$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' ));
2013-07-27 21:41:03 +02:00
// Incorrect input.
2012-03-31 23:51:02 +02:00
$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' ));
2013-07-27 21:41:03 +02:00
// 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).
2012-03-31 23:51:02 +02:00
$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' ));
2013-07-27 21:41:03 +02:00
// Fixed input.
2012-03-31 23:51:02 +02:00
$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 ' ));
2013-07-27 21:41:03 +02:00
// Incorrect input.
2012-03-31 23:51:02 +02:00
$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' ));
2013-07-27 21:41:03 +02:00
// 3: xxx.xxx or xxx.xxx. or xxx:xxx:xxxx or xxx:xxx:xxxx. (incomplete address, a bit non-technical ;-).
2012-03-31 23:51:02 +02:00
$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:' ));
2013-07-27 21:41:03 +02:00
// Multiple subnets.
2012-03-31 23:51:02 +02:00
$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' ));
2013-07-27 21:41:03 +02:00
// Other incorrect input.
2012-03-31 23:51:02 +02:00
$this -> assertFalse ( address_in_subnet ( '123.123.123.123' , '' ));
}
2013-07-27 21:41:03 +02:00
public function test_fix_utf8 () {
// Make sure valid data including other types is not changed.
2012-03-31 23:51:02 +02:00
$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 ( '' ));
2012-05-15 23:07:10 +02:00
$this -> assertSame ( 'abc' , fix_utf8 ( 'abc' ));
2012-03-31 23:51:02 +02:00
$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 ));
2013-07-24 23:06:53 +02:00
// 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 " ));
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
// Invalid utf8 string.
2012-05-15 23:07:10 +02:00
$this -> assertSame ( 'aš' , fix_utf8 ( 'a' . chr ( 130 ) . 'š' ), 'This fails with buggy iconv() when mbstring extenstion is not available as fallback.' );
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
public function test_optional_param () {
2012-03-31 23:51:02 +02:00
global $CFG ;
$_POST [ 'username' ] = 'post_user' ;
$_GET [ 'username' ] = 'get_user' ;
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $_POST [ 'username' ], optional_param ( 'username' , 'default_user' , PARAM_RAW ));
2012-03-31 23:51:02 +02:00
unset ( $_POST [ 'username' ]);
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $_GET [ 'username' ], optional_param ( 'username' , 'default_user' , PARAM_RAW ));
2012-03-31 23:51:02 +02:00
unset ( $_GET [ 'username' ]);
2013-07-27 21:41:03 +02:00
$this -> assertSame ( 'default_user' , optional_param ( 'username' , 'default_user' , PARAM_RAW ));
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
// Make sure exception is triggered when some params are missing, hide error notices here - new in 2.2.
2012-03-31 23:51:02 +02:00
$_POST [ 'username' ] = 'post_user' ;
try {
optional_param ( 'username' , 'default_user' , null );
$this -> fail ( 'coding_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
try {
@ optional_param ( 'username' , 'default_user' );
$this -> fail ( 'coding_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2016-09-06 10:42:08 +08:00
} catch ( Error $error ) {
// PHP 7.1 throws Error even earlier.
$this -> assertRegExp ( '/Too few arguments to function/' , $error -> getMessage ());
2012-03-31 23:51:02 +02:00
}
try {
@ optional_param ( 'username' );
$this -> fail ( 'coding_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2016-09-06 10:42:08 +08:00
} catch ( Error $error ) {
// PHP 7.1 throws Error even earlier.
$this -> assertRegExp ( '/Too few arguments to function/' , $error -> getMessage ());
2012-03-31 23:51:02 +02:00
}
try {
optional_param ( '' , 'default_user' , PARAM_RAW );
$this -> fail ( 'coding_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
// Make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3.
2012-09-17 11:58:43 +02:00
$_POST [ 'username' ] = array ( 'a' => 'a' );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $_POST [ 'username' ], optional_param ( 'username' , 'default_user' , PARAM_RAW ));
2012-09-17 11:58:43 +02:00
$this -> assertDebuggingCalled ();
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
public function test_optional_param_array () {
2012-03-31 23:51:02 +02:00
global $CFG ;
$_POST [ 'username' ] = array ( 'a' => 'post_user' );
$_GET [ 'username' ] = array ( 'a' => 'get_user' );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $_POST [ 'username' ], optional_param_array ( 'username' , array ( 'a' => 'default_user' ), PARAM_RAW ));
2012-03-31 23:51:02 +02:00
unset ( $_POST [ 'username' ]);
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $_GET [ 'username' ], optional_param_array ( 'username' , array ( 'a' => 'default_user' ), PARAM_RAW ));
2012-03-31 23:51:02 +02:00
unset ( $_GET [ 'username' ]);
2013-07-27 21:41:03 +02:00
$this -> assertSame ( array ( 'a' => 'default_user' ), optional_param_array ( 'username' , array ( 'a' => 'default_user' ), PARAM_RAW ));
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
// Make sure exception is triggered when some params are missing, hide error notices here - new in 2.2.
2012-03-31 23:51:02 +02:00
$_POST [ 'username' ] = array ( 'a' => 'post_user' );
try {
optional_param_array ( 'username' , array ( 'a' => 'default_user' ), null );
$this -> fail ( 'coding_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
try {
@ optional_param_array ( 'username' , array ( 'a' => 'default_user' ));
$this -> fail ( 'coding_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2016-09-06 10:42:08 +08:00
} catch ( Error $error ) {
// PHP 7.1 throws Error even earlier.
$this -> assertRegExp ( '/Too few arguments to function/' , $error -> getMessage ());
2012-03-31 23:51:02 +02:00
}
try {
@ optional_param_array ( 'username' );
$this -> fail ( 'coding_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2016-09-06 10:42:08 +08:00
} catch ( Error $error ) {
// PHP 7.1 throws Error even earlier.
$this -> assertRegExp ( '/Too few arguments to function/' , $error -> getMessage ());
2012-03-31 23:51:02 +02:00
}
try {
optional_param_array ( '' , array ( 'a' => 'default_user' ), PARAM_RAW );
$this -> fail ( 'coding_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
// Do not allow nested arrays.
2012-03-31 23:51:02 +02:00
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 );
}
2013-07-27 21:41:03 +02:00
// Do not allow non-arrays.
2012-03-31 23:51:02 +02:00
$_POST [ 'username' ] = 'post_user' ;
2013-07-27 21:41:03 +02:00
$this -> assertSame ( array ( 'a' => 'default_user' ), optional_param_array ( 'username' , array ( 'a' => 'default_user' ), PARAM_RAW ));
2012-09-17 11:58:43 +02:00
$this -> assertDebuggingCalled ();
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
// Make sure array keys are sanitised.
2012-03-31 23:51:02 +02:00
$_POST [ 'username' ] = array ( 'abc123_;-/*-+ ' => 'arrggh' , 'a1_-' => 'post_user' );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( array ( 'a1_-' => 'post_user' ), optional_param_array ( 'username' , array (), PARAM_RAW ));
2012-09-17 11:58:43 +02:00
$this -> assertDebuggingCalled ();
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
public function test_required_param () {
2012-03-31 23:51:02 +02:00
$_POST [ 'username' ] = 'post_user' ;
$_GET [ 'username' ] = 'get_user' ;
2013-07-27 21:41:03 +02:00
$this -> assertSame ( 'post_user' , required_param ( 'username' , PARAM_RAW ));
2012-03-31 23:51:02 +02:00
unset ( $_POST [ 'username' ]);
2013-07-27 21:41:03 +02:00
$this -> assertSame ( 'get_user' , required_param ( 'username' , PARAM_RAW ));
2012-03-31 23:51:02 +02:00
unset ( $_GET [ 'username' ]);
try {
2013-07-27 21:41:03 +02:00
$this -> assertSame ( 'default_user' , required_param ( 'username' , PARAM_RAW ));
2012-03-31 23:51:02 +02:00
$this -> fail ( 'moodle_exception expected' );
} catch ( moodle_exception $ex ) {
2013-07-27 21:41:03 +02:00
$this -> assertInstanceOf ( 'moodle_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
// Make sure exception is triggered when some params are missing, hide error notices here - new in 2.2.
2012-03-31 23:51:02 +02:00
$_POST [ 'username' ] = 'post_user' ;
try {
@ required_param ( 'username' );
$this -> fail ( 'coding_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2016-09-06 10:42:08 +08:00
} catch ( Error $error ) {
// PHP 7.1 throws Error even earlier.
$this -> assertRegExp ( '/Too few arguments to function/' , $error -> getMessage ());
2012-03-31 23:51:02 +02:00
}
try {
required_param ( 'username' , '' );
$this -> fail ( 'coding_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
try {
required_param ( '' , PARAM_RAW );
$this -> fail ( 'coding_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
// Make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3.
2012-09-17 11:58:43 +02:00
$_POST [ 'username' ] = array ( 'a' => 'a' );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $_POST [ 'username' ], required_param ( 'username' , PARAM_RAW ));
2012-09-17 11:58:43 +02:00
$this -> assertDebuggingCalled ();
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
public function test_required_param_array () {
2012-03-31 23:51:02 +02:00
global $CFG ;
$_POST [ 'username' ] = array ( 'a' => 'post_user' );
$_GET [ 'username' ] = array ( 'a' => 'get_user' );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $_POST [ 'username' ], required_param_array ( 'username' , PARAM_RAW ));
2012-03-31 23:51:02 +02:00
unset ( $_POST [ 'username' ]);
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $_GET [ 'username' ], required_param_array ( 'username' , PARAM_RAW ));
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
// Make sure exception is triggered when some params are missing, hide error notices here - new in 2.2.
2012-03-31 23:51:02 +02:00
$_POST [ 'username' ] = array ( 'a' => 'post_user' );
try {
required_param_array ( 'username' , null );
$this -> fail ( 'coding_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
try {
@ required_param_array ( 'username' );
$this -> fail ( 'coding_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2016-09-06 10:42:08 +08:00
} catch ( Error $error ) {
// PHP 7.1 throws Error.
$this -> assertRegExp ( '/Too few arguments to function/' , $error -> getMessage ());
2012-03-31 23:51:02 +02:00
}
try {
required_param_array ( '' , PARAM_RAW );
$this -> fail ( 'coding_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
// Do not allow nested arrays.
2012-03-31 23:51:02 +02:00
try {
$_POST [ 'username' ] = array ( 'a' => array ( 'b' => 'post_user' ));
required_param_array ( 'username' , PARAM_RAW );
$this -> fail ( 'coding_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
// Do not allow non-arrays.
2012-03-31 23:51:02 +02:00
try {
$_POST [ 'username' ] = 'post_user' ;
required_param_array ( 'username' , PARAM_RAW );
$this -> fail ( 'moodle_exception expected' );
} catch ( moodle_exception $ex ) {
2013-07-27 21:41:03 +02:00
$this -> assertInstanceOf ( 'moodle_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
// Make sure array keys are sanitised.
2012-03-31 23:51:02 +02:00
$_POST [ 'username' ] = array ( 'abc123_;-/*-+ ' => 'arrggh' , 'a1_-' => 'post_user' );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( array ( 'a1_-' => 'post_user' ), required_param_array ( 'username' , PARAM_RAW ));
2012-09-17 11:58:43 +02:00
$this -> assertDebuggingCalled ();
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
public function test_clean_param () {
// Forbid objects and arrays.
2012-03-31 23:51:02 +02:00
try {
clean_param ( array ( 'x' , 'y' ), PARAM_RAW );
$this -> fail ( 'coding_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
try {
$param = new stdClass ();
$param -> id = 1 ;
clean_param ( $param , PARAM_RAW );
$this -> fail ( 'coding_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
// Require correct type.
2012-03-31 23:51:02 +02:00
try {
clean_param ( 'x' , 'xxxxxx' );
$this -> fail ( 'moodle_exception expected' );
} catch ( moodle_exception $ex ) {
2013-07-27 21:41:03 +02:00
$this -> assertInstanceOf ( 'moodle_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
try {
@ clean_param ( 'x' );
$this -> fail ( 'moodle_exception expected' );
} catch ( moodle_exception $ex ) {
2013-07-27 21:41:03 +02:00
$this -> assertInstanceOf ( 'moodle_exception' , $ex );
2016-09-06 10:42:08 +08:00
} catch ( Error $error ) {
// PHP 7.1 throws Error even earlier.
$this -> assertRegExp ( '/Too few arguments to function/' , $error -> getMessage ());
2012-03-31 23:51:02 +02:00
}
}
2013-07-27 21:41:03 +02:00
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 ));
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
// Require correct type.
2012-03-31 23:51:02 +02:00
try {
clean_param_array ( array ( 'x' ), 'xxxxxx' );
$this -> fail ( 'moodle_exception expected' );
} catch ( moodle_exception $ex ) {
2013-07-27 21:41:03 +02:00
$this -> assertInstanceOf ( 'moodle_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
try {
@ clean_param_array ( array ( 'x' ));
$this -> fail ( 'moodle_exception expected' );
} catch ( moodle_exception $ex ) {
2013-07-27 21:41:03 +02:00
$this -> assertInstanceOf ( 'moodle_exception' , $ex );
2016-09-06 10:42:08 +08:00
} catch ( Error $error ) {
// PHP 7.1 throws Error even earlier.
$this -> assertRegExp ( '/Too few arguments to function/' , $error -> getMessage ());
2012-03-31 23:51:02 +02:00
}
try {
clean_param_array ( array ( 'x' , array ( 'y' )), PARAM_RAW );
$this -> fail ( 'coding_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
// Test recursive.
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
public function test_clean_param_raw () {
$this -> assertSame (
'#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)' ,
clean_param ( '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)' , PARAM_RAW ));
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
public function test_clean_param_trim () {
$this -> assertSame ( 'Frog toad' , clean_param ( " Frog toad \r \n " , PARAM_RAW_TRIMMED ));
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
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 ));
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
public function test_clean_param_alpha () {
$this -> assertSame ( 'DSFMOSDJ' , clean_param ( '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)' , PARAM_ALPHA ));
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
public function test_clean_param_alphanum () {
$this -> assertSame ( '978942897DSFMOSDJ' , clean_param ( '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)' , PARAM_ALPHANUM ));
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
public function test_clean_param_alphaext () {
$this -> assertSame ( 'DSFMOSDJ' , clean_param ( '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)' , PARAM_ALPHAEXT ));
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
public function test_clean_param_sequence () {
$this -> assertSame ( ',9789,42897' , clean_param ( '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)' , PARAM_SEQUENCE ));
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
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 ));
2013-10-04 15:56:37 +02:00
$this -> assertSame ( 'feedback360' , clean_param ( 'feedback360' , PARAM_COMPONENT ));
$this -> assertSame ( 'mod_feedback360' , clean_param ( 'mod_feedback360' , PARAM_COMPONENT ));
2013-07-27 21:41:03 +02:00
$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 ));
2013-10-11 00:09:04 +02:00
$this -> assertSame ( '' , clean_param ( 'mod__something' , PARAM_COMPONENT ));
2013-07-27 21:41:03 +02:00
$this -> assertSame ( '' , clean_param ( 'auth_xx-yy' , PARAM_COMPONENT ));
$this -> assertSame ( '' , clean_param ( '_auth_xx' , PARAM_COMPONENT ));
2020-01-21 13:35:37 +01:00
$this -> assertSame ( 'a2uth_xx' , clean_param ( 'a2uth_xx' , PARAM_COMPONENT ));
2013-07-27 21:41:03 +02:00
$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 ));
2012-03-31 23:51:02 +02:00
}
2019-01-23 15:55:04 +11:00
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 ));
}
2013-07-27 21:41:03 +02:00
public function test_is_valid_plugin_name () {
2013-05-01 11:35:14 +01:00
$this -> assertTrue ( is_valid_plugin_name ( 'forum' ));
$this -> assertTrue ( is_valid_plugin_name ( 'forum2' ));
2013-10-04 15:56:37 +02:00
$this -> assertTrue ( is_valid_plugin_name ( 'feedback360' ));
2013-05-01 11:35:14 +01:00
$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_' ));
}
2013-07-27 21:41:03 +02:00
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 ));
2013-10-04 15:56:37 +02:00
$this -> assertSame ( 'feedback360' , clean_param ( 'feedback360' , PARAM_PLUGIN ));
2013-07-27 21:41:03 +02:00
$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 ));
2013-10-04 15:56:37 +02:00
$this -> assertSame ( 'feedback360' , clean_param ( 'feedback360' , PARAM_AREA ));
2013-07-27 21:41:03 +02:00
$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.
2017-06-19 10:12:27 +01:00
// Valid URLs.
2013-07-27 21:41:03 +02:00
$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 ));
2017-06-19 10:12:27 +01:00
$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 ));
2013-07-27 21:41:03 +02:00
$this -> assertSame ( '/just/a/path' , clean_param ( '/just/a/path' , PARAM_URL ));
2017-06-19 10:12:27 +01:00
// Invalid URLs.
2013-07-27 21:41:03 +02:00
$this -> assertSame ( '' , clean_param ( 'funny:thing' , PARAM_URL ));
2017-06-19 10:12:27 +01:00
$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 ));
2017-06-16 16:12:09 +01:00
$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 ));
2013-07-27 21:41:03 +02:00
}
public function test_clean_param_localurl () {
2012-03-31 23:51:02 +02:00
global $CFG ;
2015-06-29 17:12:55 +02:00
$this -> resetAfterTest ();
2015-03-05 09:30:52 +01:00
// External, invalid.
$this -> assertSame ( '' , clean_param ( 'funny:thing' , PARAM_LOCALURL ));
2013-07-27 21:41:03 +02:00
$this -> assertSame ( '' , clean_param ( 'http://google.com/' , PARAM_LOCALURL ));
2015-03-05 09:30:52 +01:00
$this -> assertSame ( '' , clean_param ( 'https://google.com/?test=true' , PARAM_LOCALURL ));
2013-07-27 21:41:03 +02:00
$this -> assertSame ( '' , clean_param ( 'http://some.very.long.and.silly.domain/with/a/path/' , PARAM_LOCALURL ));
2015-03-05 09:30:52 +01:00
// Local absolute.
2013-07-27 21:41:03 +02:00
$this -> assertSame ( clean_param ( $CFG -> wwwroot , PARAM_LOCALURL ), $CFG -> wwwroot );
2015-03-05 09:30:52 +01:00
$this -> assertSame ( $CFG -> wwwroot . '/with/something?else=true' ,
clean_param ( $CFG -> wwwroot . '/with/something?else=true' , PARAM_LOCALURL ));
// Local relative.
2013-07-27 21:41:03 +02:00
$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 ));
2015-03-05 09:30:52 +01:00
2017-04-18 02:08:03 +02:00
// Local absolute HTTPS in a non HTTPS site.
$CFG -> wwwroot = str_replace ( 'https:' , 'http:' , $CFG -> wwwroot ); // Need to simulate non-https site.
2015-03-05 09:30:52 +01:00
$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 ));
2017-04-18 02:08:03 +02:00
// Local absolute HTTPS in a HTTPS site.
2017-09-14 16:13:13 +08:00
$CFG -> wwwroot = str_replace ( 'http:' , 'https:' , $CFG -> wwwroot );
2017-04-18 02:08:03 +02:00
$httpsroot = $CFG -> wwwroot ;
2015-03-05 09:30:52 +01:00
$this -> assertSame ( $httpsroot , clean_param ( $httpsroot , PARAM_LOCALURL ));
$this -> assertSame ( $httpsroot . '/with/something?else=true' ,
clean_param ( $httpsroot . '/with/something?else=true' , PARAM_LOCALURL ));
2015-06-29 17:12:55 +02:00
// 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 ));
2017-09-14 16:13:13 +08:00
$CFG -> wwwroot = 'https://www.example.com' ;
2015-06-29 17:12:55 +02:00
$this -> assertSame ( '' , clean_param ( 'https://www.example.com.evil.net/hack.php' , PARAM_LOCALURL ));
2013-07-27 21:41:03 +02:00
}
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 \n break.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 () {
2012-03-31 23:51:02 +02:00
global $CFG ;
$currentstatus = $CFG -> extendedusernamechars ;
2013-07-27 21:41:03 +02:00
// 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 ));
2014-08-29 02:19:37 +00:00
$this -> assertSame ( clean_param ( 'john# $%&()+_^' , PARAM_USERNAME ), 'john_' );
$this -> assertSame ( clean_param ( ' john# $%&()+_^ ' , PARAM_USERNAME ), 'john_' );
2013-07-27 21:41:03 +02:00
$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 ));
2014-08-29 02:19:37 +00:00
$this -> assertSame ( clean_param ( 'john# $%&()+_^' , PARAM_USERNAME ), 'john# $%&()+_^' );
$this -> assertSame ( clean_param ( ' john# $%&()+_^ ' , PARAM_USERNAME ), 'john# $%&()+_^' );
2013-07-27 21:41:03 +02:00
$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 ));
2012-03-31 23:51:02 +02:00
$CFG -> extendedusernamechars = $currentstatus ;
}
2013-07-27 21:41:03 +02:00
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.
2012-03-31 23:51:02 +02:00
$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' ,
2012-11-30 10:11:55 +08:00
'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' ,
2012-03-31 23:51:02 +02:00
'-12.5' => '-12.5' ,
'+12.5' => '+12.5' ,
2012-11-30 10:11:55 +08:00
'13.0' => '13.0' ,
'-13.0' => '-13.0' ,
'+13.0' => '+13.0' ,
2012-03-31 23:51:02 +02:00
'13.5' => '' ,
2012-11-30 10:11:55 +08:00
'+13.5' => '' ,
2012-03-31 23:51:02 +02:00
'-13.5' => '' ,
'0.2' => '' );
foreach ( $testvalues as $testvalue => $expectedvalue ) {
$actualvalue = clean_param ( $testvalue , PARAM_TIMEZONE );
2013-07-27 21:41:03 +02:00
$this -> assertEquals ( $expectedvalue , $actualvalue );
2012-03-31 23:51:02 +02:00
}
}
2013-07-27 21:41:03 +02:00
public function test_validate_param () {
2012-03-31 23:51:02 +02:00
try {
$param = validate_param ( '11a' , PARAM_INT );
$this -> fail ( 'invalid_parameter_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'invalid_parameter_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
$param = validate_param ( '11' , PARAM_INT );
$this -> assertSame ( 11 , $param );
2012-03-31 23:51:02 +02:00
try {
$param = validate_param ( null , PARAM_INT , false );
$this -> fail ( 'invalid_parameter_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'invalid_parameter_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
$param = validate_param ( null , PARAM_INT , true );
$this -> assertSame ( null , $param );
2012-03-31 23:51:02 +02:00
try {
$param = validate_param ( array (), PARAM_INT );
$this -> fail ( 'invalid_parameter_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'invalid_parameter_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
try {
$param = validate_param ( new stdClass , PARAM_INT );
$this -> fail ( 'invalid_parameter_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'invalid_parameter_exception' , $ex );
2012-07-27 18:42:18 +02:00
}
2013-07-27 21:41:03 +02:00
$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 );
2012-07-27 18:42:18 +02:00
try {
$param = validate_param ( '1,2' , PARAM_FLOAT );
$this -> fail ( 'invalid_parameter_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'invalid_parameter_exception' , $ex );
2012-07-27 18:42:18 +02:00
}
try {
$param = validate_param ( '' , PARAM_FLOAT );
$this -> fail ( 'invalid_parameter_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'invalid_parameter_exception' , $ex );
2012-07-27 18:42:18 +02:00
}
try {
$param = validate_param ( '.' , PARAM_FLOAT );
$this -> fail ( 'invalid_parameter_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'invalid_parameter_exception' , $ex );
2012-07-27 18:42:18 +02:00
}
try {
$param = validate_param ( 'e10' , PARAM_FLOAT );
$this -> fail ( 'invalid_parameter_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'invalid_parameter_exception' , $ex );
2012-07-27 18:42:18 +02:00
}
try {
$param = validate_param ( 'abc' , PARAM_FLOAT );
$this -> fail ( 'invalid_parameter_exception expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'invalid_parameter_exception' , $ex );
2012-07-27 18:42:18 +02:00
}
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
public function test_shorten_text_no_tags_already_short_enough () {
2013-04-22 19:21:40 +01:00
// ......12345678901234567890123456.
2012-03-31 23:51:02 +02:00
$text = " short text already no tags " ;
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $text , shorten_text ( $text ));
2013-04-22 19:21:40 +01:00
}
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
public function test_shorten_text_with_tags_already_short_enough () {
2013-04-22 19:21:40 +01:00
// .........123456...7890....12345678.......901234567.
2012-03-31 23:51:02 +02:00
$text = " <p>short <b>text</b> already</p><p>with tags</p> " ;
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $text , shorten_text ( $text ));
2013-04-22 19:21:40 +01:00
}
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
public function test_shorten_text_no_tags_needs_shortening () {
2013-04-22 19:21:40 +01:00
// Default truncation is after 30 chars, but allowing 3 for the final '...'.
// ......12345678901234567890123456789023456789012345678901234.
2012-03-31 23:51:02 +02:00
$text = " long text without any tags blah de blah blah blah what " ;
2013-07-27 21:41:03 +02:00
$this -> assertSame ( 'long text without any tags ...' , shorten_text ( $text ));
2013-04-22 19:21:40 +01:00
}
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
public function test_shorten_text_with_tags_needs_shortening () {
2013-04-22 19:21:40 +01:00
// .......................................123456789012345678901234567890...
2012-03-31 23:51:02 +02:00
$text = " <div class='frog'><p><blockquote>Long text with tags that will " .
" be chopped off but <b>should be added back again</b></blockquote></p></div> " ;
$this -> assertEquals ( " <div class='frog'><p><blockquote>Long text with " .
" tags that ...</blockquote></p></div> " , shorten_text ( $text ));
2013-04-22 19:21:40 +01:00
}
2012-03-31 23:51:02 +02:00
2015-11-13 17:25:35 -06:00
public function test_shorten_text_with_tags_and_html_comment () {
$text = " <div class='frog'><p><blockquote><!--[if !IE]><!-->Long text with " .
" tags that will<!--<![endif]--> " .
" be chopped off but <b>should be added back again</b></blockquote></p></div> " ;
$this -> assertEquals ( " <div class='frog'><p><blockquote><!--[if !IE]><!-->Long text with " .
" tags that ...<!--<![endif]--></blockquote></p></div> " , shorten_text ( $text ));
}
2013-07-27 21:41:03 +02:00
public function test_shorten_text_with_entities () {
2013-04-22 19:21:40 +01:00
// Remember to allow 3 chars for the final '...'.
// ......123456789012345678901234567_____890...
2012-03-31 23:51:02 +02:00
$text = " some text which shouldn't break there " ;
2013-07-27 21:41:03 +02:00
$this -> assertSame ( " some text which shouldn't ... " , shorten_text ( $text , 31 ));
$this -> assertSame ( " some text which shouldn't ... " , shorten_text ( $text , 30 ));
$this -> assertSame ( " some text which shouldn't ... " , shorten_text ( $text , 29 ));
2013-04-22 19:21:40 +01:00
}
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
public function test_shorten_text_known_tricky_case () {
2012-03-31 23:51:02 +02:00
// This case caused a bug up to 1.9.5
2013-04-22 19:21:40 +01:00
// ..........123456789012345678901234567890123456789.....0_____1___2___...
2012-03-31 23:51:02 +02:00
$text = " <h3>standard 'break-out' sub groups in TGs?</h3> <<There are several " ;
2013-07-27 21:41:03 +02:00
$this -> assertSame ( " <h3>standard 'break-out' sub groups in ...</h3> " ,
2013-04-22 19:21:40 +01:00
shorten_text ( $text , 41 ));
2013-07-27 21:41:03 +02:00
$this -> assertSame ( " <h3>standard 'break-out' sub groups in TGs?...</h3> " ,
2013-04-22 19:21:40 +01:00
shorten_text ( $text , 42 ));
2013-07-27 21:41:03 +02:00
$this -> assertSame ( " <h3>standard 'break-out' sub groups in TGs?</h3> ... " ,
2012-03-31 23:51:02 +02:00
shorten_text ( $text , 43 ));
2013-04-22 19:21:40 +01:00
}
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
public function test_shorten_text_no_spaces () {
2013-04-22 19:21:40 +01:00
// ..........123456789.
$text = " <h1>123456789</h1> " ; // A string with no convenient breaks.
2013-07-27 21:41:03 +02:00
$this -> assertSame ( " <h1>12345...</h1> " , shorten_text ( $text , 8 ));
2013-04-22 19:21:40 +01:00
}
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
public function test_shorten_text_utf8_european () {
2013-04-22 19:21:40 +01:00
// Text without tags.
// ......123456789012345678901234567.
2012-03-31 23:51:02 +02:00
$text = " Žluťoučký koníček přeskočil " ;
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $text , shorten_text ( $text )); // 30 chars by default.
$this -> assertSame ( " Žluťoučký koníče... " , shorten_text ( $text , 19 , true ));
$this -> assertSame ( " Žluťoučký ... " , shorten_text ( $text , 19 , false ));
2013-04-22 19:21:40 +01:00
// And try it with 2-less (that are, in bytes, the middle of a sequence).
2013-07-27 21:41:03 +02:00
$this -> assertSame ( " Žluťoučký koní... " , shorten_text ( $text , 17 , true ));
$this -> assertSame ( " Žluťoučký ... " , shorten_text ( $text , 17 , false ));
2012-03-31 23:51:02 +02:00
2013-04-22 19:21:40 +01:00
// .........123456789012345678...901234567....89012345.
2012-03-31 23:51:02 +02:00
$text = " <p>Žluťoučký koníček <b>přeskočil</b> potůček</p> " ;
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $text , shorten_text ( $text , 60 ));
$this -> assertSame ( " <p>Žluťoučký koníček ...</p> " , shorten_text ( $text , 21 ));
$this -> assertSame ( " <p>Žluťoučký koníče...</p> " , shorten_text ( $text , 19 , true ));
$this -> assertSame ( " <p>Žluťoučký ...</p> " , shorten_text ( $text , 19 , false ));
2013-04-22 19:21:40 +01:00
// And try it with 2 fewer (that are, in bytes, the middle of a sequence).
2013-07-27 21:41:03 +02:00
$this -> assertSame ( " <p>Žluťoučký koní...</p> " , shorten_text ( $text , 17 , true ));
$this -> assertSame ( " <p>Žluťoučký ...</p> " , shorten_text ( $text , 17 , false ));
2013-04-22 19:21:40 +01:00
// And try over one tag (start/end), it does proper text len.
2013-07-27 21:41:03 +02:00
$this -> assertSame ( " <p>Žluťoučký koníček <b>př...</b></p> " , shorten_text ( $text , 23 , true ));
$this -> assertSame ( " <p>Žluťoučký koníček <b>přeskočil</b> pot...</p> " , shorten_text ( $text , 34 , true ));
2013-04-22 19:21:40 +01:00
// And in the middle of one tag.
2013-07-27 21:41:03 +02:00
$this -> assertSame ( " <p>Žluťoučký koníček <b>přeskočil...</b></p> " , shorten_text ( $text , 30 , true ));
2013-04-22 19:21:40 +01:00
}
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
public function test_shorten_text_utf8_oriental () {
2012-03-31 23:51:02 +02:00
// Japanese
2013-04-22 19:21:40 +01:00
// text without tags
// ......123456789012345678901234.
2012-03-31 23:51:02 +02:00
$text = '言語設定言語設定abcdefghijkl' ;
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $text , shorten_text ( $text )); // 30 chars by default.
$this -> assertSame ( " 言語設定言語... " , shorten_text ( $text , 9 , true ));
$this -> assertSame ( " 言語設定言語... " , shorten_text ( $text , 9 , false ));
$this -> assertSame ( " 言語設定言語設定ab... " , shorten_text ( $text , 13 , true ));
$this -> assertSame ( " 言語設定言語設定... " , shorten_text ( $text , 13 , false ));
2012-03-31 23:51:02 +02:00
// Chinese
2013-04-22 19:21:40 +01:00
// text without tags
// ......123456789012345678901234.
2012-03-31 23:51:02 +02:00
$text = '简体中文简体中文abcdefghijkl' ;
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $text , shorten_text ( $text )); // 30 chars by default.
$this -> assertSame ( " 简体中文简体... " , shorten_text ( $text , 9 , true ));
$this -> assertSame ( " 简体中文简体... " , shorten_text ( $text , 9 , false ));
$this -> assertSame ( " 简体中文简体中文ab... " , shorten_text ( $text , 13 , true ));
$this -> assertSame ( " 简体中文简体中文... " , shorten_text ( $text , 13 , false ));
2013-04-22 19:21:40 +01:00
}
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
public function test_shorten_text_multilang () {
2013-04-22 19:21:40 +01:00
// This is not necessaryily specific to multilang. The issue is really
// tags with attributes, where before we were generating invalid HTML
2013-07-27 21:41:03 +02:00
// output like shorten_text('<span id="x" class="y">A</span> B', 1)
2013-04-22 19:21:40 +01:00
// returning '<span id="x" ...</span>'. It is just that multilang
// requires the sort of HTML that is quite likely to trigger this.
// ........................................1...
$text = '<span lang="en" class="multilang">A</span>' .
'<span lang="fr" class="multilang">B</span>' ;
2013-07-27 21:41:03 +02:00
$this -> assertSame ( '<span lang="en" class="multilang">...</span>' ,
2013-04-22 19:21:40 +01:00
shorten_text ( $text , 1 ));
2012-03-31 23:51:02 +02:00
}
2018-05-02 10:45:12 +08:00
/**
* Provider for long filenames and its expected result , with and without hash .
*
* @ return array of ( $filename , $length , $expected , $includehash )
*/
public function shorten_filename_provider () {
2018-03-26 11:14:46 +08:00
$filename = 'sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium totam rem' ;
2018-05-02 10:45:12 +08:00
$shortfilename = 'sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque' ;
return [
'More than 100 characters' => [
$filename ,
null ,
'sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium tot' ,
false ,
],
'More than 100 characters with hash' => [
$filename ,
null ,
'sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque l - 3bec1da8b8' ,
true ,
],
'More than 100 characters with extension' => [
" { $filename } .zip " ,
null ,
'sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium tot.zip' ,
false ,
],
'More than 100 characters with extension and hash' => [
" { $filename } .zip " ,
null ,
'sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque l - 3bec1da8b8.zip' ,
true ,
],
'Limit filename to 50 chars' => [
$filename ,
50 ,
'sed ut perspiciatis unde omnis iste natus error si' ,
false ,
],
'Limit filename to 50 chars with hash' => [
$filename ,
50 ,
'sed ut perspiciatis unde omnis iste n - 3bec1da8b8' ,
true ,
],
'Limit filename to 50 chars with extension' => [
" { $filename } .zip " ,
50 ,
'sed ut perspiciatis unde omnis iste natus error si.zip' ,
false ,
],
'Limit filename to 50 chars with extension and hash' => [
" { $filename } .zip " ,
50 ,
'sed ut perspiciatis unde omnis iste n - 3bec1da8b8.zip' ,
true ,
],
'Test filename that contains less than 100 characters' => [
$shortfilename ,
null ,
$shortfilename ,
false ,
],
'Test filename that contains less than 100 characters and hash' => [
$shortfilename ,
null ,
$shortfilename ,
true ,
],
'Test filename that contains less than 100 characters with extension' => [
" { $shortfilename } .zip " ,
null ,
" { $shortfilename } .zip " ,
false ,
],
'Test filename that contains less than 100 characters with extension and hash' => [
" { $shortfilename } .zip " ,
null ,
" { $shortfilename } .zip " ,
true ,
],
];
}
/**
* Test the { @ link shorten_filename ()} method .
*
* @ dataProvider shorten_filename_provider
*
* @ param string $filename
* @ param int $length
* @ param string $expected
* @ param boolean $includehash
*/
public function test_shorten_filename ( $filename , $length , $expected , $includehash ) {
if ( null === $length ) {
$length = MAX_FILENAME_SIZE ;
}
$this -> assertSame ( $expected , shorten_filename ( $filename , $length , $includehash ));
2018-03-26 11:14:46 +08:00
}
2018-04-27 12:42:53 +02:00
/**
* Provider for long filenames and its expected result , with and without hash .
*
* @ return array of ( $filename , $length , $expected , $includehash )
*/
public function shorten_filenames_provider () {
$shortfilename = 'sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque' ;
$longfilename = 'sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium totam rem' ;
$extfilename = $longfilename . '.zip' ;
$expected = 'sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium tot' ;
$expectedwithhash = 'sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque l - 3bec1da8b8' ;
$expectedext = 'sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium tot.zip' ;
$expectedextwithhash = 'sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque l - 3bec1da8b8.zip' ;
$expected50 = 'sed ut perspiciatis unde omnis iste natus error si' ;
$expected50withhash = 'sed ut perspiciatis unde omnis iste n - 3bec1da8b8' ;
$expected50ext = 'sed ut perspiciatis unde omnis iste natus error si.zip' ;
$expected50extwithhash = 'sed ut perspiciatis unde omnis iste n - 3bec1da8b8.zip' ;
$expected50short = 'sed ut perspiciatis unde omnis iste n - 5fb6543490' ;
return [
'Empty array without hash' => [
[],
null ,
[],
false ,
],
'Empty array with hash' => [
[],
null ,
[],
true ,
],
'Array with less than 100 characters' => [
[ $shortfilename , $shortfilename , $shortfilename ],
null ,
[ $shortfilename , $shortfilename , $shortfilename ],
false ,
],
'Array with more than 100 characters without hash' => [
[ $longfilename , $longfilename , $longfilename ],
null ,
[ $expected , $expected , $expected ],
false ,
],
'Array with more than 100 characters with hash' => [
[ $longfilename , $longfilename , $longfilename ],
null ,
[ $expectedwithhash , $expectedwithhash , $expectedwithhash ],
true ,
],
'Array with more than 100 characters with extension' => [
[ $extfilename , $extfilename , $extfilename ],
null ,
[ $expectedext , $expectedext , $expectedext ],
false ,
],
'Array with more than 100 characters with extension and hash' => [
[ $extfilename , $extfilename , $extfilename ],
null ,
[ $expectedextwithhash , $expectedextwithhash , $expectedextwithhash ],
true ,
],
'Array with more than 100 characters mix (short, long, with extension) without hash' => [
[ $shortfilename , $longfilename , $extfilename ],
null ,
[ $shortfilename , $expected , $expectedext ],
false ,
],
'Array with more than 100 characters mix (short, long, with extension) with hash' => [
[ $shortfilename , $longfilename , $extfilename ],
null ,
[ $shortfilename , $expectedwithhash , $expectedextwithhash ],
true ,
],
'Array with less than 50 characters without hash' => [
[ $longfilename , $longfilename , $longfilename ],
50 ,
[ $expected50 , $expected50 , $expected50 ],
false ,
],
'Array with less than 50 characters with hash' => [
[ $longfilename , $longfilename , $longfilename ],
50 ,
[ $expected50withhash , $expected50withhash , $expected50withhash ],
true ,
],
'Array with less than 50 characters with extension' => [
[ $extfilename , $extfilename , $extfilename ],
50 ,
[ $expected50ext , $expected50ext , $expected50ext ],
false ,
],
'Array with less than 50 characters with extension and hash' => [
[ $extfilename , $extfilename , $extfilename ],
50 ,
[ $expected50extwithhash , $expected50extwithhash , $expected50extwithhash ],
true ,
],
'Array with less than 50 characters mix (short, long, with extension) without hash' => [
[ $shortfilename , $longfilename , $extfilename ],
50 ,
[ $expected50 , $expected50 , $expected50ext ],
false ,
],
'Array with less than 50 characters mix (short, long, with extension) with hash' => [
[ $shortfilename , $longfilename , $extfilename ],
50 ,
[ $expected50short , $expected50withhash , $expected50extwithhash ],
true ,
],
];
}
/**
* Test the { @ link shorten_filenames ()} method .
*
* @ dataProvider shorten_filenames_provider
*
* @ param string $filenames
* @ param int $length
* @ param string $expected
* @ param boolean $includehash
*/
public function test_shorten_filenames ( $filenames , $length , $expected , $includehash ) {
if ( null === $length ) {
$length = MAX_FILENAME_SIZE ;
}
$this -> assertSame ( $expected , shorten_filenames ( $filenames , $length , $includehash ));
}
2013-07-27 21:41:03 +02:00
public function test_usergetdate () {
2012-03-31 23:51:02 +02:00
global $USER , $CFG , $DB ;
2013-07-27 21:41:03 +02:00
$this -> resetAfterTest ();
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
$this -> setAdminUser ();
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
$USER -> timezone = 2 ; // Set the timezone to a known state.
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
$ts = 1261540267 ; // The time this function was created.
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
$arr = usergetdate ( $ts , 1 ); // Specify the timezone as an argument.
2012-03-31 23:51:02 +02:00
$arr = array_values ( $arr );
2013-07-27 21:41:03 +02:00
list ( $seconds , $minutes , $hours , $mday , $wday , $mon , $year , $yday , $weekday , $month ) = $arr ;
$this -> assertSame ( 7 , $seconds );
$this -> assertSame ( 51 , $minutes );
$this -> assertSame ( 4 , $hours );
$this -> assertSame ( 23 , $mday );
$this -> assertSame ( 3 , $wday );
$this -> assertSame ( 12 , $mon );
$this -> assertSame ( 2009 , $year );
$this -> assertSame ( 356 , $yday );
$this -> assertSame ( 'Wednesday' , $weekday );
$this -> assertSame ( 'December' , $month );
$arr = usergetdate ( $ts ); // Gets the timezone from the $USER object.
2012-03-31 23:51:02 +02:00
$arr = array_values ( $arr );
2013-07-27 21:41:03 +02:00
list ( $seconds , $minutes , $hours , $mday , $wday , $mon , $year , $yday , $weekday , $month ) = $arr ;
$this -> assertSame ( 7 , $seconds );
$this -> assertSame ( 51 , $minutes );
$this -> assertSame ( 5 , $hours );
$this -> assertSame ( 23 , $mday );
$this -> assertSame ( 3 , $wday );
$this -> assertSame ( 12 , $mon );
$this -> assertSame ( 2009 , $year );
$this -> assertSame ( 356 , $yday );
$this -> assertSame ( 'Wednesday' , $weekday );
$this -> assertSame ( 'December' , $month );
2012-03-31 23:51:02 +02:00
}
public function test_mark_user_preferences_changed () {
2013-07-27 21:41:03 +02:00
$this -> resetAfterTest ();
$otheruser = $this -> getDataGenerator () -> create_user ();
$otheruserid = $otheruser -> id ;
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
set_cache_flag ( 'userpreferenceschanged' , $otheruserid , null );
2012-03-31 23:51:02 +02:00
mark_user_preferences_changed ( $otheruserid );
$this -> assertEquals ( get_cache_flag ( 'userpreferenceschanged' , $otheruserid , time () - 10 ), 1 );
2013-07-27 21:41:03 +02:00
set_cache_flag ( 'userpreferenceschanged' , $otheruserid , null );
2012-03-31 23:51:02 +02:00
}
public function test_check_user_preferences_loaded () {
global $DB ;
2013-07-27 21:41:03 +02:00
$this -> resetAfterTest ();
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
$otheruser = $this -> getDataGenerator () -> create_user ();
$otheruserid = $otheruser -> id ;
2012-03-31 23:51:02 +02:00
$DB -> delete_records ( 'user_preferences' , array ( 'userid' => $otheruserid ));
2013-07-27 21:41:03 +02:00
set_cache_flag ( 'userpreferenceschanged' , $otheruserid , null );
2012-03-31 23:51:02 +02:00
$user = new stdClass ();
$user -> id = $otheruserid ;
2013-07-27 21:41:03 +02:00
// Load.
2012-03-31 23:51:02 +02:00
check_user_preferences_loaded ( $user );
$this -> assertTrue ( isset ( $user -> preference ));
$this -> assertTrue ( is_array ( $user -> preference ));
2013-07-27 21:41:03 +02:00
$this -> assertArrayHasKey ( '_lastloaded' , $user -> preference );
$this -> assertCount ( 1 , $user -> preference );
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
// Add preference via direct call.
2012-03-31 23:51:02 +02:00
$DB -> insert_record ( 'user_preferences' , array ( 'name' => 'xxx' , 'value' => 'yyy' , 'userid' => $user -> id ));
2013-07-27 21:41:03 +02:00
// No cache reload yet.
2012-03-31 23:51:02 +02:00
check_user_preferences_loaded ( $user );
2013-07-27 21:41:03 +02:00
$this -> assertCount ( 1 , $user -> preference );
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
// Forced reloading of cache.
2012-03-31 23:51:02 +02:00
unset ( $user -> preference );
check_user_preferences_loaded ( $user );
2013-07-27 21:41:03 +02:00
$this -> assertCount ( 2 , $user -> preference );
$this -> assertSame ( 'yyy' , $user -> preference [ 'xxx' ]);
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
// Add preference via direct call.
2012-03-31 23:51:02 +02:00
$DB -> insert_record ( 'user_preferences' , array ( 'name' => 'aaa' , 'value' => 'bbb' , 'userid' => $user -> id ));
2013-07-27 21:41:03 +02:00
// Test timeouts and modifications from different session.
2012-03-31 23:51:02 +02:00
set_cache_flag ( 'userpreferenceschanged' , $user -> id , 1 , time () + 1000 );
$user -> preference [ '_lastloaded' ] = $user -> preference [ '_lastloaded' ] - 20 ;
check_user_preferences_loaded ( $user );
2013-07-27 21:41:03 +02:00
$this -> assertCount ( 2 , $user -> preference );
2012-03-31 23:51:02 +02:00
check_user_preferences_loaded ( $user , 10 );
2013-07-27 21:41:03 +02:00
$this -> assertCount ( 3 , $user -> preference );
$this -> assertSame ( 'bbb' , $user -> preference [ 'aaa' ]);
2012-03-31 23:51:02 +02:00
set_cache_flag ( 'userpreferenceschanged' , $user -> id , null );
}
public function test_set_user_preference () {
global $DB , $USER ;
2013-07-27 21:41:03 +02:00
$this -> resetAfterTest ();
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
$this -> setAdminUser ();
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
$otheruser = $this -> getDataGenerator () -> create_user ();
$otheruserid = $otheruser -> id ;
2012-03-31 23:51:02 +02:00
$DB -> delete_records ( 'user_preferences' , array ( 'userid' => $otheruserid ));
set_cache_flag ( 'userpreferenceschanged' , $otheruserid , null );
$user = new stdClass ();
$user -> id = $otheruserid ;
set_user_preference ( 'aaa' , 'bbb' , $otheruserid );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( 'bbb' , $DB -> get_field ( 'user_preferences' , 'value' , array ( 'userid' => $otheruserid , 'name' => 'aaa' )));
$this -> assertSame ( 'bbb' , get_user_preferences ( 'aaa' , null , $otheruserid ));
2012-03-31 23:51:02 +02:00
set_user_preference ( 'xxx' , 'yyy' , $user );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( 'yyy' , $DB -> get_field ( 'user_preferences' , 'value' , array ( 'userid' => $otheruserid , 'name' => 'xxx' )));
$this -> assertSame ( 'yyy' , get_user_preferences ( 'xxx' , null , $otheruserid ));
2012-03-31 23:51:02 +02:00
$this -> assertTrue ( is_array ( $user -> preference ));
2013-07-27 21:41:03 +02:00
$this -> assertSame ( 'bbb' , $user -> preference [ 'aaa' ]);
$this -> assertSame ( 'yyy' , $user -> preference [ 'xxx' ]);
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
set_user_preference ( 'xxx' , null , $user );
$this -> assertFalse ( $DB -> get_field ( 'user_preferences' , 'value' , array ( 'userid' => $otheruserid , 'name' => 'xxx' )));
$this -> assertNull ( get_user_preferences ( 'xxx' , null , $otheruserid ));
2012-03-31 23:51:02 +02:00
set_user_preference ( 'ooo' , true , $user );
$prefs = get_user_preferences ( null , null , $otheruserid );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $user -> preference [ 'aaa' ], $prefs [ 'aaa' ]);
$this -> assertSame ( $user -> preference [ 'ooo' ], $prefs [ 'ooo' ]);
$this -> assertSame ( '1' , $prefs [ 'ooo' ]);
2012-03-31 23:51:02 +02:00
set_user_preference ( 'null' , 0 , $user );
$this -> assertSame ( '0' , get_user_preferences ( 'null' , null , $otheruserid ));
$this -> assertSame ( 'lala' , get_user_preferences ( 'undefined' , 'lala' , $otheruserid ));
$DB -> delete_records ( 'user_preferences' , array ( 'userid' => $otheruserid ));
set_cache_flag ( 'userpreferenceschanged' , $otheruserid , null );
2013-07-27 21:41:03 +02:00
// Test $USER default.
2012-03-31 23:51:02 +02:00
set_user_preference ( '_test_user_preferences_pref' , 'ok' );
$this -> assertSame ( 'ok' , $USER -> preference [ '_test_user_preferences_pref' ]);
unset_user_preference ( '_test_user_preferences_pref' );
$this -> assertTrue ( ! isset ( $USER -> preference [ '_test_user_preferences_pref' ]));
2013-07-27 21:41:03 +02:00
// Test 1333 char values (no need for unicode, there are already tests for that in DB tests).
2012-03-31 23:51:02 +02:00
$longvalue = str_repeat ( 'a' , 1333 );
set_user_preference ( '_test_long_user_preference' , $longvalue );
$this -> assertEquals ( $longvalue , get_user_preferences ( '_test_long_user_preference' ));
$this -> assertEquals ( $longvalue ,
$DB -> get_field ( 'user_preferences' , 'value' , array ( 'userid' => $USER -> id , 'name' => '_test_long_user_preference' )));
2013-07-27 21:41:03 +02:00
// Test > 1333 char values, coding_exception expected.
2012-03-31 23:51:02 +02:00
$longvalue = str_repeat ( 'a' , 1334 );
try {
set_user_preference ( '_test_long_user_preference' , $longvalue );
2013-02-05 11:57:03 +08:00
$this -> fail ( 'Exception expected - longer than 1333 chars not allowed as preference value' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
2013-07-27 21:41:03 +02:00
// Test invalid params.
2012-03-31 23:51:02 +02:00
try {
set_user_preference ( '_test_user_preferences_pref' , array ());
2013-02-05 11:57:03 +08:00
$this -> fail ( 'Exception expected - array not valid preference value' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
try {
set_user_preference ( '_test_user_preferences_pref' , new stdClass );
2013-02-05 11:57:03 +08:00
$this -> fail ( 'Exception expected - class not valid preference value' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
try {
2013-02-05 11:57:03 +08:00
set_user_preference ( '_test_user_preferences_pref' , 1 , array ( 'xx' => 1 ));
$this -> fail ( 'Exception expected - user instance expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
try {
set_user_preference ( '_test_user_preferences_pref' , 1 , 'abc' );
2013-02-05 11:57:03 +08:00
$this -> fail ( 'Exception expected - user instance expected' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
try {
set_user_preference ( '' , 1 );
2013-02-05 11:57:03 +08:00
$this -> fail ( 'Exception expected - invalid name accepted' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
try {
set_user_preference ( '1' , 1 );
2013-02-05 11:57:03 +08:00
$this -> fail ( 'Exception expected - invalid name accepted' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-03-31 23:51:02 +02:00
}
}
2017-04-04 16:20:26 +01:00
public function test_set_user_preference_for_current_user () {
global $USER ;
$this -> resetAfterTest ();
$this -> setAdminUser ();
set_user_preference ( 'test_pref' , 2 );
set_user_preference ( 'test_pref' , 1 , $USER -> id );
$this -> assertEquals ( 1 , get_user_preferences ( 'test_pref' ));
}
public function test_unset_user_preference_for_current_user () {
global $USER ;
$this -> resetAfterTest ();
$this -> setAdminUser ();
set_user_preference ( 'test_pref' , 1 );
unset_user_preference ( 'test_pref' , $USER -> id );
$this -> assertNull ( get_user_preferences ( 'test_pref' ));
}
2018-07-13 12:48:14 +02:00
/**
* Test essential features implementation of { @ link get_extra_user_fields ()} as the admin user with all capabilities .
*/
public function test_get_extra_user_fields_essentials () {
2012-03-31 23:51:02 +02:00
global $CFG , $USER , $DB ;
2013-07-27 21:41:03 +02:00
$this -> resetAfterTest ();
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
$this -> setAdminUser ();
2012-03-31 23:51:02 +02:00
$context = context_system :: instance ();
2013-07-27 21:41:03 +02:00
// No fields.
2012-03-31 23:51:02 +02:00
$CFG -> showuseridentity = '' ;
$this -> assertEquals ( array (), get_extra_user_fields ( $context ));
2013-07-27 21:41:03 +02:00
// One field.
2012-03-31 23:51:02 +02:00
$CFG -> showuseridentity = 'frog' ;
$this -> assertEquals ( array ( 'frog' ), get_extra_user_fields ( $context ));
2013-07-27 21:41:03 +02:00
// Two fields.
2012-03-31 23:51:02 +02:00
$CFG -> showuseridentity = 'frog,zombie' ;
$this -> assertEquals ( array ( 'frog' , 'zombie' ), get_extra_user_fields ( $context ));
2013-07-27 21:41:03 +02:00
// No fields, except.
2012-03-31 23:51:02 +02:00
$CFG -> showuseridentity = '' ;
$this -> assertEquals ( array (), get_extra_user_fields ( $context , array ( 'frog' )));
2013-07-27 21:41:03 +02:00
// One field.
2012-03-31 23:51:02 +02:00
$CFG -> showuseridentity = 'frog' ;
$this -> assertEquals ( array (), get_extra_user_fields ( $context , array ( 'frog' )));
2013-07-27 21:41:03 +02:00
// Two fields.
2012-03-31 23:51:02 +02:00
$CFG -> showuseridentity = 'frog,zombie' ;
$this -> assertEquals ( array ( 'zombie' ), get_extra_user_fields ( $context , array ( 'frog' )));
}
2018-07-13 12:48:14 +02:00
/**
* Prepare environment for couple of tests related to permission checks in { @ link get_extra_user_fields ()} .
*
* @ return stdClass
*/
protected function environment_for_get_extra_user_fields_tests () {
global $CFG , $DB ;
$CFG -> showuseridentity = 'idnumber,country,city' ;
$CFG -> hiddenuserfields = 'country,city' ;
$env = new stdClass ();
$env -> course = $this -> getDataGenerator () -> create_course ();
$env -> coursecontext = context_course :: instance ( $env -> course -> id );
$env -> teacherrole = $DB -> get_record ( 'role' , array ( 'shortname' => 'teacher' ));
$env -> studentrole = $DB -> get_record ( 'role' , array ( 'shortname' => 'student' ));
$env -> managerrole = $DB -> get_record ( 'role' , array ( 'shortname' => 'manager' ));
$env -> student = $this -> getDataGenerator () -> create_user ();
$env -> teacher = $this -> getDataGenerator () -> create_user ();
$env -> manager = $this -> getDataGenerator () -> create_user ();
role_assign ( $env -> studentrole -> id , $env -> student -> id , $env -> coursecontext -> id );
role_assign ( $env -> teacherrole -> id , $env -> teacher -> id , $env -> coursecontext -> id );
role_assign ( $env -> managerrole -> id , $env -> manager -> id , SYSCONTEXTID );
return $env ;
}
/**
* No identity fields shown to student user ( no permission to view identity fields ) .
*/
public function test_get_extra_user_fields_no_access () {
$this -> resetAfterTest ();
$env = $this -> environment_for_get_extra_user_fields_tests ();
$this -> setUser ( $env -> student );
$this -> assertEquals ( array (), get_extra_user_fields ( $env -> coursecontext ));
$this -> assertEquals ( array (), get_extra_user_fields ( context_system :: instance ()));
}
/**
* Teacher can see students ' identity fields only within the course .
*/
public function test_get_extra_user_fields_course_only_access () {
$this -> resetAfterTest ();
$env = $this -> environment_for_get_extra_user_fields_tests ();
$this -> setUser ( $env -> teacher );
$this -> assertEquals ( array ( 'idnumber' , 'country' , 'city' ), get_extra_user_fields ( $env -> coursecontext ));
$this -> assertEquals ( array (), get_extra_user_fields ( context_system :: instance ()));
}
/**
* Teacher can be prevented from seeing students ' identity fields even within the course .
*/
public function test_get_extra_user_fields_course_prevented_access () {
$this -> resetAfterTest ();
$env = $this -> environment_for_get_extra_user_fields_tests ();
$this -> setUser ( $env -> teacher );
assign_capability ( 'moodle/course:viewhiddenuserfields' , CAP_PREVENT , $env -> teacherrole -> id , $env -> coursecontext -> id );
$this -> assertEquals ( array ( 'idnumber' ), get_extra_user_fields ( $env -> coursecontext ));
}
/**
* Manager can see students ' identity fields anywhere .
*/
public function test_get_extra_user_fields_anywhere_access () {
$this -> resetAfterTest ();
$env = $this -> environment_for_get_extra_user_fields_tests ();
$this -> setUser ( $env -> manager );
$this -> assertEquals ( array ( 'idnumber' , 'country' , 'city' ), get_extra_user_fields ( $env -> coursecontext ));
$this -> assertEquals ( array ( 'idnumber' , 'country' , 'city' ), get_extra_user_fields ( context_system :: instance ()));
}
/**
* Manager can be prevented from seeing hidden fields outside the course .
*/
public function test_get_extra_user_fields_schismatic_access () {
$this -> resetAfterTest ();
$env = $this -> environment_for_get_extra_user_fields_tests ();
$this -> setUser ( $env -> manager );
assign_capability ( 'moodle/user:viewhiddendetails' , CAP_PREVENT , $env -> managerrole -> id , SYSCONTEXTID , true );
$this -> assertEquals ( array ( 'idnumber' ), get_extra_user_fields ( context_system :: instance ()));
// Note that inside the course, the manager can still see the hidden identifiers as this is currently
// controlled by a separate capability for legacy reasons.
$this -> assertEquals ( array ( 'idnumber' , 'country' , 'city' ), get_extra_user_fields ( $env -> coursecontext ));
}
/**
* Two capabilities must be currently set to prevent manager from seeing hidden fields .
*/
public function test_get_extra_user_fields_hard_to_prevent_access () {
$this -> resetAfterTest ();
$env = $this -> environment_for_get_extra_user_fields_tests ();
$this -> setUser ( $env -> manager );
assign_capability ( 'moodle/user:viewhiddendetails' , CAP_PREVENT , $env -> managerrole -> id , SYSCONTEXTID , true );
assign_capability ( 'moodle/course:viewhiddenuserfields' , CAP_PREVENT , $env -> managerrole -> id , SYSCONTEXTID , true );
$this -> assertEquals ( array ( 'idnumber' ), get_extra_user_fields ( context_system :: instance ()));
$this -> assertEquals ( array ( 'idnumber' ), get_extra_user_fields ( $env -> coursecontext ));
}
2012-03-31 23:51:02 +02:00
public function test_get_extra_user_fields_sql () {
global $CFG , $USER , $DB ;
2013-07-27 21:41:03 +02:00
$this -> resetAfterTest ();
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
$this -> setAdminUser ();
2012-03-31 23:51:02 +02:00
$context = context_system :: instance ();
2013-07-27 21:41:03 +02:00
// No fields.
2012-03-31 23:51:02 +02:00
$CFG -> showuseridentity = '' ;
2013-07-27 21:41:03 +02:00
$this -> assertSame ( '' , get_extra_user_fields_sql ( $context ));
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
// One field.
2012-03-31 23:51:02 +02:00
$CFG -> showuseridentity = 'frog' ;
2013-07-27 21:41:03 +02:00
$this -> assertSame ( ', frog' , get_extra_user_fields_sql ( $context ));
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
// Two fields with table prefix.
2012-03-31 23:51:02 +02:00
$CFG -> showuseridentity = 'frog,zombie' ;
2013-07-27 21:41:03 +02:00
$this -> assertSame ( ', u1.frog, u1.zombie' , get_extra_user_fields_sql ( $context , 'u1' ));
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
// Two fields with field prefix.
2012-03-31 23:51:02 +02:00
$CFG -> showuseridentity = 'frog,zombie' ;
2013-07-27 21:41:03 +02:00
$this -> assertSame ( ', frog AS u_frog, zombie AS u_zombie' ,
2012-03-31 23:51:02 +02:00
get_extra_user_fields_sql ( $context , '' , 'u_' ));
2013-07-27 21:41:03 +02:00
// One field excluded.
2012-03-31 23:51:02 +02:00
$CFG -> showuseridentity = 'frog' ;
2013-07-27 21:41:03 +02:00
$this -> assertSame ( '' , get_extra_user_fields_sql ( $context , '' , '' , array ( 'frog' )));
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
// Two fields, one excluded, table+field prefix.
2012-03-31 23:51:02 +02:00
$CFG -> showuseridentity = 'frog,zombie' ;
$this -> assertEquals ( ', u1.zombie AS u_zombie' ,
get_extra_user_fields_sql ( $context , 'u1' , 'u_' , array ( 'frog' )));
}
2013-04-08 16:09:41 +02:00
/**
* Test some critical TZ / DST .
*
* This method tests some special TZ / DST combinations that were fixed
* by MDL - 38999. The tests are done by comparing the results of the
* output using Moodle TZ / DST support and PHP native one .
*
* Note : If you don ' t trust PHP TZ / DST support , can verify the
* harcoded expectations below with :
* http :// www . tools4noobs . com / online_tools / unix_timestamp_to_datetime /
*/
public function test_some_moodle_special_dst () {
$stamp = 1365386400 ; // 2013/04/08 02:00:00 GMT/UTC.
// In Europe/Tallinn it was 2013/04/08 05:00:00.
$expectation = '2013/04/08 05:00:00' ;
$phpdt = DateTime :: createFromFormat ( 'U' , $stamp , new DateTimeZone ( 'UTC' ));
$phpdt -> setTimezone ( new DateTimeZone ( 'Europe/Tallinn' ));
$phpres = $phpdt -> format ( 'Y/m/d H:i:s' ); // PHP result.
$moodleres = userdate ( $stamp , '%Y/%m/%d %H:%M:%S' , 'Europe/Tallinn' , false ); // Moodle result.
$this -> assertSame ( $expectation , $phpres );
$this -> assertSame ( $expectation , $moodleres );
// In St. Johns it was 2013/04/07 23:30:00.
$expectation = '2013/04/07 23:30:00' ;
$phpdt = DateTime :: createFromFormat ( 'U' , $stamp , new DateTimeZone ( 'UTC' ));
$phpdt -> setTimezone ( new DateTimeZone ( 'America/St_Johns' ));
$phpres = $phpdt -> format ( 'Y/m/d H:i:s' ); // PHP result.
$moodleres = userdate ( $stamp , '%Y/%m/%d %H:%M:%S' , 'America/St_Johns' , false ); // Moodle result.
$this -> assertSame ( $expectation , $phpres );
$this -> assertSame ( $expectation , $moodleres );
$stamp = 1383876000 ; // 2013/11/08 02:00:00 GMT/UTC.
// In Europe/Tallinn it was 2013/11/08 04:00:00.
$expectation = '2013/11/08 04:00:00' ;
$phpdt = DateTime :: createFromFormat ( 'U' , $stamp , new DateTimeZone ( 'UTC' ));
$phpdt -> setTimezone ( new DateTimeZone ( 'Europe/Tallinn' ));
$phpres = $phpdt -> format ( 'Y/m/d H:i:s' ); // PHP result.
$moodleres = userdate ( $stamp , '%Y/%m/%d %H:%M:%S' , 'Europe/Tallinn' , false ); // Moodle result.
$this -> assertSame ( $expectation , $phpres );
$this -> assertSame ( $expectation , $moodleres );
// In St. Johns it was 2013/11/07 22:30:00.
$expectation = '2013/11/07 22:30:00' ;
$phpdt = DateTime :: createFromFormat ( 'U' , $stamp , new DateTimeZone ( 'UTC' ));
$phpdt -> setTimezone ( new DateTimeZone ( 'America/St_Johns' ));
$phpres = $phpdt -> format ( 'Y/m/d H:i:s' ); // PHP result.
$moodleres = userdate ( $stamp , '%Y/%m/%d %H:%M:%S' , 'America/St_Johns' , false ); // Moodle result.
$this -> assertSame ( $expectation , $phpres );
$this -> assertSame ( $expectation , $moodleres );
}
2012-03-31 23:51:02 +02:00
public function test_userdate () {
global $USER , $CFG , $DB ;
2013-07-27 21:41:03 +02:00
$this -> resetAfterTest ();
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
$this -> setAdminUser ();
2012-03-31 23:51:02 +02:00
$testvalues = array (
array (
'time' => '1309514400' ,
'usertimezone' => 'America/Moncton' ,
2013-07-27 21:41:03 +02:00
'timezone' => '0.0' , // No dst offset.
2018-09-10 15:52:07 +08:00
'expectedoutput' => 'Friday, 1 July 2011, 10:00 AM' ,
'expectedoutputhtml' => '<time datetime="2011-07-01T07:00:00-03:00">Friday, 1 July 2011, 10:00 AM</time>'
2012-03-31 23:51:02 +02:00
),
array (
'time' => '1309514400' ,
'usertimezone' => 'America/Moncton' ,
2013-07-27 21:41:03 +02:00
'timezone' => '99' , // Dst offset and timezone offset.
2018-09-10 15:52:07 +08:00
'expectedoutput' => 'Friday, 1 July 2011, 7:00 AM' ,
'expectedoutputhtml' => '<time datetime="2011-07-01T07:00:00-03:00">Friday, 1 July 2011, 7:00 AM</time>'
2012-03-31 23:51:02 +02:00
),
array (
'time' => '1309514400' ,
'usertimezone' => 'America/Moncton' ,
2013-07-27 21:41:03 +02:00
'timezone' => 'America/Moncton' , // Dst offset and timezone offset.
2018-09-10 15:52:07 +08:00
'expectedoutput' => 'Friday, 1 July 2011, 7:00 AM' ,
'expectedoutputhtml' => '<time datetime="2011-07-01t07:00:00-03:00">Friday, 1 July 2011, 7:00 AM</time>'
2012-03-31 23:51:02 +02:00
),
array (
'time' => '1293876000 ' ,
'usertimezone' => 'America/Moncton' ,
2013-07-27 21:41:03 +02:00
'timezone' => '0.0' , // No dst offset.
2018-09-10 15:52:07 +08:00
'expectedoutput' => 'Saturday, 1 January 2011, 10:00 AM' ,
'expectedoutputhtml' => '<time datetime="2011-01-01T06:00:00-04:00">Saturday, 1 January 2011, 10:00 AM</time>'
2012-03-31 23:51:02 +02:00
),
array (
'time' => '1293876000 ' ,
'usertimezone' => 'America/Moncton' ,
2013-07-27 21:41:03 +02:00
'timezone' => '99' , // No dst offset in jan, so just timezone offset.
2018-09-10 15:52:07 +08:00
'expectedoutput' => 'Saturday, 1 January 2011, 6:00 AM' ,
'expectedoutputhtml' => '<time datetime="2011-01-01T06:00:00-04:00">Saturday, 1 January 2011, 6:00 AM</time>'
2012-03-31 23:51:02 +02:00
),
array (
'time' => '1293876000 ' ,
'usertimezone' => 'America/Moncton' ,
2013-07-27 21:41:03 +02:00
'timezone' => 'America/Moncton' , // No dst offset in jan.
2018-09-10 15:52:07 +08:00
'expectedoutput' => 'Saturday, 1 January 2011, 6:00 AM' ,
'expectedoutputhtml' => '<time datetime="2011-01-01T06:00:00-04:00">Saturday, 1 January 2011, 6:00 AM</time>'
2012-03-31 23:51:02 +02:00
),
array (
'time' => '1293876000 ' ,
'usertimezone' => '2' ,
2013-07-27 21:41:03 +02:00
'timezone' => '99' , // Take user timezone.
2018-09-10 15:52:07 +08:00
'expectedoutput' => 'Saturday, 1 January 2011, 12:00 PM' ,
'expectedoutputhtml' => '<time datetime="2011-01-01T12:00:00+02:00">Saturday, 1 January 2011, 12:00 PM</time>'
2012-03-31 23:51:02 +02:00
),
array (
'time' => '1293876000 ' ,
'usertimezone' => '-2' ,
2013-07-27 21:41:03 +02:00
'timezone' => '99' , // Take user timezone.
2018-09-10 15:52:07 +08:00
'expectedoutput' => 'Saturday, 1 January 2011, 8:00 AM' ,
'expectedoutputhtml' => '<time datetime="2011-01-01T08:00:00-02:00">Saturday, 1 January 2011, 8:00 AM</time>'
2012-03-31 23:51:02 +02:00
),
array (
'time' => '1293876000 ' ,
'usertimezone' => '-10' ,
2013-07-27 21:41:03 +02:00
'timezone' => '2' , // Take this timezone.
2018-09-10 15:52:07 +08:00
'expectedoutput' => 'Saturday, 1 January 2011, 12:00 PM' ,
'expectedoutputhtml' => '<time datetime="2011-01-01T00:00:00-10:00">Saturday, 1 January 2011, 12:00 PM</time>'
2012-03-31 23:51:02 +02:00
),
array (
'time' => '1293876000 ' ,
'usertimezone' => '-10' ,
2013-07-27 21:41:03 +02:00
'timezone' => '-2' , // Take this timezone.
2018-09-10 15:52:07 +08:00
'expectedoutput' => 'Saturday, 1 January 2011, 8:00 AM' ,
'expectedoutputhtml' => '<time datetime="2011-01-01T00:00:00-10:00">Saturday, 1 January 2011, 8:00 AM</time>'
2012-03-31 23:51:02 +02:00
),
array (
'time' => '1293876000 ' ,
'usertimezone' => '-10' ,
2013-07-27 21:41:03 +02:00
'timezone' => 'random/time' , // This should show server time.
2018-09-10 15:52:07 +08:00
'expectedoutput' => 'Saturday, 1 January 2011, 6:00 PM' ,
'expectedoutputhtml' => '<time datetime="2011-01-01T00:00:00-10:00">Saturday, 1 January 2011, 6:00 PM</time>'
2012-03-31 23:51:02 +02:00
),
array (
'time' => '1293876000 ' ,
2015-03-28 18:46:16 +13:00
'usertimezone' => '20' , // Fallback to server time zone.
2013-07-27 21:41:03 +02:00
'timezone' => '99' , // This should show user time.
2018-09-10 15:52:07 +08:00
'expectedoutput' => 'Saturday, 1 January 2011, 6:00 PM' ,
'expectedoutputhtml' => '<time datetime="2011-01-01T18:00:00+08:00">Saturday, 1 January 2011, 6:00 PM</time>'
2012-03-31 23:51:02 +02:00
),
);
2013-07-27 21:41:03 +02:00
// Set default timezone to Australia/Perth, else time calculated
2015-03-28 18:46:16 +13:00
// will not match expected values.
$this -> setTimezone ( 99 , 'Australia/Perth' );
2012-03-31 23:51:02 +02:00
foreach ( $testvalues as $vals ) {
$USER -> timezone = $vals [ 'usertimezone' ];
$actualoutput = userdate ( $vals [ 'time' ], '%A, %d %B %Y, %I:%M %p' , $vals [ 'timezone' ]);
2018-09-10 15:52:07 +08:00
$actualoutputhtml = userdate_htmltime ( $vals [ 'time' ], '%A, %d %B %Y, %I:%M %p' , $vals [ 'timezone' ]);
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
// On different systems case of AM PM changes so compare case insensitive.
2013-08-06 20:58:28 +02:00
$vals [ 'expectedoutput' ] = core_text :: strtolower ( $vals [ 'expectedoutput' ]);
2018-09-10 15:52:07 +08:00
$vals [ 'expectedoutputhtml' ] = core_text :: strtolower ( $vals [ 'expectedoutputhtml' ]);
2013-08-06 20:58:28 +02:00
$actualoutput = core_text :: strtolower ( $actualoutput );
2018-09-10 15:52:07 +08:00
$actualoutputhtml = core_text :: strtolower ( $actualoutputhtml );
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $vals [ 'expectedoutput' ], $actualoutput ,
2015-03-28 18:46:16 +13:00
" Expected: { $vals [ 'expectedoutput' ] } => Actual: { $actualoutput } \n data: " . var_export ( $vals , true ));
2018-09-10 15:52:07 +08:00
$this -> assertSame ( $vals [ 'expectedoutputhtml' ], $actualoutputhtml ,
" Expected: { $vals [ 'expectedoutputhtml' ] } => Actual: { $actualoutputhtml } \n data: " . var_export ( $vals , true ));
2012-03-31 23:51:02 +02:00
}
}
2015-04-12 13:47:06 +12:00
/**
* Make sure the DST changes happen at the right time in Moodle .
*/
public function test_dst_changes () {
// DST switching in Prague.
// From 2AM to 3AM in 1989.
$date = new DateTime ( '1989-03-26T01:59:00+01:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 26 March 1989, 01:59' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Europe/Prague' ));
2015-04-12 13:47:06 +12:00
$date = new DateTime ( '1989-03-26T02:01:00+01:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 26 March 1989, 03:01' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Europe/Prague' ));
2015-04-12 13:47:06 +12:00
// From 3AM to 2AM in 1989 - not the same as the west Europe.
$date = new DateTime ( '1989-09-24T01:59:00+01:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 24 September 1989, 02:59' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Europe/Prague' ));
2015-04-12 13:47:06 +12:00
$date = new DateTime ( '1989-09-24T02:01:00+01:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 24 September 1989, 02:01' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Europe/Prague' ));
2015-04-12 13:47:06 +12:00
// From 2AM to 3AM in 2014.
$date = new DateTime ( '2014-03-30T01:59:00+01:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 30 March 2014, 01:59' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Europe/Prague' ));
2015-04-12 13:47:06 +12:00
$date = new DateTime ( '2014-03-30T02:01:00+01:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 30 March 2014, 03:01' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Europe/Prague' ));
2015-04-12 13:47:06 +12:00
// From 3AM to 2AM in 2014.
$date = new DateTime ( '2014-10-26T01:59:00+01:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 26 October 2014, 02:59' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Europe/Prague' ));
2015-04-12 13:47:06 +12:00
$date = new DateTime ( '2014-10-26T02:01:00+01:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 26 October 2014, 02:01' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Europe/Prague' ));
2015-04-12 13:47:06 +12:00
// From 2AM to 3AM in 2020.
$date = new DateTime ( '2020-03-29T01:59:00+01:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 29 March 2020, 01:59' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Europe/Prague' ));
2015-04-12 13:47:06 +12:00
$date = new DateTime ( '2020-03-29T02:01:00+01:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 29 March 2020, 03:01' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Europe/Prague' ));
2015-04-12 13:47:06 +12:00
// From 3AM to 2AM in 2020.
$date = new DateTime ( '2020-10-25T01:59:00+01:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 25 October 2020, 02:59' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Europe/Prague' ));
2015-04-12 13:47:06 +12:00
$date = new DateTime ( '2020-10-25T02:01:00+01:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 25 October 2020, 02:01' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Europe/Prague' ));
2015-04-12 13:47:06 +12:00
// DST switching in NZ.
// From 3AM to 2AM in 2015.
$date = new DateTime ( '2015-04-05T02:59:00+13:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 5 April 2015, 02:59' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Pacific/Auckland' ));
2015-04-12 13:47:06 +12:00
$date = new DateTime ( '2015-04-05T03:01:00+13:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 5 April 2015, 02:01' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Pacific/Auckland' ));
2015-04-12 13:47:06 +12:00
// From 2AM to 3AM in 2009.
$date = new DateTime ( '2015-09-27T01:59:00+12:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 27 September 2015, 01:59' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Pacific/Auckland' ));
2015-04-12 13:47:06 +12:00
$date = new DateTime ( '2015-09-27T02:01:00+12:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 27 September 2015, 03:01' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Pacific/Auckland' ));
2015-04-12 13:47:06 +12:00
// DST switching in Perth.
// From 3AM to 2AM in 2009.
$date = new DateTime ( '2008-03-30T01:59:00+08:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 30 March 2008, 02:59' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Australia/Perth' ));
2015-04-12 13:47:06 +12:00
$date = new DateTime ( '2008-03-30T02:01:00+08:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 30 March 2008, 02:01' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Australia/Perth' ));
2015-04-12 13:47:06 +12:00
// From 2AM to 3AM in 2009.
$date = new DateTime ( '2008-10-26T01:59:00+08:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 26 October 2008, 01:59' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Australia/Perth' ));
2015-04-12 13:47:06 +12:00
$date = new DateTime ( '2008-10-26T02:01:00+08:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 26 October 2008, 03:01' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'Australia/Perth' ));
2015-04-12 13:47:06 +12:00
// DST switching in US.
// From 2AM to 3AM in 2014.
$date = new DateTime ( '2014-03-09T01:59:00-05:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 9 March 2014, 01:59' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'America/New_York' ));
2015-04-12 13:47:06 +12:00
$date = new DateTime ( '2014-03-09T02:01:00-05:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 9 March 2014, 03:01' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'America/New_York' ));
2015-04-12 13:47:06 +12:00
// From 3AM to 2AM in 2014.
$date = new DateTime ( '2014-11-02T01:59:00-04:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 2 November 2014, 01:59' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'America/New_York' ));
2015-04-12 13:47:06 +12:00
$date = new DateTime ( '2014-11-02T02:01:00-04:00' );
2015-04-24 13:01:01 +08:00
$this -> assertSame ( 'Sunday, 2 November 2014, 01:01' , userdate ( $date -> getTimestamp (), '%A, %d %B %Y, %H:%M' , 'America/New_York' ));
2015-04-12 13:47:06 +12:00
}
2012-03-31 23:51:02 +02:00
public function test_make_timestamp () {
global $USER , $CFG , $DB ;
2013-07-27 21:41:03 +02:00
$this -> resetAfterTest ();
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
$this -> setAdminUser ();
2012-03-31 23:51:02 +02:00
$testvalues = array (
array (
'usertimezone' => 'America/Moncton' ,
'year' => '2011' ,
'month' => '7' ,
'day' => '1' ,
'hour' => '10' ,
'minutes' => '00' ,
'seconds' => '00' ,
2012-08-14 11:25:06 +08:00
'timezone' => '0.0' ,
2013-07-27 21:41:03 +02:00
'applydst' => false , // No dst offset.
'expectedoutput' => '1309514400' // 6pm at UTC+0.
2012-03-31 23:51:02 +02:00
),
array (
'usertimezone' => 'America/Moncton' ,
'year' => '2011' ,
'month' => '7' ,
'day' => '1' ,
'hour' => '10' ,
'minutes' => '00' ,
'seconds' => '00' ,
2013-07-27 21:41:03 +02:00
'timezone' => '99' , // User default timezone.
'applydst' => false , // Don't apply dst.
2012-03-31 23:51:02 +02:00
'expectedoutput' => '1309528800'
),
array (
'usertimezone' => 'America/Moncton' ,
'year' => '2011' ,
'month' => '7' ,
'day' => '1' ,
'hour' => '10' ,
'minutes' => '00' ,
'seconds' => '00' ,
2013-07-27 21:41:03 +02:00
'timezone' => '99' , // User default timezone.
'applydst' => true , // Apply dst.
2012-03-31 23:51:02 +02:00
'expectedoutput' => '1309525200'
),
array (
'usertimezone' => 'America/Moncton' ,
'year' => '2011' ,
'month' => '7' ,
'day' => '1' ,
'hour' => '10' ,
'minutes' => '00' ,
'seconds' => '00' ,
2013-07-27 21:41:03 +02:00
'timezone' => 'America/Moncton' , // String timezone.
'applydst' => true , // Apply dst.
2012-03-31 23:51:02 +02:00
'expectedoutput' => '1309525200'
),
array (
2013-07-27 21:41:03 +02:00
'usertimezone' => '2' , // No dst applyed.
2012-03-31 23:51:02 +02:00
'year' => '2011' ,
'month' => '7' ,
'day' => '1' ,
'hour' => '10' ,
'minutes' => '00' ,
'seconds' => '00' ,
2013-07-27 21:41:03 +02:00
'timezone' => '99' , // Take user timezone.
'applydst' => true , // Apply dst.
2012-03-31 23:51:02 +02:00
'expectedoutput' => '1309507200'
),
array (
2013-07-27 21:41:03 +02:00
'usertimezone' => '-2' , // No dst applyed.
2012-03-31 23:51:02 +02:00
'year' => '2011' ,
'month' => '7' ,
'day' => '1' ,
'hour' => '10' ,
'minutes' => '00' ,
'seconds' => '00' ,
2013-07-27 21:41:03 +02:00
'timezone' => '99' , // Take usertimezone.
'applydst' => true , // Apply dst.
2012-03-31 23:51:02 +02:00
'expectedoutput' => '1309521600'
),
array (
2013-07-27 21:41:03 +02:00
'usertimezone' => '-10' , // No dst applyed.
2012-03-31 23:51:02 +02:00
'year' => '2011' ,
'month' => '7' ,
'day' => '1' ,
'hour' => '10' ,
'minutes' => '00' ,
'seconds' => '00' ,
2013-07-27 21:41:03 +02:00
'timezone' => '2' , // Take this timezone.
'applydst' => true , // Apply dst.
2012-03-31 23:51:02 +02:00
'expectedoutput' => '1309507200'
),
array (
2013-07-27 21:41:03 +02:00
'usertimezone' => '-10' , // No dst applyed.
2012-03-31 23:51:02 +02:00
'year' => '2011' ,
'month' => '7' ,
'day' => '1' ,
'hour' => '10' ,
'minutes' => '00' ,
'seconds' => '00' ,
2013-07-27 21:41:03 +02:00
'timezone' => '-2' , // Take this timezone.
'applydst' => true , // Apply dst.
2012-03-31 23:51:02 +02:00
'expectedoutput' => '1309521600'
),
array (
2013-07-27 21:41:03 +02:00
'usertimezone' => '-10' , // No dst applyed.
2012-03-31 23:51:02 +02:00
'year' => '2011' ,
'month' => '7' ,
'day' => '1' ,
'hour' => '10' ,
'minutes' => '00' ,
'seconds' => '00' ,
2013-07-27 21:41:03 +02:00
'timezone' => 'random/time' , // This should show server time.
'applydst' => true , // Apply dst.
2012-03-31 23:51:02 +02:00
'expectedoutput' => '1309485600'
),
array (
2016-05-09 13:27:25 +08:00
'usertimezone' => '-14' , // Server time.
2012-03-31 23:51:02 +02:00
'year' => '2011' ,
'month' => '7' ,
'day' => '1' ,
'hour' => '10' ,
'minutes' => '00' ,
'seconds' => '00' ,
2013-07-27 21:41:03 +02:00
'timezone' => '99' , // Get user time.
'applydst' => true , // Apply dst.
2012-03-31 23:51:02 +02:00
'expectedoutput' => '1309485600'
)
);
2013-07-27 21:41:03 +02:00
// Set default timezone to Australia/Perth, else time calculated
2015-03-28 18:46:16 +13:00
// will not match expected values.
$this -> setTimezone ( 99 , 'Australia/Perth' );
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
// Test make_timestamp with all testvals and assert if anything wrong.
2012-03-31 23:51:02 +02:00
foreach ( $testvalues as $vals ) {
$USER -> timezone = $vals [ 'usertimezone' ];
$actualoutput = make_timestamp (
$vals [ 'year' ],
$vals [ 'month' ],
$vals [ 'day' ],
$vals [ 'hour' ],
$vals [ 'minutes' ],
$vals [ 'seconds' ],
$vals [ 'timezone' ],
$vals [ 'applydst' ]
);
2013-07-27 21:41:03 +02:00
// On different systems case of AM PM changes so compare case insensitive.
2013-08-06 20:58:28 +02:00
$vals [ 'expectedoutput' ] = core_text :: strtolower ( $vals [ 'expectedoutput' ]);
$actualoutput = core_text :: strtolower ( $actualoutput );
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $vals [ 'expectedoutput' ], $actualoutput ,
2012-03-31 23:51:02 +02:00
" Expected: { $vals [ 'expectedoutput' ] } => Actual: { $actualoutput } ,
Please check if timezones are updated ( Site adminstration -> location -> update timezone ) " );
}
}
/**
* Test get_string and most importantly the implementation of the lang_string
* object .
*/
public function test_get_string () {
global $COURSE ;
2013-07-27 21:41:03 +02:00
// Make sure we are using English.
2012-03-31 23:51:02 +02:00
$originallang = $COURSE -> lang ;
$COURSE -> lang = 'en' ;
$yes = get_string ( 'yes' );
$yesexpected = 'Yes' ;
MDL-67673 phpunit: Remove deprecated assertInternalType()
While this is not strictly required, because removal will
happen in PHPUnit 9.0, we are already getting rid of all
uses in core.
From release notes:https://phpunit.de/announcements/phpunit-8.html
assertInternalType() is deprecated and will be removed in
PHPUnit 9. Refactor your test to use assertIsArray(), assertIsBool(),
assertIsFloat(), assertIsInt(), assertIsNumeric(), assertIsObject(),
assertIsResource(), assertIsString(), assertIsScalar(),
assertIsCallable(), or assertIsIterable() instead.
2020-08-02 13:08:42 +02:00
$this -> assertIsString ( $yes );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $yesexpected , $yes );
2012-03-31 23:51:02 +02:00
$yes = get_string ( 'yes' , 'moodle' );
MDL-67673 phpunit: Remove deprecated assertInternalType()
While this is not strictly required, because removal will
happen in PHPUnit 9.0, we are already getting rid of all
uses in core.
From release notes:https://phpunit.de/announcements/phpunit-8.html
assertInternalType() is deprecated and will be removed in
PHPUnit 9. Refactor your test to use assertIsArray(), assertIsBool(),
assertIsFloat(), assertIsInt(), assertIsNumeric(), assertIsObject(),
assertIsResource(), assertIsString(), assertIsScalar(),
assertIsCallable(), or assertIsIterable() instead.
2020-08-02 13:08:42 +02:00
$this -> assertIsString ( $yes );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $yesexpected , $yes );
2012-03-31 23:51:02 +02:00
$yes = get_string ( 'yes' , 'core' );
MDL-67673 phpunit: Remove deprecated assertInternalType()
While this is not strictly required, because removal will
happen in PHPUnit 9.0, we are already getting rid of all
uses in core.
From release notes:https://phpunit.de/announcements/phpunit-8.html
assertInternalType() is deprecated and will be removed in
PHPUnit 9. Refactor your test to use assertIsArray(), assertIsBool(),
assertIsFloat(), assertIsInt(), assertIsNumeric(), assertIsObject(),
assertIsResource(), assertIsString(), assertIsScalar(),
assertIsCallable(), or assertIsIterable() instead.
2020-08-02 13:08:42 +02:00
$this -> assertIsString ( $yes );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $yesexpected , $yes );
2012-03-31 23:51:02 +02:00
$yes = get_string ( 'yes' , '' );
MDL-67673 phpunit: Remove deprecated assertInternalType()
While this is not strictly required, because removal will
happen in PHPUnit 9.0, we are already getting rid of all
uses in core.
From release notes:https://phpunit.de/announcements/phpunit-8.html
assertInternalType() is deprecated and will be removed in
PHPUnit 9. Refactor your test to use assertIsArray(), assertIsBool(),
assertIsFloat(), assertIsInt(), assertIsNumeric(), assertIsObject(),
assertIsResource(), assertIsString(), assertIsScalar(),
assertIsCallable(), or assertIsIterable() instead.
2020-08-02 13:08:42 +02:00
$this -> assertIsString ( $yes );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $yesexpected , $yes );
2012-03-31 23:51:02 +02:00
$yes = get_string ( 'yes' , null );
MDL-67673 phpunit: Remove deprecated assertInternalType()
While this is not strictly required, because removal will
happen in PHPUnit 9.0, we are already getting rid of all
uses in core.
From release notes:https://phpunit.de/announcements/phpunit-8.html
assertInternalType() is deprecated and will be removed in
PHPUnit 9. Refactor your test to use assertIsArray(), assertIsBool(),
assertIsFloat(), assertIsInt(), assertIsNumeric(), assertIsObject(),
assertIsResource(), assertIsString(), assertIsScalar(),
assertIsCallable(), or assertIsIterable() instead.
2020-08-02 13:08:42 +02:00
$this -> assertIsString ( $yes );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $yesexpected , $yes );
2012-03-31 23:51:02 +02:00
$yes = get_string ( 'yes' , null , 1 );
MDL-67673 phpunit: Remove deprecated assertInternalType()
While this is not strictly required, because removal will
happen in PHPUnit 9.0, we are already getting rid of all
uses in core.
From release notes:https://phpunit.de/announcements/phpunit-8.html
assertInternalType() is deprecated and will be removed in
PHPUnit 9. Refactor your test to use assertIsArray(), assertIsBool(),
assertIsFloat(), assertIsInt(), assertIsNumeric(), assertIsObject(),
assertIsResource(), assertIsString(), assertIsScalar(),
assertIsCallable(), or assertIsIterable() instead.
2020-08-02 13:08:42 +02:00
$this -> assertIsString ( $yes );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $yesexpected , $yes );
2012-03-31 23:51:02 +02:00
$days = 1 ;
$numdays = get_string ( 'numdays' , 'core' , '1' );
$numdaysexpected = $days . ' days' ;
MDL-67673 phpunit: Remove deprecated assertInternalType()
While this is not strictly required, because removal will
happen in PHPUnit 9.0, we are already getting rid of all
uses in core.
From release notes:https://phpunit.de/announcements/phpunit-8.html
assertInternalType() is deprecated and will be removed in
PHPUnit 9. Refactor your test to use assertIsArray(), assertIsBool(),
assertIsFloat(), assertIsInt(), assertIsNumeric(), assertIsObject(),
assertIsResource(), assertIsString(), assertIsScalar(),
assertIsCallable(), or assertIsIterable() instead.
2020-08-02 13:08:42 +02:00
$this -> assertIsString ( $numdays );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $numdaysexpected , $numdays );
2012-03-31 23:51:02 +02:00
$yes = get_string ( 'yes' , null , null , true );
2013-08-04 22:16:03 +02:00
$this -> assertInstanceOf ( 'lang_string' , $yes );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $yesexpected , ( string ) $yes );
2012-03-31 23:51:02 +02:00
// Test using a lang_string object as the $a argument for a normal
2013-07-27 21:41:03 +02:00
// get_string call (returning string).
2012-03-31 23:51:02 +02:00
$test = new lang_string ( 'yes' , null , null , true );
$testexpected = get_string ( 'numdays' , 'core' , get_string ( 'yes' ));
$testresult = get_string ( 'numdays' , null , $test );
MDL-67673 phpunit: Remove deprecated assertInternalType()
While this is not strictly required, because removal will
happen in PHPUnit 9.0, we are already getting rid of all
uses in core.
From release notes:https://phpunit.de/announcements/phpunit-8.html
assertInternalType() is deprecated and will be removed in
PHPUnit 9. Refactor your test to use assertIsArray(), assertIsBool(),
assertIsFloat(), assertIsInt(), assertIsNumeric(), assertIsObject(),
assertIsResource(), assertIsString(), assertIsScalar(),
assertIsCallable(), or assertIsIterable() instead.
2020-08-02 13:08:42 +02:00
$this -> assertIsString ( $testresult );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $testexpected , $testresult );
2012-03-31 23:51:02 +02:00
// Test using a lang_string object as the $a argument for an object
2013-07-27 21:41:03 +02:00
// get_string call (returning lang_string).
2012-03-31 23:51:02 +02:00
$test = new lang_string ( 'yes' , null , null , true );
$testexpected = get_string ( 'numdays' , 'core' , get_string ( 'yes' ));
$testresult = get_string ( 'numdays' , null , $test , true );
2013-08-04 22:16:03 +02:00
$this -> assertInstanceOf ( 'lang_string' , $testresult );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $testexpected , " $testresult " );
2012-03-31 23:51:02 +02:00
// Make sure that object properties that can't be converted don't cause
2013-07-27 21:41:03 +02:00
// errors.
// Level one: This is as deep as current language processing goes.
2012-03-31 23:51:02 +02:00
$test = new stdClass ;
$test -> one = 'here' ;
$string = get_string ( 'yes' , null , $test , true );
2013-07-27 21:41:03 +02:00
$this -> assertEquals ( $yesexpected , $string );
2012-03-31 23:51:02 +02:00
// Make sure that object properties that can't be converted don't cause
// errors.
// Level two: Language processing doesn't currently reach this deep.
// only immediate scalar properties are worked with.
$test = new stdClass ;
$test -> one = new stdClass ;
$test -> one -> two = 'here' ;
$string = get_string ( 'yes' , null , $test , true );
2013-07-27 21:41:03 +02:00
$this -> assertEquals ( $yesexpected , $string );
2012-03-31 23:51:02 +02:00
// Make sure that object properties that can't be converted don't cause
// errors.
// Level three: It should never ever go this deep, but we're making sure
// it doesn't cause any probs anyway.
$test = new stdClass ;
$test -> one = new stdClass ;
$test -> one -> two = new stdClass ;
$test -> one -> two -> three = 'here' ;
$string = get_string ( 'yes' , null , $test , true );
2013-07-27 21:41:03 +02:00
$this -> assertEquals ( $yesexpected , $string );
2012-03-31 23:51:02 +02:00
// Make sure that object properties that can't be converted don't cause
// errors and check lang_string properties.
2013-07-27 21:41:03 +02:00
// Level one: This is as deep as current language processing goes.
2012-03-31 23:51:02 +02:00
$test = new stdClass ;
$test -> one = new lang_string ( 'yes' );
$string = get_string ( 'yes' , null , $test , true );
2013-07-27 21:41:03 +02:00
$this -> assertEquals ( $yesexpected , $string );
2012-03-31 23:51:02 +02:00
// Make sure that object properties that can't be converted don't cause
// errors and check lang_string properties.
// Level two: Language processing doesn't currently reach this deep.
// only immediate scalar properties are worked with.
$test = new stdClass ;
$test -> one = new stdClass ;
$test -> one -> two = new lang_string ( 'yes' );
$string = get_string ( 'yes' , null , $test , true );
2013-07-27 21:41:03 +02:00
$this -> assertEquals ( $yesexpected , $string );
2012-03-31 23:51:02 +02:00
// Make sure that object properties that can't be converted don't cause
// errors and check lang_string properties.
// Level three: It should never ever go this deep, but we're making sure
// it doesn't cause any probs anyway.
$test = new stdClass ;
$test -> one = new stdClass ;
$test -> one -> two = new stdClass ;
$test -> one -> two -> three = new lang_string ( 'yes' );
$string = get_string ( 'yes' , null , $test , true );
2013-07-27 21:41:03 +02:00
$this -> assertEquals ( $yesexpected , $string );
2012-03-31 23:51:02 +02:00
// Make sure that array properties that can't be converted don't cause
2013-07-27 21:41:03 +02:00
// errors.
2012-03-31 23:51:02 +02:00
$test = array ();
$test [ 'one' ] = new stdClass ;
$test [ 'one' ] -> two = 'here' ;
$string = get_string ( 'yes' , null , $test , true );
2013-07-27 21:41:03 +02:00
$this -> assertEquals ( $yesexpected , $string );
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
// Same thing but as above except using an object... this is allowed :P.
2012-03-31 23:51:02 +02:00
$string = get_string ( 'yes' , null , null , true );
$object = new stdClass ;
$object -> $string = 'Yes' ;
2013-07-27 21:41:03 +02:00
$this -> assertEquals ( $yesexpected , $string );
$this -> assertEquals ( $yesexpected , $object -> $string );
2012-03-31 23:51:02 +02:00
2013-07-27 21:41:03 +02:00
// Reset the language.
2012-03-31 23:51:02 +02:00
$COURSE -> lang = $originallang ;
}
public function test_get_string_limitation () {
// This is one of the limitations to the lang_string class. It can't be
2013-07-27 21:41:03 +02:00
// used as a key.
2020-08-03 01:19:50 +02:00
$this -> expectException ( \PHPUnit\Framework\Error\Warning :: class );
2012-03-31 23:51:02 +02:00
$array = array ( get_string ( 'yes' , null , null , true ) => 'yes' );
}
2012-04-26 15:32:45 +01:00
/**
* Test localised float formatting .
*/
public function test_format_float () {
2013-07-27 21:41:03 +02:00
// Special case for null.
2012-04-26 15:32:45 +01:00
$this -> assertEquals ( '' , format_float ( null ));
2013-07-27 21:41:03 +02:00
// Default 1 decimal place.
2012-04-26 15:32:45 +01:00
$this -> assertEquals ( '5.4' , format_float ( 5.43 ));
$this -> assertEquals ( '5.0' , format_float ( 5.001 ));
2013-07-27 21:41:03 +02:00
// Custom number of decimal places.
2012-04-26 15:32:45 +01:00
$this -> assertEquals ( '5.43000' , format_float ( 5.43 , 5 ));
2019-11-27 20:49:38 +11:00
// Auto detect the number of decimal places.
$this -> assertEquals ( '5.43' , format_float ( 5.43 , - 1 ));
$this -> assertEquals ( '5.43' , format_float ( 5.43000 , - 1 ));
$this -> assertEquals ( '5' , format_float ( 5 , - 1 ));
$this -> assertEquals ( '5' , format_float ( 5.0 , - 1 ));
$this -> assertEquals ( '0.543' , format_float ( '5.43e-1' , - 1 ));
$this -> assertEquals ( '0.543' , format_float ( '5.43000e-1' , - 1 ));
2013-07-27 21:41:03 +02:00
// Option to strip ending zeros after rounding.
2012-04-26 15:32:45 +01:00
$this -> assertEquals ( '5.43' , format_float ( 5.43 , 5 , true , true ));
$this -> assertEquals ( '5' , format_float ( 5.0001 , 3 , true , true ));
2013-05-09 11:35:38 -04:00
// Tests with a localised decimal separator.
$this -> define_local_decimal_separator ();
2012-04-26 15:32:45 +01:00
2013-07-27 21:41:03 +02:00
// Localisation on (default).
2012-04-26 15:32:45 +01:00
$this -> assertEquals ( '5X43000' , format_float ( 5.43 , 5 ));
$this -> assertEquals ( '5X43' , format_float ( 5.43 , 5 , true , true ));
2013-07-27 21:41:03 +02:00
// Localisation off.
2012-04-26 15:32:45 +01:00
$this -> assertEquals ( '5.43000' , format_float ( 5.43 , 5 , false ));
$this -> assertEquals ( '5.43' , format_float ( 5.43 , 5 , false , true ));
2019-04-08 13:58:30 +02:00
// Tests with tilde as localised decimal separator.
$this -> define_local_decimal_separator ( '~' );
// Must also work for '~' as decimal separator.
$this -> assertEquals ( '5' , format_float ( 5.0001 , 3 , true , true ));
$this -> assertEquals ( '5~43000' , format_float ( 5.43 , 5 ));
$this -> assertEquals ( '5~43' , format_float ( 5.43 , 5 , true , true ));
2012-04-26 15:32:45 +01:00
}
2012-09-01 14:13:40 +02:00
2013-05-09 11:35:38 -04:00
/**
* Test localised float unformatting .
*/
public function test_unformat_float () {
// Tests without the localised decimal separator.
// Special case for null, empty or white spaces only strings.
$this -> assertEquals ( null , unformat_float ( null ));
$this -> assertEquals ( null , unformat_float ( '' ));
$this -> assertEquals ( null , unformat_float ( ' ' ));
// Regular use.
$this -> assertEquals ( 5.4 , unformat_float ( '5.4' ));
$this -> assertEquals ( 5.4 , unformat_float ( '5.4' , true ));
// No decimal.
$this -> assertEquals ( 5.0 , unformat_float ( '5' ));
// Custom number of decimal.
$this -> assertEquals ( 5.43267 , unformat_float ( '5.43267' ));
// Empty decimal.
$this -> assertEquals ( 100.0 , unformat_float ( '100.00' ));
// With the thousand separator.
$this -> assertEquals ( 1000.0 , unformat_float ( '1 000' ));
$this -> assertEquals ( 1000.32 , unformat_float ( '1 000.32' ));
// Negative number.
$this -> assertEquals ( - 100.0 , unformat_float ( '-100' ));
// Wrong value.
$this -> assertEquals ( 0.0 , unformat_float ( 'Wrong value' ));
// Wrong value in strict mode.
$this -> assertFalse ( unformat_float ( 'Wrong value' , true ));
// Combining options.
$this -> assertEquals ( - 1023.862567 , unformat_float ( ' -1 023.862567 ' ));
// Bad decimal separator (should crop the decimal).
$this -> assertEquals ( 50.0 , unformat_float ( '50,57' ));
// Bad decimal separator in strict mode (should return false).
$this -> assertFalse ( unformat_float ( '50,57' , true ));
// Tests with a localised decimal separator.
$this -> define_local_decimal_separator ();
// We repeat the tests above but with the current decimal separator.
// Regular use without and with the localised separator.
$this -> assertEquals ( 5.4 , unformat_float ( '5.4' ));
$this -> assertEquals ( 5.4 , unformat_float ( '5X4' ));
// Custom number of decimal.
$this -> assertEquals ( 5.43267 , unformat_float ( '5X43267' ));
// Empty decimal.
$this -> assertEquals ( 100.0 , unformat_float ( '100X00' ));
// With the thousand separator.
$this -> assertEquals ( 1000.32 , unformat_float ( '1 000X32' ));
// Bad different separator (should crop the decimal).
$this -> assertEquals ( 50.0 , unformat_float ( '50Y57' ));
// Bad different separator in strict mode (should return false).
$this -> assertFalse ( unformat_float ( '50Y57' , true ));
// Combining options.
$this -> assertEquals ( - 1023.862567 , unformat_float ( ' -1 023X862567 ' ));
// Combining options in strict mode.
$this -> assertEquals ( - 1023.862567 , unformat_float ( ' -1 023X862567 ' , true ));
}
2012-09-01 14:13:40 +02:00
/**
* Test deleting of users .
*/
public function test_delete_user () {
global $DB , $CFG ;
$this -> resetAfterTest ();
$guest = $DB -> get_record ( 'user' , array ( 'id' => $CFG -> siteguest ), '*' , MUST_EXIST );
$admin = $DB -> get_record ( 'user' , array ( 'id' => $CFG -> siteadmins ), '*' , MUST_EXIST );
$this -> assertEquals ( 0 , $DB -> count_records ( 'user' , array ( 'deleted' => 1 )));
$user = $this -> getDataGenerator () -> create_user ( array ( 'idnumber' => 'abc' ));
$user2 = $this -> getDataGenerator () -> create_user ( array ( 'idnumber' => 'xyz' ));
2015-07-23 11:54:46 +08:00
$usersharedemail1 = $this -> getDataGenerator () -> create_user ( array ( 'email' => 'sharedemail@example.invalid' ));
$usersharedemail2 = $this -> getDataGenerator () -> create_user ( array ( 'email' => 'sharedemail@example.invalid' ));
$useremptyemail1 = $this -> getDataGenerator () -> create_user ( array ( 'email' => '' ));
$useremptyemail2 = $this -> getDataGenerator () -> create_user ( array ( 'email' => '' ));
2012-09-01 14:13:40 +02:00
2013-08-19 17:03:39 +08:00
// Delete user and capture event.
$sink = $this -> redirectEvents ();
2012-09-01 14:13:40 +02:00
$result = delete_user ( $user );
2013-08-19 17:03:39 +08:00
$events = $sink -> get_events ();
$sink -> close ();
$event = array_pop ( $events );
// Test user is deleted in DB.
2012-09-01 14:13:40 +02:00
$this -> assertTrue ( $result );
$deluser = $DB -> get_record ( 'user' , array ( 'id' => $user -> id ), '*' , MUST_EXIST );
$this -> assertEquals ( 1 , $deluser -> deleted );
$this -> assertEquals ( 0 , $deluser -> picture );
$this -> assertSame ( '' , $deluser -> idnumber );
$this -> assertSame ( md5 ( $user -> username ), $deluser -> email );
$this -> assertRegExp ( '/^' . preg_quote ( $user -> email , '/' ) . '\.\d*$/' , $deluser -> username );
$this -> assertEquals ( 1 , $DB -> count_records ( 'user' , array ( 'deleted' => 1 )));
2013-08-19 17:03:39 +08:00
// Test Event.
$this -> assertInstanceOf ( '\core\event\user_deleted' , $event );
$this -> assertSame ( $user -> id , $event -> objectid );
$this -> assertSame ( 'user_deleted' , $event -> get_legacy_eventname ());
$this -> assertEventLegacyData ( $user , $event );
$expectedlogdata = array ( SITEID , 'user' , 'delete' , " view.php?id= $user->id " , $user -> firstname . ' ' . $user -> lastname );
$this -> assertEventLegacyLogData ( $expectedlogdata , $event );
2013-10-28 11:48:40 +08:00
$eventdata = $event -> get_data ();
$this -> assertSame ( $eventdata [ 'other' ][ 'username' ], $user -> username );
$this -> assertSame ( $eventdata [ 'other' ][ 'email' ], $user -> email );
$this -> assertSame ( $eventdata [ 'other' ][ 'idnumber' ], $user -> idnumber );
$this -> assertSame ( $eventdata [ 'other' ][ 'picture' ], $user -> picture );
$this -> assertSame ( $eventdata [ 'other' ][ 'mnethostid' ], $user -> mnethostid );
$this -> assertEquals ( $user , $event -> get_record_snapshot ( 'user' , $event -> objectid ));
2014-01-21 17:04:37 +08:00
$this -> assertEventContextNotUsed ( $event );
2012-09-01 14:13:40 +02:00
2013-08-19 17:03:39 +08:00
// Try invalid params.
2012-09-01 14:13:40 +02:00
$record = new stdClass ();
$record -> grrr = 1 ;
try {
delete_user ( $record );
$this -> fail ( 'Expecting exception for invalid delete_user() $user parameter' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-09-01 14:13:40 +02:00
}
$record -> id = 1 ;
try {
delete_user ( $record );
$this -> fail ( 'Expecting exception for invalid delete_user() $user parameter' );
2013-07-27 21:41:03 +02:00
} catch ( moodle_exception $ex ) {
$this -> assertInstanceOf ( 'coding_exception' , $ex );
2012-09-01 14:13:40 +02:00
}
$record = new stdClass ();
$record -> id = 666 ;
$record -> username = 'xx' ;
$this -> assertFalse ( $DB -> record_exists ( 'user' , array ( 'id' => 666 ))); // Any non-existent id is ok.
$result = delete_user ( $record );
$this -> assertFalse ( $result );
$result = delete_user ( $guest );
$this -> assertFalse ( $result );
$result = delete_user ( $admin );
$this -> assertFalse ( $result );
2012-09-17 12:06:24 +02:00
2015-07-23 11:54:46 +08:00
// Simultaneously deleting users with identical email addresses.
$result1 = delete_user ( $usersharedemail1 );
$result2 = delete_user ( $usersharedemail2 );
$usersharedemail1after = $DB -> get_record ( 'user' , array ( 'id' => $usersharedemail1 -> id ));
$usersharedemail2after = $DB -> get_record ( 'user' , array ( 'id' => $usersharedemail2 -> id ));
$this -> assertTrue ( $result1 );
$this -> assertTrue ( $result2 );
$this -> assertStringStartsWith ( $usersharedemail1 -> email . '.' , $usersharedemail1after -> username );
$this -> assertStringStartsWith ( $usersharedemail2 -> email . '.' , $usersharedemail2after -> username );
// Simultaneously deleting users without email addresses.
$result1 = delete_user ( $useremptyemail1 );
$result2 = delete_user ( $useremptyemail2 );
$useremptyemail1after = $DB -> get_record ( 'user' , array ( 'id' => $useremptyemail1 -> id ));
$useremptyemail2after = $DB -> get_record ( 'user' , array ( 'id' => $useremptyemail2 -> id ));
$this -> assertTrue ( $result1 );
$this -> assertTrue ( $result2 );
$this -> assertStringStartsWith ( $useremptyemail1 -> username . '.' . $useremptyemail1 -> id . '@unknownemail.invalid.' ,
$useremptyemail1after -> username );
$this -> assertStringStartsWith ( $useremptyemail2 -> username . '.' . $useremptyemail2 -> id . '@unknownemail.invalid.' ,
$useremptyemail2after -> username );
2012-09-17 12:06:24 +02:00
$this -> resetDebugging ();
2012-09-01 14:13:40 +02:00
}
2012-09-19 16:15:10 +08:00
2019-12-31 13:18:29 +00:00
/**
* Test deletion of user with long username
*/
public function test_delete_user_long_username () {
global $DB ;
$this -> resetAfterTest ();
// For users without an e-mail, one will be created during deletion using {$username}.{$id}@unknownemail.invalid format.
$user = $this -> getDataGenerator () -> create_user ([
'username' => str_repeat ( 'a' , 75 ),
'email' => '' ,
]);
delete_user ( $user );
// The username for the deleted user shouldn't exceed 100 characters.
$usernamedeleted = $DB -> get_field ( 'user' , 'username' , [ 'id' => $user -> id ]);
$this -> assertEquals ( 100 , core_text :: strlen ( $usernamedeleted ));
$timestrlength = core_text :: strlen (( string ) time ());
// It should start with the user name, and end with the current time.
$this -> assertStringStartsWith ( " { $user -> username } . { $user -> id } @ " , $usernamedeleted );
$this -> assertRegExp ( '/\.\d{' . $timestrlength . '}$/' , $usernamedeleted );
}
/**
* Test deletion of user with long email address
*/
public function test_delete_user_long_email () {
global $DB ;
$this -> resetAfterTest ();
// Create user with 90 character email address.
$user = $this -> getDataGenerator () -> create_user ([
'email' => str_repeat ( 'a' , 78 ) . '@example.com' ,
]);
delete_user ( $user );
// The username for the deleted user shouldn't exceed 100 characters.
$usernamedeleted = $DB -> get_field ( 'user' , 'username' , [ 'id' => $user -> id ]);
$this -> assertEquals ( 100 , core_text :: strlen ( $usernamedeleted ));
$timestrlength = core_text :: strlen (( string ) time ());
// Max username length is 100 chars. Select up to limit - (length of current time + 1 [period character]) from users email.
$expectedemail = core_text :: substr ( $user -> email , 0 , 100 - ( $timestrlength + 1 ));
$this -> assertRegExp ( '/^' . preg_quote ( $expectedemail ) . '\.\d{' . $timestrlength . '}$/' , $usernamedeleted );
}
2012-09-19 16:15:10 +08:00
/**
* Test function convert_to_array ()
*/
public function test_convert_to_array () {
2013-07-27 21:41:03 +02:00
// Check that normal classes are converted to arrays the same way as (array) would do.
2012-09-19 16:15:10 +08:00
$obj = new stdClass ();
$obj -> prop1 = 'hello' ;
$obj -> prop2 = array ( 'first' , 'second' , 13 );
$obj -> prop3 = 15 ;
$this -> assertEquals ( convert_to_array ( $obj ), ( array ) $obj );
2013-07-27 21:41:03 +02:00
// Check that context object (with iterator) is converted to array properly.
2013-07-03 14:39:10 +08:00
$obj = context_system :: instance ();
2012-09-19 16:15:10 +08:00
$ar = array (
'id' => $obj -> id ,
'contextlevel' => $obj -> contextlevel ,
'instanceid' => $obj -> instanceid ,
'path' => $obj -> path ,
2018-06-07 14:35:26 +08:00
'depth' => $obj -> depth ,
'locked' => $obj -> locked ,
2012-09-19 16:15:10 +08:00
);
$this -> assertEquals ( convert_to_array ( $obj ), $ar );
}
2012-09-20 17:18:33 +08:00
/**
* Test the function date_format_string () .
*/
2013-07-27 21:41:03 +02:00
public function test_date_format_string () {
2013-01-30 22:30:28 +01:00
global $CFG ;
2015-03-28 18:46:16 +13:00
$this -> resetAfterTest ();
$this -> setTimezone ( 99 , 'Australia/Perth' );
2012-09-20 17:18:33 +08:00
2012-10-16 17:21:11 +08:00
$tests = array (
array (
'tz' => 99 ,
'str' => '%A, %d %B %Y, %I:%M %p' ,
'expected' => 'Saturday, 01 January 2011, 06:00 PM'
),
array (
'tz' => 0 ,
'str' => '%A, %d %B %Y, %I:%M %p' ,
'expected' => 'Saturday, 01 January 2011, 10:00 AM'
),
array (
2015-03-28 18:46:16 +13:00
// Note: this function expected the timestamp in weird format before,
// since 2.9 it uses UTC.
'tz' => 'Pacific/Auckland' ,
2012-10-16 17:21:11 +08:00
'str' => '%A, %d %B %Y, %I:%M %p' ,
2015-03-28 18:46:16 +13:00
'expected' => 'Saturday, 01 January 2011, 11:00 PM'
2012-10-16 17:21:11 +08:00
),
2013-01-30 22:30:28 +01:00
// Following tests pass on Windows only because en lang pack does
// not contain localewincharset, in real life lang pack maintainers
// may use only characters that are present in localewincharset
// in format strings!
2012-10-16 17:21:11 +08:00
array (
'tz' => 99 ,
'str' => 'Žluťoučký koníček %A' ,
'expected' => 'Žluťoučký koníček Saturday'
),
array (
'tz' => 99 ,
'str' => '言語設定言語 %A' ,
'expected' => '言語設定言語 Saturday'
),
array (
'tz' => 99 ,
'str' => '简体中文简体 %A' ,
'expected' => '简体中文简体 Saturday'
),
);
2012-09-20 17:18:33 +08:00
// Note: date_format_string() uses the timezone only to differenciate
// the server time from the UTC time. It does not modify the timestamp.
// Hence similar results for timezones <= 13.
2012-10-16 17:21:11 +08:00
// On different systems case of AM PM changes so compare case insensitive.
foreach ( $tests as $test ) {
$str = date_format_string ( 1293876000 , $test [ 'str' ], $test [ 'tz' ]);
2013-08-06 20:58:28 +02:00
$this -> assertSame ( core_text :: strtolower ( $test [ 'expected' ]), core_text :: strtolower ( $str ));
2012-10-16 17:21:11 +08:00
}
2012-09-20 17:18:33 +08:00
}
2013-01-08 09:38:11 +13:00
public function test_get_config () {
global $CFG ;
$this -> resetAfterTest ();
// Preparation.
set_config ( 'phpunit_test_get_config_1' , 'test 1' );
set_config ( 'phpunit_test_get_config_2' , 'test 2' , 'mod_forum' );
if ( ! is_array ( $CFG -> config_php_settings )) {
$CFG -> config_php_settings = array ();
}
$CFG -> config_php_settings [ 'phpunit_test_get_config_3' ] = 'test 3' ;
if ( ! is_array ( $CFG -> forced_plugin_settings )) {
$CFG -> forced_plugin_settings = array ();
}
if ( ! array_key_exists ( 'mod_forum' , $CFG -> forced_plugin_settings )) {
$CFG -> forced_plugin_settings [ 'mod_forum' ] = array ();
}
$CFG -> forced_plugin_settings [ 'mod_forum' ][ 'phpunit_test_get_config_4' ] = 'test 4' ;
$CFG -> phpunit_test_get_config_5 = 'test 5' ;
// Testing.
2013-07-27 21:41:03 +02:00
$this -> assertSame ( 'test 1' , get_config ( 'core' , 'phpunit_test_get_config_1' ));
$this -> assertSame ( 'test 2' , get_config ( 'mod_forum' , 'phpunit_test_get_config_2' ));
$this -> assertSame ( 'test 3' , get_config ( 'core' , 'phpunit_test_get_config_3' ));
$this -> assertSame ( 'test 4' , get_config ( 'mod_forum' , 'phpunit_test_get_config_4' ));
2013-01-08 09:38:11 +13:00
$this -> assertFalse ( get_config ( 'core' , 'phpunit_test_get_config_5' ));
$this -> assertFalse ( get_config ( 'core' , 'phpunit_test_get_config_x' ));
$this -> assertFalse ( get_config ( 'mod_forum' , 'phpunit_test_get_config_x' ));
// Test config we know to exist.
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $CFG -> dataroot , get_config ( 'core' , 'dataroot' ));
$this -> assertSame ( $CFG -> phpunit_dataroot , get_config ( 'core' , 'phpunit_dataroot' ));
$this -> assertSame ( $CFG -> dataroot , get_config ( 'core' , 'phpunit_dataroot' ));
$this -> assertSame ( get_config ( 'core' , 'dataroot' ), get_config ( 'core' , 'phpunit_dataroot' ));
2013-01-08 09:38:11 +13:00
// Test setting a config var that already exists.
set_config ( 'phpunit_test_get_config_1' , 'test a' );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( 'test a' , $CFG -> phpunit_test_get_config_1 );
$this -> assertSame ( 'test a' , get_config ( 'core' , 'phpunit_test_get_config_1' ));
2013-01-08 09:38:11 +13:00
// Test cache invalidation.
$cache = cache :: make ( 'core' , 'config' );
MDL-67673 phpunit: Remove deprecated assertInternalType()
While this is not strictly required, because removal will
happen in PHPUnit 9.0, we are already getting rid of all
uses in core.
From release notes:https://phpunit.de/announcements/phpunit-8.html
assertInternalType() is deprecated and will be removed in
PHPUnit 9. Refactor your test to use assertIsArray(), assertIsBool(),
assertIsFloat(), assertIsInt(), assertIsNumeric(), assertIsObject(),
assertIsResource(), assertIsString(), assertIsScalar(),
assertIsCallable(), or assertIsIterable() instead.
2020-08-02 13:08:42 +02:00
$this -> assertIsArray ( $cache -> get ( 'core' ));
$this -> assertIsArray ( $cache -> get ( 'mod_forum' ));
2013-01-08 09:38:11 +13:00
set_config ( 'phpunit_test_get_config_1' , 'test b' );
$this -> assertFalse ( $cache -> get ( 'core' ));
set_config ( 'phpunit_test_get_config_4' , 'test c' , 'mod_forum' );
$this -> assertFalse ( $cache -> get ( 'mod_forum' ));
}
2013-01-21 14:36:57 +08:00
2013-07-27 21:41:03 +02:00
public function test_get_max_upload_sizes () {
2013-01-21 14:36:57 +08:00
// Test with very low limits so we are not affected by php upload limits.
// Test activity limit smallest.
$sitebytes = 102400 ;
$coursebytes = 51200 ;
$modulebytes = 10240 ;
$result = get_max_upload_sizes ( $sitebytes , $coursebytes , $modulebytes );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( 'Activity upload limit (10KB)' , $result [ '0' ]);
$this -> assertCount ( 2 , $result );
2013-01-21 14:36:57 +08:00
// Test course limit smallest.
$sitebytes = 102400 ;
$coursebytes = 10240 ;
$modulebytes = 51200 ;
$result = get_max_upload_sizes ( $sitebytes , $coursebytes , $modulebytes );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( 'Course upload limit (10KB)' , $result [ '0' ]);
$this -> assertCount ( 2 , $result );
2013-01-21 14:36:57 +08:00
// Test site limit smallest.
$sitebytes = 10240 ;
$coursebytes = 102400 ;
$modulebytes = 51200 ;
$result = get_max_upload_sizes ( $sitebytes , $coursebytes , $modulebytes );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( 'Site upload limit (10KB)' , $result [ '0' ]);
$this -> assertCount ( 2 , $result );
2013-01-21 14:36:57 +08:00
// Test site limit not set.
$sitebytes = 0 ;
$coursebytes = 102400 ;
$modulebytes = 51200 ;
$result = get_max_upload_sizes ( $sitebytes , $coursebytes , $modulebytes );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( 'Activity upload limit (50KB)' , $result [ '0' ]);
$this -> assertCount ( 3 , $result );
2013-01-21 14:36:57 +08:00
$sitebytes = 0 ;
$coursebytes = 51200 ;
$modulebytes = 102400 ;
$result = get_max_upload_sizes ( $sitebytes , $coursebytes , $modulebytes );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( 'Course upload limit (50KB)' , $result [ '0' ]);
$this -> assertCount ( 3 , $result );
2013-01-21 14:36:57 +08:00
// Test custom bytes in range.
$sitebytes = 102400 ;
$coursebytes = 51200 ;
$modulebytes = 51200 ;
$custombytes = 10240 ;
$result = get_max_upload_sizes ( $sitebytes , $coursebytes , $modulebytes , $custombytes );
2013-07-27 21:41:03 +02:00
$this -> assertCount ( 3 , $result );
2013-01-21 14:36:57 +08:00
// Test custom bytes in range but non-standard.
$sitebytes = 102400 ;
$coursebytes = 51200 ;
$modulebytes = 51200 ;
$custombytes = 25600 ;
$result = get_max_upload_sizes ( $sitebytes , $coursebytes , $modulebytes , $custombytes );
2013-07-27 21:41:03 +02:00
$this -> assertCount ( 4 , $result );
2013-01-21 14:36:57 +08:00
// Test custom bytes out of range.
$sitebytes = 102400 ;
$coursebytes = 51200 ;
$modulebytes = 51200 ;
$custombytes = 102400 ;
$result = get_max_upload_sizes ( $sitebytes , $coursebytes , $modulebytes , $custombytes );
2013-07-27 21:41:03 +02:00
$this -> assertCount ( 3 , $result );
2013-01-21 14:36:57 +08:00
// Test custom bytes out of range and non-standard.
$sitebytes = 102400 ;
$coursebytes = 51200 ;
$modulebytes = 51200 ;
$custombytes = 256000 ;
$result = get_max_upload_sizes ( $sitebytes , $coursebytes , $modulebytes , $custombytes );
2013-07-27 21:41:03 +02:00
$this -> assertCount ( 3 , $result );
2013-04-27 16:08:15 +02:00
// Test site limit only.
$sitebytes = 51200 ;
$result = get_max_upload_sizes ( $sitebytes );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( 'Site upload limit (50KB)' , $result [ '0' ]);
$this -> assertSame ( '50KB' , $result [ '51200' ]);
$this -> assertSame ( '10KB' , $result [ '10240' ]);
2013-04-27 16:08:15 +02:00
$this -> assertCount ( 3 , $result );
// Test no limit.
$result = get_max_upload_sizes ();
$this -> assertArrayHasKey ( '0' , $result );
$this -> assertArrayHasKey ( get_max_upload_file_size (), $result );
2013-01-21 14:36:57 +08:00
}
2013-02-11 14:51:11 +08:00
2012-09-30 20:58:13 +13:00
/**
* Test function password_is_legacy_hash () .
*/
public function test_password_is_legacy_hash () {
// Well formed md5s should be matched.
foreach ( array ( 'some' , 'strings' , 'to_check!' ) as $string ) {
$md5 = md5 ( $string );
$this -> assertTrue ( password_is_legacy_hash ( $md5 ));
}
// Strings that are not md5s should not be matched.
foreach ( array ( '' , AUTH_PASSWORD_NOT_CACHED , 'IPW8WTcsWNgAWcUS1FBVHegzJnw5M2jOmYkmfc8z.xdBOyC4Caeum' ) as $notmd5 ) {
$this -> assertFalse ( password_is_legacy_hash ( $notmd5 ));
}
}
/**
* Test function validate_internal_user_password () .
*/
public function test_validate_internal_user_password () {
2014-01-07 15:48:06 +08:00
// Test bcrypt hashes.
2013-12-22 17:59:35 +08:00
$validhashes = array (
'pw' => '$2y$10$LOSDi5eaQJhutSRun.OVJ.ZSxQZabCMay7TO1KmzMkDMPvU40zGXK' ,
'abc' => '$2y$10$VWTOhVdsBbWwtdWNDRHSpewjd3aXBQlBQf5rBY/hVhw8hciarFhXa' ,
'C0mP1eX_&}<?@*&%` |\"' => '$2y$10$3PJf.q.9ywNJlsInPbqc8.IFeSsvXrGvQLKRFBIhVu1h1I3vpIry6' ,
'ĩńťėŕňăţĩōŋāĹ' => '$2y$10$3A2Y8WpfRAnP3czJiSv6N.6Xp0T8hW3QZz2hUCYhzyWr1kGP1yUve'
);
2012-09-30 20:58:13 +13:00
foreach ( $validhashes as $password => $hash ) {
$user = new stdClass ();
$user -> auth = 'manual' ;
$user -> password = $hash ;
// The correct password should be validated.
$this -> assertTrue ( validate_internal_user_password ( $user , $password ));
// An incorrect password should not be validated.
$this -> assertFalse ( validate_internal_user_password ( $user , 'badpw' ));
}
}
/**
* Test function hash_internal_user_password () .
*/
public function test_hash_internal_user_password () {
$passwords = array ( 'pw' , 'abc123' , 'C0mP1eX_&}<?@*&%` |\"' , 'ĩńťėŕňăţĩōŋāĹ' );
// Check that some passwords that we convert to hashes can
// be validated.
foreach ( $passwords as $password ) {
$hash = hash_internal_user_password ( $password );
$fasthash = hash_internal_user_password ( $password , true );
$user = new stdClass ();
$user -> auth = 'manual' ;
$user -> password = $hash ;
$this -> assertTrue ( validate_internal_user_password ( $user , $password ));
2014-01-07 15:48:06 +08:00
// They should not be in md5 format.
2013-12-22 17:59:35 +08:00
$this -> assertFalse ( password_is_legacy_hash ( $hash ));
// Check that cost factor in hash is correctly set.
$this -> assertRegExp ( '/\$10\$/' , $hash );
$this -> assertRegExp ( '/\$04\$/' , $fasthash );
2012-09-30 20:58:13 +13:00
}
}
/**
* Test function update_internal_user_password () .
*/
public function test_update_internal_user_password () {
global $DB ;
$this -> resetAfterTest ();
$passwords = array ( 'password' , '1234' , 'changeme' , '****' );
foreach ( $passwords as $password ) {
$user = $this -> getDataGenerator () -> create_user ( array ( 'auth' => 'manual' ));
update_internal_user_password ( $user , $password );
// The user object should have been updated.
$this -> assertTrue ( validate_internal_user_password ( $user , $password ));
// The database field for the user should also have been updated to the
// same value.
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $user -> password , $DB -> get_field ( 'user' , 'password' , array ( 'id' => $user -> id )));
2012-09-30 20:58:13 +13:00
}
$user = $this -> getDataGenerator () -> create_user ( array ( 'auth' => 'manual' ));
// Manually set the user's password to the md5 of the string 'password'.
$DB -> set_field ( 'user' , 'password' , '5f4dcc3b5aa765d61d8327deb882cf99' , array ( 'id' => $user -> id ));
2014-06-05 13:41:20 +08:00
$sink = $this -> redirectEvents ();
2012-09-30 20:58:13 +13:00
// Update the password.
update_internal_user_password ( $user , 'password' );
2014-06-05 13:41:20 +08:00
$events = $sink -> get_events ();
$sink -> close ();
$event = array_pop ( $events );
2012-09-30 20:58:13 +13:00
2014-01-07 15:48:06 +08:00
// Password should have been updated to a bcrypt hash.
2013-12-22 17:59:35 +08:00
$this -> assertFalse ( password_is_legacy_hash ( $user -> password ));
2014-06-05 13:41:20 +08:00
// Verify event information.
$this -> assertInstanceOf ( '\core\event\user_password_updated' , $event );
$this -> assertSame ( $user -> id , $event -> relateduserid );
$this -> assertEquals ( context_user :: instance ( $user -> id ), $event -> get_context ());
$this -> assertEventContextNotUsed ( $event );
2014-08-15 17:17:17 +08:00
// Verify recovery of property 'auth'.
unset ( $user -> auth );
update_internal_user_password ( $user , 'newpassword' );
$this -> assertDebuggingCalled ( 'User record in update_internal_user_password() must include field auth' ,
DEBUG_DEVELOPER );
$this -> assertEquals ( 'manual' , $user -> auth );
2012-09-30 20:58:13 +13:00
}
2013-04-24 10:12:42 +08:00
2014-08-01 13:36:55 -07:00
/**
* Testing that if the password is not cached , that it does not update
* the user table and fire event .
*/
public function test_update_internal_user_password_no_cache () {
2020-04-06 12:36:35 +02:00
global $DB ;
2014-08-01 13:36:55 -07:00
$this -> resetAfterTest ();
$user = $this -> getDataGenerator () -> create_user ( array ( 'auth' => 'cas' ));
2020-04-06 12:36:35 +02:00
$DB -> update_record ( 'user' , [ 'id' => $user -> id , 'password' => AUTH_PASSWORD_NOT_CACHED ]);
$user -> password = AUTH_PASSWORD_NOT_CACHED ;
2014-08-01 13:36:55 -07:00
$sink = $this -> redirectEvents ();
update_internal_user_password ( $user , 'wonkawonka' );
$this -> assertEquals ( 0 , $sink -> count (), 'User updated event should not fire' );
}
/**
* Test if the user has a password hash , but now their auth method
* says not to cache it . Then it should update .
*/
public function test_update_internal_user_password_update_no_cache () {
$this -> resetAfterTest ();
$user = $this -> getDataGenerator () -> create_user ( array ( 'password' => 'test' ));
$this -> assertNotEquals ( AUTH_PASSWORD_NOT_CACHED , $user -> password );
$user -> auth = 'cas' ; // Change to a auth that does not store passwords.
$sink = $this -> redirectEvents ();
update_internal_user_password ( $user , 'wonkawonka' );
$this -> assertGreaterThanOrEqual ( 1 , $sink -> count (), 'User updated event should fire' );
$this -> assertEquals ( AUTH_PASSWORD_NOT_CACHED , $user -> password );
}
2013-04-24 10:12:42 +08:00
public function test_fullname () {
global $CFG ;
$this -> resetAfterTest ();
// Create a user to test the name display on.
$record = array ();
$record [ 'firstname' ] = 'Scott' ;
$record [ 'lastname' ] = 'Fletcher' ;
$record [ 'firstnamephonetic' ] = 'スコット' ;
$record [ 'lastnamephonetic' ] = 'フレチャー' ;
$record [ 'alternatename' ] = 'No friends' ;
$user = $this -> getDataGenerator () -> create_user ( $record );
2013-07-27 21:41:03 +02:00
// Back up config settings for restore later.
2013-04-24 10:12:42 +08:00
$originalcfg = new stdClass ();
$originalcfg -> fullnamedisplay = $CFG -> fullnamedisplay ;
2014-09-16 10:57:28 +08:00
$originalcfg -> alternativefullnameformat = $CFG -> alternativefullnameformat ;
2013-04-24 10:12:42 +08:00
// Testing existing fullnamedisplay settings.
$CFG -> fullnamedisplay = 'firstname' ;
$testname = fullname ( $user );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $user -> firstname , $testname );
2013-04-24 10:12:42 +08:00
$CFG -> fullnamedisplay = 'firstname lastname' ;
$expectedname = " $user->firstname $user->lastname " ;
$testname = fullname ( $user );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $expectedname , $testname );
2013-04-24 10:12:42 +08:00
$CFG -> fullnamedisplay = 'lastname firstname' ;
$expectedname = " $user->lastname $user->firstname " ;
$testname = fullname ( $user );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $expectedname , $testname );
2013-04-24 10:12:42 +08:00
$expectedname = get_string ( 'fullnamedisplay' , null , $user );
$CFG -> fullnamedisplay = 'language' ;
$testname = fullname ( $user );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $expectedname , $testname );
2013-04-24 10:12:42 +08:00
// Test override parameter.
$CFG -> fullnamedisplay = 'firstname' ;
$expectedname = " $user->firstname $user->lastname " ;
$testname = fullname ( $user , true );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $expectedname , $testname );
2013-04-24 10:12:42 +08:00
2014-09-16 10:57:28 +08:00
// Test alternativefullnameformat setting.
// Test alternativefullnameformat that has been set to nothing.
$CFG -> alternativefullnameformat = '' ;
$expectedname = " $user->firstname $user->lastname " ;
$testname = fullname ( $user , true );
$this -> assertSame ( $expectedname , $testname );
// Test alternativefullnameformat that has been set to 'language'.
$CFG -> alternativefullnameformat = 'language' ;
$expectedname = " $user->firstname $user->lastname " ;
$testname = fullname ( $user , true );
$this -> assertSame ( $expectedname , $testname );
// Test customising the alternativefullnameformat setting with all additional name fields.
$CFG -> alternativefullnameformat = 'firstname lastname firstnamephonetic lastnamephonetic middlename alternatename' ;
$expectedname = " $user->firstname $user->lastname $user->firstnamephonetic $user->lastnamephonetic $user->middlename $user->alternatename " ;
$testname = fullname ( $user , true );
$this -> assertSame ( $expectedname , $testname );
2013-04-24 10:12:42 +08:00
// Test additional name fields.
$CFG -> fullnamedisplay = 'lastname lastnamephonetic firstname firstnamephonetic' ;
$expectedname = " $user->lastname $user->lastnamephonetic $user->firstname $user->firstnamephonetic " ;
$testname = fullname ( $user );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $expectedname , $testname );
2013-04-24 10:12:42 +08:00
// Test for handling missing data.
$user -> middlename = null ;
// Parenthesis with no data.
$CFG -> fullnamedisplay = 'firstname (middlename) lastname' ;
$expectedname = " $user->firstname $user->lastname " ;
$testname = fullname ( $user );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $expectedname , $testname );
2013-04-24 10:12:42 +08:00
// Extra spaces due to no data.
$CFG -> fullnamedisplay = 'firstname middlename lastname' ;
$expectedname = " $user->firstname $user->lastname " ;
$testname = fullname ( $user );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $expectedname , $testname );
2013-04-24 10:12:42 +08:00
// Regular expression testing.
2013-07-27 21:41:03 +02:00
// Remove some data from the user fields.
2013-04-24 10:12:42 +08:00
$user -> firstnamephonetic = '' ;
$user -> lastnamephonetic = '' ;
// Removing empty brackets and excess whitespace.
// All of these configurations should resolve to just firstname lastname.
$configarray = array ();
$configarray [] = 'firstname lastname [firstnamephonetic lastnamephonetic]' ;
$configarray [] = 'firstname lastname \'middlename\'' ;
$configarray [] = 'firstname "firstnamephonetic" lastname' ;
$configarray [] = 'firstname 「firstnamephonetic」 lastname 「lastnamephonetic」' ;
foreach ( $configarray as $config ) {
$CFG -> fullnamedisplay = $config ;
$expectedname = " $user->firstname $user->lastname " ;
$testname = fullname ( $user );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $expectedname , $testname );
2013-04-24 10:12:42 +08:00
}
// Check to make sure that other characters are left in place.
$configarray = array ();
$configarray [ '0' ] = new stdClass ();
$configarray [ '0' ] -> config = 'lastname firstname, middlename' ;
$configarray [ '0' ] -> expectedname = " $user->lastname $user->firstname , " ;
$configarray [ '1' ] = new stdClass ();
$configarray [ '1' ] -> config = 'lastname firstname + alternatename' ;
$configarray [ '1' ] -> expectedname = " $user->lastname $user->firstname + $user->alternatename " ;
$configarray [ '2' ] = new stdClass ();
$configarray [ '2' ] -> config = 'firstname aka: alternatename' ;
$configarray [ '2' ] -> expectedname = " $user->firstname aka: $user->alternatename " ;
$configarray [ '3' ] = new stdClass ();
$configarray [ '3' ] -> config = 'firstname (alternatename)' ;
$configarray [ '3' ] -> expectedname = " $user->firstname ( $user->alternatename ) " ;
$configarray [ '4' ] = new stdClass ();
$configarray [ '4' ] -> config = 'firstname [alternatename]' ;
$configarray [ '4' ] -> expectedname = " $user->firstname [ $user->alternatename ] " ;
$configarray [ '5' ] = new stdClass ();
$configarray [ '5' ] -> config = 'firstname "lastname"' ;
$configarray [ '5' ] -> expectedname = " $user->firstname \" $user->lastname\ " " ;
foreach ( $configarray as $config ) {
$CFG -> fullnamedisplay = $config -> config ;
$expectedname = $config -> expectedname ;
$testname = fullname ( $user );
2013-07-27 21:41:03 +02:00
$this -> assertSame ( $expectedname , $testname );
2013-04-24 10:12:42 +08:00
}
2013-10-02 16:11:47 +08:00
// Test debugging message displays when
// fullnamedisplay setting is "normal".
$CFG -> fullnamedisplay = 'firstname lastname' ;
unset ( $user );
$user = new stdClass ();
$user -> firstname = 'Stan' ;
$user -> lastname = 'Lee' ;
$namedisplay = fullname ( $user );
$this -> assertDebuggingCalled ();
2013-07-27 21:41:03 +02:00
// Tidy up after we finish testing.
2013-04-24 10:12:42 +08:00
$CFG -> fullnamedisplay = $originalcfg -> fullnamedisplay ;
2014-09-16 10:57:28 +08:00
$CFG -> alternativefullnameformat = $originalcfg -> alternativefullnameformat ;
2013-04-24 10:12:42 +08:00
}
public function test_get_all_user_name_fields () {
$this -> resetAfterTest ();
// Additional names in an array.
2013-10-30 14:41:33 +08:00
$testarray = array ( 'firstnamephonetic' => 'firstnamephonetic' ,
'lastnamephonetic' => 'lastnamephonetic' ,
'middlename' => 'middlename' ,
'alternatename' => 'alternatename' ,
'firstname' => 'firstname' ,
'lastname' => 'lastname' );
2013-04-24 10:12:42 +08:00
$this -> assertEquals ( $testarray , get_all_user_name_fields ());
// Additional names as a string.
$teststring = 'firstnamephonetic,lastnamephonetic,middlename,alternatename,firstname,lastname' ;
$this -> assertEquals ( $teststring , get_all_user_name_fields ( true ));
// Additional names as a string with an alias.
$teststring = 't.firstnamephonetic,t.lastnamephonetic,t.middlename,t.alternatename,t.firstname,t.lastname' ;
$this -> assertEquals ( $teststring , get_all_user_name_fields ( true , 't' ));
2013-10-30 14:41:33 +08:00
2013-10-30 14:41:59 +08:00
// Additional name fields with a prefix - object.
2013-10-30 14:41:33 +08:00
$testarray = array ( 'firstnamephonetic' => 'authorfirstnamephonetic' ,
'lastnamephonetic' => 'authorlastnamephonetic' ,
'middlename' => 'authormiddlename' ,
'alternatename' => 'authoralternatename' ,
'firstname' => 'authorfirstname' ,
'lastname' => 'authorlastname' );
$this -> assertEquals ( $testarray , get_all_user_name_fields ( false , null , 'author' ));
2013-10-30 14:41:59 +08:00
// Additional name fields with an alias and a title - string.
2013-10-30 14:41:33 +08:00
$teststring = 'u.firstnamephonetic AS authorfirstnamephonetic,u.lastnamephonetic AS authorlastnamephonetic,u.middlename AS authormiddlename,u.alternatename AS authoralternatename,u.firstname AS authorfirstname,u.lastname AS authorlastname' ;
$this -> assertEquals ( $teststring , get_all_user_name_fields ( true , 'u' , null , 'author' ));
2014-09-09 14:52:01 +08:00
// Test the order parameter of the function.
// Returning an array.
$testarray = array ( 'firstname' => 'firstname' ,
'lastname' => 'lastname' ,
'firstnamephonetic' => 'firstnamephonetic' ,
'lastnamephonetic' => 'lastnamephonetic' ,
'middlename' => 'middlename' ,
'alternatename' => 'alternatename'
);
$this -> assertEquals ( $testarray , get_all_user_name_fields ( false , null , null , null , true ));
// Returning a string.
$teststring = 'firstname,lastname,firstnamephonetic,lastnamephonetic,middlename,alternatename' ;
$this -> assertEquals ( $teststring , get_all_user_name_fields ( true , null , null , null , true ));
2013-04-24 10:12:42 +08:00
}
public function test_order_in_string () {
$this -> resetAfterTest ();
// Return an array in an order as they are encountered in a string.
$valuearray = array ( 'second' , 'firsthalf' , 'first' );
$formatstring = 'first firsthalf some other text (second)' ;
$expectedarray = array ( '0' => 'first' , '6' => 'firsthalf' , '33' => 'second' );
$this -> assertEquals ( $expectedarray , order_in_string ( $valuearray , $formatstring ));
// Try again with a different order for the format.
$valuearray = array ( 'second' , 'firsthalf' , 'first' );
$formatstring = 'firsthalf first second' ;
$expectedarray = array ( '0' => 'firsthalf' , '10' => 'first' , '16' => 'second' );
$this -> assertEquals ( $expectedarray , order_in_string ( $valuearray , $formatstring ));
// Try again with yet another different order for the format.
$valuearray = array ( 'second' , 'firsthalf' , 'first' );
$formatstring = 'start seconds away second firstquater first firsthalf' ;
$expectedarray = array ( '19' => 'second' , '38' => 'first' , '44' => 'firsthalf' );
$this -> assertEquals ( $expectedarray , order_in_string ( $valuearray , $formatstring ));
}
2013-08-19 17:03:39 +08:00
2013-09-08 08:38:52 +02:00
public function test_complete_user_login () {
2013-10-29 21:49:08 +01:00
global $USER , $DB ;
2013-09-08 08:38:52 +02:00
$this -> resetAfterTest ();
$user = $this -> getDataGenerator () -> create_user ();
$this -> setUser ( 0 );
$sink = $this -> redirectEvents ();
2013-10-29 21:49:08 +01:00
$loginuser = clone ( $user );
$this -> setCurrentTimeStart ();
@ complete_user_login ( $loginuser ); // Hide session header errors.
$this -> assertSame ( $loginuser , $USER );
2013-09-08 08:38:52 +02:00
$this -> assertEquals ( $user -> id , $USER -> id );
$events = $sink -> get_events ();
$sink -> close ();
2014-01-24 09:59:40 +08:00
$this -> assertCount ( 1 , $events );
$event = reset ( $events );
2013-09-08 08:38:52 +02:00
$this -> assertInstanceOf ( '\core\event\user_loggedin' , $event );
$this -> assertEquals ( 'user' , $event -> objecttable );
$this -> assertEquals ( $user -> id , $event -> objectid );
$this -> assertEquals ( context_system :: instance () -> id , $event -> contextid );
2014-01-21 17:04:37 +08:00
$this -> assertEventContextNotUsed ( $event );
2013-10-29 21:49:08 +01:00
$user = $DB -> get_record ( 'user' , array ( 'id' => $user -> id ));
$this -> assertTimeCurrent ( $user -> firstaccess );
$this -> assertTimeCurrent ( $user -> lastaccess );
$this -> assertTimeCurrent ( $USER -> firstaccess );
$this -> assertTimeCurrent ( $USER -> lastaccess );
$this -> assertTimeCurrent ( $USER -> currentlogin );
$this -> assertSame ( sesskey (), $USER -> sesskey );
$this -> assertTimeCurrent ( $USER -> preference [ '_lastloaded' ]);
$this -> assertObjectNotHasAttribute ( 'password' , $USER );
$this -> assertObjectNotHasAttribute ( 'description' , $USER );
2013-09-08 08:38:52 +02:00
}
2013-08-19 17:03:39 +08:00
/**
* Test require_logout .
*/
public function test_require_logout () {
$this -> resetAfterTest ();
$user = $this -> getDataGenerator () -> create_user ();
$this -> setUser ( $user );
$this -> assertTrue ( isloggedin ());
// Logout user and capture event.
$sink = $this -> redirectEvents ();
require_logout ();
$events = $sink -> get_events ();
$sink -> close ();
$event = array_pop ( $events );
// Check if user is logged out.
$this -> assertFalse ( isloggedin ());
// Test Event.
$this -> assertInstanceOf ( '\core\event\user_loggedout' , $event );
$this -> assertSame ( $user -> id , $event -> objectid );
$this -> assertSame ( 'user_logout' , $event -> get_legacy_eventname ());
$this -> assertEventLegacyData ( $user , $event );
$expectedlogdata = array ( SITEID , 'user' , 'logout' , 'view.php?id=' . $event -> objectid . '&course=' . SITEID , $event -> objectid , 0 ,
$event -> objectid );
$this -> assertEventLegacyLogData ( $expectedlogdata , $event );
2014-01-21 17:04:37 +08:00
$this -> assertEventContextNotUsed ( $event );
2013-08-19 17:03:39 +08:00
}
2013-09-29 23:28:50 +02:00
2016-02-15 13:38:30 +11:00
/**
* A data provider for testing email messageid
*/
public function generate_email_messageid_provider () {
return array (
'nopath' => array (
'wwwroot' => 'http://www.example.com' ,
'ids' => array (
'a-custom-id' => '<a-custom-id@www.example.com>' ,
'an-id-with-/-a-slash' => '<an-id-with-%2F-a-slash@www.example.com>' ,
),
),
'path' => array (
'wwwroot' => 'http://www.example.com/path/subdir' ,
'ids' => array (
'a-custom-id' => '<a-custom-id/path/subdir@www.example.com>' ,
'an-id-with-/-a-slash' => '<an-id-with-%2F-a-slash/path/subdir@www.example.com>' ,
),
),
);
}
/**
* Test email message id generation
*
* @ dataProvider generate_email_messageid_provider
*
* @ param string $wwwroot The wwwroot
* @ param array $msgids An array of msgid local parts and the final result
*/
public function test_generate_email_messageid ( $wwwroot , $msgids ) {
global $CFG ;
$this -> resetAfterTest ();
$CFG -> wwwroot = $wwwroot ;
foreach ( $msgids as $local => $final ) {
$this -> assertEquals ( $final , generate_email_messageid ( $local ));
}
}
2016-02-02 18:05:53 +11:00
2020-07-14 17:43:45 +10:00
/**
* Test email with custom headers
*/
public function test_send_email_with_custom_header () {
global $DB , $CFG ;
$this -> preventResetByRollback ();
$this -> resetAfterTest ();
$touser = $this -> getDataGenerator () -> create_user ();
$fromuser = $this -> getDataGenerator () -> create_user ();
$fromuser -> customheaders = 'X-Custom-Header: foo' ;
set_config ( 'allowedemaildomains' , 'example.com' );
set_config ( 'emailheaders' , 'X-Fixed-Header: bar' );
$sink = $this -> redirectEmails ();
email_to_user ( $touser , $fromuser , 'subject' , 'message' );
$emails = $sink -> get_messages ();
$this -> assertCount ( 1 , $emails );
$email = reset ( $emails );
2020-09-01 00:44:13 +02:00
$this -> assertStringContainsString ( 'X-Custom-Header: foo' , $email -> header );
$this -> assertStringContainsString ( " X-Fixed-Header: bar " , $email -> header );
2020-07-14 17:43:45 +10:00
$sink -> clear ();
}
2016-03-02 19:45:51 +11:00
/**
* A data provider for testing email diversion
*/
2016-02-02 18:05:53 +11:00
public function diverted_emails_provider () {
return array (
'nodiverts' => array (
'divertallemailsto' => null ,
'divertallemailsexcept' => null ,
array (
'foo@example.com' ,
'test@real.com' ,
'fred.jones@example.com' ,
'dev1@dev.com' ,
'fred@example.com' ,
'fred+verp@example.com' ,
),
false ,
),
'alldiverts' => array (
'divertallemailsto' => 'somewhere@elsewhere.com' ,
'divertallemailsexcept' => null ,
array (
'foo@example.com' ,
'test@real.com' ,
'fred.jones@example.com' ,
'dev1@dev.com' ,
'fred@example.com' ,
'fred+verp@example.com' ,
),
true ,
),
'alsodiverts' => array (
'divertallemailsto' => 'somewhere@elsewhere.com' ,
'divertallemailsexcept' => '@dev.com, fred(\+.*)?@example.com' ,
array (
'foo@example.com' ,
'test@real.com' ,
'fred.jones@example.com' ,
),
true ,
),
'divertsexceptions' => array (
'divertallemailsto' => 'somewhere@elsewhere.com' ,
'divertallemailsexcept' => '@dev.com, fred(\+.*)?@example.com' ,
array (
'dev1@dev.com' ,
'fred@example.com' ,
'fred+verp@example.com' ,
),
false ,
),
2020-09-03 11:10:48 +10:00
'divertsexceptionsnewline' => array (
'divertallemailsto' => 'somewhere@elsewhere.com' ,
'divertallemailsexcept' => " @dev.com \n fred( \ +.*)?@example.com " ,
array (
'dev1@dev.com' ,
'fred@example.com' ,
'fred+verp@example.com' ,
),
false ,
),
'alsodivertsnewline' => array (
'divertallemailsto' => 'somewhere@elsewhere.com' ,
'divertallemailsexcept' => " @dev.com \n fred( \ +.*)?@example.com " ,
array (
'foo@example.com' ,
'test@real.com' ,
'fred.jones@example.com' ,
),
true ,
),
2016-02-02 18:05:53 +11:00
);
}
/**
2016-03-02 19:45:51 +11:00
* Test email diversion
*
2016-02-02 18:05:53 +11:00
* @ dataProvider diverted_emails_provider
2016-03-02 19:45:51 +11:00
*
* @ param string $divertallemailsto An optional email address
* @ param string $divertallemailsexcept An optional exclusion list
* @ param array $addresses An array of test addresses
* @ param boolean $expected Expected result
2016-02-02 18:05:53 +11:00
*/
public function test_email_should_be_diverted ( $divertallemailsto , $divertallemailsexcept , $addresses , $expected ) {
global $CFG ;
$this -> resetAfterTest ();
$CFG -> divertallemailsto = $divertallemailsto ;
$CFG -> divertallemailsexcept = $divertallemailsexcept ;
foreach ( $addresses as $address ) {
$this -> assertEquals ( $expected , email_should_be_diverted ( $address ));
}
}
2013-09-29 23:28:50 +02:00
public function test_email_to_user () {
2013-10-09 23:19:46 +02:00
global $CFG ;
2013-09-29 23:28:50 +02:00
$this -> resetAfterTest ();
2019-07-23 17:12:45 +08:00
$user1 = $this -> getDataGenerator () -> create_user ( array ( 'maildisplay' => 1 , 'mailformat' => 0 ));
$user2 = $this -> getDataGenerator () -> create_user ( array ( 'maildisplay' => 1 , 'mailformat' => 1 ));
2016-10-21 12:26:27 +08:00
$user3 = $this -> getDataGenerator () -> create_user ( array ( 'maildisplay' => 0 ));
2017-02-23 10:13:33 +08:00
set_config ( 'allowedemaildomains' , " example.com \r \n moodle.org " );
2013-09-29 23:28:50 +02:00
$subject = 'subject' ;
$messagetext = 'message text' ;
$subject2 = 'subject 2' ;
2019-07-23 17:12:45 +08:00
$messagetext2 = '<b>message text 2</b>' ;
2013-09-29 23:28:50 +02:00
2014-11-12 15:51:18 +08:00
// Close the default email sink.
$sink = $this -> redirectEmails ();
$sink -> close ();
$CFG -> noemailever = true ;
2013-10-09 23:19:46 +02:00
$this -> assertNotEmpty ( $CFG -> noemailever );
email_to_user ( $user1 , $user2 , $subject , $messagetext );
$this -> assertDebuggingCalled ( 'Not sending email due to $CFG->noemailever config setting' );
2013-09-29 23:28:50 +02:00
unset_config ( 'noemailever' );
2013-10-09 23:19:46 +02:00
email_to_user ( $user1 , $user2 , $subject , $messagetext );
$this -> assertDebuggingCalled ( 'Unit tests must not send real emails! Use $this->redirectEmails()' );
2013-09-29 23:28:50 +02:00
$sink = $this -> redirectEmails ();
email_to_user ( $user1 , $user2 , $subject , $messagetext );
email_to_user ( $user2 , $user1 , $subject2 , $messagetext2 );
$this -> assertSame ( 2 , $sink -> count ());
$result = $sink -> get_messages ();
$this -> assertCount ( 2 , $result );
$sink -> close ();
$this -> assertSame ( $subject , $result [ 0 ] -> subject );
$this -> assertSame ( $messagetext , trim ( $result [ 0 ] -> body ));
$this -> assertSame ( $user1 -> email , $result [ 0 ] -> to );
$this -> assertSame ( $user2 -> email , $result [ 0 ] -> from );
2020-09-01 00:44:13 +02:00
$this -> assertStringContainsString ( 'Content-Type: text/plain' , $result [ 0 ] -> header );
2013-09-29 23:28:50 +02:00
$this -> assertSame ( $subject2 , $result [ 1 ] -> subject );
2020-09-01 00:44:13 +02:00
$this -> assertStringContainsString ( $messagetext2 , quoted_printable_decode ( $result [ 1 ] -> body ));
2013-09-29 23:28:50 +02:00
$this -> assertSame ( $user2 -> email , $result [ 1 ] -> to );
$this -> assertSame ( $user1 -> email , $result [ 1 ] -> from );
2020-09-01 00:44:13 +02:00
$this -> assertStringNotContainsString ( 'Content-Type: text/plain' , $result [ 1 ] -> header );
2013-10-09 23:19:46 +02:00
email_to_user ( $user1 , $user2 , $subject , $messagetext );
$this -> assertDebuggingCalled ( 'Unit tests must not send real emails! Use $this->redirectEmails()' );
2014-04-16 12:20:25 +01:00
2016-10-21 12:26:27 +08:00
// Test that an empty noreplyaddress will default to a no-reply address.
$sink = $this -> redirectEmails ();
email_to_user ( $user1 , $user3 , $subject , $messagetext );
$result = $sink -> get_messages ();
$this -> assertEquals ( $CFG -> noreplyaddress , $result [ 0 ] -> from );
$sink -> close ();
set_config ( 'noreplyaddress' , '' );
$sink = $this -> redirectEmails ();
email_to_user ( $user1 , $user3 , $subject , $messagetext );
$result = $sink -> get_messages ();
$this -> assertEquals ( 'noreply@www.example.com' , $result [ 0 ] -> from );
$sink -> close ();
// Test $CFG->allowedemaildomains.
set_config ( 'noreplyaddress' , 'noreply@www.example.com' );
$this -> assertNotEmpty ( $CFG -> allowedemaildomains );
2014-04-16 12:20:25 +01:00
$sink = $this -> redirectEmails ();
email_to_user ( $user1 , $user2 , $subject , $messagetext );
2016-10-21 12:26:27 +08:00
unset_config ( 'allowedemaildomains' );
2014-04-16 12:20:25 +01:00
email_to_user ( $user1 , $user2 , $subject , $messagetext );
$result = $sink -> get_messages ();
2016-10-21 12:26:27 +08:00
$this -> assertNotEquals ( $CFG -> noreplyaddress , $result [ 0 ] -> from );
$this -> assertEquals ( $CFG -> noreplyaddress , $result [ 1 ] -> from );
2014-04-16 12:20:25 +01:00
$sink -> close ();
2017-03-02 10:34:45 +08:00
// Try to send an unsafe attachment, we should see an error message in the eventual mail body.
$attachment = '../test.txt' ;
$attachname = 'txt' ;
$sink = $this -> redirectEmails ();
email_to_user ( $user1 , $user2 , $subject , $messagetext , '' , $attachment , $attachname );
$this -> assertSame ( 1 , $sink -> count ());
$result = $sink -> get_messages ();
$this -> assertCount ( 1 , $result );
2020-09-01 00:44:13 +02:00
$this -> assertStringContainsString ( 'error.txt' , $result [ 0 ] -> body );
$this -> assertStringContainsString ( 'Error in attachment. User attempted to attach a filename with a unsafe name.' , $result [ 0 ] -> body );
2017-03-02 10:34:45 +08:00
$sink -> close ();
2013-09-29 23:28:50 +02:00
}
2013-10-07 17:27:45 +08:00
2020-05-06 21:08:15 +01:00
/**
* Data provider for { @ see test_email_to_user_attachment }
*
* @ return array
*/
public function email_to_user_attachment_provider () : array {
global $CFG ;
// Return all paths that can be used to send attachments from.
return [
'cachedir' => [ $CFG -> cachedir ],
'dataroot' => [ $CFG -> dataroot ],
'dirroot' => [ $CFG -> dirroot ],
'localcachedir' => [ $CFG -> localcachedir ],
'tempdir' => [ $CFG -> tempdir ],
2020-11-05 17:01:54 +01:00
// Paths within $CFG->localrequestdir.
'localrequestdir_request_directory' => [ make_request_directory ()],
'localrequestdir_request_storage_directory' => [ get_request_storage_directory ()],
2020-05-06 21:08:15 +01:00
// Pass null to indicate we want to test a path relative to $CFG->dataroot.
'relative' => [ null ]
];
}
/**
* Test sending attachments with email_to_user
*
* @ param string | null $filedir
*
* @ dataProvider email_to_user_attachment_provider
*/
public function test_email_to_user_attachment ( ? string $filedir ) : void {
global $CFG ;
// If $filedir is null, then write our test file to $CFG->dataroot.
$filepath = ( $filedir ? : $CFG -> dataroot ) . '/hello.txt' ;
file_put_contents ( $filepath , 'Hello' );
$user = core_user :: get_support_user ();
$message = 'Test attachment path' ;
// Create sink to catch all sent e-mails.
$sink = $this -> redirectEmails ();
// Attachment path will be that of the test file if $filedir was passed, otherwise the relative path from $CFG->dataroot.
$filename = basename ( $filepath );
$attachmentpath = $filedir ? $filepath : $filename ;
email_to_user ( $user , $user , $message , $message , $message , $attachmentpath , $filename );
$messages = $sink -> get_messages ();
$sink -> close ();
$this -> assertCount ( 1 , $messages );
// Verify attachment in message body (attachment is in MIME format, but we can detect some Content fields).
$messagebody = reset ( $messages ) -> body ;
2021-01-19 09:08:44 +02:00
$this -> assertStringContainsString ( 'Content-Type: text/plain; name=' . $filename , $messagebody );
2020-09-01 00:44:13 +02:00
$this -> assertStringContainsString ( 'Content-Disposition: attachment; filename=' . $filename , $messagebody );
2020-05-06 21:08:15 +01:00
// Cleanup.
unlink ( $filepath );
}
/**
* Test sending an attachment that doesn ' t exist to email_to_user
*/
public function test_email_to_user_attachment_missing () : void {
$user = core_user :: get_support_user ();
$message = 'Test attachment path' ;
// Create sink to catch all sent e-mails.
$sink = $this -> redirectEmails ();
$attachmentpath = '/hola/hello.txt' ;
$filename = basename ( $attachmentpath );
email_to_user ( $user , $user , $message , $message , $message , $attachmentpath , $filename );
$messages = $sink -> get_messages ();
$sink -> close ();
$this -> assertCount ( 1 , $messages );
// Verify attachment not in message body (attachment is in MIME format, but we can detect some Content fields).
$messagebody = reset ( $messages ) -> body ;
2020-09-01 00:44:13 +02:00
$this -> assertStringNotContainsString ( 'Content-Type: text/plain; name="' . $filename . '"' , $messagebody );
$this -> assertStringNotContainsString ( 'Content-Disposition: attachment; filename=' . $filename , $messagebody );
2020-05-06 21:08:15 +01:00
}
2013-10-07 17:27:45 +08:00
/**
2014-06-05 13:41:20 +08:00
* Test setnew_password_and_mail .
2013-10-07 17:27:45 +08:00
*/
2014-06-05 13:41:20 +08:00
public function test_setnew_password_and_mail () {
2013-10-07 17:27:45 +08:00
global $DB , $CFG ;
$this -> resetAfterTest ();
$user = $this -> getDataGenerator () -> create_user ();
// Update user password.
$sink = $this -> redirectEvents ();
$sink2 = $this -> redirectEmails (); // Make sure we are redirecting emails.
setnew_password_and_mail ( $user );
$events = $sink -> get_events ();
$sink -> close ();
$sink2 -> close ();
2014-06-05 13:41:20 +08:00
$event = array_pop ( $events );
2013-10-07 17:27:45 +08:00
// Test updated value.
$dbuser = $DB -> get_record ( 'user' , array ( 'id' => $user -> id ));
$this -> assertSame ( $user -> firstname , $dbuser -> firstname );
2014-06-05 13:41:20 +08:00
$this -> assertNotEmpty ( $dbuser -> password );
2013-10-07 17:27:45 +08:00
// Test event.
2014-06-05 13:41:20 +08:00
$this -> assertInstanceOf ( '\core\event\user_password_updated' , $event );
$this -> assertSame ( $user -> id , $event -> relateduserid );
$this -> assertEquals ( context_user :: instance ( $user -> id ), $event -> get_context ());
$this -> assertEventContextNotUsed ( $event );
2013-10-07 17:27:45 +08:00
}
2016-12-07 11:43:54 +08:00
/**
* Data provider for test_generate_confirmation_link
* @ return Array of confirmation urls and expected resultant confirmation links
*/
public function generate_confirmation_link_provider () {
global $CFG ;
return [
" Simple name " => [
" username " => " simplename " ,
" confirmationurl " => null ,
" expected " => $CFG -> wwwroot . " /login/confirm.php?data=/simplename "
],
" Period in between words in username " => [
" username " => " period.inbetween " ,
" confirmationurl " => null ,
" expected " => $CFG -> wwwroot . " /login/confirm.php?data=/period%2Einbetween "
],
" Trailing periods in username " => [
" username " => " trailingperiods... " ,
" confirmationurl " => null ,
" expected " => $CFG -> wwwroot . " /login/confirm.php?data=/trailingperiods%2E%2E%2E "
],
" At symbol in username " => [
" username " => " at@symbol " ,
" confirmationurl " => null ,
" expected " => $CFG -> wwwroot . " /login/confirm.php?data=/at%40symbol "
],
" Dash symbol in username " => [
" username " => " has-dash " ,
" confirmationurl " => null ,
" expected " => $CFG -> wwwroot . " /login/confirm.php?data=/has-dash "
],
" Underscore in username " => [
" username " => " under_score " ,
" confirmationurl " => null ,
" expected " => $CFG -> wwwroot . " /login/confirm.php?data=/under_score "
],
" Many different characters in username " => [
" username " => " many_-.@characters@_@-..-.. " ,
" confirmationurl " => null ,
" expected " => $CFG -> wwwroot . " /login/confirm.php?data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E "
],
" Custom relative confirmation url " => [
" username " => " many_-.@characters@_@-..-.. " ,
" confirmationurl " => " /custom/local/url.php " ,
" expected " => $CFG -> wwwroot . " /custom/local/url.php?data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E "
],
" Custom relative confirmation url with parameters " => [
" username " => " many_-.@characters@_@-..-.. " ,
" confirmationurl " => " /custom/local/url.php?with=param " ,
" expected " => $CFG -> wwwroot . " /custom/local/url.php?with=param&data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E "
],
" Custom local confirmation url " => [
" username " => " many_-.@characters@_@-..-.. " ,
" confirmationurl " => $CFG -> wwwroot . " /custom/local/url.php " ,
" expected " => $CFG -> wwwroot . " /custom/local/url.php?data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E "
],
" Custom local confirmation url with parameters " => [
" username " => " many_-.@characters@_@-..-.. " ,
" confirmationurl " => $CFG -> wwwroot . " /custom/local/url.php?with=param " ,
" expected " => $CFG -> wwwroot . " /custom/local/url.php?with=param&data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E "
],
" Custom external confirmation url " => [
" username " => " many_-.@characters@_@-..-.. " ,
" confirmationurl " => " http://moodle.org/custom/external/url.php " ,
" expected " => " http://moodle.org/custom/external/url.php?data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E "
],
" Custom external confirmation url with parameters " => [
" username " => " many_-.@characters@_@-..-.. " ,
" confirmationurl " => " http://moodle.org/ext.php?with=some¶m=eters " ,
" expected " => " http://moodle.org/ext.php?with=some¶m=eters&data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E "
],
" Custom external confirmation url with parameters " => [
" username " => " many_-.@characters@_@-..-.. " ,
" confirmationurl " => " http://moodle.org/ext.php?with=some&data=test " ,
" expected " => " http://moodle.org/ext.php?with=some&data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E "
],
];
}
/**
* Test generate_confirmation_link
* @ dataProvider generate_confirmation_link_provider
* @ param string $username The name of the user
* @ param string $confirmationurl The url the user should go to to confirm
* @ param string $expected The expected url of the confirmation link
*/
public function test_generate_confirmation_link ( $username , $confirmationurl , $expected ) {
$this -> resetAfterTest ();
$sink = $this -> redirectEmails ();
$user = $this -> getDataGenerator () -> create_user (
[
" username " => $username ,
2020-04-06 12:36:35 +02:00
" confirmed " => 0 ,
2016-12-07 11:43:54 +08:00
" email " => 'test@example.com' ,
]
);
send_confirmation_email ( $user , $confirmationurl );
$sink -> close ();
$messages = $sink -> get_messages ();
$message = array_shift ( $messages );
$messagebody = quoted_printable_decode ( $message -> body );
2020-09-01 00:44:13 +02:00
$this -> assertStringContainsString ( $expected , $messagebody );
2016-12-07 11:43:54 +08:00
}
/**
* Test generate_confirmation_link with custom admin link
*/
public function test_generate_confirmation_link_with_custom_admin () {
global $CFG ;
$this -> resetAfterTest ();
$sink = $this -> redirectEmails ();
$admin = $CFG -> admin ;
$CFG -> admin = 'custom/admin/path' ;
$user = $this -> getDataGenerator () -> create_user (
[
" username " => " many_-.@characters@_@-..-.. " ,
2020-04-06 12:36:35 +02:00
" confirmed " => 0 ,
2016-12-07 11:43:54 +08:00
" email " => 'test@example.com' ,
]
);
$confirmationurl = " /admin/test.php?with=params " ;
$expected = $CFG -> wwwroot . " / " . $CFG -> admin . " /test.php?with=params&data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E " ;
send_confirmation_email ( $user , $confirmationurl );
$sink -> close ();
$messages = $sink -> get_messages ();
$message = array_shift ( $messages );
$messagebody = quoted_printable_decode ( $message -> body );
$sink -> close ();
2020-09-01 00:44:13 +02:00
$this -> assertStringContainsString ( $expected , $messagebody );
2016-12-07 11:43:54 +08:00
$CFG -> admin = $admin ;
}
2013-11-08 14:26:02 +08:00
/**
* Test remove_course_content deletes course contents
* TODO Add asserts to verify other data related to course is deleted as well .
*/
public function test_remove_course_contents () {
$this -> resetAfterTest ();
$course = $this -> getDataGenerator () -> create_course ();
$user = $this -> getDataGenerator () -> create_user ();
$gen = $this -> getDataGenerator () -> get_plugin_generator ( 'core_notes' );
$note = $gen -> create_instance ( array ( 'courseid' => $course -> id , 'userid' => $user -> id ));
$this -> assertNotEquals ( false , note_load ( $note -> id ));
remove_course_contents ( $course -> id , false );
$this -> assertFalse ( note_load ( $note -> id ));
}
2013-10-30 14:41:33 +08:00
/**
* Test function username_load_fields_from_object () .
*/
public function test_username_load_fields_from_object () {
$this -> resetAfterTest ();
// This object represents the information returned from an sql query.
$userinfo = new stdClass ();
$userinfo -> userid = 1 ;
$userinfo -> username = 'loosebruce' ;
$userinfo -> firstname = 'Bruce' ;
$userinfo -> lastname = 'Campbell' ;
$userinfo -> firstnamephonetic = 'ブルース' ;
$userinfo -> lastnamephonetic = 'カンベッル' ;
$userinfo -> middlename = '' ;
$userinfo -> alternatename = '' ;
$userinfo -> email = '' ;
$userinfo -> picture = 23 ;
2013-10-30 14:41:59 +08:00
$userinfo -> imagealt = 'Michael Jordan draining another basket.' ;
2013-10-30 14:41:33 +08:00
$userinfo -> idnumber = 3982 ;
// Just user name fields.
$user = new stdClass ();
$user = username_load_fields_from_object ( $user , $userinfo );
$expectedarray = new stdClass ();
$expectedarray -> firstname = 'Bruce' ;
$expectedarray -> lastname = 'Campbell' ;
$expectedarray -> firstnamephonetic = 'ブルース' ;
$expectedarray -> lastnamephonetic = 'カンベッル' ;
$expectedarray -> middlename = '' ;
$expectedarray -> alternatename = '' ;
$this -> assertEquals ( $user , $expectedarray );
// User information for showing a picture.
$user = new stdClass ();
2013-11-11 17:18:16 +08:00
$additionalfields = explode ( ',' , user_picture :: fields ());
2013-10-30 14:41:33 +08:00
$user = username_load_fields_from_object ( $user , $userinfo , null , $additionalfields );
2013-11-11 17:18:16 +08:00
$user -> id = $userinfo -> userid ;
2013-10-30 14:41:33 +08:00
$expectedarray = new stdClass ();
$expectedarray -> id = 1 ;
$expectedarray -> firstname = 'Bruce' ;
$expectedarray -> lastname = 'Campbell' ;
$expectedarray -> firstnamephonetic = 'ブルース' ;
$expectedarray -> lastnamephonetic = 'カンベッル' ;
$expectedarray -> middlename = '' ;
$expectedarray -> alternatename = '' ;
$expectedarray -> email = '' ;
$expectedarray -> picture = 23 ;
2013-10-30 14:41:59 +08:00
$expectedarray -> imagealt = 'Michael Jordan draining another basket.' ;
2013-10-30 14:41:33 +08:00
$this -> assertEquals ( $user , $expectedarray );
// Alter the userinfo object to have a prefix.
$userinfo -> authorfirstname = 'Bruce' ;
$userinfo -> authorlastname = 'Campbell' ;
$userinfo -> authorfirstnamephonetic = 'ブルース' ;
$userinfo -> authorlastnamephonetic = 'カンベッル' ;
$userinfo -> authormiddlename = '' ;
2013-11-11 17:18:16 +08:00
$userinfo -> authorpicture = 23 ;
$userinfo -> authorimagealt = 'Michael Jordan draining another basket.' ;
2015-04-14 10:47:05 +08:00
$userinfo -> authoremail = 'test@example.com' ;
2013-11-11 17:18:16 +08:00
2013-10-30 14:41:33 +08:00
// Return an object with user picture information.
$user = new stdClass ();
2013-11-11 17:18:16 +08:00
$additionalfields = explode ( ',' , user_picture :: fields ());
2013-10-30 14:41:33 +08:00
$user = username_load_fields_from_object ( $user , $userinfo , 'author' , $additionalfields );
2013-11-11 17:18:16 +08:00
$user -> id = $userinfo -> userid ;
2013-10-30 14:41:33 +08:00
$expectedarray = new stdClass ();
$expectedarray -> id = 1 ;
$expectedarray -> firstname = 'Bruce' ;
$expectedarray -> lastname = 'Campbell' ;
$expectedarray -> firstnamephonetic = 'ブルース' ;
$expectedarray -> lastnamephonetic = 'カンベッル' ;
$expectedarray -> middlename = '' ;
$expectedarray -> alternatename = '' ;
2015-04-14 10:47:05 +08:00
$expectedarray -> email = 'test@example.com' ;
2013-10-30 14:41:33 +08:00
$expectedarray -> picture = 23 ;
2013-10-30 14:41:59 +08:00
$expectedarray -> imagealt = 'Michael Jordan draining another basket.' ;
2013-10-30 14:41:33 +08:00
$this -> assertEquals ( $user , $expectedarray );
}
2014-03-21 13:20:11 +00:00
/**
2021-01-19 15:39:24 +00:00
* Test function { @ see count_words ()} .
2021-01-19 15:29:30 +00:00
*
* @ dataProvider count_words_testcases
* @ param int $expectedcount number of words in $string .
* @ param string $string the test string to count the words of .
2014-03-21 13:20:11 +00:00
*/
2021-01-19 15:29:30 +00:00
public function test_count_words ( int $expectedcount , string $string ) : void {
$this -> assertEquals ( $expectedcount , count_words ( $string ));
}
2014-03-21 13:20:11 +00:00
2021-01-19 15:29:30 +00:00
/**
* Data provider for { @ see test_count_words } .
*
* @ return array of test cases .
*/
public function count_words_testcases () : array {
2021-02-04 18:17:35 +00:00
// The counts here should match MS Word and Libre Office.
2021-01-19 15:29:30 +00:00
return [
2021-01-19 15:39:24 +00:00
[ 0 , '' ],
[ 4 , 'one two three four' ],
2021-02-04 18:17:35 +00:00
[ 1 , " a'b " ],
[ 1 , '1+1=2' ],
[ 1 , ' one-sided ' ],
[ 2 , 'one two' ],
[ 1 , 'email@example.com' ],
[ 2 , 'first\part second/part' ],
2021-01-19 15:39:24 +00:00
[ 4 , '<p>one two<br></br>three four</p>' ],
[ 4 , '<p>one two<br>three four</p>' ],
[ 4 , '<p>one two<br />three four</p>' ], // XHTML style.
2021-02-04 18:17:35 +00:00
[ 3 , ' one ... three ' ],
[ 1 , 'just...one' ],
[ 3 , ' one & three ' ],
[ 1 , 'just&one' ],
[ 2 , 'em—dash' ],
[ 2 , 'en– dash' ],
2021-01-19 15:29:30 +00:00
[ 4 , '1³ £2 €3.45 $6,789' ],
2021-02-04 18:17:35 +00:00
[ 2 , 'ブルース カンベッル' ], // MS word counts this as 11, but we don't handle that yet.
2021-01-19 15:39:24 +00:00
[ 4 , '<p>one two</p><p>three four</p>' ],
[ 4 , '<p>one two</p><p><br/></p><p>three four</p>' ],
[ 4 , '<p>one</p><ul><li>two</li><li>three</li></ul><p>four.</p>' ],
[ 1 , '<p>em<b>phas</b>is.</p>' ],
[ 1 , '<p>em<i>phas</i>is.</p>' ],
[ 1 , '<p>em<strong>phas</strong>is.</p>' ],
[ 1 , '<p>em<em>phas</em>is.</p>' ],
[ 2 , " one \n two " ],
2021-02-04 18:17:35 +00:00
[ 2 , " one \r two " ],
[ 2 , " one \t two " ],
[ 2 , " one \ vtwo " ],
[ 2 , " one \ ftwo " ],
2021-01-19 15:39:24 +00:00
[ 1 , " SO<sub>4</sub><sup>2-</sup> " ],
2021-02-04 18:17:35 +00:00
[ 6 , '4+4=8 i.e. O(1) a,b,c,d I’ m black&blue_really' ],
2021-01-19 15:29:30 +00:00
];
2014-03-21 13:20:11 +00:00
}
2021-01-19 15:50:05 +00:00
/**
* Test function { @ see count_letters ()} .
*
* @ dataProvider count_letters_testcases
* @ param int $expectedcount number of characters in $string .
* @ param string $string the test string to count the letters of .
*/
public function test_count_letters ( int $expectedcount , string $string ) : void {
$this -> assertEquals ( $expectedcount , count_letters ( $string ));
}
2014-03-21 13:20:11 +00:00
2021-01-19 15:50:05 +00:00
/**
* Data provider for { @ see count_letters_testcases } .
*
* @ return array of test cases .
*/
public function count_letters_testcases () : array {
return [
[ 0 , '' ],
[ 1 , 'x' ],
[ 1 , '&' ],
[ 4 , '<p>frog</p>' ],
];
2014-03-21 13:20:11 +00:00
}
2021-01-19 15:50:05 +00:00
2014-09-11 13:10:29 +01:00
/**
* Tests the getremoteaddr () function .
*/
public function test_getremoteaddr () {
2020-03-05 13:31:32 +08:00
global $CFG ;
$this -> resetAfterTest ();
2020-12-29 13:29:33 -05:00
$CFG -> getremoteaddrconf = null ; // Use default value, GETREMOTEADDR_SKIP_DEFAULT.
$noip = getremoteaddr ( '1.1.1.1' );
$this -> assertEquals ( '1.1.1.1' , $noip );
$remoteaddr = isset ( $_SERVER [ 'REMOTE_ADDR' ]) ? $_SERVER [ 'REMOTE_ADDR' ] : null ;
$_SERVER [ 'REMOTE_ADDR' ] = '127.0.0.1' ;
$singleip = getremoteaddr ();
$this -> assertEquals ( '127.0.0.1' , $singleip );
$_SERVER [ 'REMOTE_ADDR' ] = $remoteaddr ; // Restore server value.
$CFG -> getremoteaddrconf = 0 ; // Don't skip any source.
$noip = getremoteaddr ( '1.1.1.1' );
$this -> assertEquals ( '1.1.1.1' , $noip );
// Populate all $_SERVER values to review order.
$ipsources = [
'HTTP_CLIENT_IP' => '2.2.2.2' ,
'HTTP_X_FORWARDED_FOR' => '3.3.3.3' ,
'REMOTE_ADDR' => '4.4.4.4' ,
];
$originalvalues = [];
foreach ( $ipsources as $source => $ip ) {
$originalvalues [ $source ] = isset ( $_SERVER [ $source ]) ? $_SERVER [ $source ] : null ; // Saving data to restore later.
$_SERVER [ $source ] = $ip ;
}
foreach ( $ipsources as $source => $expectedip ) {
$ip = getremoteaddr ();
$this -> assertEquals ( $expectedip , $ip );
unset ( $_SERVER [ $source ]); // Removing the value so next time we get the following ip.
}
// Restore server values.
foreach ( $originalvalues as $source => $ip ) {
$_SERVER [ $source ] = $ip ;
}
// All $_SERVER values have been removed, we should get the default again.
$noip = getremoteaddr ( '1.1.1.1' );
$this -> assertEquals ( '1.1.1.1' , $noip );
2020-03-05 13:31:32 +08:00
$CFG -> getremoteaddrconf = GETREMOTEADDR_SKIP_HTTP_CLIENT_IP ;
2014-11-11 16:06:30 +08:00
$xforwardedfor = isset ( $_SERVER [ 'HTTP_X_FORWARDED_FOR' ]) ? $_SERVER [ 'HTTP_X_FORWARDED_FOR' ] : null ;
2014-09-11 13:10:29 +01:00
$_SERVER [ 'HTTP_X_FORWARDED_FOR' ] = '' ;
$noip = getremoteaddr ( '1.1.1.1' );
$this -> assertEquals ( '1.1.1.1' , $noip );
$_SERVER [ 'HTTP_X_FORWARDED_FOR' ] = '' ;
$noip = getremoteaddr ();
$this -> assertEquals ( '0.0.0.0' , $noip );
$_SERVER [ 'HTTP_X_FORWARDED_FOR' ] = '127.0.0.1' ;
$singleip = getremoteaddr ();
$this -> assertEquals ( '127.0.0.1' , $singleip );
$_SERVER [ 'HTTP_X_FORWARDED_FOR' ] = '127.0.0.1,127.0.0.2' ;
$twoip = getremoteaddr ();
2020-03-05 13:31:32 +08:00
$this -> assertEquals ( '127.0.0.2' , $twoip );
2014-09-11 13:10:29 +01:00
2020-03-05 13:31:32 +08:00
$_SERVER [ 'HTTP_X_FORWARDED_FOR' ] = '127.0.0.1,127.0.0.2,127.0.0.3' ;
2014-09-11 13:10:29 +01:00
$threeip = getremoteaddr ();
2020-03-05 13:31:32 +08:00
$this -> assertEquals ( '127.0.0.3' , $threeip );
2014-09-11 13:10:29 +01:00
2020-03-05 13:31:32 +08:00
$_SERVER [ 'HTTP_X_FORWARDED_FOR' ] = '127.0.0.1,127.0.0.2:65535' ;
2014-11-11 16:06:30 +08:00
$portip = getremoteaddr ();
2020-03-05 13:31:32 +08:00
$this -> assertEquals ( '127.0.0.2' , $portip );
2014-11-11 16:06:30 +08:00
2020-03-05 13:31:32 +08:00
$_SERVER [ 'HTTP_X_FORWARDED_FOR' ] = '127.0.0.1,0:0:0:0:0:0:0:2' ;
2014-11-11 16:06:30 +08:00
$portip = getremoteaddr ();
2020-03-05 13:31:32 +08:00
$this -> assertEquals ( '0:0:0:0:0:0:0:2' , $portip );
2014-11-11 16:06:30 +08:00
2020-03-05 13:31:32 +08:00
$_SERVER [ 'HTTP_X_FORWARDED_FOR' ] = '127.0.0.1,0::2' ;
2014-11-11 16:06:30 +08:00
$portip = getremoteaddr ();
2020-03-05 13:31:32 +08:00
$this -> assertEquals ( '0:0:0:0:0:0:0:2' , $portip );
2014-11-11 16:06:30 +08:00
2020-03-05 13:31:32 +08:00
$_SERVER [ 'HTTP_X_FORWARDED_FOR' ] = '127.0.0.1,[0:0:0:0:0:0:0:2]:65535' ;
2014-11-11 16:06:30 +08:00
$portip = getremoteaddr ();
2020-03-05 13:31:32 +08:00
$this -> assertEquals ( '0:0:0:0:0:0:0:2' , $portip );
2014-11-11 16:06:30 +08:00
$_SERVER [ 'HTTP_X_FORWARDED_FOR' ] = $xforwardedfor ;
2014-09-11 13:10:29 +01:00
}
2015-08-19 09:33:12 +12:00
/*
* Test emulation of random_bytes () function .
*/
public function test_random_bytes_emulate () {
$result = random_bytes_emulate ( 10 );
$this -> assertSame ( 10 , strlen ( $result ));
$this -> assertnotSame ( $result , random_bytes_emulate ( 10 ));
$result = random_bytes_emulate ( 21 );
$this -> assertSame ( 21 , strlen ( $result ));
$this -> assertnotSame ( $result , random_bytes_emulate ( 21 ));
$result = random_bytes_emulate ( 666 );
$this -> assertSame ( 666 , strlen ( $result ));
2016-07-19 10:39:50 +10:00
$result = random_bytes_emulate ( 40 );
$this -> assertSame ( 40 , strlen ( $result ));
2015-08-19 09:33:12 +12:00
$this -> assertDebuggingNotCalled ();
$result = random_bytes_emulate ( 0 );
$this -> assertSame ( '' , $result );
$this -> assertDebuggingCalled ();
$result = random_bytes_emulate ( - 1 );
$this -> assertSame ( '' , $result );
$this -> assertDebuggingCalled ();
}
/**
* Test function for creation of random strings .
*/
public function test_random_string () {
$pool = 'a-zA-Z0-9' ;
$result = random_string ( 10 );
$this -> assertSame ( 10 , strlen ( $result ));
$this -> assertRegExp ( '/^[' . $pool . ']+$/' , $result );
$this -> assertNotSame ( $result , random_string ( 10 ));
$result = random_string ( 21 );
$this -> assertSame ( 21 , strlen ( $result ));
$this -> assertRegExp ( '/^[' . $pool . ']+$/' , $result );
$this -> assertNotSame ( $result , random_string ( 21 ));
$result = random_string ( 666 );
$this -> assertSame ( 666 , strlen ( $result ));
$this -> assertRegExp ( '/^[' . $pool . ']+$/' , $result );
$result = random_string ();
$this -> assertSame ( 15 , strlen ( $result ));
$this -> assertRegExp ( '/^[' . $pool . ']+$/' , $result );
$this -> assertDebuggingNotCalled ();
$result = random_string ( 0 );
$this -> assertSame ( '' , $result );
$this -> assertDebuggingCalled ();
$result = random_string ( - 1 );
$this -> assertSame ( '' , $result );
$this -> assertDebuggingCalled ();
}
/**
* Test function for creation of complex random strings .
*/
public function test_complex_random_string () {
$pool = preg_quote ( 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`~!@#%^&*()_+-=[];,./<>?:{} ' , '/' );
$result = complex_random_string ( 10 );
$this -> assertSame ( 10 , strlen ( $result ));
$this -> assertRegExp ( '/^[' . $pool . ']+$/' , $result );
$this -> assertNotSame ( $result , complex_random_string ( 10 ));
$result = complex_random_string ( 21 );
$this -> assertSame ( 21 , strlen ( $result ));
$this -> assertRegExp ( '/^[' . $pool . ']+$/' , $result );
$this -> assertNotSame ( $result , complex_random_string ( 21 ));
$result = complex_random_string ( 666 );
$this -> assertSame ( 666 , strlen ( $result ));
$this -> assertRegExp ( '/^[' . $pool . ']+$/' , $result );
$result = complex_random_string ();
2020-09-02 01:14:14 +02:00
$this -> assertEqualsWithDelta ( 28 , strlen ( $result ), 4 ); // Expected length is 24 - 32.
2015-08-19 09:33:12 +12:00
$this -> assertRegExp ( '/^[' . $pool . ']+$/' , $result );
$this -> assertDebuggingNotCalled ();
$result = complex_random_string ( 0 );
$this -> assertSame ( '' , $result );
$this -> assertDebuggingCalled ();
$result = complex_random_string ( - 1 );
$this -> assertSame ( '' , $result );
$this -> assertDebuggingCalled ();
}
2016-02-05 14:56:01 +00:00
/**
* Data provider for private ips .
*/
public function data_private_ips () {
return array (
array ( '10.0.0.0' ),
array ( '172.16.0.0' ),
array ( '192.168.1.0' ),
array ( 'fdfe:dcba:9876:ffff:fdc6:c46b:bb8f:7d4c' ),
array ( 'fdc6:c46b:bb8f:7d4c:fdc6:c46b:bb8f:7d4c' ),
array ( 'fdc6:c46b:bb8f:7d4c:0000:8a2e:0370:7334' ),
array ( '127.0.0.1' ), // This has been buggy in past: https://bugs.php.net/bug.php?id=53150.
);
}
/**
* Checks ip_is_public returns false for private ips .
*
* @ param string $ip the ipaddress to test
* @ dataProvider data_private_ips
*/
public function test_ip_is_public_private_ips ( $ip ) {
$this -> assertFalse ( ip_is_public ( $ip ));
}
/**
* Data provider for public ips .
*/
public function data_public_ips () {
return array (
array ( '2400:cb00:2048:1::8d65:71b3' ),
array ( '2400:6180:0:d0::1b:2001' ),
array ( '141.101.113.179' ),
array ( '123.45.67.178' ),
);
}
/**
* Checks ip_is_public returns true for public ips .
*
* @ param string $ip the ipaddress to test
* @ dataProvider data_public_ips
*/
public function test_ip_is_public_public_ips ( $ip ) {
$this -> assertTrue ( ip_is_public ( $ip ));
}
2016-10-21 12:26:27 +08:00
/**
* Test the function can_send_from_real_email_address
*
* @ param string $email Email address for the from user .
* @ param int $display The user ' s email display preference .
* @ param bool $samecourse Are the users in the same course ?
2017-02-23 10:13:33 +08:00
* @ param string $config The CFG -> allowedemaildomains config values
2016-10-21 12:26:27 +08:00
* @ param bool $result The expected result .
* @ dataProvider data_can_send_from_real_email_address
*/
2017-02-23 10:13:33 +08:00
public function test_can_send_from_real_email_address ( $email , $display , $samecourse , $config , $result ) {
2016-10-21 12:26:27 +08:00
$this -> resetAfterTest ();
$fromuser = $this -> getDataGenerator () -> create_user ();
$touser = $this -> getDataGenerator () -> create_user ();
$course = $this -> getDataGenerator () -> create_course ();
2017-02-23 10:13:33 +08:00
set_config ( 'allowedemaildomains' , $config );
2016-10-21 12:26:27 +08:00
$fromuser -> email = $email ;
$fromuser -> maildisplay = $display ;
if ( $samecourse ) {
$this -> getDataGenerator () -> enrol_user ( $fromuser -> id , $course -> id , 'student' );
$this -> getDataGenerator () -> enrol_user ( $touser -> id , $course -> id , 'student' );
} else {
$this -> getDataGenerator () -> enrol_user ( $fromuser -> id , $course -> id , 'student' );
}
2017-02-23 10:13:33 +08:00
$this -> assertEquals ( $result , can_send_from_real_email_address ( $fromuser , $touser ));
2016-10-21 12:26:27 +08:00
}
/**
* Data provider for test_can_send_from_real_email_address .
*
* @ return array Returns an array of test data for the above function .
*/
public function data_can_send_from_real_email_address () {
return [
// Test from email is in allowed domain.
// Test that from display is set to show no one.
2017-02-23 10:13:33 +08:00
[
'email' => 'fromuser@example.com' ,
'display' => core_user :: MAILDISPLAY_HIDE ,
'samecourse' => false ,
'config' => " example.com \r \n test.com " ,
'result' => false
],
2016-10-21 12:26:27 +08:00
// Test that from display is set to course members only (course member).
2017-02-23 10:13:33 +08:00
[
'email' => 'fromuser@example.com' ,
'display' => core_user :: MAILDISPLAY_COURSE_MEMBERS_ONLY ,
'samecourse' => true ,
'config' => " example.com \r \n test.com " ,
'result' => true
],
2016-10-21 12:26:27 +08:00
// Test that from display is set to course members only (Non course member).
2017-02-23 10:13:33 +08:00
[
'email' => 'fromuser@example.com' ,
'display' => core_user :: MAILDISPLAY_COURSE_MEMBERS_ONLY ,
'samecourse' => false ,
'config' => " example.com \r \n test.com " ,
'result' => false
],
// Test that from display is set to show everyone.
[
'email' => 'fromuser@example.com' ,
'display' => core_user :: MAILDISPLAY_EVERYONE ,
'samecourse' => false ,
'config' => " example.com \r \n test.com " ,
'result' => true
],
// Test a few different config value formats for parsing correctness.
[
'email' => 'fromuser@example.com' ,
'display' => core_user :: MAILDISPLAY_EVERYONE ,
'samecourse' => false ,
'config' => " \n test.com \n example.com \n " ,
'result' => true
],
[
'email' => 'fromuser@example.com' ,
'display' => core_user :: MAILDISPLAY_EVERYONE ,
'samecourse' => false ,
'config' => " \r \n example.com \r \n test.com \r \n " ,
'result' => true
],
2016-10-21 12:26:27 +08:00
// Test from email is not in allowed domain.
// Test that from display is set to show no one.
2017-02-23 10:13:33 +08:00
[ 'email' => 'fromuser@moodle.com' ,
'display' => core_user :: MAILDISPLAY_HIDE ,
'samecourse' => false ,
'config' => " example.com \r \n test.com " ,
'result' => false
],
// Test that from display is set to course members only (course member).
[ 'email' => 'fromuser@moodle.com' ,
'display' => core_user :: MAILDISPLAY_COURSE_MEMBERS_ONLY ,
'samecourse' => true ,
'config' => " example.com \r \n test.com " ,
'result' => false
],
// Test that from display is set to course members only (Non course member.
[ 'email' => 'fromuser@moodle.com' ,
'display' => core_user :: MAILDISPLAY_COURSE_MEMBERS_ONLY ,
'samecourse' => false ,
'config' => " example.com \r \n test.com " ,
'result' => false
],
// Test that from display is set to show everyone.
[ 'email' => 'fromuser@moodle.com' ,
'display' => core_user :: MAILDISPLAY_EVERYONE ,
'samecourse' => false ,
'config' => " example.com \r \n test.com " ,
'result' => false
],
// Test a few erroneous config value and confirm failure.
[ 'email' => 'fromuser@moodle.com' ,
'display' => core_user :: MAILDISPLAY_EVERYONE ,
'samecourse' => false ,
'config' => " \r \n \r \n " ,
'result' => false
],
[ 'email' => 'fromuser@moodle.com' ,
'display' => core_user :: MAILDISPLAY_EVERYONE ,
'samecourse' => false ,
'config' => " \n \n \n " ,
'result' => false
],
2016-10-21 12:26:27 +08:00
];
}
2017-01-03 22:09:30 +01:00
/**
* Test that generate_email_processing_address () returns valid email address .
*/
public function test_generate_email_processing_address () {
global $CFG ;
$this -> resetAfterTest ();
$data = ( object )[
'id' => 42 ,
'email' => 'my.email+from_moodle@example.com' ,
];
$modargs = 'B' . base64_encode ( pack ( 'V' , $data -> id )) . substr ( md5 ( $data -> email ), 0 , 16 );
$CFG -> maildomain = 'example.com' ;
$CFG -> mailprefix = 'mdl+' ;
2017-01-04 13:13:31 +01:00
$this -> assertTrue ( validate_email ( generate_email_processing_address ( 0 , $modargs )));
2017-01-03 22:09:30 +01:00
$CFG -> maildomain = 'mail.example.com' ;
$CFG -> mailprefix = 'mdl-' ;
2017-01-04 13:13:31 +01:00
$this -> assertTrue ( validate_email ( generate_email_processing_address ( 23 , $modargs )));
2017-01-03 22:09:30 +01:00
}
2017-03-05 13:20:03 +08:00
2018-12-19 15:23:05 +01:00
/**
* Test allowemailaddresses setting .
*
* @ param string $email Email address for the from user .
* @ param string $config The CFG -> allowemailaddresses config values
* @ param false / string $result The expected result .
*
* @ dataProvider data_email_is_not_allowed_for_allowemailaddresses
*/
public function test_email_is_not_allowed_for_allowemailaddresses ( $email , $config , $result ) {
$this -> resetAfterTest ();
set_config ( 'allowemailaddresses' , $config );
$this -> assertEquals ( $result , email_is_not_allowed ( $email ));
}
/**
* Data provider for data_email_is_not_allowed_for_allowemailaddresses .
*
* @ return array Returns an array of test data for the above function .
*/
public function data_email_is_not_allowed_for_allowemailaddresses () {
return [
// Test allowed domain empty list.
[
'email' => 'fromuser@example.com' ,
'config' => '' ,
'result' => false
],
// Test from email is in allowed domain.
[
'email' => 'fromuser@example.com' ,
'config' => 'example.com test.com' ,
'result' => false
],
// Test from email is in allowed domain but uppercase config.
[
'email' => 'fromuser@example.com' ,
'config' => 'EXAMPLE.com test.com' ,
'result' => false
],
// Test from email is in allowed domain but uppercase email.
[
'email' => 'fromuser@EXAMPLE.com' ,
'config' => 'example.com test.com' ,
'result' => false
],
// Test from email is in allowed subdomain.
[
'email' => 'fromuser@something.example.com' ,
'config' => '.example.com test.com' ,
'result' => false
],
// Test from email is in allowed subdomain but uppercase config.
[
'email' => 'fromuser@something.example.com' ,
'config' => '.EXAMPLE.com test.com' ,
'result' => false
],
// Test from email is in allowed subdomain but uppercase email.
[
'email' => 'fromuser@something.EXAMPLE.com' ,
'config' => '.example.com test.com' ,
'result' => false
],
// Test from email is not in allowed domain.
[ 'email' => 'fromuser@moodle.com' ,
'config' => 'example.com test.com' ,
'result' => get_string ( 'emailonlyallowed' , '' , 'example.com test.com' )
],
// Test from email is not in allowed subdomain.
[ 'email' => 'fromuser@something.example.com' ,
'config' => 'example.com test.com' ,
'result' => get_string ( 'emailonlyallowed' , '' , 'example.com test.com' )
],
];
}
/**
* Test denyemailaddresses setting .
*
* @ param string $email Email address for the from user .
* @ param string $config The CFG -> denyemailaddresses config values
* @ param false / string $result The expected result .
*
* @ dataProvider data_email_is_not_allowed_for_denyemailaddresses
*/
public function test_email_is_not_allowed_for_denyemailaddresses ( $email , $config , $result ) {
$this -> resetAfterTest ();
set_config ( 'denyemailaddresses' , $config );
$this -> assertEquals ( $result , email_is_not_allowed ( $email ));
}
/**
* Data provider for test_email_is_not_allowed_for_denyemailaddresses .
*
* @ return array Returns an array of test data for the above function .
*/
public function data_email_is_not_allowed_for_denyemailaddresses () {
return [
// Test denied domain empty list.
[
'email' => 'fromuser@example.com' ,
'config' => '' ,
'result' => false
],
// Test from email is in denied domain.
[
'email' => 'fromuser@example.com' ,
'config' => 'example.com test.com' ,
'result' => get_string ( 'emailnotallowed' , '' , 'example.com test.com' )
],
// Test from email is in denied domain but uppercase config.
[
'email' => 'fromuser@example.com' ,
'config' => 'EXAMPLE.com test.com' ,
'result' => get_string ( 'emailnotallowed' , '' , 'EXAMPLE.com test.com' )
],
// Test from email is in denied domain but uppercase email.
[
'email' => 'fromuser@EXAMPLE.com' ,
'config' => 'example.com test.com' ,
'result' => get_string ( 'emailnotallowed' , '' , 'example.com test.com' )
],
// Test from email is in denied subdomain.
[
'email' => 'fromuser@something.example.com' ,
'config' => '.example.com test.com' ,
'result' => get_string ( 'emailnotallowed' , '' , '.example.com test.com' )
],
// Test from email is in denied subdomain but uppercase config.
[
'email' => 'fromuser@something.example.com' ,
'config' => '.EXAMPLE.com test.com' ,
'result' => get_string ( 'emailnotallowed' , '' , '.EXAMPLE.com test.com' )
],
// Test from email is in denied subdomain but uppercase email.
[
'email' => 'fromuser@something.EXAMPLE.com' ,
'config' => '.example.com test.com' ,
'result' => get_string ( 'emailnotallowed' , '' , '.example.com test.com' )
],
// Test from email is not in denied domain.
[ 'email' => 'fromuser@moodle.com' ,
'config' => 'example.com test.com' ,
'result' => false
],
// Test from email is not in denied subdomain.
[ 'email' => 'fromuser@something.example.com' ,
'config' => 'example.com test.com' ,
'result' => false
],
];
}
2017-03-05 13:20:03 +08:00
/**
* Test safe method unserialize_array () .
*/
public function test_unserialize_array () {
$a = [ 1 , 2 , 3 ];
$this -> assertEquals ( $a , unserialize_array ( serialize ( $a )));
$this -> assertEquals ( $a , unserialize_array ( serialize ( $a )));
$a = [ 'a' => 1 , 2 => 2 , 'b' => 'cde' ];
$this -> assertEquals ( $a , unserialize_array ( serialize ( $a )));
$this -> assertEquals ( $a , unserialize_array ( serialize ( $a )));
$a = [ 'a' => 1 , 2 => 2 , 'b' => 'c"d"e' ];
$this -> assertEquals ( $a , unserialize_array ( serialize ( $a )));
$a = [ 'a' => 1 , 2 => [ 'c' => 'd' , 'e' => 'f' ], 'b' => 'cde' ];
$this -> assertEquals ( $a , unserialize_array ( serialize ( $a )));
// Can not unserialize if any string contains semicolons.
$a = [ 'a' => 1 , 2 => 2 , 'b' => 'c"d";e' ];
$this -> assertEquals ( false , unserialize_array ( serialize ( $a )));
// Can not unserialize if there are any objects.
$a = ( object )[ 'a' => 1 , 2 => 2 , 'b' => 'cde' ];
$this -> assertEquals ( false , unserialize_array ( serialize ( $a )));
$a = [ 'a' => 1 , 2 => 2 , 'b' => ( object )[ 'a' => 'cde' ]];
$this -> assertEquals ( false , unserialize_array ( serialize ( $a )));
// Array used in the grader report.
$a = array ( 'aggregatesonly' => [ 51 , 34 ], 'gradesonly' => [ 21 , 45 , 78 ]);
$this -> assertEquals ( $a , unserialize_array ( serialize ( $a )));
}
2018-03-09 09:48:28 +08:00
/**
* Test that the component_class_callback returns the correct default value when the class was not found .
*
* @ dataProvider component_class_callback_default_provider
* @ param $default
*/
public function test_component_class_callback_not_found ( $default ) {
$this -> assertSame ( $default , component_class_callback ( 'thisIsNotTheClassYouWereLookingFor' , 'anymethod' , [], $default ));
}
/**
* Test that the component_class_callback returns the correct default value when the class was not found .
*
* @ dataProvider component_class_callback_default_provider
* @ param $default
*/
public function test_component_class_callback_method_not_found ( $default ) {
require_once ( __DIR__ . '/fixtures/component_class_callback_example.php' );
$this -> assertSame ( $default , component_class_callback ( test_component_class_callback_example :: class , 'this_is_not_the_method_you_were_looking_for' , [ 'abc' ], $default ));
}
/**
* Test that the component_class_callback returns the default when the method returned null .
*
* @ dataProvider component_class_callback_default_provider
* @ param $default
*/
public function test_component_class_callback_found_returns_null ( $default ) {
require_once ( __DIR__ . '/fixtures/component_class_callback_example.php' );
$this -> assertSame ( $default , component_class_callback ( test_component_class_callback_example :: class , 'method_returns_value' , [ null ], $default ));
$this -> assertSame ( $default , component_class_callback ( test_component_class_callback_child_example :: class , 'method_returns_value' , [ null ], $default ));
}
/**
* Test that the component_class_callback returns the expected value and not the default when there was a value .
*
* @ dataProvider component_class_callback_data_provider
* @ param $default
*/
public function test_component_class_callback_found_returns_value ( $value ) {
require_once ( __DIR__ . '/fixtures/component_class_callback_example.php' );
$this -> assertSame ( $value , component_class_callback ( test_component_class_callback_example :: class , 'method_returns_value' , [ $value ], 'This is not the value you were looking for' ));
$this -> assertSame ( $value , component_class_callback ( test_component_class_callback_child_example :: class , 'method_returns_value' , [ $value ], 'This is not the value you were looking for' ));
}
/**
* Test that the component_class_callback handles multiple params correctly .
*
* @ dataProvider component_class_callback_multiple_params_provider
* @ param $default
*/
public function test_component_class_callback_found_accepts_multiple ( $params , $count ) {
require_once ( __DIR__ . '/fixtures/component_class_callback_example.php' );
$this -> assertSame ( $count , component_class_callback ( test_component_class_callback_example :: class , 'method_returns_all_params' , $params , 'This is not the value you were looking for' ));
$this -> assertSame ( $count , component_class_callback ( test_component_class_callback_child_example :: class , 'method_returns_all_params' , $params , 'This is not the value you were looking for' ));
}
/**
* Data provider with list of default values for user in component_class_callback tests .
*
* @ return array
*/
public function component_class_callback_default_provider () {
return [
'null' => [ null ],
'empty string' => [ '' ],
'string' => [ 'This is a string' ],
'int' => [ 12345 ],
'stdClass' => [( object ) [ 'this is my content' ]],
'array' => [[ 'a' => 'b' ,]],
];
}
/**
* Data provider with list of default values for user in component_class_callback tests .
*
* @ return array
*/
public function component_class_callback_data_provider () {
return [
'empty string' => [ '' ],
'string' => [ 'This is a string' ],
'int' => [ 12345 ],
'stdClass' => [( object ) [ 'this is my content' ]],
'array' => [[ 'a' => 'b' ,]],
];
}
/**
* Data provider with list of default values for user in component_class_callback tests .
*
* @ return array
*/
public function component_class_callback_multiple_params_provider () {
return [
'empty array' => [
[],
0 ,
],
'string value' => [
[ 'one' ],
1 ,
],
'string values' => [
[ 'one' , 'two' ],
2 ,
],
'arrays' => [
[[], []],
2 ,
],
'nulls' => [
[ null , null , null , null ],
4 ,
],
'mixed' => [
[ 'a' , 1 , null , ( object ) [], []],
5 ,
],
];
}
2018-07-10 13:18:38 +02:00
/**
* Test that { @ link get_callable_name ()} describes the callable as expected .
*
* @ dataProvider callable_names_provider
* @ param callable $callable
* @ param string $expectedname
*/
public function test_get_callable_name ( $callable , $expectedname ) {
$this -> assertSame ( $expectedname , get_callable_name ( $callable ));
}
/**
* Provides a set of callables and their human readable names .
*
* @ return array of ( string ) case => [( mixed ) callable , ( string | bool ) expected description ]
*/
public function callable_names_provider () {
return [
'integer' => [
386 ,
false ,
],
'boolean' => [
true ,
false ,
],
'static_method_as_literal' => [
'my_foobar_class::my_foobar_method' ,
'my_foobar_class::my_foobar_method' ,
],
'static_method_of_literal_class' => [
[ 'my_foobar_class' , 'my_foobar_method' ],
'my_foobar_class::my_foobar_method' ,
],
'static_method_of_object' => [
[ $this , 'my_foobar_method' ],
'core_moodlelib_testcase::my_foobar_method' ,
],
'method_of_object' => [
[ new lang_string ( 'parentlanguage' , 'core_langconfig' ), 'my_foobar_method' ],
'lang_string::my_foobar_method' ,
],
'function_as_literal' => [
'my_foobar_callback' ,
'my_foobar_callback' ,
],
'function_as_closure' => [
function ( $a ) { return $a ; },
'Closure::__invoke' ,
],
];
}
2019-03-06 12:41:14 +08:00
/**
* Data provider for \core_moodlelib_testcase :: test_get_complete_user_data () .
*
* @ return array
*/
public function user_data_provider () {
return [
'Fetch data using a valid username' => [
'username' , 's1' , true
],
'Fetch data using a valid username, different case' => [
'username' , 'S1' , true
],
'Fetch data using a valid username, different case for fieldname and value' => [
'USERNAME' , 'S1' , true
],
'Fetch data using an invalid username' => [
'username' , 's2' , false
],
2019-03-06 14:49:34 +08:00
'Fetch by email' => [
'email' , 's1@example.com' , true
],
'Fetch data using a non-existent email' => [
'email' , 's2@example.com' , false
],
'Fetch data using a non-existent email, throw exception' => [
'email' , 's2@example.com' , false , dml_missing_record_exception :: class
],
'Multiple accounts with the same email' => [
'email' , 's1@example.com' , false , 1
],
'Multiple accounts with the same email, throw exception' => [
'email' , 's1@example.com' , false , 1 , dml_multiple_records_exception :: class
],
'Fetch data using a valid user ID' => [
'id' , true , true
],
'Fetch data using a non-existent user ID' => [
'id' , false , false
],
2019-03-06 12:41:14 +08:00
];
}
/**
* Test for get_complete_user_data () .
*
* @ dataProvider user_data_provider
* @ param string $field The field to use for the query .
* @ param string | boolean $value The field value . When fetching by ID , set true to fetch valid user ID , false otherwise .
* @ param boolean $success Whether we expect for the fetch to succeed or return false .
2019-03-06 14:49:34 +08:00
* @ param int $allowaccountssameemail Value for $CFG -> allowaccountssameemail .
* @ param string $expectedexception The exception to be expected .
2019-03-06 12:41:14 +08:00
*/
2019-03-06 14:49:34 +08:00
public function test_get_complete_user_data ( $field , $value , $success , $allowaccountssameemail = 0 , $expectedexception = '' ) {
2019-03-06 12:41:14 +08:00
$this -> resetAfterTest ();
2019-03-06 14:49:34 +08:00
// Set config settings we need for our environment.
set_config ( 'allowaccountssameemail' , $allowaccountssameemail );
2019-03-06 12:41:14 +08:00
// Generate the user data.
$generator = $this -> getDataGenerator ();
$userdata = [
'username' => 's1' ,
'email' => 's1@example.com' ,
];
$user = $generator -> create_user ( $userdata );
2019-03-06 14:49:34 +08:00
if ( $allowaccountssameemail ) {
// Create another user with the same email address.
$generator -> create_user ([ 'email' => 's1@example.com' ]);
}
2019-03-06 12:41:14 +08:00
// Since the data provider can't know what user ID to use, do a special handling for ID field tests.
if ( $field === 'id' ) {
if ( $value ) {
// Test for fetching data using a valid user ID. Use the generated user's ID.
$value = $user -> id ;
} else {
// Test for fetching data using a non-existent user ID.
$value = $user -> id + 1 ;
}
}
2019-03-06 14:49:34 +08:00
// When an exception is expected.
$throwexception = false ;
if ( $expectedexception ) {
$this -> expectException ( $expectedexception );
$throwexception = true ;
}
$fetcheduser = get_complete_user_data ( $field , $value , null , $throwexception );
2019-03-06 12:41:14 +08:00
if ( $success ) {
$this -> assertEquals ( $user -> id , $fetcheduser -> id );
$this -> assertEquals ( $user -> username , $fetcheduser -> username );
$this -> assertEquals ( $user -> email , $fetcheduser -> email );
} else {
$this -> assertFalse ( $fetcheduser );
}
}
2019-03-26 00:59:56 +11:00
/**
* Test for send_password_change_ () .
*/
public function test_send_password_change_info () {
$this -> resetAfterTest ();
$user = $this -> getDataGenerator () -> create_user ();
$sink = $this -> redirectEmails (); // Make sure we are redirecting emails.
send_password_change_info ( $user );
$result = $sink -> get_messages ();
$sink -> close ();
2020-09-01 00:44:13 +02:00
$this -> assertStringContainsString ( 'passwords cannot be reset on this site' , quoted_printable_decode ( $result [ 0 ] -> body ));
2019-03-26 00:59:56 +11:00
}
2019-08-06 16:27:26 +08:00
/**
* Test the get_time_interval_string for a range of inputs .
*
* @ dataProvider get_time_interval_string_provider
* @ param int $time1 the time1 param .
* @ param int $time2 the time2 param .
* @ param string | null $format the format param .
* @ param string $expected the expected string .
*/
public function test_get_time_interval_string ( int $time1 , int $time2 , ? string $format , string $expected ) {
if ( is_null ( $format )) {
$this -> assertEquals ( $expected , get_time_interval_string ( $time1 , $time2 ));
} else {
$this -> assertEquals ( $expected , get_time_interval_string ( $time1 , $time2 , $format ));
}
}
/**
* Data provider for the test_get_time_interval_string () method .
*/
public function get_time_interval_string_provider () {
return [
'Time is after the reference time by 1 minute, omitted format' => [
'time1' => 12345660 ,
'time2' => 12345600 ,
'format' => null ,
'expected' => '0d 0h 1m'
],
'Time is before the reference time by 1 minute, omitted format' => [
'time1' => 12345540 ,
'time2' => 12345600 ,
'format' => null ,
'expected' => '0d 0h 1m'
],
'Time is equal to the reference time, omitted format' => [
'time1' => 12345600 ,
'time2' => 12345600 ,
'format' => null ,
'expected' => '0d 0h 0m'
],
'Time is after the reference time by 1 minute, empty string format' => [
'time1' => 12345660 ,
'time2' => 12345600 ,
'format' => '' ,
'expected' => '0d 0h 1m'
],
'Time is before the reference time by 1 minute, empty string format' => [
'time1' => 12345540 ,
'time2' => 12345600 ,
'format' => '' ,
'expected' => '0d 0h 1m'
],
'Time is equal to the reference time, empty string format' => [
'time1' => 12345600 ,
'time2' => 12345600 ,
'format' => '' ,
'expected' => '0d 0h 0m'
],
'Time is after the reference time by 1 minute, custom format' => [
'time1' => 12345660 ,
'time2' => 12345600 ,
'format' => '%R%adays %hhours %imins' ,
'expected' => '+0days 0hours 1mins'
],
'Time is before the reference time by 1 minute, custom format' => [
'time1' => 12345540 ,
'time2' => 12345600 ,
'format' => '%R%adays %hhours %imins' ,
'expected' => '-0days 0hours 1mins'
],
'Time is equal to the reference time, custom format' => [
'time1' => 12345600 ,
'time2' => 12345600 ,
'format' => '%R%adays %hhours %imins' ,
'expected' => '+0days 0hours 0mins'
],
];
}
2020-08-19 16:20:20 +01:00
/**
* Tests the rename_to_unused_name function with a file .
*/
public function test_rename_to_unused_name_file () {
global $CFG ;
// Create a new file in dataroot.
$file = $CFG -> dataroot . '/argh.txt' ;
file_put_contents ( $file , 'Frogs' );
// Rename it.
$newname = rename_to_unused_name ( $file );
// Check new name has expected format.
$this -> assertRegExp ( '~/_temp_[a-f0-9]+$~' , $newname );
// Check it's still in the same folder.
$this -> assertEquals ( $CFG -> dataroot , dirname ( $newname ));
// Check file can be loaded.
$this -> assertEquals ( 'Frogs' , file_get_contents ( $newname ));
// OK, delete the file.
unlink ( $newname );
}
/**
* Tests the rename_to_unused_name function with a directory .
*/
public function test_rename_to_unused_name_dir () {
global $CFG ;
// Create a new directory in dataroot.
$file = $CFG -> dataroot . '/arghdir' ;
mkdir ( $file );
// Rename it.
$newname = rename_to_unused_name ( $file );
// Check new name has expected format.
$this -> assertRegExp ( '~/_temp_[a-f0-9]+$~' , $newname );
// Check it's still in the same folder.
$this -> assertEquals ( $CFG -> dataroot , dirname ( $newname ));
// Check it's still a directory
$this -> assertTrue ( is_dir ( $newname ));
// OK, delete the directory.
rmdir ( $newname );
}
/**
* Tests the rename_to_unused_name function with error cases .
*/
public function test_rename_to_unused_name_failure () {
global $CFG ;
// Rename a file that doesn't exist.
$file = $CFG -> dataroot . '/argh.txt' ;
$this -> assertFalse ( rename_to_unused_name ( $file ));
}
2020-09-17 10:57:22 +10:00
/**
* Provider for display_size
*
* @ return array of ( $size , $expected )
*/
public function display_size_provider () {
return [
[ 0 , '0 bytes' ],
[ 1 , '1 bytes' ],
[ 1023 , '1023 bytes' ],
[ 1024 , '1KB' ],
[ 2222 , '2.2KB' ],
[ 33333 , '32.6KB' ],
[ 444444 , '434KB' ],
[ 5555555 , '5.3MB' ],
[ 66666666 , '63.6MB' ],
[ 777777777 , '741.7MB' ],
[ 8888888888 , '8.3GB' ],
[ 99999999999 , '93.1GB' ],
[ 111111111111 , '103.5GB' ],
[ 2222222222222 , '2TB' ],
[ 33333333333333 , '30.3TB' ],
[ 444444444444444 , '404.2TB' ],
[ 5555555555555555 , '4.9PB' ],
[ 66666666666666666 , '59.2PB' ],
[ 777777777777777777 , '690.8PB' ],
];
}
/**
* Test display_size
* @ dataProvider display_size_provider
* @ param int $size the size in bytes
* @ param string $expected the expected string .
*/
public function test_display_size ( $size , $expected ) {
$result = display_size ( $size );
$this -> assertEquals ( $expected , $result );
}
2012-03-31 23:51:02 +02:00
}