205
144
$this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:'));
206
145
$this->assertFalse(address_in_subnet('bab:baba::bab', 'baba:'));
210
148
$this->assertTrue(address_in_subnet('123.121.234.12', '::1/64, 124., 123.121.234.10-30'));
211
149
$this->assertTrue(address_in_subnet('124.121.234.12', '::1/64, 124., 123.121.234.10-30'));
212
150
$this->assertTrue(address_in_subnet('::2', '::1/64, 124., 123.121.234.10-30'));
213
151
$this->assertFalse(address_in_subnet('12.121.234.12', '::1/64, 124., 123.121.234.10-30'));
216
/// other incorrect input
153
// Other incorrect input.
217
154
$this->assertFalse(address_in_subnet('123.123.123.123', ''));
221
* Modifies $_SERVER['HTTP_USER_AGENT'] manually to check if check_browser_version
224
function test_check_browser_version()
228
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['412']['Mac OS X'];
229
$this->assertTrue(check_browser_version('Safari'));
230
$this->assertTrue(check_browser_version('WebKit'));
231
$this->assertTrue(check_browser_version('Safari', '312'));
232
$this->assertFalse(check_browser_version('Safari', '500'));
233
$this->assertFalse(check_browser_version('Chrome'));
234
$this->assertFalse(check_browser_version('Safari iOS'));
236
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari iOS']['528']['iPhone'];
237
$this->assertTrue(check_browser_version('Safari iOS'));
238
$this->assertTrue(check_browser_version('WebKit'));
239
$this->assertTrue(check_browser_version('Safari iOS', '527'));
240
$this->assertFalse(check_browser_version('Safari iOS', 590));
241
$this->assertFalse(check_browser_version('Safari', '312'));
242
$this->assertFalse(check_browser_version('Safari', '500'));
243
$this->assertFalse(check_browser_version('Chrome'));
245
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['WebKit Android']['530']['Nexus'];
246
$this->assertTrue(check_browser_version('WebKit'));
247
$this->assertTrue(check_browser_version('WebKit Android', '527'));
248
$this->assertFalse(check_browser_version('WebKit Android', 590));
249
$this->assertFalse(check_browser_version('Safari'));
250
$this->assertFalse(check_browser_version('Chrome'));
252
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Chrome']['8']['Mac OS X'];
253
$this->assertTrue(check_browser_version('Chrome'));
254
$this->assertTrue(check_browser_version('WebKit'));
255
$this->assertTrue(check_browser_version('Chrome', 8));
256
$this->assertFalse(check_browser_version('Chrome', 10));
257
$this->assertFalse(check_browser_version('Safari', '1'));
259
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
260
$this->assertTrue(check_browser_version('Opera'));
261
$this->assertTrue(check_browser_version('Opera', '8.0'));
262
$this->assertFalse(check_browser_version('Opera', '10.0'));
264
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
265
$this->assertTrue(check_browser_version('MSIE'));
266
$this->assertTrue(check_browser_version('MSIE', '5.0'));
267
$this->assertFalse(check_browser_version('MSIE', '7.0'));
269
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['5.0']['Windows 98'];
270
$this->assertFalse(check_browser_version('MSIE'));
271
$this->assertTrue(check_browser_version('MSIE', 0));
272
$this->assertTrue(check_browser_version('MSIE', '5.0'));
273
$this->assertFalse(check_browser_version('MSIE', '7.0'));
275
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['9.0']['Windows 7'];
276
$this->assertTrue(check_browser_version('MSIE'));
277
$this->assertTrue(check_browser_version('MSIE', 0));
278
$this->assertTrue(check_browser_version('MSIE', '5.0'));
279
$this->assertTrue(check_browser_version('MSIE', '9.0'));
280
$this->assertFalse(check_browser_version('MSIE', '10'));
282
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['9.0i']['Windows 7'];
283
$this->assertTrue(check_browser_version('MSIE'));
284
$this->assertTrue(check_browser_version('MSIE', 0));
285
$this->assertTrue(check_browser_version('MSIE', '5.0'));
286
$this->assertTrue(check_browser_version('MSIE', '9.0'));
287
$this->assertFalse(check_browser_version('MSIE', '10'));
289
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['10.0']['Windows 8'];
290
$this->assertTrue(check_browser_version('MSIE'));
291
$this->assertTrue(check_browser_version('MSIE', 0));
292
$this->assertTrue(check_browser_version('MSIE', '5.0'));
293
$this->assertTrue(check_browser_version('MSIE', '9.0'));
294
$this->assertTrue(check_browser_version('MSIE', '10'));
295
$this->assertFalse(check_browser_version('MSIE', '11'));
297
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['10.0i']['Windows 8'];
298
$this->assertTrue(check_browser_version('MSIE'));
299
$this->assertTrue(check_browser_version('MSIE', 0));
300
$this->assertTrue(check_browser_version('MSIE', '5.0'));
301
$this->assertTrue(check_browser_version('MSIE', '9.0'));
302
$this->assertTrue(check_browser_version('MSIE', '10'));
303
$this->assertFalse(check_browser_version('MSIE', '11'));
305
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['11.0']['Windows 8.1'];
306
$this->assertTrue(check_browser_version('MSIE'));
307
$this->assertTrue(check_browser_version('MSIE', 0));
308
$this->assertTrue(check_browser_version('MSIE', '5.0'));
309
$this->assertTrue(check_browser_version('MSIE', '9.0'));
310
$this->assertTrue(check_browser_version('MSIE', '10'));
311
$this->assertTrue(check_browser_version('MSIE', '11'));
312
$this->assertFalse(check_browser_version('MSIE', '12'));
314
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
315
$this->assertTrue(check_browser_version('Firefox'));
316
$this->assertTrue(check_browser_version('Firefox', '1.5'));
317
$this->assertFalse(check_browser_version('Firefox', '3.0'));
318
$this->assertTrue(check_browser_version('Gecko', '2'));
319
$this->assertTrue(check_browser_version('Gecko', 20030516));
320
$this->assertTrue(check_browser_version('Gecko', 20051106));
321
$this->assertTrue(check_browser_version('Gecko', 2006010100));
323
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['1.0.6']['Windows XP'];
324
$this->assertTrue(check_browser_version('Firefox'));
325
$this->assertTrue(check_browser_version('Gecko', '1'));
326
$this->assertFalse(check_browser_version('Gecko', 20030516));
327
$this->assertFalse(check_browser_version('Gecko', 20051106));
328
$this->assertFalse(check_browser_version('Gecko', 2006010100));
329
$this->assertFalse(check_browser_version('Firefox', '1.5'));
330
$this->assertFalse(check_browser_version('Firefox', '3.0'));
331
$this->assertFalse(check_browser_version('Gecko', '2'));
333
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
334
$this->assertTrue(check_browser_version('Firefox'));
335
$this->assertTrue(check_browser_version('Firefox', '1.5'));
336
$this->assertTrue(check_browser_version('Gecko', '1'));
337
$this->assertTrue(check_browser_version('Gecko', '2'));
338
$this->assertTrue(check_browser_version('Gecko', 20030516));
339
$this->assertTrue(check_browser_version('Gecko', 20051106));
340
$this->assertTrue(check_browser_version('Gecko', 2006010100));
341
$this->assertFalse(check_browser_version('Firefox', '3.0'));
343
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['3.6']['Linux'];
344
$this->assertTrue(check_browser_version('Firefox'));
345
$this->assertTrue(check_browser_version('Firefox', '1.5'));
346
$this->assertTrue(check_browser_version('Firefox', '3.0'));
347
$this->assertTrue(check_browser_version('Gecko', '2'));
348
$this->assertTrue(check_browser_version('Gecko', '3.6'));
349
$this->assertTrue(check_browser_version('Gecko', 20030516));
350
$this->assertTrue(check_browser_version('Gecko', 20051106));
351
$this->assertTrue(check_browser_version('Gecko', 2006010100));
352
$this->assertFalse(check_browser_version('Firefox', '4'));
353
$this->assertFalse(check_browser_version('Firefox', '10'));
355
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['3.6']['Linux'];
356
$this->assertTrue(check_browser_version('Firefox'));
357
$this->assertTrue(check_browser_version('Firefox', '1.5'));
358
$this->assertTrue(check_browser_version('Firefox', '3.0'));
359
$this->assertTrue(check_browser_version('Gecko', '2'));
360
$this->assertTrue(check_browser_version('Gecko', '3.6'));
361
$this->assertTrue(check_browser_version('Gecko', 20030516));
362
$this->assertTrue(check_browser_version('Gecko', 20051106));
363
$this->assertTrue(check_browser_version('Gecko', 2006010100));
364
$this->assertFalse(check_browser_version('Firefox', '4'));
365
$this->assertFalse(check_browser_version('Firefox', '10'));
366
$this->assertFalse(check_browser_version('Firefox', '18'));
367
$this->assertFalse(check_browser_version('Gecko', '4'));
369
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['15.0a2']['Windows'];
370
$this->assertTrue(check_browser_version('Firefox'));
371
$this->assertTrue(check_browser_version('Firefox', '1.5'));
372
$this->assertTrue(check_browser_version('Firefox', '3.0'));
373
$this->assertTrue(check_browser_version('Gecko', '2'));
374
$this->assertTrue(check_browser_version('Gecko', '3.6'));
375
$this->assertTrue(check_browser_version('Gecko', '15.0'));
376
$this->assertTrue(check_browser_version('Gecko', 20030516));
377
$this->assertTrue(check_browser_version('Gecko', 20051106));
378
$this->assertTrue(check_browser_version('Gecko', 2006010100));
379
$this->assertTrue(check_browser_version('Firefox', '4'));
380
$this->assertTrue(check_browser_version('Firefox', '10'));
381
$this->assertTrue(check_browser_version('Firefox', '15'));
382
$this->assertFalse(check_browser_version('Firefox', '18'));
383
$this->assertFalse(check_browser_version('Gecko', '18'));
385
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['18.0']['Mac OS X'];
386
$this->assertTrue(check_browser_version('Firefox'));
387
$this->assertTrue(check_browser_version('Firefox', '1.5'));
388
$this->assertTrue(check_browser_version('Firefox', '3.0'));
389
$this->assertTrue(check_browser_version('Gecko', '2'));
390
$this->assertTrue(check_browser_version('Gecko', '3.6'));
391
$this->assertTrue(check_browser_version('Gecko', '15.0'));
392
$this->assertTrue(check_browser_version('Gecko', '18.0'));
393
$this->assertTrue(check_browser_version('Gecko', 20030516));
394
$this->assertTrue(check_browser_version('Gecko', 20051106));
395
$this->assertTrue(check_browser_version('Gecko', 2006010100));
396
$this->assertTrue(check_browser_version('Firefox', '4'));
397
$this->assertTrue(check_browser_version('Firefox', '10'));
398
$this->assertTrue(check_browser_version('Firefox', '15'));
399
$this->assertTrue(check_browser_version('Firefox', '18'));
400
$this->assertFalse(check_browser_version('Firefox', '19'));
401
$this->assertFalse(check_browser_version('Gecko', '19'));
403
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['SeaMonkey']['2.0']['Windows'];
405
$this->assertTrue(check_browser_version('Gecko', '2'));
406
$this->assertTrue(check_browser_version('Gecko', 20030516));
407
$this->assertTrue(check_browser_version('Gecko', 20051106));
408
$this->assertTrue(check_browser_version('Gecko', 2006010100));
409
$this->assertFalse(check_browser_version('Gecko', '3.6'));
410
$this->assertFalse(check_browser_version('Gecko', '4.0'));
411
$this->assertFalse(check_browser_version('Firefox'));
413
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['SeaMonkey']['2.1']['Linux'];
414
$this->assertTrue(check_browser_version('Gecko', '2'));
415
$this->assertTrue(check_browser_version('Gecko', '3.6'));
416
$this->assertTrue(check_browser_version('Gecko', '4.0'));
417
$this->assertTrue(check_browser_version('Gecko', 20030516));
418
$this->assertTrue(check_browser_version('Gecko', 20051106));
419
$this->assertTrue(check_browser_version('Gecko', 2006010100));
420
$this->assertTrue(check_browser_version('Firefox'));
421
$this->assertTrue(check_browser_version('Firefox', 4.0));
422
$this->assertFalse(check_browser_version('Firefox', 5));
423
$this->assertFalse(check_browser_version('Gecko', '18.0'));
427
function test_get_browser_version_classes() {
428
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['412']['Mac OS X'];
429
$this->assertEquals(array('safari'), get_browser_version_classes());
431
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Chrome']['8']['Mac OS X'];
432
$this->assertEquals(array('safari'), get_browser_version_classes());
434
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari iOS']['528']['iPhone'];
435
$this->assertEquals(array('safari', 'ios'), get_browser_version_classes());
437
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['WebKit Android']['530']['Nexus'];
438
$this->assertEquals(array('safari', 'android'), get_browser_version_classes());
440
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Chrome']['8']['Mac OS X'];
441
$this->assertEquals(array('safari'), get_browser_version_classes());
443
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
444
$this->assertEquals(array('opera'), get_browser_version_classes());
446
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
447
$this->assertEquals(array('ie', 'ie6'), get_browser_version_classes());
449
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['7.0']['Windows XP SP2'];
450
$this->assertEquals(array('ie', 'ie7'), get_browser_version_classes());
452
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['8.0']['Windows Vista'];
453
$this->assertEquals(array('ie', 'ie8'), get_browser_version_classes());
455
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['9.0']['Windows 7'];
456
$this->assertEquals(array('ie', 'ie9'), get_browser_version_classes());
458
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['9.0i']['Windows 7'];
459
$this->assertEquals(array('ie', 'ie9'), get_browser_version_classes());
461
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['10.0']['Windows 8'];
462
$this->assertEquals(array('ie', 'ie10'), get_browser_version_classes());
464
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['10.0i']['Windows 8'];
465
$this->assertEquals(array('ie', 'ie10'), get_browser_version_classes());
467
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
468
$this->assertEquals(array('gecko', 'gecko18'), get_browser_version_classes());
470
$_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['3.0.6']['SUSE'];
471
$this->assertEquals(array('gecko', 'gecko19'), get_browser_version_classes());
474
function test_get_device_type() {
475
// IE8 (common pattern ~1.5% of IE7/8 users have embedded IE6 agent))
476
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BT Openworld BB; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; Hotbar 10.2.197.0; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727)';
477
$this->assertEquals('default', get_device_type());
479
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 6.0; AOL 9.0; Windows NT 5.1; SV1; FunWebProducts; .NET CLR 1.0.3705; Media Center PC 2.8)';
480
$this->assertEquals('legacy', get_device_type());
483
function test_fix_utf8() {
484
// make sure valid data including other types is not changed
157
public function test_fix_utf8() {
158
// Make sure valid data including other types is not changed.
485
159
$this->assertSame(null, fix_utf8(null));
486
160
$this->assertSame(1, fix_utf8(1));
487
161
$this->assertSame(1.1, fix_utf8(1.1));
498
172
// valid utf8 string
499
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"));
501
// invalid utf8 string
175
// Invalid utf8 string.
502
176
$this->assertSame('aš', fix_utf8('a'.chr(130).'š'), 'This fails with buggy iconv() when mbstring extenstion is not available as fallback.');
505
function test_optional_param() {
179
public function test_optional_param() {
508
182
$_POST['username'] = 'post_user';
509
183
$_GET['username'] = 'get_user';
510
$this->assertSame(optional_param('username', 'default_user', PARAM_RAW), $_POST['username']);
184
$this->assertSame($_POST['username'], optional_param('username', 'default_user', PARAM_RAW));
512
186
unset($_POST['username']);
513
$this->assertSame(optional_param('username', 'default_user', PARAM_RAW), $_GET['username']);
187
$this->assertSame($_GET['username'], optional_param('username', 'default_user', PARAM_RAW));
515
189
unset($_GET['username']);
516
$this->assertSame(optional_param('username', 'default_user', PARAM_RAW), 'default_user');
190
$this->assertSame('default_user', optional_param('username', 'default_user', PARAM_RAW));
518
// make sure exception is triggered when some params are missing, hide error notices here - new in 2.2
192
// Make sure exception is triggered when some params are missing, hide error notices here - new in 2.2.
519
193
$_POST['username'] = 'post_user';
521
195
optional_param('username', 'default_user', null);
522
196
$this->fail('coding_exception expected');
523
} catch (coding_exception $ex) {
524
$this->assertTrue(true);
197
} catch (moodle_exception $ex) {
198
$this->assertInstanceOf('coding_exception', $ex);
527
201
@optional_param('username', 'default_user');
528
202
$this->fail('coding_exception expected');
529
} catch (coding_exception $ex) {
530
$this->assertTrue(true);
203
} catch (moodle_exception $ex) {
204
$this->assertInstanceOf('coding_exception', $ex);
533
207
@optional_param('username');
534
208
$this->fail('coding_exception expected');
535
} catch (coding_exception $ex) {
536
$this->assertTrue(true);
209
} catch (moodle_exception $ex) {
210
$this->assertInstanceOf('coding_exception', $ex);
539
213
optional_param('', 'default_user', PARAM_RAW);
540
214
$this->fail('coding_exception expected');
541
} catch (coding_exception $ex) {
542
$this->assertTrue(true);
215
} catch (moodle_exception $ex) {
216
$this->assertInstanceOf('coding_exception', $ex);
545
// make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3
219
// Make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3.
546
220
$_POST['username'] = array('a'=>'a');
547
$this->assertSame(optional_param('username', 'default_user', PARAM_RAW), $_POST['username']);
221
$this->assertSame($_POST['username'], optional_param('username', 'default_user', PARAM_RAW));
548
222
$this->assertDebuggingCalled();
551
function test_optional_param_array() {
225
public function test_optional_param_array() {
554
228
$_POST['username'] = array('a'=>'post_user');
555
229
$_GET['username'] = array('a'=>'get_user');
556
$this->assertSame(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), $_POST['username']);
230
$this->assertSame($_POST['username'], optional_param_array('username', array('a'=>'default_user'), PARAM_RAW));
558
232
unset($_POST['username']);
559
$this->assertSame(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), $_GET['username']);
233
$this->assertSame($_GET['username'], optional_param_array('username', array('a'=>'default_user'), PARAM_RAW));
561
235
unset($_GET['username']);
562
$this->assertSame(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), array('a'=>'default_user'));
236
$this->assertSame(array('a'=>'default_user'), optional_param_array('username', array('a'=>'default_user'), PARAM_RAW));
564
// make sure exception is triggered when some params are missing, hide error notices here - new in 2.2
238
// Make sure exception is triggered when some params are missing, hide error notices here - new in 2.2.
565
239
$_POST['username'] = array('a'=>'post_user');
567
241
optional_param_array('username', array('a'=>'default_user'), null);
568
242
$this->fail('coding_exception expected');
569
} catch (coding_exception $ex) {
570
$this->assertTrue(true);
243
} catch (moodle_exception $ex) {
244
$this->assertInstanceOf('coding_exception', $ex);
573
247
@optional_param_array('username', array('a'=>'default_user'));
574
248
$this->fail('coding_exception expected');
575
} catch (coding_exception $ex) {
576
$this->assertTrue(true);
249
} catch (moodle_exception $ex) {
250
$this->assertInstanceOf('coding_exception', $ex);
579
253
@optional_param_array('username');
580
254
$this->fail('coding_exception expected');
581
} catch (coding_exception $ex) {
582
$this->assertTrue(true);
255
} catch (moodle_exception $ex) {
256
$this->assertInstanceOf('coding_exception', $ex);
585
259
optional_param_array('', array('a'=>'default_user'), PARAM_RAW);
586
260
$this->fail('coding_exception expected');
587
} catch (coding_exception $ex) {
588
$this->assertTrue(true);
261
} catch (moodle_exception $ex) {
262
$this->assertInstanceOf('coding_exception', $ex);
591
// do not allow nested arrays
265
// Do not allow nested arrays.
593
267
$_POST['username'] = array('a'=>array('b'=>'post_user'));
594
268
optional_param_array('username', array('a'=>'default_user'), PARAM_RAW);
597
271
$this->assertTrue(true);
600
// do not allow non-arrays
274
// Do not allow non-arrays.
601
275
$_POST['username'] = 'post_user';
602
$this->assertSame(optional_param_array('username', array('a'=>'default_user'), PARAM_RAW), array('a'=>'default_user'));
276
$this->assertSame(array('a'=>'default_user'), optional_param_array('username', array('a'=>'default_user'), PARAM_RAW));
603
277
$this->assertDebuggingCalled();
605
// make sure array keys are sanitised
279
// Make sure array keys are sanitised.
606
280
$_POST['username'] = array('abc123_;-/*-+ '=>'arrggh', 'a1_-'=>'post_user');
607
$this->assertSame(optional_param_array('username', array(), PARAM_RAW), array('a1_-'=>'post_user'));
281
$this->assertSame(array('a1_-'=>'post_user'), optional_param_array('username', array(), PARAM_RAW));
608
282
$this->assertDebuggingCalled();
611
function test_required_param() {
285
public function test_required_param() {
614
286
$_POST['username'] = 'post_user';
615
287
$_GET['username'] = 'get_user';
616
$this->assertSame(required_param('username', PARAM_RAW), 'post_user');
288
$this->assertSame('post_user', required_param('username', PARAM_RAW));
618
290
unset($_POST['username']);
619
$this->assertSame(required_param('username', PARAM_RAW), 'get_user');
291
$this->assertSame('get_user', required_param('username', PARAM_RAW));
621
293
unset($_GET['username']);
623
$this->assertSame(required_param('username', PARAM_RAW), 'default_user');
295
$this->assertSame('default_user', required_param('username', PARAM_RAW));
624
296
$this->fail('moodle_exception expected');
625
297
} catch (moodle_exception $ex) {
626
$this->assertTrue(true);
298
$this->assertInstanceOf('moodle_exception', $ex);
629
// make sure exception is triggered when some params are missing, hide error notices here - new in 2.2
301
// Make sure exception is triggered when some params are missing, hide error notices here - new in 2.2.
630
302
$_POST['username'] = 'post_user';
632
304
@required_param('username');
633
305
$this->fail('coding_exception expected');
634
} catch (coding_exception $ex) {
635
$this->assertTrue(true);
306
} catch (moodle_exception $ex) {
307
$this->assertInstanceOf('coding_exception', $ex);
638
310
required_param('username', '');
639
311
$this->fail('coding_exception expected');
640
} catch (coding_exception $ex) {
641
$this->assertTrue(true);
312
} catch (moodle_exception $ex) {
313
$this->assertInstanceOf('coding_exception', $ex);
644
316
required_param('', PARAM_RAW);
645
317
$this->fail('coding_exception expected');
646
} catch (coding_exception $ex) {
647
$this->assertTrue(true);
318
} catch (moodle_exception $ex) {
319
$this->assertInstanceOf('coding_exception', $ex);
650
// make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3
322
// Make sure warning is displayed if array submitted - TODO: throw exception in Moodle 2.3.
651
323
$_POST['username'] = array('a'=>'a');
652
$this->assertSame(required_param('username', PARAM_RAW), $_POST['username']);
324
$this->assertSame($_POST['username'], required_param('username', PARAM_RAW));
653
325
$this->assertDebuggingCalled();
656
function test_required_param_array() {
328
public function test_required_param_array() {
659
331
$_POST['username'] = array('a'=>'post_user');
660
332
$_GET['username'] = array('a'=>'get_user');
661
$this->assertSame(required_param_array('username', PARAM_RAW), $_POST['username']);
333
$this->assertSame($_POST['username'], required_param_array('username', PARAM_RAW));
663
335
unset($_POST['username']);
664
$this->assertSame(required_param_array('username', PARAM_RAW), $_GET['username']);
336
$this->assertSame($_GET['username'], required_param_array('username', PARAM_RAW));
666
// make sure exception is triggered when some params are missing, hide error notices here - new in 2.2
338
// Make sure exception is triggered when some params are missing, hide error notices here - new in 2.2.
667
339
$_POST['username'] = array('a'=>'post_user');
669
341
required_param_array('username', null);
670
342
$this->fail('coding_exception expected');
671
} catch (coding_exception $ex) {
672
$this->assertTrue(true);
343
} catch (moodle_exception $ex) {
344
$this->assertInstanceOf('coding_exception', $ex);
675
347
@required_param_array('username');
676
348
$this->fail('coding_exception expected');
677
} catch (coding_exception $ex) {
678
$this->assertTrue(true);
349
} catch (moodle_exception $ex) {
350
$this->assertInstanceOf('coding_exception', $ex);
681
353
required_param_array('', PARAM_RAW);
682
354
$this->fail('coding_exception expected');
683
} catch (coding_exception $ex) {
684
$this->assertTrue(true);
355
} catch (moodle_exception $ex) {
356
$this->assertInstanceOf('coding_exception', $ex);
687
// do not allow nested arrays
359
// Do not allow nested arrays.
689
361
$_POST['username'] = array('a'=>array('b'=>'post_user'));
690
362
required_param_array('username', PARAM_RAW);
691
363
$this->fail('coding_exception expected');
692
} catch (coding_exception $ex) {
693
$this->assertTrue(true);
364
} catch (moodle_exception $ex) {
365
$this->assertInstanceOf('coding_exception', $ex);
696
// do not allow non-arrays
368
// Do not allow non-arrays.
698
370
$_POST['username'] = 'post_user';
699
371
required_param_array('username', PARAM_RAW);
700
372
$this->fail('moodle_exception expected');
701
373
} catch (moodle_exception $ex) {
702
$this->assertTrue(true);
374
$this->assertInstanceOf('moodle_exception', $ex);
705
// make sure array keys are sanitised
377
// Make sure array keys are sanitised.
706
378
$_POST['username'] = array('abc123_;-/*-+ '=>'arrggh', 'a1_-'=>'post_user');
707
$this->assertSame(required_param_array('username', PARAM_RAW), array('a1_-'=>'post_user'));
379
$this->assertSame(array('a1_-'=>'post_user'), required_param_array('username', PARAM_RAW));
708
380
$this->assertDebuggingCalled();
711
function test_clean_param() {
712
// forbid objects and arrays
383
public function test_clean_param() {
384
// Forbid objects and arrays.
714
386
clean_param(array('x', 'y'), PARAM_RAW);
715
387
$this->fail('coding_exception expected');
716
} catch (coding_exception $ex) {
717
$this->assertTrue(true);
388
} catch (moodle_exception $ex) {
389
$this->assertInstanceOf('coding_exception', $ex);
720
392
$param = new stdClass();
722
394
clean_param($param, PARAM_RAW);
723
395
$this->fail('coding_exception expected');
724
} catch (coding_exception $ex) {
725
$this->assertTrue(true);
396
} catch (moodle_exception $ex) {
397
$this->assertInstanceOf('coding_exception', $ex);
728
// require correct type
400
// Require correct type.
730
402
clean_param('x', 'xxxxxx');
731
403
$this->fail('moodle_exception expected');
732
404
} catch (moodle_exception $ex) {
733
$this->assertTrue(true);
405
$this->assertInstanceOf('moodle_exception', $ex);
736
408
@clean_param('x');
737
409
$this->fail('moodle_exception expected');
738
410
} catch (moodle_exception $ex) {
739
$this->assertTrue(true);
411
$this->assertInstanceOf('moodle_exception', $ex);
744
function test_clean_param_array() {
745
$this->assertSame(clean_param_array(null, PARAM_RAW), array());
746
$this->assertSame(clean_param_array(array('a', 'b'), PARAM_RAW), array('a', 'b'));
747
$this->assertSame(clean_param_array(array('a', array('b')), PARAM_RAW, true), array('a', array('b')));
415
public function test_clean_param_array() {
416
$this->assertSame(array(), clean_param_array(null, PARAM_RAW));
417
$this->assertSame(array('a', 'b'), clean_param_array(array('a', 'b'), PARAM_RAW));
418
$this->assertSame(array('a', array('b')), clean_param_array(array('a', array('b')), PARAM_RAW, true));
749
// require correct type
420
// Require correct type.
751
422
clean_param_array(array('x'), 'xxxxxx');
752
423
$this->fail('moodle_exception expected');
753
424
} catch (moodle_exception $ex) {
754
$this->assertTrue(true);
425
$this->assertInstanceOf('moodle_exception', $ex);
757
428
@clean_param_array(array('x'));
758
429
$this->fail('moodle_exception expected');
759
430
} catch (moodle_exception $ex) {
760
$this->assertTrue(true);
431
$this->assertInstanceOf('moodle_exception', $ex);
764
435
clean_param_array(array('x', array('y')), PARAM_RAW);
765
436
$this->fail('coding_exception expected');
766
} catch (coding_exception $ex) {
767
$this->assertTrue(true);
437
} catch (moodle_exception $ex) {
438
$this->assertInstanceOf('coding_exception', $ex);
773
function test_clean_param_raw() {
774
$this->assertEquals(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_RAW),
775
'#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)');
778
function test_clean_param_trim() {
779
$this->assertEquals(clean_param(" Frog toad \r\n ", PARAM_RAW_TRIMMED), 'Frog toad');
782
function test_clean_param_clean() {
783
// PARAM_CLEAN is an ugly hack, do not use in new code (skodak)
784
// instead use more specific type, or submit sothing that can be verified properly
785
$this->assertEquals(clean_param('xx<script>', PARAM_CLEAN), 'xx');
788
function test_clean_param_alpha() {
789
$this->assertEquals(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHA),
793
function test_clean_param_alphanum() {
794
$this->assertEquals(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHANUM),
795
'978942897DSFMOSDJ');
798
function test_clean_param_alphaext() {
799
$this->assertEquals(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHAEXT),
803
function test_clean_param_sequence() {
804
$this->assertEquals(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_SEQUENCE),
808
function test_clean_param_component() {
809
// please note the cleaning of component names is very strict, no guessing here
810
$this->assertSame(clean_param('mod_forum', PARAM_COMPONENT), 'mod_forum');
811
$this->assertSame(clean_param('block_online_users', PARAM_COMPONENT), 'block_online_users');
812
$this->assertSame(clean_param('block_blond_online_users', PARAM_COMPONENT), 'block_blond_online_users');
813
$this->assertSame(clean_param('mod_something2', PARAM_COMPONENT), 'mod_something2');
814
$this->assertSame(clean_param('forum', PARAM_COMPONENT), 'forum');
815
$this->assertSame(clean_param('user', PARAM_COMPONENT), 'user');
816
$this->assertSame(clean_param('rating', PARAM_COMPONENT), 'rating');
817
$this->assertSame(clean_param('mod_2something', PARAM_COMPONENT), '');
818
$this->assertSame(clean_param('2mod_something', PARAM_COMPONENT), '');
819
$this->assertSame(clean_param('mod_something_xx', PARAM_COMPONENT), '');
820
$this->assertSame(clean_param('auth_something__xx', PARAM_COMPONENT), '');
821
$this->assertSame(clean_param('mod_Something', PARAM_COMPONENT), '');
822
$this->assertSame(clean_param('mod_somethíng', PARAM_COMPONENT), '');
823
$this->assertSame(clean_param('auth_xx-yy', PARAM_COMPONENT), '');
824
$this->assertSame(clean_param('_auth_xx', PARAM_COMPONENT), '');
825
$this->assertSame(clean_param('a2uth_xx', PARAM_COMPONENT), '');
826
$this->assertSame(clean_param('auth_xx_', PARAM_COMPONENT), '');
827
$this->assertSame(clean_param('auth_xx.old', PARAM_COMPONENT), '');
828
$this->assertSame(clean_param('_user', PARAM_COMPONENT), '');
829
$this->assertSame(clean_param('2rating', PARAM_COMPONENT), '');
830
$this->assertSame(clean_param('user_', PARAM_COMPONENT), '');
833
function test_is_valid_plugin_name() {
444
public function test_clean_param_raw() {
446
'#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)',
447
clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_RAW));
450
public function test_clean_param_trim() {
451
$this->assertSame('Frog toad', clean_param(" Frog toad \r\n ", PARAM_RAW_TRIMMED));
454
public function test_clean_param_clean() {
455
// PARAM_CLEAN is an ugly hack, do not use in new code (skodak),
456
// instead use more specific type, or submit sothing that can be verified properly.
457
$this->assertSame('xx', clean_param('xx<script>', PARAM_CLEAN));
460
public function test_clean_param_alpha() {
461
$this->assertSame('DSFMOSDJ', clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHA));
464
public function test_clean_param_alphanum() {
465
$this->assertSame('978942897DSFMOSDJ', clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHANUM));
468
public function test_clean_param_alphaext() {
469
$this->assertSame('DSFMOSDJ', clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_ALPHAEXT));
472
public function test_clean_param_sequence() {
473
$this->assertSame(',9789,42897', clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_SEQUENCE));
476
public function test_clean_param_component() {
477
// Please note the cleaning of component names is very strict, no guessing here.
478
$this->assertSame('mod_forum', clean_param('mod_forum', PARAM_COMPONENT));
479
$this->assertSame('block_online_users', clean_param('block_online_users', PARAM_COMPONENT));
480
$this->assertSame('block_blond_online_users', clean_param('block_blond_online_users', PARAM_COMPONENT));
481
$this->assertSame('mod_something2', clean_param('mod_something2', PARAM_COMPONENT));
482
$this->assertSame('forum', clean_param('forum', PARAM_COMPONENT));
483
$this->assertSame('user', clean_param('user', PARAM_COMPONENT));
484
$this->assertSame('rating', clean_param('rating', PARAM_COMPONENT));
485
$this->assertSame('feedback360', clean_param('feedback360', PARAM_COMPONENT));
486
$this->assertSame('mod_feedback360', clean_param('mod_feedback360', PARAM_COMPONENT));
487
$this->assertSame('', clean_param('mod_2something', PARAM_COMPONENT));
488
$this->assertSame('', clean_param('2mod_something', PARAM_COMPONENT));
489
$this->assertSame('', clean_param('mod_something_xx', PARAM_COMPONENT));
490
$this->assertSame('', clean_param('auth_something__xx', PARAM_COMPONENT));
491
$this->assertSame('', clean_param('mod_Something', PARAM_COMPONENT));
492
$this->assertSame('', clean_param('mod_somethíng', PARAM_COMPONENT));
493
$this->assertSame('', clean_param('mod__something', PARAM_COMPONENT));
494
$this->assertSame('', clean_param('auth_xx-yy', PARAM_COMPONENT));
495
$this->assertSame('', clean_param('_auth_xx', PARAM_COMPONENT));
496
$this->assertSame('', clean_param('a2uth_xx', PARAM_COMPONENT));
497
$this->assertSame('', clean_param('auth_xx_', PARAM_COMPONENT));
498
$this->assertSame('', clean_param('auth_xx.old', PARAM_COMPONENT));
499
$this->assertSame('', clean_param('_user', PARAM_COMPONENT));
500
$this->assertSame('', clean_param('2rating', PARAM_COMPONENT));
501
$this->assertSame('', clean_param('user_', PARAM_COMPONENT));
504
public function test_is_valid_plugin_name() {
834
505
$this->assertTrue(is_valid_plugin_name('forum'));
835
506
$this->assertTrue(is_valid_plugin_name('forum2'));
507
$this->assertTrue(is_valid_plugin_name('feedback360'));
836
508
$this->assertTrue(is_valid_plugin_name('online_users'));
837
509
$this->assertTrue(is_valid_plugin_name('blond_online_users'));
838
510
$this->assertFalse(is_valid_plugin_name('online__users'));
845
517
$this->assertFalse(is_valid_plugin_name('xx_'));
848
function test_clean_param_plugin() {
849
// please note the cleaning of plugin names is very strict, no guessing here
850
$this->assertSame(clean_param('forum', PARAM_PLUGIN), 'forum');
851
$this->assertSame(clean_param('forum2', PARAM_PLUGIN), 'forum2');
852
$this->assertSame(clean_param('online_users', PARAM_PLUGIN), 'online_users');
853
$this->assertSame(clean_param('blond_online_users', PARAM_PLUGIN), 'blond_online_users');
854
$this->assertSame(clean_param('online__users', PARAM_PLUGIN), '');
855
$this->assertSame(clean_param('forum ', PARAM_PLUGIN), '');
856
$this->assertSame(clean_param('forum.old', PARAM_PLUGIN), '');
857
$this->assertSame(clean_param('xx-yy', PARAM_PLUGIN), '');
858
$this->assertSame(clean_param('2xx', PARAM_PLUGIN), '');
859
$this->assertSame(clean_param('Xx', PARAM_PLUGIN), '');
860
$this->assertSame(clean_param('_xx', PARAM_PLUGIN), '');
861
$this->assertSame(clean_param('xx_', PARAM_PLUGIN), '');
864
function test_clean_param_area() {
865
// please note the cleaning of area names is very strict, no guessing here
866
$this->assertSame(clean_param('something', PARAM_AREA), 'something');
867
$this->assertSame(clean_param('something2', PARAM_AREA), 'something2');
868
$this->assertSame(clean_param('some_thing', PARAM_AREA), 'some_thing');
869
$this->assertSame(clean_param('some_thing_xx', PARAM_AREA), 'some_thing_xx');
870
$this->assertSame(clean_param('_something', PARAM_AREA), '');
871
$this->assertSame(clean_param('something_', PARAM_AREA), '');
872
$this->assertSame(clean_param('2something', PARAM_AREA), '');
873
$this->assertSame(clean_param('Something', PARAM_AREA), '');
874
$this->assertSame(clean_param('some-thing', PARAM_AREA), '');
875
$this->assertSame(clean_param('somethííng', PARAM_AREA), '');
876
$this->assertSame(clean_param('something.x', PARAM_AREA), '');
879
function test_clean_param_text() {
880
$this->assertEquals(PARAM_TEXT, PARAM_MULTILANG);
882
$this->assertEquals(clean_param('xx<lang lang="en">aa</lang><lang lang="yy">pp</lang>', PARAM_TEXT), 'xx<lang lang="en">aa</lang><lang lang="yy">pp</lang>');
883
$this->assertEquals(clean_param('<span lang="en" class="multilang">aa</span><span lang="xy" class="multilang">bb</span>', PARAM_TEXT), '<span lang="en" class="multilang">aa</span><span lang="xy" class="multilang">bb</span>');
884
$this->assertEquals(clean_param('xx<lang lang="en">aa'."\n".'</lang><lang lang="yy">pp</lang>', PARAM_TEXT), 'xx<lang lang="en">aa'."\n".'</lang><lang lang="yy">pp</lang>');
886
$this->assertEquals(clean_param('<span lang="en" class="multilang">aa</span>', PARAM_TEXT), '<span lang="en" class="multilang">aa</span>');
887
$this->assertEquals(clean_param('<span lang="en" class="nothing" class="multilang">aa</span>', PARAM_TEXT), 'aa');
888
$this->assertEquals(clean_param('<lang lang="en" class="multilang">aa</lang>', PARAM_TEXT), 'aa');
889
$this->assertEquals(clean_param('<lang lang="en!!">aa</lang>', PARAM_TEXT), 'aa');
890
$this->assertEquals(clean_param('<span lang="en==" class="multilang">aa</span>', PARAM_TEXT), 'aa');
891
$this->assertEquals(clean_param('a<em>b</em>c', PARAM_TEXT), 'abc');
892
$this->assertEquals(clean_param('a><xx >c>', PARAM_TEXT), 'a>c>'); // standard strip_tags() behaviour
893
$this->assertEquals(clean_param('a<b', PARAM_TEXT), 'a');
894
$this->assertEquals(clean_param('a>b', PARAM_TEXT), 'a>b');
895
$this->assertEquals(clean_param('<lang lang="en">a>a</lang>', PARAM_TEXT), '<lang lang="en">a>a</lang>'); // standard strip_tags() behaviour
896
$this->assertEquals(clean_param('<lang lang="en">a<a</lang>', PARAM_TEXT), 'a');
897
$this->assertEquals(clean_param('<lang lang="en">a<br>a</lang>', PARAM_TEXT), '<lang lang="en">aa</lang>');
900
function test_clean_param_url() {
901
// Test PARAM_URL and PARAM_LOCALURL a bit
902
$this->assertEquals(clean_param('http://google.com/', PARAM_URL), 'http://google.com/');
903
$this->assertEquals(clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_URL), 'http://some.very.long.and.silly.domain/with/a/path/');
904
$this->assertEquals(clean_param('http://localhost/', PARAM_URL), 'http://localhost/');
905
$this->assertEquals(clean_param('http://0.255.1.1/numericip.php', PARAM_URL), 'http://0.255.1.1/numericip.php');
906
$this->assertEquals(clean_param('/just/a/path', PARAM_URL), '/just/a/path');
907
$this->assertEquals(clean_param('funny:thing', PARAM_URL), '');
910
function test_clean_param_localurl() {
520
public function test_clean_param_plugin() {
521
// Please note the cleaning of plugin names is very strict, no guessing here.
522
$this->assertSame('forum', clean_param('forum', PARAM_PLUGIN));
523
$this->assertSame('forum2', clean_param('forum2', PARAM_PLUGIN));
524
$this->assertSame('feedback360', clean_param('feedback360', PARAM_PLUGIN));
525
$this->assertSame('online_users', clean_param('online_users', PARAM_PLUGIN));
526
$this->assertSame('blond_online_users', clean_param('blond_online_users', PARAM_PLUGIN));
527
$this->assertSame('', clean_param('online__users', PARAM_PLUGIN));
528
$this->assertSame('', clean_param('forum ', PARAM_PLUGIN));
529
$this->assertSame('', clean_param('forum.old', PARAM_PLUGIN));
530
$this->assertSame('', clean_param('xx-yy', PARAM_PLUGIN));
531
$this->assertSame('', clean_param('2xx', PARAM_PLUGIN));
532
$this->assertSame('', clean_param('Xx', PARAM_PLUGIN));
533
$this->assertSame('', clean_param('_xx', PARAM_PLUGIN));
534
$this->assertSame('', clean_param('xx_', PARAM_PLUGIN));
537
public function test_clean_param_area() {
538
// Please note the cleaning of area names is very strict, no guessing here.
539
$this->assertSame('something', clean_param('something', PARAM_AREA));
540
$this->assertSame('something2', clean_param('something2', PARAM_AREA));
541
$this->assertSame('some_thing', clean_param('some_thing', PARAM_AREA));
542
$this->assertSame('some_thing_xx', clean_param('some_thing_xx', PARAM_AREA));
543
$this->assertSame('feedback360', clean_param('feedback360', PARAM_AREA));
544
$this->assertSame('', clean_param('_something', PARAM_AREA));
545
$this->assertSame('', clean_param('something_', PARAM_AREA));
546
$this->assertSame('', clean_param('2something', PARAM_AREA));
547
$this->assertSame('', clean_param('Something', PARAM_AREA));
548
$this->assertSame('', clean_param('some-thing', PARAM_AREA));
549
$this->assertSame('', clean_param('somethííng', PARAM_AREA));
550
$this->assertSame('', clean_param('something.x', PARAM_AREA));
553
public function test_clean_param_text() {
554
$this->assertSame(PARAM_TEXT, PARAM_MULTILANG);
556
$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));
557
$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));
558
$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));
560
$this->assertSame('<span lang="en" class="multilang">aa</span>', clean_param('<span lang="en" class="multilang">aa</span>', PARAM_TEXT));
561
$this->assertSame('aa', clean_param('<span lang="en" class="nothing" class="multilang">aa</span>', PARAM_TEXT));
562
$this->assertSame('aa', clean_param('<lang lang="en" class="multilang">aa</lang>', PARAM_TEXT));
563
$this->assertSame('aa', clean_param('<lang lang="en!!">aa</lang>', PARAM_TEXT));
564
$this->assertSame('aa', clean_param('<span lang="en==" class="multilang">aa</span>', PARAM_TEXT));
565
$this->assertSame('abc', clean_param('a<em>b</em>c', PARAM_TEXT));
566
$this->assertSame('a>c>', clean_param('a><xx >c>', PARAM_TEXT)); // Standard strip_tags() behaviour.
567
$this->assertSame('a', clean_param('a<b', PARAM_TEXT));
568
$this->assertSame('a>b', clean_param('a>b', PARAM_TEXT));
569
$this->assertSame('<lang lang="en">a>a</lang>', clean_param('<lang lang="en">a>a</lang>', PARAM_TEXT)); // Standard strip_tags() behaviour.
570
$this->assertSame('a', clean_param('<lang lang="en">a<a</lang>', PARAM_TEXT));
571
$this->assertSame('<lang lang="en">aa</lang>', clean_param('<lang lang="en">a<br>a</lang>', PARAM_TEXT));
574
public function test_clean_param_url() {
575
// Test PARAM_URL and PARAM_LOCALURL a bit.
576
$this->assertSame('http://google.com/', clean_param('http://google.com/', PARAM_URL));
577
$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));
578
$this->assertSame('http://localhost/', clean_param('http://localhost/', PARAM_URL));
579
$this->assertSame('http://0.255.1.1/numericip.php', clean_param('http://0.255.1.1/numericip.php', PARAM_URL));
580
$this->assertSame('/just/a/path', clean_param('/just/a/path', PARAM_URL));
581
$this->assertSame('', clean_param('funny:thing', PARAM_URL));
584
public function test_clean_param_localurl() {
912
$this->assertEquals(clean_param('http://google.com/', PARAM_LOCALURL), '');
913
$this->assertEquals(clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_LOCALURL), '');
914
$this->assertEquals(clean_param($CFG->wwwroot, PARAM_LOCALURL), $CFG->wwwroot);
915
$this->assertEquals(clean_param('/just/a/path', PARAM_LOCALURL), '/just/a/path');
916
$this->assertEquals(clean_param('funny:thing', PARAM_LOCALURL), '');
917
$this->assertEquals(clean_param('course/view.php?id=3', PARAM_LOCALURL), 'course/view.php?id=3');
920
function test_clean_param_file() {
921
$this->assertEquals(clean_param('correctfile.txt', PARAM_FILE), 'correctfile.txt');
922
$this->assertEquals(clean_param('b\'a<d`\\/fi:l>e.t"x|t', PARAM_FILE), 'badfile.txt');
923
$this->assertEquals(clean_param('../parentdirfile.txt', PARAM_FILE), '..parentdirfile.txt');
924
$this->assertEquals(clean_param('../../grandparentdirfile.txt', PARAM_FILE), '....grandparentdirfile.txt');
925
$this->assertEquals(clean_param('..\winparentdirfile.txt', PARAM_FILE), '..winparentdirfile.txt');
926
$this->assertEquals(clean_param('..\..\wingrandparentdir.txt', PARAM_FILE), '....wingrandparentdir.txt');
927
$this->assertEquals(clean_param('myfile.a.b.txt', PARAM_FILE), 'myfile.a.b.txt');
928
$this->assertEquals(clean_param('myfile..a..b.txt', PARAM_FILE), 'myfile..a..b.txt');
929
$this->assertEquals(clean_param('myfile.a..b...txt', PARAM_FILE), 'myfile.a..b...txt');
930
$this->assertEquals(clean_param('myfile.a.txt', PARAM_FILE), 'myfile.a.txt');
931
$this->assertEquals(clean_param('myfile...txt', PARAM_FILE), 'myfile...txt');
932
$this->assertEquals(clean_param('...jpg', PARAM_FILE), '...jpg');
933
$this->assertEquals(clean_param('.a.b.', PARAM_FILE), '.a.b.');
934
$this->assertEquals(clean_param('.', PARAM_FILE), '');
935
$this->assertEquals(clean_param('..', PARAM_FILE), '');
936
$this->assertEquals(clean_param('...', PARAM_FILE), '...');
937
$this->assertEquals(clean_param('. . . .', PARAM_FILE), '. . . .');
938
$this->assertEquals(clean_param('dontrtrim.me. .. .. . ', PARAM_FILE), 'dontrtrim.me. .. .. . ');
939
$this->assertEquals(clean_param(' . .dontltrim.me', PARAM_FILE), ' . .dontltrim.me');
940
$this->assertEquals(clean_param("here is a tab\t.txt", PARAM_FILE), 'here is a tab.txt');
941
$this->assertEquals(clean_param("here is a line\r\nbreak.txt", PARAM_FILE), 'here is a linebreak.txt');
943
//The following behaviours have been maintained although they seem a little odd
944
$this->assertEquals(clean_param('funny:thing', PARAM_FILE), 'funnything');
945
$this->assertEquals(clean_param('./currentdirfile.txt', PARAM_FILE), '.currentdirfile.txt');
946
$this->assertEquals(clean_param('c:\temp\windowsfile.txt', PARAM_FILE), 'ctempwindowsfile.txt');
947
$this->assertEquals(clean_param('/home/user/linuxfile.txt', PARAM_FILE), 'homeuserlinuxfile.txt');
948
$this->assertEquals(clean_param('~/myfile.txt', PARAM_FILE), '~myfile.txt');
951
function test_clean_param_path() {
952
$this->assertEquals(clean_param('correctfile.txt', PARAM_PATH), 'correctfile.txt');
953
$this->assertEquals(clean_param('b\'a<d`\\/fi:l>e.t"x|t', PARAM_PATH), 'bad/file.txt');
954
$this->assertEquals(clean_param('../parentdirfile.txt', PARAM_PATH), '/parentdirfile.txt');
955
$this->assertEquals(clean_param('../../grandparentdirfile.txt', PARAM_PATH), '/grandparentdirfile.txt');
956
$this->assertEquals(clean_param('..\winparentdirfile.txt', PARAM_PATH), '/winparentdirfile.txt');
957
$this->assertEquals(clean_param('..\..\wingrandparentdir.txt', PARAM_PATH), '/wingrandparentdir.txt');
958
$this->assertEquals(clean_param('funny:thing', PARAM_PATH), 'funnything');
959
$this->assertEquals(clean_param('./././here', PARAM_PATH), './here');
960
$this->assertEquals(clean_param('./currentdirfile.txt', PARAM_PATH), './currentdirfile.txt');
961
$this->assertEquals(clean_param('c:\temp\windowsfile.txt', PARAM_PATH), 'c/temp/windowsfile.txt');
962
$this->assertEquals(clean_param('/home/user/linuxfile.txt', PARAM_PATH), '/home/user/linuxfile.txt');
963
$this->assertEquals(clean_param('/home../user ./.linuxfile.txt', PARAM_PATH), '/home../user ./.linuxfile.txt');
964
$this->assertEquals(clean_param('~/myfile.txt', PARAM_PATH), '~/myfile.txt');
965
$this->assertEquals(clean_param('~/../myfile.txt', PARAM_PATH), '~/myfile.txt');
966
$this->assertEquals(clean_param('/..b../.../myfile.txt', PARAM_PATH), '/..b../.../myfile.txt');
967
$this->assertEquals(clean_param('..b../.../myfile.txt', PARAM_PATH), '..b../.../myfile.txt');
968
$this->assertEquals(clean_param('/super//slashes///', PARAM_PATH), '/super/slashes/');
971
function test_clean_param_username() {
586
$this->assertSame('', clean_param('http://google.com/', PARAM_LOCALURL));
587
$this->assertSame('', clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_LOCALURL));
588
$this->assertSame(clean_param($CFG->wwwroot, PARAM_LOCALURL), $CFG->wwwroot);
589
$this->assertSame('/just/a/path', clean_param('/just/a/path', PARAM_LOCALURL));
590
$this->assertSame('', clean_param('funny:thing', PARAM_LOCALURL));
591
$this->assertSame('course/view.php?id=3', clean_param('course/view.php?id=3', PARAM_LOCALURL));
594
public function test_clean_param_file() {
595
$this->assertSame('correctfile.txt', clean_param('correctfile.txt', PARAM_FILE));
596
$this->assertSame('badfile.txt', clean_param('b\'a<d`\\/fi:l>e.t"x|t', PARAM_FILE));
597
$this->assertSame('..parentdirfile.txt', clean_param('../parentdirfile.txt', PARAM_FILE));
598
$this->assertSame('....grandparentdirfile.txt', clean_param('../../grandparentdirfile.txt', PARAM_FILE));
599
$this->assertSame('..winparentdirfile.txt', clean_param('..\winparentdirfile.txt', PARAM_FILE));
600
$this->assertSame('....wingrandparentdir.txt', clean_param('..\..\wingrandparentdir.txt', PARAM_FILE));
601
$this->assertSame('myfile.a.b.txt', clean_param('myfile.a.b.txt', PARAM_FILE));
602
$this->assertSame('myfile..a..b.txt', clean_param('myfile..a..b.txt', PARAM_FILE));
603
$this->assertSame('myfile.a..b...txt', clean_param('myfile.a..b...txt', PARAM_FILE));
604
$this->assertSame('myfile.a.txt', clean_param('myfile.a.txt', PARAM_FILE));
605
$this->assertSame('myfile...txt', clean_param('myfile...txt', PARAM_FILE));
606
$this->assertSame('...jpg', clean_param('...jpg', PARAM_FILE));
607
$this->assertSame('.a.b.', clean_param('.a.b.', PARAM_FILE));
608
$this->assertSame('', clean_param('.', PARAM_FILE));
609
$this->assertSame('', clean_param('..', PARAM_FILE));
610
$this->assertSame('...', clean_param('...', PARAM_FILE));
611
$this->assertSame('. . . .', clean_param('. . . .', PARAM_FILE));
612
$this->assertSame('dontrtrim.me. .. .. . ', clean_param('dontrtrim.me. .. .. . ', PARAM_FILE));
613
$this->assertSame(' . .dontltrim.me', clean_param(' . .dontltrim.me', PARAM_FILE));
614
$this->assertSame('here is a tab.txt', clean_param("here is a tab\t.txt", PARAM_FILE));
615
$this->assertSame('here is a linebreak.txt', clean_param("here is a line\r\nbreak.txt", PARAM_FILE));
617
// The following behaviours have been maintained although they seem a little odd.
618
$this->assertSame('funnything', clean_param('funny:thing', PARAM_FILE));
619
$this->assertSame('.currentdirfile.txt', clean_param('./currentdirfile.txt', PARAM_FILE));
620
$this->assertSame('ctempwindowsfile.txt', clean_param('c:\temp\windowsfile.txt', PARAM_FILE));
621
$this->assertSame('homeuserlinuxfile.txt', clean_param('/home/user/linuxfile.txt', PARAM_FILE));
622
$this->assertSame('~myfile.txt', clean_param('~/myfile.txt', PARAM_FILE));
625
public function test_clean_param_path() {
626
$this->assertSame('correctfile.txt', clean_param('correctfile.txt', PARAM_PATH));
627
$this->assertSame('bad/file.txt', clean_param('b\'a<d`\\/fi:l>e.t"x|t', PARAM_PATH));
628
$this->assertSame('/parentdirfile.txt', clean_param('../parentdirfile.txt', PARAM_PATH));
629
$this->assertSame('/grandparentdirfile.txt', clean_param('../../grandparentdirfile.txt', PARAM_PATH));
630
$this->assertSame('/winparentdirfile.txt', clean_param('..\winparentdirfile.txt', PARAM_PATH));
631
$this->assertSame('/wingrandparentdir.txt', clean_param('..\..\wingrandparentdir.txt', PARAM_PATH));
632
$this->assertSame('funnything', clean_param('funny:thing', PARAM_PATH));
633
$this->assertSame('./here', clean_param('./././here', PARAM_PATH));
634
$this->assertSame('./currentdirfile.txt', clean_param('./currentdirfile.txt', PARAM_PATH));
635
$this->assertSame('c/temp/windowsfile.txt', clean_param('c:\temp\windowsfile.txt', PARAM_PATH));
636
$this->assertSame('/home/user/linuxfile.txt', clean_param('/home/user/linuxfile.txt', PARAM_PATH));
637
$this->assertSame('/home../user ./.linuxfile.txt', clean_param('/home../user ./.linuxfile.txt', PARAM_PATH));
638
$this->assertSame('~/myfile.txt', clean_param('~/myfile.txt', PARAM_PATH));
639
$this->assertSame('~/myfile.txt', clean_param('~/../myfile.txt', PARAM_PATH));
640
$this->assertSame('/..b../.../myfile.txt', clean_param('/..b../.../myfile.txt', PARAM_PATH));
641
$this->assertSame('..b../.../myfile.txt', clean_param('..b../.../myfile.txt', PARAM_PATH));
642
$this->assertSame('/super/slashes/', clean_param('/super//slashes///', PARAM_PATH));
645
public function test_clean_param_username() {
973
647
$currentstatus = $CFG->extendedusernamechars;
975
// Run tests with extended character == FALSE;
976
$CFG->extendedusernamechars = FALSE;
977
$this->assertEquals(clean_param('johndoe123', PARAM_USERNAME), 'johndoe123' );
978
$this->assertEquals(clean_param('john.doe', PARAM_USERNAME), 'john.doe');
979
$this->assertEquals(clean_param('john-doe', PARAM_USERNAME), 'john-doe');
980
$this->assertEquals(clean_param('john- doe', PARAM_USERNAME), 'john-doe');
981
$this->assertEquals(clean_param('john_doe', PARAM_USERNAME), 'john_doe');
982
$this->assertEquals(clean_param('john@doe', PARAM_USERNAME), 'john@doe');
983
$this->assertEquals(clean_param('john~doe', PARAM_USERNAME), 'johndoe');
984
$this->assertEquals(clean_param('john´doe', PARAM_USERNAME), 'johndoe');
985
$this->assertEquals(clean_param('john#$%&() ', PARAM_USERNAME), 'john');
986
$this->assertEquals(clean_param('JOHNdóé ', PARAM_USERNAME), 'johnd');
987
$this->assertEquals(clean_param('john.,:;-_/|\ñÑ[]A_X-,D {} ~!@#$%^&*()_+ ?><[] ščřžžý ?ýá?ý??doe ', PARAM_USERNAME), 'john.-_a_x-d@_doe');
990
// Test success condition, if extendedusernamechars == ENABLE;
991
$CFG->extendedusernamechars = TRUE;
992
$this->assertEquals(clean_param('john_doe', PARAM_USERNAME), 'john_doe');
993
$this->assertEquals(clean_param('john@doe', PARAM_USERNAME), 'john@doe');
994
$this->assertEquals(clean_param('john# $%&()+_^', PARAM_USERNAME), 'john#$%&()+_^');
995
$this->assertEquals(clean_param('john~doe', PARAM_USERNAME), 'john~doe');
996
$this->assertEquals(clean_param('joHN´doe', PARAM_USERNAME), 'john´doe');
997
$this->assertEquals(clean_param('johnDOE', PARAM_USERNAME), 'johndoe');
998
$this->assertEquals(clean_param('johndóé ', PARAM_USERNAME), 'johndóé');
649
// Run tests with extended character == false;.
650
$CFG->extendedusernamechars = false;
651
$this->assertSame('johndoe123', clean_param('johndoe123', PARAM_USERNAME) );
652
$this->assertSame('john.doe', clean_param('john.doe', PARAM_USERNAME));
653
$this->assertSame('john-doe', clean_param('john-doe', PARAM_USERNAME));
654
$this->assertSame('john-doe', clean_param('john- doe', PARAM_USERNAME));
655
$this->assertSame('john_doe', clean_param('john_doe', PARAM_USERNAME));
656
$this->assertSame('john@doe', clean_param('john@doe', PARAM_USERNAME));
657
$this->assertSame('johndoe', clean_param('john~doe', PARAM_USERNAME));
658
$this->assertSame('johndoe', clean_param('john´doe', PARAM_USERNAME));
659
$this->assertSame(clean_param('john#$%&() ', PARAM_USERNAME), 'john');
660
$this->assertSame('johnd', clean_param('JOHNdóé ', PARAM_USERNAME));
661
$this->assertSame(clean_param('john.,:;-_/|\ñÑ[]A_X-,D {} ~!@#$%^&*()_+ ?><[] ščřžžý ?ýá?ý??doe ', PARAM_USERNAME), 'john.-_a_x-d@_doe');
663
// Test success condition, if extendedusernamechars == ENABLE;.
664
$CFG->extendedusernamechars = true;
665
$this->assertSame('john_doe', clean_param('john_doe', PARAM_USERNAME));
666
$this->assertSame('john@doe', clean_param('john@doe', PARAM_USERNAME));
667
$this->assertSame(clean_param('john# $%&()+_^', PARAM_USERNAME), 'john#$%&()+_^');
668
$this->assertSame('john~doe', clean_param('john~doe', PARAM_USERNAME));
669
$this->assertSame('john´doe', clean_param('joHN´doe', PARAM_USERNAME));
670
$this->assertSame('johndoe', clean_param('johnDOE', PARAM_USERNAME));
671
$this->assertSame('johndóé', clean_param('johndóé ', PARAM_USERNAME));
1000
673
$CFG->extendedusernamechars = $currentstatus;
1003
function test_clean_param_stringid() {
1004
// Test string identifiers validation
1006
$this->assertEquals(clean_param('validstring', PARAM_STRINGID), 'validstring');
1007
$this->assertEquals(clean_param('mod/foobar:valid_capability', PARAM_STRINGID), 'mod/foobar:valid_capability');
1008
$this->assertEquals(clean_param('CZ', PARAM_STRINGID), 'CZ');
1009
$this->assertEquals(clean_param('application/vnd.ms-powerpoint', PARAM_STRINGID), 'application/vnd.ms-powerpoint');
1010
$this->assertEquals(clean_param('grade2', PARAM_STRINGID), 'grade2');
1012
$this->assertEquals(clean_param('trailing ', PARAM_STRINGID), '');
1013
$this->assertEquals(clean_param('space bar', PARAM_STRINGID), '');
1014
$this->assertEquals(clean_param('0numeric', PARAM_STRINGID), '');
1015
$this->assertEquals(clean_param('*', PARAM_STRINGID), '');
1016
$this->assertEquals(clean_param(' ', PARAM_STRINGID), '');
676
public function test_clean_param_stringid() {
677
// Test string identifiers validation.
679
$this->assertSame('validstring', clean_param('validstring', PARAM_STRINGID));
680
$this->assertSame('mod/foobar:valid_capability', clean_param('mod/foobar:valid_capability', PARAM_STRINGID));
681
$this->assertSame('CZ', clean_param('CZ', PARAM_STRINGID));
682
$this->assertSame('application/vnd.ms-powerpoint', clean_param('application/vnd.ms-powerpoint', PARAM_STRINGID));
683
$this->assertSame('grade2', clean_param('grade2', PARAM_STRINGID));
685
$this->assertSame('', clean_param('trailing ', PARAM_STRINGID));
686
$this->assertSame('', clean_param('space bar', PARAM_STRINGID));
687
$this->assertSame('', clean_param('0numeric', PARAM_STRINGID));
688
$this->assertSame('', clean_param('*', PARAM_STRINGID));
689
$this->assertSame('', clean_param(' ', PARAM_STRINGID));
1019
function test_clean_param_timezone() {
1020
// Test timezone validation
692
public function test_clean_param_timezone() {
693
// Test timezone validation.
1021
694
$testvalues = array (
1022
695
'America/Jamaica' => 'America/Jamaica',
1023
696
'America/Argentina/Cordoba' => 'America/Argentina/Cordoba',
1053
726
foreach ($testvalues as $testvalue => $expectedvalue) {
1054
727
$actualvalue = clean_param($testvalue, PARAM_TIMEZONE);
1055
$this->assertEquals($actualvalue, $expectedvalue);
728
$this->assertEquals($expectedvalue, $actualvalue);
1059
function test_validate_param() {
732
public function test_validate_param() {
1061
734
$param = validate_param('11a', PARAM_INT);
1062
735
$this->fail('invalid_parameter_exception expected');
1063
} catch (invalid_parameter_exception $ex) {
1064
$this->assertTrue(true);
1067
$param = validate_param('11', PARAM_INT);
1068
$this->assertEquals($param, 11);
1069
} catch (invalid_parameter_exception $ex) {
1070
$this->fail('invalid_parameter_exception not expected');
736
} catch (moodle_exception $ex) {
737
$this->assertInstanceOf('invalid_parameter_exception', $ex);
740
$param = validate_param('11', PARAM_INT);
741
$this->assertSame(11, $param);
1073
744
$param = validate_param(null, PARAM_INT, false);
1074
745
$this->fail('invalid_parameter_exception expected');
1075
} catch (invalid_parameter_exception $ex) {
1076
$this->assertTrue(true);
1079
$param = validate_param(null, PARAM_INT, true);
1080
$this->assertTrue($param===null);
1081
} catch (invalid_parameter_exception $ex) {
1082
$this->fail('invalid_parameter_exception expected');
746
} catch (moodle_exception $ex) {
747
$this->assertInstanceOf('invalid_parameter_exception', $ex);
750
$param = validate_param(null, PARAM_INT, true);
751
$this->assertSame(null, $param);
1085
754
$param = validate_param(array(), PARAM_INT);
1086
755
$this->fail('invalid_parameter_exception expected');
1087
} catch (invalid_parameter_exception $ex) {
1088
$this->assertTrue(true);
756
} catch (moodle_exception $ex) {
757
$this->assertInstanceOf('invalid_parameter_exception', $ex);
1091
760
$param = validate_param(new stdClass, PARAM_INT);
1092
761
$this->fail('invalid_parameter_exception expected');
1093
} catch (invalid_parameter_exception $ex) {
1094
$this->assertTrue(true);
1097
$param = validate_param('1.0', PARAM_FLOAT);
1098
$this->assertSame(1.0, $param);
1100
// Make sure valid floats do not cause exception.
1101
validate_param(1.0, PARAM_FLOAT);
1102
validate_param(10, PARAM_FLOAT);
1103
validate_param('0', PARAM_FLOAT);
1104
validate_param('119813454.545464564564546564545646556564465465456465465465645645465645645645', PARAM_FLOAT);
1105
validate_param('011.1', PARAM_FLOAT);
1106
validate_param('11', PARAM_FLOAT);
1107
validate_param('+.1', PARAM_FLOAT);
1108
validate_param('-.1', PARAM_FLOAT);
1109
validate_param('1e10', PARAM_FLOAT);
1110
validate_param('.1e+10', PARAM_FLOAT);
1111
validate_param('1E-1', PARAM_FLOAT);
1112
$this->assertTrue(true);
1113
} catch (invalid_parameter_exception $ex) {
1114
$this->fail('Valid float notation not accepted');
762
} catch (moodle_exception $ex) {
763
$this->assertInstanceOf('invalid_parameter_exception', $ex);
766
$param = validate_param('1.0', PARAM_FLOAT);
767
$this->assertSame(1.0, $param);
769
// Make sure valid floats do not cause exception.
770
validate_param(1.0, PARAM_FLOAT);
771
validate_param(10, PARAM_FLOAT);
772
validate_param('0', PARAM_FLOAT);
773
validate_param('119813454.545464564564546564545646556564465465456465465465645645465645645645', PARAM_FLOAT);
774
validate_param('011.1', PARAM_FLOAT);
775
validate_param('11', PARAM_FLOAT);
776
validate_param('+.1', PARAM_FLOAT);
777
validate_param('-.1', PARAM_FLOAT);
778
validate_param('1e10', PARAM_FLOAT);
779
validate_param('.1e+10', PARAM_FLOAT);
780
validate_param('1E-1', PARAM_FLOAT);
1117
783
$param = validate_param('1,2', PARAM_FLOAT);
1118
784
$this->fail('invalid_parameter_exception expected');
1119
} catch (invalid_parameter_exception $ex) {
1120
$this->assertTrue(true);
785
} catch (moodle_exception $ex) {
786
$this->assertInstanceOf('invalid_parameter_exception', $ex);
1123
789
$param = validate_param('', PARAM_FLOAT);
1124
790
$this->fail('invalid_parameter_exception expected');
1125
} catch (invalid_parameter_exception $ex) {
1126
$this->assertTrue(true);
791
} catch (moodle_exception $ex) {
792
$this->assertInstanceOf('invalid_parameter_exception', $ex);
1129
795
$param = validate_param('.', PARAM_FLOAT);
1130
796
$this->fail('invalid_parameter_exception expected');
1131
} catch (invalid_parameter_exception $ex) {
1132
$this->assertTrue(true);
797
} catch (moodle_exception $ex) {
798
$this->assertInstanceOf('invalid_parameter_exception', $ex);
1135
801
$param = validate_param('e10', PARAM_FLOAT);
1136
802
$this->fail('invalid_parameter_exception expected');
1137
} catch (invalid_parameter_exception $ex) {
1138
$this->assertTrue(true);
803
} catch (moodle_exception $ex) {
804
$this->assertInstanceOf('invalid_parameter_exception', $ex);
1141
807
$param = validate_param('abc', PARAM_FLOAT);
1142
808
$this->fail('invalid_parameter_exception expected');
1143
} catch (invalid_parameter_exception $ex) {
1144
$this->assertTrue(true);
809
} catch (moodle_exception $ex) {
810
$this->assertInstanceOf('invalid_parameter_exception', $ex);
1148
function test_shorten_text_no_tags_already_short_enough() {
814
public function test_shorten_text_no_tags_already_short_enough() {
1149
815
// ......12345678901234567890123456.
1150
816
$text = "short text already no tags";
1151
$this->assertEquals($text, shorten_text($text));
817
$this->assertSame($text, shorten_text($text));
1154
function test_shorten_text_with_tags_already_short_enough() {
820
public function test_shorten_text_with_tags_already_short_enough() {
1155
821
// .........123456...7890....12345678.......901234567.
1156
822
$text = "<p>short <b>text</b> already</p><p>with tags</p>";
1157
$this->assertEquals($text, shorten_text($text));
823
$this->assertSame($text, shorten_text($text));
1160
function test_shorten_text_no_tags_needs_shortening() {
826
public function test_shorten_text_no_tags_needs_shortening() {
1161
827
// Default truncation is after 30 chars, but allowing 3 for the final '...'.
1162
828
// ......12345678901234567890123456789023456789012345678901234.
1163
829
$text = "long text without any tags blah de blah blah blah what";
1164
$this->assertEquals('long text without any tags ...', shorten_text($text));
830
$this->assertSame('long text without any tags ...', shorten_text($text));
1167
function test_shorten_text_with_tags_needs_shortening() {
833
public function test_shorten_text_with_tags_needs_shortening() {
1168
834
// .......................................123456789012345678901234567890...
1169
835
$text = "<div class='frog'><p><blockquote>Long text with tags that will ".
1170
836
"be chopped off but <b>should be added back again</b></blockquote></p></div>";
1172
838
"tags that ...</blockquote></p></div>", shorten_text($text));
1175
function test_shorten_text_with_entities() {
841
public function test_shorten_text_with_entities() {
1176
842
// Remember to allow 3 chars for the final '...'.
1177
843
// ......123456789012345678901234567_____890...
1178
844
$text = "some text which shouldn't break there";
1179
$this->assertEquals("some text which shouldn't ...",
1180
shorten_text($text, 31));
1181
$this->assertEquals("some text which shouldn't ...",
1182
shorten_text($text, 30));
1183
$this->assertEquals("some text which shouldn't ...",
1184
shorten_text($text, 29));
845
$this->assertSame("some text which shouldn't ...", shorten_text($text, 31));
846
$this->assertSame("some text which shouldn't ...", shorten_text($text, 30));
847
$this->assertSame("some text which shouldn't ...", shorten_text($text, 29));
1187
function test_shorten_text_known_tricky_case() {
850
public function test_shorten_text_known_tricky_case() {
1188
851
// This case caused a bug up to 1.9.5
1189
852
// ..........123456789012345678901234567890123456789.....0_____1___2___...
1190
853
$text = "<h3>standard 'break-out' sub groups in TGs?</h3> <<There are several";
1191
$this->assertEquals("<h3>standard 'break-out' sub groups in ...</h3>",
854
$this->assertSame("<h3>standard 'break-out' sub groups in ...</h3>",
1192
855
shorten_text($text, 41));
1193
$this->assertEquals("<h3>standard 'break-out' sub groups in TGs?...</h3>",
856
$this->assertSame("<h3>standard 'break-out' sub groups in TGs?...</h3>",
1194
857
shorten_text($text, 42));
1195
$this->assertEquals("<h3>standard 'break-out' sub groups in TGs?</h3> ...",
858
$this->assertSame("<h3>standard 'break-out' sub groups in TGs?</h3> ...",
1196
859
shorten_text($text, 43));
1199
function test_shorten_text_no_spaces() {
862
public function test_shorten_text_no_spaces() {
1200
863
// ..........123456789.
1201
864
$text = "<h1>123456789</h1>"; // A string with no convenient breaks.
1202
$this->assertEquals("<h1>12345...</h1>",
1203
shorten_text($text, 8));
865
$this->assertSame("<h1>12345...</h1>", shorten_text($text, 8));
1206
function test_shorten_text_utf8_european() {
868
public function test_shorten_text_utf8_european() {
1207
869
// Text without tags.
1208
870
// ......123456789012345678901234567.
1209
871
$text = "Žluťoučký koníček přeskočil";
1210
$this->assertEquals($text, shorten_text($text)); // 30 chars by default.
1211
$this->assertEquals("Žluťoučký koníče...", shorten_text($text, 19, true));
1212
$this->assertEquals("Žluťoučký ...", shorten_text($text, 19, false));
872
$this->assertSame($text, shorten_text($text)); // 30 chars by default.
873
$this->assertSame("Žluťoučký koníče...", shorten_text($text, 19, true));
874
$this->assertSame("Žluťoučký ...", shorten_text($text, 19, false));
1213
875
// And try it with 2-less (that are, in bytes, the middle of a sequence).
1214
$this->assertEquals("Žluťoučký koní...", shorten_text($text, 17, true));
1215
$this->assertEquals("Žluťoučký ...", shorten_text($text, 17, false));
876
$this->assertSame("Žluťoučký koní...", shorten_text($text, 17, true));
877
$this->assertSame("Žluťoučký ...", shorten_text($text, 17, false));
1217
879
// .........123456789012345678...901234567....89012345.
1218
880
$text = "<p>Žluťoučký koníček <b>přeskočil</b> potůček</p>";
1219
$this->assertEquals($text, shorten_text($text, 60));
1220
$this->assertEquals("<p>Žluťoučký koníček ...</p>", shorten_text($text, 21));
1221
$this->assertEquals("<p>Žluťoučký koníče...</p>", shorten_text($text, 19, true));
1222
$this->assertEquals("<p>Žluťoučký ...</p>", shorten_text($text, 19, false));
881
$this->assertSame($text, shorten_text($text, 60));
882
$this->assertSame("<p>Žluťoučký koníček ...</p>", shorten_text($text, 21));
883
$this->assertSame("<p>Žluťoučký koníče...</p>", shorten_text($text, 19, true));
884
$this->assertSame("<p>Žluťoučký ...</p>", shorten_text($text, 19, false));
1223
885
// And try it with 2 fewer (that are, in bytes, the middle of a sequence).
1224
$this->assertEquals("<p>Žluťoučký koní...</p>", shorten_text($text, 17, true));
1225
$this->assertEquals("<p>Žluťoučký ...</p>", shorten_text($text, 17, false));
886
$this->assertSame("<p>Žluťoučký koní...</p>", shorten_text($text, 17, true));
887
$this->assertSame("<p>Žluťoučký ...</p>", shorten_text($text, 17, false));
1226
888
// And try over one tag (start/end), it does proper text len.
1227
$this->assertEquals("<p>Žluťoučký koníček <b>př...</b></p>", shorten_text($text, 23, true));
1228
$this->assertEquals("<p>Žluťoučký koníček <b>přeskočil</b> pot...</p>", shorten_text($text, 34, true));
889
$this->assertSame("<p>Žluťoučký koníček <b>př...</b></p>", shorten_text($text, 23, true));
890
$this->assertSame("<p>Žluťoučký koníček <b>přeskočil</b> pot...</p>", shorten_text($text, 34, true));
1229
891
// And in the middle of one tag.
1230
$this->assertEquals("<p>Žluťoučký koníček <b>přeskočil...</b></p>", shorten_text($text, 30, true));
892
$this->assertSame("<p>Žluťoučký koníček <b>přeskočil...</b></p>", shorten_text($text, 30, true));
1233
function test_shorten_text_utf8_oriental() {
895
public function test_shorten_text_utf8_oriental() {
1235
897
// text without tags
1236
898
// ......123456789012345678901234.
1237
899
$text = '言語設定言語設定abcdefghijkl';
1238
$this->assertEquals($text, shorten_text($text)); // 30 chars by default
1239
$this->assertEquals("言語設定言語...", shorten_text($text, 9, true));
1240
$this->assertEquals("言語設定言語...", shorten_text($text, 9, false));
1241
$this->assertEquals("言語設定言語設定ab...", shorten_text($text, 13, true));
1242
$this->assertEquals("言語設定言語設定...", shorten_text($text, 13, false));
900
$this->assertSame($text, shorten_text($text)); // 30 chars by default.
901
$this->assertSame("言語設定言語...", shorten_text($text, 9, true));
902
$this->assertSame("言語設定言語...", shorten_text($text, 9, false));
903
$this->assertSame("言語設定言語設定ab...", shorten_text($text, 13, true));
904
$this->assertSame("言語設定言語設定...", shorten_text($text, 13, false));
1245
907
// text without tags
1246
908
// ......123456789012345678901234.
1247
909
$text = '简体中文简体中文abcdefghijkl';
1248
$this->assertEquals($text, shorten_text($text)); // 30 chars by default
1249
$this->assertEquals("简体中文简体...", shorten_text($text, 9, true));
1250
$this->assertEquals("简体中文简体...", shorten_text($text, 9, false));
1251
$this->assertEquals("简体中文简体中文ab...", shorten_text($text, 13, true));
1252
$this->assertEquals("简体中文简体中文...", shorten_text($text, 13, false));
910
$this->assertSame($text, shorten_text($text)); // 30 chars by default.
911
$this->assertSame("简体中文简体...", shorten_text($text, 9, true));
912
$this->assertSame("简体中文简体...", shorten_text($text, 9, false));
913
$this->assertSame("简体中文简体中文ab...", shorten_text($text, 13, true));
914
$this->assertSame("简体中文简体中文...", shorten_text($text, 13, false));
1255
function test_shorten_text_multilang() {
917
public function test_shorten_text_multilang() {
1256
918
// This is not necessaryily specific to multilang. The issue is really
1257
919
// tags with attributes, where before we were generating invalid HTML
1258
// output like shorten_text('<span id="x" class="y">A</span> B', 1);
920
// output like shorten_text('<span id="x" class="y">A</span> B', 1)
1259
921
// returning '<span id="x" ...</span>'. It is just that multilang
1260
922
// requires the sort of HTML that is quite likely to trigger this.
1261
923
// ........................................1...
1262
924
$text = '<span lang="en" class="multilang">A</span>' .
1263
925
'<span lang="fr" class="multilang">B</span>';
1264
$this->assertEquals('<span lang="en" class="multilang">...</span>',
926
$this->assertSame('<span lang="en" class="multilang">...</span>',
1265
927
shorten_text($text, 1));
1268
function test_usergetdate() {
930
public function test_usergetdate() {
1269
931
global $USER, $CFG, $DB;
932
$this->resetAfterTest();
1271
//Check if forcetimezone is set then save it and set it to use user timezone
934
// Check if forcetimezone is set then save it and set it to use user timezone.
1272
935
$cfgforcetimezone = null;
1273
936
if (isset($CFG->forcetimezone)) {
1274
937
$cfgforcetimezone = $CFG->forcetimezone;
1275
$CFG->forcetimezone = 99; //get user default timezone.
938
$CFG->forcetimezone = 99; // Get user default timezone.
1279
$USER = $DB->get_record('user', array('id'=>2)); //admin
941
$this->setAdminUser();
1281
943
$userstimezone = $USER->timezone;
1282
$USER->timezone = 2;//set the timezone to a known state
944
$USER->timezone = 2;// Set the timezone to a known state.
1284
946
// The string version of date comes from server locale setting and does
1285
947
// not respect user language, so it is necessary to reset that.
1286
948
$oldlocale = setlocale(LC_TIME, '0');
1287
949
setlocale(LC_TIME, 'en_AU.UTF-8');
1289
$ts = 1261540267; //the time this function was created
1291
$arr = usergetdate($ts,1);//specify the timezone as an argument
1292
$arr = array_values($arr);
1294
list($seconds,$minutes,$hours,$mday,$wday,$mon,$year,$yday,$weekday,$month) = $arr;
1295
$this->assertSame($seconds, 7);
1296
$this->assertSame($minutes, 51);
1297
$this->assertSame($hours, 4);
1298
$this->assertSame($mday, 23);
1299
$this->assertSame($wday, 3);
1300
$this->assertSame($mon, 12);
1301
$this->assertSame($year, 2009);
1302
$this->assertSame($yday, 356);
1303
$this->assertSame($weekday, 'Wednesday');
1304
$this->assertSame($month, 'December');
1305
$arr = usergetdate($ts);//gets the timezone from the $USER object
1306
$arr = array_values($arr);
1308
list($seconds,$minutes,$hours,$mday,$wday,$mon,$year,$yday,$weekday,$month) = $arr;
1309
$this->assertSame($seconds, 7);
1310
$this->assertSame($minutes, 51);
1311
$this->assertSame($hours, 5);
1312
$this->assertSame($mday, 23);
1313
$this->assertSame($wday, 3);
1314
$this->assertSame($mon, 12);
1315
$this->assertSame($year, 2009);
1316
$this->assertSame($yday, 356);
1317
$this->assertSame($weekday, 'Wednesday');
1318
$this->assertSame($month, 'December');
1319
//set the timezone back to what it was
951
$ts = 1261540267; // The time this function was created.
953
$arr = usergetdate($ts, 1); // Specify the timezone as an argument.
954
$arr = array_values($arr);
956
list($seconds, $minutes, $hours, $mday, $wday, $mon, $year, $yday, $weekday, $month) = $arr;
957
$this->assertSame(7, $seconds);
958
$this->assertSame(51, $minutes);
959
$this->assertSame(4, $hours);
960
$this->assertSame(23, $mday);
961
$this->assertSame(3, $wday);
962
$this->assertSame(12, $mon);
963
$this->assertSame(2009, $year);
964
$this->assertSame(356, $yday);
965
$this->assertSame('Wednesday', $weekday);
966
$this->assertSame('December', $month);
967
$arr = usergetdate($ts); // Gets the timezone from the $USER object.
968
$arr = array_values($arr);
970
list($seconds, $minutes, $hours, $mday, $wday, $mon, $year, $yday, $weekday, $month) = $arr;
971
$this->assertSame(7, $seconds);
972
$this->assertSame(51, $minutes);
973
$this->assertSame(5, $hours);
974
$this->assertSame(23, $mday);
975
$this->assertSame(3, $wday);
976
$this->assertSame(12, $mon);
977
$this->assertSame(2009, $year);
978
$this->assertSame(356, $yday);
979
$this->assertSame('Wednesday', $weekday);
980
$this->assertSame('December', $month);
981
// Set the timezone back to what it was.
1320
982
$USER->timezone = $userstimezone;
1322
//restore forcetimezone if changed.
984
// Restore forcetimezone if changed.
1323
985
if (!is_null($cfgforcetimezone)) {
1324
986
$CFG->forcetimezone = $cfgforcetimezone;
1327
989
setlocale(LC_TIME, $oldlocale);
1332
public function test_normalize_component() {
1335
$this->assertEquals(normalize_component('moodle'), array('core', null));
1336
$this->assertEquals(normalize_component('core'), array('core', null));
1338
// moodle core subsystems
1339
$this->assertEquals(normalize_component('admin'), array('core', 'admin'));
1340
$this->assertEquals(normalize_component('core_admin'), array('core', 'admin'));
1342
// activity modules and their subplugins
1343
$this->assertEquals(normalize_component('workshop'), array('mod', 'workshop'));
1344
$this->assertEquals(normalize_component('mod_workshop'), array('mod', 'workshop'));
1345
$this->assertEquals(normalize_component('workshopform_accumulative'), array('workshopform', 'accumulative'));
1346
$this->assertEquals(normalize_component('quiz'), array('mod', 'quiz'));
1347
$this->assertEquals(normalize_component('quiz_grading'), array('quiz', 'grading'));
1348
$this->assertEquals(normalize_component('data'), array('mod', 'data'));
1349
$this->assertEquals(normalize_component('datafield_checkbox'), array('datafield', 'checkbox'));
1351
// other plugin types
1352
$this->assertEquals(normalize_component('auth_mnet'), array('auth', 'mnet'));
1353
$this->assertEquals(normalize_component('enrol_self'), array('enrol', 'self'));
1354
$this->assertEquals(normalize_component('block_html'), array('block', 'html'));
1355
$this->assertEquals(normalize_component('block_mnet_hosts'), array('block', 'mnet_hosts'));
1356
$this->assertEquals(normalize_component('local_amos'), array('local', 'amos'));
1358
// unknown components are supposed to be activity modules
1359
$this->assertEquals(normalize_component('whothefuckwouldcomewithsuchastupidnameofcomponent'),
1360
array('mod', 'whothefuckwouldcomewithsuchastupidnameofcomponent'));
1361
$this->assertEquals(normalize_component('whothefuck_wouldcomewithsuchastupidnameofcomponent'),
1362
array('mod', 'whothefuck_wouldcomewithsuchastupidnameofcomponent'));
1363
$this->assertEquals(normalize_component('whothefuck_would_come_withsuchastupidnameofcomponent'),
1364
array('mod', 'whothefuck_would_come_withsuchastupidnameofcomponent'));
1367
protected function get_fake_preference_test_userid() {
1370
// we need some nonexistent user id
1371
$id = 2147483647 - 666;
1372
if ($DB->get_records('user', array('id'=>$id))) {
1379
992
public function test_mark_user_preferences_changed() {
1380
$this->resetAfterTest(true);
1381
if (!$otheruserid = $this->get_fake_preference_test_userid()) {
1382
$this->fail('Can not find unused user id for the preferences test');
993
$this->resetAfterTest();
994
$otheruser = $this->getDataGenerator()->create_user();
995
$otheruserid = $otheruser->id;
1386
set_cache_flag('userpreferenceschanged', $otheruserid, NULL);
997
set_cache_flag('userpreferenceschanged', $otheruserid, null);
1387
998
mark_user_preferences_changed($otheruserid);
1389
1000
$this->assertEquals(get_cache_flag('userpreferenceschanged', $otheruserid, time()-10), 1);
1390
set_cache_flag('userpreferenceschanged', $otheruserid, NULL);
1001
set_cache_flag('userpreferenceschanged', $otheruserid, null);
1393
1004
public function test_check_user_preferences_loaded() {
1395
$this->resetAfterTest(true);
1006
$this->resetAfterTest();
1397
if (!$otheruserid = $this->get_fake_preference_test_userid()) {
1398
$this->fail('Can not find unused user id for the preferences test');
1008
$otheruser = $this->getDataGenerator()->create_user();
1009
$otheruserid = $otheruser->id;
1402
1011
$DB->delete_records('user_preferences', array('userid'=>$otheruserid));
1403
set_cache_flag('userpreferenceschanged', $otheruserid, NULL);
1012
set_cache_flag('userpreferenceschanged', $otheruserid, null);
1405
1014
$user = new stdClass();
1406
1015
$user->id = $otheruserid;
1409
1018
check_user_preferences_loaded($user);
1410
1019
$this->assertTrue(isset($user->preference));
1411
1020
$this->assertTrue(is_array($user->preference));
1412
$this->assertTrue(isset($user->preference['_lastloaded']));
1413
$this->assertEquals(count($user->preference), 1);
1021
$this->assertArrayHasKey('_lastloaded', $user->preference);
1022
$this->assertCount(1, $user->preference);
1415
// add preference via direct call
1024
// Add preference via direct call.
1416
1025
$DB->insert_record('user_preferences', array('name'=>'xxx', 'value'=>'yyy', 'userid'=>$user->id));
1418
// no cache reload yet
1027
// No cache reload yet.
1419
1028
check_user_preferences_loaded($user);
1420
$this->assertEquals(count($user->preference), 1);
1029
$this->assertCount(1, $user->preference);
1422
// forced reloading of cache
1031
// Forced reloading of cache.
1423
1032
unset($user->preference);
1424
1033
check_user_preferences_loaded($user);
1425
$this->assertEquals(count($user->preference), 2);
1426
$this->assertEquals($user->preference['xxx'], 'yyy');
1034
$this->assertCount(2, $user->preference);
1035
$this->assertSame('yyy', $user->preference['xxx']);
1428
// add preference via direct call
1037
// Add preference via direct call.
1429
1038
$DB->insert_record('user_preferences', array('name'=>'aaa', 'value'=>'bbb', 'userid'=>$user->id));
1431
// test timeouts and modifications from different session
1040
// Test timeouts and modifications from different session.
1432
1041
set_cache_flag('userpreferenceschanged', $user->id, 1, time() + 1000);
1433
1042
$user->preference['_lastloaded'] = $user->preference['_lastloaded'] - 20;
1434
1043
check_user_preferences_loaded($user);
1435
$this->assertEquals(count($user->preference), 2);
1044
$this->assertCount(2, $user->preference);
1436
1045
check_user_preferences_loaded($user, 10);
1437
$this->assertEquals(count($user->preference), 3);
1438
$this->assertEquals($user->preference['aaa'], 'bbb');
1046
$this->assertCount(3, $user->preference);
1047
$this->assertSame('bbb', $user->preference['aaa']);
1439
1048
set_cache_flag('userpreferenceschanged', $user->id, null);
1442
1051
public function test_set_user_preference() {
1443
1052
global $DB, $USER;
1444
$this->resetAfterTest(true);
1447
$USER = $DB->get_record('user', array('id'=>2)); //admin
1449
if (!$otheruserid = $this->get_fake_preference_test_userid()) {
1450
$this->fail('Can not find unused user id for the preferences test');
1053
$this->resetAfterTest();
1055
$this->setAdminUser();
1057
$otheruser = $this->getDataGenerator()->create_user();
1058
$otheruserid = $otheruser->id;
1454
1060
$DB->delete_records('user_preferences', array('userid'=>$otheruserid));
1455
1061
set_cache_flag('userpreferenceschanged', $otheruserid, null);
2279
public function test_fullname() {
2282
$this->resetAfterTest();
2284
// Create a user to test the name display on.
2286
$record['firstname'] = 'Scott';
2287
$record['lastname'] = 'Fletcher';
2288
$record['firstnamephonetic'] = 'スコット';
2289
$record['lastnamephonetic'] = 'フレチャー';
2290
$record['alternatename'] = 'No friends';
2291
$user = $this->getDataGenerator()->create_user($record);
2293
// Back up config settings for restore later.
2294
$originalcfg = new stdClass();
2295
$originalcfg->fullnamedisplay = $CFG->fullnamedisplay;
2297
// Testing existing fullnamedisplay settings.
2298
$CFG->fullnamedisplay = 'firstname';
2299
$testname = fullname($user);
2300
$this->assertSame($user->firstname, $testname);
2302
$CFG->fullnamedisplay = 'firstname lastname';
2303
$expectedname = "$user->firstname $user->lastname";
2304
$testname = fullname($user);
2305
$this->assertSame($expectedname, $testname);
2307
$CFG->fullnamedisplay = 'lastname firstname';
2308
$expectedname = "$user->lastname $user->firstname";
2309
$testname = fullname($user);
2310
$this->assertSame($expectedname, $testname);
2312
$expectedname = get_string('fullnamedisplay', null, $user);
2313
$CFG->fullnamedisplay = 'language';
2314
$testname = fullname($user);
2315
$this->assertSame($expectedname, $testname);
2317
// Test override parameter.
2318
$CFG->fullnamedisplay = 'firstname';
2319
$expectedname = "$user->firstname $user->lastname";
2320
$testname = fullname($user, true);
2321
$this->assertSame($expectedname, $testname);
2323
// Test additional name fields.
2324
$CFG->fullnamedisplay = 'lastname lastnamephonetic firstname firstnamephonetic';
2325
$expectedname = "$user->lastname $user->lastnamephonetic $user->firstname $user->firstnamephonetic";
2326
$testname = fullname($user);
2327
$this->assertSame($expectedname, $testname);
2329
// Test for handling missing data.
2330
$user->middlename = null;
2331
// Parenthesis with no data.
2332
$CFG->fullnamedisplay = 'firstname (middlename) lastname';
2333
$expectedname = "$user->firstname $user->lastname";
2334
$testname = fullname($user);
2335
$this->assertSame($expectedname, $testname);
2337
// Extra spaces due to no data.
2338
$CFG->fullnamedisplay = 'firstname middlename lastname';
2339
$expectedname = "$user->firstname $user->lastname";
2340
$testname = fullname($user);
2341
$this->assertSame($expectedname, $testname);
2343
// Regular expression testing.
2344
// Remove some data from the user fields.
2345
$user->firstnamephonetic = '';
2346
$user->lastnamephonetic = '';
2348
// Removing empty brackets and excess whitespace.
2349
// All of these configurations should resolve to just firstname lastname.
2350
$configarray = array();
2351
$configarray[] = 'firstname lastname [firstnamephonetic lastnamephonetic]';
2352
$configarray[] = 'firstname lastname \'middlename\'';
2353
$configarray[] = 'firstname "firstnamephonetic" lastname';
2354
$configarray[] = 'firstname 「firstnamephonetic」 lastname 「lastnamephonetic」';
2356
foreach ($configarray as $config) {
2357
$CFG->fullnamedisplay = $config;
2358
$expectedname = "$user->firstname $user->lastname";
2359
$testname = fullname($user);
2360
$this->assertSame($expectedname, $testname);
2363
// Check to make sure that other characters are left in place.
2364
$configarray = array();
2365
$configarray['0'] = new stdClass();
2366
$configarray['0']->config = 'lastname firstname, middlename';
2367
$configarray['0']->expectedname = "$user->lastname $user->firstname,";
2368
$configarray['1'] = new stdClass();
2369
$configarray['1']->config = 'lastname firstname + alternatename';
2370
$configarray['1']->expectedname = "$user->lastname $user->firstname + $user->alternatename";
2371
$configarray['2'] = new stdClass();
2372
$configarray['2']->config = 'firstname aka: alternatename';
2373
$configarray['2']->expectedname = "$user->firstname aka: $user->alternatename";
2374
$configarray['3'] = new stdClass();
2375
$configarray['3']->config = 'firstname (alternatename)';
2376
$configarray['3']->expectedname = "$user->firstname ($user->alternatename)";
2377
$configarray['4'] = new stdClass();
2378
$configarray['4']->config = 'firstname [alternatename]';
2379
$configarray['4']->expectedname = "$user->firstname [$user->alternatename]";
2380
$configarray['5'] = new stdClass();
2381
$configarray['5']->config = 'firstname "lastname"';
2382
$configarray['5']->expectedname = "$user->firstname \"$user->lastname\"";
2384
foreach ($configarray as $config) {
2385
$CFG->fullnamedisplay = $config->config;
2386
$expectedname = $config->expectedname;
2387
$testname = fullname($user);
2388
$this->assertSame($expectedname, $testname);
2391
// Test debugging message displays when
2392
// fullnamedisplay setting is "normal".
2393
$CFG->fullnamedisplay = 'firstname lastname';
2395
$user = new stdClass();
2396
$user->firstname = 'Stan';
2397
$user->lastname = 'Lee';
2398
$namedisplay = fullname($user);
2399
$this->assertDebuggingCalled();
2401
// Tidy up after we finish testing.
2402
$CFG->fullnamedisplay = $originalcfg->fullnamedisplay;
2405
public function test_get_all_user_name_fields() {
2406
$this->resetAfterTest();
2408
// Additional names in an array.
2409
$testarray = array('firstnamephonetic' => 'firstnamephonetic',
2410
'lastnamephonetic' => 'lastnamephonetic',
2411
'middlename' => 'middlename',
2412
'alternatename' => 'alternatename',
2413
'firstname' => 'firstname',
2414
'lastname' => 'lastname');
2415
$this->assertEquals($testarray, get_all_user_name_fields());
2417
// Additional names as a string.
2418
$teststring = 'firstnamephonetic,lastnamephonetic,middlename,alternatename,firstname,lastname';
2419
$this->assertEquals($teststring, get_all_user_name_fields(true));
2421
// Additional names as a string with an alias.
2422
$teststring = 't.firstnamephonetic,t.lastnamephonetic,t.middlename,t.alternatename,t.firstname,t.lastname';
2423
$this->assertEquals($teststring, get_all_user_name_fields(true, 't'));
2425
// Additional name fields with a prefix - object.
2426
$testarray = array('firstnamephonetic' => 'authorfirstnamephonetic',
2427
'lastnamephonetic' => 'authorlastnamephonetic',
2428
'middlename' => 'authormiddlename',
2429
'alternatename' => 'authoralternatename',
2430
'firstname' => 'authorfirstname',
2431
'lastname' => 'authorlastname');
2432
$this->assertEquals($testarray, get_all_user_name_fields(false, null, 'author'));
2434
// Additional name fields with an alias and a title - string.
2435
$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';
2436
$this->assertEquals($teststring, get_all_user_name_fields(true, 'u', null, 'author'));
2439
public function test_order_in_string() {
2440
$this->resetAfterTest();
2442
// Return an array in an order as they are encountered in a string.
2443
$valuearray = array('second', 'firsthalf', 'first');
2444
$formatstring = 'first firsthalf some other text (second)';
2445
$expectedarray = array('0' => 'first', '6' => 'firsthalf', '33' => 'second');
2446
$this->assertEquals($expectedarray, order_in_string($valuearray, $formatstring));
2448
// Try again with a different order for the format.
2449
$valuearray = array('second', 'firsthalf', 'first');
2450
$formatstring = 'firsthalf first second';
2451
$expectedarray = array('0' => 'firsthalf', '10' => 'first', '16' => 'second');
2452
$this->assertEquals($expectedarray, order_in_string($valuearray, $formatstring));
2454
// Try again with yet another different order for the format.
2455
$valuearray = array('second', 'firsthalf', 'first');
2456
$formatstring = 'start seconds away second firstquater first firsthalf';
2457
$expectedarray = array('19' => 'second', '38' => 'first', '44' => 'firsthalf');
2458
$this->assertEquals($expectedarray, order_in_string($valuearray, $formatstring));
2461
public function test_complete_user_login() {
2464
$this->resetAfterTest();
2465
$user = $this->getDataGenerator()->create_user();
2468
$sink = $this->redirectEvents();
2469
$loginuser = clone($user);
2470
$this->setCurrentTimeStart();
2471
@complete_user_login($loginuser); // Hide session header errors.
2472
$this->assertSame($loginuser, $USER);
2473
$this->assertEquals($user->id, $USER->id);
2474
$events = $sink->get_events();
2477
$this->assertCount(1, $events);
2478
$event = reset($events);
2479
$this->assertInstanceOf('\core\event\user_loggedin', $event);
2480
$this->assertEquals('user', $event->objecttable);
2481
$this->assertEquals($user->id, $event->objectid);
2482
$this->assertEquals(context_system::instance()->id, $event->contextid);
2484
$user = $DB->get_record('user', array('id'=>$user->id));
2486
$this->assertTimeCurrent($user->firstaccess);
2487
$this->assertTimeCurrent($user->lastaccess);
2489
$this->assertTimeCurrent($USER->firstaccess);
2490
$this->assertTimeCurrent($USER->lastaccess);
2491
$this->assertTimeCurrent($USER->currentlogin);
2492
$this->assertSame(sesskey(), $USER->sesskey);
2493
$this->assertTimeCurrent($USER->preference['_lastloaded']);
2494
$this->assertObjectNotHasAttribute('password', $USER);
2495
$this->assertObjectNotHasAttribute('description', $USER);
2499
* Test require_logout.
2501
public function test_require_logout() {
2502
$this->resetAfterTest();
2503
$user = $this->getDataGenerator()->create_user();
2504
$this->setUser($user);
2506
$this->assertTrue(isloggedin());
2508
// Logout user and capture event.
2509
$sink = $this->redirectEvents();
2511
$events = $sink->get_events();
2513
$event = array_pop($events);
2515
// Check if user is logged out.
2516
$this->assertFalse(isloggedin());
2519
$this->assertInstanceOf('\core\event\user_loggedout', $event);
2520
$this->assertSame($user->id, $event->objectid);
2521
$this->assertSame('user_logout', $event->get_legacy_eventname());
2522
$this->assertEventLegacyData($user, $event);
2523
$expectedlogdata = array(SITEID, 'user', 'logout', 'view.php?id='.$event->objectid.'&course='.SITEID, $event->objectid, 0,
2525
$this->assertEventLegacyLogData($expectedlogdata, $event);
2672
2528
public function test_email_to_user() {
2673
2531
$this->resetAfterTest();
2675
2533
$user1 = $this->getDataGenerator()->create_user();
2699
2564
$this->assertSame($messagetext2, trim($result[1]->body));
2700
2565
$this->assertSame($user2->email, $result[1]->to);
2701
2566
$this->assertSame($user1->email, $result[1]->from);
2568
email_to_user($user1, $user2, $subject, $messagetext);
2569
$this->assertDebuggingCalled('Unit tests must not send real emails! Use $this->redirectEmails()');
2573
* Test user_updated event trigger by various apis.
2575
public function test_user_updated_event() {
2578
$this->resetAfterTest();
2580
$user = $this->getDataGenerator()->create_user();
2582
// Set config to allow email_to_user() to be called.
2583
$CFG->noemailever = false;
2585
// Update user password.
2586
$sink = $this->redirectEvents();
2587
$sink2 = $this->redirectEmails(); // Make sure we are redirecting emails.
2588
setnew_password_and_mail($user);
2589
update_internal_user_password($user, 'randompass');
2590
$events = $sink->get_events();
2594
// Test updated value.
2595
$dbuser = $DB->get_record('user', array('id' => $user->id));
2596
$this->assertSame($user->firstname, $dbuser->firstname);
2597
$this->assertNotSame('M00dLe@T', $dbuser->password);
2600
foreach ($events as $event) {
2601
$this->assertInstanceOf('\core\event\user_updated', $event);
2602
$this->assertSame($user->id, $event->objectid);
2603
$this->assertSame('user_updated', $event->get_legacy_eventname());
2604
$this->assertEventLegacyData($user, $event);
2605
$this->assertEquals(context_user::instance($user->id), $event->get_context());
2606
$expectedlogdata = array(SITEID, 'user', 'update', 'view.php?id='.$user->id, '');
2607
$this->assertEventLegacyLogData($expectedlogdata, $event);
2612
* Test remove_course_content deletes course contents
2613
* TODO Add asserts to verify other data related to course is deleted as well.
2615
public function test_remove_course_contents() {
2617
$this->resetAfterTest();
2619
$course = $this->getDataGenerator()->create_course();
2620
$user = $this->getDataGenerator()->create_user();
2621
$gen = $this->getDataGenerator()->get_plugin_generator('core_notes');
2622
$note = $gen->create_instance(array('courseid' => $course->id, 'userid' => $user->id));
2624
$this->assertNotEquals(false, note_load($note->id));
2625
remove_course_contents($course->id, false);
2626
$this->assertFalse(note_load($note->id));
2630
* Test function username_load_fields_from_object().
2632
public function test_username_load_fields_from_object() {
2633
$this->resetAfterTest();
2635
// This object represents the information returned from an sql query.
2636
$userinfo = new stdClass();
2637
$userinfo->userid = 1;
2638
$userinfo->username = 'loosebruce';
2639
$userinfo->firstname = 'Bruce';
2640
$userinfo->lastname = 'Campbell';
2641
$userinfo->firstnamephonetic = 'ブルース';
2642
$userinfo->lastnamephonetic = 'カンベッル';
2643
$userinfo->middlename = '';
2644
$userinfo->alternatename = '';
2645
$userinfo->email = '';
2646
$userinfo->picture = 23;
2647
$userinfo->imagealt = 'Michael Jordan draining another basket.';
2648
$userinfo->idnumber = 3982;
2650
// Just user name fields.
2651
$user = new stdClass();
2652
$user = username_load_fields_from_object($user, $userinfo);
2653
$expectedarray = new stdClass();
2654
$expectedarray->firstname = 'Bruce';
2655
$expectedarray->lastname = 'Campbell';
2656
$expectedarray->firstnamephonetic = 'ブルース';
2657
$expectedarray->lastnamephonetic = 'カンベッル';
2658
$expectedarray->middlename = '';
2659
$expectedarray->alternatename = '';
2660
$this->assertEquals($user, $expectedarray);
2662
// User information for showing a picture.
2663
$user = new stdClass();
2664
$additionalfields = explode(',', user_picture::fields());
2665
$user = username_load_fields_from_object($user, $userinfo, null, $additionalfields);
2666
$user->id = $userinfo->userid;
2667
$expectedarray = new stdClass();
2668
$expectedarray->id = 1;
2669
$expectedarray->firstname = 'Bruce';
2670
$expectedarray->lastname = 'Campbell';
2671
$expectedarray->firstnamephonetic = 'ブルース';
2672
$expectedarray->lastnamephonetic = 'カンベッル';
2673
$expectedarray->middlename = '';
2674
$expectedarray->alternatename = '';
2675
$expectedarray->email = '';
2676
$expectedarray->picture = 23;
2677
$expectedarray->imagealt = 'Michael Jordan draining another basket.';
2678
$this->assertEquals($user, $expectedarray);
2680
// Alter the userinfo object to have a prefix.
2681
$userinfo->authorfirstname = 'Bruce';
2682
$userinfo->authorlastname = 'Campbell';
2683
$userinfo->authorfirstnamephonetic = 'ブルース';
2684
$userinfo->authorlastnamephonetic = 'カンベッル';
2685
$userinfo->authormiddlename = '';
2686
$userinfo->authorpicture = 23;
2687
$userinfo->authorimagealt = 'Michael Jordan draining another basket.';
2688
$userinfo->authoremail = 'test@testing.net';
2691
// Return an object with user picture information.
2692
$user = new stdClass();
2693
$additionalfields = explode(',', user_picture::fields());
2694
$user = username_load_fields_from_object($user, $userinfo, 'author', $additionalfields);
2695
$user->id = $userinfo->userid;
2696
$expectedarray = new stdClass();
2697
$expectedarray->id = 1;
2698
$expectedarray->firstname = 'Bruce';
2699
$expectedarray->lastname = 'Campbell';
2700
$expectedarray->firstnamephonetic = 'ブルース';
2701
$expectedarray->lastnamephonetic = 'カンベッル';
2702
$expectedarray->middlename = '';
2703
$expectedarray->alternatename = '';
2704
$expectedarray->email = 'test@testing.net';
2705
$expectedarray->picture = 23;
2706
$expectedarray->imagealt = 'Michael Jordan draining another basket.';
2707
$this->assertEquals($user, $expectedarray);