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

    r51438 r51462  
    243243        }
    244244        $this->assertInstanceOf( 'WPDieException', $exception );
    245         $this->assertContains( 'you are not allowed to customize this site', $exception->getMessage() );
     245        $this->assertStringContainsString( 'you are not allowed to customize this site', $exception->getMessage() );
    246246
    247247        // Bad changeset.
     
    255255        }
    256256        $this->assertInstanceOf( 'WPDieException', $exception );
    257         $this->assertContains( 'Invalid changeset UUID', $exception->getMessage() );
     257        $this->assertStringContainsString( 'Invalid changeset UUID', $exception->getMessage() );
    258258
    259259        update_option( 'fresh_site', '0' );
     
    312312        }
    313313        $this->assertInstanceOf( 'WPDieException', $exception );
    314         $this->assertContains( 'Non-existent changeset UUID', $exception->getMessage() );
     314        $this->assertStringContainsString( 'Non-existent changeset UUID', $exception->getMessage() );
    315315
    316316        wp_set_current_user( self::$admin_user_id );
     
    718718        $this->assertSame( 'waffles', get_post_meta( $posts_by_name['waffles'], '_customize_draft_post_name', true ) );
    719719        $this->assertArrayHasKey( 'file', $attachment_metadata );
    720         $this->assertContains( 'waffles', $attachment_metadata['file'] );
     720        $this->assertStringContainsString( 'waffles', $attachment_metadata['file'] );
    721721
    722722        $this->assertSame( 'page', $changeset_values['show_on_front'] );
     
    799799        $this->assertNotEmpty( get_header_image() );
    800800        $this->assertNotEmpty( get_background_image() );
    801         $this->assertContains( 'canola', get_custom_logo() );
    802         $this->assertContains( 'waffles', get_header_image() );
    803         $this->assertContains( 'waffles', get_background_image() );
     801        $this->assertStringContainsString( 'canola', get_custom_logo() );
     802        $this->assertStringContainsString( 'waffles', get_header_image() );
     803        $this->assertStringContainsString( 'waffles', get_background_image() );
    804804        $this->assertSame( 'waffles', get_post( $posts_by_name['waffles'] )->post_name );
    805805        $this->assertEmpty( get_post_meta( $posts_by_name['waffles'], '_customize_draft_post_name', true ) );
     
    921921        }
    922922        $this->assertNotNull( $exception );
    923         $this->assertContains( 'Unauthorized', $exception->getMessage() );
     923        $this->assertStringContainsString( 'Unauthorized', $exception->getMessage() );
    924924    }
    925925
     
    12111211        $this->assertSame( 'trash', get_post_status( $post_id ) ); // Auto-trashed.
    12121212        $this->assertSame( $original_capabilities, wp_list_pluck( $manager->settings(), 'capability' ) );
    1213         $this->assertContains( '<script>', get_post( $post_id )->post_content );
     1213        $this->assertStringContainsString( '<script>', get_post( $post_id )->post_content );
    12141214        $this->assertSame( $manager->changeset_uuid(), get_post( $post_id )->post_name, 'Expected that the "__trashed" suffix to not be added.' );
    12151215        wp_set_current_user( self::$admin_user_id );
     
    13401340
    13411341        // User saved as one who can bypass content_save_pre filter.
    1342         $this->assertContains( '<script>', get_option( 'custom_html_1' ) );
    1343         $this->assertContains( 'Wordpress', get_option( 'custom_html_1' ) ); // phpcs:ignore WordPress.WP.CapitalPDangit.Misspelled
     1342        $this->assertStringContainsString( '<script>', get_option( 'custom_html_1' ) );
     1343        $this->assertStringContainsString( 'Wordpress', get_option( 'custom_html_1' ) ); // phpcs:ignore WordPress.WP.CapitalPDangit.Misspelled
    13441344
    13451345        // User saved as one who cannot bypass content_save_pre filter.
    1346         $this->assertNotContains( '<script>', get_option( 'custom_html_2' ) );
    1347         $this->assertContains( 'WordPress', get_option( 'custom_html_2' ) );
     1346        $this->assertStringNotContainsString( '<script>', get_option( 'custom_html_2' ) );
     1347        $this->assertStringContainsString( 'WordPress', get_option( 'custom_html_2' ) );
    13481348
    13491349        // User saved as one who also cannot bypass content_save_pre filter.
    1350         $this->assertNotContains( '<script>', get_option( 'custom_html_3' ) );
    1351         $this->assertContains( 'WordPress', get_option( 'custom_html_3' ) );
     1350        $this->assertStringNotContainsString( '<script>', get_option( 'custom_html_3' ) );
     1351        $this->assertStringContainsString( 'WordPress', get_option( 'custom_html_3' ) );
    13521352    }
    13531353
     
    18341834        );
    18351835        $this->assertFalse( wp_get_post_autosave( $changeset_post_id, get_current_user_id() ) );
    1836         $this->assertContains( 'Autosaved Auto-draft Title', get_post( $changeset_post_id )->post_content );
     1836        $this->assertStringContainsString( 'Autosaved Auto-draft Title', get_post( $changeset_post_id )->post_content );
    18371837
    18381838        // Update status to draft for subsequent tests.
     
    18481848            )
    18491849        );
    1850         $this->assertContains( 'Draft Title', get_post( $changeset_post_id )->post_content );
     1850        $this->assertStringContainsString( 'Draft Title', get_post( $changeset_post_id )->post_content );
    18511851
    18521852        // Fail: illegal_autosave_with_date_gmt.
     
    18951895        $autosave_revision = wp_get_post_autosave( $changeset_post_id, get_current_user_id() );
    18961896        $this->assertInstanceOf( 'WP_Post', $autosave_revision );
    1897         $this->assertContains( 'Draft Title', get_post( $changeset_post_id )->post_content );
    1898         $this->assertContains( 'Autosave Title', $autosave_revision->post_content );
     1897        $this->assertStringContainsString( 'Draft Title', get_post( $changeset_post_id )->post_content );
     1898        $this->assertStringContainsString( 'Autosave Title', $autosave_revision->post_content );
    18991899    }
    19001900
     
    29412941    function test_get_document_title_template() {
    29422942        $tpl = $this->manager->get_document_title_template();
    2943         $this->assertContains( '%s', $tpl );
     2943        $this->assertStringContainsString( '%s', $tpl );
    29442944    }
    29452945
     
    30803080        $content = ob_get_clean();
    30813081
    3082         $this->assertContains( 'var _wpCustomizeSettings =', $content );
    3083         $this->assertContains( '"blogname"', $content );
    3084         $this->assertContains( '"type":"option"', $content );
    3085         $this->assertContains( '_wpCustomizeSettings.controls', $content );
    3086         $this->assertContains( '_wpCustomizeSettings.settings', $content );
    3087         $this->assertContains( '</script>', $content );
     3082        $this->assertStringContainsString( 'var _wpCustomizeSettings =', $content );
     3083        $this->assertStringContainsString( '"blogname"', $content );
     3084        $this->assertStringContainsString( '"type":"option"', $content );
     3085        $this->assertStringContainsString( '_wpCustomizeSettings.controls', $content );
     3086        $this->assertStringContainsString( '_wpCustomizeSettings.settings', $content );
     3087        $this->assertStringContainsString( '</script>', $content );
    30883088
    30893089        $this->assertNotEmpty( preg_match( '#var _wpCustomizeSettings\s*=\s*({.*?});\s*\n#', $content, $matches ) );
     
    31323132        $manager->remove_frameless_preview_messenger_channel();
    31333133        $output = ob_get_clean();
    3134         $this->assertContains( '<script>', $output );
     3134        $this->assertStringContainsString( '<script>', $output );
    31353135    }
    31363136
Note: See TracChangeset for help on using the changeset viewer.