Tests_User::wpSetUpBeforeClass()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
nc 1
nop 1
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
// test functions in wp-includes/user.php
4
/**
5
 * @group user
6
 */
7
class Tests_User extends WP_UnitTestCase
8
{
9
    protected static $admin_id;
10
    protected static $editor_id;
11
    protected static $author_id;
12
    protected static $contrib_id;
13
    protected static $sub_id;
14
15
    protected static $user_ids = array();
16
17
    protected static $_author;
18
    protected $author;
19
    protected $user_data;
20
21
    public static function wpSetUpBeforeClass( $factory ) 
0 ignored issues
show
Coding Style introduced by
The function name wpSetUpBeforeClass is in camel caps, but expected wp_set_up_before_class instead as per the coding standard.
Loading history...
22
    {
23
        self::$user_ids[] = self::$contrib_id = $factory->user->create(
24
            array(
25
            'user_login' => 'user1',
26
            'user_nicename' => 'userone',
27
            'user_pass'  => 'password',
28
            'first_name' => 'John',
29
            'last_name'  => 'Doe',
30
            'display_name' => 'John Doe',
31
            'user_email' => '[email protected]',
32
            'user_url' => 'http://tacos.com',
33
            'role' => 'contributor'
34
            ) 
35
        );
36
37
        self::$user_ids[] = self::$author_id = $factory->user->create(
38
            array(
39
            'user_login' => 'author_login',
40
            'user_email' => '[email protected]',
41
            'role' => 'author'
42
            ) 
43
        );
44
45
        self::$user_ids[] = self::$admin_id = $factory->user->create(array( 'role' => 'administrator' ));
46
        self::$user_ids[] = self::$editor_id = $factory->user->create(
47
            array(
48
            'role' => 'editor',
49
            'user_email' => '[email protected]',
50
            ) 
51
        );
52
        self::$user_ids[] = self::$sub_id = $factory->user->create(array( 'role' => 'subscriber' ));
53
54
        self::$_author = get_user_by('ID', self::$author_id);
55
    }
56
57
    function setUp() 
0 ignored issues
show
Coding Style introduced by
The function name setUp is in camel caps, but expected set_up instead as per the coding standard.
Loading history...
58
    {
59
        parent::setUp();
60
61
        $this->author = clone self::$_author;
62
    }
63
64
    function test_get_users_of_blog() 
65
    {
66
        // add one of each user role
67
        $nusers = array(
68
         self::$contrib_id,
69
         self::$author_id,
70
         self::$admin_id,
71
         self::$editor_id,
72
         self::$sub_id,
73
        );
74
75
        $user_list = get_users();
76
77
        // find the role of each user as returned by get_users_of_blog
78
        $found = array();
79
        foreach ( $user_list as $user ) {
80
               // only include the users we just created - there might be some others that existed previously
81
            if (in_array($user->ID, $nusers) ) {
82
                $found[] = $user->ID;
83
            }
84
        }
85
86
        // make sure every user we created was returned
87
        $this->assertEqualSets($nusers, $found);
88
    }
89
90
    // simple get/set tests for user_option functions
91
    function test_user_option() 
92
    {
93
        $key = rand_str();
94
        $val = rand_str();
95
96
        // get an option that doesn't exist
97
        $this->assertFalse(get_user_option($key, self::$author_id));
98
99
        // set and get
100
        update_user_option(self::$author_id, $key, $val);
101
        $this->assertEquals($val, get_user_option($key, self::$author_id));
102
103
        // change and get again
104
        $val2 = rand_str();
105
        update_user_option(self::$author_id, $key, $val2);
106
        $this->assertEquals($val2, get_user_option($key, self::$author_id));
107
    }
108
109
    // simple tests for usermeta functions
110
    function test_usermeta() 
111
    {
112
        $key = 'key';
113
        $val = 'value1';
114
115
        // get a meta key that doesn't exist
116
        $this->assertEquals('', get_user_meta(self::$author_id, $key, true));
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
117
118
        // set and get
119
        update_user_meta(self::$author_id, $key, $val);
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
120
        $this->assertEquals($val, get_user_meta(self::$author_id, $key, true));
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
121
122
        // change and get again
123
        $val2 = 'value2';
124
        update_user_meta(self::$author_id, $key, $val2);
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
125
        $this->assertEquals($val2, get_user_meta(self::$author_id, $key, true));
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
126
127
        // delete and get
128
        delete_user_meta(self::$author_id, $key);
0 ignored issues
show
introduced by
delete_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
129
        $this->assertEquals('', get_user_meta(self::$author_id, $key, true));
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
130
131
        // delete by key AND value
132
        update_user_meta(self::$author_id, $key, $val);
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
133
        // incorrect key: key still exists
134
        delete_user_meta(self::$author_id, $key, rand_str());
0 ignored issues
show
introduced by
delete_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
135
        $this->assertEquals($val, get_user_meta(self::$author_id, $key, true));
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
136
        // correct key: deleted
137
        delete_user_meta(self::$author_id, $key, $val);
0 ignored issues
show
introduced by
delete_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
138
        $this->assertEquals('', get_user_meta(self::$author_id, $key, true));
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
139
140
    }
141
142
    // test usermeta functions in array mode
143
    function test_usermeta_array() 
144
    {
145
        // some values to set
146
        $vals = array(
147
         rand_str() => 'val-'.rand_str(),
148
         rand_str() => 'val-'.rand_str(),
149
         rand_str() => 'val-'.rand_str(),
150
        );
151
152
        // there is already some stuff in the array
153
        $this->assertTrue(is_array(get_user_meta(self::$author_id)));
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
154
155
        foreach ( $vals as $k => $v ) {
156
               update_user_meta(self::$author_id, $k, $v);
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
157
        }
158
        // get the complete usermeta array
159
        $out = get_user_meta(self::$author_id);
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
160
161
        // for reasons unclear, the resulting array is indexed numerically; meta keys are not included anywhere.
162
        // so we'll just check to make sure our values are included somewhere.
163
        foreach ( $vals as $k => $v ) {
164
             $this->assertTrue(isset($out[$k]) && $out[$k][0] == $v);
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
165
        }
166
        // delete one key and check again
167
        $keys = array_keys($vals);
168
        $key_to_delete = array_pop($keys);
169
        delete_user_meta(self::$author_id, $key_to_delete);
0 ignored issues
show
introduced by
delete_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
170
        $out = get_user_meta(self::$author_id);
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
171
        // make sure that key is excluded from the results
172
        foreach ($vals as $k=>$v) {
0 ignored issues
show
introduced by
Expected 1 space before "=>"; 0 found
Loading history...
introduced by
Expected 1 space after "=>"; 0 found
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
173
            if ($k == $key_to_delete) {
0 ignored issues
show
introduced by
No space before closing parenthesis is prohibited
Loading history...
174
                $this->assertFalse(isset($out[$k]));
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
175
            } else {
176
                $this->assertTrue(isset($out[$k]) && $out[$k][0] == $v);
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
177
            }
178
        }
179
    }
180
181
    // Test property magic functions for property get/set/isset.
182
    function test_user_properties() 
183
    {
184
        $user = new WP_User(self::$author_id);
185
186
        foreach ( $user->data as $key => $data ) {
187
            $this->assertEquals($data, $user->$key);
188
        }
189
190
        $this->assertTrue(isset($user->$key));
0 ignored issues
show
Bug introduced by
The variable $key seems to be defined by a foreach iteration on line 186. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
191
        $this->assertFalse(isset($user->fooooooooo));
192
193
        $user->$key = 'foo';
194
        $this->assertEquals('foo', $user->$key);
195
        $this->assertEquals('foo', $user->data->$key);  // This will fail with WP < 3.3
196
197
        foreach ( (array) $user as $key => $value ) {
198
            $this->assertEquals($value, $user->$key);
199
        }
200
    }
201
202
    /**
203
     * Test the magic __unset method
204
     *
205
     * @ticket 20043
206
     */
207
    public function test_user_unset() 
208
    {
209
        $user = new WP_User(self::$author_id);
210
211
        // Test custom fields
212
        $user->customField = 123;
213
        $this->assertEquals($user->customField, 123);
214
        unset($user->customField);
215
        $this->assertFalse(isset($user->customField));
216
        return $user;
217
    }
218
219
    /**
220
     * @depends test_user_unset
221
     * @expectedDeprecated WP_User->id
222
     * @ticket 20043
223
     */
224
    function test_user_unset_lowercase_id( $user ) 
225
    {
226
        // Test 'id' (lowercase)
227
        $id = $user->id;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
228
        unset($user->id);
229
        $this->assertSame($id, $user->id);
230
        return $user;
231
    }
232
233
    /**
234
     * @depends test_user_unset_lowercase_id
235
     * @ticket 20043
236
     */
237
    function test_user_unset_uppercase_id( $user ) 
238
    {
239
        // Test 'ID'
240
        $this->assertNotEmpty($user->ID);
241
        unset($user->ID);
242
        $this->assertNotEmpty($user->ID);
243
    }
244
245
    // Test meta property magic functions for property get/set/isset.
246
    function test_user_meta_properties() 
247
    {
248
        $user = new WP_User(self::$author_id);
249
250
        update_user_option(self::$author_id, 'foo', 'foo', true);
251
252
        $this->assertTrue(isset($user->foo));
253
254
        $this->assertEquals('foo', $user->foo);
255
    }
256
257
    /**
258
     * @expectedDeprecated WP_User->id
259
     */
260
    function test_id_property_back_compat() 
261
    {
262
        $user = new WP_User(self::$author_id);
263
264
        $this->assertTrue(isset($user->id));
265
        $this->assertEquals($user->ID, $user->id);
266
        $user->id = 1234;
267
        $this->assertEquals($user->ID, $user->id);
268
    }
269
270
    /**
271
     * ticket 19265
272
     */
273
    function test_user_level_property_back_compat() 
274
    {
275
        $roles = array(
276
         self::$admin_id => 10,
277
         self::$editor_id => 7,
278
         self::$author_id => 2,
279
         self::$contrib_id => 1,
280
         self::$sub_id => 0,
281
        );
282
283
        foreach ( $roles as $user_id => $level ) {
284
               $user = new WP_User($user_id);
285
286
               $this->assertTrue(isset($user->user_level));
287
               $this->assertEquals($level, $user->user_level);
288
        }
289
    }
290
291
    function test_construction() 
292
    {
293
        $user = new WP_User(self::$author_id);
294
        $this->assertInstanceOf('WP_User', $user);
295
        $this->assertEquals(self::$author_id, $user->ID);
296
297
        $user2 = new WP_User(0,  $user->user_login);
298
        $this->assertInstanceOf('WP_User', $user2);
299
        $this->assertEquals(self::$author_id, $user2->ID);
300
        $this->assertEquals($user->user_login, $user2->user_login);
301
302
        $user3 = new WP_User();
303
        $this->assertInstanceOf('WP_User', $user3);
304
        $this->assertEquals(0, $user3->ID);
305
        $this->assertFalse(isset($user3->user_login));
306
307
        $user3->init($user->data);
308
        $this->assertEquals(self::$author_id, $user3->ID);
309
310
        $user4 = new WP_User($user->user_login);
311
        $this->assertInstanceOf('WP_User', $user4);
312
        $this->assertEquals(self::$author_id, $user4->ID);
313
        $this->assertEquals($user->user_login, $user4->user_login);
314
315
        $user5 = new WP_User(null, $user->user_login);
316
        $this->assertInstanceOf('WP_User', $user5);
317
        $this->assertEquals(self::$author_id, $user5->ID);
318
        $this->assertEquals($user->user_login, $user5->user_login);
319
320
        $user6 = new WP_User($user);
321
        $this->assertInstanceOf('WP_User', $user6);
322
        $this->assertEquals(self::$author_id, $user6->ID);
323
        $this->assertEquals($user->user_login, $user6->user_login);
324
325
        $user7 = new WP_User($user->data);
326
        $this->assertInstanceOf('WP_User', $user7);
327
        $this->assertEquals(self::$author_id, $user7->ID);
328
        $this->assertEquals($user->user_login, $user7->user_login);
329
    }
330
331
    function test_get() 
332
    {
333
        $user = new WP_User(self::$author_id);
334
        $this->assertEquals('author_login', $user->get('user_login'));
335
        $this->assertEquals('[email protected]', $user->get('user_email'));
336
        $this->assertEquals(0, $user->get('use_ssl'));
337
        $this->assertEquals('', $user->get('field_that_does_not_exist'));
338
339
        update_user_meta(self::$author_id, 'dashed-key', 'abcdefg');
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
340
        $this->assertEquals('abcdefg', $user->get('dashed-key'));
341
    }
342
343
    function test_has_prop() 
344
    {
345
        $user = new WP_User(self::$author_id);
346
        $this->assertTrue($user->has_prop('user_email'));
347
        $this->assertTrue($user->has_prop('use_ssl'));
348
        $this->assertFalse($user->has_prop('field_that_does_not_exist'));
349
350
        update_user_meta(self::$author_id, 'dashed-key', 'abcdefg');
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
351
        $this->assertTrue($user->has_prop('dashed-key'));
352
    }
353
354
    function test_update_user() 
355
    {
356
        $user = new WP_User(self::$author_id);
357
358
        update_user_meta(self::$author_id, 'description', 'about me');
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
359
        $this->assertEquals('about me', $user->get('description'));
360
361
        $user_data = array( 'ID' => self::$author_id, 'display_name' => 'test user' );
362
        wp_update_user($user_data);
363
364
        $user = new WP_User(self::$author_id);
365
        $this->assertEquals('test user', $user->get('display_name'));
366
367
        // Make sure there is no collateral damage to fields not in $user_data
368
        $this->assertEquals('about me', $user->get('description'));
369
370
        // Pass as stdClass
371
        $user_data = array( 'ID' => self::$author_id, 'display_name' => 'a test user' );
372
        wp_update_user((object) $user_data);
0 ignored issues
show
introduced by
No space before opening casting parenthesis is prohibited
Loading history...
373
374
        $user = new WP_User(self::$author_id);
375
        $this->assertEquals('a test user', $user->get('display_name'));
376
377
        $user->display_name = 'some test user';
378
        wp_update_user($user);
379
380
        $this->assertEquals('some test user', $user->get('display_name'));
381
382
        // Test update of fields in _get_additional_user_keys()
383
        $user_data = array(
384
         'ID' => self::$author_id, 'use_ssl' => 1, 'show_admin_bar_front' => 1,
385
         'rich_editing' => 1, 'first_name' => 'first', 'last_name' => 'last',
386
         'nickname' => 'nick', 'comment_shortcuts' => 'true', 'admin_color' => 'classic',
387
         'description' => 'describe'
388
        );
389
        wp_update_user($user_data);
390
391
        $user = new WP_User(self::$author_id);
392
        foreach ( $user_data as $key => $value ) {
393
               $this->assertEquals($value, $user->get($key), $key);
394
        }
395
    }
396
397
    /**
398
     * ticket 19595
399
     */
400
    function test_global_userdata() 
401
    {
402
        global $userdata, $wpdb;
403
404
        wp_set_current_user(self::$sub_id);
405
406
        $this->assertNotEmpty($userdata);
407
        $this->assertInstanceOf('WP_User', $userdata);
408
        $this->assertEquals($userdata->ID, self::$sub_id);
409
        $prefix = $wpdb->get_blog_prefix();
410
        $cap_key = $prefix . 'capabilities';
411
        $this->assertTrue(isset($userdata->$cap_key));
412
    }
413
414
    /**
415
     * ticket 19769
416
     */
417
    function test_global_userdata_is_null_when_logged_out() 
418
    {
419
        global $userdata;
420
        wp_set_current_user(0);
421
        $this->assertNull($userdata);
422
    }
423
424
    function test_exists() 
425
    {
426
        $user = new WP_User(self::$author_id);
427
428
        $this->assertTrue($user->exists());
429
430
        $user = new WP_User(123456789);
431
432
        $this->assertFalse($user->exists());
433
434
        $user = new WP_User(0);
435
436
        $this->assertFalse($user->exists());
437
    }
438
439
    function test_global_authordata() 
440
    {
441
        global $authordata, $id;
442
443
        $old_post_id = $id;
444
445
        $user = new WP_User(self::$author_id);
446
447
        $post = array(
448
         'post_author' => self::$author_id,
449
         'post_status' => 'publish',
450
         'post_content' => rand_str(),
451
         'post_title' => rand_str(),
452
         'post_type' => 'post'
453
        );
454
455
        // insert a post and make sure the ID is ok
456
        $post_id = wp_insert_post($post);
457
        $this->assertTrue(is_numeric($post_id));
458
459
        setup_postdata(get_post($post_id));
460
461
        $this->assertNotEmpty($authordata);
462
        $this->assertInstanceOf('WP_User', $authordata);
463
        $this->assertEquals($authordata->ID, self::$author_id);
464
465
        if ($old_post_id ) {
466
               setup_postdata(get_post($old_post_id));
467
        }
468
    }
469
470
    /**
471
     * @ticket 13317
472
     */
473
    function test_get_userdata() 
474
    {
475
        $this->assertFalse(get_userdata(0));
476
        $this->assertFalse(get_userdata('0'));
477
        $this->assertFalse(get_userdata('string'));
478
        $this->assertFalse(get_userdata(array( 'array' )));
479
    }
480
481
    function test_user_get_data_by_id() 
482
    {
483
        $user = WP_User::get_data_by('id', self::$author_id);
484
        $this->assertInstanceOf('stdClass', $user);
485
        $this->assertEquals(self::$author_id, $user->ID);
486
487
        // @ticket 23480
488
        $user1 = WP_User::get_data_by('id', -1);
489
        $this->assertEquals(false, $user1);
490
491
        $user2 = WP_User::get_data_by('id', 0);
492
        $this->assertEquals(false, $user2);
493
494
        $user3 = WP_User::get_data_by('id', null);
495
        $this->assertEquals(false, $user3);
496
497
        $user4 = WP_User::get_data_by('id', '');
498
        $this->assertEquals(false, $user4);
499
500
        $user5 = WP_User::get_data_by('id', false);
501
        $this->assertEquals(false, $user5);
502
503
        $user6 = WP_User::get_data_by('id', $user->user_nicename);
504
        $this->assertEquals(false, $user6);
505
506
        $user7 = WP_User::get_data_by('id', 99999);
507
        $this->assertEquals(false, $user7);
508
    }
509
510
    /**
511
     * @ticket 33869
512
     */
513
    public function test_user_get_data_by_ID_should_alias_to_id() 
0 ignored issues
show
Coding Style introduced by
The function name test_user_get_data_by_ID_should_alias_to_id is in camel caps, but expected test_user_get_data_by_i_d_should_alias_to_id instead as per the coding standard.
Loading history...
514
    {
515
        $user = WP_User::get_data_by('ID', self::$author_id);
516
        $this->assertEquals(self::$author_id, $user->ID);
517
    }
518
519
    /**
520
     * @ticket 21431
521
     */
522
    function test_count_many_users_posts() 
523
    {
524
        $user_id_b = self::factory()->user->create(array( 'role' => 'author' ));
525
        $post_id_a = self::factory()->post->create(array( 'post_author' => self::$author_id ));
526
        $post_id_b = self::factory()->post->create(array( 'post_author' => $user_id_b ));
527
        $post_id_c = self::factory()->post->create(array( 'post_author' => $user_id_b, 'post_status' => 'private' ));
528
529
        wp_set_current_user(self::$author_id);
530
        $counts = count_many_users_posts(array( self::$author_id, $user_id_b ), 'post', false);
531
        $this->assertEquals(1, $counts[self::$author_id]);
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
532
        $this->assertEquals(1, $counts[$user_id_b]);
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
533
534
        $counts = count_many_users_posts(array( self::$author_id, $user_id_b ), 'post', true);
535
        $this->assertEquals(1, $counts[self::$author_id]);
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
536
        $this->assertEquals(1, $counts[$user_id_b]);
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
537
538
        wp_set_current_user($user_id_b);
539
        $counts = count_many_users_posts(array( self::$author_id, $user_id_b ), 'post', false);
540
        $this->assertEquals(1, $counts[self::$author_id]);
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
541
        $this->assertEquals(2, $counts[$user_id_b]);
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
542
543
        $counts = count_many_users_posts(array( self::$author_id, $user_id_b ), 'post', true);
544
        $this->assertEquals(1, $counts[self::$author_id]);
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
545
        $this->assertEquals(1, $counts[$user_id_b]);
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
546
    }
547
548
    /**
549
     * @ticket 22858
550
     */
551
    function test_wp_update_user_on_nonexistent_users() 
552
    {
553
        $user_id = 1;
554
        // Find me a non-existent user ID.
555
        while ( get_userdata($user_id) ) {
556
              ++$user_id;
557
        }
558
559
        // If this test fails, it will error out for calling the to_array() method on a non-object.
560
        $this->assertInstanceOf('WP_Error', wp_update_user(array( 'ID' => $user_id )));
561
    }
562
563
    /**
564
     * @ticket 28435
565
     */
566
    function test_wp_update_user_should_not_change_password_when_passed_WP_User_instance() 
0 ignored issues
show
Coding Style introduced by
The function name test_wp_update_user_shou...passed_WP_User_instance is in camel caps, but expected test_wp_update_user_shou...assed_w_p_user_instance instead as per the coding standard.
Loading history...
567
    {
568
        $testuserid = 1;
569
        $user = get_userdata($testuserid);
570
        $pwd_before = $user->user_pass;
571
        wp_update_user($user);
572
573
        // Reload the data
574
        $pwd_after = get_userdata($testuserid)->user_pass;
575
        $this->assertEquals($pwd_before, $pwd_after);
576
    }
577
578
    /**
579
     * @ticket 28315
580
     */
581
    function test_user_meta_error() 
582
    {
583
        $id1 = wp_insert_user(
584
            array(
585
            'user_login' => rand_str(),
586
            'user_pass' => 'password',
587
            'user_email' => '[email protected]',
588
            ) 
589
        );
590
        $this->assertEquals($id1, email_exists('[email protected]'));
591
592
        $id2 = wp_insert_user(
593
            array(
594
            'user_login' => rand_str(),
595
            'user_pass' => 'password',
596
            'user_email' => '[email protected]',
597
            ) 
598
        );
599
600
        if (! defined('WP_IMPORTING') ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
601
            $this->assertWPError($id2);
602
        }
603
604
        @update_user_meta($id2, 'key', 'value');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Coding Style introduced by
Silencing errors is discouraged
Loading history...
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
605
606
        $metas = array_keys(get_user_meta(1));
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
607
        $this->assertNotContains('key', $metas);
608
    }
609
610
    /**
611
     * @ticket 30647
612
     */
613
    function test_user_update_email_error() 
614
    {
615
        $id1 = wp_insert_user(
616
            array(
617
            'user_login' => 'blackburn',
618
            'user_pass'  => 'password',
619
            'user_email' => '[email protected]',
620
            ) 
621
        );
622
        $this->assertEquals($id1, email_exists('[email protected]'));
623
624
        $id2 = wp_insert_user(
625
            array(
626
            'user_login' => 'miller',
627
            'user_pass'  => 'password',
628
            'user_email' => '[email protected]',
629
            ) 
630
        );
631
        $this->assertEquals($id2, email_exists('[email protected]'));
632
633
        if (! is_wp_error($id2) ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
634
            wp_update_user(
635
                array(
636
                'ID'         => $id2,
637
                'user_email' => '[email protected]',
638
                ) 
639
            );
640
            $this->assertEquals($id2, email_exists('[email protected]'));
641
642
            $return = wp_update_user(
643
                array(
644
                'ID'         => $id2,
645
                'user_email' => '[email protected]',
646
                ) 
647
            );
648
649
            if (! defined('WP_IMPORTING') ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
650
                $this->assertWPError($return);
651
            }
652
        }
653
    }
654
655
    /**
656
     * @ticket 27317
657
     * @dataProvider _illegal_user_logins_data
658
     */
659
    function test_illegal_user_logins_single( $user_login ) 
660
    {
661
        $user_data = array(
662
         'user_login' => $user_login,
663
         'user_email' => '[email protected]',
664
         'user_pass'  => wp_generate_password(),
665
        );
666
667
        add_filter('illegal_user_logins', array( $this, '_illegal_user_logins' ));
668
669
        $response = wp_insert_user($user_data);
670
        $this->assertInstanceOf('WP_Error', $response);
671
        $this->assertEquals('invalid_username', $response->get_error_code());
672
673
        remove_filter('illegal_user_logins', array( $this, '_illegal_user_logins' ));
674
675
        $user_id = wp_insert_user($user_data);
676
        $user = get_user_by('id', $user_id);
677
        $this->assertInstanceOf('WP_User', $user);
678
    }
679
680
    /**
681
     * @ticket 27317
682
     * @dataProvider _illegal_user_logins_data
683
     */
684
    function test_illegal_user_logins_single_wp_create_user( $user_login ) 
685
    {
686
        $user_email = 'testuser-' . $user_login . '@example.com';
687
688
        add_filter('illegal_user_logins', array( $this, '_illegal_user_logins' ));
689
690
        $response = register_new_user($user_login, $user_email);
691
        $this->assertInstanceOf('WP_Error', $response);
692
        $this->assertEquals('invalid_username', $response->get_error_code());
693
694
        remove_filter('illegal_user_logins', array( $this, '_illegal_user_logins' ));
695
696
        $response = register_new_user($user_login, $user_email);
697
        $user = get_user_by('id', $response);
698
        $this->assertInstanceOf('WP_User', $user);
699
    }
700
701
    /**
702
     * @ticket 27317
703
     */
704
    function test_illegal_user_logins_multisite() 
705
    {
706
        if (! is_multisite() ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
707
            $this->markTestSkipped(__METHOD__ . ' requires multisite.');
708
        }
709
710
        $user_data = array(
711
         'user_login' => 'testuser',
712
         'user_email' => '[email protected]',
713
        );
714
715
        add_filter('illegal_user_logins', array( $this, '_illegal_user_logins' ));
716
717
        $response = wpmu_validate_user_signup($user_data['user_login'], $user_data['user_email']);
718
        $this->assertInstanceOf('WP_Error', $response['errors']);
719
        $this->assertEquals('user_name', $response['errors']->get_error_code());
720
721
        remove_filter('illegal_user_logins', array( $this, '_illegal_user_logins' ));
722
723
        $response = wpmu_validate_user_signup($user_data['user_login'], $user_data['user_email']);
724
        $this->assertInstanceOf('WP_Error', $response['errors']);
725
        $this->assertEquals(0, count($response['errors']->get_error_codes()));
726
    }
727
728
    function _illegal_user_logins_data() 
729
    {
730
        $data = array(
731
         array( 'testuser' )
732
        );
733
734
        // Multisite doesn't allow mixed case logins ever
735
        if (! is_multisite() ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
736
               $data[] = array( 'TestUser' );
737
        }
738
        return $data;
739
    }
740
741
    function _illegal_user_logins() 
742
    {
743
        return array( 'testuser' );
744
    }
745
746
    /**
747
     * @ticket 24618
748
     */
749
    public function test_validate_username_string() 
750
    {
751
        $this->assertTrue(validate_username('johndoe'));
752
        $this->assertTrue(validate_username('[email protected]'));
753
    }
754
755
    /**
756
     * @ticket 24618
757
     */
758
    public function test_validate_username_contains_uppercase_letters() 
759
    {
760
        if (is_multisite() ) {
761
            $this->assertFalse(validate_username('JohnDoe'));
762
        } else {
763
            $this->assertTrue(validate_username('JohnDoe'));
764
        }
765
    }
766
767
    /**
768
     * @ticket 24618
769
     */
770
    public function test_validate_username_empty() 
771
    {
772
        $this->assertFalse(validate_username(''));
773
    }
774
775
    /**
776
     * @ticket 24618
777
     */
778
    public function test_validate_username_invalid() 
779
    {
780
        $this->assertFalse(validate_username('@#&99sd'));
781
    }
782
783
    /**
784
     * @ticket 29880
785
     */
786
    public function test_wp_insert_user_should_not_wipe_existing_password() 
787
    {
788
        $user_details = array(
789
         'user_login' => rand_str(),
790
         'user_pass' => 'password',
791
         'user_email' => rand_str() . '@example.com',
792
        );
793
794
        $user_id = wp_insert_user($user_details);
795
        $this->assertEquals($user_id, email_exists($user_details['user_email']));
796
797
        // Check that providing an empty password doesn't remove a user's password.
798
        $user_details['ID'] = $user_id;
799
        $user_details['user_pass'] = '';
800
801
        $user_id = wp_insert_user($user_details);
802
        $user = WP_User::get_data_by('id', $user_id);
803
        $this->assertNotEmpty($user->user_pass);
804
    }
805
806
    /**
807
     * @ticket 29696
808
     */
809
    public function test_wp_insert_user_should_sanitize_user_nicename_parameter() 
810
    {
811
        $user = $this->author;
812
813
        $userdata = $user->to_array();
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
814
        $userdata['user_nicename'] = str_replace('-', '.', $user->user_nicename);
815
        wp_insert_user($userdata);
816
817
        $updated_user = new WP_User($user->ID);
818
819
        $this->assertSame($user->user_nicename, $updated_user->user_nicename);
820
    }
821
822
    /**
823
     * @ticket 33793
824
     */
825
    public function test_wp_insert_user_should_accept_user_login_with_60_characters() 
826
    {
827
        $user_login = str_repeat('a', 60);
828
        $u = wp_insert_user(
829
            array(
830
            'user_login' => $user_login,
831
            'user_email' => $user_login . '@example.com',
832
            'user_pass' => 'password',
833
            'user_nicename' => 'something-short',
834
            ) 
835
        );
836
837
        $this->assertInternalType('int', $u);
838
        $this->assertGreaterThan(0, $u);
839
840
        $user = new WP_User($u);
841
        $this->assertSame($user_login, $user->user_login);
842
    }
843
844
    /**
845
     * @ticket 33793
846
     */
847 View Code Duplication
    public function test_wp_insert_user_should_reject_user_login_over_60_characters() 
848
    {
849
        $user_login = str_repeat('a', 61);
850
        $u = wp_insert_user(
851
            array(
852
            'user_login' => $user_login,
853
            'user_email' => $user_login . '@example.com',
854
            'user_pass' => 'password',
855
            'user_nicename' => 'something-short',
856
            ) 
857
        );
858
859
        $this->assertWPError($u);
860
        $this->assertSame('user_login_too_long', $u->get_error_code());
861
    }
862
863
    /**
864
     * @ticket 33793
865
     */
866 View Code Duplication
    public function test_wp_insert_user_should_reject_user_nicename_over_50_characters() 
867
    {
868
        $user_nicename = str_repeat('a', 51);
869
        $u = wp_insert_user(
870
            array(
871
            'user_login' => 'mynicenamehas50chars',
872
            'user_email' => $user_nicename . '@example.com',
873
            'user_pass' => 'password',
874
            'user_nicename' => $user_nicename,
875
            ) 
876
        );
877
878
        $this->assertWPError($u);
879
        $this->assertSame('user_nicename_too_long', $u->get_error_code());
880
    }
881
882
    /**
883
     * @ticket 33793
884
     */
885
    public function test_wp_insert_user_should_not_generate_user_nicename_longer_than_50_chars() 
886
    {
887
        $user_login = str_repeat('a', 55);
888
        $u = wp_insert_user(
889
            array(
890
            'user_login' => $user_login,
891
            'user_email' => $user_login . '@example.com',
892
            'user_pass' => 'password',
893
            ) 
894
        );
895
896
        $this->assertNotEmpty($u);
897
        $user = new WP_User($u);
898
        $expected = str_repeat('a', 50);
899
        $this->assertSame($expected, $user->user_nicename);
900
    }
901
902
    /**
903
     * @ticket 33793
904
     */
905
    public function test_wp_insert_user_should_not_truncate_to_a_duplicate_user_nicename() 
906
    {
907
        $u1 = self::factory()->user->create(
908
            array(
909
            'user_nicename' => str_repeat('a', 50),
910
            ) 
911
        );
912
913
        $user1 = new WP_User($u1);
914
915
        $expected = str_repeat('a', 50);
916
        $this->assertSame($expected, $user1->user_nicename);
917
918
        $user_login = str_repeat('a', 55);
919
        $u = wp_insert_user(
920
            array(
921
            'user_login' => $user_login,
922
            'user_email' => $user_login . '@example.com',
923
            'user_pass' => 'password',
924
            ) 
925
        );
926
927
        $this->assertNotEmpty($u);
928
        $user2 = new WP_User($u);
929
        $expected = str_repeat('a', 48) . '-2';
930
        $this->assertSame($expected, $user2->user_nicename);
931
    }
932
933
    /**
934
     * @ticket 33793
935
     */
936
    public function test_wp_insert_user_should_not_truncate_to_a_duplicate_user_nicename_when_suffix_has_more_than_one_character() 
937
    {
938
        $user_ids = self::factory()->user->create_many(
939
            4, array(
940
            'user_nicename' => str_repeat('a', 50),
941
            ) 
942
        );
943
944
        foreach ( $user_ids as $i => $user_id ) {
945
            $user = new WP_User($user_id);
946
            if (0 === $i ) {
947
                $expected = str_repeat('a', 50);
948
            } else {
949
                $expected = str_repeat('a', 48) . '-' . ( $i + 1 );
950
            }
951
            $this->assertSame($expected, $user->user_nicename);
952
        }
953
954
        $user_login = str_repeat('a', 55);
955
        $u = wp_insert_user(
956
            array(
957
            'user_login' => $user_login,
958
            'user_email' => $user_login . '@example.com',
959
            'user_pass' => 'password',
960
            ) 
961
        );
962
963
        $this->assertNotEmpty($u);
964
        $user = new WP_User($u);
965
        $expected = str_repeat('a', 48) . '-5';
966
        $this->assertSame($expected, $user->user_nicename);
967
    }
968
969
    /**
970
     * @ticket 28004
971
     */
972
    public function test_wp_insert_user_with_invalid_user_id() 
973
    {
974
        global $wpdb;
975
        $max_user = $wpdb->get_var("SELECT MAX(ID) FROM $wpdb->users");
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
introduced by
Usage of users/usermeta tables is highly discouraged in VIP context, For storing user additional user metadata, you should look at User Attributes.
Loading history...
976
977
        $u = wp_insert_user(
978
            array(
979
            'ID' => $max_user + 1,
980
            'user_login' => 'whatever',
981
            'user_email' => '[email protected]',
982
            'user_pass' => 'password',
983
            ) 
984
        );
985
986
        $this->assertWPError($u);
987
    }
988
989
    /**
990
     * @ticket 35750
991
     */
992
    public function test_wp_update_user_should_delete_userslugs_cache() 
993
    {
994
        $u = self::factory()->user->create();
995
        $user = get_userdata($u);
996
997
        wp_update_user(
998
            array(
999
            'ID' => $u,
1000
            'user_nicename' => 'newusernicename',
1001
            ) 
1002
        );
1003
        $updated_user = get_userdata($u);
1004
1005
        $this->assertFalse(wp_cache_get($user->user_nicename, 'userslugs'));
1006
        $this->assertEquals($u, wp_cache_get($updated_user->user_nicename, 'userslugs'));
1007
    }
1008
1009
    function test_changing_email_invalidates_password_reset_key() 
1010
    {
1011
        global $wpdb;
1012
1013
        $user = $this->author;
1014
        $wpdb->update($wpdb->users, array( 'user_activation_key' => 'key' ), array( 'ID' => $user->ID ));
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of users/usermeta tables is highly discouraged in VIP context, For storing user additional user metadata, you should look at User Attributes.
Loading history...
1015
        clean_user_cache($user);
1016
1017
        $user = get_userdata($user->ID);
1018
        $this->assertEquals('key', $user->user_activation_key);
1019
1020
        // Check that changing something other than the email doesn't remove the key.
1021
        $userdata = array(
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
1022
         'ID'            => $user->ID,
1023
         'user_nicename' => 'wat',
1024
        );
1025
        wp_update_user($userdata);
1026
1027
        $user = get_userdata($user->ID);
1028
        $this->assertEquals('key', $user->user_activation_key);
1029
1030
        // Now check that changing the email does remove it.
1031
        $userdata = array(
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
1032
         'ID'            => $user->ID,
1033
         'user_nicename' => 'cat',
1034
         'user_email'    => '[email protected]',
1035
        );
1036
        wp_update_user($userdata);
1037
1038
        $user = get_userdata($user->ID);
1039
        $this->assertEmpty($user->user_activation_key);
1040
    }
1041
1042
    public function test_search_users_login() 
1043
    {
1044
        $users = get_users(array( 'search' => 'user1', 'fields' => 'ID' ));
1045
1046
        $this->assertTrue(in_array(self::$contrib_id, $users));
1047
    }
1048
1049
    public function test_search_users_url() 
1050
    {
1051
        $users = get_users(array( 'search' => '*tacos*', 'fields' => 'ID' ));
1052
1053
        $this->assertTrue(in_array(self::$contrib_id, $users));
1054
    }
1055
1056
    public function test_search_users_email() 
1057
    {
1058
        $users = get_users(array( 'search' => '*battle*', 'fields' => 'ID' ));
1059
1060
        $this->assertTrue(in_array(self::$contrib_id, $users));
1061
    }
1062
1063
    public function test_search_users_nicename() 
1064
    {
1065
        $users = get_users(array( 'search' => '*one*', 'fields' => 'ID' ));
1066
1067
        $this->assertTrue(in_array(self::$contrib_id, $users));
1068
    }
1069
1070
    public function test_search_users_display_name() 
1071
    {
1072
        $users = get_users(array( 'search' => '*Doe*', 'fields' => 'ID' ));
1073
1074
        $this->assertTrue(in_array(self::$contrib_id, $users));
1075
    }
1076
1077
    /**
1078
     * @ticket 32158
1079
     */
1080
    function test_email_case() 
1081
    {
1082
        // Alter the case of the email address (which stays the same).
1083
        $userdata = array(
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
1084
         'ID' => self::$editor_id,
1085
         'user_email' => '[email protected]',
1086
        );
1087
        $update = wp_update_user($userdata);
1088
1089
        $this->assertEquals(self::$editor_id, $update);
1090
    }
1091
1092
    /**
1093
     * @ticket 32158
1094
     */
1095
    function test_email_change() 
1096
    {
1097
        // Change the email address.
1098
        $userdata = array(
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
1099
         'ID' => self::$editor_id,
1100
         'user_email' => '[email protected]',
1101
        );
1102
        $update = wp_update_user($userdata);
1103
1104
        // Was this successful?
1105
        $this->assertEquals(self::$editor_id, $update);
1106
1107
        // Verify that the email address has been updated.
1108
        $user = get_userdata(self::$editor_id);
1109
        $this->assertEquals($user->user_email, '[email protected]');
1110
    }
1111
1112
    /**
1113
     * Testing wp_new_user_notification email statuses.
1114
     *
1115
     * @dataProvider data_wp_new_user_notifications
1116
     * @ticket       33654
1117
     * @ticket       36009
1118
     */
1119
    function test_wp_new_user_notification( $notify, $admin_email_sent_expected, $user_email_sent_expected ) 
1120
    {
1121
        reset_phpmailer_instance();
1122
1123
        $was_admin_email_sent = false;
1124
        $was_user_email_sent = false;
1125
1126
        wp_new_user_notification(self::$contrib_id, null, $notify);
1127
1128
        $mailer = tests_retrieve_phpmailer_instance();
1129
1130
        /*
1131
         * Check to see if a notification email was sent to the
1132
         * post author `[email protected]` and and site admin `[email protected]`.
1133
         */
1134
        $first_recipient = $mailer->get_recipient('to');
1135
        if ($first_recipient ) {
1136
            $was_admin_email_sent = WP_TESTS_EMAIL === $first_recipient->address;
1137
            $was_user_email_sent = '[email protected]' === $first_recipient->address;
1138
        }
1139
1140
        $second_recipient = $mailer->get_recipient('to', 1);
1141
        if ($second_recipient ) {
1142
            $was_user_email_sent = '[email protected]' === $second_recipient->address;
1143
        }
1144
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
1145
1146
        $this->assertSame($admin_email_sent_expected, $was_admin_email_sent, 'Admin email result was not as expected in test_wp_new_user_notification');
1147
        $this->assertSame($user_email_sent_expected, $was_user_email_sent, 'User email result was not as expected in test_wp_new_user_notification');
1148
    }
1149
1150
    /**
1151
     * Data provider for test_wp_new_user_notification().
1152
     *
1153
     * Passes the three available options for the $notify parameter and the expected email
1154
     * emails sent status as a bool.
1155
     *
1156
     * @return array {
1157
     *     @type   array {
1158
     *         @type   string $post_args               The arguments that will merged with the $_POST array.
1159
     *         @type   bool $admin_email_sent_expected The expected result of whether an email was sent to the admin.
1160
     *         @type   bool $user_email_sent_expected  The expected result of whether an email was sent to the user.
1161
     *     }
1162
     * }
1163
     */
1164
    function data_wp_new_user_notifications() 
1165
    {
1166
        return array(
1167
         array(
1168
          '',
1169
          true,
1170
          false,
1171
         ),
1172
         array(
1173
          'admin',
1174
          true,
1175
          false,
1176
         ),
1177
         array(
1178
          'user',
1179
          false,
1180
          true,
1181
         ),
1182
         array(
1183
          'both',
1184
          true,
1185
          true,
1186
         ),
1187
        );
1188
    }
1189
1190
    /**
1191
     * Set up a user and try sending a notification using the old, deprecated
1192
     * function signature `wp_new_user_notification( $user, 'plaintext_password' );`.
1193
     *
1194
     * @ticket             33654
1195
     * @expectedDeprecated wp_new_user_notification
1196
     */
1197 View Code Duplication
    function test_wp_new_user_notification_old_signature_throws_deprecated_warning_but_sends() 
1198
    {
1199
        reset_phpmailer_instance();
1200
1201
        $was_admin_email_sent = false;
1202
        $was_user_email_sent = false;
1203
        wp_new_user_notification(self::$contrib_id, 'this_is_a_test_password');
1204
1205
        /*
1206
         * Check to see if a notification email was sent to the
1207
         * post author `[email protected]` and and site admin `[email protected]`.
1208
         */
1209
        if (! empty($GLOBALS['phpmailer']->mock_sent) ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
1210
            $was_admin_email_sent = ( isset($GLOBALS['phpmailer']->mock_sent[0]) && WP_TESTS_EMAIL == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] );
1211
            $was_user_email_sent = ( isset($GLOBALS['phpmailer']->mock_sent[1]) && '[email protected]' == $GLOBALS['phpmailer']->mock_sent[1]['to'][0][0] );
1212
        }
1213
1214
        $this->assertTrue($was_admin_email_sent);
1215
        $this->assertTrue($was_user_email_sent);
1216
    }
1217
1218
    /**
1219
     * Set up a user and try sending a notification using `wp_new_user_notification( $user );`.
1220
     *
1221
     * @ticket 34377
1222
     */
1223 View Code Duplication
    function test_wp_new_user_notification_old_signature_no_password() 
1224
    {
1225
        reset_phpmailer_instance();
1226
1227
        $was_admin_email_sent = false;
1228
        $was_user_email_sent = false;
1229
        wp_new_user_notification(self::$contrib_id);
1230
1231
        /*
1232
         * Check to see if a notification email was sent to the
1233
         * post author `[email protected]` and and site admin `[email protected]`.
1234
         */
1235
        if (! empty($GLOBALS['phpmailer']->mock_sent) ) {
0 ignored issues
show
introduced by
Expected 1 space before "!"; 0 found
Loading history...
1236
            $was_admin_email_sent = ( isset($GLOBALS['phpmailer']->mock_sent[0]) && WP_TESTS_EMAIL == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] );
1237
            $was_user_email_sent = ( isset($GLOBALS['phpmailer']->mock_sent[1]) && '[email protected]' == $GLOBALS['phpmailer']->mock_sent[1]['to'][0][0] );
1238
        }
1239
1240
        $this->assertTrue($was_admin_email_sent);
1241
        $this->assertFalse($was_user_email_sent);
1242
    }
1243
1244
    /**
1245
     * Checks that calling edit_user() with no password returns an error when adding, and doesn't when updating.
1246
     *
1247
     * @ticket 35715
1248
     */
1249
    function test_edit_user_blank_pw() 
1250
    {
1251
        $_POST = $_GET = $_REQUEST = array();
1252
        $_POST['role'] = 'subscriber';
1253
        $_POST['email'] = '[email protected]';
1254
        $_POST['user_login'] = 'user_login1';
1255
        $_POST['first_name'] = 'first_name1';
1256
        $_POST['last_name'] = 'last_name1';
1257
        $_POST['nickname'] = 'nickname1';
1258
        $_POST['display_name'] = 'display_name1';
1259
1260
        // Check new user with missing password.
1261
        $response = edit_user();
1262
1263
        $this->assertInstanceOf('WP_Error', $response);
1264
        $this->assertEquals('pass', $response->get_error_code());
1265
1266
        // Check new user with password set.
1267
        $_POST['pass1'] = $_POST['pass2'] = 'password';
1268
1269
        $user_id = edit_user();
1270
        $user = get_user_by('ID', $user_id);
1271
1272
        $this->assertInternalType('int', $user_id);
1273
        $this->assertInstanceOf('WP_User', $user);
1274
        $this->assertEquals('nickname1', $user->nickname);
1275
1276
        // Check updating user with empty password.
1277
        $_POST['nickname'] = 'nickname_updated';
1278
        $_POST['pass1'] = $_POST['pass2'] = '';
1279
1280
        $user_id = edit_user($user_id);
1281
1282
        $this->assertInternalType('int', $user_id);
1283
        $this->assertEquals('nickname_updated', $user->nickname);
1284
1285
        // Check updating user with missing second password.
1286
        $_POST['nickname'] = 'nickname_updated2';
1287
        $_POST['pass1'] = 'blank_pass2';
1288
        $_POST['pass2'] = '';
1289
1290
        $response = edit_user($user_id);
1291
1292
        $this->assertInstanceOf('WP_Error', $response);
1293
        $this->assertEquals('pass', $response->get_error_code());
1294
        $this->assertEquals('nickname_updated', $user->nickname);
1295
1296
        // Check updating user with empty password via `check_passwords` action.
1297
        add_action('check_passwords', array( $this, 'action_check_passwords_blank_pw' ), 10, 2);
1298
        $user_id = edit_user($user_id);
1299
        remove_action('check_passwords', array( $this, 'action_check_passwords_blank_pw' ));
1300
1301
        $this->assertInternalType('int', $user_id);
1302
        $this->assertEquals('nickname_updated2', $user->nickname);
1303
    }
1304
1305
    /**
1306
     * Check passwords action for test_edit_user_blank_pw().
1307
     */
1308
    function action_check_passwords_blank_pw( $user_login, &$pass1 ) 
1309
    {
1310
        $pass1 = '';
1311
    }
1312
}
1313