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/kses.php

    r48744 r48937  
    3030                $string        = "<address $name='$value'>1 WordPress Avenue, The Internet.</address>";
    3131                $expect_string = "<address $name='" . str_replace( '; ', ';', trim( $value, ';' ) ) . "'>1 WordPress Avenue, The Internet.</address>";
    32                 $this->assertEquals( $expect_string, wp_kses( $string, $allowedposttags ) );
     32                $this->assertSame( $expect_string, wp_kses( $string, $allowedposttags ) );
    3333            }
    3434        }
     
    6464            $string        = "<a $attr>I link this</a>";
    6565            $expect_string = "<a $expected_attr>I link this</a>";
    66             $this->assertEquals( $expect_string, wp_kses( $string, $allowedposttags ) );
     66            $this->assertSame( $expect_string, wp_kses( $string, $allowedposttags ) );
    6767        }
    6868    }
     
    138138            $string        = "<abbr $name='$value'>WP</abbr>";
    139139            $expect_string = "<abbr $name='" . trim( $value, ';' ) . "'>WP</abbr>";
    140             $this->assertEquals( $expect_string, wp_kses( $string, $allowedposttags ) );
     140            $this->assertSame( $expect_string, wp_kses( $string, $allowedposttags ) );
    141141        }
    142142    }
     
    173173EOF;
    174174
    175         $this->assertEquals( $expected, wp_kses( $content, $allowedposttags ) );
     175        $this->assertSame( $expected, wp_kses( $content, $allowedposttags ) );
    176176    }
    177177
     
    212212                switch ( $k ) {
    213213                    case 6:
    214                         $this->assertEquals( 'javascript&amp;#0000058alert(1);', $result );
     214                        $this->assertSame( 'javascript&amp;#0000058alert(1);', $result );
    215215                        break;
    216216                    case 12:
    217                         $this->assertEquals( str_replace( '&', '&amp;', $x ), $result );
     217                        $this->assertSame( str_replace( '&', '&amp;', $x ), $result );
    218218                        break;
    219219                    case 22:
    220                         $this->assertEquals( 'javascript&amp;#0000058alert(1);', $result );
     220                        $this->assertSame( 'javascript&amp;#0000058alert(1);', $result );
    221221                        break;
    222222                    case 23:
    223                         $this->assertEquals( 'javascript&amp;#0000058alert(1)//?:', $result );
     223                        $this->assertSame( 'javascript&amp;#0000058alert(1)//?:', $result );
    224224                        break;
    225225                    case 24:
    226                         $this->assertEquals( 'feed:alert(1)', $result );
     226                        $this->assertSame( 'feed:alert(1)', $result );
    227227                        break;
    228228                    case 26:
    229                         $this->assertEquals( 'javascript&amp;#58alert(1)', $result );
     229                        $this->assertSame( 'javascript&amp;#58alert(1)', $result );
    230230                        break;
    231231                    case 27:
    232                         $this->assertEquals( 'javascript&amp;#x3ax=1;alert(1)', $result );
     232                        $this->assertSame( 'javascript&amp;#x3ax=1;alert(1)', $result );
    233233                        break;
    234234                    default:
     
    305305            switch ( $attack->name ) {
    306306                case 'XSS Locator':
    307                     $this->assertEquals( '\';alert(String.fromCharCode(88,83,83))//\\\';alert(String.fromCharCode(88,83,83))//";alert(String.fromCharCode(88,83,83))//\\";alert(String.fromCharCode(88,83,83))//--&gt;"&gt;\'&gt;alert(String.fromCharCode(88,83,83))=&amp;{}', $result );
     307                    $this->assertSame( '\';alert(String.fromCharCode(88,83,83))//\\\';alert(String.fromCharCode(88,83,83))//";alert(String.fromCharCode(88,83,83))//\\";alert(String.fromCharCode(88,83,83))//--&gt;"&gt;\'&gt;alert(String.fromCharCode(88,83,83))=&amp;{}', $result );
    308308                    break;
    309309                case 'XSS Quick Test':
    310                     $this->assertEquals( '\'\';!--"=&amp;{()}', $result );
     310                    $this->assertSame( '\'\';!--"=&amp;{()}', $result );
    311311                    break;
    312312                case 'SCRIPT w/Alert()':
    313                     $this->assertEquals( "alert('XSS')", $result );
     313                    $this->assertSame( "alert('XSS')", $result );
    314314                    break;
    315315                case 'SCRIPT w/Char Code':
    316                     $this->assertEquals( 'alert(String.fromCharCode(88,83,83))', $result );
     316                    $this->assertSame( 'alert(String.fromCharCode(88,83,83))', $result );
    317317                    break;
    318318                case 'IMG STYLE w/expression':
    319                     $this->assertEquals( 'exp/*', $result );
     319                    $this->assertSame( 'exp/*', $result );
    320320                    break;
    321321                case 'List-style-image':
    322                     $this->assertEquals( 'li {list-style-image: url("javascript:alert(\'XSS\')");}XSS', $result );
     322                    $this->assertSame( 'li {list-style-image: url("javascript:alert(\'XSS\')");}XSS', $result );
    323323                    break;
    324324                case 'STYLE':
    325                     $this->assertEquals( "alert('XSS');", $result );
     325                    $this->assertSame( "alert('XSS');", $result );
    326326                    break;
    327327                case 'STYLE w/background-image':
    328                     $this->assertEquals( '.XSS{background-image:url("javascript:alert(\'XSS\')");}<A></A>', $result );
     328                    $this->assertSame( '.XSS{background-image:url("javascript:alert(\'XSS\')");}<A></A>', $result );
    329329                    break;
    330330                case 'STYLE w/background':
    331                     $this->assertEquals( 'BODY{background:url("javascript:alert(\'XSS\')")}', $result );
     331                    $this->assertSame( 'BODY{background:url("javascript:alert(\'XSS\')")}', $result );
    332332                    break;
    333333                case 'Remote Stylesheet 2':
    334                     $this->assertEquals( "@import'http://ha.ckers.org/xss.css';", $result );
     334                    $this->assertSame( "@import'http://ha.ckers.org/xss.css';", $result );
    335335                    break;
    336336                case 'Remote Stylesheet 3':
    337                     $this->assertEquals( '&lt;META HTTP-EQUIV=&quot;Link&quot; Content=&quot;; REL=stylesheet"&gt;', $result );
     337                    $this->assertSame( '&lt;META HTTP-EQUIV=&quot;Link&quot; Content=&quot;; REL=stylesheet"&gt;', $result );
    338338                    break;
    339339                case 'Remote Stylesheet 4':
    340                     $this->assertEquals( 'BODY{-moz-binding:url("http://ha.ckers.org/xssmoz.xml#xss")}', $result );
     340                    $this->assertSame( 'BODY{-moz-binding:url("http://ha.ckers.org/xssmoz.xml#xss")}', $result );
    341341                    break;
    342342                case 'XML data island w/CDATA':
    343                     $this->assertEquals( '&lt;![CDATA[]]&gt;', $result );
     343                    $this->assertSame( '&lt;![CDATA[]]&gt;', $result );
    344344                    break;
    345345                case 'XML data island w/comment':
    346                     $this->assertEquals( "<I><B>&lt;IMG SRC=&quot;javas<!-- -->cript:alert('XSS')\"&gt;</B></I>", $result );
     346                    $this->assertSame( "<I><B>&lt;IMG SRC=&quot;javas<!-- -->cript:alert('XSS')\"&gt;</B></I>", $result );
    347347                    break;
    348348                case 'XML HTML+TIME':
    349                     $this->assertEquals( '&lt;t:set attributeName=&quot;innerHTML&quot; to=&quot;XSSalert(\'XSS\')"&gt;', $result );
     349                    $this->assertSame( '&lt;t:set attributeName=&quot;innerHTML&quot; to=&quot;XSSalert(\'XSS\')"&gt;', $result );
    350350                    break;
    351351                case 'Commented-out Block':
    352                     $this->assertEquals( "<!--[if gte IE 4]&gt;-->\nalert('XSS');", $result );
     352                    $this->assertSame( "<!--[if gte IE 4]&gt;-->\nalert('XSS');", $result );
    353353                    break;
    354354                case 'Cookie Manipulation':
    355                     $this->assertEquals( '&lt;META HTTP-EQUIV=&quot;Set-Cookie&quot; Content=&quot;USERID=alert(\'XSS\')"&gt;', $result );
     355                    $this->assertSame( '&lt;META HTTP-EQUIV=&quot;Set-Cookie&quot; Content=&quot;USERID=alert(\'XSS\')"&gt;', $result );
    356356                    break;
    357357                case 'SSI':
    358                     $this->assertEquals( '&lt;!--#exec cmd=&quot;/bin/echo &#039;<!--#exec cmd="/bin/echo \'=http://ha.ckers.org/xss.js&gt;\'"-->', $result );
     358                    $this->assertSame( '&lt;!--#exec cmd=&quot;/bin/echo &#039;<!--#exec cmd="/bin/echo \'=http://ha.ckers.org/xss.js&gt;\'"-->', $result );
    359359                    break;
    360360                case 'PHP':
    361                     $this->assertEquals( '&lt;? echo(&#039;alert("XSS")\'); ?&gt;', $result );
     361                    $this->assertSame( '&lt;? echo(&#039;alert("XSS")\'); ?&gt;', $result );
    362362                    break;
    363363                case 'UTF-7 Encoding':
    364                     $this->assertEquals( '+ADw-SCRIPT+AD4-alert(\'XSS\');+ADw-/SCRIPT+AD4-', $result );
     364                    $this->assertSame( '+ADw-SCRIPT+AD4-alert(\'XSS\');+ADw-/SCRIPT+AD4-', $result );
    365365                    break;
    366366                case 'Escaping JavaScript escapes':
    367                     $this->assertEquals( '\";alert(\'XSS\');//', $result );
     367                    $this->assertSame( '\";alert(\'XSS\');//', $result );
    368368                    break;
    369369                case 'STYLE w/broken up JavaScript':
    370                     $this->assertEquals( '@im\port\'\ja\vasc\ript:alert("XSS")\';', $result );
     370                    $this->assertSame( '@im\port\'\ja\vasc\ript:alert("XSS")\';', $result );
    371371                    break;
    372372                case 'Null Chars 2':
    373                     $this->assertEquals( '&amp;alert("XSS")', $result );
     373                    $this->assertSame( '&amp;alert("XSS")', $result );
    374374                    break;
    375375                case 'No Closing Script Tag':
    376                     $this->assertEquals( '&lt;SCRIPT SRC=http://ha.ckers.org/xss.js', $result );
     376                    $this->assertSame( '&lt;SCRIPT SRC=http://ha.ckers.org/xss.js', $result );
    377377                    break;
    378378                case 'Half-Open HTML/JavaScript':
    379                     $this->assertEquals( '&lt;IMG SRC=&quot;javascript:alert(&#039;XSS&#039;)&quot;', $result );
     379                    $this->assertSame( '&lt;IMG SRC=&quot;javascript:alert(&#039;XSS&#039;)&quot;', $result );
    380380                    break;
    381381                case 'Double open angle brackets':
    382                     $this->assertEquals( '&lt;IFRAME SRC=http://ha.ckers.org/scriptlet.html &lt;', $result );
     382                    $this->assertSame( '&lt;IFRAME SRC=http://ha.ckers.org/scriptlet.html &lt;', $result );
    383383                    break;
    384384                case 'Extraneous Open Brackets':
    385                     $this->assertEquals( '&lt;alert("XSS");//&lt;', $result );
     385                    $this->assertSame( '&lt;alert("XSS");//&lt;', $result );
    386386                    break;
    387387                case 'Malformed IMG Tags':
    388                     $this->assertEquals( 'alert("XSS")"&gt;', $result );
     388                    $this->assertSame( 'alert("XSS")"&gt;', $result );
    389389                    break;
    390390                case 'No Quotes/Semicolons':
    391                     $this->assertEquals( "a=/XSS/\nalert(a.source)", $result );
     391                    $this->assertSame( "a=/XSS/\nalert(a.source)", $result );
    392392                    break;
    393393                case 'Evade Regex Filter 1':
    394                     $this->assertEquals( '" SRC="http://ha.ckers.org/xss.js"&gt;', $result );
     394                    $this->assertSame( '" SRC="http://ha.ckers.org/xss.js"&gt;', $result );
    395395                    break;
    396396                case 'Evade Regex Filter 4':
    397                     $this->assertEquals( '\'" SRC="http://ha.ckers.org/xss.js"&gt;', $result );
     397                    $this->assertSame( '\'" SRC="http://ha.ckers.org/xss.js"&gt;', $result );
    398398                    break;
    399399                case 'Evade Regex Filter 5':
    400                     $this->assertEquals( '` SRC="http://ha.ckers.org/xss.js"&gt;', $result );
     400                    $this->assertSame( '` SRC="http://ha.ckers.org/xss.js"&gt;', $result );
    401401                    break;
    402402                case 'Filter Evasion 1':
    403                     $this->assertEquals( 'document.write("&lt;SCRI&quot;);PT SRC="http://ha.ckers.org/xss.js"&gt;', $result );
     403                    $this->assertSame( 'document.write("&lt;SCRI&quot;);PT SRC="http://ha.ckers.org/xss.js"&gt;', $result );
    404404                    break;
    405405                case 'Filter Evasion 2':
    406                     $this->assertEquals( '\'&gt;" SRC="http://ha.ckers.org/xss.js"&gt;', $result );
     406                    $this->assertSame( '\'&gt;" SRC="http://ha.ckers.org/xss.js"&gt;', $result );
    407407                    break;
    408408                default:
     
    426426        global $allowedposttags, $allowedtags, $allowedentitynames;
    427427
    428         $this->assertEquals( $allowedposttags, wp_kses_allowed_html( 'post' ) );
     428        $this->assertSame( $allowedposttags, wp_kses_allowed_html( 'post' ) );
    429429
    430430        $tags = wp_kses_allowed_html( 'post' );
     
    437437        }
    438438
    439         $this->assertEquals( $allowedtags, wp_kses_allowed_html( 'data' ) );
    440         $this->assertEquals( $allowedtags, wp_kses_allowed_html( '' ) );
    441         $this->assertEquals( $allowedtags, wp_kses_allowed_html() );
     439        $this->assertSame( $allowedtags, wp_kses_allowed_html( 'data' ) );
     440        $this->assertSame( $allowedtags, wp_kses_allowed_html( '' ) );
     441        $this->assertSame( $allowedtags, wp_kses_allowed_html() );
    442442
    443443        $tags = wp_kses_allowed_html( 'user_description' );
     
    447447        $this->assertFalse( isset( $tags['a']['rel'] ) );
    448448
    449         $this->assertEquals( array(), wp_kses_allowed_html( 'strip' ) );
     449        $this->assertSame( array(), wp_kses_allowed_html( 'strip' ) );
    450450
    451451        $custom_tags = array(
     
    459459        );
    460460
    461         $this->assertEquals( $custom_tags, wp_kses_allowed_html( $custom_tags ) );
     461        $this->assertSame( $custom_tags, wp_kses_allowed_html( $custom_tags ) );
    462462
    463463        add_filter( 'wp_kses_allowed_html', array( $this, '_wp_kses_allowed_html_filter' ), 10, 2 );
    464464
    465         $this->assertEquals( array( 'a' => array( 'href' => true ) ), wp_kses_allowed_html( 'post' ) );
    466         $this->assertEquals( array( 'a' => array( 'href' => false ) ), wp_kses_allowed_html( 'data' ) );
     465        $this->assertSame( array( 'a' => array( 'href' => true ) ), wp_kses_allowed_html( 'post' ) );
     466        $this->assertSame( array( 'a' => array( 'href' => false ) ), wp_kses_allowed_html( 'data' ) );
    467467
    468468        remove_filter( 'wp_kses_allowed_html', array( $this, '_wp_kses_allowed_html_filter' ) );
    469         $this->assertEquals( $allowedposttags, wp_kses_allowed_html( 'post' ) );
    470         $this->assertEquals( $allowedtags, wp_kses_allowed_html( 'data' ) );
     469        $this->assertSame( $allowedposttags, wp_kses_allowed_html( 'post' ) );
     470        $this->assertSame( $allowedtags, wp_kses_allowed_html( 'data' ) );
    471471    }
    472472
     
    481481
    482482        $expect_valid_string = '<hyphenated-tag attribute="value">Alot of hyphens.</hyphenated-tag>';
    483         $this->assertEquals( $expect_stripped_string, wp_kses_post( $string ) );
    484         $this->assertEquals( $expect_valid_string, wp_kses( $string, $custom_tags ) );
     483        $this->assertSame( $expect_stripped_string, wp_kses_post( $string ) );
     484        $this->assertSame( $expect_valid_string, wp_kses( $string, $custom_tags ) );
    485485    }
    486486
     
    489489     */
    490490    public function test_wp_kses_normalize_entities() {
    491         $this->assertEquals( '&spades;', wp_kses_normalize_entities( '&spades;' ) );
    492 
    493         $this->assertEquals( '&sup1;', wp_kses_normalize_entities( '&sup1;' ) );
    494         $this->assertEquals( '&sup2;', wp_kses_normalize_entities( '&sup2;' ) );
    495         $this->assertEquals( '&sup3;', wp_kses_normalize_entities( '&sup3;' ) );
    496         $this->assertEquals( '&frac14;', wp_kses_normalize_entities( '&frac14;' ) );
    497         $this->assertEquals( '&frac12;', wp_kses_normalize_entities( '&frac12;' ) );
    498         $this->assertEquals( '&frac34;', wp_kses_normalize_entities( '&frac34;' ) );
    499         $this->assertEquals( '&there4;', wp_kses_normalize_entities( '&there4;' ) );
     491        $this->assertSame( '&spades;', wp_kses_normalize_entities( '&spades;' ) );
     492
     493        $this->assertSame( '&sup1;', wp_kses_normalize_entities( '&sup1;' ) );
     494        $this->assertSame( '&sup2;', wp_kses_normalize_entities( '&sup2;' ) );
     495        $this->assertSame( '&sup3;', wp_kses_normalize_entities( '&sup3;' ) );
     496        $this->assertSame( '&frac14;', wp_kses_normalize_entities( '&frac14;' ) );
     497        $this->assertSame( '&frac12;', wp_kses_normalize_entities( '&frac12;' ) );
     498        $this->assertSame( '&frac34;', wp_kses_normalize_entities( '&frac34;' ) );
     499        $this->assertSame( '&there4;', wp_kses_normalize_entities( '&there4;' ) );
    500500    }
    501501
     
    509509        global $allowedposttags;
    510510
    511         return $this->assertEquals( $output, wp_kses( $input, $allowedposttags ) );
     511        return $this->assertSame( $output, wp_kses( $input, $allowedposttags ) );
    512512    }
    513513
     
    546546        global $allowedposttags;
    547547
    548         return $this->assertEquals( $output, wp_kses( $input, $allowedposttags ) );
     548        return $this->assertSame( $output, wp_kses( $input, $allowedposttags ) );
    549549    }
    550550
     
    596596     */
    597597    function test_hair_parse( $input, $output ) {
    598         return $this->assertEquals( $output, wp_kses_hair_parse( $input ) );
     598        return $this->assertSame( $output, wp_kses_hair_parse( $input ) );
    599599    }
    600600
     
    662662     */
    663663    function test_attr_parse( $input, $output ) {
    664         return $this->assertEquals( $output, wp_kses_attr_parse( $input ) );
     664        return $this->assertSame( $output, wp_kses_attr_parse( $input ) );
    665665    }
    666666
     
    720720     */
    721721    function test_one_attr( $element, $input, $output ) {
    722         return $this->assertEquals( $output, wp_kses_one_attr( $input, $element ) );
     722        return $this->assertSame( $output, wp_kses_one_attr( $input, $element ) );
    723723    }
    724724
     
    801801        $input = '<p>This is <bdo dir="rtl">a BDO tag</bdo>. Weird, <bdo dir="ltr">right?</bdo></p>';
    802802
    803         $this->assertEquals( $input, wp_kses( $input, $allowedposttags ) );
     803        $this->assertSame( $input, wp_kses( $input, $allowedposttags ) );
    804804    }
    805805
     
    812812        $input = '<ol reversed="reversed"><li>Item 1</li><li>Item 2</li><li>Item 3</li></ol>';
    813813
    814         $this->assertEquals( $input, wp_kses( $input, $allowedposttags ) );
     814        $this->assertSame( $input, wp_kses( $input, $allowedposttags ) );
    815815    }
    816816
     
    822822        $attribute = 'title="foo" class="bar"';
    823823
    824         $this->assertEquals( "<{$element}>", wp_kses_attr( $element, $attribute, array( 'foo' => array() ), array() ) );
     824        $this->assertSame( "<{$element}>", wp_kses_attr( $element, $attribute, array( 'foo' => array() ), array() ) );
    825825    }
    826826
     
    832832        $attribute = 'title="foo" class="bar"';
    833833
    834         $this->assertEquals( "<{$element}>", wp_kses_attr( $element, $attribute, array( 'foo' => true ), array() ) );
     834        $this->assertSame( "<{$element}>", wp_kses_attr( $element, $attribute, array( 'foo' => true ), array() ) );
    835835    }
    836836
     
    842842        $attribute = 'title="foo" class="bar"';
    843843
    844         $this->assertEquals( "<{$element} title=\"foo\">", wp_kses_attr( $element, $attribute, array( 'foo' => array( 'title' => true ) ), array() ) );
     844        $this->assertSame( "<{$element} title=\"foo\">", wp_kses_attr( $element, $attribute, array( 'foo' => array( 'title' => true ) ), array() ) );
    845845    }
    846846
     
    852852        $attribute = 'title="foo" class="bar"';
    853853
    854         $this->assertEquals( "<{$element}>", wp_kses_attr( $element, $attribute, array( 'foo' => false ), array() ) );
     854        $this->assertSame( "<{$element}>", wp_kses_attr( $element, $attribute, array( 'foo' => false ), array() ) );
    855855    }
    856856
     
    10581058        $expected = '<div data-foo="foo" data-bar="bar" data-two-hyphens="remains">Pens and pencils</div>';
    10591059
    1060         $this->assertEquals( $expected, wp_kses_post( $test ) );
     1060        $this->assertSame( $expected, wp_kses_post( $test ) );
    10611061    }
    10621062
Note: See TracChangeset for help on using the changeset viewer.