Make WordPress Core

Changeset 51451


Ignore:
Timestamp:
07/17/2021 10:36:52 AM (3 years ago)
Author:
SergeyBiryukov
Message:

Tests: Use more appropriate assertions in various tests.

This replaces instances of assertTrue( strpos( ... ) > 0 ) with assertStringContainsString() to use native PHPUnit functionality.

Going forward, these methods introduced in PHPUnit 7.5 should be used for similar assertions:

  • assertStringContainsString()
  • assertStringNotContainsString()

As WordPress currently uses PHPUnit 5.7.x to run tests on PHP 5.6, polyfills for these methods are now added to the WP_UnitTestCase class for PHPUnit < 7.5.

Follow-up to [51335], [51337], [51367], [51397], [51403], [51404], [51436], [51438], [51448], [51449].

See #53363.

Location:
trunk/tests/phpunit
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/testcase.php

    r51331 r51451  
    373373        static::assertFalse( is_iterable( $actual ), $message );
    374374    }
     375
     376    /**
     377     * Asserts that a string haystack contains a needle.
     378     *
     379     * This method has been backported from a more recent PHPUnit version,
     380     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     381     *
     382     * @since 5.9.0
     383     *
     384     * @param string $needle   The string to search for.
     385     * @param string $haystack The string to treat as the haystack.
     386     * @param string $message  Optional. Message to display when the assertion fails.
     387     */
     388    public static function assertStringContainsString( $needle, $haystack, $message = '' ) {
     389        static::assertContains( $needle, $haystack, $message );
     390    }
     391
     392    /**
     393     * Asserts that a string haystack does not contain a needle.
     394     *
     395     * This method has been backported from a more recent PHPUnit version,
     396     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     397     *
     398     * @since 5.9.0
     399     *
     400     * @param string $needle   The string to search for.
     401     * @param string $haystack The string to treat as the haystack.
     402     * @param string $message  Optional. Message to display when the assertion fails.
     403     */
     404    public static function assertStringNotContainsString( $needle, $haystack, $message = '' ) {
     405        static::assertNotContains( $needle, $haystack, $message );
     406    }
    375407}
  • trunk/tests/phpunit/tests/ajax/Autosave.php

    r51450 r51451  
    8080        // Check that the edit happened.
    8181        $post = get_post( self::$post_id );
    82         $this->assertNotFalse( strpos( $post->post_content, $md5 ) );
     82        $this->assertStringContainsString( $md5, $post->post_content );
    8383    }
    8484
     
    126126        // Check that the original post was NOT edited.
    127127        $post = get_post( self::$post_id );
    128         $this->assertFalse( strpos( $post->post_content, $md5 ) );
     128        $this->assertStringNotContainsString( $md5, $post->post_content );
    129129
    130130        // Check if the autosave post was created.
    131131        $autosave = wp_get_post_autosave( self::$post_id, get_current_user_id() );
    132132        $this->assertNotEmpty( $autosave );
    133         $this->assertNotFalse( strpos( $autosave->post_content, $md5 ) );
     133        $this->assertStringContainsString( $md5, $autosave->post_content );
    134134    }
    135135
  • trunk/tests/phpunit/tests/image/intermediateSize.php

    r51415 r51451  
    107107        // Test for the expected string because the array will by definition
    108108        // return with the correct height and width attributes.
    109         $this->assertTrue( strpos( $image['file'], '330x220' ) > 0 );
     109        $this->assertStringContainsString( '330x220', $image['file'] );
    110110    }
    111111
     
    129129        // Test for the expected string because the array will by definition
    130130        // return with the correct height and width attributes.
    131         $this->assertTrue( strpos( $image['file'], '330x220' ) > 0 );
     131        $this->assertStringContainsString( '330x220', $image['file'] );
    132132    }
    133133
     
    152152        // Test for the expected string because the array will by definition
    153153        // return with the correct height and width attributes.
    154         $this->assertTrue( strpos( $image['file'], '450x300' ) > 0 );
     154        $this->assertStringContainsString( '450x300', $image['file'] );
    155155    }
    156156
     
    202202        // Test for the expected string because the array will by definition
    203203        // return with the correct height and width attributes.
    204         $this->assertTrue( strpos( $image['file'], $image_w . 'x' . $image_h ) > 0 );
     204        $this->assertStringContainsString( $image_w . 'x' . $image_h, $image['file'] );
    205205    }
    206206
     
    231231        // Test for the expected string because the array will by definition
    232232        // return with the correct height and width attributes.
    233         $this->assertTrue( strpos( $image['file'], $image_w . 'x' . $image_h ) > 0 );
     233        $this->assertStringContainsString( $image_w . 'x' . $image_h, $image['file'] );
    234234    }
    235235
     
    256256        $image = image_get_intermediate_size( $id, array( 0, $height ) );
    257257
    258         $this->assertTrue( strpos( $image['file'], $width . 'x' . $height ) > 0 );
     258        $this->assertStringContainsString( $width . 'x' . $height, $image['file'] );
    259259    }
    260260
     
    274274
    275275        // We should get the 'test-size' file and not the thumbnail.
    276         $this->assertTrue( strpos( $image['file'], '200x100' ) > 0 );
     276        $this->assertStringContainsString( '200x100', $image['file'] );
    277277    }
    278278
  • trunk/tests/phpunit/tests/mail.php

    r50610 r51451  
    8282        // We need some better assertions here but these catch the failure for now.
    8383        $this->assertSameIgnoreEOL( $body, $mailer->get_sent()->body );
    84         $this->assertTrue( strpos( iconv_mime_decode_headers( ( $mailer->get_sent()->header ) )['Content-Type'][0], 'boundary="----=_Part_4892_25692638.1192452070893"' ) > 0 );
    85         $this->assertTrue( strpos( $mailer->get_sent()->header, 'charset=' ) > 0 );
     84        $this->assertStringContainsString( 'boundary="----=_Part_4892_25692638.1192452070893"', iconv_mime_decode_headers( ( $mailer->get_sent()->header ) )['Content-Type'][0] );
     85        $this->assertStringContainsString( 'charset=', $mailer->get_sent()->header );
    8686    }
    8787
     
    191191
    192192        $mailer = tests_retrieve_phpmailer_instance();
    193         $this->assertTrue( strpos( $mailer->get_sent()->header, $expected ) > 0 );
     193        $this->assertStringContainsString( $expected, $mailer->get_sent()->header );
    194194    }
    195195
     
    207207
    208208        $mailer = tests_retrieve_phpmailer_instance();
    209         $this->assertTrue( strpos( $mailer->get_sent()->header, $expected ) > 0 );
     209        $this->assertStringContainsString( $expected, $mailer->get_sent()->header );
    210210    }
    211211
     
    223223
    224224        $mailer = tests_retrieve_phpmailer_instance();
    225         $this->assertTrue( strpos( $mailer->get_sent()->header, $expected ) > 0 );
     225        $this->assertStringContainsString( $expected, $mailer->get_sent()->header );
    226226    }
    227227
     
    239239
    240240        $mailer = tests_retrieve_phpmailer_instance();
    241         $this->assertTrue( strpos( $mailer->get_sent()->header, $expected ) > 0 );
     241        $this->assertStringContainsString( $expected, $mailer->get_sent()->header );
    242242    }
    243243
     
    255255
    256256        $mailer = tests_retrieve_phpmailer_instance();
    257         $this->assertTrue( strpos( $mailer->get_sent()->header, $expected ) > 0 );
     257        $this->assertStringContainsString( $expected, $mailer->get_sent()->header );
    258258    }
    259259
     
    271271
    272272        $mailer = tests_retrieve_phpmailer_instance();
    273         $this->assertTrue( strpos( $mailer->get_sent()->header, $expected ) > 0 );
     273        $this->assertStringContainsString( $expected, $mailer->get_sent()->header );
    274274    }
    275275
  • trunk/tests/phpunit/tests/media.php

    r51415 r51451  
    16551655        // Test to confirm all sources in the array include the same edit hash.
    16561656        foreach ( $sizes as $size ) {
    1657             $this->assertNotFalse( strpos( $size, $hash ) );
     1657            $this->assertStringContainsString( $hash, $size );
    16581658        }
    16591659    }
     
    22312231        $this->assertFalse( wp_calculate_image_srcset( $size_array, $full_src, $image_meta ) );
    22322232        // Intermediate sized GIFs should not include the full size in the srcset.
    2233         $this->assertFalse( strpos( wp_calculate_image_srcset( $size_array, $large_src, $image_meta ), $full_src ) );
     2233        $this->assertStringNotContainsString( $full_src, wp_calculate_image_srcset( $size_array, $large_src, $image_meta ) );
    22342234    }
    22352235
  • trunk/tests/phpunit/tests/oembed/template.php

    r50441 r51451  
    3333        $doc = new DOMDocument();
    3434        $this->assertTrue( $doc->loadHTML( $actual ) );
    35         $this->assertFalse( strpos( $actual, 'That embed can&#8217;t be found.' ) );
    36         $this->assertNotFalse( strpos( $actual, 'Hello World' ) );
     35        $this->assertStringNotContainsString( 'That embed can&#8217;t be found.', $actual );
     36        $this->assertStringContainsString( 'Hello World', $actual );
    3737    }
    3838
     
    6565        $doc = new DOMDocument();
    6666        $this->assertTrue( $doc->loadHTML( $actual ) );
    67         $this->assertFalse( strpos( $actual, 'That embed can&#8217;t be found.' ) );
    68         $this->assertNotFalse( strpos( $actual, 'Hello World' ) );
    69         $this->assertNotFalse( strpos( $actual, 'canola.jpg' ) );
     67        $this->assertStringNotContainsString( 'That embed can&#8217;t be found.', $actual );
     68        $this->assertStringContainsString( 'Hello World', $actual );
     69        $this->assertStringContainsString( 'canola.jpg', $actual );
    7070    }
    7171
     
    8282        $doc = new DOMDocument();
    8383        $this->assertTrue( $doc->loadHTML( $actual ) );
    84         $this->assertNotFalse( strpos( $actual, 'That embed can&#8217;t be found.' ) );
     84        $this->assertStringContainsString( 'That embed can&#8217;t be found.', $actual );
    8585    }
    8686
     
    109109        $doc = new DOMDocument();
    110110        $this->assertTrue( $doc->loadHTML( $actual ) );
    111         $this->assertFalse( strpos( $actual, 'That embed can&#8217;t be found.' ) );
    112         $this->assertNotFalse( strpos( $actual, 'Hello World' ) );
    113         $this->assertNotFalse( strpos( $actual, 'canola.jpg' ) );
     111        $this->assertStringNotContainsString( 'That embed can&#8217;t be found.', $actual );
     112        $this->assertStringContainsString( 'Hello World', $actual );
     113        $this->assertStringContainsString( 'canola.jpg', $actual );
    114114    }
    115115
     
    134134        $doc = new DOMDocument();
    135135        $this->assertTrue( $doc->loadHTML( $actual ) );
    136         $this->assertNotFalse( strpos( $actual, 'That embed can&#8217;t be found.' ) );
     136        $this->assertStringContainsString( 'That embed can&#8217;t be found.', $actual );
    137137    }
    138138
     
    158158        $doc = new DOMDocument();
    159159        $this->assertTrue( $doc->loadHTML( $actual ) );
    160         $this->assertNotFalse( strpos( $actual, 'That embed can&#8217;t be found.' ) );
     160        $this->assertStringContainsString( 'That embed can&#8217;t be found.', $actual );
    161161    }
    162162
     
    181181        $doc = new DOMDocument();
    182182        $this->assertTrue( $doc->loadHTML( $actual ) );
    183         $this->assertNotFalse( strpos( $actual, 'That embed can&#8217;t be found.' ) );
     183        $this->assertStringContainsString( 'That embed can&#8217;t be found.', $actual );
    184184    }
    185185
     
    208208        $doc = new DOMDocument();
    209209        $this->assertTrue( $doc->loadHTML( $actual ) );
    210         $this->assertFalse( strpos( $actual, 'That embed can&#8217;t be found.' ) );
    211         $this->assertNotFalse( strpos( $actual, 'Hello World' ) );
     210        $this->assertStringNotContainsString( 'That embed can&#8217;t be found.', $actual );
     211        $this->assertStringContainsString( 'Hello World', $actual );
    212212    }
    213213
Note: See TracChangeset for help on using the changeset viewer.