WordPress.org

Make WordPress Core

Changeset 35247


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

Unit Tests: better fixtures for Tests_User.

See #30017, #33968.

File:
1 edited

Legend:

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

    r35245 r35247  
    66 */
    77class Tests_User extends WP_UnitTestCase {
    8     protected static $test_id;
     8    protected static $admin_id;
     9    protected static $editor_id;
    910    protected static $author_id;
     11    protected static $contrib_id;
     12    protected static $sub_id;
     13
     14    protected static $user_ids = array();
     15
    1016    protected static $_author;
    1117    protected $author;
     
    1319
    1420    public static function wpSetUpBeforeClass( $factory ) {
    15         self::$test_id = $factory->user->create( array(
     21        self::$user_ids[] = self::$contrib_id = $factory->user->create( array(
    1622            'user_login' => 'user1',
    1723            'user_nicename' => 'userone',
     
    2127            'display_name' => 'John Doe',
    2228            'user_email' => 'blackburn@battlefield3.com',
    23             'user_url' => 'http://tacos.com'
    24         ) );
    25         self::$author_id = $factory->user->create( array( 'role' => 'author' ) );
     29            'user_url' => 'http://tacos.com',
     30            'role' => 'contributor'
     31        ) );
     32
     33        self::$user_ids[] = self::$author_id = $factory->user->create( array(
     34            'user_login' => 'author_login',
     35            'user_email' => 'author@email.com',
     36            'role' => 'author'
     37        ) );
     38
     39        self::$user_ids[] = self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
     40        self::$user_ids[] = self::$editor_id = $factory->user->create( array(
     41            'role' => 'editor',
     42            'user_email' => 'test@test.com',
     43        ) );
     44        self::$user_ids[] = self::$sub_id = $factory->user->create( array( 'role' => 'subscriber' ) );
     45
    2646        self::$_author = get_user_by( 'ID', self::$author_id );
    2747    }
    2848
    2949    public static function wpTearDownAfterClass() {
    30         $ids = array( self::$test_id, self::$author_id );
    31         foreach ( $ids as $id ) {
     50        foreach ( self::$user_ids as $id ) {
    3251            self::delete_user( $id );
    3352        }
     
    4261    function test_get_users_of_blog() {
    4362        // add one of each user role
    44         $nusers = array();
    45         foreach ( array('administrator', 'editor', 'author', 'contributor', 'subscriber' ) as $role ) {
    46             $id = self::factory()->user->create( array( 'role' => $role ) );
    47             $nusers[ $id ] = $id;
    48         }
     63        $nusers = array(
     64            self::$contrib_id,
     65            self::$author_id,
     66            self::$admin_id,
     67            self::$editor_id,
     68            self::$sub_id,
     69        );
    4970
    5071        $user_list = get_users();
     
    5475        foreach ( $user_list as $user ) {
    5576            // only include the users we just created - there might be some others that existed previously
    56             if ( isset( $nusers[$user->ID] ) ) {
    57                 $found[ $user->ID] = $user->ID;
     77            if ( in_array( $user->ID, $nusers ) ) {
     78                $found[] = $user->ID;
    5879            }
    5980        }
    6081
    6182        // make sure every user we created was returned
    62         $this->assertEquals($nusers, $found);
     83        $this->assertEqualSets( $nusers, $found );
    6384    }
    6485
     
    237258    function test_user_level_property_back_compat() {
    238259        $roles = array(
    239             'administrator' => 10,
    240             'editor' => 7,
    241             'author' => 2,
    242             'contributor' => 1,
    243             'subscriber' => 0,
    244         );
    245 
    246         foreach ( $roles as $role => $level ) {
    247             $user_id = self::factory()->user->create( array( 'role' => $role ) );
     260            self::$admin_id => 10,
     261            self::$editor_id => 7,
     262            self::$author_id => 2,
     263            self::$contrib_id => 1,
     264            self::$sub_id => 0,
     265        );
     266
     267        foreach ( $roles as $user_id => $level ) {
    248268            $user = new WP_User( $user_id );
    249269
     
    293313
    294314    function test_get() {
    295         $user_id = self::factory()->user->create( array(
    296             'role' => 'author',
    297             'user_login' => 'test_wp_user_get',
    298             'user_pass' => 'password',
    299             'user_email' => 'test@test.com',
    300         ) );
    301 
    302         $user = new WP_User( $user_id );
    303         $this->assertEquals( 'test_wp_user_get', $user->get( 'user_login' ) );
    304         $this->assertEquals( 'test@test.com', $user->get( 'user_email' ) );
     315        $user = new WP_User( self::$author_id );
     316        $this->assertEquals( 'author_login', $user->get( 'user_login' ) );
     317        $this->assertEquals( 'author@email.com', $user->get( 'user_email' ) );
    305318        $this->assertEquals( 0, $user->get( 'use_ssl' ) );
    306319        $this->assertEquals( '', $user->get( 'field_that_does_not_exist' ) );
    307320
    308         update_user_meta( $user_id, 'dashed-key', 'abcdefg' );
     321        update_user_meta( self::$author_id, 'dashed-key', 'abcdefg' );
    309322        $this->assertEquals( 'abcdefg', $user->get( 'dashed-key' ) );
    310323    }
    311324
    312325    function test_has_prop() {
    313         $user_id = self::factory()->user->create( array(
    314             'role' => 'author',
    315             'user_login' => 'test_wp_user_has_prop',
    316             'user_pass' => 'password',
    317             'user_email' => 'test2@test.com',
    318         ) );
    319 
    320         $user = new WP_User( $user_id );
     326        $user = new WP_User( self::$author_id );
    321327        $this->assertTrue( $user->has_prop( 'user_email') );
    322328        $this->assertTrue( $user->has_prop( 'use_ssl' ) );
    323329        $this->assertFalse( $user->has_prop( 'field_that_does_not_exist' ) );
    324330
    325         update_user_meta( $user_id, 'dashed-key', 'abcdefg' );
     331        update_user_meta( self::$author_id, 'dashed-key', 'abcdefg' );
    326332        $this->assertTrue( $user->has_prop( 'dashed-key' ) );
    327333    }
    328334
    329335    function test_update_user() {
    330         $user_id = self::factory()->user->create( array(
    331             'role' => 'author',
    332             'user_login' => 'test_wp_update_user',
    333             'user_pass' => 'password',
    334             'user_email' => 'test3@test.com',
    335         ) );
    336         $user = new WP_User( $user_id );
    337 
    338         update_user_meta( $user_id, 'description', 'about me' );
     336        $user = new WP_User( self::$author_id );
     337
     338        update_user_meta( self::$author_id, 'description', 'about me' );
    339339        $this->assertEquals( 'about me', $user->get( 'description' ) );
    340340
    341         $user_data = array( 'ID' => $user_id, 'display_name' => 'test user' );
     341        $user_data = array( 'ID' => self::$author_id, 'display_name' => 'test user' );
    342342        wp_update_user( $user_data );
    343343
    344         $user = new WP_User( $user_id );
     344        $user = new WP_User( self::$author_id );
    345345        $this->assertEquals( 'test user', $user->get( 'display_name' ) );
    346346
     
    349349
    350350        // Pass as stdClass
    351         $user_data = array( 'ID' => $user_id, 'display_name' => 'a test user' );
     351        $user_data = array( 'ID' => self::$author_id, 'display_name' => 'a test user' );
    352352        wp_update_user( (object) $user_data );
    353353
    354         $user = new WP_User( $user_id );
     354        $user = new WP_User( self::$author_id );
    355355        $this->assertEquals( 'a test user', $user->get( 'display_name' ) );
    356356
    357         // Pass as WP_User
    358         $user = new WP_User( $user_id );
    359357        $user->display_name = 'some test user';
    360358        wp_update_user( $user );
    361359
    362         $user = new WP_User( $user_id );
    363360        $this->assertEquals( 'some test user', $user->get( 'display_name' ) );
    364361
    365362        // Test update of fields in _get_additional_user_keys()
    366363        $user_data = array(
    367             'ID' => $user_id, 'use_ssl' => 1, 'show_admin_bar_front' => 1,
     364            'ID' => self::$author_id, 'use_ssl' => 1, 'show_admin_bar_front' => 1,
    368365            'rich_editing' => 1, 'first_name' => 'first', 'last_name' => 'last',
    369366            'nickname' => 'nick', 'comment_shortcuts' => 'true', 'admin_color' => 'classic',
     
    372369        wp_update_user( $user_data );
    373370
    374         $user = new WP_User( $user_id );
     371        $user = new WP_User( self::$author_id );
    375372        foreach ( $user_data as $key => $value ) {
    376373            $this->assertEquals( $value, $user->get( $key ), $key );
     
    384381        global $userdata, $wpdb;
    385382
    386         $user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) );
    387         wp_set_current_user( $user_id );
     383        wp_set_current_user( self::$sub_id );
    388384
    389385        $this->assertNotEmpty( $userdata );
    390386        $this->assertInstanceOf( 'WP_User', $userdata );
    391         $this->assertEquals( $userdata->ID, $user_id );
     387        $this->assertEquals( $userdata->ID, self::$sub_id );
    392388        $prefix = $wpdb->get_blog_prefix();
    393389        $cap_key = $prefix . 'capabilities';
     
    498494     */
    499495    function test_count_many_users_posts() {
    500         $user_id_a = self::factory()->user->create( array( 'role' => 'author' ) );
    501496        $user_id_b = self::factory()->user->create( array( 'role' => 'author' ) );
    502         $post_id_a = self::factory()->post->create( array( 'post_author' => $user_id_a ) );
     497        $post_id_a = self::factory()->post->create( array( 'post_author' => self::$author_id ) );
    503498        $post_id_b = self::factory()->post->create( array( 'post_author' => $user_id_b ) );
    504499        $post_id_c = self::factory()->post->create( array( 'post_author' => $user_id_b, 'post_status' => 'private' ) );
    505500
    506         wp_set_current_user( $user_id_a );
    507         $counts = count_many_users_posts( array( $user_id_a, $user_id_b), 'post', false );
    508         $this->assertEquals( 1, $counts[$user_id_a] );
     501        wp_set_current_user( self::$author_id );
     502        $counts = count_many_users_posts( array( self::$author_id, $user_id_b ), 'post', false );
     503        $this->assertEquals( 1, $counts[self::$author_id] );
    509504        $this->assertEquals( 1, $counts[$user_id_b] );
    510505
    511         $counts = count_many_users_posts( array( $user_id_a, $user_id_b), 'post', true );
    512         $this->assertEquals( 1, $counts[$user_id_a] );
     506        $counts = count_many_users_posts( array( self::$author_id, $user_id_b ), 'post', true );
     507        $this->assertEquals( 1, $counts[self::$author_id] );
    513508        $this->assertEquals( 1, $counts[$user_id_b] );
    514509
    515510        wp_set_current_user( $user_id_b );
    516         $counts = count_many_users_posts( array( $user_id_a, $user_id_b), 'post', false );
    517         $this->assertEquals( 1, $counts[$user_id_a] );
     511        $counts = count_many_users_posts( array( self::$author_id, $user_id_b ), 'post', false );
     512        $this->assertEquals( 1, $counts[self::$author_id] );
    518513        $this->assertEquals( 2, $counts[$user_id_b] );
    519514
    520         $counts = count_many_users_posts( array( $user_id_a, $user_id_b), 'post', true );
    521         $this->assertEquals( 1, $counts[$user_id_a] );
     515        $counts = count_many_users_posts( array( self::$author_id, $user_id_b ), 'post', true );
     516        $this->assertEquals( 1, $counts[self::$author_id] );
    522517        $this->assertEquals( 1, $counts[$user_id_b] );
    523518    }
     
    860855        $users = get_users( array( 'search' => 'user1', 'fields' => 'ID' ) );
    861856
    862         $this->assertTrue( in_array( self::$test_id, $users ) );
     857        $this->assertTrue( in_array( self::$contrib_id, $users ) );
    863858    }
    864859
     
    866861        $users = get_users( array( 'search' => '*tacos*', 'fields' => 'ID' ) );
    867862
    868         $this->assertTrue( in_array( self::$test_id, $users ) );
     863        $this->assertTrue( in_array( self::$contrib_id, $users ) );
    869864    }
    870865
     
    872867        $users = get_users( array( 'search' => '*battle*', 'fields' => 'ID' ) );
    873868
    874         $this->assertTrue( in_array( self::$test_id, $users ) );
     869        $this->assertTrue( in_array( self::$contrib_id, $users ) );
    875870    }
    876871
     
    878873        $users = get_users( array( 'search' => '*one*', 'fields' => 'ID' ) );
    879874
    880         $this->assertTrue( in_array( self::$test_id, $users ) );
     875        $this->assertTrue( in_array( self::$contrib_id, $users ) );
    881876    }
    882877
     
    884879        $users = get_users( array( 'search' => '*Doe*', 'fields' => 'ID' ) );
    885880
    886         $this->assertTrue( in_array( self::$test_id, $users ) );
     881        $this->assertTrue( in_array( self::$contrib_id, $users ) );
    887882    }
    888883
     
    891886     */
    892887    function test_email_case() {
    893         // Create a test user with a lower-case email address.
    894         $user_id = self::factory()->user->create( array(
    895             'user_email' => 'test@test.com',
    896         ) );
    897 
    898888        // Alter the case of the email address (which stays the same).
    899889        $userdata = array(
    900             'ID' => $user_id,
     890            'ID' => self::$editor_id,
    901891            'user_email' => 'test@TEST.com',
    902892        );
    903893        $update = wp_update_user( $userdata );
    904894
    905         $this->assertEquals( $user_id, $update );
     895        $this->assertEquals( self::$editor_id, $update );
    906896    }
    907897
     
    910900     */
    911901    function test_email_change() {
    912         // Create a test user.
    913         $user_id = self::factory()->user->create( array(
    914             'user_email' => 'test@test.com',
    915         ) );
    916 
    917902        // Change the email address.
    918903        $userdata = array(
    919             'ID' => $user_id,
     904            'ID' => self::$editor_id,
    920905            'user_email' => 'test2@test.com',
    921906        );
     
    923908
    924909        // Was this successful?
    925         $this->assertEquals( $user_id, $update );
     910        $this->assertEquals( self::$editor_id, $update );
    926911
    927912        // Verify that the email address has been updated.
    928         $user = get_userdata( $user_id );
     913        $user = get_userdata( self::$editor_id );
    929914        $this->assertEquals( $user->user_email, 'test2@test.com' );
    930915    }
     
    942927        $was_user_email_sent = false;
    943928
    944         wp_new_user_notification( self::$test_id, null, $notify );
     929        wp_new_user_notification( self::$contrib_id, null, $notify );
    945930
    946931        /*
     
    999984     */
    1000985    function test_wp_new_user_notification_old_signature_throws_deprecated_warning() {
    1001         $user = self::factory()->user->create( array(
    1002             'role'       => 'author',
    1003             'user_login' => 'test_wp_new_user_notification',
    1004             'user_pass'  => 'password',
    1005             'user_email' => 'test@test.com',
    1006         ) );
    1007 
    1008         wp_new_user_notification( $user, 'this_is_deprecated' );
     986        wp_new_user_notification( self::$author_id, 'this_is_deprecated' );
    1009987    }
    1010988
Note: See TracChangeset for help on using the changeset viewer.