Make WordPress Core


Ignore:
Timestamp:
07/19/2021 02:00:11 PM (6 months 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/admin/includesListTable.php

    r49691 r51462  
    222222
    223223        foreach ( $expected_ids as $id ) {
    224             $this->assertContains( sprintf( 'id="post-%d"', $id ), $output );
     224            $this->assertStringContainsString( sprintf( 'id="post-%d"', $id ), $output );
    225225        }
    226226    }
     
    237237        $output = ob_get_clean();
    238238
    239         $this->assertNotContains( 'id="post-query-submit"', $output );
     239        $this->assertStringNotContainsString( 'id="post-query-submit"', $output );
    240240    }
    241241
     
    251251        $output = ob_get_clean();
    252252
    253         $this->assertNotContains( 'id="filter-by-date"', $output );
     253        $this->assertStringNotContainsString( 'id="filter-by-date"', $output );
    254254    }
    255255
     
    265265        $output = ob_get_clean();
    266266
    267         $this->assertNotContains( 'id="cat"', $output );
     267        $this->assertStringNotContainsString( 'id="cat"', $output );
    268268    }
    269269
     
    279279        $output = ob_get_clean();
    280280
    281         $this->assertNotContains( 'id="delete_all"', $output );
     281        $this->assertStringNotContainsString( 'id="delete_all"', $output );
    282282    }
    283283
     
    292292        $output = ob_get_clean();
    293293
    294         $this->assertNotContains( 'id="post-query-submit"', $output );
     294        $this->assertStringNotContainsString( 'id="post-query-submit"', $output );
    295295    }
    296296
     
    314314        $output = ob_get_clean();
    315315
    316         $this->assertContains( 'id="post-query-submit"', $output );
     316        $this->assertStringContainsString( 'id="post-query-submit"', $output );
    317317    }
    318318
     
    336336        $output = ob_get_clean();
    337337
    338         $this->assertContains( 'id="filter-by-comment-type"', $output );
    339         $this->assertContains( "<option value='comment'>", $output );
     338        $this->assertStringContainsString( 'id="filter-by-comment-type"', $output );
     339        $this->assertStringContainsString( "<option value='comment'>", $output );
    340340    }
    341341
     
    350350        $output = ob_get_clean();
    351351
    352         $this->assertNotContains( 'id="delete_all"', $output );
     352        $this->assertStringNotContainsString( 'id="delete_all"', $output );
    353353    }
    354354
     
    385385        $expected = str_replace( "\r\n", "\n", $expected );
    386386
    387         $this->assertContains( $expected, $output );
     387        $this->assertStringContainsString( $expected, $output );
    388388    }
    389389
     
    412412        $output = get_echo( array( $object, 'print_column_headers' ) );
    413413
    414         $this->assertContains( '?orderby=comment_author&#038;order=desc', $output, 'Mismatch of the default link ordering for comment author column. Should be desc.' );
    415         $this->assertContains( 'column-author sortable asc', $output, 'Mismatch of CSS classes for the comment author column.' );
    416 
    417         $this->assertContains( '?orderby=comment_post_ID&#038;order=asc', $output, 'Mismatch of the default link ordering for comment response column. Should be asc.' );
    418         $this->assertContains( 'column-response sortable desc', $output, 'Mismatch of CSS classes for the comment post ID column.' );
    419 
    420         $this->assertContains( '?orderby=comment_date&#038;order=desc', $output, 'Mismatch of the default link ordering for comment date column. Should be asc.' );
    421         $this->assertContains( 'column-date sortable asc', $output, 'Mismatch of CSS classes for the comment date column.' );
     414        $this->assertStringContainsString( '?orderby=comment_author&#038;order=desc', $output, 'Mismatch of the default link ordering for comment author column. Should be desc.' );
     415        $this->assertStringContainsString( 'column-author sortable asc', $output, 'Mismatch of CSS classes for the comment author column.' );
     416
     417        $this->assertStringContainsString( '?orderby=comment_post_ID&#038;order=asc', $output, 'Mismatch of the default link ordering for comment response column. Should be asc.' );
     418        $this->assertStringContainsString( 'column-response sortable desc', $output, 'Mismatch of CSS classes for the comment post ID column.' );
     419
     420        $this->assertStringContainsString( '?orderby=comment_date&#038;order=desc', $output, 'Mismatch of the default link ordering for comment date column. Should be asc.' );
     421        $this->assertStringContainsString( 'column-date sortable asc', $output, 'Mismatch of CSS classes for the comment date column.' );
    422422    }
    423423
     
    450450        $output = get_echo( array( $object, 'print_column_headers' ) );
    451451
    452         $this->assertContains( '?orderby=comment_author&#038;order=asc', $output, 'Mismatch of the default link ordering for comment author column. Should be asc.' );
    453         $this->assertContains( 'column-author sortable desc', $output, 'Mismatch of CSS classes for the comment author column.' );
    454 
    455         $this->assertContains( '?orderby=comment_post_ID&#038;order=asc', $output, 'Mismatch of the default link ordering for comment response column. Should be asc.' );
    456         $this->assertContains( 'column-response sortable desc', $output, 'Mismatch of CSS classes for the comment post ID column.' );
    457 
    458         $this->assertContains( '?orderby=comment_date&#038;order=asc', $output, 'Mismatch of the current link ordering for comment date column. Should be asc.' );
    459         $this->assertContains( 'column-date sorted desc', $output, 'Mismatch of CSS classes for the comment date column.' );
     452        $this->assertStringContainsString( '?orderby=comment_author&#038;order=asc', $output, 'Mismatch of the default link ordering for comment author column. Should be asc.' );
     453        $this->assertStringContainsString( 'column-author sortable desc', $output, 'Mismatch of CSS classes for the comment author column.' );
     454
     455        $this->assertStringContainsString( '?orderby=comment_post_ID&#038;order=asc', $output, 'Mismatch of the default link ordering for comment response column. Should be asc.' );
     456        $this->assertStringContainsString( 'column-response sortable desc', $output, 'Mismatch of CSS classes for the comment post ID column.' );
     457
     458        $this->assertStringContainsString( '?orderby=comment_date&#038;order=asc', $output, 'Mismatch of the current link ordering for comment date column. Should be asc.' );
     459        $this->assertStringContainsString( 'column-date sorted desc', $output, 'Mismatch of CSS classes for the comment date column.' );
    460460    }
    461461
Note: See TracChangeset for help on using the changeset viewer.