Tests: Replace assertRegExp() with assertMatchesRegularExpression().

The `assertRegExp()` and `assertNotRegExp()` methods were hard deprecated in PHPUnit 9.1 and the functionality will be removed in PHPUnit 10.0.

The `assertMatchesRegularExpression()` and `assertDoesNotMatchRegularExpression()` methods were introduced as a replacement in PHPUnit 9.1.

These new PHPUnit methods are polyfilled by the PHPUnit Polyfills and switching to them will future-proof the tests some more.

References:
* https://github.com/sebastianbergmann/phpunit/blob/9.1.5/ChangeLog-9.1.md#910---2020-04-03
* https://github.com/sebastianbergmann/phpunit/issues/4085
* https://github.com/sebastianbergmann/phpunit/issues/4088

Follow-up to [51559-51564].

Props jrf.
See #46149.

git-svn-id: https://develop.svn.wordpress.org/trunk@51565 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2021-08-06 21:52:06 +00:00
parent 29d678d08b
commit 457b7b3a61
16 changed files with 39 additions and 39 deletions

View File

@ -100,6 +100,6 @@ class Tests_Ajax_Response extends WP_UnitTestCase {
// Check the XML tag.
$contents = ob_get_clean();
$this->assertRegExp( '/<\?xml\s+version=\'1.0\'\s+encoding=\'' . preg_quote( get_option( 'blog_charset' ) ) . '\'\s+standalone=\'yes\'\?>/', $contents );
$this->assertMatchesRegularExpression( '/<\?xml\s+version=\'1.0\'\s+encoding=\'' . preg_quote( get_option( 'blog_charset' ) ) . '\'\s+standalone=\'yes\'\?>/', $contents );
}
}

View File

@ -257,7 +257,7 @@ class Tests_Avatar extends WP_UnitTestCase {
$actual_data = get_avatar_data( $comment );
$this->assertTrue( is_avatar_comment_type( $comment_type ) );
$this->assertRegexp( '|^http?://[0-9]+.gravatar.com/avatar/[0-9a-f]{32}\?|', $actual_data['url'] );
$this->assertMatchesRegularExpression( '|^http?://[0-9]+.gravatar.com/avatar/[0-9a-f]{32}\?|', $actual_data['url'] );
}
/**

View File

@ -202,7 +202,7 @@ class Tests_Category_WpDropdownCategories extends WP_UnitTestCase {
$dropdown_categories = wp_dropdown_categories( $args );
// Test to see if it contains the "required" attribute.
$this->assertRegExp( '/<select[^>]+required/', $dropdown_categories );
$this->assertMatchesRegularExpression( '/<select[^>]+required/', $dropdown_categories );
}
/**

View File

@ -30,7 +30,7 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
);
$this->assertNotRegExp( '/class="[^"]*cat-item-' . $c1 . '[^"]*current-cat[^"]*"/', $found );
$this->assertRegExp( '/class="[^"]*cat-item-' . $c2 . '[^"]*current-cat[^"]*"/', $found );
$this->assertMatchesRegularExpression( '/class="[^"]*cat-item-' . $c2 . '[^"]*current-cat[^"]*"/', $found );
}
public function test_class_containing_current_cat_parent() {
@ -49,7 +49,7 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
)
);
$this->assertRegExp( '/class="[^"]*cat-item-' . $c1 . '[^"]*current-cat-parent[^"]*"/', $found );
$this->assertMatchesRegularExpression( '/class="[^"]*cat-item-' . $c1 . '[^"]*current-cat-parent[^"]*"/', $found );
$this->assertNotRegExp( '/class="[^"]*cat-item-' . $c2 . '[^"]*current-cat-parent[^"]*"/', $found );
}
@ -67,9 +67,9 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
)
);
$this->assertRegExp( '/class="[^"]*cat-item-' . $cats[0] . '[^"]*current-cat[^"]*"/', $found );
$this->assertMatchesRegularExpression( '/class="[^"]*cat-item-' . $cats[0] . '[^"]*current-cat[^"]*"/', $found );
$this->assertNotRegExp( '/class="[^"]*cat-item-' . $cats[1] . '[^"]*current[^"]*"/', $found );
$this->assertRegExp( '/class="[^"]*cat-item-' . $cats[2] . '[^"]*current-cat[^"]*"/', $found );
$this->assertMatchesRegularExpression( '/class="[^"]*cat-item-' . $cats[2] . '[^"]*current-cat[^"]*"/', $found );
}
/**
@ -650,8 +650,8 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
)
);
$this->assertRegExp( '/class="[^"]*cat-item-' . $parent . '[^"]*current-cat-ancestor[^"]*"/', $actual );
$this->assertRegExp( '/class="[^"]*cat-item-' . $child . '[^"]*current-cat-ancestor[^"]*"/', $actual );
$this->assertMatchesRegularExpression( '/class="[^"]*cat-item-' . $parent . '[^"]*current-cat-ancestor[^"]*"/', $actual );
$this->assertMatchesRegularExpression( '/class="[^"]*cat-item-' . $child . '[^"]*current-cat-ancestor[^"]*"/', $actual );
$this->assertNotRegExp( '/class="[^"]*cat-item-' . $grandchild . '[^"]*current-cat-ancestor[^"]*"/', $actual );
$this->assertNotRegExp( '/class="[^"]*cat-item-' . $child2 . '[^"]*current-cat-ancestor[^"]*"/', $actual );
}

View File

@ -18,7 +18,7 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase {
$button = '<input name="foo-name" type="submit" id="foo-id" class="foo-class" value="foo-label" />';
$hidden = get_comment_id_fields( $p );
$this->assertRegExp( '|<p class="form\-submit">\s*' . $button . '\s*' . $hidden . '\s*|', $form );
$this->assertMatchesRegularExpression( '|<p class="form\-submit">\s*' . $button . '\s*' . $hidden . '\s*|', $form );
}
public function test_custom_submit_button() {
@ -53,7 +53,7 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase {
$button = '<input name="foo-name" type="submit" id="foo-id" class="foo-class" value="foo-label" />';
$hidden = get_comment_id_fields( $p );
$this->assertRegExp( '|<p class="my\-custom\-submit\-field">\s*' . $button . '\s*' . $hidden . '\s*|', $form );
$this->assertMatchesRegularExpression( '|<p class="my\-custom\-submit\-field">\s*' . $button . '\s*' . $hidden . '\s*|', $form );
}
/**
@ -75,7 +75,7 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase {
$button = '<input name="foo-name" type="submit" id="foo-id" class="foo-class" value="foo-label" />';
$hidden = get_comment_id_fields( $p );
$this->assertRegExp( '|<p class="form\-submit">\s*' . $button . '\s*' . $hidden . '\s*|', $form );
$this->assertMatchesRegularExpression( '|<p class="form\-submit">\s*' . $button . '\s*' . $hidden . '\s*|', $form );
}
public function filter_comment_form_defaults( $defaults ) {
@ -102,7 +102,7 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase {
remove_filter( 'option_show_comments_cookies_opt_in', '__return_true' );
$this->assertRegExp( '|<p class="comment\-form\-cookies\-consent">.*?</p>|', $form );
$this->assertMatchesRegularExpression( '|<p class="comment\-form\-cookies\-consent">.*?</p>|', $form );
}
/**

View File

@ -770,7 +770,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
if ( $post_types ) {
foreach ( $post_types as $type ) {
$this->assertStringContainsString( 'available-menu-items-post_type-' . esc_attr( $type->name ), $template );
$this->assertRegExp( '#<h4 class="accordion-section-title".*>\s*' . esc_html( $type->labels->name ) . '#', $template );
$this->assertMatchesRegularExpression( '#<h4 class="accordion-section-title".*>\s*' . esc_html( $type->labels->name ) . '#', $template );
$this->assertStringContainsString( 'data-type="post_type"', $template );
$this->assertStringContainsString( 'data-object="' . esc_attr( $type->name ) . '"', $template );
$this->assertStringContainsString( 'data-type_label="' . esc_attr( $type->labels->singular_name ) . '"', $template );
@ -781,7 +781,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
if ( $taxonomies ) {
foreach ( $taxonomies as $tax ) {
$this->assertStringContainsString( 'available-menu-items-taxonomy-' . esc_attr( $tax->name ), $template );
$this->assertRegExp( '#<h4 class="accordion-section-title".*>\s*' . esc_html( $tax->labels->name ) . '#', $template );
$this->assertMatchesRegularExpression( '#<h4 class="accordion-section-title".*>\s*' . esc_html( $tax->labels->name ) . '#', $template );
$this->assertStringContainsString( 'data-type="taxonomy"', $template );
$this->assertStringContainsString( 'data-object="' . esc_attr( $tax->name ) . '"', $template );
$this->assertStringContainsString( 'data-type_label="' . esc_attr( $tax->labels->singular_name ) . '"', $template );
@ -789,7 +789,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
}
$this->assertStringContainsString( 'available-menu-items-custom_type', $template );
$this->assertRegExp( '#<h4 class="accordion-section-title".*>\s*Custom#', $template );
$this->assertMatchesRegularExpression( '#<h4 class="accordion-section-title".*>\s*Custom#', $template );
$this->assertStringContainsString( 'data-type="custom_type"', $template );
$this->assertStringContainsString( 'data-object="custom_object"', $template );
$this->assertStringContainsString( 'data-type_label="Custom Type"', $template );
@ -1013,7 +1013,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
$this->assertSame( $save_action_count + 1, did_action( 'customize_save_nav_menus_created_posts' ) );
foreach ( $drafted_post_ids as $post_id ) {
$this->assertSame( 'publish', get_post_status( $post_id ) );
$this->assertRegExp( '/^auto-draft-\d+$/', get_post( $post_id )->post_name );
$this->assertMatchesRegularExpression( '/^auto-draft-\d+$/', get_post( $post_id )->post_name );
$this->assertEmpty( get_post_meta( $post_id, '_customize_draft_post_name', true ) );
}

View File

@ -610,8 +610,8 @@ class Tests_WP_Customize_Widgets extends WP_UnitTestCase {
$params = $control->json();
$this->assertSame( 'widget_form', $params['type'] );
$this->assertRegExp( '#^<li[^>]+>\s*</li>$#', $params['content'] );
$this->assertRegExp( '#^<div[^>]*class=\'widget\'[^>]*#s', $params['widget_control'] );
$this->assertMatchesRegularExpression( '#^<li[^>]+>\s*</li>$#', $params['content'] );
$this->assertMatchesRegularExpression( '#^<div[^>]*class=\'widget\'[^>]*#s', $params['widget_control'] );
$this->assertStringContainsString( '<div class="widget-content"></div>', $params['widget_control'] );
$this->assertStringNotContainsString( '<input class="widefat"', $params['widget_control'] );
$this->assertStringContainsString( '<input class="widefat"', $params['widget_content'] );

View File

@ -713,7 +713,7 @@ class Tests_Date_Query extends WP_UnitTestCase {
// $compare value is floating point - use regex to account for
// varying precision on different PHP installations.
$this->assertRegExp( "/DATE_FORMAT\( post_date, '%H\.%i' \) = 5\.150*/", $wpdb->remove_placeholder_escape( $found ) );
$this->assertMatchesRegularExpression( "/DATE_FORMAT\( post_date, '%H\.%i' \) = 5\.150*/", $wpdb->remove_placeholder_escape( $found ) );
}
public function test_build_time_query_hour_minute_second() {
@ -724,7 +724,7 @@ class Tests_Date_Query extends WP_UnitTestCase {
// $compare value is floating point - use regex to account for
// varying precision on different PHP installations.
$this->assertRegExp( "/DATE_FORMAT\( post_date, '%H\.%i%s' \) = 5\.15350*/", $wpdb->remove_placeholder_escape( $found ) );
$this->assertMatchesRegularExpression( "/DATE_FORMAT\( post_date, '%H\.%i%s' \) = 5\.15350*/", $wpdb->remove_placeholder_escape( $found ) );
}
public function test_build_time_query_minute_second() {
@ -735,7 +735,7 @@ class Tests_Date_Query extends WP_UnitTestCase {
// $compare value is floating point - use regex to account for
// varying precision on different PHP installations.
$this->assertRegExp( "/DATE_FORMAT\( post_date, '0\.%i%s' \) = 0\.15350*/", $wpdb->remove_placeholder_escape( $found ) );
$this->assertMatchesRegularExpression( "/DATE_FORMAT\( post_date, '0\.%i%s' \) = 0\.15350*/", $wpdb->remove_placeholder_escape( $found ) );
}
/**

View File

@ -1207,7 +1207,7 @@ class Tests_Functions extends WP_UnitTestCase {
$ids = array();
for ( $i = 0; $i < 20; $i += 1 ) {
$id = wp_unique_id( 'foo-' );
$this->assertRegExp( '/^foo-\d+$/', $id );
$this->assertMatchesRegularExpression( '/^foo-\d+$/', $id );
$ids[] = $id;
}
$this->assertSame( $ids, array_unique( $ids ) );

View File

@ -925,7 +925,7 @@ class Tests_Meta_Query extends WP_UnitTestCase {
$sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
// Use regex because we don't care about the whitespace before OR.
$this->assertRegExp( "/{$wpdb->postmeta}\.meta_key = \'exclude\'\s+OR/", $sql['where'] );
$this->assertMatchesRegularExpression( "/{$wpdb->postmeta}\.meta_key = \'exclude\'\s+OR/", $sql['where'] );
$this->assertStringNotContainsString( "{$wpdb->postmeta}.post_id IS NULL", $sql['where'] );
}

View File

@ -34,7 +34,7 @@ class Tests_Post_GetPostsByAuthorSql extends WP_UnitTestCase {
public function test_full_true() {
$maybe_string = get_posts_by_author_sql( 'post', true );
$this->assertRegExp( '/^WHERE /', $maybe_string );
$this->assertMatchesRegularExpression( '/^WHERE /', $maybe_string );
}
public function test_full_false() {

View File

@ -460,7 +460,7 @@ class Tests_Post_Nav_Menu extends WP_UnitTestCase {
);
// The markup should include whitespace between <li>'s.
$this->assertRegExp( '/\s<li.*>|<\/li>\s/U', $menu );
$this->assertMatchesRegularExpression( '/\s<li.*>|<\/li>\s/U', $menu );
$this->assertNotRegExp( '/<\/li><li.*>/U', $menu );
// Whitespace suppressed.
@ -474,7 +474,7 @@ class Tests_Post_Nav_Menu extends WP_UnitTestCase {
// The markup should not include whitespace around <li>'s.
$this->assertNotRegExp( '/\s<li.*>|<\/li>\s/U', $menu );
$this->assertRegExp( '/><li.*>|<\/li></U', $menu );
$this->assertMatchesRegularExpression( '/><li.*>|<\/li></U', $menu );
}
/*

View File

@ -340,7 +340,7 @@ NO;
)
);
$this->assertRegExp( '/<select[^>]+class=\'bar\'/', $found );
$this->assertMatchesRegularExpression( '/<select[^>]+class=\'bar\'/', $found );
}
/**
@ -419,13 +419,13 @@ NO;
$menu = wp_nav_menu( array( 'echo' => false ) );
// After falling back, the 'before' argument should be set and output as '<ul>'.
$this->assertRegExp( '/<div class="menu"><ul>/', $menu );
$this->assertMatchesRegularExpression( '/<div class="menu"><ul>/', $menu );
// After falling back, the 'after' argument should be set and output as '</ul>'.
$this->assertRegExp( '/<\/ul><\/div>/', $menu );
$this->assertMatchesRegularExpression( '/<\/ul><\/div>/', $menu );
// After falling back, the markup should include whitespace around <li>'s.
$this->assertRegExp( '/\s<li.*>|<\/li>\s/U', $menu );
$this->assertMatchesRegularExpression( '/\s<li.*>|<\/li>\s/U', $menu );
$this->assertNotRegExp( '/><li.*>|<\/li></U', $menu );
// No menus + wp_nav_menu() falls back to wp_page_menu(), this time without a container.
@ -437,7 +437,7 @@ NO;
);
// After falling back, the empty 'container' argument should still return a container element.
$this->assertRegExp( '/<div class="menu">/', $menu );
$this->assertMatchesRegularExpression( '/<div class="menu">/', $menu );
// No menus + wp_nav_menu() falls back to wp_page_menu(), this time without white-space.
$menu = wp_nav_menu(
@ -449,7 +449,7 @@ NO;
// After falling back, the markup should not include whitespace around <li>'s.
$this->assertNotRegExp( '/\s<li.*>|<\/li>\s/U', $menu );
$this->assertRegExp( '/><li.*>|<\/li></U', $menu );
$this->assertMatchesRegularExpression( '/><li.*>|<\/li></U', $menu );
}

View File

@ -795,7 +795,7 @@ class Tests_Query_Results extends WP_UnitTestCase {
$posts = $this->q->query( array( 'post_type' => 'any' ) );
$this->assertEmpty( $posts );
$this->assertRegExp( '#AND 1=0#', $this->q->request );
$this->assertMatchesRegularExpression( '#AND 1=0#', $this->q->request );
foreach ( array_keys( $wp_post_types ) as $slug ) {
$wp_post_types[ $slug ]->exclude_from_search = false;

View File

@ -129,8 +129,8 @@ class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase {
)
);
$this->assertRegExp( "|^<ul class='wp-tag-cloud' role='list'>|", $found );
$this->assertRegExp( "|</ul>\n|", $found );
$this->assertMatchesRegularExpression( "|^<ul class='wp-tag-cloud' role='list'>|", $found );
$this->assertMatchesRegularExpression( "|</ul>\n|", $found );
$this->assertStringContainsString( '>' . $tags[0]->name . '<', $found );
}
@ -184,8 +184,8 @@ class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase {
)
);
$this->assertRegExp( "|^<ul class='wp-tag-cloud' role='list'>|", $found );
$this->assertRegExp( "|</ul>\n|", $found );
$this->assertMatchesRegularExpression( "|^<ul class='wp-tag-cloud' role='list'>|", $found );
$this->assertMatchesRegularExpression( "|</ul>\n|", $found );
foreach ( $tags as $tag ) {
$this->assertStringContainsString( '>' . $tag->name . '<', $found );

View File

@ -798,7 +798,7 @@ class Tests_Widgets extends WP_UnitTestCase {
unregister_widget( 'WP_Widget_Text' );
$this->assertRegExp( '/<span class="special widget_text">/', $actual );
$this->assertMatchesRegularExpression( '/<span class="special widget_text">/', $actual );
}