Tests: Simplify some assertions in Tests_Media.

A number of assertions are checking for the number of times a hard-coded substring existed in a larger `$haystack`.

These assertions were using `preg_match_all()` to do so, but without actually using a regex.

In these cases, it is more performant (and simpler) to use the PHP native `substr_count()` function, which will yield the same result, without the overhead of regex parsing.

Reference: [https://www.php.net/manual/en/function.substr-count.php PHP Manual: substr_count()]

Follow-up to [711/tests], [38838], [42694], [53558].

Props jrf.
See #55652.

git-svn-id: https://develop.svn.wordpress.org/trunk@53790 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2022-07-28 22:37:00 +00:00
parent 9f5935c2e8
commit 7b43b08dd5

View File

@ -157,14 +157,14 @@ CAP;
)
);
$this->assertSame( 2, preg_match_all( '/wp-caption/', $result, $_r ) );
$this->assertSame( 1, preg_match_all( '/alignnone/', $result, $_r ) );
$this->assertSame( 1, preg_match_all( '/' . self::CAPTION . '/', $result, $_r ) );
$this->assertSame( 2, substr_count( $result, 'wp-caption' ) );
$this->assertSame( 1, substr_count( $result, 'alignnone' ) );
$this->assertSame( 1, substr_count( $result, self::CAPTION ) );
if ( current_theme_supports( 'html5', 'caption' ) ) {
$this->assertSame( 1, preg_match_all( '/width: 20/', $result, $_r ) );
$this->assertSame( 1, substr_count( $result, 'width: 20' ) );
} else {
$this->assertSame( 1, preg_match_all( '/width: 30/', $result, $_r ) );
$this->assertSame( 1, substr_count( $result, 'width: 30' ) );
}
}
@ -177,9 +177,9 @@ CAP;
'align' => '&myAlignment',
)
);
$this->assertSame( 1, preg_match_all( '/wp-caption &myAlignment/', $result, $_r ) );
$this->assertSame( 1, preg_match_all( '/id="myId"/', $result, $_r ) );
$this->assertSame( 1, preg_match_all( '/' . self::CAPTION . '/', $result, $_r ) );
$this->assertSame( 1, substr_count( $result, 'wp-caption &myAlignment' ) );
$this->assertSame( 1, substr_count( $result, 'id="myId"' ) );
$this->assertSame( 1, substr_count( $result, self::CAPTION ) );
}
public function test_img_caption_shortcode_with_old_format_and_class() {
@ -190,20 +190,19 @@ CAP;
'caption' => self::CAPTION,
)
);
$this->assertSame( 1, preg_match_all( '/wp-caption alignnone some-class another-class/', $result, $_r ) );
$this->assertSame( 1, substr_count( $result, 'wp-caption alignnone some-class another-class' ) );
}
public function test_new_img_caption_shortcode_with_html_caption() {
$result = img_caption_shortcode(
$result = img_caption_shortcode(
array(
'width' => 20,
'caption' => self::HTML_CONTENT,
)
);
$our_preg = preg_quote( self::HTML_CONTENT );
$this->assertSame( 1, preg_match_all( "~{$our_preg}~", $result, $_r ) );
$this->assertSame( 1, substr_count( $result, self::HTML_CONTENT ) );
}
public function test_new_img_caption_shortcode_new_format() {
@ -256,7 +255,7 @@ CAP;
self::IMG_CONTENT . self::HTML_CONTENT
);
$this->assertSame( 1, preg_match_all( '/aria-describedby="caption-myId"/', $result, $_r ) );
$this->assertSame( 1, substr_count( $result, 'aria-describedby="caption-myId"' ) );
}
public function test_add_remove_oembed_provider() {