WordPress.org

Make WordPress Core


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

    r47357 r48937  
    4343     */
    4444    function test_capability_type_post_with_invalid_id() {
    45         $this->assertEquals(
     45        $this->assertSame(
    4646            array( 'do_not_allow' ),
    4747            map_meta_cap( 'edit_post', self::$user_id, self::$post_id + 1 )
     
    6161        $this->assertTrue( $post_type_object->map_meta_cap );
    6262
    63         $this->assertEquals(
     63        $this->assertSame(
    6464            array( 'edit_others_posts', 'edit_private_posts' ),
    6565            map_meta_cap( 'edit_post', self::$user_id, self::$post_id )
    6666        );
    67         $this->assertEquals(
     67        $this->assertSame(
    6868            array( 'edit_others_posts', 'edit_private_posts' ),
    6969            map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id )
    7070        );
    7171
    72         $this->assertEquals(
    73             array( 'read_private_posts' ),
    74             map_meta_cap( 'read_post', self::$user_id, self::$post_id )
    75         );
    76         $this->assertEquals(
    77             array( 'read_private_posts' ),
    78             map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id )
    79         );
    80 
    81         $this->assertEquals(
    82             array( 'delete_others_posts', 'delete_private_posts' ),
    83             map_meta_cap( 'delete_post', self::$user_id, self::$post_id )
    84         );
    85         $this->assertEquals(
     72        $this->assertSame(
     73            array( 'read_private_posts' ),
     74            map_meta_cap( 'read_post', self::$user_id, self::$post_id )
     75        );
     76        $this->assertSame(
     77            array( 'read_private_posts' ),
     78            map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id )
     79        );
     80
     81        $this->assertSame(
     82            array( 'delete_others_posts', 'delete_private_posts' ),
     83            map_meta_cap( 'delete_post', self::$user_id, self::$post_id )
     84        );
     85        $this->assertSame(
    8686            array( 'delete_others_posts', 'delete_private_posts' ),
    8787            map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id )
     
    100100        $post_type_object = get_post_type_object( self::$post_type );
    101101
    102         $this->assertEquals(
     102        $this->assertSame(
    103103            array( 'edit_others_books', 'edit_private_books' ),
    104104            map_meta_cap( 'edit_post', self::$user_id, self::$post_id )
    105105        );
    106         $this->assertEquals(
     106        $this->assertSame(
    107107            array( 'edit_others_books', 'edit_private_books' ),
    108108            map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id )
    109109        );
    110110
    111         $this->assertEquals(
     111        $this->assertSame(
    112112            array( 'read_private_books' ),
    113113            map_meta_cap( 'read_post', self::$user_id, self::$post_id )
    114114        );
    115         $this->assertEquals(
     115        $this->assertSame(
    116116            array( 'read_private_books' ),
    117117            map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id )
    118118        );
    119119
    120         $this->assertEquals(
     120        $this->assertSame(
    121121            array( 'delete_others_books', 'delete_private_books' ),
    122122            map_meta_cap( 'delete_post', self::$user_id, self::$post_id )
    123123        );
    124         $this->assertEquals(
     124        $this->assertSame(
    125125            array( 'delete_others_books', 'delete_private_books' ),
    126126            map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id )
     
    141141        $this->assertFalse( $post_type_object->map_meta_cap );
    142142
    143         $this->assertEquals(
     143        $this->assertSame(
    144144            array( 'edit_post' ),
    145145            map_meta_cap( 'edit_post', self::$user_id, self::$post_id )
    146146        );
    147         $this->assertEquals(
     147        $this->assertSame(
    148148            array( 'edit_post' ),
    149149            map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id )
    150150        );
    151151
    152         $this->assertEquals(
     152        $this->assertSame(
    153153            array( 'read_post' ),
    154154            map_meta_cap( 'read_post', self::$user_id, self::$post_id )
    155155        );
    156         $this->assertEquals(
     156        $this->assertSame(
    157157            array( 'read_post' ),
    158158            map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id )
    159159        );
    160160
    161         $this->assertEquals(
     161        $this->assertSame(
    162162            array( 'delete_post' ),
    163163            map_meta_cap( 'delete_post', self::$user_id, self::$post_id )
    164164        );
    165         $this->assertEquals(
     165        $this->assertSame(
    166166            array( 'delete_post' ),
    167167            map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id )
     
    186186        $this->assertTrue( $post_type_object->map_meta_cap );
    187187
    188         $this->assertEquals(
     188        $this->assertSame(
    189189            array( 'edit_others_books', 'edit_private_posts' ),
    190190            map_meta_cap( 'edit_post', self::$user_id, self::$post_id )
    191191        );
    192         $this->assertEquals(
     192        $this->assertSame(
    193193            array( 'edit_others_books', 'edit_private_posts' ),
    194194            map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id )
    195195        );
    196196
    197         $this->assertEquals(
    198             array( 'read_private_posts' ),
    199             map_meta_cap( 'read_post', self::$user_id, self::$post_id )
    200         );
    201         $this->assertEquals(
    202             array( 'read_private_posts' ),
    203             map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id )
    204         );
    205 
    206         $this->assertEquals(
    207             array( 'delete_others_posts', 'delete_private_posts' ),
    208             map_meta_cap( 'delete_post', self::$user_id, self::$post_id )
    209         );
    210         $this->assertEquals(
     197        $this->assertSame(
     198            array( 'read_private_posts' ),
     199            map_meta_cap( 'read_post', self::$user_id, self::$post_id )
     200        );
     201        $this->assertSame(
     202            array( 'read_private_posts' ),
     203            map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id )
     204        );
     205
     206        $this->assertSame(
     207            array( 'delete_others_posts', 'delete_private_posts' ),
     208            map_meta_cap( 'delete_post', self::$user_id, self::$post_id )
     209        );
     210        $this->assertSame(
    211211            array( 'delete_others_posts', 'delete_private_posts' ),
    212212            map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id )
     
    231231        $this->assertFalse( $post_type_object->map_meta_cap );
    232232
    233         $this->assertEquals(
     233        $this->assertSame(
    234234            array( 'edit_book' ),
    235235            map_meta_cap( 'edit_post', self::$user_id, self::$post_id )
    236236        );
    237         $this->assertEquals(
     237        $this->assertSame(
    238238            array( 'edit_book' ),
    239239            map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id )
    240240        );
    241241
    242         $this->assertEquals(
     242        $this->assertSame(
    243243            array( 'read_book' ),
    244244            map_meta_cap( 'read_post', self::$user_id, self::$post_id )
    245245        );
    246         $this->assertEquals(
     246        $this->assertSame(
    247247            array( 'read_book' ),
    248248            map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id )
    249249        );
    250250
    251         $this->assertEquals(
     251        $this->assertSame(
    252252            array( 'delete_book' ),
    253253            map_meta_cap( 'delete_post', self::$user_id, self::$post_id )
    254254        );
    255         $this->assertEquals(
     255        $this->assertSame(
    256256            array( 'delete_book' ),
    257257            map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id )
     
    277277        $this->assertTrue( $post_type_object->map_meta_cap );
    278278
    279         $this->assertEquals(
     279        $this->assertSame(
    280280            array( 'edit_others_posts', 'edit_private_posts' ),
    281281            map_meta_cap( 'edit_post', self::$user_id, self::$post_id )
    282282        );
    283         $this->assertEquals(
     283        $this->assertSame(
    284284            array( 'edit_others_posts', 'edit_private_posts' ),
    285285            map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id )
    286286        );
    287287
    288         $this->assertEquals(
    289             array( 'read_private_posts' ),
    290             map_meta_cap( 'read_post', self::$user_id, self::$post_id )
    291         );
    292         $this->assertEquals(
    293             array( 'read_private_posts' ),
    294             map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id )
    295         );
    296 
    297         $this->assertEquals(
    298             array( 'delete_others_posts', 'delete_private_posts' ),
    299             map_meta_cap( 'delete_post', self::$user_id, self::$post_id )
    300         );
    301         $this->assertEquals(
     288        $this->assertSame(
     289            array( 'read_private_posts' ),
     290            map_meta_cap( 'read_post', self::$user_id, self::$post_id )
     291        );
     292        $this->assertSame(
     293            array( 'read_private_posts' ),
     294            map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id )
     295        );
     296
     297        $this->assertSame(
     298            array( 'delete_others_posts', 'delete_private_posts' ),
     299            map_meta_cap( 'delete_post', self::$user_id, self::$post_id )
     300        );
     301        $this->assertSame(
    302302            array( 'delete_others_posts', 'delete_private_posts' ),
    303303            map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id )
     
    320320
    321321        $this->assertFalse( $post_type_object->map_meta_cap );
    322         $this->assertEquals( 'delete_posts', $post_type_object->cap->delete_posts );
     322        $this->assertSame( 'delete_posts', $post_type_object->cap->delete_posts );
    323323    }
    324324
     
    329329
    330330        if ( is_multisite() ) {
    331             $this->assertEquals( array( 'do_not_allow' ), map_meta_cap( 'unfiltered_html', 0 ) );
    332             $this->assertEquals( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', self::$user_id ) );
     331            $this->assertSame( array( 'do_not_allow' ), map_meta_cap( 'unfiltered_html', 0 ) );
     332            $this->assertSame( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', self::$user_id ) );
    333333        } else {
    334             $this->assertEquals( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', self::$user_id ) );
     334            $this->assertSame( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', self::$user_id ) );
    335335        }
    336336    }
     
    348348
    349349        $this->assertTrue( DISALLOW_UNFILTERED_HTML );
    350         $this->assertEquals( array( 'update_core' ), map_meta_cap( 'update_core', self::$user_id ) );
    351         $this->assertEquals( array( 'edit_plugins' ), map_meta_cap( 'edit_plugins', self::$user_id ) );
     350        $this->assertSame( array( 'update_core' ), map_meta_cap( 'update_core', self::$user_id ) );
     351        $this->assertSame( array( 'edit_plugins' ), map_meta_cap( 'edit_plugins', self::$user_id ) );
    352352    }
    353353
     
    367367        $editor  = self::factory()->user->create( array( 'role' => 'editor' ) );
    368368
    369         $this->assertEquals( array( 'edit_others_posts', 'edit_published_posts' ), map_meta_cap( 'edit_post', $editor, $post_id ) );
    370         $this->assertEquals( array( 'delete_others_posts', 'delete_published_posts' ), map_meta_cap( 'delete_post', $editor, $post_id ) );
     369        $this->assertSame( array( 'edit_others_posts', 'edit_published_posts' ), map_meta_cap( 'edit_post', $editor, $post_id ) );
     370        $this->assertSame( array( 'delete_others_posts', 'delete_published_posts' ), map_meta_cap( 'delete_post', $editor, $post_id ) );
    371371
    372372    }
     
    389389        delete_option( 'page_on_front' );
    390390
    391         $this->assertEquals( array( 'manage_options' ), $caps );
     391        $this->assertSame( array( 'manage_options' ), $caps );
    392392    }
    393393
     
    409409        delete_option( 'page_for_posts' );
    410410
    411         $this->assertEquals( array( 'manage_options' ), $caps );
     411        $this->assertSame( array( 'manage_options' ), $caps );
    412412    }
    413413}
Note: See TracChangeset for help on using the changeset viewer.