2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Unit tests for (some of) ../moodlelib.php.
22 * @copyright © 2006 The Open University
23 * @author T.J.Hunt@open.ac.uk
24 * @author nicolas@moodle.com
27 defined('MOODLE_INTERNAL') || die();
29 class core_moodlelib_testcase extends advanced_testcase {
31 public static $includecoverage = array('lib/moodlelib.php');
34 * Define a local decimal separator.
36 * It is not possible to directly change the result of get_string in
37 * a unit test. Instead, we create a language pack for language 'xx' in
38 * dataroot and make langconfig.php with the string we need to change.
39 * The example separator used here is 'X'; on PHP 5.3 and before this
40 * must be a single byte character due to PHP bug/limitation in
41 * number_format, so you can't use UTF-8 characters.
43 protected function define_local_decimal_separator() {
44 global $SESSION, $CFG;
46 $SESSION->lang = 'xx';
47 $langconfig = "<?php\n\$string['decsep'] = 'X';";
48 $langfolder = $CFG->dataroot . '/lang/xx';
49 check_dir_exists($langfolder);
50 file_put_contents($langfolder . '/langconfig.php', $langconfig);
53 public function test_cleanremoteaddr() {
55 $this->assertNull(cleanremoteaddr('1023.121.234.1'));
56 $this->assertSame('123.121.234.1', cleanremoteaddr('123.121.234.01 '));
59 $this->assertNull(cleanremoteaddr('0:0:0:0:0:0:0:0:0'));
60 $this->assertNull(cleanremoteaddr('0:0:0:0:0:0:0:abh'));
61 $this->assertNull(cleanremoteaddr('0:0:0:::0:0:1'));
62 $this->assertSame('::', cleanremoteaddr('0:0:0:0:0:0:0:0', true));
63 $this->assertSame('::1:1', cleanremoteaddr('0:0:0:0:0:0:1:1', true));
64 $this->assertSame('abcd:ef::', cleanremoteaddr('abcd:00ef:0:0:0:0:0:0', true));
65 $this->assertSame('1::1', cleanremoteaddr('1:0:0:0:0:0:0:1', true));
66 $this->assertSame('0:0:0:0:0:0:10:1', cleanremoteaddr('::10:1', false));
67 $this->assertSame('1:1:0:0:0:0:0:0', cleanremoteaddr('01:1::', false));
68 $this->assertSame('10:0:0:0:0:0:0:10', cleanremoteaddr('10::10', false));
69 $this->assertSame('::ffff:c0a8:11', cleanremoteaddr('::ffff:192.168.1.1', true));
72 public function test_address_in_subnet() {
73 // 1: xxx.xxx.xxx.xxx/nn or xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/nnn (number of bits in net mask).
74 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.1/32'));
75 $this->assertFalse(address_in_subnet('123.121.23.1', '123.121.23.0/32'));
76 $this->assertTrue(address_in_subnet('10.10.10.100', '123.121.23.45/0'));
77 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.0/24'));
78 $this->assertFalse(address_in_subnet('123.121.34.1', '123.121.234.0/24'));
79 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.0/30'));
80 $this->assertFalse(address_in_subnet('123.121.23.8', '123.121.23.0/30'));
81 $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba/128'));
82 $this->assertFalse(address_in_subnet('bab:baba::baba', 'bab:baba::cece/128'));
83 $this->assertTrue(address_in_subnet('baba:baba::baba', 'cece:cece::cece/0'));
84 $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba/128'));
85 $this->assertTrue(address_in_subnet('baba:baba::00ba', 'baba:baba::/120'));
86 $this->assertFalse(address_in_subnet('baba:baba::aba', 'baba:baba::/120'));
87 $this->assertTrue(address_in_subnet('baba::baba:00ba', 'baba::baba:0/112'));
88 $this->assertFalse(address_in_subnet('baba::aba:00ba', 'baba::baba:0/112'));
89 $this->assertFalse(address_in_subnet('aba::baba:0000', 'baba::baba:0/112'));
92 $this->assertTrue(address_in_subnet('123.121.23.1 ', ' 123.121.23.0 / 24'));
93 $this->assertTrue(address_in_subnet('::ffff:10.1.1.1', ' 0:0:0:000:0:ffff:a1:10 / 126'));
96 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/-2'));
97 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/64'));
98 $this->assertFalse(address_in_subnet('123.121.234.x', '123.121.234.1/24'));
99 $this->assertFalse(address_in_subnet('123.121.234.0', '123.121.234.xx/24'));
100 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/xx0'));
101 $this->assertFalse(address_in_subnet('::1', '::aa:0/xx0'));
102 $this->assertFalse(address_in_subnet('::1', '::aa:0/-5'));
103 $this->assertFalse(address_in_subnet('::1', '::aa:0/130'));
104 $this->assertFalse(address_in_subnet('x:1', '::aa:0/130'));
105 $this->assertFalse(address_in_subnet('::1', '::ax:0/130'));
107 // 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).
108 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12-14'));
109 $this->assertTrue(address_in_subnet('123.121.234.13', '123.121.234.12-14'));
110 $this->assertTrue(address_in_subnet('123.121.234.14', '123.121.234.12-14'));
111 $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.12-14'));
112 $this->assertFalse(address_in_subnet('123.121.234.20', '123.121.234.12-14'));
113 $this->assertFalse(address_in_subnet('123.121.23.12', '123.121.234.12-14'));
114 $this->assertFalse(address_in_subnet('123.12.234.12', '123.121.234.12-14'));
115 $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba-babe'));
116 $this->assertTrue(address_in_subnet('baba:baba::babc', 'baba:baba::baba-babe'));
117 $this->assertTrue(address_in_subnet('baba:baba::babe', 'baba:baba::baba-babe'));
118 $this->assertFalse(address_in_subnet('bab:baba::bab0', 'bab:baba::baba-babe'));
119 $this->assertFalse(address_in_subnet('bab:baba::babf', 'bab:baba::baba-babe'));
120 $this->assertFalse(address_in_subnet('bab:baba::bfbe', 'bab:baba::baba-babe'));
121 $this->assertFalse(address_in_subnet('bfb:baba::babe', 'bab:baba::baba-babe'));
124 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12 - 14 '));
125 $this->assertTrue(address_in_subnet('bab:baba::babe', 'bab:baba::baba - babe '));
128 $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12-234.14'));
129 $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12-256'));
130 $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12--256'));
132 // 3: xxx.xxx or xxx.xxx. or xxx:xxx:xxxx or xxx:xxx:xxxx. (incomplete address, a bit non-technical ;-).
133 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12'));
134 $this->assertFalse(address_in_subnet('123.121.23.12', '123.121.23.13'));
135 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.'));
136 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234'));
137 $this->assertTrue(address_in_subnet('123.121.234.12', '123.121'));
138 $this->assertTrue(address_in_subnet('123.121.234.12', '123'));
139 $this->assertFalse(address_in_subnet('123.121.234.1', '12.121.234.'));
140 $this->assertFalse(address_in_subnet('123.121.234.1', '12.121.234'));
141 $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:baba::bab'));
142 $this->assertFalse(address_in_subnet('baba:baba::ba', 'baba:baba::bc'));
143 $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:baba'));
144 $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:'));
145 $this->assertFalse(address_in_subnet('bab:baba::bab', 'baba:'));
148 $this->assertTrue(address_in_subnet('123.121.234.12', '::1/64, 124., 123.121.234.10-30'));
149 $this->assertTrue(address_in_subnet('124.121.234.12', '::1/64, 124., 123.121.234.10-30'));
150 $this->assertTrue(address_in_subnet('::2', '::1/64, 124., 123.121.234.10-30'));
151 $this->assertFalse(address_in_subnet('12.121.234.12', '::1/64, 124., 123.121.234.10-30'));
153 // Other incorrect input.
154 $this->assertFalse(address_in_subnet('123.123.123.123', ''));
157 public function test_fix_utf8() {
158 // Make sure valid data including other types is not changed.
159 $this->assertSame(null, fix_utf8(null));
160 $this->assertSame(1, fix_utf8(1));
161 $this->assertSame(1.1, fix_utf8(1.1));
162 $this->assertSame(true, fix_utf8(true));
163 $this->assertSame('', fix_utf8(''));
164 $this->assertSame('abc', fix_utf8('abc'));
165 $array = array('do', 're', 'mi');
166 $this->assertSame($array, fix_utf8($array));
167 $object = new stdClass();
170 $this->assertEquals($object, fix_utf8($object));
173 $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"));
175 // Invalid utf8 string.
176 $this->assertSame('aš', fix_utf8('a'.chr(130).'š'), 'This fails with buggy iconv() when mbstring extenstion is not available as fallback.');
179 public function test_optional_param() {
182 $_POST['username'] = 'post_user';
183 $_GET['username'] = 'get_user';
184 $this->assertSame($_POST['username'], optional_param('username', 'default_user', PARAM_RAW));
186 unset($_POST['username']);
187 $this->assertSame($_GET['username'], optional_param('username', 'default_user', PARAM_RAW));
189 unset($_GET['username']);
190 $this->assertSame('default_user', optional_param('username', 'default_user', PARAM_RAW));
192 // Make sure exception is triggered when some params are missing, hide error notices here - new in 2.2.
193 $_POST['username'] = 'post_user';
195 optional_param('username', 'default_user', null);
196 $this->fail('coding_exception expected');
197 } catch (moodle_exception $ex) {
198 $this->assertInstanceOf('coding_exception', $ex);
201 @optional_param('username', 'default_user');
202 $this->fail('coding_exception expected');
203 } catch (moodle_exception $ex) {
204 $this->assertInstanceOf('coding_exception', $ex);
205 } catch (Error $error) {
206 // PHP 7.1 throws Error even earlier.
207 $this->assertRegExp('/Too few arguments to function/', $error->getMessage());
210 @optional_param('username');
211 $this->fail('coding_exception expected');
212 } catch (moodle_exception $ex) {
213 $this->assertInstanceOf('coding_exception', $ex);
214 } catch (Error $error) {
215 // PHP 7.1 throws Error even earlier.
216 $this->assertRegExp('/Too few arguments to function/', $error->getMessage());
219 optional_param('', 'default_user', PARAM_RAW);
220 $this->fail('coding_exception expected');
221 } catch (moodle_exception $ex) {
222 $this->assertInstanceOf('coding_exception', $ex);
225 // Make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3.
226 $_POST['username'] = array('a'=>'a');
227 $this->assertSame($_POST['username'], optional_param('username', 'default_user', PARAM_RAW));
228 $this->assertDebuggingCalled();
231 public function test_optional_param_array() {
234 $_POST['username'] = array('a'=>'post_user');
235 $_GET['username'] = array('a'=>'get_user');
236 $this->assertSame($_POST['username'], optional_param_array('username', array('a'=>'default_user'), PARAM_RAW));
238 unset($_POST['username']);
239 $this->assertSame($_GET['username'], optional_param_array('username', array('a'=>'default_user'), PARAM_RAW));
241 unset($_GET['username']);
242 $this->assertSame(array('a'=>'default_user'), optional_param_array('username', array('a'=>'default_user'), PARAM_RAW));
244 // Make sure exception is triggered when some params are missing, hide error notices here - new in 2.2.
245 $_POST['username'] = array('a'=>'post_user');
247 optional_param_array('username', array('a'=>'default_user'), null);
248 $this->fail('coding_exception expected');
249 } catch (moodle_exception $ex) {
250 $this->assertInstanceOf('coding_exception', $ex);
253 @optional_param_array('username', array('a'=>'default_user'));
254 $this->fail('coding_exception expected');
255 } catch (moodle_exception $ex) {
256 $this->assertInstanceOf('coding_exception', $ex);
257 } catch (Error $error) {
258 // PHP 7.1 throws Error even earlier.
259 $this->assertRegExp('/Too few arguments to function/', $error->getMessage());
262 @optional_param_array('username');
263 $this->fail('coding_exception expected');
264 } catch (moodle_exception $ex) {
265 $this->assertInstanceOf('coding_exception', $ex);
266 } catch (Error $error) {
267 // PHP 7.1 throws Error even earlier.
268 $this->assertRegExp('/Too few arguments to function/', $error->getMessage());
271 optional_param_array('', array('a'=>'default_user'), PARAM_RAW);
272 $this->fail('coding_exception expected');
273 } catch (moodle_exception $ex) {
274 $this->assertInstanceOf('coding_exception', $ex);
277 // Do not allow nested arrays.
279 $_POST['username'] = array('a'=>array('b'=>'post_user'));
280 optional_param_array('username', array('a'=>'default_user'), PARAM_RAW);
281 $this->fail('coding_exception expected');
282 } catch (coding_exception $ex) {
283 $this->assertTrue(true);
286 // Do not allow non-arrays.
287 $_POST['username'] = 'post_user';
288 $this->assertSame(array('a'=>'default_user'), optional_param_array('username', array('a'=>'default_user'), PARAM_RAW));
289 $this->assertDebuggingCalled();
291 // Make sure array keys are sanitised.
292 $_POST['username'] = array('abc123_;-/*-+ '=>'arrggh', 'a1_-'=>'post_user');
293 $this->assertSame(array('a1_-'=>'post_user'), optional_param_array('username', array(), PARAM_RAW));
294 $this->assertDebuggingCalled();
297 public function test_required_param() {
298 $_POST['username'] = 'post_user';
299 $_GET['username'] = 'get_user';
300 $this->assertSame('post_user', required_param('username', PARAM_RAW));
302 unset($_POST['username']);
303 $this->assertSame('get_user', required_param('username', PARAM_RAW));
305 unset($_GET['username']);
307 $this->assertSame('default_user', required_param('username', PARAM_RAW));
308 $this->fail('moodle_exception expected');
309 } catch (moodle_exception $ex) {
310 $this->assertInstanceOf('moodle_exception', $ex);
313 // Make sure exception is triggered when some params are missing, hide error notices here - new in 2.2.
314 $_POST['username'] = 'post_user';
316 @required_param('username');
317 $this->fail('coding_exception expected');
318 } catch (moodle_exception $ex) {
319 $this->assertInstanceOf('coding_exception', $ex);
320 } catch (Error $error) {
321 // PHP 7.1 throws Error even earlier.
322 $this->assertRegExp('/Too few arguments to function/', $error->getMessage());
325 required_param('username', '');
326 $this->fail('coding_exception expected');
327 } catch (moodle_exception $ex) {
328 $this->assertInstanceOf('coding_exception', $ex);
331 required_param('', PARAM_RAW);
332 $this->fail('coding_exception expected');
333 } catch (moodle_exception $ex) {
334 $this->assertInstanceOf('coding_exception', $ex);
337 // Make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3.
338 $_POST['username'] = array('a'=>'a');
339 $this->assertSame($_POST['username'], required_param('username', PARAM_RAW));
340 $this->assertDebuggingCalled();
343 public function test_required_param_array() {
346 $_POST['username'] = array('a'=>'post_user');
347 $_GET['username'] = array('a'=>'get_user');
348 $this->assertSame($_POST['username'], required_param_array('username', PARAM_RAW));
350 unset($_POST['username']);
351 $this->assertSame($_GET['username'], required_param_array('username', PARAM_RAW));
353 // Make sure exception is triggered when some params are missing, hide error notices here - new in 2.2.
354 $_POST['username'] = array('a'=>'post_user');
356 required_param_array('username', null);
357 $this->fail('coding_exception expected');
358 } catch (moodle_exception $ex) {
359 $this->assertInstanceOf('coding_exception', $ex);
362 @required_param_array('username');
363 $this->fail('coding_exception expected');
364 } catch (moodle_exception $ex) {
365 $this->assertInstanceOf('coding_exception', $ex);
366 } catch (Error $error) {
367 // PHP 7.1 throws Error.
368 $this->assertRegExp('/Too few arguments to function/', $error->getMessage());
371 required_param_array('', PARAM_RAW);
372 $this->fail('coding_exception expected');
373 } catch (moodle_exception $ex) {
374 $this->assertInstanceOf('coding_exception', $ex);
377 // Do not allow nested arrays.
379 $_POST['username'] = array('a'=>array('b'=>'post_user'));
380 required_param_array('username', PARAM_RAW);
381 $this->fail('coding_exception expected');
382 } catch (moodle_exception $ex) {
383 $this->assertInstanceOf('coding_exception', $ex);
386 // Do not allow non-arrays.
388 $_POST['username'] = 'post_user';
389 required_param_array('username', PARAM_RAW);
390 $this->fail('moodle_exception expected');
391 } catch (moodle_exception $ex) {
392 $this->assertInstanceOf('moodle_exception', $ex);
395 // Make sure array keys are sanitised.
396 $_POST['username'] = array('abc123_;-/*-+ '=>'arrggh', 'a1_-'=>'post_user');
397 $this->assertSame(array('a1_-'=>'post_user'), required_param_array('username', PARAM_RAW));
398 $this->assertDebuggingCalled();
401 public function test_clean_param() {
402 // Forbid objects and arrays.
404 clean_param(array('x', 'y'), PARAM_RAW);
405 $this->fail('coding_exception expected');
406 } catch (moodle_exception $ex) {
407 $this->assertInstanceOf('coding_exception', $ex);
410 $param = new stdClass();
412 clean_param($param, PARAM_RAW);
413 $this->fail('coding_exception expected');
414 } catch (moodle_exception $ex) {
415 $this->assertInstanceOf('coding_exception', $ex);
418 // Require correct type.
420 clean_param('x', 'xxxxxx');
421 $this->fail('moodle_exception expected');
422 } catch (moodle_exception $ex) {
423 $this->assertInstanceOf('moodle_exception', $ex);
427 $this->fail('moodle_exception expected');
428 } catch (moodle_exception $ex) {
429 $this->assertInstanceOf('moodle_exception', $ex);
430 } catch (Error $error) {
431 // PHP 7.1 throws Error even earlier.
432 $this->assertRegExp('/Too few arguments to function/', $error->getMessage());
436 public function test_clean_param_array() {
437 $this->assertSame(array(), clean_param_array(null, PARAM_RAW));
438 $this->assertSame(array('a', 'b'), clean_param_array(array('a', 'b'), PARAM_RAW));
439 $this->assertSame(array('a', array('b')), clean_param_array(array('a', array('b')), PARAM_RAW, true));
441 // Require correct type.
443 clean_param_array(array('x'), 'xxxxxx');
444 $this->fail('moodle_exception expected');
445 } catch (moodle_exception $ex) {
446 $this->assertInstanceOf('moodle_exception', $ex);
449 @clean_param_array(array('x'));
450 $this->fail('moodle_exception expected');
451 } catch (moodle_exception $ex) {
452 $this->assertInstanceOf('moodle_exception', $ex);
453 } catch (Error $error) {
454 // PHP 7.1 throws Error even earlier.
455 $this->assertRegExp('/Too few arguments to function/', $error->getMessage());
459 clean_param_array(array('x', array('y')), PARAM_RAW);
460 $this->fail('coding_exception expected');
461 } catch (moodle_exception $ex) {
462 $this->assertInstanceOf('coding_exception', $ex);
468 public function test_clean_param_raw() {
470 '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)',
471 clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_RAW));
474 public function test_clean_param_trim() {
475 $this->assertSame('Frog toad', clean_param(" Frog toad \r\n ", PARAM_RAW_TRIMMED));
478 public function test_clean_param_clean() {
479 // PARAM_CLEAN is an ugly hack, do not use in new code (skodak),
480 // instead use more specific type, or submit sothing that can be verified properly.
481 $this->assertSame('xx', clean_param('xx<script>', PARAM_CLEAN));
484 public function test_clean_param_alpha() {
485 $this->assertSame('DSFMOSDJ', clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHA));
488 public function test_clean_param_alphanum() {
489 $this->assertSame('978942897DSFMOSDJ', clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHANUM));
492 public function test_clean_param_alphaext() {
493 $this->assertSame('DSFMOSDJ', clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHAEXT));
496 public function test_clean_param_sequence() {
497 $this->assertSame(',9789,42897', clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_SEQUENCE));
500 public function test_clean_param_component() {
501 // Please note the cleaning of component names is very strict, no guessing here.
502 $this->assertSame('mod_forum', clean_param('mod_forum', PARAM_COMPONENT));
503 $this->assertSame('block_online_users', clean_param('block_online_users', PARAM_COMPONENT));
504 $this->assertSame('block_blond_online_users', clean_param('block_blond_online_users', PARAM_COMPONENT));
505 $this->assertSame('mod_something2', clean_param('mod_something2', PARAM_COMPONENT));
506 $this->assertSame('forum', clean_param('forum', PARAM_COMPONENT));
507 $this->assertSame('user', clean_param('user', PARAM_COMPONENT));
508 $this->assertSame('rating', clean_param('rating', PARAM_COMPONENT));
509 $this->assertSame('feedback360', clean_param('feedback360', PARAM_COMPONENT));
510 $this->assertSame('mod_feedback360', clean_param('mod_feedback360', PARAM_COMPONENT));
511 $this->assertSame('', clean_param('mod_2something', PARAM_COMPONENT));
512 $this->assertSame('', clean_param('2mod_something', PARAM_COMPONENT));
513 $this->assertSame('', clean_param('mod_something_xx', PARAM_COMPONENT));
514 $this->assertSame('', clean_param('auth_something__xx', PARAM_COMPONENT));
515 $this->assertSame('', clean_param('mod_Something', PARAM_COMPONENT));
516 $this->assertSame('', clean_param('mod_somethíng', PARAM_COMPONENT));
517 $this->assertSame('', clean_param('mod__something', PARAM_COMPONENT));
518 $this->assertSame('', clean_param('auth_xx-yy', PARAM_COMPONENT));
519 $this->assertSame('', clean_param('_auth_xx', PARAM_COMPONENT));
520 $this->assertSame('', clean_param('a2uth_xx', PARAM_COMPONENT));
521 $this->assertSame('', clean_param('auth_xx_', PARAM_COMPONENT));
522 $this->assertSame('', clean_param('auth_xx.old', PARAM_COMPONENT));
523 $this->assertSame('', clean_param('_user', PARAM_COMPONENT));
524 $this->assertSame('', clean_param('2rating', PARAM_COMPONENT));
525 $this->assertSame('', clean_param('user_', PARAM_COMPONENT));
528 public function test_is_valid_plugin_name() {
529 $this->assertTrue(is_valid_plugin_name('forum'));
530 $this->assertTrue(is_valid_plugin_name('forum2'));
531 $this->assertTrue(is_valid_plugin_name('feedback360'));
532 $this->assertTrue(is_valid_plugin_name('online_users'));
533 $this->assertTrue(is_valid_plugin_name('blond_online_users'));
534 $this->assertFalse(is_valid_plugin_name('online__users'));
535 $this->assertFalse(is_valid_plugin_name('forum '));
536 $this->assertFalse(is_valid_plugin_name('forum.old'));
537 $this->assertFalse(is_valid_plugin_name('xx-yy'));
538 $this->assertFalse(is_valid_plugin_name('2xx'));
539 $this->assertFalse(is_valid_plugin_name('Xx'));
540 $this->assertFalse(is_valid_plugin_name('_xx'));
541 $this->assertFalse(is_valid_plugin_name('xx_'));
544 public function test_clean_param_plugin() {
545 // Please note the cleaning of plugin names is very strict, no guessing here.
546 $this->assertSame('forum', clean_param('forum', PARAM_PLUGIN));
547 $this->assertSame('forum2', clean_param('forum2', PARAM_PLUGIN));
548 $this->assertSame('feedback360', clean_param('feedback360', PARAM_PLUGIN));
549 $this->assertSame('online_users', clean_param('online_users', PARAM_PLUGIN));
550 $this->assertSame('blond_online_users', clean_param('blond_online_users', PARAM_PLUGIN));
551 $this->assertSame('', clean_param('online__users', PARAM_PLUGIN));
552 $this->assertSame('', clean_param('forum ', PARAM_PLUGIN));
553 $this->assertSame('', clean_param('forum.old', PARAM_PLUGIN));
554 $this->assertSame('', clean_param('xx-yy', PARAM_PLUGIN));
555 $this->assertSame('', clean_param('2xx', PARAM_PLUGIN));
556 $this->assertSame('', clean_param('Xx', PARAM_PLUGIN));
557 $this->assertSame('', clean_param('_xx', PARAM_PLUGIN));
558 $this->assertSame('', clean_param('xx_', PARAM_PLUGIN));
561 public function test_clean_param_area() {
562 // Please note the cleaning of area names is very strict, no guessing here.
563 $this->assertSame('something', clean_param('something', PARAM_AREA));
564 $this->assertSame('something2', clean_param('something2', PARAM_AREA));
565 $this->assertSame('some_thing', clean_param('some_thing', PARAM_AREA));
566 $this->assertSame('some_thing_xx', clean_param('some_thing_xx', PARAM_AREA));
567 $this->assertSame('feedback360', clean_param('feedback360', PARAM_AREA));
568 $this->assertSame('', clean_param('_something', PARAM_AREA));
569 $this->assertSame('', clean_param('something_', PARAM_AREA));
570 $this->assertSame('', clean_param('2something', PARAM_AREA));
571 $this->assertSame('', clean_param('Something', PARAM_AREA));
572 $this->assertSame('', clean_param('some-thing', PARAM_AREA));
573 $this->assertSame('', clean_param('somethííng', PARAM_AREA));
574 $this->assertSame('', clean_param('something.x', PARAM_AREA));
577 public function test_clean_param_text() {
578 $this->assertSame(PARAM_TEXT, PARAM_MULTILANG);
580 $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));
581 $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));
582 $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));
584 $this->assertSame('<span lang="en" class="multilang">aa</span>', clean_param('<span lang="en" class="multilang">aa</span>', PARAM_TEXT));
585 $this->assertSame('aa', clean_param('<span lang="en" class="nothing" class="multilang">aa</span>', PARAM_TEXT));
586 $this->assertSame('aa', clean_param('<lang lang="en" class="multilang">aa</lang>', PARAM_TEXT));
587 $this->assertSame('aa', clean_param('<lang lang="en!!">aa</lang>', PARAM_TEXT));
588 $this->assertSame('aa', clean_param('<span lang="en==" class="multilang">aa</span>', PARAM_TEXT));
589 $this->assertSame('abc', clean_param('a<em>b</em>c', PARAM_TEXT));
590 $this->assertSame('a>c>', clean_param('a><xx >c>', PARAM_TEXT)); // Standard strip_tags() behaviour.
591 $this->assertSame('a', clean_param('a<b', PARAM_TEXT));
592 $this->assertSame('a>b', clean_param('a>b', PARAM_TEXT));
593 $this->assertSame('<lang lang="en">a>a</lang>', clean_param('<lang lang="en">a>a</lang>', PARAM_TEXT)); // Standard strip_tags() behaviour.
594 $this->assertSame('a', clean_param('<lang lang="en">a<a</lang>', PARAM_TEXT));
595 $this->assertSame('<lang lang="en">aa</lang>', clean_param('<lang lang="en">a<br>a</lang>', PARAM_TEXT));
598 public function test_clean_param_url() {
599 // Test PARAM_URL and PARAM_LOCALURL a bit.
600 $this->assertSame('http://google.com/', clean_param('http://google.com/', PARAM_URL));
601 $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));
602 $this->assertSame('http://localhost/', clean_param('http://localhost/', PARAM_URL));
603 $this->assertSame('http://0.255.1.1/numericip.php', clean_param('http://0.255.1.1/numericip.php', PARAM_URL));
604 $this->assertSame('/just/a/path', clean_param('/just/a/path', PARAM_URL));
605 $this->assertSame('', clean_param('funny:thing', PARAM_URL));
608 public function test_clean_param_localurl() {
611 $this->resetAfterTest();
613 // External, invalid.
614 $this->assertSame('', clean_param('funny:thing', PARAM_LOCALURL));
615 $this->assertSame('', clean_param('http://google.com/', PARAM_LOCALURL));
616 $this->assertSame('', clean_param('https://google.com/?test=true', PARAM_LOCALURL));
617 $this->assertSame('', clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_LOCALURL));
620 $this->assertSame(clean_param($CFG->wwwroot, PARAM_LOCALURL), $CFG->wwwroot);
621 $this->assertSame($CFG->wwwroot . '/with/something?else=true',
622 clean_param($CFG->wwwroot . '/with/something?else=true', PARAM_LOCALURL));
625 $this->assertSame('/just/a/path', clean_param('/just/a/path', PARAM_LOCALURL));
626 $this->assertSame('course/view.php?id=3', clean_param('course/view.php?id=3', PARAM_LOCALURL));
628 // Local absolute HTTPS.
629 $httpsroot = str_replace('http:', 'https:', $CFG->wwwroot);
630 $CFG->loginhttps = false;
631 $this->assertSame('', clean_param($httpsroot, PARAM_LOCALURL));
632 $this->assertSame('', clean_param($httpsroot . '/with/something?else=true', PARAM_LOCALURL));
633 $CFG->loginhttps = true;
634 $this->assertSame($httpsroot, clean_param($httpsroot, PARAM_LOCALURL));
635 $this->assertSame($httpsroot . '/with/something?else=true',
636 clean_param($httpsroot . '/with/something?else=true', PARAM_LOCALURL));
638 // Test open redirects are not possible.
639 $CFG->loginhttps = false;
640 $CFG->wwwroot = 'http://www.example.com';
641 $this->assertSame('', clean_param('http://www.example.com.evil.net/hack.php', PARAM_LOCALURL));
642 $CFG->loginhttps = true;
643 $this->assertSame('', clean_param('https://www.example.com.evil.net/hack.php', PARAM_LOCALURL));
646 public function test_clean_param_file() {
647 $this->assertSame('correctfile.txt', clean_param('correctfile.txt', PARAM_FILE));
648 $this->assertSame('badfile.txt', clean_param('b\'a<d`\\/fi:l>e.t"x|t', PARAM_FILE));
649 $this->assertSame('..parentdirfile.txt', clean_param('../parentdirfile.txt', PARAM_FILE));
650 $this->assertSame('....grandparentdirfile.txt', clean_param('../../grandparentdirfile.txt', PARAM_FILE));
651 $this->assertSame('..winparentdirfile.txt', clean_param('..\winparentdirfile.txt', PARAM_FILE));
652 $this->assertSame('....wingrandparentdir.txt', clean_param('..\..\wingrandparentdir.txt', PARAM_FILE));
653 $this->assertSame('myfile.a.b.txt', clean_param('myfile.a.b.txt', PARAM_FILE));
654 $this->assertSame('myfile..a..b.txt', clean_param('myfile..a..b.txt', PARAM_FILE));
655 $this->assertSame('myfile.a..b...txt', clean_param('myfile.a..b...txt', PARAM_FILE));
656 $this->assertSame('myfile.a.txt', clean_param('myfile.a.txt', PARAM_FILE));
657 $this->assertSame('myfile...txt', clean_param('myfile...txt', PARAM_FILE));
658 $this->assertSame('...jpg', clean_param('...jpg', PARAM_FILE));
659 $this->assertSame('.a.b.', clean_param('.a.b.', PARAM_FILE));
660 $this->assertSame('', clean_param('.', PARAM_FILE));
661 $this->assertSame('', clean_param('..', PARAM_FILE));
662 $this->assertSame('...', clean_param('...', PARAM_FILE));
663 $this->assertSame('. . . .', clean_param('. . . .', PARAM_FILE));
664 $this->assertSame('dontrtrim.me. .. .. . ', clean_param('dontrtrim.me. .. .. . ', PARAM_FILE));
665 $this->assertSame(' . .dontltrim.me', clean_param(' . .dontltrim.me', PARAM_FILE));
666 $this->assertSame('here is a tab.txt', clean_param("here is a tab\t.txt", PARAM_FILE));
667 $this->assertSame('here is a linebreak.txt', clean_param("here is a line\r\nbreak.txt", PARAM_FILE));
669 // The following behaviours have been maintained although they seem a little odd.
670 $this->assertSame('funnything', clean_param('funny:thing', PARAM_FILE));
671 $this->assertSame('.currentdirfile.txt', clean_param('./currentdirfile.txt', PARAM_FILE));
672 $this->assertSame('ctempwindowsfile.txt', clean_param('c:\temp\windowsfile.txt', PARAM_FILE));
673 $this->assertSame('homeuserlinuxfile.txt', clean_param('/home/user/linuxfile.txt', PARAM_FILE));
674 $this->assertSame('~myfile.txt', clean_param('~/myfile.txt', PARAM_FILE));
677 public function test_clean_param_path() {
678 $this->assertSame('correctfile.txt', clean_param('correctfile.txt', PARAM_PATH));
679 $this->assertSame('bad/file.txt', clean_param('b\'a<d`\\/fi:l>e.t"x|t', PARAM_PATH));
680 $this->assertSame('/parentdirfile.txt', clean_param('../parentdirfile.txt', PARAM_PATH));
681 $this->assertSame('/grandparentdirfile.txt', clean_param('../../grandparentdirfile.txt', PARAM_PATH));
682 $this->assertSame('/winparentdirfile.txt', clean_param('..\winparentdirfile.txt', PARAM_PATH));
683 $this->assertSame('/wingrandparentdir.txt', clean_param('..\..\wingrandparentdir.txt', PARAM_PATH));
684 $this->assertSame('funnything', clean_param('funny:thing', PARAM_PATH));
685 $this->assertSame('./here', clean_param('./././here', PARAM_PATH));
686 $this->assertSame('./currentdirfile.txt', clean_param('./currentdirfile.txt', PARAM_PATH));
687 $this->assertSame('c/temp/windowsfile.txt', clean_param('c:\temp\windowsfile.txt', PARAM_PATH));
688 $this->assertSame('/home/user/linuxfile.txt', clean_param('/home/user/linuxfile.txt', PARAM_PATH));
689 $this->assertSame('/home../user ./.linuxfile.txt', clean_param('/home../user ./.linuxfile.txt', PARAM_PATH));
690 $this->assertSame('~/myfile.txt', clean_param('~/myfile.txt', PARAM_PATH));
691 $this->assertSame('~/myfile.txt', clean_param('~/../myfile.txt', PARAM_PATH));
692 $this->assertSame('/..b../.../myfile.txt', clean_param('/..b../.../myfile.txt', PARAM_PATH));
693 $this->assertSame('..b../.../myfile.txt', clean_param('..b../.../myfile.txt', PARAM_PATH));
694 $this->assertSame('/super/slashes/', clean_param('/super//slashes///', PARAM_PATH));
697 public function test_clean_param_username() {
699 $currentstatus = $CFG->extendedusernamechars;
701 // Run tests with extended character == false;.
702 $CFG->extendedusernamechars = false;
703 $this->assertSame('johndoe123', clean_param('johndoe123', PARAM_USERNAME) );
704 $this->assertSame('john.doe', clean_param('john.doe', PARAM_USERNAME));
705 $this->assertSame('john-doe', clean_param('john-doe', PARAM_USERNAME));
706 $this->assertSame('john-doe', clean_param('john- doe', PARAM_USERNAME));
707 $this->assertSame('john_doe', clean_param('john_doe', PARAM_USERNAME));
708 $this->assertSame('john@doe', clean_param('john@doe', PARAM_USERNAME));
709 $this->assertSame('johndoe', clean_param('john~doe', PARAM_USERNAME));
710 $this->assertSame('johndoe', clean_param('john´doe', PARAM_USERNAME));
711 $this->assertSame(clean_param('john# $%&()+_^', PARAM_USERNAME), 'john_');
712 $this->assertSame(clean_param(' john# $%&()+_^ ', PARAM_USERNAME), 'john_');
713 $this->assertSame(clean_param('john#$%&() ', PARAM_USERNAME), 'john');
714 $this->assertSame('johnd', clean_param('JOHNdóé ', PARAM_USERNAME));
715 $this->assertSame(clean_param('john.,:;-_/|\ñÑ[]A_X-,D {} ~!@#$%^&*()_+ ?><[] ščřžžý ?ýá
\9e?
\9eý??
\9adoe ', PARAM_USERNAME), 'john.-_a_x-d@_doe');
717 // Test success condition, if extendedusernamechars == ENABLE;.
718 $CFG->extendedusernamechars = true;
719 $this->assertSame('john_doe', clean_param('john_doe', PARAM_USERNAME));
720 $this->assertSame('john@doe', clean_param('john@doe', PARAM_USERNAME));
721 $this->assertSame(clean_param('john# $%&()+_^', PARAM_USERNAME), 'john# $%&()+_^');
722 $this->assertSame(clean_param(' john# $%&()+_^ ', PARAM_USERNAME), 'john# $%&()+_^');
723 $this->assertSame('john~doe', clean_param('john~doe', PARAM_USERNAME));
724 $this->assertSame('john´doe', clean_param('joHN´doe', PARAM_USERNAME));
725 $this->assertSame('johndoe', clean_param('johnDOE', PARAM_USERNAME));
726 $this->assertSame('johndóé', clean_param('johndóé ', PARAM_USERNAME));
728 $CFG->extendedusernamechars = $currentstatus;
731 public function test_clean_param_stringid() {
732 // Test string identifiers validation.
734 $this->assertSame('validstring', clean_param('validstring', PARAM_STRINGID));
735 $this->assertSame('mod/foobar:valid_capability', clean_param('mod/foobar:valid_capability', PARAM_STRINGID));
736 $this->assertSame('CZ', clean_param('CZ', PARAM_STRINGID));
737 $this->assertSame('application/vnd.ms-powerpoint', clean_param('application/vnd.ms-powerpoint', PARAM_STRINGID));
738 $this->assertSame('grade2', clean_param('grade2', PARAM_STRINGID));
740 $this->assertSame('', clean_param('trailing ', PARAM_STRINGID));
741 $this->assertSame('', clean_param('space bar', PARAM_STRINGID));
742 $this->assertSame('', clean_param('0numeric', PARAM_STRINGID));
743 $this->assertSame('', clean_param('*', PARAM_STRINGID));
744 $this->assertSame('', clean_param(' ', PARAM_STRINGID));
747 public function test_clean_param_timezone() {
748 // Test timezone validation.
749 $testvalues = array (
750 'America/Jamaica' => 'America/Jamaica',
751 'America/Argentina/Cordoba' => 'America/Argentina/Cordoba',
752 'America/Port-au-Prince' => 'America/Port-au-Prince',
753 'America/Argentina/Buenos_Aires' => 'America/Argentina/Buenos_Aires',
754 'PST8PDT' => 'PST8PDT',
756 'Wrong/.Value' => '',
757 'Wrong(Value)' => '',
781 foreach ($testvalues as $testvalue => $expectedvalue) {
782 $actualvalue = clean_param($testvalue, PARAM_TIMEZONE);
783 $this->assertEquals($expectedvalue, $actualvalue);
787 public function test_validate_param() {
789 $param = validate_param('11a', PARAM_INT);
790 $this->fail('invalid_parameter_exception expected');
791 } catch (moodle_exception $ex) {
792 $this->assertInstanceOf('invalid_parameter_exception', $ex);
795 $param = validate_param('11', PARAM_INT);
796 $this->assertSame(11, $param);
799 $param = validate_param(null, PARAM_INT, false);
800 $this->fail('invalid_parameter_exception expected');
801 } catch (moodle_exception $ex) {
802 $this->assertInstanceOf('invalid_parameter_exception', $ex);
805 $param = validate_param(null, PARAM_INT, true);
806 $this->assertSame(null, $param);
809 $param = validate_param(array(), PARAM_INT);
810 $this->fail('invalid_parameter_exception expected');
811 } catch (moodle_exception $ex) {
812 $this->assertInstanceOf('invalid_parameter_exception', $ex);
815 $param = validate_param(new stdClass, PARAM_INT);
816 $this->fail('invalid_parameter_exception expected');
817 } catch (moodle_exception $ex) {
818 $this->assertInstanceOf('invalid_parameter_exception', $ex);
821 $param = validate_param('1.0', PARAM_FLOAT);
822 $this->assertSame(1.0, $param);
824 // Make sure valid floats do not cause exception.
825 validate_param(1.0, PARAM_FLOAT);
826 validate_param(10, PARAM_FLOAT);
827 validate_param('0', PARAM_FLOAT);
828 validate_param('119813454.545464564564546564545646556564465465456465465465645645465645645645', PARAM_FLOAT);
829 validate_param('011.1', PARAM_FLOAT);
830 validate_param('11', PARAM_FLOAT);
831 validate_param('+.1', PARAM_FLOAT);
832 validate_param('-.1', PARAM_FLOAT);
833 validate_param('1e10', PARAM_FLOAT);
834 validate_param('.1e+10', PARAM_FLOAT);
835 validate_param('1E-1', PARAM_FLOAT);
838 $param = validate_param('1,2', PARAM_FLOAT);
839 $this->fail('invalid_parameter_exception expected');
840 } catch (moodle_exception $ex) {
841 $this->assertInstanceOf('invalid_parameter_exception', $ex);
844 $param = validate_param('', PARAM_FLOAT);
845 $this->fail('invalid_parameter_exception expected');
846 } catch (moodle_exception $ex) {
847 $this->assertInstanceOf('invalid_parameter_exception', $ex);
850 $param = validate_param('.', PARAM_FLOAT);
851 $this->fail('invalid_parameter_exception expected');
852 } catch (moodle_exception $ex) {
853 $this->assertInstanceOf('invalid_parameter_exception', $ex);
856 $param = validate_param('e10', PARAM_FLOAT);
857 $this->fail('invalid_parameter_exception expected');
858 } catch (moodle_exception $ex) {
859 $this->assertInstanceOf('invalid_parameter_exception', $ex);
862 $param = validate_param('abc', PARAM_FLOAT);
863 $this->fail('invalid_parameter_exception expected');
864 } catch (moodle_exception $ex) {
865 $this->assertInstanceOf('invalid_parameter_exception', $ex);
869 public function test_shorten_text_no_tags_already_short_enough() {
870 // ......12345678901234567890123456.
871 $text = "short text already no tags";
872 $this->assertSame($text, shorten_text($text));
875 public function test_shorten_text_with_tags_already_short_enough() {
876 // .........123456...7890....12345678.......901234567.
877 $text = "<p>short <b>text</b> already</p><p>with tags</p>";
878 $this->assertSame($text, shorten_text($text));
881 public function test_shorten_text_no_tags_needs_shortening() {
882 // Default truncation is after 30 chars, but allowing 3 for the final '...'.
883 // ......12345678901234567890123456789023456789012345678901234.
884 $text = "long text without any tags blah de blah blah blah what";
885 $this->assertSame('long text without any tags ...', shorten_text($text));
888 public function test_shorten_text_with_tags_needs_shortening() {
889 // .......................................123456789012345678901234567890...
890 $text = "<div class='frog'><p><blockquote>Long text with tags that will ".
891 "be chopped off but <b>should be added back again</b></blockquote></p></div>";
892 $this->assertEquals("<div class='frog'><p><blockquote>Long text with " .
893 "tags that ...</blockquote></p></div>", shorten_text($text));
896 public function test_shorten_text_with_tags_and_html_comment() {
897 $text = "<div class='frog'><p><blockquote><!--[if !IE]><!-->Long text with ".
898 "tags that will<!--<![endif]--> ".
899 "be chopped off but <b>should be added back again</b></blockquote></p></div>";
900 $this->assertEquals("<div class='frog'><p><blockquote><!--[if !IE]><!-->Long text with " .
901 "tags that ...<!--<![endif]--></blockquote></p></div>", shorten_text($text));
904 public function test_shorten_text_with_entities() {
905 // Remember to allow 3 chars for the final '...'.
906 // ......123456789012345678901234567_____890...
907 $text = "some text which shouldn't break there";
908 $this->assertSame("some text which shouldn't ...", shorten_text($text, 31));
909 $this->assertSame("some text which shouldn't ...", shorten_text($text, 30));
910 $this->assertSame("some text which shouldn't ...", shorten_text($text, 29));
913 public function test_shorten_text_known_tricky_case() {
914 // This case caused a bug up to 1.9.5
915 // ..........123456789012345678901234567890123456789.....0_____1___2___...
916 $text = "<h3>standard 'break-out' sub groups in TGs?</h3> <<There are several";
917 $this->assertSame("<h3>standard 'break-out' sub groups in ...</h3>",
918 shorten_text($text, 41));
919 $this->assertSame("<h3>standard 'break-out' sub groups in TGs?...</h3>",
920 shorten_text($text, 42));
921 $this->assertSame("<h3>standard 'break-out' sub groups in TGs?</h3> ...",
922 shorten_text($text, 43));
925 public function test_shorten_text_no_spaces() {
926 // ..........123456789.
927 $text = "<h1>123456789</h1>"; // A string with no convenient breaks.
928 $this->assertSame("<h1>12345...</h1>", shorten_text($text, 8));
931 public function test_shorten_text_utf8_european() {
932 // Text without tags.
933 // ......123456789012345678901234567.
934 $text = "Žluťoučký koníček přeskočil";
935 $this->assertSame($text, shorten_text($text)); // 30 chars by default.
936 $this->assertSame("Žluťoučký koníče...", shorten_text($text, 19, true));
937 $this->assertSame("Žluťoučký ...", shorten_text($text, 19, false));
938 // And try it with 2-less (that are, in bytes, the middle of a sequence).
939 $this->assertSame("Žluťoučký koní...", shorten_text($text, 17, true));
940 $this->assertSame("Žluťoučký ...", shorten_text($text, 17, false));
942 // .........123456789012345678...901234567....89012345.
943 $text = "<p>Žluťoučký koníček <b>přeskočil</b> potůček</p>";
944 $this->assertSame($text, shorten_text($text, 60));
945 $this->assertSame("<p>Žluťoučký koníček ...</p>", shorten_text($text, 21));
946 $this->assertSame("<p>Žluťoučký koníče...</p>", shorten_text($text, 19, true));
947 $this->assertSame("<p>Žluťoučký ...</p>", shorten_text($text, 19, false));
948 // And try it with 2 fewer (that are, in bytes, the middle of a sequence).
949 $this->assertSame("<p>Žluťoučký koní...</p>", shorten_text($text, 17, true));
950 $this->assertSame("<p>Žluťoučký ...</p>", shorten_text($text, 17, false));
951 // And try over one tag (start/end), it does proper text len.
952 $this->assertSame("<p>Žluťoučký koníček <b>př...</b></p>", shorten_text($text, 23, true));
953 $this->assertSame("<p>Žluťoučký koníček <b>přeskočil</b> pot...</p>", shorten_text($text, 34, true));
954 // And in the middle of one tag.
955 $this->assertSame("<p>Žluťoučký koníček <b>přeskočil...</b></p>", shorten_text($text, 30, true));
958 public function test_shorten_text_utf8_oriental() {
961 // ......123456789012345678901234.
962 $text = '言語設定言語設定abcdefghijkl';
963 $this->assertSame($text, shorten_text($text)); // 30 chars by default.
964 $this->assertSame("言語設定言語...", shorten_text($text, 9, true));
965 $this->assertSame("言語設定言語...", shorten_text($text, 9, false));
966 $this->assertSame("言語設定言語設定ab...", shorten_text($text, 13, true));
967 $this->assertSame("言語設定言語設定...", shorten_text($text, 13, false));
971 // ......123456789012345678901234.
972 $text = '简体中文简体中文abcdefghijkl';
973 $this->assertSame($text, shorten_text($text)); // 30 chars by default.
974 $this->assertSame("简体中文简体...", shorten_text($text, 9, true));
975 $this->assertSame("简体中文简体...", shorten_text($text, 9, false));
976 $this->assertSame("简体中文简体中文ab...", shorten_text($text, 13, true));
977 $this->assertSame("简体中文简体中文...", shorten_text($text, 13, false));
980 public function test_shorten_text_multilang() {
981 // This is not necessaryily specific to multilang. The issue is really
982 // tags with attributes, where before we were generating invalid HTML
983 // output like shorten_text('<span id="x" class="y">A</span> B', 1)
984 // returning '<span id="x" ...</span>'. It is just that multilang
985 // requires the sort of HTML that is quite likely to trigger this.
986 // ........................................1...
987 $text = '<span lang="en" class="multilang">A</span>' .
988 '<span lang="fr" class="multilang">B</span>';
989 $this->assertSame('<span lang="en" class="multilang">...</span>',
990 shorten_text($text, 1));
993 public function test_usergetdate() {
994 global $USER, $CFG, $DB;
995 $this->resetAfterTest();
997 $this->setAdminUser();
999 $USER->timezone = 2;// Set the timezone to a known state.
1001 $ts = 1261540267; // The time this function was created.
1003 $arr = usergetdate($ts, 1); // Specify the timezone as an argument.
1004 $arr = array_values($arr);
1006 list($seconds, $minutes, $hours, $mday, $wday, $mon, $year, $yday, $weekday, $month) = $arr;
1007 $this->assertSame(7, $seconds);
1008 $this->assertSame(51, $minutes);
1009 $this->assertSame(4, $hours);
1010 $this->assertSame(23, $mday);
1011 $this->assertSame(3, $wday);
1012 $this->assertSame(12, $mon);
1013 $this->assertSame(2009, $year);
1014 $this->assertSame(356, $yday);
1015 $this->assertSame('Wednesday', $weekday);
1016 $this->assertSame('December', $month);
1017 $arr = usergetdate($ts); // Gets the timezone from the $USER object.
1018 $arr = array_values($arr);
1020 list($seconds, $minutes, $hours, $mday, $wday, $mon, $year, $yday, $weekday, $month) = $arr;
1021 $this->assertSame(7, $seconds);
1022 $this->assertSame(51, $minutes);
1023 $this->assertSame(5, $hours);
1024 $this->assertSame(23, $mday);
1025 $this->assertSame(3, $wday);
1026 $this->assertSame(12, $mon);
1027 $this->assertSame(2009, $year);
1028 $this->assertSame(356, $yday);
1029 $this->assertSame('Wednesday', $weekday);
1030 $this->assertSame('December', $month);
1033 public function test_mark_user_preferences_changed() {
1034 $this->resetAfterTest();
1035 $otheruser = $this->getDataGenerator()->create_user();
1036 $otheruserid = $otheruser->id;
1038 set_cache_flag('userpreferenceschanged', $otheruserid, null);
1039 mark_user_preferences_changed($otheruserid);
1041 $this->assertEquals(get_cache_flag('userpreferenceschanged', $otheruserid, time()-10), 1);
1042 set_cache_flag('userpreferenceschanged', $otheruserid, null);
1045 public function test_check_user_preferences_loaded() {
1047 $this->resetAfterTest();
1049 $otheruser = $this->getDataGenerator()->create_user();
1050 $otheruserid = $otheruser->id;
1052 $DB->delete_records('user_preferences', array('userid'=>$otheruserid));
1053 set_cache_flag('userpreferenceschanged', $otheruserid, null);
1055 $user = new stdClass();
1056 $user->id = $otheruserid;
1059 check_user_preferences_loaded($user);
1060 $this->assertTrue(isset($user->preference));
1061 $this->assertTrue(is_array($user->preference));
1062 $this->assertArrayHasKey('_lastloaded', $user->preference);
1063 $this->assertCount(1, $user->preference);
1065 // Add preference via direct call.
1066 $DB->insert_record('user_preferences', array('name'=>'xxx', 'value'=>'yyy', 'userid'=>$user->id));
1068 // No cache reload yet.
1069 check_user_preferences_loaded($user);
1070 $this->assertCount(1, $user->preference);
1072 // Forced reloading of cache.
1073 unset($user->preference);
1074 check_user_preferences_loaded($user);
1075 $this->assertCount(2, $user->preference);
1076 $this->assertSame('yyy', $user->preference['xxx']);
1078 // Add preference via direct call.
1079 $DB->insert_record('user_preferences', array('name'=>'aaa', 'value'=>'bbb', 'userid'=>$user->id));
1081 // Test timeouts and modifications from different session.
1082 set_cache_flag('userpreferenceschanged', $user->id, 1, time() + 1000);
1083 $user->preference['_lastloaded'] = $user->preference['_lastloaded'] - 20;
1084 check_user_preferences_loaded($user);
1085 $this->assertCount(2, $user->preference);
1086 check_user_preferences_loaded($user, 10);
1087 $this->assertCount(3, $user->preference);
1088 $this->assertSame('bbb', $user->preference['aaa']);
1089 set_cache_flag('userpreferenceschanged', $user->id, null);
1092 public function test_set_user_preference() {
1094 $this->resetAfterTest();
1096 $this->setAdminUser();
1098 $otheruser = $this->getDataGenerator()->create_user();
1099 $otheruserid = $otheruser->id;
1101 $DB->delete_records('user_preferences', array('userid'=>$otheruserid));
1102 set_cache_flag('userpreferenceschanged', $otheruserid, null);
1104 $user = new stdClass();
1105 $user->id = $otheruserid;
1107 set_user_preference('aaa', 'bbb', $otheruserid);
1108 $this->assertSame('bbb', $DB->get_field('user_preferences', 'value', array('userid'=>$otheruserid, 'name'=>'aaa')));
1109 $this->assertSame('bbb', get_user_preferences('aaa', null, $otheruserid));
1111 set_user_preference('xxx', 'yyy', $user);
1112 $this->assertSame('yyy', $DB->get_field('user_preferences', 'value', array('userid'=>$otheruserid, 'name'=>'xxx')));
1113 $this->assertSame('yyy', get_user_preferences('xxx', null, $otheruserid));
1114 $this->assertTrue(is_array($user->preference));
1115 $this->assertSame('bbb', $user->preference['aaa']);
1116 $this->assertSame('yyy', $user->preference['xxx']);
1118 set_user_preference('xxx', null, $user);
1119 $this->assertFalse($DB->get_field('user_preferences', 'value', array('userid'=>$otheruserid, 'name'=>'xxx')));
1120 $this->assertNull(get_user_preferences('xxx', null, $otheruserid));
1122 set_user_preference('ooo', true, $user);
1123 $prefs = get_user_preferences(null, null, $otheruserid);
1124 $this->assertSame($user->preference['aaa'], $prefs['aaa']);
1125 $this->assertSame($user->preference['ooo'], $prefs['ooo']);
1126 $this->assertSame('1', $prefs['ooo']);
1128 set_user_preference('null', 0, $user);
1129 $this->assertSame('0', get_user_preferences('null', null, $otheruserid));
1131 $this->assertSame('lala', get_user_preferences('undefined', 'lala', $otheruserid));
1133 $DB->delete_records('user_preferences', array('userid'=>$otheruserid));
1134 set_cache_flag('userpreferenceschanged', $otheruserid, null);
1136 // Test $USER default.
1137 set_user_preference('_test_user_preferences_pref', 'ok');
1138 $this->assertSame('ok', $USER->preference['_test_user_preferences_pref']);
1139 unset_user_preference('_test_user_preferences_pref');
1140 $this->assertTrue(!isset($USER->preference['_test_user_preferences_pref']));
1142 // Test 1333 char values (no need for unicode, there are already tests for that in DB tests).
1143 $longvalue = str_repeat('a', 1333);
1144 set_user_preference('_test_long_user_preference', $longvalue);
1145 $this->assertEquals($longvalue, get_user_preferences('_test_long_user_preference'));
1146 $this->assertEquals($longvalue,
1147 $DB->get_field('user_preferences', 'value', array('userid' => $USER->id, 'name' => '_test_long_user_preference')));
1149 // Test > 1333 char values, coding_exception expected.
1150 $longvalue = str_repeat('a', 1334);
1152 set_user_preference('_test_long_user_preference', $longvalue);
1153 $this->fail('Exception expected - longer than 1333 chars not allowed as preference value');
1154 } catch (moodle_exception $ex) {
1155 $this->assertInstanceOf('coding_exception', $ex);
1158 // Test invalid params.
1160 set_user_preference('_test_user_preferences_pref', array());
1161 $this->fail('Exception expected - array not valid preference value');
1162 } catch (moodle_exception $ex) {
1163 $this->assertInstanceOf('coding_exception', $ex);
1166 set_user_preference('_test_user_preferences_pref', new stdClass);
1167 $this->fail('Exception expected - class not valid preference value');
1168 } catch (moodle_exception $ex) {
1169 $this->assertInstanceOf('coding_exception', $ex);
1172 set_user_preference('_test_user_preferences_pref', 1, array('xx' => 1));
1173 $this->fail('Exception expected - user instance expected');
1174 } catch (moodle_exception $ex) {
1175 $this->assertInstanceOf('coding_exception', $ex);
1178 set_user_preference('_test_user_preferences_pref', 1, 'abc');
1179 $this->fail('Exception expected - user instance expected');
1180 } catch (moodle_exception $ex) {
1181 $this->assertInstanceOf('coding_exception', $ex);
1184 set_user_preference('', 1);
1185 $this->fail('Exception expected - invalid name accepted');
1186 } catch (moodle_exception $ex) {
1187 $this->assertInstanceOf('coding_exception', $ex);
1190 set_user_preference('1', 1);
1191 $this->fail('Exception expected - invalid name accepted');
1192 } catch (moodle_exception $ex) {
1193 $this->assertInstanceOf('coding_exception', $ex);
1197 public function test_set_user_preference_for_current_user() {
1199 $this->resetAfterTest();
1200 $this->setAdminUser();
1202 set_user_preference('test_pref', 2);
1203 set_user_preference('test_pref', 1, $USER->id);
1204 $this->assertEquals(1, get_user_preferences('test_pref'));
1207 public function test_unset_user_preference_for_current_user() {
1209 $this->resetAfterTest();
1210 $this->setAdminUser();
1212 set_user_preference('test_pref', 1);
1213 unset_user_preference('test_pref', $USER->id);
1214 $this->assertNull(get_user_preferences('test_pref'));
1217 public function test_get_extra_user_fields() {
1218 global $CFG, $USER, $DB;
1219 $this->resetAfterTest();
1221 $this->setAdminUser();
1223 // It would be really nice if there were a way to 'mock' has_capability
1224 // checks (either to return true or false) but as there is not, this
1225 // test doesn't test the capability check. Presumably, anyone running
1226 // unit tests will have the capability.
1227 $context = context_system::instance();
1230 $CFG->showuseridentity = '';
1231 $this->assertEquals(array(), get_extra_user_fields($context));
1234 $CFG->showuseridentity = 'frog';
1235 $this->assertEquals(array('frog'), get_extra_user_fields($context));
1238 $CFG->showuseridentity = 'frog,zombie';
1239 $this->assertEquals(array('frog', 'zombie'), get_extra_user_fields($context));
1241 // No fields, except.
1242 $CFG->showuseridentity = '';
1243 $this->assertEquals(array(), get_extra_user_fields($context, array('frog')));
1246 $CFG->showuseridentity = 'frog';
1247 $this->assertEquals(array(), get_extra_user_fields($context, array('frog')));
1250 $CFG->showuseridentity = 'frog,zombie';
1251 $this->assertEquals(array('zombie'), get_extra_user_fields($context, array('frog')));
1254 public function test_get_extra_user_fields_sql() {
1255 global $CFG, $USER, $DB;
1256 $this->resetAfterTest();
1258 $this->setAdminUser();
1260 $context = context_system::instance();
1263 $CFG->showuseridentity = '';
1264 $this->assertSame('', get_extra_user_fields_sql($context));
1267 $CFG->showuseridentity = 'frog';
1268 $this->assertSame(', frog', get_extra_user_fields_sql($context));
1270 // Two fields with table prefix.
1271 $CFG->showuseridentity = 'frog,zombie';
1272 $this->assertSame(', u1.frog, u1.zombie', get_extra_user_fields_sql($context, 'u1'));
1274 // Two fields with field prefix.
1275 $CFG->showuseridentity = 'frog,zombie';
1276 $this->assertSame(', frog AS u_frog, zombie AS u_zombie',
1277 get_extra_user_fields_sql($context, '', 'u_'));
1279 // One field excluded.
1280 $CFG->showuseridentity = 'frog';
1281 $this->assertSame('', get_extra_user_fields_sql($context, '', '', array('frog')));
1283 // Two fields, one excluded, table+field prefix.
1284 $CFG->showuseridentity = 'frog,zombie';
1285 $this->assertEquals(', u1.zombie AS u_zombie',
1286 get_extra_user_fields_sql($context, 'u1', 'u_', array('frog')));
1290 * Test some critical TZ/DST.
1292 * This method tests some special TZ/DST combinations that were fixed
1293 * by MDL-38999. The tests are done by comparing the results of the
1294 * output using Moodle TZ/DST support and PHP native one.
1296 * Note: If you don't trust PHP TZ/DST support, can verify the
1297 * harcoded expectations below with:
1298 * http://www.tools4noobs.com/online_tools/unix_timestamp_to_datetime/
1300 public function test_some_moodle_special_dst() {
1301 $stamp = 1365386400; // 2013/04/08 02:00:00 GMT/UTC.
1303 // In Europe/Tallinn it was 2013/04/08 05:00:00.
1304 $expectation = '2013/04/08 05:00:00';
1305 $phpdt = DateTime::createFromFormat('U', $stamp, new DateTimeZone('UTC'));
1306 $phpdt->setTimezone(new DateTimeZone('Europe/Tallinn'));
1307 $phpres = $phpdt->format('Y/m/d H:i:s'); // PHP result.
1308 $moodleres = userdate($stamp, '%Y/%m/%d %H:%M:%S', 'Europe/Tallinn', false); // Moodle result.
1309 $this->assertSame($expectation, $phpres);
1310 $this->assertSame($expectation, $moodleres);
1312 // In St. Johns it was 2013/04/07 23:30:00.
1313 $expectation = '2013/04/07 23:30:00';
1314 $phpdt = DateTime::createFromFormat('U', $stamp, new DateTimeZone('UTC'));
1315 $phpdt->setTimezone(new DateTimeZone('America/St_Johns'));
1316 $phpres = $phpdt->format('Y/m/d H:i:s'); // PHP result.
1317 $moodleres = userdate($stamp, '%Y/%m/%d %H:%M:%S', 'America/St_Johns', false); // Moodle result.
1318 $this->assertSame($expectation, $phpres);
1319 $this->assertSame($expectation, $moodleres);
1321 $stamp = 1383876000; // 2013/11/08 02:00:00 GMT/UTC.
1323 // In Europe/Tallinn it was 2013/11/08 04:00:00.
1324 $expectation = '2013/11/08 04:00:00';
1325 $phpdt = DateTime::createFromFormat('U', $stamp, new DateTimeZone('UTC'));
1326 $phpdt->setTimezone(new DateTimeZone('Europe/Tallinn'));
1327 $phpres = $phpdt->format('Y/m/d H:i:s'); // PHP result.
1328 $moodleres = userdate($stamp, '%Y/%m/%d %H:%M:%S', 'Europe/Tallinn', false); // Moodle result.
1329 $this->assertSame($expectation, $phpres);
1330 $this->assertSame($expectation, $moodleres);
1332 // In St. Johns it was 2013/11/07 22:30:00.
1333 $expectation = '2013/11/07 22:30:00';
1334 $phpdt = DateTime::createFromFormat('U', $stamp, new DateTimeZone('UTC'));
1335 $phpdt->setTimezone(new DateTimeZone('America/St_Johns'));
1336 $phpres = $phpdt->format('Y/m/d H:i:s'); // PHP result.
1337 $moodleres = userdate($stamp, '%Y/%m/%d %H:%M:%S', 'America/St_Johns', false); // Moodle result.
1338 $this->assertSame($expectation, $phpres);
1339 $this->assertSame($expectation, $moodleres);
1342 public function test_userdate() {
1343 global $USER, $CFG, $DB;
1344 $this->resetAfterTest();
1346 $this->setAdminUser();
1348 $testvalues = array(
1350 'time' => '1309514400',
1351 'usertimezone' => 'America/Moncton',
1352 'timezone' => '0.0', // No dst offset.
1353 'expectedoutput' => 'Friday, 1 July 2011, 10:00 AM'
1356 'time' => '1309514400',
1357 'usertimezone' => 'America/Moncton',
1358 'timezone' => '99', // Dst offset and timezone offset.
1359 'expectedoutput' => 'Friday, 1 July 2011, 7:00 AM'
1362 'time' => '1309514400',
1363 'usertimezone' => 'America/Moncton',
1364 'timezone' => 'America/Moncton', // Dst offset and timezone offset.
1365 'expectedoutput' => 'Friday, 1 July 2011, 7:00 AM'
1368 'time' => '1293876000 ',
1369 'usertimezone' => 'America/Moncton',
1370 'timezone' => '0.0', // No dst offset.
1371 'expectedoutput' => 'Saturday, 1 January 2011, 10:00 AM'
1374 'time' => '1293876000 ',
1375 'usertimezone' => 'America/Moncton',
1376 'timezone' => '99', // No dst offset in jan, so just timezone offset.
1377 'expectedoutput' => 'Saturday, 1 January 2011, 6:00 AM'
1380 'time' => '1293876000 ',
1381 'usertimezone' => 'America/Moncton',
1382 'timezone' => 'America/Moncton', // No dst offset in jan.
1383 'expectedoutput' => 'Saturday, 1 January 2011, 6:00 AM'
1386 'time' => '1293876000 ',
1387 'usertimezone' => '2',
1388 'timezone' => '99', // Take user timezone.
1389 'expectedoutput' => 'Saturday, 1 January 2011, 12:00 PM'
1392 'time' => '1293876000 ',
1393 'usertimezone' => '-2',
1394 'timezone' => '99', // Take user timezone.
1395 'expectedoutput' => 'Saturday, 1 January 2011, 8:00 AM'
1398 'time' => '1293876000 ',
1399 'usertimezone' => '-10',
1400 'timezone' => '2', // Take this timezone.
1401 'expectedoutput' => 'Saturday, 1 January 2011, 12:00 PM'
1404 'time' => '1293876000 ',
1405 'usertimezone' => '-10',
1406 'timezone' => '-2', // Take this timezone.
1407 'expectedoutput' => 'Saturday, 1 January 2011, 8:00 AM'
1410 'time' => '1293876000 ',
1411 'usertimezone' => '-10',
1412 'timezone' => 'random/time', // This should show server time.
1413 'expectedoutput' => 'Saturday, 1 January 2011, 6:00 PM'
1416 'time' => '1293876000 ',
1417 'usertimezone' => '20', // Fallback to server time zone.
1418 'timezone' => '99', // This should show user time.
1419 'expectedoutput' => 'Saturday, 1 January 2011, 6:00 PM'
1423 // Set default timezone to Australia/Perth, else time calculated
1424 // will not match expected values.
1425 $this->setTimezone(99, 'Australia/Perth');
1427 foreach ($testvalues as $vals) {
1428 $USER->timezone = $vals['usertimezone'];
1429 $actualoutput = userdate($vals['time'], '%A, %d %B %Y, %I:%M %p', $vals['timezone']);
1431 // On different systems case of AM PM changes so compare case insensitive.
1432 $vals['expectedoutput'] = core_text::strtolower($vals['expectedoutput']);
1433 $actualoutput = core_text::strtolower($actualoutput);
1435 $this->assertSame($vals['expectedoutput'], $actualoutput,
1436 "Expected: {$vals['expectedoutput']} => Actual: {$actualoutput} \ndata: " . var_export($vals, true));
1441 * Make sure the DST changes happen at the right time in Moodle.
1443 public function test_dst_changes() {
1444 // DST switching in Prague.
1445 // From 2AM to 3AM in 1989.
1446 $date = new DateTime('1989-03-26T01:59:00+01:00');
1447 $this->assertSame('Sunday, 26 March 1989, 01:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague'));
1448 $date = new DateTime('1989-03-26T02:01:00+01:00');
1449 $this->assertSame('Sunday, 26 March 1989, 03:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague'));
1450 // From 3AM to 2AM in 1989 - not the same as the west Europe.
1451 $date = new DateTime('1989-09-24T01:59:00+01:00');
1452 $this->assertSame('Sunday, 24 September 1989, 02:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague'));
1453 $date = new DateTime('1989-09-24T02:01:00+01:00');
1454 $this->assertSame('Sunday, 24 September 1989, 02:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague'));
1455 // From 2AM to 3AM in 2014.
1456 $date = new DateTime('2014-03-30T01:59:00+01:00');
1457 $this->assertSame('Sunday, 30 March 2014, 01:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague'));
1458 $date = new DateTime('2014-03-30T02:01:00+01:00');
1459 $this->assertSame('Sunday, 30 March 2014, 03:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague'));
1460 // From 3AM to 2AM in 2014.
1461 $date = new DateTime('2014-10-26T01:59:00+01:00');
1462 $this->assertSame('Sunday, 26 October 2014, 02:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague'));
1463 $date = new DateTime('2014-10-26T02:01:00+01:00');
1464 $this->assertSame('Sunday, 26 October 2014, 02:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague'));
1465 // From 2AM to 3AM in 2020.
1466 $date = new DateTime('2020-03-29T01:59:00+01:00');
1467 $this->assertSame('Sunday, 29 March 2020, 01:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague'));
1468 $date = new DateTime('2020-03-29T02:01:00+01:00');
1469 $this->assertSame('Sunday, 29 March 2020, 03:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague'));
1470 // From 3AM to 2AM in 2020.
1471 $date = new DateTime('2020-10-25T01:59:00+01:00');
1472 $this->assertSame('Sunday, 25 October 2020, 02:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague'));
1473 $date = new DateTime('2020-10-25T02:01:00+01:00');
1474 $this->assertSame('Sunday, 25 October 2020, 02:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Europe/Prague'));
1476 // DST switching in NZ.
1477 // From 3AM to 2AM in 2015.
1478 $date = new DateTime('2015-04-05T02:59:00+13:00');
1479 $this->assertSame('Sunday, 5 April 2015, 02:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Pacific/Auckland'));
1480 $date = new DateTime('2015-04-05T03:01:00+13:00');
1481 $this->assertSame('Sunday, 5 April 2015, 02:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Pacific/Auckland'));
1482 // From 2AM to 3AM in 2009.
1483 $date = new DateTime('2015-09-27T01:59:00+12:00');
1484 $this->assertSame('Sunday, 27 September 2015, 01:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Pacific/Auckland'));
1485 $date = new DateTime('2015-09-27T02:01:00+12:00');
1486 $this->assertSame('Sunday, 27 September 2015, 03:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Pacific/Auckland'));
1488 // DST switching in Perth.
1489 // From 3AM to 2AM in 2009.
1490 $date = new DateTime('2008-03-30T01:59:00+08:00');
1491 $this->assertSame('Sunday, 30 March 2008, 02:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Australia/Perth'));
1492 $date = new DateTime('2008-03-30T02:01:00+08:00');
1493 $this->assertSame('Sunday, 30 March 2008, 02:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Australia/Perth'));
1494 // From 2AM to 3AM in 2009.
1495 $date = new DateTime('2008-10-26T01:59:00+08:00');
1496 $this->assertSame('Sunday, 26 October 2008, 01:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Australia/Perth'));
1497 $date = new DateTime('2008-10-26T02:01:00+08:00');
1498 $this->assertSame('Sunday, 26 October 2008, 03:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'Australia/Perth'));
1500 // DST switching in US.
1501 // From 2AM to 3AM in 2014.
1502 $date = new DateTime('2014-03-09T01:59:00-05:00');
1503 $this->assertSame('Sunday, 9 March 2014, 01:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'America/New_York'));
1504 $date = new DateTime('2014-03-09T02:01:00-05:00');
1505 $this->assertSame('Sunday, 9 March 2014, 03:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'America/New_York'));
1506 // From 3AM to 2AM in 2014.
1507 $date = new DateTime('2014-11-02T01:59:00-04:00');
1508 $this->assertSame('Sunday, 2 November 2014, 01:59', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'America/New_York'));
1509 $date = new DateTime('2014-11-02T02:01:00-04:00');
1510 $this->assertSame('Sunday, 2 November 2014, 01:01', userdate($date->getTimestamp(), '%A, %d %B %Y, %H:%M', 'America/New_York'));
1513 public function test_make_timestamp() {
1514 global $USER, $CFG, $DB;
1515 $this->resetAfterTest();
1517 $this->setAdminUser();
1519 $testvalues = array(
1521 'usertimezone' => 'America/Moncton',
1528 'timezone' => '0.0',
1529 'applydst' => false, // No dst offset.
1530 'expectedoutput' => '1309514400' // 6pm at UTC+0.
1533 'usertimezone' => 'America/Moncton',
1540 'timezone' => '99', // User default timezone.
1541 'applydst' => false, // Don't apply dst.
1542 'expectedoutput' => '1309528800'
1545 'usertimezone' => 'America/Moncton',
1552 'timezone' => '99', // User default timezone.
1553 'applydst' => true, // Apply dst.
1554 'expectedoutput' => '1309525200'
1557 'usertimezone' => 'America/Moncton',
1564 'timezone' => 'America/Moncton', // String timezone.
1565 'applydst' => true, // Apply dst.
1566 'expectedoutput' => '1309525200'
1569 'usertimezone' => '2', // No dst applyed.
1576 'timezone' => '99', // Take user timezone.
1577 'applydst' => true, // Apply dst.
1578 'expectedoutput' => '1309507200'
1581 'usertimezone' => '-2', // No dst applyed.
1588 'timezone' => '99', // Take usertimezone.
1589 'applydst' => true, // Apply dst.
1590 'expectedoutput' => '1309521600'
1593 'usertimezone' => '-10', // No dst applyed.
1600 'timezone' => '2', // Take this timezone.
1601 'applydst' => true, // Apply dst.
1602 'expectedoutput' => '1309507200'
1605 'usertimezone' => '-10', // No dst applyed.
1612 'timezone' => '-2', // Take this timezone.
1613 'applydst' => true, // Apply dst.
1614 'expectedoutput' => '1309521600'
1617 'usertimezone' => '-10', // No dst applyed.
1624 'timezone' => 'random/time', // This should show server time.
1625 'applydst' => true, // Apply dst.
1626 'expectedoutput' => '1309485600'
1629 'usertimezone' => '-14', // Server time.
1636 'timezone' => '99', // Get user time.
1637 'applydst' => true, // Apply dst.
1638 'expectedoutput' => '1309485600'
1642 // Set default timezone to Australia/Perth, else time calculated
1643 // will not match expected values.
1644 $this->setTimezone(99, 'Australia/Perth');
1646 // Test make_timestamp with all testvals and assert if anything wrong.
1647 foreach ($testvalues as $vals) {
1648 $USER->timezone = $vals['usertimezone'];
1649 $actualoutput = make_timestamp(
1660 // On different systems case of AM PM changes so compare case insensitive.
1661 $vals['expectedoutput'] = core_text::strtolower($vals['expectedoutput']);
1662 $actualoutput = core_text::strtolower($actualoutput);
1664 $this->assertSame($vals['expectedoutput'], $actualoutput,
1665 "Expected: {$vals['expectedoutput']} => Actual: {$actualoutput},
1666 Please check if timezones are updated (Site adminstration -> location -> update timezone)");
1671 * Test get_string and most importantly the implementation of the lang_string
1674 public function test_get_string() {
1677 // Make sure we are using English.
1678 $originallang = $COURSE->lang;
1679 $COURSE->lang = 'en';
1681 $yes = get_string('yes');
1682 $yesexpected = 'Yes';
1683 $this->assertInternalType('string', $yes);
1684 $this->assertSame($yesexpected, $yes);
1686 $yes = get_string('yes', 'moodle');
1687 $this->assertInternalType('string', $yes);
1688 $this->assertSame($yesexpected, $yes);
1690 $yes = get_string('yes', 'core');
1691 $this->assertInternalType('string', $yes);
1692 $this->assertSame($yesexpected, $yes);
1694 $yes = get_string('yes', '');
1695 $this->assertInternalType('string', $yes);
1696 $this->assertSame($yesexpected, $yes);
1698 $yes = get_string('yes', null);
1699 $this->assertInternalType('string', $yes);
1700 $this->assertSame($yesexpected, $yes);
1702 $yes = get_string('yes', null, 1);
1703 $this->assertInternalType('string', $yes);
1704 $this->assertSame($yesexpected, $yes);
1707 $numdays = get_string('numdays', 'core', '1');
1708 $numdaysexpected = $days.' days';
1709 $this->assertInternalType('string', $numdays);
1710 $this->assertSame($numdaysexpected, $numdays);
1712 $yes = get_string('yes', null, null, true);
1713 $this->assertInstanceOf('lang_string', $yes);
1714 $this->assertSame($yesexpected, (string)$yes);
1716 // Test using a lang_string object as the $a argument for a normal
1717 // get_string call (returning string).
1718 $test = new lang_string('yes', null, null, true);
1719 $testexpected = get_string('numdays', 'core', get_string('yes'));
1720 $testresult = get_string('numdays', null, $test);
1721 $this->assertInternalType('string', $testresult);
1722 $this->assertSame($testexpected, $testresult);
1724 // Test using a lang_string object as the $a argument for an object
1725 // get_string call (returning lang_string).
1726 $test = new lang_string('yes', null, null, true);
1727 $testexpected = get_string('numdays', 'core', get_string('yes'));
1728 $testresult = get_string('numdays', null, $test, true);
1729 $this->assertInstanceOf('lang_string', $testresult);
1730 $this->assertSame($testexpected, "$testresult");
1732 // Make sure that object properties that can't be converted don't cause
1734 // Level one: This is as deep as current language processing goes.
1735 $test = new stdClass;
1736 $test->one = 'here';
1737 $string = get_string('yes', null, $test, true);
1738 $this->assertEquals($yesexpected, $string);
1740 // Make sure that object properties that can't be converted don't cause
1742 // Level two: Language processing doesn't currently reach this deep.
1743 // only immediate scalar properties are worked with.
1744 $test = new stdClass;
1745 $test->one = new stdClass;
1746 $test->one->two = 'here';
1747 $string = get_string('yes', null, $test, true);
1748 $this->assertEquals($yesexpected, $string);
1750 // Make sure that object properties that can't be converted don't cause
1752 // Level three: It should never ever go this deep, but we're making sure
1753 // it doesn't cause any probs anyway.
1754 $test = new stdClass;
1755 $test->one = new stdClass;
1756 $test->one->two = new stdClass;
1757 $test->one->two->three = 'here';
1758 $string = get_string('yes', null, $test, true);
1759 $this->assertEquals($yesexpected, $string);
1761 // Make sure that object properties that can't be converted don't cause
1762 // errors and check lang_string properties.
1763 // Level one: This is as deep as current language processing goes.
1764 $test = new stdClass;
1765 $test->one = new lang_string('yes');
1766 $string = get_string('yes', null, $test, true);
1767 $this->assertEquals($yesexpected, $string);
1769 // Make sure that object properties that can't be converted don't cause
1770 // errors and check lang_string properties.
1771 // Level two: Language processing doesn't currently reach this deep.
1772 // only immediate scalar properties are worked with.
1773 $test = new stdClass;
1774 $test->one = new stdClass;
1775 $test->one->two = new lang_string('yes');
1776 $string = get_string('yes', null, $test, true);
1777 $this->assertEquals($yesexpected, $string);
1779 // Make sure that object properties that can't be converted don't cause
1780 // errors and check lang_string properties.
1781 // Level three: It should never ever go this deep, but we're making sure
1782 // it doesn't cause any probs anyway.
1783 $test = new stdClass;
1784 $test->one = new stdClass;
1785 $test->one->two = new stdClass;
1786 $test->one->two->three = new lang_string('yes');
1787 $string = get_string('yes', null, $test, true);
1788 $this->assertEquals($yesexpected, $string);
1790 // Make sure that array properties that can't be converted don't cause
1793 $test['one'] = new stdClass;
1794 $test['one']->two = 'here';
1795 $string = get_string('yes', null, $test, true);
1796 $this->assertEquals($yesexpected, $string);
1798 // Same thing but as above except using an object... this is allowed :P.
1799 $string = get_string('yes', null, null, true);
1800 $object = new stdClass;
1801 $object->$string = 'Yes';
1802 $this->assertEquals($yesexpected, $string);
1803 $this->assertEquals($yesexpected, $object->$string);
1805 // Reset the language.
1806 $COURSE->lang = $originallang;
1810 * @expectedException PHPUnit_Framework_Error_Warning
1812 public function test_get_string_limitation() {
1813 // This is one of the limitations to the lang_string class. It can't be
1815 $array = array(get_string('yes', null, null, true) => 'yes');
1819 * Test localised float formatting.
1821 public function test_format_float() {
1823 // Special case for null.
1824 $this->assertEquals('', format_float(null));
1826 // Default 1 decimal place.
1827 $this->assertEquals('5.4', format_float(5.43));
1828 $this->assertEquals('5.0', format_float(5.001));
1830 // Custom number of decimal places.
1831 $this->assertEquals('5.43000', format_float(5.43, 5));
1833 // Option to strip ending zeros after rounding.
1834 $this->assertEquals('5.43', format_float(5.43, 5, true, true));
1835 $this->assertEquals('5', format_float(5.0001, 3, true, true));
1837 // Tests with a localised decimal separator.
1838 $this->define_local_decimal_separator();
1840 // Localisation on (default).
1841 $this->assertEquals('5X43000', format_float(5.43, 5));
1842 $this->assertEquals('5X43', format_float(5.43, 5, true, true));
1844 // Localisation off.
1845 $this->assertEquals('5.43000', format_float(5.43, 5, false));
1846 $this->assertEquals('5.43', format_float(5.43, 5, false, true));
1850 * Test localised float unformatting.
1852 public function test_unformat_float() {
1854 // Tests without the localised decimal separator.
1856 // Special case for null, empty or white spaces only strings.
1857 $this->assertEquals(null, unformat_float(null));
1858 $this->assertEquals(null, unformat_float(''));
1859 $this->assertEquals(null, unformat_float(' '));
1862 $this->assertEquals(5.4, unformat_float('5.4'));
1863 $this->assertEquals(5.4, unformat_float('5.4', true));
1866 $this->assertEquals(5.0, unformat_float('5'));
1868 // Custom number of decimal.
1869 $this->assertEquals(5.43267, unformat_float('5.43267'));
1872 $this->assertEquals(100.0, unformat_float('100.00'));
1874 // With the thousand separator.
1875 $this->assertEquals(1000.0, unformat_float('1 000'));
1876 $this->assertEquals(1000.32, unformat_float('1 000.32'));
1879 $this->assertEquals(-100.0, unformat_float('-100'));
1882 $this->assertEquals(0.0, unformat_float('Wrong value'));
1883 // Wrong value in strict mode.
1884 $this->assertFalse(unformat_float('Wrong value', true));
1886 // Combining options.
1887 $this->assertEquals(-1023.862567, unformat_float(' -1 023.862567 '));
1889 // Bad decimal separator (should crop the decimal).
1890 $this->assertEquals(50.0, unformat_float('50,57'));
1891 // Bad decimal separator in strict mode (should return false).
1892 $this->assertFalse(unformat_float('50,57', true));
1894 // Tests with a localised decimal separator.
1895 $this->define_local_decimal_separator();
1897 // We repeat the tests above but with the current decimal separator.
1899 // Regular use without and with the localised separator.
1900 $this->assertEquals (5.4, unformat_float('5.4'));
1901 $this->assertEquals (5.4, unformat_float('5X4'));
1903 // Custom number of decimal.
1904 $this->assertEquals (5.43267, unformat_float('5X43267'));
1907 $this->assertEquals (100.0, unformat_float('100X00'));
1909 // With the thousand separator.
1910 $this->assertEquals (1000.32, unformat_float('1 000X32'));
1912 // Bad different separator (should crop the decimal).
1913 $this->assertEquals (50.0, unformat_float('50Y57'));
1914 // Bad different separator in strict mode (should return false).
1915 $this->assertFalse (unformat_float('50Y57', true));
1917 // Combining options.
1918 $this->assertEquals (-1023.862567, unformat_float(' -1 023X862567 '));
1919 // Combining options in strict mode.
1920 $this->assertEquals (-1023.862567, unformat_float(' -1 023X862567 ', true));
1924 * Test deleting of users.
1926 public function test_delete_user() {
1929 $this->resetAfterTest();
1931 $guest = $DB->get_record('user', array('id'=>$CFG->siteguest), '*', MUST_EXIST);
1932 $admin = $DB->get_record('user', array('id'=>$CFG->siteadmins), '*', MUST_EXIST);
1933 $this->assertEquals(0, $DB->count_records('user', array('deleted'=>1)));
1935 $user = $this->getDataGenerator()->create_user(array('idnumber'=>'abc'));
1936 $user2 = $this->getDataGenerator()->create_user(array('idnumber'=>'xyz'));
1937 $usersharedemail1 = $this->getDataGenerator()->create_user(array('email' => 'sharedemail@example.invalid'));
1938 $usersharedemail2 = $this->getDataGenerator()->create_user(array('email' => 'sharedemail@example.invalid'));
1939 $useremptyemail1 = $this->getDataGenerator()->create_user(array('email' => ''));
1940 $useremptyemail2 = $this->getDataGenerator()->create_user(array('email' => ''));
1942 // Delete user and capture event.
1943 $sink = $this->redirectEvents();
1944 $result = delete_user($user);
1945 $events = $sink->get_events();
1947 $event = array_pop($events);
1949 // Test user is deleted in DB.
1950 $this->assertTrue($result);
1951 $deluser = $DB->get_record('user', array('id'=>$user->id), '*', MUST_EXIST);
1952 $this->assertEquals(1, $deluser->deleted);
1953 $this->assertEquals(0, $deluser->picture);
1954 $this->assertSame('', $deluser->idnumber);
1955 $this->assertSame(md5($user->username), $deluser->email);
1956 $this->assertRegExp('/^'.preg_quote($user->email, '/').'\.\d*$/', $deluser->username);
1958 $this->assertEquals(1, $DB->count_records('user', array('deleted'=>1)));
1961 $this->assertInstanceOf('\core\event\user_deleted', $event);
1962 $this->assertSame($user->id, $event->objectid);
1963 $this->assertSame('user_deleted', $event->get_legacy_eventname());
1964 $this->assertEventLegacyData($user, $event);
1965 $expectedlogdata = array(SITEID, 'user', 'delete', "view.php?id=$user->id", $user->firstname.' '.$user->lastname);
1966 $this->assertEventLegacyLogData($expectedlogdata, $event);
1967 $eventdata = $event->get_data();
1968 $this->assertSame($eventdata['other']['username'], $user->username);
1969 $this->assertSame($eventdata['other']['email'], $user->email);
1970 $this->assertSame($eventdata['other']['idnumber'], $user->idnumber);
1971 $this->assertSame($eventdata['other']['picture'], $user->picture);
1972 $this->assertSame($eventdata['other']['mnethostid'], $user->mnethostid);
1973 $this->assertEquals($user, $event->get_record_snapshot('user', $event->objectid));
1974 $this->assertEventContextNotUsed($event);
1976 // Try invalid params.
1977 $record = new stdClass();
1980 delete_user($record);
1981 $this->fail('Expecting exception for invalid delete_user() $user parameter');
1982 } catch (moodle_exception $ex) {
1983 $this->assertInstanceOf('coding_exception', $ex);
1987 delete_user($record);
1988 $this->fail('Expecting exception for invalid delete_user() $user parameter');
1989 } catch (moodle_exception $ex) {
1990 $this->assertInstanceOf('coding_exception', $ex);
1993 $record = new stdClass();
1995 $record->username = 'xx';
1996 $this->assertFalse($DB->record_exists('user', array('id'=>666))); // Any non-existent id is ok.
1997 $result = delete_user($record);
1998 $this->assertFalse($result);
2000 $result = delete_user($guest);
2001 $this->assertFalse($result);
2003 $result = delete_user($admin);
2004 $this->assertFalse($result);
2006 // Simultaneously deleting users with identical email addresses.
2007 $result1 = delete_user($usersharedemail1);
2008 $result2 = delete_user($usersharedemail2);
2010 $usersharedemail1after = $DB->get_record('user', array('id' => $usersharedemail1->id));
2011 $usersharedemail2after = $DB->get_record('user', array('id' => $usersharedemail2->id));
2012 $this->assertTrue($result1);
2013 $this->assertTrue($result2);
2014 $this->assertStringStartsWith($usersharedemail1->email . '.', $usersharedemail1after->username);
2015 $this->assertStringStartsWith($usersharedemail2->email . '.', $usersharedemail2after->username);
2017 // Simultaneously deleting users without email addresses.
2018 $result1 = delete_user($useremptyemail1);
2019 $result2 = delete_user($useremptyemail2);
2021 $useremptyemail1after = $DB->get_record('user', array('id' => $useremptyemail1->id));
2022 $useremptyemail2after = $DB->get_record('user', array('id' => $useremptyemail2->id));
2023 $this->assertTrue($result1);
2024 $this->assertTrue($result2);
2025 $this->assertStringStartsWith($useremptyemail1->username . '.' . $useremptyemail1->id . '@unknownemail.invalid.',
2026 $useremptyemail1after->username);
2027 $this->assertStringStartsWith($useremptyemail2->username . '.' . $useremptyemail2->id . '@unknownemail.invalid.',
2028 $useremptyemail2after->username);
2030 $this->resetDebugging();
2034 * Test function convert_to_array()
2036 public function test_convert_to_array() {
2037 // Check that normal classes are converted to arrays the same way as (array) would do.
2038 $obj = new stdClass();
2039 $obj->prop1 = 'hello';
2040 $obj->prop2 = array('first', 'second', 13);
2042 $this->assertEquals(convert_to_array($obj), (array)$obj);
2044 // Check that context object (with iterator) is converted to array properly.
2045 $obj = context_system::instance();
2048 'contextlevel' => $obj->contextlevel,
2049 'instanceid' => $obj->instanceid,
2050 'path' => $obj->path,
2051 'depth' => $obj->depth
2053 $this->assertEquals(convert_to_array($obj), $ar);
2057 * Test the function date_format_string().
2059 public function test_date_format_string() {
2062 $this->resetAfterTest();
2063 $this->setTimezone(99, 'Australia/Perth');
2068 'str' => '%A, %d %B %Y, %I:%M %p',
2069 'expected' => 'Saturday, 01 January 2011, 06:00 PM'
2073 'str' => '%A, %d %B %Y, %I:%M %p',
2074 'expected' => 'Saturday, 01 January 2011, 10:00 AM'
2077 // Note: this function expected the timestamp in weird format before,
2078 // since 2.9 it uses UTC.
2079 'tz' => 'Pacific/Auckland',
2080 'str' => '%A, %d %B %Y, %I:%M %p',
2081 'expected' => 'Saturday, 01 January 2011, 11:00 PM'
2083 // Following tests pass on Windows only because en lang pack does
2084 // not contain localewincharset, in real life lang pack maintainers
2085 // may use only characters that are present in localewincharset
2086 // in format strings!
2089 'str' => 'Žluťoučký koníček %A',
2090 'expected' => 'Žluťoučký koníček Saturday'
2094 'str' => '言語設定言語 %A',
2095 'expected' => '言語設定言語 Saturday'
2099 'str' => '简体中文简体 %A',
2100 'expected' => '简体中文简体 Saturday'
2104 // Note: date_format_string() uses the timezone only to differenciate
2105 // the server time from the UTC time. It does not modify the timestamp.
2106 // Hence similar results for timezones <= 13.
2107 // On different systems case of AM PM changes so compare case insensitive.
2108 foreach ($tests as $test) {
2109 $str = date_format_string(1293876000, $test['str'], $test['tz']);
2110 $this->assertSame(core_text::strtolower($test['expected']), core_text::strtolower($str));
2114 public function test_get_config() {
2117 $this->resetAfterTest();
2120 set_config('phpunit_test_get_config_1', 'test 1');
2121 set_config('phpunit_test_get_config_2', 'test 2', 'mod_forum');
2122 if (!is_array($CFG->config_php_settings)) {
2123 $CFG->config_php_settings = array();
2125 $CFG->config_php_settings['phpunit_test_get_config_3'] = 'test 3';
2127 if (!is_array($CFG->forced_plugin_settings)) {
2128 $CFG->forced_plugin_settings = array();
2130 if (!array_key_exists('mod_forum', $CFG->forced_plugin_settings)) {
2131 $CFG->forced_plugin_settings['mod_forum'] = array();
2133 $CFG->forced_plugin_settings['mod_forum']['phpunit_test_get_config_4'] = 'test 4';
2134 $CFG->phpunit_test_get_config_5 = 'test 5';
2137 $this->assertSame('test 1', get_config('core', 'phpunit_test_get_config_1'));
2138 $this->assertSame('test 2', get_config('mod_forum', 'phpunit_test_get_config_2'));
2139 $this->assertSame('test 3', get_config('core', 'phpunit_test_get_config_3'));
2140 $this->assertSame('test 4', get_config('mod_forum', 'phpunit_test_get_config_4'));
2141 $this->assertFalse(get_config('core', 'phpunit_test_get_config_5'));
2142 $this->assertFalse(get_config('core', 'phpunit_test_get_config_x'));
2143 $this->assertFalse(get_config('mod_forum', 'phpunit_test_get_config_x'));
2145 // Test config we know to exist.
2146 $this->assertSame($CFG->dataroot, get_config('core', 'dataroot'));
2147 $this->assertSame($CFG->phpunit_dataroot, get_config('core', 'phpunit_dataroot'));
2148 $this->assertSame($CFG->dataroot, get_config('core', 'phpunit_dataroot'));
2149 $this->assertSame(get_config('core', 'dataroot'), get_config('core', 'phpunit_dataroot'));
2151 // Test setting a config var that already exists.
2152 set_config('phpunit_test_get_config_1', 'test a');
2153 $this->assertSame('test a', $CFG->phpunit_test_get_config_1);
2154 $this->assertSame('test a', get_config('core', 'phpunit_test_get_config_1'));
2156 // Test cache invalidation.
2157 $cache = cache::make('core', 'config');
2158 $this->assertInternalType('array', $cache->get('core'));
2159 $this->assertInternalType('array', $cache->get('mod_forum'));
2160 set_config('phpunit_test_get_config_1', 'test b');
2161 $this->assertFalse($cache->get('core'));
2162 set_config('phpunit_test_get_config_4', 'test c', 'mod_forum');
2163 $this->assertFalse($cache->get('mod_forum'));
2166 public function test_get_max_upload_sizes() {
2167 // Test with very low limits so we are not affected by php upload limits.
2168 // Test activity limit smallest.
2169 $sitebytes = 102400;
2170 $coursebytes = 51200;
2171 $modulebytes = 10240;
2172 $result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes);
2174 $this->assertSame('Activity upload limit (10KB)', $result['0']);
2175 $this->assertCount(2, $result);
2177 // Test course limit smallest.
2178 $sitebytes = 102400;
2179 $coursebytes = 10240;
2180 $modulebytes = 51200;
2181 $result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes);
2183 $this->assertSame('Course upload limit (10KB)', $result['0']);
2184 $this->assertCount(2, $result);
2186 // Test site limit smallest.
2188 $coursebytes = 102400;
2189 $modulebytes = 51200;
2190 $result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes);
2192 $this->assertSame('Site upload limit (10KB)', $result['0']);
2193 $this->assertCount(2, $result);
2195 // Test site limit not set.
2197 $coursebytes = 102400;
2198 $modulebytes = 51200;
2199 $result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes);
2201 $this->assertSame('Activity upload limit (50KB)', $result['0']);
2202 $this->assertCount(3, $result);
2205 $coursebytes = 51200;
2206 $modulebytes = 102400;
2207 $result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes);
2209 $this->assertSame('Course upload limit (50KB)', $result['0']);
2210 $this->assertCount(3, $result);
2212 // Test custom bytes in range.
2213 $sitebytes = 102400;
2214 $coursebytes = 51200;
2215 $modulebytes = 51200;
2216 $custombytes = 10240;
2217 $result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes, $custombytes);
2219 $this->assertCount(3, $result);
2221 // Test custom bytes in range but non-standard.
2222 $sitebytes = 102400;
2223 $coursebytes = 51200;
2224 $modulebytes = 51200;
2225 $custombytes = 25600;
2226 $result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes, $custombytes);
2228 $this->assertCount(4, $result);
2230 // Test custom bytes out of range.
2231 $sitebytes = 102400;
2232 $coursebytes = 51200;
2233 $modulebytes = 51200;
2234 $custombytes = 102400;
2235 $result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes, $custombytes);
2237 $this->assertCount(3, $result);
2239 // Test custom bytes out of range and non-standard.
2240 $sitebytes = 102400;
2241 $coursebytes = 51200;
2242 $modulebytes = 51200;
2243 $custombytes = 256000;
2244 $result = get_max_upload_sizes($sitebytes, $coursebytes, $modulebytes, $custombytes);
2246 $this->assertCount(3, $result);
2248 // Test site limit only.
2250 $result = get_max_upload_sizes($sitebytes);
2252 $this->assertSame('Site upload limit (50KB)', $result['0']);
2253 $this->assertSame('50KB', $result['51200']);
2254 $this->assertSame('10KB', $result['10240']);
2255 $this->assertCount(3, $result);
2258 $result = get_max_upload_sizes();
2259 $this->assertArrayHasKey('0', $result);
2260 $this->assertArrayHasKey(get_max_upload_file_size(), $result);
2264 * Test function password_is_legacy_hash().
2266 public function test_password_is_legacy_hash() {
2267 // Well formed md5s should be matched.
2268 foreach (array('some', 'strings', 'to_check!') as $string) {
2269 $md5 = md5($string);
2270 $this->assertTrue(password_is_legacy_hash($md5));
2272 // Strings that are not md5s should not be matched.
2273 foreach (array('', AUTH_PASSWORD_NOT_CACHED, 'IPW8WTcsWNgAWcUS1FBVHegzJnw5M2jOmYkmfc8z.xdBOyC4Caeum') as $notmd5) {
2274 $this->assertFalse(password_is_legacy_hash($notmd5));
2279 * Test function validate_internal_user_password().
2281 public function test_validate_internal_user_password() {
2282 // Test bcrypt hashes.
2283 $validhashes = array(
2284 'pw' => '$2y$10$LOSDi5eaQJhutSRun.OVJ.ZSxQZabCMay7TO1KmzMkDMPvU40zGXK',
2285 'abc' => '$2y$10$VWTOhVdsBbWwtdWNDRHSpewjd3aXBQlBQf5rBY/hVhw8hciarFhXa',
2286 'C0mP1eX_&}<?@*&%` |\"' => '$2y$10$3PJf.q.9ywNJlsInPbqc8.IFeSsvXrGvQLKRFBIhVu1h1I3vpIry6',
2287 'ĩńťėŕňăţĩōŋāĹ' => '$2y$10$3A2Y8WpfRAnP3czJiSv6N.6Xp0T8hW3QZz2hUCYhzyWr1kGP1yUve'
2290 foreach ($validhashes as $password => $hash) {
2291 $user = new stdClass();
2292 $user->auth = 'manual';
2293 $user->password = $hash;
2294 // The correct password should be validated.
2295 $this->assertTrue(validate_internal_user_password($user, $password));
2296 // An incorrect password should not be validated.
2297 $this->assertFalse(validate_internal_user_password($user, 'badpw'));
2302 * Test function hash_internal_user_password().
2304 public function test_hash_internal_user_password() {
2305 $passwords = array('pw', 'abc123', 'C0mP1eX_&}<?@*&%` |\"', 'ĩńťėŕňăţĩōŋāĹ');
2307 // Check that some passwords that we convert to hashes can
2309 foreach ($passwords as $password) {
2310 $hash = hash_internal_user_password($password);
2311 $fasthash = hash_internal_user_password($password, true);
2312 $user = new stdClass();
2313 $user->auth = 'manual';
2314 $user->password = $hash;
2315 $this->assertTrue(validate_internal_user_password($user, $password));
2317 // They should not be in md5 format.
2318 $this->assertFalse(password_is_legacy_hash($hash));
2320 // Check that cost factor in hash is correctly set.
2321 $this->assertRegExp('/\$10\$/', $hash);
2322 $this->assertRegExp('/\$04\$/', $fasthash);
2327 * Test function update_internal_user_password().
2329 public function test_update_internal_user_password() {
2331 $this->resetAfterTest();
2332 $passwords = array('password', '1234', 'changeme', '****');
2333 foreach ($passwords as $password) {
2334 $user = $this->getDataGenerator()->create_user(array('auth'=>'manual'));
2335 update_internal_user_password($user, $password);
2336 // The user object should have been updated.
2337 $this->assertTrue(validate_internal_user_password($user, $password));
2338 // The database field for the user should also have been updated to the
2340 $this->assertSame($user->password, $DB->get_field('user', 'password', array('id' => $user->id)));
2343 $user = $this->getDataGenerator()->create_user(array('auth'=>'manual'));
2344 // Manually set the user's password to the md5 of the string 'password'.
2345 $DB->set_field('user', 'password', '5f4dcc3b5aa765d61d8327deb882cf99', array('id' => $user->id));
2347 $sink = $this->redirectEvents();
2348 // Update the password.
2349 update_internal_user_password($user, 'password');
2350 $events = $sink->get_events();
2352 $event = array_pop($events);
2354 // Password should have been updated to a bcrypt hash.
2355 $this->assertFalse(password_is_legacy_hash($user->password));
2357 // Verify event information.
2358 $this->assertInstanceOf('\core\event\user_password_updated', $event);
2359 $this->assertSame($user->id, $event->relateduserid);
2360 $this->assertEquals(context_user::instance($user->id), $event->get_context());
2361 $this->assertEventContextNotUsed($event);
2363 // Verify recovery of property 'auth'.
2365 update_internal_user_password($user, 'newpassword');
2366 $this->assertDebuggingCalled('User record in update_internal_user_password() must include field auth',
2368 $this->assertEquals('manual', $user->auth);
2372 * Testing that if the password is not cached, that it does not update
2373 * the user table and fire event.
2375 public function test_update_internal_user_password_no_cache() {
2376 $this->resetAfterTest();
2378 $user = $this->getDataGenerator()->create_user(array('auth' => 'cas'));
2379 $this->assertEquals(AUTH_PASSWORD_NOT_CACHED, $user->password);
2381 $sink = $this->redirectEvents();
2382 update_internal_user_password($user, 'wonkawonka');
2383 $this->assertEquals(0, $sink->count(), 'User updated event should not fire');
2387 * Test if the user has a password hash, but now their auth method
2388 * says not to cache it. Then it should update.
2390 public function test_update_internal_user_password_update_no_cache() {
2391 $this->resetAfterTest();
2393 $user = $this->getDataGenerator()->create_user(array('password' => 'test'));
2394 $this->assertNotEquals(AUTH_PASSWORD_NOT_CACHED, $user->password);
2395 $user->auth = 'cas'; // Change to a auth that does not store passwords.
2397 $sink = $this->redirectEvents();
2398 update_internal_user_password($user, 'wonkawonka');
2399 $this->assertGreaterThanOrEqual(1, $sink->count(), 'User updated event should fire');
2401 $this->assertEquals(AUTH_PASSWORD_NOT_CACHED, $user->password);
2404 public function test_fullname() {
2407 $this->resetAfterTest();
2409 // Create a user to test the name display on.
2411 $record['firstname'] = 'Scott';
2412 $record['lastname'] = 'Fletcher';
2413 $record['firstnamephonetic'] = 'スコット';
2414 $record['lastnamephonetic'] = 'フレチャー';
2415 $record['alternatename'] = 'No friends';
2416 $user = $this->getDataGenerator()->create_user($record);
2418 // Back up config settings for restore later.
2419 $originalcfg = new stdClass();
2420 $originalcfg->fullnamedisplay = $CFG->fullnamedisplay;
2421 $originalcfg->alternativefullnameformat = $CFG->alternativefullnameformat;
2423 // Testing existing fullnamedisplay settings.
2424 $CFG->fullnamedisplay = 'firstname';
2425 $testname = fullname($user);
2426 $this->assertSame($user->firstname, $testname);
2428 $CFG->fullnamedisplay = 'firstname lastname';
2429 $expectedname = "$user->firstname $user->lastname";
2430 $testname = fullname($user);
2431 $this->assertSame($expectedname, $testname);
2433 $CFG->fullnamedisplay = 'lastname firstname';
2434 $expectedname = "$user->lastname $user->firstname";
2435 $testname = fullname($user);
2436 $this->assertSame($expectedname, $testname);
2438 $expectedname = get_string('fullnamedisplay', null, $user);
2439 $CFG->fullnamedisplay = 'language';
2440 $testname = fullname($user);
2441 $this->assertSame($expectedname, $testname);
2443 // Test override parameter.
2444 $CFG->fullnamedisplay = 'firstname';
2445 $expectedname = "$user->firstname $user->lastname";
2446 $testname = fullname($user, true);
2447 $this->assertSame($expectedname, $testname);
2449 // Test alternativefullnameformat setting.
2450 // Test alternativefullnameformat that has been set to nothing.
2451 $CFG->alternativefullnameformat = '';
2452 $expectedname = "$user->firstname $user->lastname";
2453 $testname = fullname($user, true);
2454 $this->assertSame($expectedname, $testname);
2456 // Test alternativefullnameformat that has been set to 'language'.
2457 $CFG->alternativefullnameformat = 'language';
2458 $expectedname = "$user->firstname $user->lastname";
2459 $testname = fullname($user, true);
2460 $this->assertSame($expectedname, $testname);
2462 // Test customising the alternativefullnameformat setting with all additional name fields.
2463 $CFG->alternativefullnameformat = 'firstname lastname firstnamephonetic lastnamephonetic middlename alternatename';
2464 $expectedname = "$user->firstname $user->lastname $user->firstnamephonetic $user->lastnamephonetic $user->middlename $user->alternatename";
2465 $testname = fullname($user, true);
2466 $this->assertSame($expectedname, $testname);
2468 // Test additional name fields.
2469 $CFG->fullnamedisplay = 'lastname lastnamephonetic firstname firstnamephonetic';
2470 $expectedname = "$user->lastname $user->lastnamephonetic $user->firstname $user->firstnamephonetic";
2471 $testname = fullname($user);
2472 $this->assertSame($expectedname, $testname);
2474 // Test for handling missing data.
2475 $user->middlename = null;
2476 // Parenthesis with no data.
2477 $CFG->fullnamedisplay = 'firstname (middlename) lastname';
2478 $expectedname = "$user->firstname $user->lastname";
2479 $testname = fullname($user);
2480 $this->assertSame($expectedname, $testname);
2482 // Extra spaces due to no data.
2483 $CFG->fullnamedisplay = 'firstname middlename lastname';
2484 $expectedname = "$user->firstname $user->lastname";
2485 $testname = fullname($user);
2486 $this->assertSame($expectedname, $testname);
2488 // Regular expression testing.
2489 // Remove some data from the user fields.
2490 $user->firstnamephonetic = '';
2491 $user->lastnamephonetic = '';
2493 // Removing empty brackets and excess whitespace.
2494 // All of these configurations should resolve to just firstname lastname.
2495 $configarray = array();
2496 $configarray[] = 'firstname lastname [firstnamephonetic lastnamephonetic]';
2497 $configarray[] = 'firstname lastname \'middlename\'';
2498 $configarray[] = 'firstname "firstnamephonetic" lastname';
2499 $configarray[] = 'firstname 「firstnamephonetic」 lastname 「lastnamephonetic」';
2501 foreach ($configarray as $config) {
2502 $CFG->fullnamedisplay = $config;
2503 $expectedname = "$user->firstname $user->lastname";
2504 $testname = fullname($user);
2505 $this->assertSame($expectedname, $testname);
2508 // Check to make sure that other characters are left in place.
2509 $configarray = array();
2510 $configarray['0'] = new stdClass();
2511 $configarray['0']->config = 'lastname firstname, middlename';
2512 $configarray['0']->expectedname = "$user->lastname $user->firstname,";
2513 $configarray['1'] = new stdClass();
2514 $configarray['1']->config = 'lastname firstname + alternatename';
2515 $configarray['1']->expectedname = "$user->lastname $user->firstname + $user->alternatename";
2516 $configarray['2'] = new stdClass();
2517 $configarray['2']->config = 'firstname aka: alternatename';
2518 $configarray['2']->expectedname = "$user->firstname aka: $user->alternatename";
2519 $configarray['3'] = new stdClass();
2520 $configarray['3']->config = 'firstname (alternatename)';
2521 $configarray['3']->expectedname = "$user->firstname ($user->alternatename)";
2522 $configarray['4'] = new stdClass();
2523 $configarray['4']->config = 'firstname [alternatename]';
2524 $configarray['4']->expectedname = "$user->firstname [$user->alternatename]";
2525 $configarray['5'] = new stdClass();
2526 $configarray['5']->config = 'firstname "lastname"';
2527 $configarray['5']->expectedname = "$user->firstname \"$user->lastname\"";
2529 foreach ($configarray as $config) {
2530 $CFG->fullnamedisplay = $config->config;
2531 $expectedname = $config->expectedname;
2532 $testname = fullname($user);
2533 $this->assertSame($expectedname, $testname);
2536 // Test debugging message displays when
2537 // fullnamedisplay setting is "normal".
2538 $CFG->fullnamedisplay = 'firstname lastname';
2540 $user = new stdClass();
2541 $user->firstname = 'Stan';
2542 $user->lastname = 'Lee';
2543 $namedisplay = fullname($user);
2544 $this->assertDebuggingCalled();
2546 // Tidy up after we finish testing.
2547 $CFG->fullnamedisplay = $originalcfg->fullnamedisplay;
2548 $CFG->alternativefullnameformat = $originalcfg->alternativefullnameformat;
2551 public function test_get_all_user_name_fields() {
2552 $this->resetAfterTest();
2554 // Additional names in an array.
2555 $testarray = array('firstnamephonetic' => 'firstnamephonetic',
2556 'lastnamephonetic' => 'lastnamephonetic',
2557 'middlename' => 'middlename',
2558 'alternatename' => 'alternatename',
2559 'firstname' => 'firstname',
2560 'lastname' => 'lastname');
2561 $this->assertEquals($testarray, get_all_user_name_fields());
2563 // Additional names as a string.
2564 $teststring = 'firstnamephonetic,lastnamephonetic,middlename,alternatename,firstname,lastname';
2565 $this->assertEquals($teststring, get_all_user_name_fields(true));
2567 // Additional names as a string with an alias.
2568 $teststring = 't.firstnamephonetic,t.lastnamephonetic,t.middlename,t.alternatename,t.firstname,t.lastname';
2569 $this->assertEquals($teststring, get_all_user_name_fields(true, 't'));
2571 // Additional name fields with a prefix - object.
2572 $testarray = array('firstnamephonetic' => 'authorfirstnamephonetic',
2573 'lastnamephonetic' => 'authorlastnamephonetic',
2574 'middlename' => 'authormiddlename',
2575 'alternatename' => 'authoralternatename',
2576 'firstname' => 'authorfirstname',
2577 'lastname' => 'authorlastname');
2578 $this->assertEquals($testarray, get_all_user_name_fields(false, null, 'author'));
2580 // Additional name fields with an alias and a title - string.
2581 $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';
2582 $this->assertEquals($teststring, get_all_user_name_fields(true, 'u', null, 'author'));
2584 // Test the order parameter of the function.
2585 // Returning an array.
2586 $testarray = array('firstname' => 'firstname',
2587 'lastname' => 'lastname',
2588 'firstnamephonetic' => 'firstnamephonetic',
2589 'lastnamephonetic' => 'lastnamephonetic',
2590 'middlename' => 'middlename',
2591 'alternatename' => 'alternatename'
2593 $this->assertEquals($testarray, get_all_user_name_fields(false, null, null, null, true));
2595 // Returning a string.
2596 $teststring = 'firstname,lastname,firstnamephonetic,lastnamephonetic,middlename,alternatename';
2597 $this->assertEquals($teststring, get_all_user_name_fields(true, null, null, null, true));
2600 public function test_order_in_string() {
2601 $this->resetAfterTest();
2603 // Return an array in an order as they are encountered in a string.
2604 $valuearray = array('second', 'firsthalf', 'first');
2605 $formatstring = 'first firsthalf some other text (second)';
2606 $expectedarray = array('0' => 'first', '6' => 'firsthalf', '33' => 'second');
2607 $this->assertEquals($expectedarray, order_in_string($valuearray, $formatstring));
2609 // Try again with a different order for the format.
2610 $valuearray = array('second', 'firsthalf', 'first');
2611 $formatstring = 'firsthalf first second';
2612 $expectedarray = array('0' => 'firsthalf', '10' => 'first', '16' => 'second');
2613 $this->assertEquals($expectedarray, order_in_string($valuearray, $formatstring));
2615 // Try again with yet another different order for the format.
2616 $valuearray = array('second', 'firsthalf', 'first');
2617 $formatstring = 'start seconds away second firstquater first firsthalf';
2618 $expectedarray = array('19' => 'second', '38' => 'first', '44' => 'firsthalf');
2619 $this->assertEquals($expectedarray, order_in_string($valuearray, $formatstring));
2622 public function test_complete_user_login() {
2625 $this->resetAfterTest();
2626 $user = $this->getDataGenerator()->create_user();
2629 $sink = $this->redirectEvents();
2630 $loginuser = clone($user);
2631 $this->setCurrentTimeStart();
2632 @complete_user_login($loginuser); // Hide session header errors.
2633 $this->assertSame($loginuser, $USER);
2634 $this->assertEquals($user->id, $USER->id);
2635 $events = $sink->get_events();
2638 $this->assertCount(1, $events);
2639 $event = reset($events);
2640 $this->assertInstanceOf('\core\event\user_loggedin', $event);
2641 $this->assertEquals('user', $event->objecttable);
2642 $this->assertEquals($user->id, $event->objectid);
2643 $this->assertEquals(context_system::instance()->id, $event->contextid);
2644 $this->assertEventContextNotUsed($event);
2646 $user = $DB->get_record('user', array('id'=>$user->id));
2648 $this->assertTimeCurrent($user->firstaccess);
2649 $this->assertTimeCurrent($user->lastaccess);
2651 $this->assertTimeCurrent($USER->firstaccess);
2652 $this->assertTimeCurrent($USER->lastaccess);
2653 $this->assertTimeCurrent($USER->currentlogin);
2654 $this->assertSame(sesskey(), $USER->sesskey);
2655 $this->assertTimeCurrent($USER->preference['_lastloaded']);
2656 $this->assertObjectNotHasAttribute('password', $USER);
2657 $this->assertObjectNotHasAttribute('description', $USER);
2661 * Test require_logout.
2663 public function test_require_logout() {
2664 $this->resetAfterTest();
2665 $user = $this->getDataGenerator()->create_user();
2666 $this->setUser($user);
2668 $this->assertTrue(isloggedin());
2670 // Logout user and capture event.
2671 $sink = $this->redirectEvents();
2673 $events = $sink->get_events();
2675 $event = array_pop($events);
2677 // Check if user is logged out.
2678 $this->assertFalse(isloggedin());
2681 $this->assertInstanceOf('\core\event\user_loggedout', $event);
2682 $this->assertSame($user->id, $event->objectid);
2683 $this->assertSame('user_logout', $event->get_legacy_eventname());
2684 $this->assertEventLegacyData($user, $event);
2685 $expectedlogdata = array(SITEID, 'user', 'logout', 'view.php?id='.$event->objectid.'&course='.SITEID, $event->objectid, 0,
2687 $this->assertEventLegacyLogData($expectedlogdata, $event);
2688 $this->assertEventContextNotUsed($event);
2692 * A data provider for testing email messageid
2694 public function generate_email_messageid_provider() {
2697 'wwwroot' => 'http://www.example.com',
2699 'a-custom-id' => '<a-custom-id@www.example.com>',
2700 'an-id-with-/-a-slash' => '<an-id-with-%2F-a-slash@www.example.com>',
2704 'wwwroot' => 'http://www.example.com/path/subdir',
2706 'a-custom-id' => '<a-custom-id/path/subdir@www.example.com>',
2707 'an-id-with-/-a-slash' => '<an-id-with-%2F-a-slash/path/subdir@www.example.com>',
2714 * Test email message id generation
2716 * @dataProvider generate_email_messageid_provider
2718 * @param string $wwwroot The wwwroot
2719 * @param array $msgids An array of msgid local parts and the final result
2721 public function test_generate_email_messageid($wwwroot, $msgids) {
2724 $this->resetAfterTest();
2725 $CFG->wwwroot = $wwwroot;
2727 foreach ($msgids as $local => $final) {
2728 $this->assertEquals($final, generate_email_messageid($local));
2733 * A data provider for testing email diversion
2735 public function diverted_emails_provider() {
2737 'nodiverts' => array(
2738 'divertallemailsto' => null,
2739 'divertallemailsexcept' => null,
2743 'fred.jones@example.com',
2746 'fred+verp@example.com',
2750 'alldiverts' => array(
2751 'divertallemailsto' => 'somewhere@elsewhere.com',
2752 'divertallemailsexcept' => null,
2756 'fred.jones@example.com',
2759 'fred+verp@example.com',
2763 'alsodiverts' => array(
2764 'divertallemailsto' => 'somewhere@elsewhere.com',
2765 'divertallemailsexcept' => '@dev.com, fred(\+.*)?@example.com',
2769 'fred.jones@example.com',
2773 'divertsexceptions' => array(
2774 'divertallemailsto' => 'somewhere@elsewhere.com',
2775 'divertallemailsexcept' => '@dev.com, fred(\+.*)?@example.com',
2779 'fred+verp@example.com',
2787 * Test email diversion
2789 * @dataProvider diverted_emails_provider
2791 * @param string $divertallemailsto An optional email address
2792 * @param string $divertallemailsexcept An optional exclusion list
2793 * @param array $addresses An array of test addresses
2794 * @param boolean $expected Expected result
2796 public function test_email_should_be_diverted($divertallemailsto, $divertallemailsexcept, $addresses, $expected) {
2799 $this->resetAfterTest();
2800 $CFG->divertallemailsto = $divertallemailsto;
2801 $CFG->divertallemailsexcept = $divertallemailsexcept;
2803 foreach ($addresses as $address) {
2804 $this->assertEquals($expected, email_should_be_diverted($address));
2808 public function test_email_to_user() {
2811 $this->resetAfterTest();
2813 $user1 = $this->getDataGenerator()->create_user(array('maildisplay' => 1));
2814 $user2 = $this->getDataGenerator()->create_user(array('maildisplay' => 1));
2815 $user3 = $this->getDataGenerator()->create_user(array('maildisplay' => 0));
2816 set_config('allowedemaildomains', "example.com\r\nmoodle.org");
2818 $subject = 'subject';
2819 $messagetext = 'message text';
2820 $subject2 = 'subject 2';
2821 $messagetext2 = 'message text 2';
2823 // Close the default email sink.
2824 $sink = $this->redirectEmails();
2827 $CFG->noemailever = true;
2828 $this->assertNotEmpty($CFG->noemailever);
2829 email_to_user($user1, $user2, $subject, $messagetext);
2830 $this->assertDebuggingCalled('Not sending email due to $CFG->noemailever config setting');
2832 unset_config('noemailever');
2834 email_to_user($user1, $user2, $subject, $messagetext);
2835 $this->assertDebuggingCalled('Unit tests must not send real emails! Use $this->redirectEmails()');
2837 $sink = $this->redirectEmails();
2838 email_to_user($user1, $user2, $subject, $messagetext);
2839 email_to_user($user2, $user1, $subject2, $messagetext2);
2840 $this->assertSame(2, $sink->count());
2841 $result = $sink->get_messages();
2842 $this->assertCount(2, $result);
2845 $this->assertSame($subject, $result[0]->subject);
2846 $this->assertSame($messagetext, trim($result[0]->body));
2847 $this->assertSame($user1->email, $result[0]->to);
2848 $this->assertSame($user2->email, $result[0]->from);
2850 $this->assertSame($subject2, $result[1]->subject);
2851 $this->assertSame($messagetext2, trim($result[1]->body));
2852 $this->assertSame($user2->email, $result[1]->to);
2853 $this->assertSame($user1->email, $result[1]->from);
2855 email_to_user($user1, $user2, $subject, $messagetext);
2856 $this->assertDebuggingCalled('Unit tests must not send real emails! Use $this->redirectEmails()');
2858 // Test that an empty noreplyaddress will default to a no-reply address.
2859 $sink = $this->redirectEmails();
2860 email_to_user($user1, $user3, $subject, $messagetext);
2861 $result = $sink->get_messages();
2862 $this->assertEquals($CFG->noreplyaddress, $result[0]->from);
2864 set_config('noreplyaddress', '');
2865 $sink = $this->redirectEmails();
2866 email_to_user($user1, $user3, $subject, $messagetext);
2867 $result = $sink->get_messages();
2868 $this->assertEquals('noreply@www.example.com', $result[0]->from);
2871 // Test $CFG->allowedemaildomains.
2872 set_config('noreplyaddress', 'noreply@www.example.com');
2873 $this->assertNotEmpty($CFG->allowedemaildomains);
2874 $sink = $this->redirectEmails();
2875 email_to_user($user1, $user2, $subject, $messagetext);
2876 unset_config('allowedemaildomains');
2877 email_to_user($user1, $user2, $subject, $messagetext);
2878 $result = $sink->get_messages();
2879 $this->assertNotEquals($CFG->noreplyaddress, $result[0]->from);
2880 $this->assertEquals($CFG->noreplyaddress, $result[1]->from);
2883 // Try to send an unsafe attachment, we should see an error message in the eventual mail body.
2884 $attachment = '../test.txt';
2885 $attachname = 'txt';
2887 $sink = $this->redirectEmails();
2888 email_to_user($user1, $user2, $subject, $messagetext, '', $attachment, $attachname);
2889 $this->assertSame(1, $sink->count());
2890 $result = $sink->get_messages();
2891 $this->assertCount(1, $result);
2892 $this->assertContains('error.txt', $result[0]->body);
2893 $this->assertContains('Error in attachment. User attempted to attach a filename with a unsafe name.', $result[0]->body);
2898 * Test setnew_password_and_mail.
2900 public function test_setnew_password_and_mail() {
2903 $this->resetAfterTest();
2905 $user = $this->getDataGenerator()->create_user();
2907 // Update user password.
2908 $sink = $this->redirectEvents();
2909 $sink2 = $this->redirectEmails(); // Make sure we are redirecting emails.
2910 setnew_password_and_mail($user);
2911 $events = $sink->get_events();
2914 $event = array_pop($events);
2916 // Test updated value.
2917 $dbuser = $DB->get_record('user', array('id' => $user->id));
2918 $this->assertSame($user->firstname, $dbuser->firstname);
2919 $this->assertNotEmpty($dbuser->password);
2922 $this->assertInstanceOf('\core\event\user_password_updated', $event);
2923 $this->assertSame($user->id, $event->relateduserid);
2924 $this->assertEquals(context_user::instance($user->id), $event->get_context());
2925 $this->assertEventContextNotUsed($event);
2929 * Data provider for test_generate_confirmation_link
2930 * @return Array of confirmation urls and expected resultant confirmation links
2932 public function generate_confirmation_link_provider() {
2936 "username" => "simplename",
2937 "confirmationurl" => null,
2938 "expected" => $CFG->wwwroot . "/login/confirm.php?data=/simplename"
2940 "Period in between words in username" => [
2941 "username" => "period.inbetween",
2942 "confirmationurl" => null,
2943 "expected" => $CFG->wwwroot . "/login/confirm.php?data=/period%2Einbetween"
2945 "Trailing periods in username" => [
2946 "username" => "trailingperiods...",
2947 "confirmationurl" => null,
2948 "expected" => $CFG->wwwroot . "/login/confirm.php?data=/trailingperiods%2E%2E%2E"
2950 "At symbol in username" => [
2951 "username" => "at@symbol",
2952 "confirmationurl" => null,
2953 "expected" => $CFG->wwwroot . "/login/confirm.php?data=/at%40symbol"
2955 "Dash symbol in username" => [
2956 "username" => "has-dash",
2957 "confirmationurl" => null,
2958 "expected" => $CFG->wwwroot . "/login/confirm.php?data=/has-dash"
2960 "Underscore in username" => [
2961 "username" => "under_score",
2962 "confirmationurl" => null,
2963 "expected" => $CFG->wwwroot . "/login/confirm.php?data=/under_score"
2965 "Many different characters in username" => [
2966 "username" => "many_-.@characters@_@-..-..",
2967 "confirmationurl" => null,
2968 "expected" => $CFG->wwwroot . "/login/confirm.php?data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E"
2970 "Custom relative confirmation url" => [
2971 "username" => "many_-.@characters@_@-..-..",
2972 "confirmationurl" => "/custom/local/url.php",
2973 "expected" => $CFG->wwwroot . "/custom/local/url.php?data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E"
2975 "Custom relative confirmation url with parameters" => [
2976 "username" => "many_-.@characters@_@-..-..",
2977 "confirmationurl" => "/custom/local/url.php?with=param",
2978 "expected" => $CFG->wwwroot . "/custom/local/url.php?with=param&data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E"
2980 "Custom local confirmation url" => [
2981 "username" => "many_-.@characters@_@-..-..",
2982 "confirmationurl" => $CFG->wwwroot . "/custom/local/url.php",
2983 "expected" => $CFG->wwwroot . "/custom/local/url.php?data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E"
2985 "Custom local confirmation url with parameters" => [
2986 "username" => "many_-.@characters@_@-..-..",
2987 "confirmationurl" => $CFG->wwwroot . "/custom/local/url.php?with=param",
2988 "expected" => $CFG->wwwroot . "/custom/local/url.php?with=param&data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E"
2990 "Custom external confirmation url" => [
2991 "username" => "many_-.@characters@_@-..-..",
2992 "confirmationurl" => "http://moodle.org/custom/external/url.php",
2993 "expected" => "http://moodle.org/custom/external/url.php?data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E"
2995 "Custom external confirmation url with parameters" => [
2996 "username" => "many_-.@characters@_@-..-..",
2997 "confirmationurl" => "http://moodle.org/ext.php?with=some¶m=eters",
2998 "expected" => "http://moodle.org/ext.php?with=some¶m=eters&data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E"
3000 "Custom external confirmation url with parameters" => [
3001 "username" => "many_-.@characters@_@-..-..",
3002 "confirmationurl" => "http://moodle.org/ext.php?with=some&data=test",
3003 "expected" => "http://moodle.org/ext.php?with=some&data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E"
3009 * Test generate_confirmation_link
3010 * @dataProvider generate_confirmation_link_provider
3011 * @param string $username The name of the user
3012 * @param string $confirmationurl The url the user should go to to confirm
3013 * @param string $expected The expected url of the confirmation link
3015 public function test_generate_confirmation_link($username, $confirmationurl, $expected) {
3016 $this->resetAfterTest();
3017 $sink = $this->redirectEmails();
3019 $user = $this->getDataGenerator()->create_user(
3021 "username" => $username,
3022 "confirmed" => false,
3023 "email" => 'test@example.com',
3027 send_confirmation_email($user, $confirmationurl);
3029 $messages = $sink->get_messages();
3030 $message = array_shift($messages);
3031 $messagebody = quoted_printable_decode($message->body);
3033 $this->assertContains($expected, $messagebody);
3037 * Test generate_confirmation_link with custom admin link
3039 public function test_generate_confirmation_link_with_custom_admin() {
3042 $this->resetAfterTest();
3043 $sink = $this->redirectEmails();
3045 $admin = $CFG->admin;
3046 $CFG->admin = 'custom/admin/path';
3048 $user = $this->getDataGenerator()->create_user(
3050 "username" => "many_-.@characters@_@-..-..",
3051 "confirmed" => false,
3052 "email" => 'test@example.com',
3055 $confirmationurl = "/admin/test.php?with=params";
3056 $expected = $CFG->wwwroot . "/" . $CFG->admin . "/test.php?with=params&data=/many_-%2E%40characters%40_%40-%2E%2E-%2E%2E";
3058 send_confirmation_email($user, $confirmationurl);
3060 $messages = $sink->get_messages();
3061 $message = array_shift($messages);
3062 $messagebody = quoted_printable_decode($message->body);
3065 $this->assertContains($expected, $messagebody);
3067 $CFG->admin = $admin;
3072 * Test remove_course_content deletes course contents
3073 * TODO Add asserts to verify other data related to course is deleted as well.
3075 public function test_remove_course_contents() {
3077 $this->resetAfterTest();
3079 $course = $this->getDataGenerator()->create_course();
3080 $user = $this->getDataGenerator()->create_user();
3081 $gen = $this->getDataGenerator()->get_plugin_generator('core_notes');
3082 $note = $gen->create_instance(array('courseid' => $course->id, 'userid' => $user->id));
3084 $this->assertNotEquals(false, note_load($note->id));
3085 remove_course_contents($course->id, false);
3086 $this->assertFalse(note_load($note->id));
3090 * Test function username_load_fields_from_object().
3092 public function test_username_load_fields_from_object() {
3093 $this->resetAfterTest();
3095 // This object represents the information returned from an sql query.
3096 $userinfo = new stdClass();
3097 $userinfo->userid = 1;
3098 $userinfo->username = 'loosebruce';
3099 $userinfo->firstname = 'Bruce';
3100 $userinfo->lastname = 'Campbell';
3101 $userinfo->firstnamephonetic = 'ブルース';
3102 $userinfo->lastnamephonetic = 'カンベッル';
3103 $userinfo->middlename = '';
3104 $userinfo->alternatename = '';
3105 $userinfo->email = '';
3106 $userinfo->picture = 23;
3107 $userinfo->imagealt = 'Michael Jordan draining another basket.';
3108 $userinfo->idnumber = 3982;
3110 // Just user name fields.
3111 $user = new stdClass();
3112 $user = username_load_fields_from_object($user, $userinfo);
3113 $expectedarray = new stdClass();
3114 $expectedarray->firstname = 'Bruce';
3115 $expectedarray->lastname = 'Campbell';
3116 $expectedarray->firstnamephonetic = 'ブルース';
3117 $expectedarray->lastnamephonetic = 'カンベッル';
3118 $expectedarray->middlename = '';
3119 $expectedarray->alternatename = '';
3120 $this->assertEquals($user, $expectedarray);
3122 // User information for showing a picture.
3123 $user = new stdClass();
3124 $additionalfields = explode(',', user_picture::fields());
3125 $user = username_load_fields_from_object($user, $userinfo, null, $additionalfields);
3126 $user->id = $userinfo->userid;
3127 $expectedarray = new stdClass();
3128 $expectedarray->id = 1;
3129 $expectedarray->firstname = 'Bruce';
3130 $expectedarray->lastname = 'Campbell';
3131 $expectedarray->firstnamephonetic = 'ブルース';
3132 $expectedarray->lastnamephonetic = 'カンベッル';
3133 $expectedarray->middlename = '';
3134 $expectedarray->alternatename = '';
3135 $expectedarray->email = '';
3136 $expectedarray->picture = 23;
3137 $expectedarray->imagealt = 'Michael Jordan draining another basket.';
3138 $this->assertEquals($user, $expectedarray);
3140 // Alter the userinfo object to have a prefix.
3141 $userinfo->authorfirstname = 'Bruce';
3142 $userinfo->authorlastname = 'Campbell';
3143 $userinfo->authorfirstnamephonetic = 'ブルース';
3144 $userinfo->authorlastnamephonetic = 'カンベッル';
3145 $userinfo->authormiddlename = '';
3146 $userinfo->authorpicture = 23;
3147 $userinfo->authorimagealt = 'Michael Jordan draining another basket.';
3148 $userinfo->authoremail = 'test@example.com';
3151 // Return an object with user picture information.
3152 $user = new stdClass();
3153 $additionalfields = explode(',', user_picture::fields());
3154 $user = username_load_fields_from_object($user, $userinfo, 'author', $additionalfields);
3155 $user->id = $userinfo->userid;
3156 $expectedarray = new stdClass();
3157 $expectedarray->id = 1;
3158 $expectedarray->firstname = 'Bruce';
3159 $expectedarray->lastname = 'Campbell';
3160 $expectedarray->firstnamephonetic = 'ブルース';
3161 $expectedarray->lastnamephonetic = 'カンベッル';
3162 $expectedarray->middlename = '';
3163 $expectedarray->alternatename = '';
3164 $expectedarray->email = 'test@example.com';
3165 $expectedarray->picture = 23;
3166 $expectedarray->imagealt = 'Michael Jordan draining another basket.';
3167 $this->assertEquals($user, $expectedarray);
3171 * Test function count_words().
3173 public function test_count_words() {
3174 $count = count_words("one two three'four");
3175 $this->assertEquals(3, $count);
3177 $count = count_words('one+two three’four');
3178 $this->assertEquals(3, $count);
3180 $count = count_words('one"two three-four');
3181 $this->assertEquals(2, $count);
3183 $count = count_words('one@two three_four');
3184 $this->assertEquals(4, $count);
3186 $count = count_words('one\two three/four');
3187 $this->assertEquals(4, $count);
3189 $count = count_words(' one ... two three...four ');
3190 $this->assertEquals(4, $count);
3192 $count = count_words('one.2 3,four');
3193 $this->assertEquals(4, $count);
3195 $count = count_words('1³ £2 €3.45 $6,789');
3196 $this->assertEquals(4, $count);
3198 $count = count_words('one—two ブルース カンベッル');
3199 $this->assertEquals(4, $count);
3201 $count = count_words('one…two ブルース … カンベッル');
3202 $this->assertEquals(4, $count);
3205 * Tests the getremoteaddr() function.
3207 public function test_getremoteaddr() {
3208 $xforwardedfor = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : null;
3210 $_SERVER['HTTP_X_FORWARDED_FOR'] = '';
3211 $noip = getremoteaddr('1.1.1.1');
3212 $this->assertEquals('1.1.1.1', $noip);
3214 $_SERVER['HTTP_X_FORWARDED_FOR'] = '';
3215 $noip = getremoteaddr();
3216 $this->assertEquals('0.0.0.0', $noip);
3218 $_SERVER['HTTP_X_FORWARDED_FOR'] = '127.0.0.1';
3219 $singleip = getremoteaddr();
3220 $this->assertEquals('127.0.0.1', $singleip);
3222 $_SERVER['HTTP_X_FORWARDED_FOR'] = '127.0.0.1,127.0.0.2';
3223 $twoip = getremoteaddr();
3224 $this->assertEquals('127.0.0.1', $twoip);
3226 $_SERVER['HTTP_X_FORWARDED_FOR'] = '127.0.0.1,127.0.0.2, 127.0.0.3';
3227 $threeip = getremoteaddr();
3228 $this->assertEquals('127.0.0.1', $threeip);
3230 $_SERVER['HTTP_X_FORWARDED_FOR'] = '127.0.0.1:65535,127.0.0.2';
3231 $portip = getremoteaddr();
3232 $this->assertEquals('127.0.0.1', $portip);
3234 $_SERVER['HTTP_X_FORWARDED_FOR'] = '0:0:0:0:0:0:0:1,127.0.0.2';
3235 $portip = getremoteaddr();
3236 $this->assertEquals('0:0:0:0:0:0:0:1', $portip);
3238 $_SERVER['HTTP_X_FORWARDED_FOR'] = '0::1,127.0.0.2';
3239 $portip = getremoteaddr();
3240 $this->assertEquals('0:0:0:0:0:0:0:1', $portip);
3242 $_SERVER['HTTP_X_FORWARDED_FOR'] = '[0:0:0:0:0:0:0:1]:65535,127.0.0.2';
3243 $portip = getremoteaddr();
3244 $this->assertEquals('0:0:0:0:0:0:0:1', $portip);