Make WordPress Core

Ticket #20845: 20845-1.2.patch

File 20845-1.2.patch, 1.5 KB (added by bobbingwide, 9 years ago)

Refreshed version with a unit test

  • src/wp-includes/pluggable.php

     
    2626function wp_set_current_user($id, $name = '') {
    2727        global $current_user;
    2828
    29         if ( isset( $current_user ) && ( $current_user instanceof WP_User ) && ( $id == $current_user->ID ) )
     29        if ( isset( $current_user ) && ( $current_user instanceof WP_User ) && ( $id === $current_user->ID ) )
    3030                return $current_user;
    3131
    3232        $current_user = new WP_User( $id, $name );
  • tests/phpunit/tests/user/wpSetCurrentUser.php

     
     1<?php
     2
     3/**
     4 * @group user
     5 */
     6class Tests_User_WpSetCurrentUser extends WP_UnitTestCase {
     7
     8        /**
     9         * Test that you can set the current user by the name parameter
     10         *
     11         * Initially we expect the current user to not be set - giving ID=0
     12         * Trying to set it to "admin" should return ID=1
     13         *
     14         * @ticket 20845
     15         */
     16        function test_wp_set_current_user_by_name() {
     17                $user = wp_get_current_user();
     18                $this->assertEquals( $user->ID, 0 );
     19                $admin_user = wp_set_current_user( null, "admin" );
     20                $this->assertEquals( $admin_user->ID, 1 );
     21                $this->assertEquals( $admin_user->user_login, "admin" );
     22        }
     23       
     24}
     25