Make WordPress Core

Changeset 34031


Ignore:
Timestamp:
09/11/2015 01:39:52 AM (9 years ago)
Author:
boonebgorges
Message:

Move wp_delete_user() tests to their own file.

See #33800.

Location:
branches/4.3/tests/phpunit/tests
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/4.3/tests/phpunit/tests/user.php

    r33115 r34031  
    336336
    337337    /**
    338      * Test that usermeta cache is cleared after user deletion.
    339      *
    340      * @ticket 19500
    341      */
    342     function test_get_blogs_of_user() {
    343         // Logged out users don't have blogs.
    344         $this->assertEquals( array(), get_blogs_of_user( 0 ) );
    345 
    346         $user_id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
    347         $blogs = get_blogs_of_user( $user_id );
    348         $this->assertEquals( array( 1 ), array_keys( $blogs ) );
    349 
    350         // Non-existent users don't have blogs.
    351         if ( is_multisite() )
    352             wpmu_delete_user( $user_id );
    353         else
    354             wp_delete_user( $user_id );
    355 
    356         $user = new WP_User( $user_id );
    357         $this->assertFalse( $user->exists(), 'WP_User->exists' );
    358         $this->assertEquals( array(), get_blogs_of_user( $user_id ) );
    359     }
    360 
    361     /**
    362      * Test that usermeta cache is cleared after user deletion.
    363      *
    364      * @ticket 19500
    365      */
    366     function test_is_user_member_of_blog() {
    367         $old_current = get_current_user_id();
    368 
    369         $user_id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
    370         wp_set_current_user( $user_id );
    371 
    372         $this->assertTrue( is_user_member_of_blog() );
    373         $this->assertTrue( is_user_member_of_blog( 0, 0 ) );
    374         $this->assertTrue( is_user_member_of_blog( 0, get_current_blog_id() ) );
    375         $this->assertTrue( is_user_member_of_blog( $user_id ) );
    376         $this->assertTrue( is_user_member_of_blog( $user_id, get_current_blog_id() ) );
    377 
    378         // Will only remove the user from the current site in multisite; this is desired
    379         // and will achieve the desired effect with is_user_member_of_blog().
    380         wp_delete_user( $user_id );
    381 
    382         $this->assertFalse( is_user_member_of_blog( $user_id ) );
    383         $this->assertFalse( is_user_member_of_blog( $user_id, get_current_blog_id() ) );
    384 
    385         wp_set_current_user( $old_current );
    386     }
    387 
    388     /**
    389338     * ticket 19595
    390339     */
     
    457406    }
    458407
    459     function test_delete_user() {
    460         $user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    461         $user = new WP_User( $user_id );
    462 
    463         $post = array(
    464             'post_author' => $user_id,
    465             'post_status' => 'publish',
    466             'post_content' => rand_str(),
    467             'post_title' => rand_str(),
    468             'post_type' => 'post',
    469         );
    470 
    471         // insert a post and make sure the ID is ok
    472         $post_id = wp_insert_post($post);
    473         $this->assertTrue(is_numeric($post_id));
    474         $this->assertTrue($post_id > 0);
    475 
    476         $post = get_post( $post_id );
    477         $this->assertEquals( $post_id, $post->ID );
    478 
    479         $post = array(
    480             'post_author' => $user_id,
    481             'post_status' => 'publish',
    482             'post_content' => rand_str(),
    483             'post_title' => rand_str(),
    484             'post_type' => 'nav_menu_item',
    485         );
    486 
    487         // insert a post and make sure the ID is ok
    488         $nav_id = wp_insert_post($post);
    489         $this->assertTrue(is_numeric($nav_id));
    490         $this->assertTrue($nav_id > 0);
    491 
    492         $post = get_post( $nav_id );
    493         $this->assertEquals( $nav_id, $post->ID );
    494 
    495         wp_delete_user( $user_id );
    496         $user = new WP_User( $user_id );
    497         if ( is_multisite() )
    498             $this->assertTrue( $user->exists() );
    499         else
    500             $this->assertFalse( $user->exists() );
    501 
    502         $this->assertNotNull( get_post( $post_id ) );
    503         $this->assertEquals( 'trash', get_post( $post_id )->post_status );
    504         // nav_menu_item is delete_with_user = false so the nav post should remain published.
    505         $this->assertNotNull( get_post( $nav_id ) );
    506         $this->assertEquals( 'publish', get_post( $nav_id )->post_status );
    507         wp_delete_post( $nav_id, true );
    508         $this->assertNull( get_post( $nav_id ) );
    509         wp_delete_post( $post_id, true );
    510         $this->assertNull( get_post( $post_id ) );
    511     }
    512 
    513408    /**
    514409     * @ticket 13317
     
    549444        $user = WP_User::get_data_by( 'id', 99999 );
    550445        $this->assertEquals( false, $user );
    551     }
    552 
    553     /**
    554      * @ticket 20447
    555      */
    556     function test_wp_delete_user_reassignment_clears_post_caches() {
    557         $user_id   = $this->factory->user->create();
    558         $reassign  = $this->factory->user->create();
    559         $post_id   = $this->factory->post->create( array( 'post_author' => $user_id ) );
    560 
    561         get_post( $post_id ); // Ensure this post is in the cache.
    562 
    563         wp_delete_user( $user_id, $reassign );
    564 
    565         $post = get_post( $post_id );
    566         $this->assertEquals( $reassign, $post->post_author );
    567446    }
    568447
  • branches/4.3/tests/phpunit/tests/user/wpDeleteUser.php

    r34030 r34031  
    11<?php
    22
    3 // test functions in wp-includes/user.php
    43/**
    54 * @group user
    65 */
    7 class Tests_User extends WP_UnitTestCase {
    8 
    9     protected $user_data;
    10 
    11     function setUp() {
    12         parent::setUp();
    13 
    14         $this->user_data = array(
    15             'user_login' => 'user1',
    16             'user_nicename' => 'userone',
    17             'user_pass'  => 'password',
    18             'first_name' => 'John',
    19             'last_name'  => 'Doe',
    20             'display_name' => 'John Doe',
    21             'user_email' => 'blackburn@battlefield3.com',
    22             'user_url' => 'http://tacos.com'
    23         );
    24     }
    25 
    26     function test_get_users_of_blog() {
    27         // add one of each user role
    28         $nusers = array();
    29         foreach ( array('administrator', 'editor', 'author', 'contributor', 'subscriber' ) as $role ) {
    30             $id = $this->factory->user->create( array( 'role' => $role ) );
    31             $nusers[ $id ] = $id;
    32         }
    33 
    34         $user_list = get_users();
    35 
    36         // find the role of each user as returned by get_users_of_blog
    37         $found = array();
    38         foreach ( $user_list as $user ) {
    39             // only include the users we just created - there might be some others that existed previously
    40             if ( isset( $nusers[$user->ID] ) ) {
    41                 $found[ $user->ID] = $user->ID;
    42             }
    43         }
    44 
    45         // make sure every user we created was returned
    46         $this->assertEquals($nusers, $found);
    47     }
    48 
    49     // simple get/set tests for user_option functions
    50     function test_user_option() {
    51         $key = rand_str();
    52         $val = rand_str();
    53 
    54         $user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    55 
    56         // get an option that doesn't exist
    57         $this->assertFalse(get_user_option($key, $user_id));
    58 
    59         // set and get
    60         update_user_option( $user_id, $key, $val );
    61         $this->assertEquals( $val, get_user_option($key, $user_id) );
    62 
    63         // change and get again
    64         $val2 = rand_str();
    65         update_user_option( $user_id, $key, $val2 );
    66         $this->assertEquals( $val2, get_user_option($key, $user_id) );
    67 
    68     }
    69 
    70     // simple tests for usermeta functions
    71     function test_usermeta() {
    72 
    73         $key = rand_str();
    74         $val = rand_str();
    75 
    76         $user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    77 
    78         // get a meta key that doesn't exist
    79         $this->assertEquals( '', get_user_meta($user_id, $key, true));
    80 
    81         // set and get
    82         update_user_meta( $user_id, $key, $val );
    83         $this->assertEquals( $val, get_user_meta($user_id, $key, true) );
    84 
    85         // change and get again
    86         $val2 = rand_str();
    87         update_user_meta( $user_id, $key, $val2 );
    88         $this->assertEquals( $val2, get_user_meta($user_id, $key, true) );
    89 
    90         // delete and get
    91         delete_user_meta( $user_id, $key );
    92         $this->assertEquals( '', get_user_meta($user_id, $key, true) );
    93 
    94         // delete by key AND value
    95         update_user_meta( $user_id, $key, $val );
    96         // incorrect key: key still exists
    97         delete_user_meta( $user_id, $key, rand_str() );
    98         $this->assertEquals( $val, get_user_meta($user_id, $key, true) );
    99         // correct key: deleted
    100         delete_user_meta( $user_id, $key, $val );
    101         $this->assertEquals( '', get_user_meta($user_id, $key, true) );
    102 
    103     }
    104 
    105     // test usermeta functions in array mode
    106     function test_usermeta_array() {
    107         // some values to set
    108         $vals = array(
    109             rand_str() => 'val-'.rand_str(),
    110             rand_str() => 'val-'.rand_str(),
    111             rand_str() => 'val-'.rand_str(),
    112         );
    113 
    114         $user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    115 
    116         // there is already some stuff in the array
    117         $this->assertTrue(is_array(get_user_meta($user_id)));
    118 
    119         foreach ($vals as $k=>$v)
    120             update_user_meta( $user_id, $k, $v );
    121 
    122         // get the complete usermeta array
    123         $out = get_user_meta($user_id);
    124 
    125         // for reasons unclear, the resulting array is indexed numerically; meta keys are not included anywhere.
    126         // so we'll just check to make sure our values are included somewhere.
    127         foreach ($vals as $k=>$v)
    128             $this->assertTrue(isset($out[$k]) && $out[$k][0] == $v);
    129 
    130         // delete one key and check again
    131         $keys = array_keys( $vals );
    132         $key_to_delete = array_pop( $keys );
    133         delete_user_meta($user_id, $key_to_delete);
    134         $out = get_user_meta($user_id);
    135         // make sure that key is excluded from the results
    136         foreach ($vals as $k=>$v) {
    137             if ($k == $key_to_delete)
    138                 $this->assertFalse(isset($out[$k]));
    139             else
    140             $this->assertTrue(isset($out[$k]) && $out[$k][0] == $v);
    141         }
    142     }
    143 
    144     // Test property magic functions for property get/set/isset.
    145     function test_user_properties() {
    146         $user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    147         $user = new WP_User( $user_id );
    148 
    149         foreach ( $user->data as $key => $data ) {
    150             $this->assertEquals( $data, $user->$key );
    151         }
    152 
    153         $this->assertTrue( isset( $user->$key ) );
    154         $this->assertFalse( isset( $user->fooooooooo ) );
    155 
    156         $user->$key = 'foo';
    157         $this->assertEquals( 'foo', $user->$key );
    158         $this->assertEquals( 'foo', $user->data->$key );  // This will fail with WP < 3.3
    159 
    160         foreach ( (array) $user as $key => $value ) {
    161             $this->assertEquals( $value, $user->$key );
    162         }
    163     }
    164 
    165     // Test meta property magic functions for property get/set/isset.
    166     function test_user_meta_properties() {
    167         global $wpdb;
    168 
    169         $user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    170         $user = new WP_User( $user_id );
    171 
    172         update_user_option( $user_id, 'foo', 'foo', true );
    173 
    174         $this->assertTrue( isset( $user->foo ) );
    175 
    176         $this->assertEquals( 'foo', $user->foo );
    177     }
    178 
    179     /**
    180      * @expectedDeprecated WP_User->id
    181      */
    182     function test_id_property_back_compat() {
    183         $user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    184         $user = new WP_User( $user_id );
    185 
    186         $this->assertTrue( isset( $user->id ) );
    187         $this->assertEquals( $user->ID, $user->id );
    188         $user->id = 1234;
    189         $this->assertEquals( $user->ID, $user->id );
    190     }
    191 
    192     /**
    193      * ticket 19265
    194      */
    195     function test_user_level_property_back_compat() {
    196         $roles = array(
    197             'administrator' => 10,
    198             'editor' => 7,
    199             'author' => 2,
    200             'contributor' => 1,
    201             'subscriber' => 0,
    202         );
    203 
    204         foreach ( $roles as $role => $level ) {
    205             $user_id = $this->factory->user->create( array( 'role' => $role ) );
    206             $user = new WP_User( $user_id );
    207 
    208             $this->assertTrue( isset( $user->user_level ) );
    209             $this->assertEquals( $level, $user->user_level );
    210         }
    211     }
    212 
    213     function test_construction() {
    214         $user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    215 
    216         $user = new WP_User( $user_id );
    217         $this->assertInstanceOf( 'WP_User', $user );
    218         $this->assertEquals( $user_id, $user->ID );
    219 
    220         $user2 = new WP_User( 0,  $user->user_login );
    221         $this->assertInstanceOf( 'WP_User', $user2 );
    222         $this->assertEquals( $user_id, $user2->ID );
    223         $this->assertEquals( $user->user_login, $user2->user_login );
    224 
    225         $user3 = new WP_User();
    226         $this->assertInstanceOf( 'WP_User', $user3 );
    227         $this->assertEquals( 0, $user3->ID );
    228         $this->assertFalse( isset( $user3->user_login ) );
    229 
    230         $user3->init( $user->data );
    231         $this->assertEquals( $user_id, $user3->ID );
    232 
    233         $user4 = new WP_User( $user->user_login );
    234         $this->assertInstanceOf( 'WP_User', $user4 );
    235         $this->assertEquals( $user_id, $user4->ID );
    236         $this->assertEquals( $user->user_login, $user4->user_login );
    237 
    238         $user5 = new WP_User( null, $user->user_login );
    239         $this->assertInstanceOf( 'WP_User', $user5 );
    240         $this->assertEquals( $user_id, $user5->ID );
    241         $this->assertEquals( $user->user_login, $user5->user_login );
    242 
    243         $user6 = new WP_User( $user );
    244         $this->assertInstanceOf( 'WP_User', $user6 );
    245         $this->assertEquals( $user_id, $user6->ID );
    246         $this->assertEquals( $user->user_login, $user6->user_login );
    247 
    248         $user7 = new WP_User( $user->data );
    249         $this->assertInstanceOf( 'WP_User', $user7 );
    250         $this->assertEquals( $user_id, $user7->ID );
    251         $this->assertEquals( $user->user_login, $user7->user_login );
    252     }
    253 
    254     function test_get() {
    255         $user_id = $this->factory->user->create( array(
    256             'role' => 'author',
    257             'user_login' => 'test_wp_user_get',
    258             'user_pass' => 'password',
    259             'user_email' => 'test@test.com',
    260         ) );
    261 
    262         $user = new WP_User( $user_id );
    263         $this->assertEquals( 'test_wp_user_get', $user->get( 'user_login' ) );
    264         $this->assertEquals( 'test@test.com', $user->get( 'user_email' ) );
    265         $this->assertEquals( 0, $user->get( 'use_ssl' ) );
    266         $this->assertEquals( '', $user->get( 'field_that_does_not_exist' ) );
    267 
    268         update_user_meta( $user_id, 'dashed-key', 'abcdefg' );
    269         $this->assertEquals( 'abcdefg', $user->get( 'dashed-key' ) );
    270     }
    271 
    272     function test_has_prop() {
    273         $user_id = $this->factory->user->create( array(
    274             'role' => 'author',
    275             'user_login' => 'test_wp_user_has_prop',
    276             'user_pass' => 'password',
    277             'user_email' => 'test2@test.com',
    278         ) );
    279 
    280         $user = new WP_User( $user_id );
    281         $this->assertTrue( $user->has_prop( 'user_email') );
    282         $this->assertTrue( $user->has_prop( 'use_ssl' ) );
    283         $this->assertFalse( $user->has_prop( 'field_that_does_not_exist' ) );
    284 
    285         update_user_meta( $user_id, 'dashed-key', 'abcdefg' );
    286         $this->assertTrue( $user->has_prop( 'dashed-key' ) );
    287     }
    288 
    289     function test_update_user() {
    290         $user_id = $this->factory->user->create( array(
    291             'role' => 'author',
    292             'user_login' => 'test_wp_update_user',
    293             'user_pass' => 'password',
    294             'user_email' => 'test3@test.com',
    295         ) );
    296         $user = new WP_User( $user_id );
    297 
    298         update_user_meta( $user_id, 'description', 'about me' );
    299         $this->assertEquals( 'about me', $user->get( 'description' ) );
    300 
    301         $user_data = array( 'ID' => $user_id, 'display_name' => 'test user' );
    302         wp_update_user( $user_data );
    303 
    304         $user = new WP_User( $user_id );
    305         $this->assertEquals( 'test user', $user->get( 'display_name' ) );
    306 
    307         // Make sure there is no collateral damage to fields not in $user_data
    308         $this->assertEquals( 'about me', $user->get( 'description' ) );
    309 
    310         // Pass as stdClass
    311         $user_data = array( 'ID' => $user_id, 'display_name' => 'a test user' );
    312         wp_update_user( (object) $user_data );
    313 
    314         $user = new WP_User( $user_id );
    315         $this->assertEquals( 'a test user', $user->get( 'display_name' ) );
    316 
    317         // Pass as WP_User
    318         $user = new WP_User( $user_id );
    319         $user->display_name = 'some test user';
    320         wp_update_user( $user );
    321 
    322         $user = new WP_User( $user_id );
    323         $this->assertEquals( 'some test user', $user->get( 'display_name' ) );
    324 
    325         // Test update of fields in _get_additional_user_keys()
    326         $user_data = array( 'ID' => $user_id, 'use_ssl' => 1, 'show_admin_bar_front' => 1,
    327                            'rich_editing' => 1, 'first_name' => 'first', 'last_name' => 'last',
    328                            'nickname' => 'nick', 'comment_shortcuts' => 'true', 'admin_color' => 'classic',
    329                            'description' => 'describe' );
    330         wp_update_user( $user_data );
    331 
    332         $user = new WP_User( $user_id );
    333         foreach ( $user_data as $key => $value )
    334             $this->assertEquals( $value, $user->get( $key ), $key );
    335     }
     6class Tests_User_WpDeleteUser extends WP_UnitTestCase {
    3367
    3378    /**
     
    38455
    38556        wp_set_current_user( $old_current );
    386     }
    387 
    388     /**
    389      * ticket 19595
    390      */
    391     function test_global_userdata() {
    392         global $userdata, $wpdb;
    393 
    394         $user_id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
    395         wp_set_current_user( $user_id );
    396 
    397         $this->assertNotEmpty( $userdata );
    398         $this->assertInstanceOf( 'WP_User', $userdata );
    399         $this->assertEquals( $userdata->ID, $user_id );
    400         $prefix = $wpdb->get_blog_prefix();
    401         $cap_key = $prefix . 'capabilities';
    402         $this->assertTrue( isset( $userdata->$cap_key ) );
    403     }
    404 
    405     /**
    406      * ticket 19769
    407      */
    408     function test_global_userdata_is_null_when_logged_out() {
    409         global $userdata;
    410         wp_set_current_user(0);
    411         $this->assertNull( $userdata );
    412     }
    413 
    414     function test_exists() {
    415         $user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    416         $user = new WP_User( $user_id );
    417 
    418         $this->assertTrue( $user->exists() );
    419 
    420         $user = new WP_User( 123456789 );
    421 
    422         $this->assertFalse( $user->exists() );
    423 
    424         $user = new WP_User( 0 );
    425 
    426         $this->assertFalse( $user->exists() );
    427     }
    428 
    429     function test_global_authordata() {
    430         global $authordata, $id;
    431 
    432         $old_post_id = $id;
    433 
    434         $user_id = $this->factory->user->create( array( 'role' => 'author' ) );
    435         $user = new WP_User( $user_id );
    436 
    437         $post = array(
    438             'post_author' => $user_id,
    439             'post_status' => 'publish',
    440             'post_content' => rand_str(),
    441             'post_title' => rand_str(),
    442             'post_type' => 'post'
    443         );
    444 
    445         // insert a post and make sure the ID is ok
    446         $post_id = wp_insert_post( $post );
    447         $this->assertTrue( is_numeric( $post_id ) );
    448 
    449         setup_postdata( get_post( $post_id ) );
    450 
    451         $this->assertNotEmpty( $authordata );
    452         $this->assertInstanceOf( 'WP_User', $authordata );
    453         $this->assertEquals( $authordata->ID, $user_id );
    454 
    455         if ( $old_post_id )
    456             setup_postdata( get_post( $old_post_id ) );
    45757    }
    45858
     
    512112
    513113    /**
    514      * @ticket 13317
    515      */
    516     function test_get_userdata() {
    517         $this->assertFalse( get_userdata( 0 ) );
    518         $this->assertFalse( get_userdata( '0' ) );
    519         $this->assertFalse( get_userdata( 'string' ) );
    520         $this->assertFalse( get_userdata( array( 'array' ) ) );
    521     }
    522 
    523     function test_user_get_data_by_id() {
    524         $user_id   = $this->factory->user->create();
    525 
    526         $user = WP_User::get_data_by( 'id', $user_id );
    527         $this->assertInstanceOf( 'stdClass', $user );
    528         $this->assertEquals( $user_id, $user->ID );
    529 
    530         // @ticket 23480
    531         $user = WP_User::get_data_by( 'id', -1 );
    532         $this->assertEquals( false, $user );
    533 
    534         $user = WP_User::get_data_by( 'id', 0 );
    535         $this->assertEquals( false, $user );
    536 
    537         $user = WP_User::get_data_by( 'id', null );
    538         $this->assertEquals( false, $user );
    539 
    540         $user = WP_User::get_data_by( 'id', '' );
    541         $this->assertEquals( false, $user );
    542 
    543         $user = WP_User::get_data_by( 'id', false );
    544         $this->assertEquals( false, $user );
    545 
    546         $user = WP_User::get_data_by( 'id', @$user->user_nicename );
    547         $this->assertEquals( false, $user );
    548 
    549         $user = WP_User::get_data_by( 'id', 99999 );
    550         $this->assertEquals( false, $user );
    551     }
    552 
    553     /**
    554114     * @ticket 20447
    555115     */
     
    566126        $this->assertEquals( $reassign, $post->post_author );
    567127    }
    568 
    569     /**
    570      * @ticket 21431
    571      */
    572     function test_count_many_users_posts() {
    573         $user_id_a = $this->factory->user->create( array( 'role' => 'author' ) );
    574         $user_id_b = $this->factory->user->create( array( 'role' => 'author' ) );
    575         $post_id_a = $this->factory->post->create( array( 'post_author' => $user_id_a ) );
    576         $post_id_b = $this->factory->post->create( array( 'post_author' => $user_id_b ) );
    577         $post_id_c = $this->factory->post->create( array( 'post_author' => $user_id_b, 'post_status' => 'private' ) );
    578 
    579         wp_set_current_user( $user_id_a );
    580         $counts = count_many_users_posts( array( $user_id_a, $user_id_b), 'post', false );
    581         $this->assertEquals( 1, $counts[$user_id_a] );
    582         $this->assertEquals( 1, $counts[$user_id_b] );
    583 
    584         $counts = count_many_users_posts( array( $user_id_a, $user_id_b), 'post', true );
    585         $this->assertEquals( 1, $counts[$user_id_a] );
    586         $this->assertEquals( 1, $counts[$user_id_b] );
    587 
    588         wp_set_current_user( $user_id_b );
    589         $counts = count_many_users_posts( array( $user_id_a, $user_id_b), 'post', false );
    590         $this->assertEquals( 1, $counts[$user_id_a] );
    591         $this->assertEquals( 2, $counts[$user_id_b] );
    592 
    593         $counts = count_many_users_posts( array( $user_id_a, $user_id_b), 'post', true );
    594         $this->assertEquals( 1, $counts[$user_id_a] );
    595         $this->assertEquals( 1, $counts[$user_id_b] );
    596     }
    597 
    598     /**
    599      * @ticket 22858
    600      */
    601     function test_wp_update_user_on_nonexistent_users() {
    602         $user_id = 1;
    603         // Find me a non-existent user ID.
    604         while ( get_userdata( $user_id ) )
    605             ++$user_id;
    606 
    607         // If this test fails, it will error out for calling the to_array() method on a non-object.
    608         $this->assertInstanceOf( 'WP_Error', wp_update_user( array( 'ID' => $user_id ) ) );
    609     }
    610 
    611     /**
    612      * @ticket 28315
    613      */
    614     function test_user_meta_error() {
    615         $id1 = wp_insert_user( array(
    616             'user_login' => rand_str(),
    617             'user_pass' => 'password',
    618             'user_email' => 'taco@burrito.com',
    619         ) );
    620         $this->assertEquals( $id1, email_exists( 'taco@burrito.com' ) );
    621 
    622         $id2 = wp_insert_user( array(
    623             'user_login' => rand_str(),
    624             'user_pass' => 'password',
    625             'user_email' => 'taco@burrito.com',
    626         ) );
    627 
    628         if ( ! defined( 'WP_IMPORTING' ) ) {
    629             $this->assertWPError( $id2 );
    630         }
    631 
    632         @update_user_meta( $id2, 'key', 'value' );
    633 
    634         $metas = array_keys( get_user_meta( 1 ) );
    635         $this->assertNotContains( 'key', $metas );
    636     }
    637 
    638     /**
    639      * @ticket 30647
    640      */
    641     function test_user_update_email_error() {
    642         $id1 = wp_insert_user( array(
    643             'user_login' => rand_str(),
    644             'user_pass'  => 'password',
    645             'user_email' => 'blackburn@battlefield3.com',
    646         ) );
    647         $this->assertEquals( $id1, email_exists( 'blackburn@battlefield3.com' ) );
    648 
    649         $id2 = wp_insert_user( array(
    650             'user_login' => rand_str(),
    651             'user_pass'  => 'password',
    652             'user_email' => 'miller@battlefield3.com',
    653         ) );
    654         $this->assertEquals( $id2, email_exists( 'miller@battlefield3.com' ) );
    655 
    656         if( ! is_wp_error( $id2 ) ){
    657             $return = wp_update_user( array(
    658                 'ID'         => $id2,
    659                 'user_email' => 'david@battlefield3.com',
    660             ) );
    661             $this->assertEquals( $id2, email_exists( 'david@battlefield3.com' ) );
    662 
    663             $return = wp_update_user( array(
    664                 'ID'         => $id2,
    665                 'user_email' => 'blackburn@battlefield3.com',
    666             ) );
    667             if ( ! defined( 'WP_IMPORTING' ) ) {
    668                 $this->assertWPError( $return );
    669             }
    670         }
    671     }
    672 
    673     /**
    674      * @ticket 29696
    675      */
    676     public function test_wp_insert_user_should_sanitize_user_nicename_parameter() {
    677         $user = $this->factory->user->create_and_get();
    678 
    679         $userdata = $user->to_array();
    680         $userdata['user_nicename'] = str_replace( '-', '.', $user->user_nicename );
    681         wp_insert_user( $userdata );
    682 
    683         $updated_user = new WP_User( $user->ID );
    684 
    685         $this->assertSame( $user->user_nicename, $updated_user->user_nicename );
    686     }
    687 
    688     function test_changing_email_invalidates_password_reset_key() {
    689         global $wpdb;
    690 
    691         $user = $this->factory->user->create_and_get();
    692         $wpdb->update( $wpdb->users, array( 'user_activation_key' => 'key' ), array( 'ID' => $user->ID ) );
    693         clean_user_cache( $user );
    694 
    695         $user = get_userdata( $user->ID );
    696         $this->assertEquals( 'key', $user->user_activation_key );
    697 
    698         // Check that changing something other than the email doesn't remove the key.
    699         $userdata = array(
    700             'ID'            => $user->ID,
    701             'user_nicename' => 'wat',
    702         );
    703         wp_update_user( $userdata );
    704 
    705         $user = get_userdata( $user->ID );
    706         $this->assertEquals( 'key', $user->user_activation_key );
    707 
    708         // Now check that changing the email does remove it.
    709         $userdata = array(
    710             'ID'            => $user->ID,
    711             'user_nicename' => 'cat',
    712             'user_email'    => 'foo@bar.dev',
    713         );
    714         wp_update_user( $userdata );
    715 
    716         $user = get_userdata( $user->ID );
    717         $this->assertEmpty( $user->user_activation_key );
    718     }
    719 
    720     public function test_search_users_login() {
    721         $id = $this->factory->user->create( $this->user_data );
    722 
    723         $users = get_users( array( 'search' => 'user1', 'fields' => 'ID' ) );
    724 
    725         $this->assertTrue( in_array( $id, $users ) );
    726     }
    727 
    728     public function test_search_users_url() {
    729         $id = $this->factory->user->create( $this->user_data );
    730 
    731         $users = get_users( array( 'search' => '*tacos*', 'fields' => 'ID' ) );
    732 
    733         $this->assertTrue( in_array( $id, $users ) );
    734     }
    735 
    736     public function test_search_users_email() {
    737         $id = $this->factory->user->create( $this->user_data );
    738 
    739         $users = get_users( array( 'search' => '*battle*', 'fields' => 'ID' ) );
    740 
    741         $this->assertTrue( in_array( $id, $users ) );
    742     }
    743 
    744     public function test_search_users_nicename() {
    745         $id = $this->factory->user->create( $this->user_data );
    746 
    747         $users = get_users( array( 'search' => '*one*', 'fields' => 'ID' ) );
    748 
    749         $this->assertTrue( in_array( $id, $users ) );
    750     }
    751 
    752     public function test_search_users_display_name() {
    753         $id = $this->factory->user->create( $this->user_data );
    754 
    755         $users = get_users( array( 'search' => '*Doe*', 'fields' => 'ID' ) );
    756 
    757         $this->assertTrue( in_array( $id, $users ) );
    758     }
    759 
    760     /**
    761      * @ticket 32158
    762      */
    763     function test_email_case() {
    764         // Create a test user with a lower-case email address.
    765         $user_id = $this->factory->user->create( array(
    766             'user_email' => 'test@test.com',
    767         ) );
    768 
    769         // Alter the case of the email address (which stays the same).
    770         $userdata = array(
    771             'ID' => $user_id,
    772             'user_email' => 'test@TEST.com',
    773         );
    774         $update = wp_update_user( $userdata );
    775 
    776         $this->assertEquals( $user_id, $update );
    777     }
    778 
    779     /**
    780      * @ticket 32158
    781      */
    782     function test_email_change() {
    783         // Create a test user.
    784         $user_id = $this->factory->user->create( array(
    785             'user_email' => 'test@test.com',
    786         ) );
    787 
    788         // Change the email address.
    789         $userdata = array(
    790             'ID' => $user_id,
    791             'user_email' => 'test2@test.com',
    792         );
    793         $update = wp_update_user( $userdata );
    794 
    795         // Was this successful?
    796         $this->assertEquals( $user_id, $update );
    797 
    798         // Verify that the email address has been updated.
    799         $user = get_userdata( $user_id );
    800         $this->assertEquals( $user->user_email, 'test2@test.com' );
    801     }
    802 
    803128}
Note: See TracChangeset for help on using the changeset viewer.