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/formatting/EscUrl.php

    r47122 r48937  
    1010     */
    1111    function test_spaces() {
    12         $this->assertEquals( 'http://example.com/Mr%20WordPress', esc_url( 'http://example.com/Mr WordPress' ) );
    13         $this->assertEquals( 'http://example.com/Mr%20WordPress', esc_url( 'http://example.com/Mr%20WordPress' ) );
    14         $this->assertEquals( 'http://example.com/Mr%20%20WordPress', esc_url( 'http://example.com/Mr%20%20WordPress' ) );
    15         $this->assertEquals( 'http://example.com/Mr+WordPress', esc_url( 'http://example.com/Mr+WordPress' ) );
    16         $this->assertEquals( 'http://example.com/Mr+WordPress', esc_url( ' http://example.com/Mr+WordPress' ) );
    17 
    18         $this->assertEquals( 'http://example.com/?foo=one%20two%20three&bar=four', esc_url( 'http://example.com/?foo=one two three&bar=four' ) );
    19         $this->assertEquals( 'http://example.com/?foo=one%20two%20three&bar=four', esc_url( 'http://example.com/?foo=one%20two%20three&bar=four' ) );
     12        $this->assertSame( 'http://example.com/Mr%20WordPress', esc_url( 'http://example.com/Mr WordPress' ) );
     13        $this->assertSame( 'http://example.com/Mr%20WordPress', esc_url( 'http://example.com/Mr%20WordPress' ) );
     14        $this->assertSame( 'http://example.com/Mr%20%20WordPress', esc_url( 'http://example.com/Mr%20%20WordPress' ) );
     15        $this->assertSame( 'http://example.com/Mr+WordPress', esc_url( 'http://example.com/Mr+WordPress' ) );
     16        $this->assertSame( 'http://example.com/Mr+WordPress', esc_url( ' http://example.com/Mr+WordPress' ) );
     17
     18        $this->assertSame( 'http://example.com/?foo=one%20two%20three&bar=four', esc_url( 'http://example.com/?foo=one two three&bar=four' ) );
     19        $this->assertSame( 'http://example.com/?foo=one%20two%20three&bar=four', esc_url( 'http://example.com/?foo=one%20two%20three&bar=four' ) );
    2020    }
    2121
    2222    function test_bad_characters() {
    23         $this->assertEquals( 'http://example.com/watchthelinefeedgo', esc_url( 'http://example.com/watchthelinefeed%0Ago' ) );
    24         $this->assertEquals( 'http://example.com/watchthelinefeedgo', esc_url( 'http://example.com/watchthelinefeed%0ago' ) );
    25         $this->assertEquals( 'http://example.com/watchthecarriagereturngo', esc_url( 'http://example.com/watchthecarriagereturn%0Dgo' ) );
    26         $this->assertEquals( 'http://example.com/watchthecarriagereturngo', esc_url( 'http://example.com/watchthecarriagereturn%0dgo' ) );
     23        $this->assertSame( 'http://example.com/watchthelinefeedgo', esc_url( 'http://example.com/watchthelinefeed%0Ago' ) );
     24        $this->assertSame( 'http://example.com/watchthelinefeedgo', esc_url( 'http://example.com/watchthelinefeed%0ago' ) );
     25        $this->assertSame( 'http://example.com/watchthecarriagereturngo', esc_url( 'http://example.com/watchthecarriagereturn%0Dgo' ) );
     26        $this->assertSame( 'http://example.com/watchthecarriagereturngo', esc_url( 'http://example.com/watchthecarriagereturn%0dgo' ) );
    2727        // Nesting checks.
    28         $this->assertEquals( 'http://example.com/watchthecarriagereturngo', esc_url( 'http://example.com/watchthecarriagereturn%0%0ddgo' ) );
    29         $this->assertEquals( 'http://example.com/watchthecarriagereturngo', esc_url( 'http://example.com/watchthecarriagereturn%0%0DDgo' ) );
    30         $this->assertEquals( 'http://example.com/', esc_url( 'http://example.com/%0%0%0DAD' ) );
    31         $this->assertEquals( 'http://example.com/', esc_url( 'http://example.com/%0%0%0ADA' ) );
    32         $this->assertEquals( 'http://example.com/', esc_url( 'http://example.com/%0%0%0DAd' ) );
    33         $this->assertEquals( 'http://example.com/', esc_url( 'http://example.com/%0%0%0ADa' ) );
     28        $this->assertSame( 'http://example.com/watchthecarriagereturngo', esc_url( 'http://example.com/watchthecarriagereturn%0%0ddgo' ) );
     29        $this->assertSame( 'http://example.com/watchthecarriagereturngo', esc_url( 'http://example.com/watchthecarriagereturn%0%0DDgo' ) );
     30        $this->assertSame( 'http://example.com/', esc_url( 'http://example.com/%0%0%0DAD' ) );
     31        $this->assertSame( 'http://example.com/', esc_url( 'http://example.com/%0%0%0ADA' ) );
     32        $this->assertSame( 'http://example.com/', esc_url( 'http://example.com/%0%0%0DAd' ) );
     33        $this->assertSame( 'http://example.com/', esc_url( 'http://example.com/%0%0%0ADa' ) );
    3434    }
    3535
    3636    function test_relative() {
    37         $this->assertEquals( '/example.php', esc_url( '/example.php' ) );
    38         $this->assertEquals( 'example.php', esc_url( 'example.php' ) );
    39         $this->assertEquals( '#fragment', esc_url( '#fragment' ) );
    40         $this->assertEquals( '?foo=bar', esc_url( '?foo=bar' ) );
     37        $this->assertSame( '/example.php', esc_url( '/example.php' ) );
     38        $this->assertSame( 'example.php', esc_url( 'example.php' ) );
     39        $this->assertSame( '#fragment', esc_url( '#fragment' ) );
     40        $this->assertSame( '?foo=bar', esc_url( '?foo=bar' ) );
    4141    }
    4242
     
    4444        $url = 'https://user:pass@host.example.com:1234/path;p=1?query=2&r[]=3#fragment';
    4545
    46         $this->assertEquals(
     46        $this->assertSame(
    4747            array(
    4848                'scheme'   => 'https',
     
    5757            parse_url( $url )
    5858        );
    59         $this->assertEquals( 'https://user:pass@host.example.com:1234/path;p=1?query=2&r%5B%5D=3#fragment', esc_url_raw( $url ) );
    60         $this->assertEquals( 'https://user:pass@host.example.com:1234/path;p=1?query=2&r%5B%5D=3#fragment', esc_url( $url ) );
     59        $this->assertSame( 'https://user:pass@host.example.com:1234/path;p=1?query=2&r%5B%5D=3#fragment', esc_url_raw( $url ) );
     60        $this->assertSame( 'https://user:pass@host.example.com:1234/path;p=1?query=2&r%5B%5D=3#fragment', esc_url( $url ) );
    6161    }
    6262
    6363    function test_bare() {
    64         $this->assertEquals( 'http://example.com?foo', esc_url( 'example.com?foo' ) );
    65         $this->assertEquals( 'http://example.com', esc_url( 'example.com' ) );
    66         $this->assertEquals( 'http://localhost', esc_url( 'localhost' ) );
    67         $this->assertEquals( 'http://example.com/foo', esc_url( 'example.com/foo' ) );
    68         $this->assertEquals( 'http://баба.org/баба', esc_url( 'баба.org/баба' ) );
     64        $this->assertSame( 'http://example.com?foo', esc_url( 'example.com?foo' ) );
     65        $this->assertSame( 'http://example.com', esc_url( 'example.com' ) );
     66        $this->assertSame( 'http://localhost', esc_url( 'localhost' ) );
     67        $this->assertSame( 'http://example.com/foo', esc_url( 'example.com/foo' ) );
     68        $this->assertSame( 'http://баба.org/баба', esc_url( 'баба.org/баба' ) );
    6969    }
    7070
    7171    function test_encoding() {
    72         $this->assertEquals( 'http://example.com?foo=1&bar=2', esc_url_raw( 'http://example.com?foo=1&bar=2' ) );
    73         $this->assertEquals( 'http://example.com?foo=1&bar=2', esc_url_raw( 'http://example.com?foo=1&bar=2' ) );
    74         $this->assertEquals( 'http://example.com?foo=1&bar=2', esc_url_raw( 'http://example.com?foo=1&bar=2' ) );
    75 
    76         $this->assertEquals( 'http://example.com?foo=1&bar=2', esc_url( 'http://example.com?foo=1&bar=2' ) );
    77         $this->assertEquals( 'http://example.com?foo=1&bar=2', esc_url( 'http://example.com?foo=1&bar=2' ) );
    78         $this->assertEquals( 'http://example.com?foo=1&bar=2', esc_url( 'http://example.com?foo=1&bar=2' ) );
     72        $this->assertSame( 'http://example.com?foo=1&bar=2', esc_url_raw( 'http://example.com?foo=1&bar=2' ) );
     73        $this->assertSame( 'http://example.com?foo=1&bar=2', esc_url_raw( 'http://example.com?foo=1&bar=2' ) );
     74        $this->assertSame( 'http://example.com?foo=1&bar=2', esc_url_raw( 'http://example.com?foo=1&bar=2' ) );
     75
     76        $this->assertSame( 'http://example.com?foo=1&bar=2', esc_url( 'http://example.com?foo=1&bar=2' ) );
     77        $this->assertSame( 'http://example.com?foo=1&bar=2', esc_url( 'http://example.com?foo=1&bar=2' ) );
     78        $this->assertSame( 'http://example.com?foo=1&bar=2', esc_url( 'http://example.com?foo=1&bar=2' ) );
    7979
    8080        $param = urlencode( 'http://example.com/?one=1&two=2' );
    81         $this->assertEquals( "http://example.com?url={$param}", esc_url( "http://example.com?url={$param}" ) );
     81        $this->assertSame( "http://example.com?url={$param}", esc_url( "http://example.com?url={$param}" ) );
    8282    }
    8383
    8484    function test_protocol() {
    85         $this->assertEquals( 'http://example.com', esc_url( 'http://example.com' ) );
    86         $this->assertEquals( '', esc_url( 'nasty://example.com/' ) );
    87         $this->assertEquals(
     85        $this->assertSame( 'http://example.com', esc_url( 'http://example.com' ) );
     86        $this->assertSame( '', esc_url( 'nasty://example.com/' ) );
     87        $this->assertSame(
    8888            '',
    8989            esc_url(
     
    9494            )
    9595        );
    96         $this->assertEquals(
     96        $this->assertSame(
    9797            '',
    9898            esc_url(
     
    103103            )
    104104        );
    105         $this->assertEquals(
     105        $this->assertSame(
    106106            'https://example.com',
    107107            esc_url(
     
    115115
    116116        foreach ( wp_allowed_protocols() as $scheme ) {
    117             $this->assertEquals( "{$scheme}://example.com", esc_url( "{$scheme}://example.com" ), $scheme );
    118             $this->assertEquals(
     117            $this->assertSame( "{$scheme}://example.com", esc_url( "{$scheme}://example.com" ), $scheme );
     118            $this->assertSame(
    119119                "{$scheme}://example.com",
    120120                esc_url(
     
    129129
    130130        $this->assertTrue( ! in_array( 'data', wp_allowed_protocols(), true ) );
    131         $this->assertEquals( '', esc_url( 'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D' ) );
     131        $this->assertSame( '', esc_url( 'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D' ) );
    132132
    133133        $this->assertTrue( ! in_array( 'foo', wp_allowed_protocols(), true ) );
    134         $this->assertEquals(
     134        $this->assertSame(
    135135            'foo://example.com',
    136136            esc_url(
     
    148148     */
    149149    function test_protocol_case() {
    150         $this->assertEquals( 'http://example.com', esc_url( 'HTTP://example.com' ) );
    151         $this->assertEquals( 'http://example.com', esc_url( 'Http://example.com' ) );
     150        $this->assertSame( 'http://example.com', esc_url( 'HTTP://example.com' ) );
     151        $this->assertSame( 'http://example.com', esc_url( 'Http://example.com' ) );
    152152    }
    153153
    154154    function test_display_extras() {
    155         $this->assertEquals( 'http://example.com/'quoted'', esc_url( 'http://example.com/\'quoted\'' ) );
    156         $this->assertEquals( 'http://example.com/\'quoted\'', esc_url( 'http://example.com/\'quoted\'', null, 'notdisplay' ) );
     155        $this->assertSame( 'http://example.com/'quoted'', esc_url( 'http://example.com/\'quoted\'' ) );
     156        $this->assertSame( 'http://example.com/\'quoted\'', esc_url( 'http://example.com/\'quoted\'', null, 'notdisplay' ) );
    157157    }
    158158
    159159    function test_non_ascii() {
    160         $this->assertEquals( 'http://example.org/баба', esc_url( 'http://example.org/баба' ) );
    161         $this->assertEquals( 'http://баба.org/баба', esc_url( 'http://баба.org/баба' ) );
    162         $this->assertEquals( 'http://müller.com/', esc_url( 'http://müller.com/' ) );
     160        $this->assertSame( 'http://example.org/баба', esc_url( 'http://example.org/баба' ) );
     161        $this->assertSame( 'http://баба.org/баба', esc_url( 'http://баба.org/баба' ) );
     162        $this->assertSame( 'http://müller.com/', esc_url( 'http://müller.com/' ) );
    163163    }
    164164
    165165    function test_feed() {
    166         $this->assertEquals( '', esc_url( 'feed:javascript:alert(1)' ) );
    167         $this->assertEquals( '', esc_url( 'feed:javascript:feed:alert(1)' ) );
    168         $this->assertEquals( '', esc_url( 'feed:feed:javascript:alert(1)' ) );
    169         $this->assertEquals( 'feed:feed:alert(1)', esc_url( 'feed:feed:alert(1)' ) );
    170         $this->assertEquals( 'feed:http://wordpress.org/feed/', esc_url( 'feed:http://wordpress.org/feed/' ) );
     166        $this->assertSame( '', esc_url( 'feed:javascript:alert(1)' ) );
     167        $this->assertSame( '', esc_url( 'feed:javascript:feed:alert(1)' ) );
     168        $this->assertSame( '', esc_url( 'feed:feed:javascript:alert(1)' ) );
     169        $this->assertSame( 'feed:feed:alert(1)', esc_url( 'feed:feed:alert(1)' ) );
     170        $this->assertSame( 'feed:http://wordpress.org/feed/', esc_url( 'feed:http://wordpress.org/feed/' ) );
    171171    }
    172172
     
    175175     */
    176176    function test_square_brackets() {
    177         $this->assertEquals( '/example.php?one%5B%5D=two', esc_url( '/example.php?one[]=two' ) );
    178         $this->assertEquals( '?foo%5Bbar%5D=baz', esc_url( '?foo[bar]=baz' ) );
    179         $this->assertEquals( '//example.com/?foo%5Bbar%5D=baz', esc_url( '//example.com/?foo[bar]=baz' ) );
    180         $this->assertEquals( 'http://example.com/?foo%5Bbar%5D=baz', esc_url( 'example.com/?foo[bar]=baz' ) );
    181         $this->assertEquals( 'http://localhost?foo%5Bbar%5D=baz', esc_url( 'localhost?foo[bar]=baz' ) );
    182         $this->assertEquals( 'http://example.com/?foo%5Bbar%5D=baz', esc_url( 'http://example.com/?foo[bar]=baz' ) );
    183         $this->assertEquals( 'http://example.com/?foo%5Bbar%5D=baz', esc_url( 'http://example.com/?foo%5Bbar%5D=baz' ) );
    184         $this->assertEquals( 'http://example.com/?baz=bar&foo%5Bbar%5D=baz', esc_url( 'http://example.com/?baz=bar&foo[bar]=baz' ) );
    185         $this->assertEquals( 'http://example.com/?baz=bar&foo%5Bbar%5D=baz', esc_url( 'http://example.com/?baz=bar&foo%5Bbar%5D=baz' ) );
     177        $this->assertSame( '/example.php?one%5B%5D=two', esc_url( '/example.php?one[]=two' ) );
     178        $this->assertSame( '?foo%5Bbar%5D=baz', esc_url( '?foo[bar]=baz' ) );
     179        $this->assertSame( '//example.com/?foo%5Bbar%5D=baz', esc_url( '//example.com/?foo[bar]=baz' ) );
     180        $this->assertSame( 'http://example.com/?foo%5Bbar%5D=baz', esc_url( 'example.com/?foo[bar]=baz' ) );
     181        $this->assertSame( 'http://localhost?foo%5Bbar%5D=baz', esc_url( 'localhost?foo[bar]=baz' ) );
     182        $this->assertSame( 'http://example.com/?foo%5Bbar%5D=baz', esc_url( 'http://example.com/?foo[bar]=baz' ) );
     183        $this->assertSame( 'http://example.com/?foo%5Bbar%5D=baz', esc_url( 'http://example.com/?foo%5Bbar%5D=baz' ) );
     184        $this->assertSame( 'http://example.com/?baz=bar&foo%5Bbar%5D=baz', esc_url( 'http://example.com/?baz=bar&foo[bar]=baz' ) );
     185        $this->assertSame( 'http://example.com/?baz=bar&foo%5Bbar%5D=baz', esc_url( 'http://example.com/?baz=bar&foo%5Bbar%5D=baz' ) );
    186186    }
    187187
     
    191191    function test_reserved_characters() {
    192192        $url = "http://example.com/:@-._~!$&'()*+,=;:@-._~!$&'()*+,=:@-._~!$&'()*+,==?/?:@-._~!$%27()*+,;=/?:@-._~!$%27()*+,;==#/?:@-._~!$&'()*+,;=";
    193         $this->assertEquals( $url, esc_url_raw( $url ) );
     193        $this->assertSame( $url, esc_url_raw( $url ) );
    194194    }
    195195
     
    198198     */
    199199    function test_protocol_relative_with_colon() {
    200         $this->assertEquals( '//example.com/foo?foo=abc:def', esc_url( '//example.com/foo?foo=abc:def' ) );
     200        $this->assertSame( '//example.com/foo?foo=abc:def', esc_url( '//example.com/foo?foo=abc:def' ) );
    201201    }
    202202
     
    213213        $email_link = 'mailto:?body=' . rawurlencode( $body );
    214214        $email_link = esc_url( $email_link );
    215         $this->assertEquals( 'mailto:?body=Hi%20there%2C%0A%0AI%20thought%20you%20might%20want%20to%20sign%20up%20for%20this%20newsletter', $email_link );
     215        $this->assertSame( 'mailto:?body=Hi%20there%2C%0A%0AI%20thought%20you%20might%20want%20to%20sign%20up%20for%20this%20newsletter', $email_link );
    216216    }
    217217
     
    228228        $email_link = 'http://example.com/mailto:?body=' . rawurlencode( $body );
    229229        $email_link = esc_url( $email_link );
    230         $this->assertEquals( 'http://example.com/mailto:?body=Hi%20there%2CI%20thought%20you%20might%20want%20to%20sign%20up%20for%20this%20newsletter', $email_link );
     230        $this->assertSame( 'http://example.com/mailto:?body=Hi%20there%2CI%20thought%20you%20might%20want%20to%20sign%20up%20for%20this%20newsletter', $email_link );
    231231    }
    232232
     
    239239        $email_link = 'mailto:?body=' . $body;
    240240        $email_link = esc_url( $email_link );
    241         $this->assertEquals( 'mailto:?body=Hi%20there,%20I%20thought%20you%20might%20want%20to%20sign%20up%20for%20this%20newsletter', $email_link );
     241        $this->assertSame( 'mailto:?body=Hi%20there,%20I%20thought%20you%20might%20want%20to%20sign%20up%20for%20this%20newsletter', $email_link );
    242242    }
    243243
     
    253253     */
    254254    function test_ipv6_hosts() {
    255         $this->assertEquals( '//[::127.0.0.1]', esc_url( '//[::127.0.0.1]' ) );
    256         $this->assertEquals( 'http://[::FFFF::127.0.0.1]', esc_url( 'http://[::FFFF::127.0.0.1]' ) );
    257         $this->assertEquals( 'http://[::127.0.0.1]', esc_url( 'http://[::127.0.0.1]' ) );
    258         $this->assertEquals( 'http://[::DEAD:BEEF:DEAD:BEEF:DEAD:BEEF:DEAD:BEEF]', esc_url( 'http://[::DEAD:BEEF:DEAD:BEEF:DEAD:BEEF:DEAD:BEEF]' ) );
     255        $this->assertSame( '//[::127.0.0.1]', esc_url( '//[::127.0.0.1]' ) );
     256        $this->assertSame( 'http://[::FFFF::127.0.0.1]', esc_url( 'http://[::FFFF::127.0.0.1]' ) );
     257        $this->assertSame( 'http://[::127.0.0.1]', esc_url( 'http://[::127.0.0.1]' ) );
     258        $this->assertSame( 'http://[::DEAD:BEEF:DEAD:BEEF:DEAD:BEEF:DEAD:BEEF]', esc_url( 'http://[::DEAD:BEEF:DEAD:BEEF:DEAD:BEEF:DEAD:BEEF]' ) );
    259259
    260260        // IPv6 with square brackets in the query? Why not.
    261         $this->assertEquals( '//[::FFFF::127.0.0.1]/?foo%5Bbar%5D=baz', esc_url( '//[::FFFF::127.0.0.1]/?foo[bar]=baz' ) );
    262         $this->assertEquals( 'http://[::FFFF::127.0.0.1]/?foo%5Bbar%5D=baz', esc_url( 'http://[::FFFF::127.0.0.1]/?foo[bar]=baz' ) );
     261        $this->assertSame( '//[::FFFF::127.0.0.1]/?foo%5Bbar%5D=baz', esc_url( '//[::FFFF::127.0.0.1]/?foo[bar]=baz' ) );
     262        $this->assertSame( 'http://[::FFFF::127.0.0.1]/?foo%5Bbar%5D=baz', esc_url( 'http://[::FFFF::127.0.0.1]/?foo[bar]=baz' ) );
    263263    }
    264264
Note: See TracChangeset for help on using the changeset viewer.