Make WordPress Core


Ignore:
Timestamp:
07/19/2021 02:00:11 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Tests: Replace assertContains() with assertStringContainsString() when used with strings.

Using the assertContains() and assertNotContains() methods with string haystacks was deprecated in PHPUnit 8 and removed in PHPUnit 9.

While WordPress test suite currently only supports PHPUnit up to 7.5.x, this allows us to switch to newer assertions ahead of adding full support for PHPUnit 8+.

These methods introduced in PHPUnit 7.5 should be used as an alternative:

  • assertStringContainsString()
  • assertStringContainsStringIgnoringCase
  • assertStringNotContainsString()
  • assertStringNotContainsStringIgnoringCase

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

Follow-up to [51331], [51451], [51461].

Props jrf, dd32, SergeyBiryukov.
See #53363, #46149.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/getPostsByAuthorSql.php

    r46586 r51462  
    88    public function test_post_type_post() {
    99        $maybe_string = get_posts_by_author_sql( 'post' );
    10         $this->assertContains( "post_type = 'post'", $maybe_string );
     10        $this->assertStringContainsString( "post_type = 'post'", $maybe_string );
    1111    }
    1212
    1313    public function test_post_type_page() {
    1414        $maybe_string = get_posts_by_author_sql( 'page' );
    15         $this->assertContains( "post_type = 'page'", $maybe_string );
     15        $this->assertStringContainsString( "post_type = 'page'", $maybe_string );
    1616    }
    1717
    1818    public function test_non_existent_post_type() {
    1919        $maybe_string = get_posts_by_author_sql( 'non_existent_post_type' );
    20         $this->assertContains( '1 = 0', $maybe_string );
     20        $this->assertStringContainsString( '1 = 0', $maybe_string );
    2121    }
    2222
     
    2626
    2727        $maybe_string = get_posts_by_author_sql( array( 'foo', 'bar' ) );
    28         $this->assertContains( "post_type = 'foo'", $maybe_string );
    29         $this->assertContains( "post_type = 'bar'", $maybe_string );
     28        $this->assertStringContainsString( "post_type = 'foo'", $maybe_string );
     29        $this->assertStringContainsString( "post_type = 'bar'", $maybe_string );
    3030
    3131        _unregister_post_type( 'foo' );
     
    4545    public function test_post_type_clause_should_be_included_when_full_is_true() {
    4646        $maybe_string = get_posts_by_author_sql( 'post', true );
    47         $this->assertContains( "post_type = 'post'", $maybe_string );
     47        $this->assertStringContainsString( "post_type = 'post'", $maybe_string );
    4848    }
    4949
    5050    public function test_post_type_clause_should_be_included_when_full_is_false() {
    5151        $maybe_string = get_posts_by_author_sql( 'post', false );
    52         $this->assertContains( "post_type = 'post'", $maybe_string );
     52        $this->assertStringContainsString( "post_type = 'post'", $maybe_string );
    5353    }
    5454
    5555    public function test_post_author_should_create_post_author_clause() {
    5656        $maybe_string = get_posts_by_author_sql( 'post', true, 1 );
    57         $this->assertContains( 'post_author = 1', $maybe_string );
     57        $this->assertStringContainsString( 'post_author = 1', $maybe_string );
    5858    }
    5959
     
    6464
    6565        $maybe_string = get_posts_by_author_sql( 'post', true, $u, true );
    66         $this->assertNotContains( "post_status = 'private'", $maybe_string );
     66        $this->assertStringNotContainsString( "post_status = 'private'", $maybe_string );
    6767
    6868        wp_set_current_user( $current_user );
     
    8585
    8686        $maybe_string = get_posts_by_author_sql( 'post', true, $u, false );
    87         $this->assertContains( "post_status = 'private'", $maybe_string );
     87        $this->assertStringContainsString( "post_status = 'private'", $maybe_string );
    8888
    8989        wp_set_current_user( $current_user );
     
    9797
    9898        $maybe_string = get_posts_by_author_sql( 'post', true, $u2, false );
    99         $this->assertNotContains( "post_status = 'private'", $maybe_string );
     99        $this->assertStringNotContainsString( "post_status = 'private'", $maybe_string );
    100100
    101101        wp_set_current_user( $current_user );
     
    108108
    109109        $maybe_string = get_posts_by_author_sql( 'post', true, $u, false );
    110         $this->assertContains( "post_status = 'private'", $maybe_string );
    111         $this->assertContains( "post_author = $u", $maybe_string );
     110        $this->assertStringContainsString( "post_status = 'private'", $maybe_string );
     111        $this->assertStringContainsString( "post_author = $u", $maybe_string );
    112112
    113113        wp_set_current_user( $current_user );
     
    120120
    121121        $maybe_string = get_posts_by_author_sql( 'post', true, null, false );
    122         $this->assertContains( "post_status = 'private'", $maybe_string );
    123         $this->assertNotContains( 'post_author', $maybe_string );
     122        $this->assertStringContainsString( "post_status = 'private'", $maybe_string );
     123        $this->assertStringNotContainsString( 'post_author', $maybe_string );
    124124
    125125        wp_set_current_user( $current_user );
     
    140140        $editor_role->remove_cap( 'read_private_baz' );
    141141
    142         $this->assertNotContains( "post_type = 'foo' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string );
    143         $this->assertNotContains( "post_type = 'bar' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string );
    144         $this->assertContains( "post_type = 'baz' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string );
     142        $this->assertStringNotContainsString( "post_type = 'foo' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string );
     143        $this->assertStringNotContainsString( "post_type = 'bar' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string );
     144        $this->assertStringContainsString( "post_type = 'baz' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string );
    145145
    146146        _unregister_post_type( 'foo' );
Note: See TracChangeset for help on using the changeset viewer.