WordPress.org

Make WordPress Core

Changeset 955 in tests


Ignore:
Timestamp:
08/06/2012 02:49:39 AM (6 years ago)
Author:
nbachiyski
Message:

Don't use deprecated user functions in user tests

When we have a valid usecase for deprecated functions, like testing
backeards compatibility, we disable triggering deprecated errors
temporarily.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/user.php

    r909 r955  
    2222    }
    2323
     24    public function disable_deprecated_errors( $value ) {
     25        return false;
     26    }
     27
    2428    function test_get_users_of_blog() {
    2529        // add one of each user role
    26         $user_role = array();
     30        $nusers = array();
    2731        foreach ( array('administrator', 'editor', 'author', 'contributor', 'subscriber' ) as $role ) {
    2832            $id = $this->factory->user->create( array( 'role' => $role ) );
    29             $user_role[ $id ] = $role;
     33            $nusers[ $id ] = $id;
    3034        }
    3135
    32         $user_list = get_users_of_blog();
     36        $user_list = get_users();
    3337
    3438        // find the role of each user as returned by get_users_of_blog
     
    3640        foreach ( $user_list as $user ) {
    3741            // only include the users we just created - there might be some others that existed previously
    38             if ( isset( $user_role[$user->user_id] ) ) {
    39                 $roles = array_keys( unserialize( $user->meta_value ) );
    40                 $found[ $user->user_id ] = $roles[0];
     42            if ( isset( $nusers[$user->ID] ) ) {
     43                $found[ $user->ID] = $user->ID;
    4144            }
    4245        }
    4346
    4447        // make sure every user we created was returned
    45         $this->assertEquals($user_role, $found);
     48        $this->assertEquals($nusers, $found);
    4649    }
    4750
     
    7679
    7780        // get a meta key that doesn't exist
    78         $this->assertEquals( '', get_usermeta($user_id, $key) );
     81        $this->assertEquals( '', get_user_meta($user_id, $key, true));
    7982
    8083        // set and get
    81         update_usermeta( $user_id, $key, $val );
    82         $this->assertEquals( $val, get_usermeta($user_id, $key) );
     84        update_user_meta( $user_id, $key, $val );
     85        $this->assertEquals( $val, get_user_meta($user_id, $key, true) );
    8386
    8487        // change and get again
    8588        $val2 = rand_str();
    86         update_usermeta( $user_id, $key, $val2 );
    87         $this->assertEquals( $val2, get_usermeta($user_id, $key) );
     89        update_user_meta( $user_id, $key, $val2 );
     90        $this->assertEquals( $val2, get_user_meta($user_id, $key, true) );
    8891
    8992        // delete and get
    90         delete_usermeta( $user_id, $key );
    91         $this->assertEquals( '', get_usermeta($user_id, $key) );
     93        delete_user_meta( $user_id, $key );
     94        $this->assertEquals( '', get_user_meta($user_id, $key, true) );
    9295
    9396        // delete by key AND value
    94         update_usermeta( $user_id, $key, $val );
     97        update_user_meta( $user_id, $key, $val );
    9598        // incorrect key: key still exists
    96         delete_usermeta( $user_id, $key, rand_str() );
    97         $this->assertEquals( $val, get_usermeta($user_id, $key) );
     99        delete_user_meta( $user_id, $key, rand_str() );
     100        $this->assertEquals( $val, get_user_meta($user_id, $key, true) );
    98101        // correct key: deleted
    99         delete_usermeta( $user_id, $key, $val );
    100         $this->assertEquals( '', get_usermeta($user_id, $key) );
     102        delete_user_meta( $user_id, $key, $val );
     103        $this->assertEquals( '', get_user_meta($user_id, $key, true) );
    101104
    102105    }
     
    114117
    115118        // there is already some stuff in the array
    116         $this->assertTrue(is_array(get_usermeta($user_id)));
     119        $this->assertTrue(is_array(get_user_meta($user_id)));
    117120
    118121        foreach ($vals as $k=>$v)
    119             update_usermeta( $user_id, $k, $v );
     122            update_user_meta( $user_id, $k, $v );
    120123
    121124        // get the complete usermeta array
    122         $out = get_usermeta($user_id);
     125        $out = get_user_meta($user_id);
    123126
    124127        // for reasons unclear, the resulting array is indexed numerically; meta keys are not included anywhere.
    125128        // so we'll just check to make sure our values are included somewhere.
    126         foreach ($vals as $v)
    127             $this->assertTrue(in_array($v, $out));
     129        foreach ($vals as $k=>$v)
     130            $this->assertTrue(isset($out[$k]) && $out[$k][0] == $v);
    128131
    129132        // delete one key and check again
    130133        $key_to_delete = array_pop(array_keys($vals));
    131         delete_usermeta($user_id, $key_to_delete);
    132         $out = get_usermeta($user_id);
     134        delete_user_meta($user_id, $key_to_delete);
     135        $out = get_user_meta($user_id);
    133136        // make sure that key is excluded from the results
    134137        foreach ($vals as $k=>$v) {
    135138            if ($k == $key_to_delete)
    136                 $this->assertFalse(in_array($v, $out));
     139                $this->assertFalse(isset($out[$k]));
    137140            else
    138                 $this->assertTrue(in_array($v, $out));
     141            $this->assertTrue(isset($out[$k]) && $out[$k][0] == $v);
    139142        }
    140143    }
     
    179182        // Test 'id' (lowercase)
    180183        add_action( 'deprecated_argument_run', array( $this, 'deprecated_handler' ), 10, 3 );
     184        add_filter( 'deprecated_argument_trigger_error', array( $this, 'disable_deprecated_errors' ) );
    181185        unset( $user->id );
    182186        $this->assertCount( 1, $this->_deprecated_errors );
    183187        $this->assertEquals( 'WP_User->id', $this->_deprecated_errors[0]['function'] );
    184188        $this->assertEquals( '2.1', $this->_deprecated_errors[0]['version'] );
     189        remove_filter( 'deprecated_argument_trigger_error', array( $this, 'disable_deprecated_errors' ) );
    185190        remove_action( 'deprecated_argument_run', array( $this, 'deprecated_handler' ), 10, 3);
    186191
     
    209214        $user = new WP_User( $user_id );
    210215
     216        add_filter( 'deprecated_argument_trigger_error', array( $this, 'disable_deprecated_errors' ) );
    211217        $this->assertTrue( isset( $user->id ) );
    212218        $this->assertEquals( $user->ID, $user->id );
    213219        $user->id = 1234;
    214220        $this->assertEquals( $user->ID, $user->id );
     221        remove_filter( 'deprecated_argument_trigger_error', array( $this, 'disable_deprecated_errors' ) );
    215222    }
    216223
     
    242249        $this->assertInstanceOf( 'WP_User', $user );
    243250        $this->assertEquals( $user_id, $user->ID );
    244 
     251       
    245252        $user2 = new WP_User( 0,  $user->user_login );
    246253        $this->assertInstanceOf( 'WP_User', $user2 );
Note: See TracChangeset for help on using the changeset viewer.