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.php

    r51397 r51462  
    265265
    266266        $this->assertArrayHasKey( $sidebar_id, $wp_registered_sidebars );
    267         $this->assertContains( '<div id="%1$s" class="before-sidebar %2$s">', $wp_registered_sidebars[ $sidebar_id ]['before_sidebar'] );
    268         $this->assertContains( '</div> <!-- .before-sidebar -->', $wp_registered_sidebars[ $sidebar_id ]['after_sidebar'] );
     267        $this->assertStringContainsString( '<div id="%1$s" class="before-sidebar %2$s">', $wp_registered_sidebars[ $sidebar_id ]['before_sidebar'] );
     268        $this->assertStringContainsString( '</div> <!-- .before-sidebar -->', $wp_registered_sidebars[ $sidebar_id ]['after_sidebar'] );
    269269
    270270    }
     
    379379        $widget->widget( $args, $instance );
    380380        $output = ob_get_clean();
    381         $this->assertNotContains( 'no-options-widget', $output );
    382         $this->assertContains( '<h2>Buscar</h2>', $output );
    383         $this->assertContains( '<section>', $output );
    384         $this->assertContains( '</section>', $output );
     381        $this->assertStringNotContainsString( 'no-options-widget', $output );
     382        $this->assertStringContainsString( '<h2>Buscar</h2>', $output );
     383        $this->assertStringContainsString( '<section>', $output );
     384        $this->assertStringContainsString( '</section>', $output );
    385385    }
    386386
     
    394394        $output = ob_get_clean();
    395395        $this->assertSame( 'noform', $retval );
    396         $this->assertContains( 'no-options-widget', $output );
     396        $this->assertStringContainsString( 'no-options-widget', $output );
    397397    }
    398398
     
    741741        $this->assertNotEmpty( $control );
    742742
    743         $this->assertContains( '<div class="widget-top">', $control );
    744         $this->assertContains( '<div class="widget-title-action">', $control );
    745         $this->assertContains( '<div class="widget-title">', $control );
    746         $this->assertContains( '<form method="post">', $control );
    747         $this->assertContains( '<div class="widget-content">', $control );
    748         $this->assertContains( '<input class="widefat"', $control );
    749         $this->assertContains( '<input type="hidden" name="id_base" class="id_base" value="search"', $control );
    750         $this->assertContains( '<div class="widget-control-actions">', $control );
    751         $this->assertContains( '<div class="alignleft">', $control );
    752         $this->assertContains( 'widget-control-remove', $control );
    753         $this->assertContains( 'widget-control-close', $control );
    754         $this->assertContains( '<div class="alignright">', $control );
    755         $this->assertContains( '<input type="submit" name="savewidget"', $control );
     743        $this->assertStringContainsString( '<div class="widget-top">', $control );
     744        $this->assertStringContainsString( '<div class="widget-title-action">', $control );
     745        $this->assertStringContainsString( '<div class="widget-title">', $control );
     746        $this->assertStringContainsString( '<form method="post">', $control );
     747        $this->assertStringContainsString( '<div class="widget-content">', $control );
     748        $this->assertStringContainsString( '<input class="widefat"', $control );
     749        $this->assertStringContainsString( '<input type="hidden" name="id_base" class="id_base" value="search"', $control );
     750        $this->assertStringContainsString( '<div class="widget-control-actions">', $control );
     751        $this->assertStringContainsString( '<div class="alignleft">', $control );
     752        $this->assertStringContainsString( 'widget-control-remove', $control );
     753        $this->assertStringContainsString( 'widget-control-close', $control );
     754        $this->assertStringContainsString( '<div class="alignright">', $control );
     755        $this->assertStringContainsString( '<input type="submit" name="savewidget"', $control );
    756756
    757757        $param_overrides = array(
     
    772772        $control = ob_get_clean();
    773773        $this->assertNotEmpty( $control );
    774         $this->assertNotContains( '<form method="post">', $control );
    775         $this->assertNotContains( '<div class="widget-content">', $control );
     774        $this->assertStringNotContainsString( '<form method="post">', $control );
     775        $this->assertStringNotContainsString( '<div class="widget-content">', $control );
    776776
    777777        foreach ( $param_overrides as $contained ) {
    778             $this->assertContains( $contained, $control );
     778            $this->assertStringContainsString( $contained, $control );
    779779        }
    780780    }
Note: See TracChangeset for help on using the changeset viewer.