Make WordPress Core

Changeset 31963


Ignore:
Timestamp:
04/01/2015 06:22:16 PM (10 years ago)
Author:
wonderboymusic
Message:

When updating the email address for an existing user, make sure the email address is not already in use.

Adds unit tests.

Props rittesh.patel, DrewAPicture.
Fixes #30647.

Location:
trunk
Files:
2 edited

Legend:

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

    r31883 r31963  
    18951895    $user_email = apply_filters( 'pre_user_email', $raw_user_email );
    18961896
    1897     if ( ! $update && ! defined( 'WP_IMPORTING' ) && email_exists( $user_email ) ) {
     1897    /*
     1898     * If there is no update, just check for `email_exists`. If there is an update,
     1899     * check if current email and new email are the same, or not, and check `email_exists`
     1900     * accordingly.
     1901     */
     1902    if ( ( ! $update || ( ! empty( $old_user_data ) && $user_email !== $old_user_data->user_email ) )
     1903        && ! defined( 'WP_IMPORTING' )
     1904        && email_exists( $user_email )
     1905    ) {
    18981906        return new WP_Error( 'existing_user_email', __( 'Sorry, that email address is already used!' ) );
    18991907    }
  • trunk/tests/phpunit/tests/user.php

    r30513 r31963  
    620620
    621621    /**
     622     * @ticket 30647
     623     */
     624    function test_user_update_email_error() {
     625        $id1 = wp_insert_user( array(
     626            'user_login' => rand_str(),
     627            'user_pass'  => 'password',
     628            'user_email' => 'blackburn@battlefield3.com',
     629        ) );
     630        $this->assertEquals( $id1, email_exists( 'blackburn@battlefield3.com' ) );
     631
     632        $id2 = wp_insert_user( array(
     633            'user_login' => rand_str(),
     634            'user_pass'  => 'password',
     635            'user_email' => 'miller@battlefield3.com',
     636        ) );
     637        $this->assertEquals( $id2, email_exists( 'miller@battlefield3.com' ) );
     638
     639        if( ! is_wp_error( $id2 ) ){   
     640            $return = wp_update_user( array(
     641                'ID'         => $id2,
     642                'user_email' => 'david@battlefield3.com',
     643            ) );
     644            $this->assertEquals( $id2, email_exists( 'david@battlefield3.com' ) );
     645
     646            $return = wp_update_user( array(
     647                'ID'         => $id2,
     648                'user_email' => 'blackburn@battlefield3.com',
     649            ) );
     650            if ( ! defined( 'WP_IMPORTING' ) ) {
     651                $this->assertWPError( $return );
     652            }           
     653        }
     654    }
     655
     656    /**
    622657     * @ticket 29696
    623658     */
Note: See TracChangeset for help on using the changeset viewer.