Make WordPress Core


Ignore:
Timestamp:
09/02/2020 12:35:36 AM (4 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/capabilities.php

    r48788 r48937  
    343343        sort( $single_primitive );
    344344        sort( $multi_primitive );
    345         $this->assertEquals( $single_primitive, $multi_primitive );
     345        $this->assertSame( $single_primitive, $multi_primitive );
    346346
    347347        $single_meta = array_keys( $this->_getSingleSiteMetaCaps() );
     
    349349        sort( $single_meta );
    350350        sort( $multi_meta );
    351         $this->assertEquals( $single_meta, $multi_meta );
     351        $this->assertSame( $single_meta, $multi_meta );
    352352    }
    353353
     
    379379            $diff = array_diff( array_keys( $user_caps ), array_keys( $caps ) );
    380380
    381             $this->assertEquals( array(), $diff, "User with {$role} role has capabilities that aren't being tested" );
     381            $this->assertSame( array(), $diff, "User with {$role} role has capabilities that aren't being tested" );
    382382
    383383        }
     
    522522
    523523            // Make sure the role name is correct.
    524             $this->assertEquals( array( $role ), $user->roles, "User should only have the {$role} role" );
     524            $this->assertSame( array( $role ), $user->roles, "User should only have the {$role} role" );
    525525
    526526            foreach ( $caps as $cap => $roles ) {
     
    762762
    763763        // Make sure the role name is correct.
    764         $this->assertEquals( array(), $user->roles, 'User should not have any roles' );
     764        $this->assertSame( array(), $user->roles, 'User should not have any roles' );
    765765
    766766        $caps = $this->getAllCapsAndRoles();
     
    784784
    785785        // User should have two roles now.
    786         $this->assertEquals( array( 'subscriber', 'contributor' ), $user->roles );
     786        $this->assertSame( array( 'subscriber', 'contributor' ), $user->roles );
    787787
    788788        $caps = $this->getAllCapsAndRoles();
     
    800800        $user->remove_role( 'contributor' );
    801801        // User should have one role now.
    802         $this->assertEquals( array( 'subscriber' ), $user->roles );
     802        $this->assertSame( array( 'subscriber' ), $user->roles );
    803803
    804804    }
     
    822822
    823823        // Make sure the role name is correct.
    824         $this->assertEquals( array( $role_name ), $user->roles );
     824        $this->assertSame( array( $role_name ), $user->roles );
    825825
    826826        $caps = $this->getAllCapsAndRoles();
     
    861861
    862862        // Make sure the role name is correct.
    863         $this->assertEquals( array( $role_name ), $user->roles );
     863        $this->assertSame( array( $role_name ), $user->roles );
    864864
    865865        $caps = $this->getPrimitiveCapsAndRoles();
     
    902902        $user = new WP_User( $id );
    903903        $this->assertTrue( $user->exists(), "Problem getting user $id" );
    904         $this->assertEquals( array( $role_name ), $user->roles );
     904        $this->assertSame( array( $role_name ), $user->roles );
    905905
    906906        // The user should have all the above caps.
     
    952952        $user = new WP_User( $id );
    953953        $this->assertTrue( $user->exists(), "Problem getting user $id" );
    954         $this->assertEquals( array( $role_name ), $user->roles );
     954        $this->assertSame( array( $role_name ), $user->roles );
    955955
    956956        // The user should have all the above caps.
     
    989989
    990990        // Make sure they're both still contributors.
    991         $this->assertEquals( array( 'contributor' ), $user_1->roles );
    992         $this->assertEquals( array( 'contributor' ), $user_2->roles );
     991        $this->assertSame( array( 'contributor' ), $user_1->roles );
     992        $this->assertSame( array( 'contributor' ), $user_2->roles );
    993993
    994994        // Check the extra cap on both users.
     
    10311031
    10321032        // Make sure they're both still contributors.
    1033         $this->assertEquals( array( 'contributor' ), $user_1->roles );
    1034         $this->assertEquals( array( 'contributor' ), $user_2->roles );
     1033        $this->assertSame( array( 'contributor' ), $user_1->roles );
     1034        $this->assertSame( array( 'contributor' ), $user_2->roles );
    10351035
    10361036        // Check the removed cap on both users.
     
    10541054        // They get promoted to editor - level should get bumped to 7.
    10551055        $user->set_role( 'editor' );
    1056         $this->assertEquals( 7, $user->user_level );
     1056        $this->assertSame( 7, $user->user_level );
    10571057
    10581058        // Demoted to contributor - level is reduced to 1.
    10591059        $user->set_role( 'contributor' );
    1060         $this->assertEquals( 1, $user->user_level );
     1060        $this->assertSame( 1, $user->user_level );
    10611061
    10621062        // If they have two roles, user_level should be the max of the two.
    10631063        $user->add_role( 'editor' );
    1064         $this->assertEquals( array( 'contributor', 'editor' ), $user->roles );
    1065         $this->assertEquals( 7, $user->user_level );
     1064        $this->assertSame( array( 'contributor', 'editor' ), $user->roles );
     1065        $this->assertSame( 7, $user->user_level );
    10661066    }
    10671067
     
    11541154        register_post_type( 'something', array( 'capabilities' => array( 'edit_posts' => 'draw_somethings' ) ) );
    11551155        $something = get_post_type_object( 'something' );
    1156         $this->assertEquals( 'draw_somethings', $something->cap->edit_posts );
    1157         $this->assertEquals( 'draw_somethings', $something->cap->create_posts );
     1156        $this->assertSame( 'draw_somethings', $something->cap->edit_posts );
     1157        $this->assertSame( 'draw_somethings', $something->cap->create_posts );
    11581158
    11591159        register_post_type(
     
    11681168        );
    11691169        $something = get_post_type_object( 'something' );
    1170         $this->assertEquals( 'draw_somethings', $something->cap->edit_posts );
    1171         $this->assertEquals( 'create_somethings', $something->cap->create_posts );
     1170        $this->assertSame( 'draw_somethings', $something->cap->edit_posts );
     1171        $this->assertSame( 'create_somethings', $something->cap->create_posts );
    11721172        _unregister_post_type( 'something' );
    11731173
     
    12301230        $this->assertFalse( $contributor->has_cap( 'edit_post', $post ) );
    12311231        $this->assertFalse( $contributor->has_cap( 'delete_post', $post ) );
    1232         $this->assertEquals( 'publish' === $status, $contributor->has_cap( 'read_post', $post ) );
     1232        $this->assertSame( 'publish' === $status, $contributor->has_cap( 'read_post', $post ) );
    12331233    }
    12341234
     
    12531253        $cap = get_post_type_object( 'foobar' )->cap;
    12541254
    1255         $this->assertEquals( 'edit_posts', $cap->create_posts );
     1255        $this->assertSame( 'edit_posts', $cap->create_posts );
    12561256
    12571257        $this->assertTrue( $admin->has_cap( $cap->create_posts ) );
     
    12671267        $cap = get_post_type_object( 'foobar' )->cap;
    12681268
    1269         $this->assertEquals( 'edit_foobars', $cap->create_posts );
     1269        $this->assertSame( 'edit_foobars', $cap->create_posts );
    12701270
    12711271        $this->assertFalse( $admin->has_cap( $cap->create_posts ) );
     
    12891289
    12901290        $cap = get_post_type_object( 'attachment' )->cap;
    1291         $this->assertEquals( 'upload_files', $cap->create_posts );
    1292         $this->assertEquals( 'edit_posts', $cap->edit_posts );
     1291        $this->assertSame( 'upload_files', $cap->create_posts );
     1292        $this->assertSame( 'edit_posts', $cap->edit_posts );
    12931293
    12941294        $this->assertTrue( $author->has_cap( $cap->create_posts ) );
     
    13631363        foreach ( $expected as $meta_cap => $primitive_cap ) {
    13641364            $caps = map_meta_cap( $tax->cap->$meta_cap, $user->ID );
    1365             $this->assertEquals(
     1365            $this->assertSame(
    13661366                array(
    13671367                    $primitive_cap,
     
    14721472        foreach ( $expected as $meta_cap => $primitive_cap ) {
    14731473            $caps = map_meta_cap( $tax->cap->$meta_cap, $user->ID );
    1474             $this->assertEquals(
     1474            $this->assertSame(
    14751475                array(
    14761476                    $primitive_cap,
     
    15271527        $user->set_role( 'administrator' );
    15281528        $this->assertNotEmpty( $user->caps );
    1529         $this->assertEquals( $caps, $user->caps );
     1529        $this->assertSame( $caps, $user->caps );
    15301530    }
    15311531
     
    15681568        current_user_can_for_blog( $blog_id, 'edit_posts' );
    15691569
    1570         $this->assertEquals( $orig_blog_id, get_current_blog_id() );
     1570        $this->assertSame( $orig_blog_id, get_current_blog_id() );
    15711571    }
    15721572
     
    18151815        $contributor = self::$users['contributor'];
    18161816
    1817         $this->assertEquals( 'edit_pages', $cpt->cap->edit_posts );
     1817        $this->assertSame( 'edit_pages', $cpt->cap->edit_posts );
    18181818        $this->assertTrue( user_can( $admin->ID, $cpt->cap->edit_posts ) );
    18191819        $this->assertTrue( user_can( $editor->ID, $cpt->cap->edit_posts ) );
     
    18751875        wp_logout();
    18761876
    1877         $this->assertEquals( 0, get_current_user_id() );
     1877        $this->assertSame( 0, get_current_user_id() );
    18781878
    18791879    }
     
    21932193    function test_block_caps( $role, $cap, $use_post, $expected ) {
    21942194        if ( $use_post ) {
    2195             $this->assertEquals( $expected, self::$users[ $role ]->has_cap( $cap, self::$block_id ) );
     2195            $this->assertSame( $expected, self::$users[ $role ]->has_cap( $cap, self::$block_id ) );
    21962196        } else {
    2197             $this->assertEquals( $expected, self::$users[ $role ]->has_cap( $cap ) );
     2197            $this->assertSame( $expected, self::$users[ $role ]->has_cap( $cap ) );
    21982198        }
    21992199    }
Note: See TracChangeset for help on using the changeset viewer.