WordPress.org

Make WordPress Core

Changeset 35248


Ignore:
Timestamp:
10/17/2015 09:18:38 PM (5 years ago)
Author:
wonderboymusic
Message:

Unit Tests: better fixtures for Tests_User_WpSetCurrentUser.

See #30017, #33968.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/user/wpSetCurrentUser.php

    r35242 r35248  
    55 */
    66class Tests_User_WpSetCurrentUser extends WP_UnitTestCase {
     7    protected static $user_id;
     8    protected static $user_id2;
     9    protected static $user_ids = array();
     10
     11    public static function wpSetUpBeforeClass( $factory ) {
     12        self::$user_ids[] = self::$user_id = $factory->user->create();
     13        self::$user_ids[] = self::$user_id2 = $factory->user->create( array( 'user_login' => 'foo', ) );
     14    }
     15
     16    public static function wpTearDownAfterClass() {
     17        foreach ( self::$user_ids as $id ) {
     18            self::delete_user( $id );
     19        }
     20    }
     21
    722    public function test_set_by_id() {
    8         $u = self::factory()->user->create();
     23        $user = wp_set_current_user( self::$user_id );
    924
    10         $user = wp_set_current_user( $u );
    11 
    12         $this->assertSame( $u, $user->ID );
     25        $this->assertSame( self::$user_id, $user->ID );
    1326        $this->assertEquals( $user, wp_get_current_user() );
    14         $this->assertSame( $u, get_current_user_id() );
     27        $this->assertSame( self::$user_id, get_current_user_id() );
    1528    }
    1629
    1730    public function test_name_should_be_ignored_if_id_is_not_null() {
    18         $u = self::factory()->user->create();
     31        $user = wp_set_current_user( self::$user_id, 'foo' );
    1932
    20         $user = wp_set_current_user( $u, 'foo' );
    21 
    22         $this->assertSame( $u, $user->ID );
     33        $this->assertSame( self::$user_id, $user->ID );
    2334        $this->assertEquals( $user, wp_get_current_user() );
    24         $this->assertSame( $u, get_current_user_id() );
     35        $this->assertSame( self::$user_id, get_current_user_id() );
    2536    }
    2637
    2738    public function test_should_set_by_name_if_id_is_null_and_current_user_is_nonempty() {
    28         $u1 = self::factory()->user->create();
    29         wp_set_current_user( $u1 );
    30         $this->assertSame( $u1, get_current_user_id() );
    31 
    32         $u2 = self::factory()->user->create( array(
    33             'user_login' => 'foo',
    34         ) );
     39        wp_set_current_user( self::$user_id );
     40        $this->assertSame( self::$user_id, get_current_user_id() );
    3541
    3642        $user = wp_set_current_user( null, 'foo' );
    3743
    38         $this->assertSame( $u2, $user->ID );
     44        $this->assertSame( self::$user_id2, $user->ID );
    3945        $this->assertEquals( $user, wp_get_current_user() );
    40         $this->assertSame( $u2, get_current_user_id() );
     46        $this->assertSame( self::$user_id2, get_current_user_id() );
    4147    }
    4248
     
    5056        $this->assertSame( 0, get_current_user_id() );
    5157
    52         $u = self::factory()->user->create( array(
    53             'user_login' => 'foo',
    54         ) );
    55 
    5658        $user = wp_set_current_user( null, 'foo' );
    5759
    58         $this->assertSame( $u, $user->ID );
     60        $this->assertSame( self::$user_id2, $user->ID );
    5961        $this->assertEquals( $user, wp_get_current_user() );
    60         $this->assertSame( $u, get_current_user_id() );
     62        $this->assertSame( self::$user_id2, get_current_user_id() );
    6163    }
    6264}
Note: See TracChangeset for help on using the changeset viewer.