WordPress.org

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

    r51331 r51462  
    580580        $widget_control = $this->manager->widgets->get_widget_control( $this->get_test_widget_control_args() );
    581581
    582         $this->assertContains( '<div class="form">', $widget_control );
    583         $this->assertContains( '<div class="widget-content">', $widget_control );
    584         $this->assertContains( '<input type="hidden" name="id_base" class="id_base" value="search"', $widget_control );
    585         $this->assertContains( '<input class="widefat"', $widget_control );
     582        $this->assertStringContainsString( '<div class="form">', $widget_control );
     583        $this->assertStringContainsString( '<div class="widget-content">', $widget_control );
     584        $this->assertStringContainsString( '<input type="hidden" name="id_base" class="id_base" value="search"', $widget_control );
     585        $this->assertStringContainsString( '<input class="widefat"', $widget_control );
    586586    }
    587587
     
    595595        $this->assertArrayHasKey( 'control', $widget_control_parts );
    596596
    597         $this->assertContains( '<div class="form">', $widget_control_parts['control'] );
    598         $this->assertContains( '<div class="widget-content">', $widget_control_parts['control'] );
    599         $this->assertContains( '<input type="hidden" name="id_base" class="id_base" value="search"', $widget_control_parts['control'] );
    600         $this->assertNotContains( '<input class="widefat"', $widget_control_parts['control'] );
    601         $this->assertContains( '<input class="widefat"', $widget_control_parts['content'] );
     597        $this->assertStringContainsString( '<div class="form">', $widget_control_parts['control'] );
     598        $this->assertStringContainsString( '<div class="widget-content">', $widget_control_parts['control'] );
     599        $this->assertStringContainsString( '<input type="hidden" name="id_base" class="id_base" value="search"', $widget_control_parts['control'] );
     600        $this->assertStringNotContainsString( '<input class="widefat"', $widget_control_parts['control'] );
     601        $this->assertStringContainsString( '<input class="widefat"', $widget_control_parts['content'] );
    602602    }
    603603
     
    613613        $this->assertRegExp( '#^<li[^>]+>\s*</li>$#', $params['content'] );
    614614        $this->assertRegExp( '#^<div[^>]*class=\'widget\'[^>]*#s', $params['widget_control'] );
    615         $this->assertContains( '<div class="widget-content"></div>', $params['widget_control'] );
    616         $this->assertNotContains( '<input class="widefat"', $params['widget_control'] );
    617         $this->assertContains( '<input class="widefat"', $params['widget_content'] );
     615        $this->assertStringContainsString( '<div class="widget-content"></div>', $params['widget_control'] );
     616        $this->assertStringNotContainsString( '<input class="widefat"', $params['widget_control'] );
     617        $this->assertStringContainsString( '<input class="widefat"', $params['widget_content'] );
    618618        $this->assertSame( 'search-2', $params['widget_id'] );
    619619        $this->assertSame( 'search', $params['widget_id_base'] );
     
    682682        $this->assertArrayHasKey( 'form', $result );
    683683        $this->assertSame( $instance, $result['instance'] );
    684         $this->assertContains( sprintf( 'value="%s"', esc_attr( $instance['title'] ) ), $result['form'] );
     684        $this->assertStringContainsString( sprintf( 'value="%s"', esc_attr( $instance['title'] ) ), $result['form'] );
    685685
    686686        $post_values = $this->manager->unsanitized_post_values();
     
    815815
    816816        $output = wp_kses_post( $filtered_params[0]['before_widget'] );
    817         $this->assertContains( 'data-customize-partial-id="widget[search-2]"', $output );
    818         $this->assertContains( 'data-customize-partial-type="widget"', $output );
     817        $this->assertStringContainsString( 'data-customize-partial-id="widget[search-2]"', $output );
     818        $this->assertStringContainsString( 'data-customize-partial-type="widget"', $output );
    819819    }
    820820
     
    842842        $this->assertSame( 1, substr_count( $output, 'data-customize-partial-id' ) );
    843843        $this->assertSame( 1, substr_count( $output, 'data-customize-partial-type="widget"' ) );
    844         $this->assertContains( ' id="search-2"', $output );
     844        $this->assertStringContainsString( ' id="search-2"', $output );
    845845    }
    846846
Note: See TracChangeset for help on using the changeset viewer.