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/widgets/text-widget.php

    r51220 r51462  
    154154        $widget->widget( $args, $instance );
    155155        $output = ob_get_clean();
    156         $this->assertNotContains( '<p>', $output );
    157         $this->assertNotContains( '<br />', $output );
     156        $this->assertStringNotContainsString( '<p>', $output );
     157        $this->assertStringNotContainsString( '<br />', $output );
    158158        $this->assertEmpty( $this->widget_text_content_args );
    159159        $this->assertNotEmpty( $this->widget_text_args );
    160         $this->assertContains( '[filter:widget_text]', $output );
    161         $this->assertNotContains( '[filter:widget_text_content]', $output );
     160        $this->assertStringContainsString( '[filter:widget_text]', $output );
     161        $this->assertStringNotContainsString( '[filter:widget_text_content]', $output );
    162162
    163163        // Test with filter=true, implicit legacy mode.
     
    171171        $widget->widget( $args, $instance );
    172172        $output = ob_get_clean();
    173         $this->assertContains( '<p>', $output );
    174         $this->assertContains( '<br />', $output );
     173        $this->assertStringContainsString( '<p>', $output );
     174        $this->assertStringContainsString( '<br />', $output );
    175175        $this->assertNotEmpty( $this->widget_text_args );
    176176        $this->assertSame( $instance['text'], $this->widget_text_args[0] );
     
    178178        $this->assertSame( $widget, $this->widget_text_args[2] );
    179179        $this->assertEmpty( $this->widget_text_content_args );
    180         $this->assertContains( '[filter:widget_text]', $output );
    181         $this->assertNotContains( '[filter:widget_text_content]', $output );
     180        $this->assertStringContainsString( '[filter:widget_text]', $output );
     181        $this->assertStringNotContainsString( '[filter:widget_text_content]', $output );
    182182
    183183        // Test with filter=content, the upgraded widget, in 4.8.0 only.
     
    198198        $widget->widget( $args, $instance );
    199199        $output = ob_get_clean();
    200         $this->assertContains( '<p>', $output );
    201         $this->assertContains( '<br />', $output );
     200        $this->assertStringContainsString( '<p>', $output );
     201        $this->assertStringContainsString( '<br />', $output );
    202202        $this->assertCount( 3, $this->widget_text_args );
    203203        $this->assertSame( $expected_instance['text'], $this->widget_text_args[0] );
     
    208208        $this->assertSame( $expected_instance, $this->widget_text_content_args[1] );
    209209        $this->assertSame( $widget, $this->widget_text_content_args[2] );
    210         $this->assertContains( wpautop( $expected_instance['text'] . '[filter:widget_text][filter:widget_text_content]' ), $output );
     210        $this->assertStringContainsString( wpautop( $expected_instance['text'] . '[filter:widget_text][filter:widget_text_content]' ), $output );
    211211
    212212        // Test with filter=true&visual=true, the upgraded widget, in 4.8.1 and above.
     
    222222        $widget->widget( $args, $instance );
    223223        $output = ob_get_clean();
    224         $this->assertContains( '<p>', $output );
    225         $this->assertContains( '<br />', $output );
     224        $this->assertStringContainsString( '<p>', $output );
     225        $this->assertStringContainsString( '<br />', $output );
    226226        $this->assertCount( 3, $this->widget_text_args );
    227227        $this->assertSame( $expected_instance['text'], $this->widget_text_args[0] );
     
    232232        $this->assertSame( $expected_instance, $this->widget_text_content_args[1] );
    233233        $this->assertSame( $widget, $this->widget_text_content_args[2] );
    234         $this->assertContains( wpautop( $expected_instance['text'] . '[filter:widget_text][filter:widget_text_content]' ), $output );
     234        $this->assertStringContainsString( wpautop( $expected_instance['text'] . '[filter:widget_text][filter:widget_text_content]' ), $output );
    235235
    236236        // Test with filter=true&visual=true, the upgraded widget, in 4.8.1 and above.
     
    246246        $widget->widget( $args, $instance );
    247247        $output = ob_get_clean();
    248         $this->assertContains( '<p>', $output );
    249         $this->assertContains( '<br />', $output );
     248        $this->assertStringContainsString( '<p>', $output );
     249        $this->assertStringContainsString( '<br />', $output );
    250250        $this->assertCount( 3, $this->widget_text_args );
    251251        $this->assertSame( $expected_instance['text'], $this->widget_text_args[0] );
     
    253253        $this->assertSame( $widget, $this->widget_text_args[2] );
    254254        $this->assertNull( $this->widget_text_content_args );
    255         $this->assertContains( wpautop( $expected_instance['text'] . '[filter:widget_text]' ), $output );
     255        $this->assertStringContainsString( wpautop( $expected_instance['text'] . '[filter:widget_text]' ), $output );
    256256
    257257        // Test with filter=false&visual=false, the upgraded widget, in 4.8.1 and above.
     
    267267        $widget->widget( $args, $instance );
    268268        $output = ob_get_clean();
    269         $this->assertNotContains( '<p>', $output );
    270         $this->assertNotContains( '<br />', $output );
     269        $this->assertStringNotContainsString( '<p>', $output );
     270        $this->assertStringNotContainsString( '<br />', $output );
    271271        $this->assertCount( 3, $this->widget_text_args );
    272272        $this->assertSame( $expected_instance['text'], $this->widget_text_args[0] );
     
    274274        $this->assertSame( $widget, $this->widget_text_args[2] );
    275275        $this->assertNull( $this->widget_text_content_args );
    276         $this->assertContains( $expected_instance['text'] . '[filter:widget_text]', $output );
     276        $this->assertStringContainsString( $expected_instance['text'] . '[filter:widget_text]', $output );
    277277    }
    278278
     
    346346        $output = ob_get_clean();
    347347        $this->assertSame( 1, $this->shortcode_render_count );
    348         $this->assertNotContains( '[example]', $output, 'Expected shortcode to be processed in legacy widget with plugin adding filter' );
    349         $this->assertContains( $this->example_shortcode_content, $output, 'Shortcode was applied without wpautop corrupting it.' );
    350         $this->assertNotContains( '<p>' . $this->example_shortcode_content . '</p>', $output, 'Expected shortcode_unautop() to have run.' );
     348        $this->assertStringNotContainsString( '[example]', $output, 'Expected shortcode to be processed in legacy widget with plugin adding filter' );
     349        $this->assertStringContainsString( $this->example_shortcode_content, $output, 'Shortcode was applied without wpautop corrupting it.' );
     350        $this->assertStringNotContainsString( '<p>' . $this->example_shortcode_content . '</p>', $output, 'Expected shortcode_unautop() to have run.' );
    351351        $this->assertNull( $this->post_during_shortcode );
    352352
     
    364364        $output = ob_get_clean();
    365365        $this->assertSame( 1, $this->shortcode_render_count );
    366         $this->assertNotContains( '[example]', $output, 'Expected shortcode to be processed in legacy widget with plugin adding filter' );
    367         $this->assertContains( $this->example_shortcode_content, $output, 'Shortcode was applied without wpautop corrupting it.' );
    368         $this->assertNotContains( '<p>' . $this->example_shortcode_content . '</p>', $output, 'Expected shortcode_unautop() to have run.' );
     366        $this->assertStringNotContainsString( '[example]', $output, 'Expected shortcode to be processed in legacy widget with plugin adding filter' );
     367        $this->assertStringContainsString( $this->example_shortcode_content, $output, 'Shortcode was applied without wpautop corrupting it.' );
     368        $this->assertStringNotContainsString( '<p>' . $this->example_shortcode_content . '</p>', $output, 'Expected shortcode_unautop() to have run.' );
    369369        $this->assertNull( $this->post_during_shortcode );
    370370
     
    376376        $output = ob_get_clean();
    377377        $this->assertSame( 1, $this->shortcode_render_count );
    378         $this->assertNotContains( '[example]', $output, 'Expected shortcode to be processed in legacy widget with plugin adding filter' );
    379         $this->assertContains( wpautop( $this->example_shortcode_content ), $output, 'Shortcode was applied *with* wpautop() applying to shortcode output since plugin used legacy filter.' );
     378        $this->assertStringNotContainsString( '[example]', $output, 'Expected shortcode to be processed in legacy widget with plugin adding filter' );
     379        $this->assertStringContainsString( wpautop( $this->example_shortcode_content ), $output, 'Shortcode was applied *with* wpautop() applying to shortcode output since plugin used legacy filter.' );
    380380        $this->assertNull( $this->post_during_shortcode );
    381381        remove_filter( 'widget_text', 'do_shortcode' );
     
    397397        $output = ob_get_clean();
    398398        $this->assertSame( 1, $this->shortcode_render_count );
    399         $this->assertContains( $this->example_shortcode_content, $output, 'Shortcode was applied without wpautop corrupting it.' );
    400         $this->assertNotContains( '<p>' . $this->example_shortcode_content . '</p>', $output, 'Expected shortcode_unautop() to have run.' );
     399        $this->assertStringContainsString( $this->example_shortcode_content, $output, 'Shortcode was applied without wpautop corrupting it.' );
     400        $this->assertStringNotContainsString( '<p>' . $this->example_shortcode_content . '</p>', $output, 'Expected shortcode_unautop() to have run.' );
    401401        $this->assertFalse( has_filter( 'widget_text', 'do_shortcode' ), 'The widget_text filter still lacks do_shortcode handler.' );
    402402        $this->assertSame( 11, has_filter( 'widget_text_content', 'do_shortcode' ), 'The widget_text_content filter still has do_shortcode handler.' );
     
    410410        $output = ob_get_clean();
    411411        $this->assertSame( 1, $this->shortcode_render_count );
    412         $this->assertContains( $this->example_shortcode_content, $output, 'Shortcode was applied without wpautop corrupting it.' );
    413         $this->assertNotContains( '<p>' . $this->example_shortcode_content . '</p>', $output, 'Expected shortcode_unautop() to have run.' );
     412        $this->assertStringContainsString( $this->example_shortcode_content, $output, 'Shortcode was applied without wpautop corrupting it.' );
     413        $this->assertStringNotContainsString( '<p>' . $this->example_shortcode_content . '</p>', $output, 'Expected shortcode_unautop() to have run.' );
    414414        $this->assertSame( 10, has_filter( 'widget_text', 'do_shortcode' ), 'Expected do_shortcode to be restored to widget_text.' );
    415415        $this->assertNull( $this->post_during_shortcode );
     
    425425        $output = ob_get_clean();
    426426        $this->assertSame( 0, $this->shortcode_render_count );
    427         $this->assertContains( '[example]', $output );
    428         $this->assertNotContains( $this->example_shortcode_content, $output );
     427        $this->assertStringContainsString( '[example]', $output );
     428        $this->assertStringNotContainsString( $this->example_shortcode_content, $output );
    429429        $this->assertFalse( has_filter( 'widget_text', 'do_shortcode' ) );
    430430        $this->assertFalse( has_filter( 'widget_text_content', 'do_shortcode' ) );
     
    645645        $widget->form( $instance );
    646646        $form = ob_get_clean();
    647         $this->assertContains( 'class="visual" type="hidden" value=""', $form );
    648         $this->assertNotContains( 'class="visual sync-input" type="hidden" value="on"', $form );
     647        $this->assertStringContainsString( 'class="visual" type="hidden" value=""', $form );
     648        $this->assertStringNotContainsString( 'class="visual sync-input" type="hidden" value="on"', $form );
    649649
    650650        $instance = array(
     
    657657        $widget->form( $instance );
    658658        $form = ob_get_clean();
    659         $this->assertContains( 'class="visual sync-input" type="hidden" value="on"', $form );
    660         $this->assertNotContains( 'class="visual sync-input" type="hidden" value=""', $form );
     659        $this->assertStringContainsString( 'class="visual sync-input" type="hidden" value="on"', $form );
     660        $this->assertStringNotContainsString( 'class="visual sync-input" type="hidden" value=""', $form );
    661661
    662662        $instance = array(
     
    669669        $widget->form( $instance );
    670670        $form = ob_get_clean();
    671         $this->assertContains( 'class="visual sync-input" type="hidden" value="on"', $form );
    672         $this->assertNotContains( 'class="visual sync-input" type="hidden" value=""', $form );
     671        $this->assertStringContainsString( 'class="visual sync-input" type="hidden" value="on"', $form );
     672        $this->assertStringNotContainsString( 'class="visual sync-input" type="hidden" value=""', $form );
    673673
    674674        $instance = array(
     
    682682        $widget->form( $instance );
    683683        $form = ob_get_clean();
    684         $this->assertContains( 'class="visual sync-input" type="hidden" value="on"', $form );
    685         $this->assertContains( '&lt;code&gt;&amp;lt;strong&amp;gt;BOLD!', $form );
    686         $this->assertNotContains( 'class="visual sync-input" type="hidden" value=""', $form );
     684        $this->assertStringContainsString( 'class="visual sync-input" type="hidden" value="on"', $form );
     685        $this->assertStringContainsString( '&lt;code&gt;&amp;lt;strong&amp;gt;BOLD!', $form );
     686        $this->assertStringNotContainsString( 'class="visual sync-input" type="hidden" value=""', $form );
    687687
    688688        remove_filter( 'user_can_richedit', '__return_true' );
     
    698698        $widget->form( $instance );
    699699        $form = ob_get_clean();
    700         $this->assertNotContains( 'Evil:</textarea>', $form );
    701         $this->assertContains( 'Evil:&lt;/textarea>', $form );
     700        $this->assertStringNotContainsString( 'Evil:</textarea>', $form );
     701        $this->assertStringContainsString( 'Evil:&lt;/textarea>', $form );
    702702    }
    703703
     
    10001000        $output = ob_get_clean();
    10011001
    1002         $this->assertContains( '<script type="text/html" id="tmpl-widget-text-control-fields">', $output );
     1002        $this->assertStringContainsString( '<script type="text/html" id="tmpl-widget-text-control-fields">', $output );
    10031003    }
    10041004
     
    10271027        $output = get_echo( array( $widget, 'widget' ), array( $args, $instance ) );
    10281028
    1029         $this->assertContains( 'rel="noopener"', $output );
     1029        $this->assertStringContainsString( 'rel="noopener"', $output );
    10301030    }
    10311031
     
    10541054        $output = get_echo( array( $widget, 'widget' ), array( $args, $instance ) );
    10551055
    1056         $this->assertNotContains( 'rel="noopener"', $output );
     1056        $this->assertStringNotContainsString( 'rel="noopener"', $output );
    10571057    }
    10581058}
Note: See TracChangeset for help on using the changeset viewer.