forked from backdrop/backdrop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.test
2818 lines (2401 loc) · 120 KB
/
user.test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Tests for user.module.
*/
include_once(BACKDROP_ROOT . '/core/modules/simpletest/tests/system_config_test.inc');
class UserLoginTestBase extends BackdropWebTestCase {
protected $profile = 'minimal';
function setUp() {
parent::setUp('dblog', 'user_flood_test');
}
/**
* Make an unsuccessful login attempt.
*
* @param User $account
* A user object with name and pass_raw attributes for the login attempt.
* @param bool $by_email
* Fail with an incorrect email instead of username.
* @param $incorrect_pass
* Fail because of an incorrect password.
* @param $flood_trigger
* Whether or not to expect that the flood control mechanism will be
* triggered..
*/
function assertFailedLogin($account, $by_email = FALSE, $incorrect_pass = FALSE, $flood_trigger = NULL) {
if ($this->loggedInUser) {
$this->backdropLogout();
}
$edit = array(
'name' => $by_email ? $account->mail : $account->name,
'pass' => $account->pass_raw,
);
$this->backdropPost('user', $edit, t('Log in'));
$this->assertNoFieldByXPath("//input[@name='pass' and @value!='']", NULL, 'Password value attribute is blank.');
if (isset($flood_trigger)) {
$this->assertResponse(403);
$user_log = db_query_range('SELECT message FROM {watchdog} WHERE type = :type ORDER BY wid DESC', 0, 1, array(':type' => 'user'))->fetchField();
$user_flood_test_log = db_query_range('SELECT message FROM {watchdog} WHERE type = :type ORDER BY wid DESC', 0, 1, array(':type' => 'user_flood_test'))->fetchField();
if ($flood_trigger == 'user') {
$this->assertRaw(t('Sorry, there have been more than @count failed login attempts for this account. It is temporarily blocked. Try again later or <a href="@url">request a new password</a>.', array('@url' => url('user/password'), '@count' => config_get('user.flood', 'flood_user_limit'))));
$this->assertEqual('Flood control blocked login attempt for %user from %ip.', $user_log, 'A watchdog message was logged for the login attempt blocked by flood control per user');
$this->assertEqual('hook_user_flood_control was passed username %username and IP %ip.', $user_flood_test_log, 'hook_user_flood_control was invoked by flood control per user');
}
else {
// No UID, so the limit is IP-based.
$this->assertRaw(t('Sorry, too many failed login attempts from your IP address. This IP address is temporarily blocked. Try again later or <a href="@url">request a new password</a>.', array('@url' => url('user/password'))));
$this->assertEqual('Flood control blocked login attempt from %ip.', $user_log, 'A watchdog message was logged for the login attempt blocked by flood control per IP');
$this->assertEqual('hook_user_flood_control was passed IP %ip.', $user_flood_test_log, 'hook_user_flood_control was invoked by flood control per IP');
}
}
elseif ($incorrect_pass) {
$this->assertRaw(t('Sorry, incorrect password. <a href="@password">Have you forgotten your password?</a>', array('@password' => url('user/password', array('query' => array('name' => $edit['name']))))));
}
else {
$login_method = config_get('system.core', 'user_login_method');
switch ($login_method) {
case USER_LOGIN_EMAIL_ONLY:
if (!$by_email) {
$this->assertRaw(t('The email address %email is not valid.', array('%email' => $account->name)));
}
else {
$this->assertRaw(t('Sorry, no account with that email address found.'));
}
break;
case USER_LOGIN_USERNAME_OR_EMAIL:
if (!$by_email) {
$this->assertRaw(t('Sorry, unrecognized username.'));
}
else {
$this->assertRaw(t('Sorry, no account with that email address found.'));
}
break;
case USER_LOGIN_USERNAME_ONLY:
default:
$this->assertRaw(t('Sorry, unrecognized username.'));
}
}
}
}
class UserRegistrationTestCase extends UserLoginTestBase {
protected $profile = 'testing';
function testRegistrationWithEmailVerification() {
$config = config('system.core');
// Require email verification.
$config->set('user_email_verification', TRUE);
// Set registration to administrator only.
$config->set('user_register', USER_REGISTER_ADMINISTRATORS_ONLY);
$config->save();
$this->backdropGet('user/register');
$this->assertResponse(403, 'Registration page is inaccessible when only administrators can create accounts.');
// Allow registration by site visitors without administrator approval.
$config->set('user_register', USER_REGISTER_VISITORS)->save();
$edit = array();
$edit['name'] = $name = $this->randomName();
$edit['mail'] = $mail = $edit['name'] . '@example.com';
$this->backdropPost('user/register', $edit, t('Create new account'));
$this->assertText(t('A welcome message with further instructions has been sent to your email address.'), 'User registered successfully.');
$accounts = user_load_multiple(array(), array('name' => $name, 'mail' => $mail));
$new_user = reset($accounts);
$this->assertTrue($new_user->status, 'New account is active after registration.');
// Allow registration by site visitors, but require administrator approval.
$config->set('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)->save();
$edit = array();
$edit['name'] = $name = $this->randomName();
$edit['mail'] = $mail = $edit['name'] . '@example.com';
$this->backdropPost('user/register', $edit, t('Create new account'));
$accounts = user_load_multiple(array(), array('name' => $name, 'mail' => $mail));
$new_user = reset($accounts);
$this->assertFalse($new_user->status, 'New account is blocked until approved by an administrator.');
}
function testRegistrationWithoutEmailVerification() {
$config = config('system.core');
// Don't require email verification.
$config->set('user_email_verification', FALSE)->save();
// Allow registration by site visitors without administrator approval.
$config->set('user_register', USER_REGISTER_VISITORS)->save();
$edit = array();
$edit['name'] = $name = $this->randomName();
$edit['mail'] = $mail = $edit['name'] . '@example.com';
$edit['pass'] = $new_pass = $this->randomName();
$this->backdropPost('user/register', $edit, t('Create new account'));
$accounts = user_load_multiple(array(), array('name' => $name, 'mail' => $mail));
$new_user = reset($accounts);
$this->assertText(t('Registration successful. You are now logged in.'), 'Users are logged in after registering.');
$this->backdropLogout();
// Allow registration by site visitors, but require administrator approval.
$config->set('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)->save();
$edit = array();
$edit['name'] = $name = $this->randomName();
$edit['mail'] = $mail = $edit['name'] . '@example.com';
$edit['pass'] = $pass = $this->randomName();
$this->backdropPost('user/register', $edit, t('Create new account'));
$this->assertText(t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.'), 'Users are notified of pending approval');
// Try to login before administrator approval.
$auth = array(
'name' => $name,
'pass' => $pass,
);
$this->backdropPost('user/login', $auth, t('Log in'));
$this->assertText(t('The account for @name has not been activated or is blocked.', array('@name' => $name)), 'User cannot login yet.');
// Activate the new account.
$accounts = user_load_multiple(array(), array('name' => $name, 'mail' => $mail));
$new_user = reset($accounts);
$admin_user = $this->backdropCreateUser(array('administer users'));
$this->backdropLogin($admin_user);
$edit = array(
'status' => 1,
);
$this->backdropPost('user/' . $new_user->uid . '/edit', $edit, t('Save'));
$this->backdropLogout();
// Login after administrator approval.
$this->backdropPost('user/login', $auth, t('Log in'));
$this->assertText(t('Member for'), 'User can log in after administrator approval.');
}
function testRegistrationEmailDuplicates() {
$config = config('system.core');
// Don't require email verification.
$config->set('user_email_verification', FALSE)->save();
// Allow registration by site visitors without administrator approval.
$config->set('user_register', USER_REGISTER_VISITORS)->save();
// Set up a user to check for duplicates.
$duplicate_user = $this->backdropCreateUser();
$edit = array();
$edit['name'] = $this->randomName();
$edit['mail'] = $duplicate_user->mail;
// Attempt to create a new account using an existing email address.
$this->backdropPost('user/register', $edit, t('Create new account'));
$this->assertRaw(t('The email address %email is already registered.', array('%email' => $duplicate_user->mail)), 'Supplying an exact duplicate email address displays an error message');
// Attempt to bypass duplicate email registration validation by adding spaces.
$edit['mail'] = ' ' . $duplicate_user->mail . ' ';
$this->backdropPost('user/register', $edit, t('Create new account'));
$this->assertRaw(t('The email address %email is already registered.', array('%email' => $duplicate_user->mail)), 'Supplying a duplicate email address with added whitespace displays an error message');
}
function testRegistrationDefaultValues() {
$config_user_settings = config('system.core');
// Allow registration by site visitors without administrator approval.
$config_user_settings->set('user_register', USER_REGISTER_VISITORS)->save();
// Don't require email verification.
$config_user_settings->set('user_email_verification', FALSE)->save();
// Set the default timezone to Brussels.
$config_system_date = config('system.date')
->set('user_configurable_timezones', 1)
->set('default_timezone', 'Europe/Brussels')
->save();
// Check that the account information fieldset's options are not displayed
// is a fieldset if there is not more than one fieldset in the form.
$this->backdropGet('user/register');
$this->assertNoRaw('<fieldset id="edit-account"><legend>Account information</legend>', 'Account settings fieldset was hidden.');
$edit = array();
$edit['name'] = $name = $this->randomName();
$edit['mail'] = $mail = $edit['name'] . '@example.com';
$edit['pass'] = $new_pass = $this->randomName();
$this->backdropPost(NULL, $edit, t('Create new account'));
// Check user fields.
$accounts = user_load_multiple(array(), array('name' => $name, 'mail' => $mail));
$new_user = reset($accounts);
$this->assertEqual($new_user->name, $name, 'Username matches.');
$this->assertEqual($new_user->mail, $mail, 'Email address matches.');
$this->assertEqual($new_user->signature, '', 'Correct signature field.');
$this->assertTrue(($new_user->created > REQUEST_TIME - 20 ), 'Correct creation time.');
$this->assertEqual($new_user->changed, $new_user->created, 'Correct changed time.');
$this->assertEqual($new_user->status, $config_user_settings->get('user_register') == USER_REGISTER_VISITORS ? 1 : 0, 'Correct status field.');
$this->assertEqual($new_user->timezone, $config_system_date->get('default_timezone'), 'Correct time zone field.');
$this->assertEqual($new_user->language, '', 'Correct language field.');
$this->assertEqual($new_user->picture, 0, 'Correct picture field.');
$this->assertEqual($new_user->init, $mail, 'Correct init field.');
}
/**
* Tests Field API fields on user registration forms.
*/
function testRegistrationWithUserFields() {
module_enable(array('field', 'field_test'));
$config_user_settings = config('system.core');
// Create a field, and an instance on 'user' entity type.
$field = array(
'type' => 'test_field',
'field_name' => 'test_user_field',
'cardinality' => 1,
);
field_create_field($field);
$instance = array(
'field_name' => 'test_user_field',
'entity_type' => 'user',
'label' => 'Some user field',
'bundle' => 'user',
'required' => TRUE,
'settings' => array('user_register_form' => FALSE),
);
field_create_instance($instance);
// Check that the field does not appear on the registration form.
$config_user_settings->set('user_register', USER_REGISTER_VISITORS)->save();
$this->backdropGet('user/register');
$this->assertNoText($instance['label'], 'The field does not appear on user registration form');
// Have the field appear on the registration form.
$instance['settings']['user_register_form'] = TRUE;
field_update_instance($instance);
$this->backdropGet('user/register');
$this->assertText($instance['label'], 'The field appears on user registration form');
// Check that validation errors are correctly reported.
$edit = array();
$edit['name'] = $name = $this->randomName();
$edit['mail'] = $mail = $edit['name'] . '@example.com';
// Missing input in required field.
$edit['test_user_field[und][0][value]'] = '';
$this->backdropPost(NULL, $edit, t('Create new account'));
$this->assertRaw(t('@name field is required.', array('@name' => $instance['label'])), 'Field validation error was correctly reported.');
// Invalid input.
$edit['test_user_field[und][0][value]'] = '-1';
$this->backdropPost(NULL, $edit, t('Create new account'));
$this->assertRaw(t('%name does not accept the value -1.', array('%name' => $instance['label'])), 'Field validation error was correctly reported.');
// Submit with valid data.
$value = rand(1, 255);
$edit['test_user_field[und][0][value]'] = $value;
$this->backdropPost(NULL, $edit, t('Create new account'));
// Check user fields.
$accounts = user_load_multiple(array(), array('name' => $name, 'mail' => $mail));
$new_user = reset($accounts);
$this->assertEqual($new_user->test_user_field[LANGUAGE_NONE][0]['value'], $value, 'The field value was correctly saved.');
// Check that the 'add more' button works.
$field['cardinality'] = FIELD_CARDINALITY_UNLIMITED;
field_update_field($field);
foreach (array('js', 'nojs') as $js) {
$this->backdropGet('user/register');
// Add two inputs.
$value = rand(1, 255);
$edit = array();
$edit['test_user_field[und][0][value]'] = $value;
if ($js == 'js') {
$this->backdropPostAJAX(NULL, $edit, 'test_user_field_add_more');
$this->backdropPostAJAX(NULL, $edit, 'test_user_field_add_more');
}
else {
$this->backdropPost(NULL, $edit, t('Add another'));
$this->backdropPost(NULL, $edit, t('Add another'));
}
// Submit with three values.
$edit['test_user_field[und][1][value]'] = $value + 1;
$edit['test_user_field[und][2][value]'] = $value + 2;
$edit['name'] = $name = $this->randomName();
$edit['mail'] = $mail = $edit['name'] . '@example.com';
$this->backdropPost(NULL, $edit, t('Create new account'));
// Check user fields.
$accounts = user_load_multiple(array(), array('name' => $name, 'mail' => $mail));
$new_user = reset($accounts);
$this->assertEqual($new_user->test_user_field[LANGUAGE_NONE][0]['value'], $value, format_string('@js : The field value was correctly saved.', array('@js' => $js)));
$this->assertEqual($new_user->test_user_field[LANGUAGE_NONE][1]['value'], $value + 1, format_string('@js : The field value was correctly saved.', array('@js' => $js)));
$this->assertEqual($new_user->test_user_field[LANGUAGE_NONE][2]['value'], $value + 2, format_string('@js : The field value was correctly saved.', array('@js' => $js)));
}
}
/**
* Tests new users username matches their email if username is an email.
*/
function testRegistrationEmailAsUsername() {
// Don't require email verification.
// Allow registration by site visitors without administrator approval.
config('system.core')
->set('user_email_verification', FALSE)
->set('user_register', USER_REGISTER_VISITORS)
->save();
$mail = $this->randomName() . '@example.com';
$different = $this->randomName() . $mail;
// Set up edit array.
$edit = array();
$edit['mail'] = $mail;
$edit['name'] = $different;
$edit['pass'] = $this->randomName();
// Attempt to create an account using an email that doesn't match the name.
$this->backdropPost('user/register', $edit, t('Create new account'));
$this->assertText(t('An email address was provided as a username, but does not match the account email address.'), 'Email username does not match user email - error message found.');
$this->assertNoText(t('Registration successful. You are now logged in.'), 'The user was not created and logged in.');
// Attempt to create new account using matching email address.
$edit['name'] = $edit['mail'] = $this->randomName() . '@example.com';
$this->backdropPost('user/register', $edit, t('Create new account'));
$this->assertText(t('Registration successful. You are now logged in.'), 'The user was created and logged in with matching email.');
$new_user = user_load_by_name($edit['name']);
$this->assertTrue(($new_user->name === $edit['name']) && ($new_user->mail === $edit['mail']), 'Created user with matching username and email address.');
}
/**
* Tests new users username not matching their email if username is an email.
*/
function testRegistrationEmailAsUsernameDisabled() {
// Test that 'user_email_match' turned off allows emails that don't match.
config('system.core')
->set('user_email_match', FALSE)
->set('user_email_verification', FALSE)
->set('user_register', USER_REGISTER_VISITORS)
->save();
$mail = $this->randomName() . '@example.com';
$different = $this->randomName() . $mail;
$edit = array();
$edit['mail'] = $mail;
$edit['name'] = $different;
$edit['pass'] = $this->randomName();
// Attempt to create an account using an email that doesn't match the name.
// This should be OK, as 'user_email_match' is disabled.
$this->backdropPost('user/register', $edit, t('Create new account'));
$this->assertNoText(t('An email address was provided as a username, but does not match the account email address.'), 'Email username does not match user email - error message found.');
$this->assertText(t('Registration successful. You are now logged in.'), 'The user was not created and logged in.');
}
}
class UserValidationTestCase extends BackdropUnitTestCase {
// Username validation.
function testUsernames() {
$test_cases = array( // '<username>' => array('<description>', 'assert<testName>'),
'foo' => array('Valid username', 'assertNull'),
'FOO' => array('Valid username', 'assertNull'),
'Foo O\'Bar' => array('Valid username', 'assertNull'),
'foo@bar' => array('Valid username', 'assertNull'),
'foo@example.com' => array('Valid username', 'assertNull'),
// Invalid domains are allowed in usernames.
'foo@-example.com' => array('Valid username', 'assertNull'),
// cspell:disable
'þòøÇߪř€' => array('Valid username', 'assertNull'),
// Runes.
'ᚠᛇᚻ᛫ᛒᛦᚦ' => array('Valid UTF8 username', 'assertNull'),
// cspell:enable
// '+' symbol is allowed.
'foo+bar' => array('Valid username', 'assertNull'),
' foo' => array('Invalid username that starts with a space', 'assertNotNull'),
'foo ' => array('Invalid username that ends with a space', 'assertNotNull'),
'foo bar' => array('Invalid username that contains 2 spaces \' \'', 'assertNotNull'),
'' => array('Invalid empty username', 'assertNotNull'),
'foo/' => array('Invalid username containing invalid chars', 'assertNotNull'),
// NULL.
'foo' . chr(0) . 'bar' => array('Invalid username containing chr(0)', 'assertNotNull'),
// CR.
'foo' . chr(13) . 'bar' => array('Invalid username containing chr(13)', 'assertNotNull'),
str_repeat('x', USERNAME_MAX_LENGTH + 1) => array('Invalid excessively long username', 'assertNotNull'),
);
foreach ($test_cases as $name => $test_case) {
list($description, $test) = $test_case;
$result = user_validate_name($name);
$this->$test($result, $description . ' (' . $name . ')');
}
}
}
/**
* Functional tests for user logins, including rate limiting of login attempts.
*/
class UserLoginTestCase extends UserLoginTestBase {
protected $profile = 'testing';
function setUp() {
parent::setUp('user_session_test');
}
/**
* Test that login credentials work (or not) in different login modes.
*/
function testLoginMethods() {
$account = $this->backdropCreateUser(array());
// Login via name or email (both succeed). The default for new sites.
config_set('system.core', 'user_login_method', USER_LOGIN_USERNAME_OR_EMAIL);
$this->backdropLogin($account, TRUE);
$this->backdropLogin($account);
// Login via email only.
config_set('system.core', 'user_login_method', USER_LOGIN_EMAIL_ONLY);
$this->backdropLogin($account, TRUE);
// Fail login via name.
$this->assertFailedLogin($account);
// Login via name.
config_set('system.core', 'user_login_method', USER_LOGIN_USERNAME_ONLY);
$this->backdropLogin($account);
// Fail login via email.
$this->assertFailedLogin($account, TRUE);
// Test login when no user_login_method is set. Allows for logging in for
// users that are upgrading their site for the first time to support login
// by username.
config_set('system.core', 'user_login_method', NULL);
$this->backdropLogin($account);
// Fail login via email.
$this->assertFailedLogin($account, TRUE);
}
/**
* Test the global login flood control.
*/
function testGlobalLoginFloodControl() {
config('user.flood')
->set('flood_ip_limit', 10)
// Set a high per-user limit out so that it is not relevant in the test.
->set('flood_user_limit', 4000)
->save();
$user1 = $this->backdropCreateUser(array());
$incorrect_user1 = clone $user1;
$incorrect_user1->pass_raw .= 'incorrect';
// Try 2 failed logins.
for ($i = 0; $i < 2; $i++) {
$this->assertFailedLogin($incorrect_user1, NULL, TRUE);
}
// A successful login will not reset the IP-based flood control count.
$this->backdropLogin($user1);
$this->backdropLogout();
// Try 8 more failed logins, they should not trigger the flood control
// mechanism.
for ($i = 0; $i < 8; $i++) {
$this->assertFailedLogin($incorrect_user1, NULL, TRUE);
}
// The next login trial should result in an IP-based flood error message.
$this->assertFailedLogin($incorrect_user1, NULL, TRUE, 'ip');
// A login with the correct password should also result in a flood error
// message.
$this->assertFailedLogin($user1, NULL, TRUE, 'ip');
}
/**
* Test the per-user login flood control.
*/
function testPerUserLoginFloodControl() {
config('user.flood')
// Set a high global limit out so that it is not relevant in the test.
->set('flood_ip_limit', 4000)
->set('flood_user_limit', 3)
->save();
$user1 = $this->backdropCreateUser(array());
$incorrect_user1 = clone $user1;
$incorrect_user1->pass_raw .= 'incorrect';
$user2 = $this->backdropCreateUser(array());
// Try 2 failed logins.
for ($i = 0; $i < 2; $i++) {
$this->assertFailedLogin($incorrect_user1, NULL, TRUE);
}
// A successful login will reset the per-user flood control count.
$this->backdropLogin($user1);
$this->backdropLogout();
// Try 3 failed logins for user 1, they will not trigger flood control.
for ($i = 0; $i < 3; $i++) {
$this->assertFailedLogin($incorrect_user1, NULL, TRUE);
}
// Try one successful attempt for user 2, it should not trigger any
// flood control.
$this->backdropLogin($user2);
$this->backdropLogout();
// Try one more attempt for user 1, it should be rejected, even if the
// correct password has been used.
$this->assertFailedLogin($user1, NULL, TRUE, 'user');
}
/**
* Test that user password is re-hashed upon login after changing $count_log2.
*/
function testPasswordRehashOnLogin() {
// Load password hashing API.
require_once BACKDROP_ROOT . '/' . settings_get('password_inc', 'core/includes/password.inc');
// Set initial $count_log2 to the default, BACKDROP_HASH_COUNT.
$GLOBALS['settings']['password_count_log2'] = BACKDROP_HASH_COUNT;
tempstore_set('simpletest', 'settings', array(
'password_count_log2' => BACKDROP_HASH_COUNT,
), REQUEST_TIME + 3600);
// Create a new user and authenticate.
$account = $this->backdropCreateUser(array());
$password = $account->pass_raw;
$this->backdropLogin($account);
$this->backdropLogout();
// Load the stored user. The password hash should reflect $count_log2.
$account = user_load($account->uid);
$this->assertIdentical(_password_get_count_log2($account->pass), BACKDROP_HASH_COUNT);
// Change $count_log2 and log in again.
tempstore_set('simpletest', 'settings', array(
'password_count_log2' => BACKDROP_HASH_COUNT + 1,
), REQUEST_TIME + 3600);
$account->pass_raw = $password;
$this->backdropLogin($account);
// Load the stored user, which should have a different password hash now.
$account = user_load($account->uid, TRUE);
$this->assertIdentical(_password_get_count_log2($account->pass), BACKDROP_HASH_COUNT + 1);
}
/**
* Test logging in when an anon session already exists.
*/
function testLoginWithAnonSession() {
// Visit the callback to generate a session for this anon user.
$this->backdropGet('user_session_test_anon_session');
// Now login.
$account = $this->backdropCreateUser(array());
$this->backdropLogin($account);
}
/**
* Attempt to login with an unregistered username.
*/
function testAccountNotFound() {
$edit = array(
'name' => $this->randomName(8),
'pass' => $this->randomName(8),
);
$this->backdropPost('user', $edit, t('Log in'));
$this->assertText(t('Sorry, unrecognized username.'));
}
/**
* Attempt to login with an invalid array users and passwords.
*/
function testArrayLoginValues() {
$edit = array(
'pass' => $this->randomName(8),
);
$extra_post = '&' . http_build_query(array(
'name[0]' => $this->randomName(8),
'name[1]' => $this->randomName(8),
));
$this->backdropPost('user', $edit, t('Log in'), array(), array(), NULL, $extra_post);
$this->assertText(t('Sorry, unrecognized username.'));
$account = $this->backdropCreateUser(array());
$edit = array(
'name' => $account->name,
);
$extra_post = '&' . http_build_query(array(
'pass[0]' => $this->randomName(8),
'pass[1]' => $this->randomName(8),
));
$this->backdropPost('user', $edit, t('Log in'), array(), array(), NULL, $extra_post);
$this->assertText(t('Sorry, incorrect password.'));
}
}
/**
* Test cancelling a user.
*/
class UserCancelTestCase extends BackdropWebTestCase {
/**
* @var User
*/
protected $admin_user;
/**
* Attempt to cancel account without permission.
*/
function testUserCancelWithoutPermission() {
config_set('system.core', 'user_cancel_method', 'user_cancel_reassign');
// Create a user.
$account = $this->backdropCreateUser(array());
$this->backdropLogin($account);
// Load real user object.
$account = user_load($account->uid, TRUE);
// Create a node.
$node = $this->backdropCreateNode(array('uid' => $account->uid));
// Attempt to cancel account.
$this->backdropGet('user/' . $account->uid . '/edit');
$this->assertNoRaw(t('Cancel account'), 'No cancel account button displayed.');
// Attempt bogus account cancellation request confirmation.
$timestamp = $account->login;
$this->backdropGet("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid));
$this->assertResponse(403, 'Bogus cancelling request rejected.');
$account = user_load($account->uid);
$this->assertTrue($account->status == 1, 'User account was not canceled.');
// Confirm user's content has not been altered.
$test_node = node_load($node->nid, NULL, TRUE);
$this->assertTrue(($test_node->uid == $account->uid && $test_node->status == 1), 'Node of the user has not been altered.');
}
/**
* Tests that user account for uid 1 cannot be cancelled.
*
* This should never be possible, or the site owner would become unable to
* administer the site.
*/
function testUserCancelUid1() {
// Update uid 1's name and password to we know it.
$password = user_password();
require_once BACKDROP_ROOT . '/' . settings_get('password_inc', 'core/includes/password.inc');
$account = array(
'name' => 'user1',
'pass' => user_hash_password(trim($password)),
);
// We cannot use $account->save() here, because this would result in the
// password being hashed again.
db_update('users')
->fields($account)
->condition('uid', 1)
->execute();
// Reload and log in uid 1.
$user1 = user_load(1, TRUE);
$user1->pass_raw = $password;
// Try to cancel uid 1's account with a different user.
$this->admin_user = $this->backdropCreateUser(array('administer users'));
$this->backdropLogin($this->admin_user);
$edit = array(
'action' => 'user_cancel_user_action',
'bulk_form[0]' => TRUE,
);
$this->backdropPost('admin/people', $edit, t('Execute'));
// Verify that uid 1's account was not cancelled.
$user1 = user_load(1, TRUE);
$this->assertEqual($user1->status, 1, 'User #1 still exists and is not blocked.');
}
/**
* Attempt invalid account cancellations.
*/
function testUserCancelInvalid() {
config_set('system.core', 'user_cancel_method', 'user_cancel_reassign');
// Create a user.
$account = $this->backdropCreateUser(array('cancel account'));
$this->backdropLogin($account);
// Load real user object.
$account = user_load($account->uid, TRUE);
// Create a node.
$node = $this->backdropCreateNode(array('uid' => $account->uid));
// Attempt to cancel account.
$this->backdropPost('user/' . $account->uid . '/edit', NULL, t('Cancel account'));
// Confirm account cancellation.
$timestamp = time();
$this->backdropPost(NULL, NULL, t('Cancel account'));
$this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
// Attempt bogus account cancellation request confirmation.
$bogus_timestamp = $timestamp + 60;
$this->backdropGet("user/$account->uid/cancel/confirm/$bogus_timestamp/" . user_pass_rehash($account->pass, $bogus_timestamp, $account->login, $account->uid));
$this->assertText(t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'), 'Bogus cancelling request rejected.');
$account = user_load($account->uid);
$this->assertTrue($account->status == 1, 'User account was not canceled.');
// Attempt expired account cancellation request confirmation.
$bogus_timestamp = $timestamp - 86400 - 60;
$this->backdropGet("user/$account->uid/cancel/confirm/$bogus_timestamp/" . user_pass_rehash($account->pass, $bogus_timestamp, $account->login, $account->uid));
$this->assertText(t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'), 'Expired cancel account request rejected.');
$accounts = user_load_multiple(array($account->uid), array('status' => 1));
$this->assertTrue(reset($accounts), 'User account was not canceled.');
// Confirm user's content has not been altered.
$test_node = node_load($node->nid, NULL, TRUE);
$this->assertTrue(($test_node->uid == $account->uid && $test_node->status == 1), 'Node of the user has not been altered.');
}
/**
* Disable account and keep all content.
*/
function testUserBlock() {
config_set('system.core', 'user_cancel_method', 'user_cancel_block');
// Create a user.
$web_user = $this->backdropCreateUser(array('cancel account'));
$this->backdropLogin($web_user);
// Load real user object.
$account = user_load($web_user->uid, TRUE);
// Attempt to cancel account.
$this->backdropGet('user/' . $account->uid . '/edit');
$this->backdropPost(NULL, NULL, t('Cancel account'));
$this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
$this->assertText(t('Your account will be blocked and you will no longer be able to log in. All of your content will remain attributed to your user name.'), 'Informs that all content will be remain as is.');
$this->assertNoText(t('Select the method to cancel the account above.'), 'Does not allow user to select account cancellation method.');
// Confirm account cancellation.
$timestamp = time();
$this->backdropPost(NULL, NULL, t('Cancel account'));
$this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
// Confirm account cancellation request.
$this->backdropGet("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid));
$account = user_load($account->uid, TRUE);
$this->assertTrue($account->status == 0, 'User has been blocked.');
// Confirm that the confirmation message made it through to the end user.
$this->assertRaw(t('%name has been disabled.', array('%name' => $account->name)), 'Confirmation message displayed to user.');
}
/**
* Disable account and unpublish all content.
*/
function testUserBlockUnpublish() {
config_set('system.core', 'user_cancel_method', 'user_cancel_block_unpublish');
// Create a user.
$account = $this->backdropCreateUser(array('cancel account'));
$this->backdropLogin($account);
// Load real user object.
$account = user_load($account->uid, TRUE);
// Create a node with two revisions.
$node = $this->backdropCreateNode(array('uid' => $account->uid));
$settings = get_object_vars($node);
$settings['revision'] = 1;
$node = $this->backdropCreateNode($settings);
// Attempt to cancel account.
$this->backdropGet('user/' . $account->uid . '/edit');
$this->backdropPost(NULL, NULL, t('Cancel account'));
$this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
$this->assertText(t('Your account will be blocked and you will no longer be able to log in. All of your content will be hidden from everyone but administrators.'), 'Informs that all content will be unpublished.');
// Confirm account cancellation.
$timestamp = time();
$this->backdropPost(NULL, NULL, t('Cancel account'));
$this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
// Confirm account cancellation request.
$this->backdropGet("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid));
$account = user_load($account->uid, TRUE);
$this->assertTrue($account->status == 0, 'User has been blocked.');
// Confirm user's content has been unpublished.
$test_node = node_load($node->nid, NULL, TRUE);
$this->assertTrue($test_node->status == 0, 'Node of the user has been unpublished.');
$test_node = node_load($node->nid, $node->vid, TRUE);
$this->assertTrue($test_node->status == 0, 'Node revision of the user has been unpublished.');
// Confirm that the confirmation message made it through to the end user.
$this->assertRaw(t('%name has been disabled.', array('%name' => $account->name)), 'Confirmation message displayed to user.');
}
/**
* Delete account and anonymize all content.
*/
function testUserAnonymize() {
config_set('system.core', 'user_cancel_method', 'user_cancel_reassign');
// Create a user.
$account = $this->backdropCreateUser(array('cancel account'));
$this->backdropLogin($account);
// Load real user object.
$account = user_load($account->uid, TRUE);
// Create a simple node.
$node = $this->backdropCreateNode(array('uid' => $account->uid));
// Create a node with two revisions, the initial one belonging to the
// cancelling user.
$revision_node = $this->backdropCreateNode(array('uid' => $account->uid));
$revision = $revision_node->vid;
$settings = get_object_vars($revision_node);
$settings['revision'] = 1;
$settings['uid'] = 1; // Set new/current revision to someone else.
$revision_node = $this->backdropCreateNode($settings);
// Attempt to cancel account.
$this->backdropGet('user/' . $account->uid . '/edit');
$this->backdropPost(NULL, NULL, t('Cancel account'));
$this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
$this->assertRaw(t('Your account will be removed and all account information deleted. All of your content will be assigned to the %anonymous-name user.', array('%anonymous-name' => config_get('system.core', 'anonymous'))), 'Informs that all content will be attributed to anonymous account.');
// Confirm account cancellation.
$timestamp = time();
$this->backdropPost(NULL, NULL, t('Cancel account'));
$this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
// Confirm account cancellation request.
$this->backdropGet("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid));
$this->assertFalse(user_load($account->uid, TRUE), 'User is not found in the database.');
// Confirm that user's content has been attributed to anonymous user.
$test_node = node_load($node->nid, NULL, TRUE);
$this->assertTrue(($test_node->uid == 0 && $test_node->status == 1), 'Node of the user has been attributed to anonymous user.');
$test_node = node_load($revision_node->nid, $revision, TRUE);
$this->assertTrue(($test_node->revision_uid == 0 && $test_node->status == 1), 'Node revision of the user has been attributed to anonymous user.');
$test_node = node_load($revision_node->nid, NULL, TRUE);
$this->assertTrue(($test_node->uid != 0 && $test_node->status == 1), "Current revision of the user's node was not attributed to anonymous user.");
// Confirm that the confirmation message made it through to the end user.
$this->assertRaw(t('%name has been deleted.', array('%name' => $account->name)), 'Confirmation message displayed to user.');
}
/**
* Delete account and remove all content.
*/
function testUserDelete() {
config_set('system.core', 'user_cancel_method', 'user_cancel_delete');
// Create a user.
$account = $this->backdropCreateUser(array('cancel account', 'post comments', 'skip comment approval'));
$this->backdropLogin($account);
// Load real user object.
$account = user_load($account->uid, TRUE);
// Enable comment subjects on 'page' nodes
$node_type = node_type_get_type('page');
$node_type->settings['comment_title_options'] = COMMENT_TITLE_CUSTOM;
node_type_save($node_type);
// Create a simple node.
$node = $this->backdropCreateNode(array('uid' => $account->uid));
// Create comment.
$langcode = LANGUAGE_NONE;
$edit = array();
$edit['subject'] = $this->randomName(8);
$edit['comment_body[' . $langcode . '][0][value]'] = $this->randomName(16);
$this->backdropPost('comment/reply/' . $node->nid, $edit, t('Preview'));
$this->backdropPost(NULL, array(), t('Save'));
$this->assertText(t('Your comment has been posted.'));
$comments = comment_load_multiple(FALSE, array('subject' => $edit['subject']));
$comment = reset($comments);
$this->assertTrue($comment->cid, 'Comment found.');
// Create a node with two revisions, the initial one belonging to the
// cancelling user.
$revision_node = $this->backdropCreateNode(array('uid' => $account->uid));
$revision = $revision_node->vid;
$settings = get_object_vars($revision_node);
$settings['revision'] = 1;
$settings['uid'] = 1; // Set new/current revision to someone else.
$revision_node = $this->backdropCreateNode($settings);
// Attempt to cancel account.
$this->backdropGet('user/' . $account->uid . '/edit');
$this->backdropPost(NULL, NULL, t('Cancel account'));
$this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.');
$this->assertText(t('Your account will be removed and all account information deleted. All of your content will also be deleted.'), 'Informs that all content will be deleted.');
// Confirm account cancellation.
$timestamp = time();
$this->backdropPost(NULL, NULL, t('Cancel account'));
$this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
// Confirm account cancellation request.
$this->backdropGet("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid));
$this->assertFalse(user_load($account->uid, TRUE), 'User is not found in the database.');
// Confirm that user's content has been deleted.
$this->assertFalse(node_load($node->nid, NULL, TRUE), 'Node of the user has been deleted.');
$this->assertFalse(node_load($node->nid, $revision, TRUE), 'Node revision of the user has been deleted.');
$this->assertTrue(node_load($revision_node->nid, NULL, TRUE), "Current revision of the user's node was not deleted.");
$this->assertFalse(comment_load($comment->cid), 'Comment of the user has been deleted.');
// Confirm that the confirmation message made it through to the end user.
$this->assertRaw(t('%name has been deleted.', array('%name' => $account->name)), 'Confirmation message displayed to user.');
}
/**
* Create an administrative user and delete another user.
*/
function testUserCancelByAdmin() {
config_set('system.core', 'user_cancel_method', 'user_cancel_reassign');
// Create a regular user.
$account = $this->backdropCreateUser(array());
// Create administrative user.
$admin_user = $this->backdropCreateUser(array('administer users'));
$this->backdropLogin($admin_user);
// Delete regular user.
$this->backdropGet('user/' . $account->uid . '/edit');
$this->backdropPost(NULL, NULL, t('Cancel account'));
$this->assertRaw(t('Are you sure you want to cancel the account %name?', array('%name' => $account->name)), 'Confirmation form to cancel account displayed.');
$this->assertText(t('Select the method to cancel the account above.'), 'Allows to select account cancellation method.');
// Confirm deletion.
$this->backdropPost(NULL, NULL, t('Cancel account'));
$this->assertRaw(t('%name has been deleted.', array('%name' => $account->name)), 'User deleted.');
$this->assertFalse(user_load($account->uid), 'User is not found in the database.');
}
/**
* Create an administrative user and mass-delete other users.
*/
function testMassUserCancelByAdmin() {
config_set('system.core', 'user_cancel_method', 'user_cancel_reassign');
// Enable account cancellation notification.
config_set('system.core', 'user_mail_status_canceled_notify', TRUE);
// Create administrative user.
$admin_user = $this->backdropCreateUser(array('administer users'));
$this->backdropLogin($admin_user);
// Create some users.
$users = array();
for ($i = 0; $i < 3; $i++) {
$account = $this->backdropCreateUser(array());
$users[$account->uid] = $account;
}
// Cancel user accounts, including own one.
$edit = array();
$edit['action'] = 'user_cancel_user_action';
$edit['bulk_form[0]'] = TRUE; // Try to cancel uid 1.
$edit['bulk_form[1]'] = TRUE; // Cancel the created admin user.
$position = 1;
foreach ($users as $account) {
$position++;