Make WordPress Core


Ignore:
Timestamp:
09/02/2020 12:35:36 AM (5 years ago)
Author:
SergeyBiryukov
Message:

Tests: First pass at using assertSame() instead of assertEquals() in most of the unit tests.

This ensures that not only the return values match the expected results, but also that their type is the same.

Going forward, stricter type checking by using assertSame() should generally be preferred to assertEquals() where appropriate, to make the tests more reliable.

Props johnbillion, jrf, SergeyBiryukov.
See #38266.

File:
1 edited

Legend:

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

    r48100 r48937  
    102102        // Set and get.
    103103        update_user_option( self::$author_id, $key, $val );
    104         $this->assertEquals( $val, get_user_option( $key, self::$author_id ) );
     104        $this->assertSame( $val, get_user_option( $key, self::$author_id ) );
    105105
    106106        // Change and get again.
    107107        $val2 = rand_str();
    108108        update_user_option( self::$author_id, $key, $val2 );
    109         $this->assertEquals( $val2, get_user_option( $key, self::$author_id ) );
     109        $this->assertSame( $val2, get_user_option( $key, self::$author_id ) );
    110110    }
    111111
     
    118118
    119119        // Get a meta key that doesn't exist.
    120         $this->assertEquals( '', get_user_meta( self::$author_id, $key, true ) );
     120        $this->assertSame( '', get_user_meta( self::$author_id, $key, true ) );
    121121
    122122        // Set and get.
    123123        update_user_meta( self::$author_id, $key, $val );
    124         $this->assertEquals( $val, get_user_meta( self::$author_id, $key, true ) );
     124        $this->assertSame( $val, get_user_meta( self::$author_id, $key, true ) );
    125125
    126126        // Change and get again.
    127127        $val2 = 'value2';
    128128        update_user_meta( self::$author_id, $key, $val2 );
    129         $this->assertEquals( $val2, get_user_meta( self::$author_id, $key, true ) );
     129        $this->assertSame( $val2, get_user_meta( self::$author_id, $key, true ) );
    130130
    131131        // Delete and get.
    132132        delete_user_meta( self::$author_id, $key );
    133         $this->assertEquals( '', get_user_meta( self::$author_id, $key, true ) );
     133        $this->assertSame( '', get_user_meta( self::$author_id, $key, true ) );
    134134
    135135        // Delete by key AND value.
     
    137137        // Incorrect key: key still exists.
    138138        delete_user_meta( self::$author_id, $key, rand_str() );
    139         $this->assertEquals( $val, get_user_meta( self::$author_id, $key, true ) );
     139        $this->assertSame( $val, get_user_meta( self::$author_id, $key, true ) );
    140140        // Correct key: deleted.
    141141        delete_user_meta( self::$author_id, $key, $val );
    142         $this->assertEquals( '', get_user_meta( self::$author_id, $key, true ) );
     142        $this->assertSame( '', get_user_meta( self::$author_id, $key, true ) );
    143143
    144144    }
     
    198198
    199199        $user->$key = 'foo';
    200         $this->assertEquals( 'foo', $user->$key );
    201         $this->assertEquals( 'foo', $user->data->$key );  // This will fail with WP < 3.3.
     200        $this->assertSame( 'foo', $user->$key );
     201        $this->assertSame( 'foo', $user->data->$key );  // This will fail with WP < 3.3.
    202202
    203203        foreach ( get_object_vars( $user ) as $key => $value ) {
    204             $this->assertEquals( $value, $user->$key );
     204            $this->assertSame( $value, $user->$key );
    205205        }
    206206    }
     
    217217        // Test custom fields.
    218218        $user->customField = 123;
    219         $this->assertEquals( $user->customField, 123 );
     219        $this->assertSame( $user->customField, 123 );
    220220        unset( $user->customField );
    221221        $this->assertFalse( isset( $user->customField ) );
     
    260260        $this->assertTrue( isset( $user->foo ) );
    261261
    262         $this->assertEquals( 'foo', $user->foo );
     262        $this->assertSame( 'foo', $user->foo );
    263263    }
    264264
     
    270270
    271271        $this->assertTrue( isset( $user->id ) );
    272         $this->assertEquals( $user->ID, $user->id );
     272        $this->assertSame( $user->ID, $user->id );
    273273        $user->id = 1234;
    274         $this->assertEquals( $user->ID, $user->id );
     274        $this->assertSame( $user->ID, $user->id );
    275275    }
    276276
     
    298298        $user = new WP_User( self::$author_id );
    299299        $this->assertInstanceOf( 'WP_User', $user );
    300         $this->assertEquals( self::$author_id, $user->ID );
     300        $this->assertSame( self::$author_id, $user->ID );
    301301
    302302        $user2 = new WP_User( 0, $user->user_login );
    303303        $this->assertInstanceOf( 'WP_User', $user2 );
    304         $this->assertEquals( self::$author_id, $user2->ID );
    305         $this->assertEquals( $user->user_login, $user2->user_login );
     304        $this->assertSame( self::$author_id, $user2->ID );
     305        $this->assertSame( $user->user_login, $user2->user_login );
    306306
    307307        $user3 = new WP_User();
    308308        $this->assertInstanceOf( 'WP_User', $user3 );
    309         $this->assertEquals( 0, $user3->ID );
     309        $this->assertSame( 0, $user3->ID );
    310310        $this->assertFalse( isset( $user3->user_login ) );
    311311
    312312        $user3->init( $user->data );
    313         $this->assertEquals( self::$author_id, $user3->ID );
     313        $this->assertSame( self::$author_id, $user3->ID );
    314314
    315315        $user4 = new WP_User( $user->user_login );
    316316        $this->assertInstanceOf( 'WP_User', $user4 );
    317         $this->assertEquals( self::$author_id, $user4->ID );
    318         $this->assertEquals( $user->user_login, $user4->user_login );
     317        $this->assertSame( self::$author_id, $user4->ID );
     318        $this->assertSame( $user->user_login, $user4->user_login );
    319319
    320320        $user5 = new WP_User( null, $user->user_login );
    321321        $this->assertInstanceOf( 'WP_User', $user5 );
    322         $this->assertEquals( self::$author_id, $user5->ID );
    323         $this->assertEquals( $user->user_login, $user5->user_login );
     322        $this->assertSame( self::$author_id, $user5->ID );
     323        $this->assertSame( $user->user_login, $user5->user_login );
    324324
    325325        $user6 = new WP_User( $user );
    326326        $this->assertInstanceOf( 'WP_User', $user6 );
    327         $this->assertEquals( self::$author_id, $user6->ID );
    328         $this->assertEquals( $user->user_login, $user6->user_login );
     327        $this->assertSame( self::$author_id, $user6->ID );
     328        $this->assertSame( $user->user_login, $user6->user_login );
    329329
    330330        $user7 = new WP_User( $user->data );
    331331        $this->assertInstanceOf( 'WP_User', $user7 );
    332         $this->assertEquals( self::$author_id, $user7->ID );
    333         $this->assertEquals( $user->user_login, $user7->user_login );
     332        $this->assertSame( self::$author_id, $user7->ID );
     333        $this->assertSame( $user->user_login, $user7->user_login );
    334334    }
    335335
    336336    function test_get() {
    337337        $user = new WP_User( self::$author_id );
    338         $this->assertEquals( 'author_login', $user->get( 'user_login' ) );
    339         $this->assertEquals( 'author@email.com', $user->get( 'user_email' ) );
     338        $this->assertSame( 'author_login', $user->get( 'user_login' ) );
     339        $this->assertSame( 'author@email.com', $user->get( 'user_email' ) );
    340340        $this->assertEquals( 0, $user->get( 'use_ssl' ) );
    341         $this->assertEquals( '', $user->get( 'field_that_does_not_exist' ) );
     341        $this->assertSame( '', $user->get( 'field_that_does_not_exist' ) );
    342342
    343343        update_user_meta( self::$author_id, 'dashed-key', 'abcdefg' );
    344         $this->assertEquals( 'abcdefg', $user->get( 'dashed-key' ) );
     344        $this->assertSame( 'abcdefg', $user->get( 'dashed-key' ) );
    345345    }
    346346
     
    359359
    360360        update_user_meta( self::$author_id, 'description', 'about me' );
    361         $this->assertEquals( 'about me', $user->get( 'description' ) );
     361        $this->assertSame( 'about me', $user->get( 'description' ) );
    362362
    363363        $user_data = array(
     
    368368
    369369        $user = new WP_User( self::$author_id );
    370         $this->assertEquals( 'test user', $user->get( 'display_name' ) );
     370        $this->assertSame( 'test user', $user->get( 'display_name' ) );
    371371
    372372        // Make sure there is no collateral damage to fields not in $user_data.
    373         $this->assertEquals( 'about me', $user->get( 'description' ) );
     373        $this->assertSame( 'about me', $user->get( 'description' ) );
    374374
    375375        // Pass as stdClass.
     
    381381
    382382        $user = new WP_User( self::$author_id );
    383         $this->assertEquals( 'a test user', $user->get( 'display_name' ) );
     383        $this->assertSame( 'a test user', $user->get( 'display_name' ) );
    384384
    385385        $user->display_name = 'some test user';
    386386        wp_update_user( $user );
    387387
    388         $this->assertEquals( 'some test user', $user->get( 'display_name' ) );
     388        $this->assertSame( 'some test user', $user->get( 'display_name' ) );
    389389
    390390        // Test update of fields in _get_additional_user_keys().
     
    420420        $this->assertNotEmpty( $userdata );
    421421        $this->assertInstanceOf( 'WP_User', $userdata );
    422         $this->assertEquals( $userdata->ID, self::$sub_id );
     422        $this->assertSame( $userdata->ID, self::$sub_id );
    423423        $prefix  = $wpdb->get_blog_prefix();
    424424        $cap_key = $prefix . 'capabilities';
     
    472472        $this->assertNotEmpty( $authordata );
    473473        $this->assertInstanceOf( 'WP_User', $authordata );
    474         $this->assertEquals( $authordata->ID, self::$author_id );
     474        $this->assertSame( $authordata->ID, self::$author_id );
    475475
    476476        if ( $old_post_id ) {
     
    499499        // @ticket 23480
    500500        $user1 = WP_User::get_data_by( 'id', -1 );
    501         $this->assertEquals( false, $user1 );
     501        $this->assertFalse( $user1 );
    502502
    503503        $user2 = WP_User::get_data_by( 'id', 0 );
    504         $this->assertEquals( false, $user2 );
     504        $this->assertFalse( $user2 );
    505505
    506506        $user3 = WP_User::get_data_by( 'id', null );
    507         $this->assertEquals( false, $user3 );
     507        $this->assertFalse( $user3 );
    508508
    509509        $user4 = WP_User::get_data_by( 'id', '' );
    510         $this->assertEquals( false, $user4 );
     510        $this->assertFalse( $user4 );
    511511
    512512        $user5 = WP_User::get_data_by( 'id', false );
    513         $this->assertEquals( false, $user5 );
     513        $this->assertFalse( $user5 );
    514514
    515515        $user6 = WP_User::get_data_by( 'id', $user->user_nicename );
    516         $this->assertEquals( false, $user6 );
     516        $this->assertFalse( $user6 );
    517517
    518518        $user7 = WP_User::get_data_by( 'id', 99999 );
    519         $this->assertEquals( false, $user7 );
     519        $this->assertFalse( $user7 );
    520520    }
    521521
     
    586586        // Reload the data.
    587587        $pwd_after = get_userdata( $testuserid )->user_pass;
    588         $this->assertEquals( $pwd_before, $pwd_after );
     588        $this->assertSame( $pwd_before, $pwd_after );
    589589    }
    590590
     
    625625            )
    626626        );
    627         $this->assertEquals( $id1, email_exists( 'taco@burrito.com' ) );
     627        $this->assertSame( $id1, email_exists( 'taco@burrito.com' ) );
    628628
    629629        $id2 = wp_insert_user(
     
    656656            )
    657657        );
    658         $this->assertEquals( $id1, email_exists( 'blackburn@battlefield4.com' ) );
     658        $this->assertSame( $id1, email_exists( 'blackburn@battlefield4.com' ) );
    659659
    660660        $id2 = wp_insert_user(
     
    665665            )
    666666        );
    667         $this->assertEquals( $id2, email_exists( 'miller@battlefield4.com' ) );
     667        $this->assertSame( $id2, email_exists( 'miller@battlefield4.com' ) );
    668668
    669669        if ( ! is_wp_error( $id2 ) ) {
     
    674674                )
    675675            );
    676             $this->assertEquals( $id2, email_exists( 'david@battlefield4.com' ) );
     676            $this->assertSame( $id2, email_exists( 'david@battlefield4.com' ) );
    677677
    678678            $return = wp_update_user(
     
    704704        $response = wp_insert_user( $user_data );
    705705        $this->assertInstanceOf( 'WP_Error', $response );
    706         $this->assertEquals( 'invalid_username', $response->get_error_code() );
     706        $this->assertSame( 'invalid_username', $response->get_error_code() );
    707707
    708708        remove_filter( 'illegal_user_logins', array( $this, '_illegal_user_logins' ) );
     
    724724        $response = register_new_user( $user_login, $user_email );
    725725        $this->assertInstanceOf( 'WP_Error', $response );
    726         $this->assertEquals( 'invalid_username', $response->get_error_code() );
     726        $this->assertSame( 'invalid_username', $response->get_error_code() );
    727727
    728728        remove_filter( 'illegal_user_logins', array( $this, '_illegal_user_logins' ) );
     
    747747        $response = wpmu_validate_user_signup( $user_data['user_login'], $user_data['user_email'] );
    748748        $this->assertInstanceOf( 'WP_Error', $response['errors'] );
    749         $this->assertEquals( 'user_name', $response['errors']->get_error_code() );
     749        $this->assertSame( 'user_name', $response['errors']->get_error_code() );
    750750
    751751        remove_filter( 'illegal_user_logins', array( $this, '_illegal_user_logins' ) );
     
    753753        $response = wpmu_validate_user_signup( $user_data['user_login'], $user_data['user_email'] );
    754754        $this->assertInstanceOf( 'WP_Error', $response['errors'] );
    755         $this->assertEquals( 0, count( $response['errors']->get_error_codes() ) );
     755        $this->assertSame( 0, count( $response['errors']->get_error_codes() ) );
    756756    }
    757757
     
    816816
    817817        $user_id = wp_insert_user( $user_details );
    818         $this->assertEquals( $user_id, email_exists( $user_details['user_email'] ) );
     818        $this->assertSame( $user_id, email_exists( $user_details['user_email'] ) );
    819819
    820820        // Check that providing an empty password doesn't remove a user's password.
     
    10451045
    10461046        $user = get_userdata( $user->ID );
    1047         $this->assertEquals( 'key', $user->user_activation_key );
     1047        $this->assertSame( 'key', $user->user_activation_key );
    10481048
    10491049        // Check that changing something other than the email doesn't remove the key.
     
    10551055
    10561056        $user = get_userdata( $user->ID );
    1057         $this->assertEquals( 'key', $user->user_activation_key );
     1057        $this->assertSame( 'key', $user->user_activation_key );
    10581058
    10591059        // Now check that changing the email does remove it.
     
    10771077
    10781078        $user = get_userdata( $user->ID );
    1079         $this->assertEquals( 'key', $user->user_activation_key );
     1079        $this->assertSame( 'key', $user->user_activation_key );
    10801080
    10811081        $userdata = array(
     
    11551155        $update   = wp_update_user( $userdata );
    11561156
    1157         $this->assertEquals( self::$editor_id, $update );
     1157        $this->assertSame( self::$editor_id, $update );
    11581158    }
    11591159
     
    11701170
    11711171        // Was this successful?
    1172         $this->assertEquals( self::$editor_id, $update );
     1172        $this->assertSame( self::$editor_id, $update );
    11731173
    11741174        // Verify that the email address has been updated.
    11751175        $user = get_userdata( self::$editor_id );
    1176         $this->assertEquals( $user->user_email, 'test2@test.com' );
     1176        $this->assertSame( $user->user_email, 'test2@test.com' );
    11771177    }
    11781178
     
    14691469
    14701470        $this->assertInstanceOf( 'WP_Error', $response );
    1471         $this->assertEquals( 'pass', $response->get_error_code() );
     1471        $this->assertSame( 'pass', $response->get_error_code() );
    14721472
    14731473        // Check new user with password set.
     
    14801480        $this->assertInternalType( 'int', $user_id );
    14811481        $this->assertInstanceOf( 'WP_User', $user );
    1482         $this->assertEquals( 'nickname1', $user->nickname );
     1482        $this->assertSame( 'nickname1', $user->nickname );
    14831483
    14841484        // Check updating user with empty password.
     
    14901490
    14911491        $this->assertInternalType( 'int', $user_id );
    1492         $this->assertEquals( 'nickname_updated', $user->nickname );
     1492        $this->assertSame( 'nickname_updated', $user->nickname );
    14931493
    14941494        // Check updating user with missing second password.
     
    15001500
    15011501        $this->assertInstanceOf( 'WP_Error', $response );
    1502         $this->assertEquals( 'pass', $response->get_error_code() );
    1503         $this->assertEquals( 'nickname_updated', $user->nickname );
     1502        $this->assertSame( 'pass', $response->get_error_code() );
     1503        $this->assertSame( 'nickname_updated', $user->nickname );
    15041504
    15051505        // Check updating user with empty password via `check_passwords` action.
     
    15091509
    15101510        $this->assertInternalType( 'int', $user_id );
    1511         $this->assertEquals( 'nickname_updated2', $user->nickname );
     1511        $this->assertSame( 'nickname_updated2', $user->nickname );
    15121512    }
    15131513
     
    15481548        // The new email address gets put into user_meta.
    15491549        $new_email_meta = get_user_meta( $user->ID, '_new_email', true );
    1550         $this->assertEquals( 'after@example.com', $new_email_meta['newemail'] );
     1550        $this->assertSame( 'after@example.com', $new_email_meta['newemail'] );
    15511551
    15521552        // The email address of the user doesn't change. $_POST['email'] should be the email address pre-update.
    1553         $this->assertEquals( $_POST['email'], $user->user_email );
     1553        $this->assertSame( $_POST['email'], $user->user_email );
    15541554    }
    15551555
     
    15881588
    15891589        // $_POST['email'] should be the email address posted from the form.
    1590         $this->assertEquals( $_POST['email'], 'after@example.com' );
     1590        $this->assertSame( $_POST['email'], 'after@example.com' );
    15911591    }
    15921592
     
    17241724
    17251725        // Contains 'Community Events Location'.
    1726         $this->assertEquals( 'Community Events Location', $actual['data'][1]['group_label'] );
     1726        $this->assertSame( 'Community Events Location', $actual['data'][1]['group_label'] );
    17271727
    17281728        // Contains location IP.
    1729         $this->assertEquals( 'IP', $actual['data'][1]['data'][0]['name'] );
    1730         $this->assertEquals( '0.0.0.0', $actual['data'][1]['data'][0]['value'] );
     1729        $this->assertSame( 'IP', $actual['data'][1]['data'][0]['name'] );
     1730        $this->assertSame( '0.0.0.0', $actual['data'][1]['data'][0]['value'] );
    17311731    }
    17321732
     
    17531753
    17541754        // Contains 'Community Events Location'.
    1755         $this->assertEquals( 'Community Events Location', $actual['data'][1]['group_label'] );
     1755        $this->assertSame( 'Community Events Location', $actual['data'][1]['group_label'] );
    17561756
    17571757        // Contains location city.
    1758         $this->assertEquals( 'City', $actual['data'][1]['data'][0]['name'] );
    1759         $this->assertEquals( 'Cincinnati', $actual['data'][1]['data'][0]['value'] );
     1758        $this->assertSame( 'City', $actual['data'][1]['data'][0]['name'] );
     1759        $this->assertSame( 'Cincinnati', $actual['data'][1]['data'][0]['value'] );
    17601760
    17611761        // Contains location country.
    1762         $this->assertEquals( 'Country', $actual['data'][1]['data'][1]['name'] );
    1763         $this->assertEquals( 'US', $actual['data'][1]['data'][1]['value'] );
     1762        $this->assertSame( 'Country', $actual['data'][1]['data'][1]['name'] );
     1763        $this->assertSame( 'US', $actual['data'][1]['data'][1]['value'] );
    17641764
    17651765        // Contains location latitude.
    1766         $this->assertEquals( 'Latitude', $actual['data'][1]['data'][2]['name'] );
    1767         $this->assertEquals( '39.1271100', $actual['data'][1]['data'][2]['value'] );
     1766        $this->assertSame( 'Latitude', $actual['data'][1]['data'][2]['name'] );
     1767        $this->assertSame( '39.1271100', $actual['data'][1]['data'][2]['value'] );
    17681768
    17691769        // Contains location longitude.
    1770         $this->assertEquals( 'Longitude', $actual['data'][1]['data'][3]['name'] );
    1771         $this->assertEquals( '-84.5143900', $actual['data'][1]['data'][3]['value'] );
     1770        $this->assertSame( 'Longitude', $actual['data'][1]['data'][3]['name'] );
     1771        $this->assertSame( '-84.5143900', $actual['data'][1]['data'][3]['value'] );
    17721772
    17731773    }
     
    17971797
    17981798        // Contains Session Tokens.
    1799         $this->assertEquals( 'Session Tokens', $actual['data'][1]['group_label'] );
     1799        $this->assertSame( 'Session Tokens', $actual['data'][1]['group_label'] );
    18001800
    18011801        // Contains Expiration.
    1802         $this->assertEquals( 'Expiration', $actual['data'][1]['data'][0]['name'] );
    1803         $this->assertEquals( 'January 31, 2020 09:13 AM', $actual['data'][1]['data'][0]['value'] );
     1802        $this->assertSame( 'Expiration', $actual['data'][1]['data'][0]['name'] );
     1803        $this->assertSame( 'January 31, 2020 09:13 AM', $actual['data'][1]['data'][0]['value'] );
    18041804
    18051805        // Contains IP.
    1806         $this->assertEquals( 'IP', $actual['data'][1]['data'][1]['name'] );
    1807         $this->assertEquals( '0.0.0.0', $actual['data'][1]['data'][1]['value'] );
     1806        $this->assertSame( 'IP', $actual['data'][1]['data'][1]['name'] );
     1807        $this->assertSame( '0.0.0.0', $actual['data'][1]['data'][1]['value'] );
    18081808
    18091809        // Contains IP.
    1810         $this->assertEquals( 'User Agent', $actual['data'][1]['data'][2]['name'] );
    1811         $this->assertEquals( 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36', $actual['data'][1]['data'][2]['value'] );
     1810        $this->assertSame( 'User Agent', $actual['data'][1]['data'][2]['name'] );
     1811        $this->assertSame( 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36', $actual['data'][1]['data'][2]['value'] );
    18121812
    18131813        // Contains IP.
    1814         $this->assertEquals( 'Last Login', $actual['data'][1]['data'][3]['name'] );
    1815         $this->assertEquals( 'January 29, 2020 09:13 AM', $actual['data'][1]['data'][3]['value'] );
     1814        $this->assertSame( 'Last Login', $actual['data'][1]['data'][3]['name'] );
     1815        $this->assertSame( 'January 29, 2020 09:13 AM', $actual['data'][1]['data'][3]['value'] );
    18161816    }
    18171817
Note: See TracChangeset for help on using the changeset viewer.