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/category/wpListCategories.php

    r46586 r51462  
    1515        );
    1616
    17         $this->assertContains( 'class="cat-item cat-item-' . $c . '"', $found );
     17        $this->assertStringContainsString( 'class="cat-item cat-item-' . $c . '"', $found );
    1818    }
    1919
     
    9797        remove_filter( 'list_cats', array( $this, 'list_cats_callback' ) );
    9898
    99         $this->assertContains( "cat-item-$c2", $found );
    100         $this->assertContains( 'Test Cat 2', $found );
    101 
    102         $this->assertNotContains( "cat-item-$c1", $found );
    103         $this->assertNotContains( 'Test Cat 1', $found );
     99        $this->assertStringContainsString( "cat-item-$c2", $found );
     100        $this->assertStringContainsString( 'Test Cat 2', $found );
     101
     102        $this->assertStringNotContainsString( "cat-item-$c1", $found );
     103        $this->assertStringNotContainsString( 'Test Cat 1', $found );
    104104    }
    105105
     
    129129        );
    130130
    131         $this->assertContains( "cat-item-$c", $found );
    132         $this->assertContains( '0', $found );
     131        $this->assertStringContainsString( "cat-item-$c", $found );
     132        $this->assertStringContainsString( '0', $found );
    133133    }
    134134
     
    145145        );
    146146
    147         $this->assertContains( "<li class='cat-item-all'><a href='" . home_url( '/' ) . "'>All</a></li>", $found );
     147        $this->assertStringContainsString( "<li class='cat-item-all'><a href='" . home_url( '/' ) . "'>All</a></li>", $found );
    148148    }
    149149
     
    164164        );
    165165
    166         $this->assertContains( "<li class='cat-item-all'><a href='" . get_permalink( $p ) . "'>All</a></li>", $found );
     166        $this->assertStringContainsString( "<li class='cat-item-all'><a href='" . get_permalink( $p ) . "'>All</a></li>", $found );
    167167    }
    168168
     
    193193        $pt_archive = get_post_type_archive_link( 'wptests_pt' );
    194194
    195         $this->assertContains( "<li class='cat-item-all'><a href='" . $pt_archive . "'>All</a></li>", $found );
     195        $this->assertStringContainsString( "<li class='cat-item-all'><a href='" . $pt_archive . "'>All</a></li>", $found );
    196196    }
    197197
     
    222222        $pt_archive = get_post_type_archive_link( 'wptests_pt2' );
    223223
    224         $this->assertContains( "<li class='cat-item-all'><a href='" . $pt_archive . "'>All</a></li>", $found );
     224        $this->assertStringContainsString( "<li class='cat-item-all'><a href='" . $pt_archive . "'>All</a></li>", $found );
    225225    }
    226226
     
    248248        $url = home_url( '/' );
    249249
    250         $this->assertContains( "<li class='cat-item-all'><a href='" . $url . "'>All</a></li>", $found );
     250        $this->assertStringContainsString( "<li class='cat-item-all'><a href='" . $url . "'>All</a></li>", $found );
    251251    }
    252252
     
    274274        $url = home_url( '/' );
    275275
    276         $this->assertContains( "<li class='cat-item-all'><a href='" . $url . "'>All</a></li>", $found );
     276        $this->assertStringContainsString( "<li class='cat-item-all'><a href='" . $url . "'>All</a></li>", $found );
    277277    }
    278278
     
    287287        );
    288288
    289         $this->assertContains( '<li class="categories">Categories', $found );
     289        $this->assertStringContainsString( '<li class="categories">Categories', $found );
    290290    }
    291291
     
    301301        );
    302302
    303         $this->assertNotContains( '<li class="categories">Categories', $found );
     303        $this->assertStringNotContainsString( '<li class="categories">Categories', $found );
    304304    }
    305305
     
    315315        );
    316316
    317         $this->assertContains( '<li class="categories">Categories', $found );
     317        $this->assertStringContainsString( '<li class="categories">Categories', $found );
    318318    }
    319319
     
    332332        );
    333333
    334         $this->assertContains( '<li class="categories">Categories', $found );
     334        $this->assertStringContainsString( '<li class="categories">Categories', $found );
    335335    }
    336336
     
    350350        );
    351351
    352         $this->assertNotContains( '</ul></li>', $found );
     352        $this->assertStringNotContainsString( '</ul></li>', $found );
    353353    }
    354354
     
    380380        $actual = wp_list_categories( $args );
    381381
    382         $this->assertNotContains( '<li class="cat-item cat-item-' . $parent . '">', $actual );
    383 
    384         $this->assertNotContains( '<li class="cat-item cat-item-' . $child . '">', $actual );
     382        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $parent . '">', $actual );
     383
     384        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $child . '">', $actual );
    385385    }
    386386
     
    432432        );
    433433
    434         $this->assertNotContains( '<li class="cat-item cat-item-' . $parent . '">', $actual );
    435         $this->assertNotContains( '<li class="cat-item cat-item-' . $parent2 . '">', $actual );
    436         $this->assertNotContains( '<li class="cat-item cat-item-' . $child . '">', $actual );
    437 
    438         $this->assertNotContains( '<li class="cat-item cat-item-' . $child2 . '">', $actual );
     434        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $parent . '">', $actual );
     435        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $parent2 . '">', $actual );
     436        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $child . '">', $actual );
     437
     438        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $child2 . '">', $actual );
    439439    }
    440440
     
    512512        );
    513513
    514         $this->assertContains( '<li class="cat-item cat-item-' . $c . '">', $actual );
    515 
    516         $this->assertNotContains( '<li class="cat-item cat-item-' . $parent . '">', $actual );
    517         $this->assertNotContains( '<li class="cat-item cat-item-' . $parent2 . '">', $actual );
    518         $this->assertNotContains( '<li class="cat-item cat-item-' . $child . '">', $actual );
    519         $this->assertNotContains( '<li class="cat-item cat-item-' . $child2 . '">', $actual );
    520 
    521         $this->assertNotContains( '<li class="cat-item cat-item-' . $parent3 . '">', $actual );
    522         $this->assertNotContains( '<li class="cat-item cat-item-' . $parent4 . '">', $actual );
    523         $this->assertNotContains( '<li class="cat-item cat-item-' . $child3 . '">', $actual );
    524         $this->assertNotContains( '<li class="cat-item cat-item-' . $child4 . '">', $actual );
     514        $this->assertStringContainsString( '<li class="cat-item cat-item-' . $c . '">', $actual );
     515
     516        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $parent . '">', $actual );
     517        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $parent2 . '">', $actual );
     518        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $child . '">', $actual );
     519        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $child2 . '">', $actual );
     520
     521        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $parent3 . '">', $actual );
     522        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $parent4 . '">', $actual );
     523        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $child3 . '">', $actual );
     524        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $child4 . '">', $actual );
    525525    }
    526526
     
    598598        );
    599599
    600         $this->assertContains( '<li class="cat-item cat-item-' . $c . '">', $actual );
    601 
    602         $this->assertNotContains( '<li class="cat-item cat-item-' . $parent . '">', $actual );
    603         $this->assertNotContains( '<li class="cat-item cat-item-' . $parent2 . '">', $actual );
    604         $this->assertNotContains( '<li class="cat-item cat-item-' . $child . '">', $actual );
    605         $this->assertNotContains( '<li class="cat-item cat-item-' . $child2 . '">', $actual );
    606 
    607         $this->assertNotContains( '<li class="cat-item cat-item-' . $parent3 . '">', $actual );
    608         $this->assertNotContains( '<li class="cat-item cat-item-' . $parent4 . '">', $actual );
    609         $this->assertNotContains( '<li class="cat-item cat-item-' . $child3 . '">', $actual );
    610         $this->assertNotContains( '<li class="cat-item cat-item-' . $child4 . '">', $actual );
     600        $this->assertStringContainsString( '<li class="cat-item cat-item-' . $c . '">', $actual );
     601
     602        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $parent . '">', $actual );
     603        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $parent2 . '">', $actual );
     604        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $child . '">', $actual );
     605        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $child2 . '">', $actual );
     606
     607        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $parent3 . '">', $actual );
     608        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $parent4 . '">', $actual );
     609        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $child3 . '">', $actual );
     610        $this->assertStringNotContainsString( '<li class="cat-item cat-item-' . $child4 . '">', $actual );
    611611    }
    612612
Note: See TracChangeset for help on using the changeset viewer.