Make WordPress Core

Changeset 34380


Ignore:
Timestamp:
09/22/2015 04:59:35 AM (11 years ago)
Author:
wonderboymusic
Message:

Users: add __unset to WP_User.

Adds unit tests.

Props johnjamesjacoby, MikeHansenMe, wonderboymusic.
Fixes #20043.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-user.php

    r34379 r34380  
    326326
    327327        /**
     328         * Magic method for unsetting a certain custom field
     329         *
     330         * @since 4.4.0
     331         */
     332        function __unset( $key ) {
     333                if ( 'id' == $key ) {
     334                        _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
     335                        $key = 'ID';
     336                }
     337
     338                if ( isset( $this->data->$key ) ) {
     339                        unset( $this->data->$key );
     340                }
     341
     342                if ( isset( self::$back_compat_keys[ $key ] ) ) {
     343                        unset( self::$back_compat_keys[ $key ] );
     344                }
     345        }
     346
     347        /**
    328348         * Determine whether the user exists in the database.
    329349         *
  • trunk/tests/phpunit/tests/user.php

    r34218 r34380  
    161161                        $this->assertEquals( $value, $user->$key );
    162162                }
     163        }
     164
     165        /**
     166         * Test the magic __unset method
     167         *
     168         * @ticket 20043
     169         */
     170        public function test_user_unset() {
     171                // New user
     172                $user_id = $this->factory->user->create( array( 'role' => 'author' ) );
     173                $user = new WP_User( $user_id );
     174
     175                // Test custom fields
     176                $user->customField = 123;
     177                $this->assertEquals( $user->customField, 123 );
     178                unset( $user->customField );
     179                $this->assertFalse( isset( $user->customField ) );
     180                return $user;
     181        }
     182
     183        /**
     184         * @depends test_user_unset
     185         * @expectedDeprecated WP_User->id
     186         * @ticket 20043
     187         */
     188        function test_user_unset_lowercase_id( $user ) {
     189                // Test 'id' (lowercase)
     190                unset( $user->id );
     191                return $user;
     192        }
     193
     194        /**
     195         * @depends test_user_unset_lowercase_id
     196         * @ticket 20043
     197         */
     198        function test_user_unset_uppercase_id( $user ) {
     199                // Test 'ID'
     200                $this->assertNotEmpty( $user->ID );
     201                unset( $user->ID );
     202                $this->assertEmpty( $user->ID );
    163203        }
    164204
Note: See TracChangeset for help on using the changeset viewer.