forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.php
More file actions
2054 lines (1689 loc) · 58.3 KB
/
user.php
File metadata and controls
2054 lines (1689 loc) · 58.3 KB
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
/**
* Test functions in wp-includes/user.php
*
* @group user
*/
class Tests_User extends WP_UnitTestCase {
protected static $admin_id;
protected static $editor_id;
protected static $author_id;
protected static $contrib_id;
protected static $sub_id;
protected static $user_ids = array();
protected static $_author;
protected $author;
protected $user_data;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$contrib_id = $factory->user->create(
array(
'user_login' => 'user1',
'user_nicename' => 'userone',
'user_pass' => 'password',
'first_name' => 'John',
'last_name' => 'Doe',
'display_name' => 'John Doe',
'user_email' => 'blackburn@battlefield3.com',
'user_url' => 'http://tacos.com',
'role' => 'contributor',
'nickname' => 'Johnny',
'description' => 'I am a WordPress user that cares about privacy.',
)
);
self::$user_ids[] = self::$contrib_id;
self::$author_id = $factory->user->create(
array(
'user_login' => 'author_login',
'user_email' => 'author@email.com',
'role' => 'author',
)
);
self::$user_ids[] = self::$author_id;
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
self::$user_ids[] = self::$admin_id;
self::$editor_id = $factory->user->create(
array(
'user_email' => 'test@test.com',
'role' => 'editor',
)
);
self::$user_ids[] = self::$editor_id;
self::$sub_id = $factory->user->create( array( 'role' => 'subscriber' ) );
self::$user_ids[] = self::$sub_id;
self::$_author = get_user_by( 'ID', self::$author_id );
}
public function set_up() {
parent::set_up();
$this->author = clone self::$_author;
}
public function test_get_users_of_blog() {
// Add one of each user role.
$nusers = array(
self::$contrib_id,
self::$author_id,
self::$admin_id,
self::$editor_id,
self::$sub_id,
);
$user_list = get_users();
// Find the role of each user as returned by get_users_of_blog().
$found = array();
foreach ( $user_list as $user ) {
// Only include the users we just created - there might be some others that existed previously.
if ( in_array( $user->ID, $nusers, true ) ) {
$found[] = $user->ID;
}
}
// Make sure every user we created was returned.
$this->assertSameSets( $nusers, $found );
}
// Simple get/set tests for user_option functions.
public function test_user_option() {
$key = rand_str();
$val = rand_str();
// Get an option that doesn't exist.
$this->assertFalse( get_user_option( $key, self::$author_id ) );
// Set and get.
update_user_option( self::$author_id, $key, $val );
$this->assertSame( $val, get_user_option( $key, self::$author_id ) );
// Change and get again.
$val2 = rand_str();
update_user_option( self::$author_id, $key, $val2 );
$this->assertSame( $val2, get_user_option( $key, self::$author_id ) );
}
/**
* Simple tests for usermeta functions.
*/
public function test_usermeta() {
$key = 'key';
$val = 'value1';
// Get a meta key that doesn't exist.
$this->assertSame( '', get_user_meta( self::$author_id, $key, true ) );
// Set and get.
update_user_meta( self::$author_id, $key, $val );
$this->assertSame( $val, get_user_meta( self::$author_id, $key, true ) );
// Change and get again.
$val2 = 'value2';
update_user_meta( self::$author_id, $key, $val2 );
$this->assertSame( $val2, get_user_meta( self::$author_id, $key, true ) );
// Delete and get.
delete_user_meta( self::$author_id, $key );
$this->assertSame( '', get_user_meta( self::$author_id, $key, true ) );
// Delete by key AND value.
update_user_meta( self::$author_id, $key, $val );
// Incorrect key: key still exists.
delete_user_meta( self::$author_id, $key, rand_str() );
$this->assertSame( $val, get_user_meta( self::$author_id, $key, true ) );
// Correct key: deleted.
delete_user_meta( self::$author_id, $key, $val );
$this->assertSame( '', get_user_meta( self::$author_id, $key, true ) );
}
/**
* Test usermeta functions in array mode.
*/
public function test_usermeta_array() {
// Some values to set.
$vals = array(
rand_str() => 'val-' . rand_str(),
rand_str() => 'val-' . rand_str(),
rand_str() => 'val-' . rand_str(),
);
// There is already some stuff in the array.
$this->assertIsArray( get_user_meta( self::$author_id ) );
foreach ( $vals as $k => $v ) {
update_user_meta( self::$author_id, $k, $v );
}
// Get the complete usermeta array.
$out = get_user_meta( self::$author_id );
// For reasons unclear, the resulting array is indexed numerically; meta keys are not included anywhere.
// So we'll just check to make sure our values are included somewhere.
foreach ( $vals as $k => $v ) {
$this->assertArrayHasKey( $k, $out );
$this->assertSame( $v, $out[ $k ][0] );
}
// Delete one key and check again.
$keys = array_keys( $vals );
$key_to_delete = array_pop( $keys );
delete_user_meta( self::$author_id, $key_to_delete );
$out = get_user_meta( self::$author_id );
// Make sure that key is excluded from the results.
foreach ( $vals as $k => $v ) {
if ( $k === $key_to_delete ) {
$this->assertArrayNotHasKey( $k, $out );
} else {
$this->assertArrayHasKey( $k, $out );
$this->assertSame( $v, $out[ $k ][0] );
}
}
}
/**
* Test property magic functions for property get/set/isset.
*/
public function test_user_properties() {
$user = new WP_User( self::$author_id );
foreach ( $user->data as $key => $data ) {
$this->assertEquals( $data, $user->$key );
}
$this->assertTrue( isset( $user->$key ) );
$this->assertFalse( isset( $user->fooooooooo ) );
$user->$key = 'foo';
$this->assertSame( 'foo', $user->$key );
$this->assertSame( 'foo', $user->data->$key ); // This will fail with WP < 3.3.
foreach ( get_object_vars( $user ) as $key => $value ) {
$this->assertSame( $value, $user->$key );
}
}
/**
* @ticket 53235
*/
public function test_numeric_properties_should_be_cast_to_ints() {
$user = new WP_User( self::$author_id );
$contexts = array( 'raw', 'edit', 'db', 'display', 'attribute', 'js' );
foreach ( $contexts as $context ) {
$user->filter = $context;
$user->init( $user->data );
$this->assertIsInt( $user->ID );
}
}
/**
* Test the magic __unset() method.
*
* @ticket 20043
*/
public function test_user_unset() {
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$user = new WP_User( self::$author_id );
// Test custom fields.
$user->customField = 123;
$this->assertSame( $user->customField, 123 );
unset( $user->customField );
$this->assertFalse( isset( $user->customField ) );
return $user;
// phpcs:enable
}
/**
* Test 'id' (lowercase).
*
* @depends test_user_unset
* @expectedDeprecated WP_User->id
* @ticket 20043
*/
public function test_user_unset_lowercase_id( $user ) {
$id = $user->id;
unset( $user->id );
$this->assertSame( $id, $user->id );
return $user;
}
/**
* Test 'ID'.
*
* @depends test_user_unset_lowercase_id
* @ticket 20043
*/
public function test_user_unset_uppercase_id( $user ) {
$this->assertNotEmpty( $user->ID );
unset( $user->ID );
$this->assertNotEmpty( $user->ID );
}
/**
* Test meta property magic functions for property get/set/isset.
*/
public function test_user_meta_properties() {
$user = new WP_User( self::$author_id );
update_user_option( self::$author_id, 'foo', 'foo', true );
$this->assertTrue( isset( $user->foo ) );
$this->assertSame( 'foo', $user->foo );
}
/**
* @expectedDeprecated WP_User->id
*/
public function test_id_property_back_compat() {
$user = new WP_User( self::$author_id );
$this->assertTrue( isset( $user->id ) );
$this->assertSame( $user->ID, $user->id );
$user->id = 1234;
$this->assertSame( $user->ID, $user->id );
}
/**
* @ticket 19265
*/
public function test_user_level_property_back_compat() {
$roles = array(
self::$admin_id => 10,
self::$editor_id => 7,
self::$author_id => 2,
self::$contrib_id => 1,
self::$sub_id => 0,
);
foreach ( $roles as $user_id => $level ) {
$user = new WP_User( $user_id );
$this->assertTrue( isset( $user->user_level ) );
$this->assertEquals( $level, $user->user_level );
}
}
public function test_construction() {
$user = new WP_User( self::$author_id );
$this->assertInstanceOf( 'WP_User', $user );
$this->assertSame( self::$author_id, $user->ID );
$user2 = new WP_User( 0, $user->user_login );
$this->assertInstanceOf( 'WP_User', $user2 );
$this->assertSame( self::$author_id, $user2->ID );
$this->assertSame( $user->user_login, $user2->user_login );
$user3 = new WP_User();
$this->assertInstanceOf( 'WP_User', $user3 );
$this->assertSame( 0, $user3->ID );
$this->assertFalse( isset( $user3->user_login ) );
$user3->init( $user->data );
$this->assertSame( self::$author_id, $user3->ID );
$user4 = new WP_User( $user->user_login );
$this->assertInstanceOf( 'WP_User', $user4 );
$this->assertSame( self::$author_id, $user4->ID );
$this->assertSame( $user->user_login, $user4->user_login );
$user5 = new WP_User( null, $user->user_login );
$this->assertInstanceOf( 'WP_User', $user5 );
$this->assertSame( self::$author_id, $user5->ID );
$this->assertSame( $user->user_login, $user5->user_login );
$user6 = new WP_User( $user );
$this->assertInstanceOf( 'WP_User', $user6 );
$this->assertSame( self::$author_id, $user6->ID );
$this->assertSame( $user->user_login, $user6->user_login );
$user7 = new WP_User( $user->data );
$this->assertInstanceOf( 'WP_User', $user7 );
$this->assertSame( self::$author_id, $user7->ID );
$this->assertSame( $user->user_login, $user7->user_login );
}
public function test_get() {
$user = new WP_User( self::$author_id );
$this->assertSame( 'author_login', $user->get( 'user_login' ) );
$this->assertSame( 'author@email.com', $user->get( 'user_email' ) );
$this->assertEquals( 0, $user->get( 'use_ssl' ) );
$this->assertSame( '', $user->get( 'field_that_does_not_exist' ) );
update_user_meta( self::$author_id, 'dashed-key', 'abcdefg' );
$this->assertSame( 'abcdefg', $user->get( 'dashed-key' ) );
}
public function test_has_prop() {
$user = new WP_User( self::$author_id );
$this->assertTrue( $user->has_prop( 'user_email' ) );
$this->assertTrue( $user->has_prop( 'use_ssl' ) );
$this->assertFalse( $user->has_prop( 'field_that_does_not_exist' ) );
update_user_meta( self::$author_id, 'dashed-key', 'abcdefg' );
$this->assertTrue( $user->has_prop( 'dashed-key' ) );
}
public function test_update_user() {
$user = new WP_User( self::$author_id );
update_user_meta( self::$author_id, 'description', 'about me' );
$this->assertSame( 'about me', $user->get( 'description' ) );
$user_data = array(
'ID' => self::$author_id,
'display_name' => 'test user',
);
wp_update_user( $user_data );
$user = new WP_User( self::$author_id );
$this->assertSame( 'test user', $user->get( 'display_name' ) );
// Make sure there is no collateral damage to fields not in $user_data.
$this->assertSame( 'about me', $user->get( 'description' ) );
// Pass as stdClass.
$user_data = array(
'ID' => self::$author_id,
'display_name' => 'a test user',
);
wp_update_user( (object) $user_data );
$user = new WP_User( self::$author_id );
$this->assertSame( 'a test user', $user->get( 'display_name' ) );
$user->display_name = 'some test user';
wp_update_user( $user );
$this->assertSame( 'some test user', $user->get( 'display_name' ) );
// Test update of fields in _get_additional_user_keys().
$user_data = array(
'ID' => self::$author_id,
'use_ssl' => 1,
'show_admin_bar_front' => 1,
'rich_editing' => 1,
'syntax_highlighting' => 1,
'first_name' => 'first',
'last_name' => 'last',
'nickname' => 'nick',
'comment_shortcuts' => 'true',
'admin_color' => 'classic',
'description' => 'describe',
);
wp_update_user( $user_data );
$user = new WP_User( self::$author_id );
foreach ( $user_data as $key => $value ) {
$this->assertEquals( $value, $user->get( $key ), $key );
}
}
/**
* @ticket 19595
*/
public function test_global_userdata() {
global $userdata, $wpdb;
wp_set_current_user( self::$sub_id );
$this->assertNotEmpty( $userdata );
$this->assertInstanceOf( 'WP_User', $userdata );
$this->assertSame( $userdata->ID, self::$sub_id );
$prefix = $wpdb->get_blog_prefix();
$cap_key = $prefix . 'capabilities';
$this->assertTrue( isset( $userdata->$cap_key ) );
}
/**
* @ticket 19769
*/
public function test_global_userdata_is_null_when_logged_out() {
global $userdata;
wp_set_current_user( 0 );
$this->assertNull( $userdata );
}
public function test_exists() {
$user = new WP_User( self::$author_id );
$this->assertTrue( $user->exists() );
$user = new WP_User( 123456789 );
$this->assertFalse( $user->exists() );
$user = new WP_User( 0 );
$this->assertFalse( $user->exists() );
}
public function test_global_authordata() {
global $authordata, $id;
$old_post_id = $id;
$user = new WP_User( self::$author_id );
$post = array(
'post_author' => self::$author_id,
'post_status' => 'publish',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_type' => 'post',
);
// Insert a post and make sure the ID is OK.
$post_id = wp_insert_post( $post );
$this->assertIsNumeric( $post_id );
setup_postdata( get_post( $post_id ) );
$this->assertNotEmpty( $authordata );
$this->assertInstanceOf( 'WP_User', $authordata );
$this->assertSame( $authordata->ID, self::$author_id );
if ( $old_post_id ) {
setup_postdata( get_post( $old_post_id ) );
}
}
/**
* @ticket 13317
*/
public function test_get_userdata() {
$this->assertFalse( get_userdata( 0 ) );
$this->assertFalse( get_userdata( '0' ) );
$this->assertFalse( get_userdata( 'string' ) );
$this->assertFalse( get_userdata( array( 'array' ) ) );
}
/**
* @ticket 23480
*/
public function test_user_get_data_by_id() {
$user = WP_User::get_data_by( 'id', self::$author_id );
$this->assertInstanceOf( 'stdClass', $user );
$this->assertEquals( self::$author_id, $user->ID );
// @ticket 23480
$user1 = WP_User::get_data_by( 'id', -1 );
$this->assertFalse( $user1 );
$user2 = WP_User::get_data_by( 'id', 0 );
$this->assertFalse( $user2 );
$user3 = WP_User::get_data_by( 'id', null );
$this->assertFalse( $user3 );
$user4 = WP_User::get_data_by( 'id', '' );
$this->assertFalse( $user4 );
$user5 = WP_User::get_data_by( 'id', false );
$this->assertFalse( $user5 );
$user6 = WP_User::get_data_by( 'id', $user->user_nicename );
$this->assertFalse( $user6 );
$user7 = WP_User::get_data_by( 'id', 99999 );
$this->assertFalse( $user7 );
}
/**
* @ticket 33869
*/
public function test_user_get_data_by_ID_should_alias_to_id() {
$user = WP_User::get_data_by( 'ID', self::$author_id );
$this->assertEquals( self::$author_id, $user->ID );
}
/**
* @ticket 21431
*/
public function test_count_many_users_posts() {
$user_id_b = self::factory()->user->create( array( 'role' => 'author' ) );
$post_id_a = self::factory()->post->create( array( 'post_author' => self::$author_id ) );
$post_id_b = self::factory()->post->create( array( 'post_author' => $user_id_b ) );
$post_id_c = self::factory()->post->create(
array(
'post_author' => $user_id_b,
'post_status' => 'private',
)
);
wp_set_current_user( self::$author_id );
$counts = count_many_users_posts( array( self::$author_id, $user_id_b ), 'post', false );
$this->assertEquals( 1, $counts[ self::$author_id ] );
$this->assertEquals( 1, $counts[ $user_id_b ] );
$counts = count_many_users_posts( array( self::$author_id, $user_id_b ), 'post', true );
$this->assertEquals( 1, $counts[ self::$author_id ] );
$this->assertEquals( 1, $counts[ $user_id_b ] );
wp_set_current_user( $user_id_b );
$counts = count_many_users_posts( array( self::$author_id, $user_id_b ), 'post', false );
$this->assertEquals( 1, $counts[ self::$author_id ] );
$this->assertEquals( 2, $counts[ $user_id_b ] );
$counts = count_many_users_posts( array( self::$author_id, $user_id_b ), 'post', true );
$this->assertEquals( 1, $counts[ self::$author_id ] );
$this->assertEquals( 1, $counts[ $user_id_b ] );
}
/**
* @ticket 22858
*/
public function test_wp_update_user_on_nonexistent_users() {
$user_id = 1;
// Find me a non-existent user ID.
while ( get_userdata( $user_id ) ) {
++$user_id;
}
// If this test fails, it will error out for calling the to_array() method on a non-object.
$this->assertInstanceOf( 'WP_Error', wp_update_user( array( 'ID' => $user_id ) ) );
}
/**
* @ticket 28435
*/
public function test_wp_update_user_should_not_change_password_when_passed_WP_User_instance() {
$testuserid = 1;
$user = get_userdata( $testuserid );
$pwd_before = $user->user_pass;
wp_update_user( $user );
// Reload the data.
$pwd_after = get_userdata( $testuserid )->user_pass;
$this->assertSame( $pwd_before, $pwd_after );
}
/**
* @ticket 45747
* @group ms-excluded
*/
public function test_wp_update_user_should_not_mark_user_as_spam_on_single_site() {
$u = wp_update_user(
array(
'ID' => self::$contrib_id,
'spam' => '0',
)
);
$this->assertNotWPError( $u );
$u = wp_update_user(
array(
'ID' => self::$contrib_id,
'spam' => '1',
)
);
$this->assertWPError( $u );
$this->assertSame( 'no_spam', $u->get_error_code() );
}
/**
* @ticket 28315
*/
public function test_user_meta_error() {
$id1 = wp_insert_user(
array(
'user_login' => rand_str(),
'user_pass' => 'password',
'user_email' => 'taco@burrito.com',
)
);
$this->assertSame( $id1, email_exists( 'taco@burrito.com' ) );
$id2 = wp_insert_user(
array(
'user_login' => rand_str(),
'user_pass' => 'password',
'user_email' => 'taco@burrito.com',
)
);
if ( ! defined( 'WP_IMPORTING' ) ) {
$this->assertWPError( $id2 );
}
update_user_meta( $id2, 'key', 'value' );
$metas = array_keys( get_user_meta( 1 ) );
$this->assertNotContains( 'key', $metas );
}
/**
* @ticket 30647
*/
public function test_user_update_email_error() {
$id1 = wp_insert_user(
array(
'user_login' => 'blackburn',
'user_pass' => 'password',
'user_email' => 'blackburn@battlefield4.com',
)
);
$this->assertSame( $id1, email_exists( 'blackburn@battlefield4.com' ) );
$id2 = wp_insert_user(
array(
'user_login' => 'miller',
'user_pass' => 'password',
'user_email' => 'miller@battlefield4.com',
)
);
$this->assertSame( $id2, email_exists( 'miller@battlefield4.com' ) );
if ( ! is_wp_error( $id2 ) ) {
wp_update_user(
array(
'ID' => $id2,
'user_email' => 'david@battlefield4.com',
)
);
$this->assertSame( $id2, email_exists( 'david@battlefield4.com' ) );
$return = wp_update_user(
array(
'ID' => $id2,
'user_email' => 'blackburn@battlefield4.com',
)
);
if ( ! defined( 'WP_IMPORTING' ) ) {
$this->assertWPError( $return );
}
}
}
/**
* @ticket 27317
* @dataProvider data_illegal_user_logins
*/
public function test_illegal_user_logins_single( $user_login ) {
$user_data = array(
'user_login' => $user_login,
'user_email' => 'testuser@example.com',
'user_pass' => wp_generate_password(),
);
add_filter( 'illegal_user_logins', array( $this, 'illegal_user_logins' ) );
$response = wp_insert_user( $user_data );
$this->assertInstanceOf( 'WP_Error', $response );
$this->assertSame( 'invalid_username', $response->get_error_code() );
remove_filter( 'illegal_user_logins', array( $this, 'illegal_user_logins' ) );
$user_id = wp_insert_user( $user_data );
$user = get_user_by( 'id', $user_id );
$this->assertInstanceOf( 'WP_User', $user );
}
/**
* @ticket 27317
* @dataProvider data_illegal_user_logins
*/
public function test_illegal_user_logins_single_wp_create_user( $user_login ) {
$user_email = 'testuser-' . $user_login . '@example.com';
add_filter( 'illegal_user_logins', array( $this, 'illegal_user_logins' ) );
$response = register_new_user( $user_login, $user_email );
$this->assertInstanceOf( 'WP_Error', $response );
$this->assertSame( 'invalid_username', $response->get_error_code() );
remove_filter( 'illegal_user_logins', array( $this, 'illegal_user_logins' ) );
$response = register_new_user( $user_login, $user_email );
$user = get_user_by( 'id', $response );
$this->assertInstanceOf( 'WP_User', $user );
}
/**
* @ticket 27317
* @group ms-required
*/
public function test_illegal_user_logins_multisite() {
$user_data = array(
'user_login' => 'testuser',
'user_email' => 'testuser@example.com',
);
add_filter( 'illegal_user_logins', array( $this, 'illegal_user_logins' ) );
$response = wpmu_validate_user_signup( $user_data['user_login'], $user_data['user_email'] );
$this->assertInstanceOf( 'WP_Error', $response['errors'] );
$this->assertSame( 'user_name', $response['errors']->get_error_code() );
remove_filter( 'illegal_user_logins', array( $this, 'illegal_user_logins' ) );
$response = wpmu_validate_user_signup( $user_data['user_login'], $user_data['user_email'] );
$this->assertInstanceOf( 'WP_Error', $response['errors'] );
$this->assertCount( 0, $response['errors']->get_error_codes() );
}
public function data_illegal_user_logins() {
$data = array(
array( 'testuser' ),
);
// Multisite doesn't allow mixed case logins ever.
if ( ! is_multisite() ) {
$data[] = array( 'TestUser' );
}
return $data;
}
public function illegal_user_logins() {
return array( 'testuser' );
}
/**
* @ticket 24618
*/
public function test_validate_username_string() {
$this->assertTrue( validate_username( 'johndoe' ) );
$this->assertTrue( validate_username( 'test@test.com' ) );
}
/**
* @ticket 24618
*/
public function test_validate_username_contains_uppercase_letters() {
if ( is_multisite() ) {
$this->assertFalse( validate_username( 'JohnDoe' ) );
} else {
$this->assertTrue( validate_username( 'JohnDoe' ) );
}
}
/**
* @ticket 24618
*/
public function test_validate_username_empty() {
$this->assertFalse( validate_username( '' ) );
}
/**
* @ticket 24618
*/
public function test_validate_username_invalid() {
$this->assertFalse( validate_username( '@#&99sd' ) );
}
/**
* @ticket 29880
*/
public function test_wp_insert_user_should_not_wipe_existing_password() {
$user_details = array(
'user_login' => rand_str(),
'user_pass' => 'password',
'user_email' => rand_str() . '@example.com',
);
$user_id = wp_insert_user( $user_details );
$this->assertSame( $user_id, email_exists( $user_details['user_email'] ) );
// Check that providing an empty password doesn't remove a user's password.
$user_details['ID'] = $user_id;
$user_details['user_pass'] = '';
$user_id = wp_insert_user( $user_details );
$user = WP_User::get_data_by( 'id', $user_id );
$this->assertNotEmpty( $user->user_pass );
}
/**
* @ticket 29696
*/
public function test_wp_insert_user_should_sanitize_user_nicename_parameter() {
$user = $this->author;
$userdata = $user->to_array();
$userdata['user_nicename'] = str_replace( '-', '.', $user->user_nicename );
wp_insert_user( $userdata );
$updated_user = new WP_User( $user->ID );
$this->assertSame( $user->user_nicename, $updated_user->user_nicename );
}
/**
* @ticket 33793
*/
public function test_wp_insert_user_should_accept_user_login_with_60_characters() {
$user_login = str_repeat( 'a', 60 );
$u = wp_insert_user(
array(
'user_login' => $user_login,
'user_email' => $user_login . '@example.com',
'user_pass' => 'password',
'user_nicename' => 'something-short',
)
);
$this->assertIsInt( $u );
$this->assertGreaterThan( 0, $u );
$user = new WP_User( $u );
$this->assertSame( $user_login, $user->user_login );
}
/**
* @ticket 33793
*/
public function test_wp_insert_user_should_reject_user_login_over_60_characters() {
$user_login = str_repeat( 'a', 61 );
$u = wp_insert_user(
array(
'user_login' => $user_login,
'user_email' => $user_login . '@example.com',
'user_pass' => 'password',
'user_nicename' => 'something-short',
)
);
$this->assertWPError( $u );
$this->assertSame( 'user_login_too_long', $u->get_error_code() );
}
/**
* @ticket 33793
*/
public function test_wp_insert_user_should_reject_user_nicename_over_50_characters() {
$user_nicename = str_repeat( 'a', 51 );
$u = wp_insert_user(
array(
'user_login' => 'mynicenamehas50chars',
'user_email' => $user_nicename . '@example.com',
'user_pass' => 'password',
'user_nicename' => $user_nicename,
)
);
$this->assertWPError( $u );
$this->assertSame( 'user_nicename_too_long', $u->get_error_code() );
}
/**
* @ticket 33793
*/
public function test_wp_insert_user_should_not_generate_user_nicename_longer_than_50_chars() {
$user_login = str_repeat( 'a', 55 );
$u = wp_insert_user(
array(
'user_login' => $user_login,
'user_email' => $user_login . '@example.com',
'user_pass' => 'password',
)
);
$this->assertNotEmpty( $u );
$user = new WP_User( $u );
$expected = str_repeat( 'a', 50 );
$this->assertSame( $expected, $user->user_nicename );
}
/**
* @ticket 33793
*/
public function test_wp_insert_user_should_not_truncate_to_a_duplicate_user_nicename() {
$u1 = self::factory()->user->create(
array(
'user_nicename' => str_repeat( 'a', 50 ),
)
);
$user1 = new WP_User( $u1 );
$expected = str_repeat( 'a', 50 );
$this->assertSame( $expected, $user1->user_nicename );
$user_login = str_repeat( 'a', 55 );
$u = wp_insert_user(
array(
'user_login' => $user_login,
'user_email' => $user_login . '@example.com',
'user_pass' => 'password',
)
);
$this->assertNotEmpty( $u );
$user2 = new WP_User( $u );
$expected = str_repeat( 'a', 48 ) . '-2';
$this->assertSame( $expected, $user2->user_nicename );
}
/**
* @ticket 33793
*/
public function test_wp_insert_user_should_not_truncate_to_a_duplicate_user_nicename_when_suffix_has_more_than_one_character() {
$user_ids = self::factory()->user->create_many(
4,
array(
'user_nicename' => str_repeat( 'a', 50 ),
)
);
foreach ( $user_ids as $i => $user_id ) {
$user = new WP_User( $user_id );
if ( 0 === $i ) {
$expected = str_repeat( 'a', 50 );
} else {
$expected = str_repeat( 'a', 48 ) . '-' . ( $i + 1 );
}
$this->assertSame( $expected, $user->user_nicename );
}
$user_login = str_repeat( 'a', 55 );
$u = wp_insert_user(
array(
'user_login' => $user_login,
'user_email' => $user_login . '@example.com',
'user_pass' => 'password',
)
);
$this->assertNotEmpty( $u );
$user = new WP_User( $u );
$expected = str_repeat( 'a', 48 ) . '-5';
$this->assertSame( $expected, $user->user_nicename );