Make WordPress Core


Ignore:
Timestamp:
07/19/2021 02:00:11 PM (3 years 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/nav-menus.php

    r51331 r51462  
    746746        );
    747747
    748         $this->assertContains( $expected, $template );
     748        $this->assertStringContainsString( $expected, $template );
    749749    }
    750750
     
    765765        $expected = sprintf( 'Customizing &#9656; %s', esc_html( $this->wp_customize->get_panel( 'nav_menus' )->title ) );
    766766
    767         $this->assertContains( $expected, $template );
     767        $this->assertStringContainsString( $expected, $template );
    768768
    769769        $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
    770770        if ( $post_types ) {
    771771            foreach ( $post_types as $type ) {
    772                 $this->assertContains( 'available-menu-items-post_type-' . esc_attr( $type->name ), $template );
     772                $this->assertStringContainsString( 'available-menu-items-post_type-' . esc_attr( $type->name ), $template );
    773773                $this->assertRegExp( '#<h4 class="accordion-section-title".*>\s*' . esc_html( $type->labels->name ) . '#', $template );
    774                 $this->assertContains( 'data-type="post_type"', $template );
    775                 $this->assertContains( 'data-object="' . esc_attr( $type->name ) . '"', $template );
    776                 $this->assertContains( 'data-type_label="' . esc_attr( $type->labels->singular_name ) . '"', $template );
     774                $this->assertStringContainsString( 'data-type="post_type"', $template );
     775                $this->assertStringContainsString( 'data-object="' . esc_attr( $type->name ) . '"', $template );
     776                $this->assertStringContainsString( 'data-type_label="' . esc_attr( $type->labels->singular_name ) . '"', $template );
    777777            }
    778778        }
     
    781781        if ( $taxonomies ) {
    782782            foreach ( $taxonomies as $tax ) {
    783                 $this->assertContains( 'available-menu-items-taxonomy-' . esc_attr( $tax->name ), $template );
     783                $this->assertStringContainsString( 'available-menu-items-taxonomy-' . esc_attr( $tax->name ), $template );
    784784                $this->assertRegExp( '#<h4 class="accordion-section-title".*>\s*' . esc_html( $tax->labels->name ) . '#', $template );
    785                 $this->assertContains( 'data-type="taxonomy"', $template );
    786                 $this->assertContains( 'data-object="' . esc_attr( $tax->name ) . '"', $template );
    787                 $this->assertContains( 'data-type_label="' . esc_attr( $tax->labels->singular_name ) . '"', $template );
     785                $this->assertStringContainsString( 'data-type="taxonomy"', $template );
     786                $this->assertStringContainsString( 'data-object="' . esc_attr( $tax->name ) . '"', $template );
     787                $this->assertStringContainsString( 'data-type_label="' . esc_attr( $tax->labels->singular_name ) . '"', $template );
    788788            }
    789789        }
    790790
    791         $this->assertContains( 'available-menu-items-custom_type', $template );
     791        $this->assertStringContainsString( 'available-menu-items-custom_type', $template );
    792792        $this->assertRegExp( '#<h4 class="accordion-section-title".*>\s*Custom#', $template );
    793         $this->assertContains( 'data-type="custom_type"', $template );
    794         $this->assertContains( 'data-object="custom_object"', $template );
    795         $this->assertContains( 'data-type_label="Custom Type"', $template );
     793        $this->assertStringContainsString( 'data-type="custom_type"', $template );
     794        $this->assertStringContainsString( 'data-object="custom_object"', $template );
     795        $this->assertStringContainsString( 'data-type_label="Custom Type"', $template );
    796796    }
    797797
     
    11411141        $result = $menus->filter_wp_nav_menu( $nav_menu_content, (object) $args );
    11421142
    1143         $this->assertContains( sprintf( ' data-customize-partial-id="nav_menu_instance[%s]"', $args['customize_preview_nav_menus_args']['args_hmac'] ), $result );
    1144         $this->assertContains( ' data-customize-partial-type="nav_menu_instance"', $result );
     1143        $this->assertStringContainsString( sprintf( ' data-customize-partial-id="nav_menu_instance[%s]"', $args['customize_preview_nav_menus_args']['args_hmac'] ), $result );
     1144        $this->assertStringContainsString( ' data-customize-partial-type="nav_menu_instance"', $result );
    11451145        $this->assertTrue( (bool) preg_match( '/data-customize-partial-placement-context="(.+?)"/', $result, $matches ) );
    11461146        $context = json_decode( html_entity_decode( $matches[1] ), true );
     
    12261226
    12271227        $rendered = $partial->render( $nav_menu_args['customize_preview_nav_menus_args'] );
    1228         $this->assertContains( 'data-customize-partial-type="nav_menu_instance"', $rendered );
    1229         $this->assertContains( 'WordPress.org', $rendered );
     1228        $this->assertStringContainsString( 'data-customize-partial-type="nav_menu_instance"', $rendered );
     1229        $this->assertStringContainsString( 'WordPress.org', $rendered );
    12301230    }
    12311231}
Note: See TracChangeset for help on using the changeset viewer.