Skip to content

Commit

Permalink
Tests: Replace assertContains() with assertStringContainsString()
Browse files Browse the repository at this point in the history
… 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.

git-svn-id: https://develop.svn.wordpress.org/trunk@51462 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Jul 19, 2021
1 parent bb3bf22 commit c70fe62
Show file tree
Hide file tree
Showing 97 changed files with 812 additions and 812 deletions.
46 changes: 23 additions & 23 deletions tests/phpunit/tests/admin/includesListTable.php
Expand Up @@ -221,7 +221,7 @@ protected function _test_list_hierarchical_page( array $args, array $expected_id
$this->assertCount( count( $expected_ids ), array_keys( $matches[0] ) );

foreach ( $expected_ids as $id ) {
$this->assertContains( sprintf( 'id="post-%d"', $id ), $output );
$this->assertStringContainsString( sprintf( 'id="post-%d"', $id ), $output );
}
}

Expand All @@ -236,7 +236,7 @@ function test_filter_button_should_not_be_shown_if_there_are_no_posts() {
$this->table->extra_tablenav( 'top' );
$output = ob_get_clean();

$this->assertNotContains( 'id="post-query-submit"', $output );
$this->assertStringNotContainsString( 'id="post-query-submit"', $output );
}

/**
Expand All @@ -250,7 +250,7 @@ function test_months_dropdown_should_not_be_shown_if_there_are_no_posts() {
$this->table->extra_tablenav( 'top' );
$output = ob_get_clean();

$this->assertNotContains( 'id="filter-by-date"', $output );
$this->assertStringNotContainsString( 'id="filter-by-date"', $output );
}

/**
Expand All @@ -264,7 +264,7 @@ function test_category_dropdown_should_not_be_shown_if_there_are_no_posts() {
$this->table->extra_tablenav( 'top' );
$output = ob_get_clean();

$this->assertNotContains( 'id="cat"', $output );
$this->assertStringNotContainsString( 'id="cat"', $output );
}

/**
Expand All @@ -278,7 +278,7 @@ public function test_empty_trash_button_should_not_be_shown_if_there_are_no_post
$this->table->extra_tablenav( 'top' );
$output = ob_get_clean();

$this->assertNotContains( 'id="delete_all"', $output );
$this->assertStringNotContainsString( 'id="delete_all"', $output );
}

/**
Expand All @@ -291,7 +291,7 @@ public function test_filter_button_should_not_be_shown_if_there_are_no_comments(
$table->extra_tablenav( 'top' );
$output = ob_get_clean();

$this->assertNotContains( 'id="post-query-submit"', $output );
$this->assertStringNotContainsString( 'id="post-query-submit"', $output );
}

/**
Expand All @@ -313,7 +313,7 @@ public function test_filter_button_should_be_shown_if_there_are_comments() {
$table->extra_tablenav( 'top' );
$output = ob_get_clean();

$this->assertContains( 'id="post-query-submit"', $output );
$this->assertStringContainsString( 'id="post-query-submit"', $output );
}

/**
Expand All @@ -335,8 +335,8 @@ public function test_filter_comment_type_dropdown_should_be_shown_if_there_are_c
$table->extra_tablenav( 'top' );
$output = ob_get_clean();

$this->assertContains( 'id="filter-by-comment-type"', $output );
$this->assertContains( "<option value='comment'>", $output );
$this->assertStringContainsString( 'id="filter-by-comment-type"', $output );
$this->assertStringContainsString( "<option value='comment'>", $output );
}

/**
Expand All @@ -349,7 +349,7 @@ public function test_empty_trash_button_should_not_be_shown_if_there_are_no_comm
$table->extra_tablenav( 'top' );
$output = ob_get_clean();

$this->assertNotContains( 'id="delete_all"', $output );
$this->assertStringNotContainsString( 'id="delete_all"', $output );
}

/**
Expand Down Expand Up @@ -384,7 +384,7 @@ function() {
OPTIONS;
$expected = str_replace( "\r\n", "\n", $expected );

$this->assertContains( $expected, $output );
$this->assertStringContainsString( $expected, $output );
}

/**
Expand All @@ -411,14 +411,14 @@ public function test_sortable_columns() {

$output = get_echo( array( $object, 'print_column_headers' ) );

$this->assertContains( '?orderby=comment_author&#038;order=desc', $output, 'Mismatch of the default link ordering for comment author column. Should be desc.' );
$this->assertContains( 'column-author sortable asc', $output, 'Mismatch of CSS classes for the comment author column.' );
$this->assertStringContainsString( '?orderby=comment_author&#038;order=desc', $output, 'Mismatch of the default link ordering for comment author column. Should be desc.' );
$this->assertStringContainsString( 'column-author sortable asc', $output, 'Mismatch of CSS classes for the comment author column.' );

$this->assertContains( '?orderby=comment_post_ID&#038;order=asc', $output, 'Mismatch of the default link ordering for comment response column. Should be asc.' );
$this->assertContains( 'column-response sortable desc', $output, 'Mismatch of CSS classes for the comment post ID column.' );
$this->assertStringContainsString( '?orderby=comment_post_ID&#038;order=asc', $output, 'Mismatch of the default link ordering for comment response column. Should be asc.' );
$this->assertStringContainsString( 'column-response sortable desc', $output, 'Mismatch of CSS classes for the comment post ID column.' );

$this->assertContains( '?orderby=comment_date&#038;order=desc', $output, 'Mismatch of the default link ordering for comment date column. Should be asc.' );
$this->assertContains( 'column-date sortable asc', $output, 'Mismatch of CSS classes for the comment date column.' );
$this->assertStringContainsString( '?orderby=comment_date&#038;order=desc', $output, 'Mismatch of the default link ordering for comment date column. Should be asc.' );
$this->assertStringContainsString( 'column-date sortable asc', $output, 'Mismatch of CSS classes for the comment date column.' );
}

/**
Expand Down Expand Up @@ -449,14 +449,14 @@ public function test_sortable_columns_with_current_ordering() {

$output = get_echo( array( $object, 'print_column_headers' ) );

$this->assertContains( '?orderby=comment_author&#038;order=asc', $output, 'Mismatch of the default link ordering for comment author column. Should be asc.' );
$this->assertContains( 'column-author sortable desc', $output, 'Mismatch of CSS classes for the comment author column.' );
$this->assertStringContainsString( '?orderby=comment_author&#038;order=asc', $output, 'Mismatch of the default link ordering for comment author column. Should be asc.' );
$this->assertStringContainsString( 'column-author sortable desc', $output, 'Mismatch of CSS classes for the comment author column.' );

$this->assertContains( '?orderby=comment_post_ID&#038;order=asc', $output, 'Mismatch of the default link ordering for comment response column. Should be asc.' );
$this->assertContains( 'column-response sortable desc', $output, 'Mismatch of CSS classes for the comment post ID column.' );
$this->assertStringContainsString( '?orderby=comment_post_ID&#038;order=asc', $output, 'Mismatch of the default link ordering for comment response column. Should be asc.' );
$this->assertStringContainsString( 'column-response sortable desc', $output, 'Mismatch of CSS classes for the comment post ID column.' );

$this->assertContains( '?orderby=comment_date&#038;order=asc', $output, 'Mismatch of the current link ordering for comment date column. Should be asc.' );
$this->assertContains( 'column-date sorted desc', $output, 'Mismatch of CSS classes for the comment date column.' );
$this->assertStringContainsString( '?orderby=comment_date&#038;order=asc', $output, 'Mismatch of the current link ordering for comment date column. Should be asc.' );
$this->assertStringContainsString( 'column-date sorted desc', $output, 'Mismatch of CSS classes for the comment date column.' );
}

}
26 changes: 13 additions & 13 deletions tests/phpunit/tests/admin/includesPost.php
Expand Up @@ -391,8 +391,8 @@ public function test_get_sample_permalink_html_should_use_default_permalink_for_
);

$found = get_sample_permalink_html( $p );
$this->assertContains( 'href="' . get_option( 'home' ) . '/?p=' . $p . '"', $found );
$this->assertContains( '>' . get_option( 'home' ) . '/?p=' . $p . '<', $found );
$this->assertStringContainsString( 'href="' . get_option( 'home' ) . '/?p=' . $p . '"', $found );
$this->assertStringContainsString( '>' . get_option( 'home' ) . '/?p=' . $p . '<', $found );
}

/**
Expand All @@ -415,8 +415,8 @@ public function test_get_sample_permalink_html_should_use_pretty_permalink_for_v

$found = get_sample_permalink_html( $p );
$post = get_post( $p );
$this->assertContains( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found );
$this->assertContains( '>' . urldecode( $post->post_name ) . '<', $found );
$this->assertStringContainsString( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found );
$this->assertStringContainsString( '>' . urldecode( $post->post_name ) . '<', $found );
}

/**
Expand All @@ -440,8 +440,8 @@ public function test_get_sample_permalink_html_should_use_pretty_permalink_for_v

$found = get_sample_permalink_html( $p );
$post = get_post( $p );
$this->assertContains( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found );
$this->assertContains( '>' . urldecode( get_permalink( $post ) ) . '<', $found );
$this->assertStringContainsString( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found );
$this->assertStringContainsString( '>' . urldecode( get_permalink( $post ) ) . '<', $found );
}

/**
Expand All @@ -464,8 +464,8 @@ public function test_get_sample_permalink_html_should_use_correct_permalink_for_
$found = get_sample_permalink_html( $p, null, 'new_slug-صورة' );
$post = get_post( $p );
$message = 'Published post';
$this->assertContains( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found, $message );
$this->assertContains( '>new_slug-صورة<', $found, $message );
$this->assertStringContainsString( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found, $message );
$this->assertStringContainsString( '>new_slug-صورة<', $found, $message );

// Scheduled posts should use published permalink.
$future_date = gmdate( 'Y-m-d H:i:s', time() + 100 );
Expand All @@ -480,8 +480,8 @@ public function test_get_sample_permalink_html_should_use_correct_permalink_for_
$found = get_sample_permalink_html( $p, null, 'new_slug-صورة' );
$post = get_post( $p );
$message = 'Scheduled post';
$this->assertContains( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found, $message );
$this->assertContains( '>new_slug-صورة<', $found, $message );
$this->assertStringContainsString( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found, $message );
$this->assertStringContainsString( '>new_slug-صورة<', $found, $message );

// Draft posts should use preview link.
$p = self::factory()->post->create(
Expand All @@ -498,8 +498,8 @@ public function test_get_sample_permalink_html_should_use_correct_permalink_for_
$preview_link = get_permalink( $post->ID );
$preview_link = add_query_arg( 'preview', 'true', $preview_link );

$this->assertContains( 'href="' . esc_url( $preview_link ) . '"', $found, $message );
$this->assertContains( '>new_slug-صورة<', $found, $message );
$this->assertStringContainsString( 'href="' . esc_url( $preview_link ) . '"', $found, $message );
$this->assertStringContainsString( '>new_slug-صورة<', $found, $message );
}

/**
Expand All @@ -522,7 +522,7 @@ public function test_get_sample_permalink_html_should_use_preview_links_for_draf

$found = get_sample_permalink_html( $p );
$post = get_post( $p );
$this->assertContains( 'href="' . esc_url( get_preview_post_link( $post ) ), $found );
$this->assertStringContainsString( 'href="' . esc_url( get_preview_post_link( $post ) ), $found );
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/tests/admin/includesTemplate.php
Expand Up @@ -17,7 +17,7 @@ public function test_wp_terms_checklist_with_selected_cats( $term_id ) {
)
);

$this->assertContains( "checked='checked'", $output );
$this->assertStringContainsString( "checked='checked'", $output );
}

/**
Expand All @@ -33,7 +33,7 @@ public function test_wp_terms_checklist_with_popular_cats( $term_id ) {
)
);

$this->assertContains( 'class="popular-category"', $output );
$this->assertStringContainsString( 'class="popular-category"', $output );
}

public function data_wp_terms_checklist_with_selected_cats() {
Expand Down Expand Up @@ -185,8 +185,8 @@ public function test_settings_errors_css_classes( $type, $expected ) {

$expected = sprintf( 'notice %s settings-error is-dismissible', $expected );

$this->assertContains( $expected, $output );
$this->assertNotContains( 'notice-notice-', $output );
$this->assertStringContainsString( $expected, $output );
$this->assertStringNotContainsString( 'notice-notice-', $output );
}

public function settings_errors_css_classes_provider() {
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/admin/wpPrivacyRequestsTable.php
Expand Up @@ -82,7 +82,7 @@ public function test_columns_should_be_sortable( $order, $orderby, $search, $exp
unset( $_REQUEST['orderby'] );
unset( $_REQUEST['s'] );

$this->assertContains( "ORDER BY {$wpdb->posts}.{$expected}", $this->sql );
$this->assertStringContainsString( "ORDER BY {$wpdb->posts}.{$expected}", $this->sql );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/adminbar.php
Expand Up @@ -264,7 +264,7 @@ public function test_admin_bar_with_tabindex_meta( $node_data, $expected_html )
$admin_bar = new WP_Admin_Bar();
$admin_bar->add_node( $node_data );
$admin_bar_html = get_echo( array( $admin_bar, 'render' ) );
$this->assertContains( $expected_html, $admin_bar_html );
$this->assertStringContainsString( $expected_html, $admin_bar_html );
}

/**
Expand Down Expand Up @@ -678,7 +678,7 @@ public function test_customize_link() {
$query_params = array();
wp_parse_str( $parsed_url['query'], $query_params );
$this->assertSame( $uuid, $query_params['changeset_uuid'] );
$this->assertNotContains( 'changeset_uuid', $query_params['url'] );
$this->assertStringNotContainsString( 'changeset_uuid', $query_params['url'] );
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/tests/ajax/Compression.php
Expand Up @@ -49,7 +49,7 @@ public function test_text() {
}

// Ensure we found the right match.
$this->assertContains( 'wpCompressionTest', $this->_last_response );
$this->assertStringContainsString( 'wpCompressionTest', $this->_last_response );
}

/**
Expand All @@ -74,7 +74,7 @@ public function test_gzdeflate() {
}

// Ensure we found the right match.
$this->assertContains( 'wpCompressionTest', gzinflate( $this->_last_response ) );
$this->assertStringContainsString( 'wpCompressionTest', gzinflate( $this->_last_response ) );
}

/**
Expand All @@ -99,7 +99,7 @@ public function test_gzencode() {
}

// Ensure we found the right match.
$this->assertContains( 'wpCompressionTest', $this->_gzdecode( $this->_last_response ) );
$this->assertStringContainsString( 'wpCompressionTest', $this->_gzdecode( $this->_last_response ) );
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/phpunit/tests/ajax/CustomizeManager.php
Expand Up @@ -474,8 +474,8 @@ public function test_save_autosave() {
$autosave_revision = wp_get_post_autosave( $post_id );
$this->assertInstanceOf( 'WP_Post', $autosave_revision );

$this->assertContains( 'New Site Title', get_post( $post_id )->post_content );
$this->assertContains( 'Autosaved Site Title', $autosave_revision->post_content );
$this->assertStringContainsString( 'New Site Title', get_post( $post_id )->post_content );
$this->assertStringContainsString( 'Autosaved Site Title', $autosave_revision->post_content );
}

/**
Expand Down Expand Up @@ -679,7 +679,7 @@ public function test_handle_dismiss_autosave_or_lock_request() {
$_REQUEST['dismiss_autosave'] = true;
$this->assertNotWPError( $r );
$this->assertFalse( wp_get_post_autosave( $wp_customize->changeset_post_id() ) );
$this->assertContains( 'Foo', get_post( $wp_customize->changeset_post_id() )->post_content );
$this->assertStringContainsString( 'Foo', get_post( $wp_customize->changeset_post_id() )->post_content );

// Since no autosave yet, confirm no action.
$this->make_ajax_call( 'customize_dismiss_autosave_or_lock' );
Expand All @@ -700,8 +700,8 @@ public function test_handle_dismiss_autosave_or_lock_request() {
$this->assertNotWPError( $r );
$autosave_revision = wp_get_post_autosave( $wp_customize->changeset_post_id() );
$this->assertInstanceOf( 'WP_Post', $autosave_revision );
$this->assertContains( 'Foo', get_post( $wp_customize->changeset_post_id() )->post_content );
$this->assertContains( 'Bar', $autosave_revision->post_content );
$this->assertStringContainsString( 'Foo', get_post( $wp_customize->changeset_post_id() )->post_content );
$this->assertStringContainsString( 'Bar', $autosave_revision->post_content );

// Confirm autosave gets deleted.
$this->make_ajax_call( 'customize_dismiss_autosave_or_lock' );
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/ajax/DimComment.php
Expand Up @@ -197,7 +197,7 @@ public function test_with_bad_id() {
// Ensure everything is correct.
$this->assertSame( '0', (string) $xml->response[0]->comment['id'] );
$this->assertSame( 'dim-comment_0', (string) $xml->response['action'] );
$this->assertContains( 'Comment ' . $_POST['id'] . ' does not exist', $this->_last_response );
$this->assertStringContainsString( 'Comment ' . $_POST['id'] . ' does not exist', $this->_last_response );

} catch ( Exception $e ) {
$this->fail( 'Unexpected exception type: ' . get_class( $e ) );
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/ajax/ReplytoComment.php
Expand Up @@ -219,7 +219,7 @@ public function test_blocked_comment() {
$this->fail();
} catch ( WPAjaxDieStopException $e ) {
$wpdb->suppress_errors( false );
$this->assertContains( '1', $e->getMessage() );
$this->assertStringContainsString( '1', $e->getMessage() );
}
}

Expand Down
10 changes: 5 additions & 5 deletions tests/phpunit/tests/blocks/block-editor.php
Expand Up @@ -435,7 +435,7 @@ function test_block_editor_rest_api_preload_no_paths() {
block_editor_rest_api_preload( array(), $editor_context );

$after = implode( '', wp_scripts()->registered['wp-api-fetch']->extra['after'] );
$this->assertNotContains( 'wp.apiFetch.createPreloadingMiddleware', $after );
$this->assertStringNotContainsString( 'wp.apiFetch.createPreloadingMiddleware', $after );
}

/**
Expand All @@ -462,7 +462,7 @@ function filter_remove_preload_paths( $preload_paths, $post ) {
remove_filter( 'block_editor_preload_paths', 'filter_remove_preload_paths' );

$after = implode( '', wp_scripts()->registered['wp-api-fetch']->extra['after'] );
$this->assertNotContains( 'wp.apiFetch.createPreloadingMiddleware', $after );
$this->assertStringNotContainsString( 'wp.apiFetch.createPreloadingMiddleware', $after );
}

/**
Expand All @@ -489,9 +489,9 @@ function filter_add_preload_paths( $preload_paths, WP_Block_Editor_Context $cont
remove_filter( 'block_editor_rest_api_preload_paths', 'filter_add_preload_paths' );

$after = implode( '', wp_scripts()->registered['wp-api-fetch']->extra['after'] );
$this->assertContains( 'wp.apiFetch.createPreloadingMiddleware', $after );
$this->assertContains( '"\/wp\/v2\/blocks"', $after );
$this->assertContains( '"\/wp\/v2\/types"', $after );
$this->assertStringContainsString( 'wp.apiFetch.createPreloadingMiddleware', $after );
$this->assertStringContainsString( '"\/wp\/v2\/blocks"', $after );
$this->assertStringContainsString( '"\/wp\/v2\/types"', $after );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/blocks/render.php
Expand Up @@ -304,7 +304,7 @@ public function test_render_latest_comments_on_password_protected_post() {
);
$comments = do_blocks( '<!-- wp:latest-comments {"commentsToShow":1,"displayExcerpt":true} /-->' );

$this->assertNotContains( $comment_text, $comments );
$this->assertStringNotContainsString( $comment_text, $comments );
}

/**
Expand Down

0 comments on commit c70fe62

Please sign in to comment.